comparison orpg/map/_lines.py @ 0:4385a7d0efd1 grumpy-goblin

Deleted and repushed it with the 'grumpy-goblin' branch. I forgot a y
author sirebral
date Tue, 14 Jul 2009 16:41:58 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4385a7d0efd1
1 from math import sqrt
2
3 import wx
4
5 import orpg.dirpath
6 from orpg.orpgCore import *
7
8 from _object import MapObject
9
10 class MapLine(MapObject):
11 def __init__(self, canvas, start=wx.Point(0,0), width=1, color="#000000", points=[]):
12 MapObject.__init__(self, canvas=canvas)
13 self.start = wx.Point(start[0], start[1])
14 self.color = color
15 self.points = points
16 self.width = width
17
18 r, g, b = self.RGBHex.rgb_tuple(self.color)
19 self.hcolor = self.RGBHex.hexstring(r^255, g^255, b^255)
20
21 self.id = 'line-' + self.canvas.GetNewObjectId()
22
23
24 def Draw(self, dc):
25 path = dc.CreatePath()
26
27 if not self.highlighed:
28 c = self.color
29 else:
30 c = self.hcolor
31 r, g, b = self.RGBHex.rgb_tuple(c)
32
33 pen = wx.Pen(wx.Color(r, g, b, 0), self.width)
34 if self.IsShown():
35 pen = wx.Pen(wx.Color(r, g, b, 255), self.width)
36 elif self.canvas.toolWnd.gmToolBar.IsShown():
37 pen = wx.Pen(wx.Color(r, g, b, 40), self.width)
38 dc.SetPen(pen)
39
40 dc.DrawLines(self.points)
41
42 dc.SetBrush(wx.NullBrush)
43 dc.SetPen(wx.NullPen)
44
45 if self.selected:
46 self.DrawSelection(dc)
47
48 def DrawSelection(self, dc):
49 dc.SetBrush(wx.GREEN_BRUSH)
50 dc.SetPen(wx.GREEN_PEN)
51 path = dc.CreatePath()
52
53 dc.DrawPath(path)
54
55 dc.SetBrush(wx.NullBrush)
56 dc.SetPen(wx.NullPen)
57
58 def InObject(self, pos):
59 for point in self.points:
60 xd = (point[0]-pos.x)*(point[0]-pos.x)
61 yd = (point[1]-pos.y)*(point[1]-pos.y)
62 distance = sqrt(xd+yd)
63
64 if distance <= self.width+1:
65 return True
66
67 return False
68
69 def GetName(self):
70 return self.id + ' Color:' + self.color
71
72 def ShowProperties(self, event):
73 dlg = wx.Dialog(self.canvas, wx.ID_ANY, "Circle Properties")
74 sizer = wx.BoxSizer(wx.HORIZONTAL)
75
76 radius = wx.TextCtrl(dlg, wx.ID_ANY)
77 radius.SetValue(str(self.radius))
78
79 colorbtn = wx.Button(dlg, wx.ID_ANY, "Color")
80 colorbtn.SetForegroundColour(self.hcolor)
81
82 def ColorBtn(event):
83 newcolor = self.RGBHex.do_hex_color_dlg(self.canvas)
84 if newcolor == None:
85 return
86
87 colorbtn.SetForegroundColour(newcolor)
88 dlg.Unbind(wx.EVT_BUTTON)
89
90 dlg.Bind(wx.EVT_BUTTON, ColorBtn, colorbtn)
91
92 sizer.Add(wx.StaticText(dlg, wx.ID_ANY, "Radius:"), 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 2)
93 sizer.Add(radius, 0, wx.EXPAND|wx.ALL, 3)
94 sizer.Add(colorbtn, 0, wx.ALL, 2)
95 sizer.Add(wx.Button(dlg, wx.ID_OK), 0, wx.ALL, 3)
96
97 dlg.SetSizer(sizer)
98 dlg.SetAutoLayout(True)
99 dlg.Fit()
100 dlg.Show()
101
102 if dlg.ShowModal() == wx.ID_OK:
103 self.radius = int(radius.GetValue())
104 r,g,b = colorbtn.GetForegroundColour().Get()
105 self.color = self.RGBHex.hexstring(r, g, b)
106 self.hcolor = self.RGBHex.hexstring(r^255, g^255, b^255)
107 self.Update(send=True, action="update")
108
109
110 def OnLeftDown(self, pos):
111 self.lastPoint = pos
112 self.start = pos
113 self.points.append((pos.x, pos.y))
114
115 def OnMotion(self, pos):
116 dc = wx.ClientDC(self.canvas)
117 self.canvas.PrepareDC(dc)
118
119 r,g,b = self.RGBHex.rgb_tuple(self.canvas.whiteboardColor)
120 pen = wx.Pen(wx.Color(r,g,b,255), int(self.canvas.toolWnd.LineWidth.GetStringSelection()))
121 dc.SetPen(pen)
122
123 xd = (self.lastPoint.x-pos.x)*(self.lastPoint.x-pos.x)
124 yd = (self.lastPoint.y-pos.y)*(self.lastPoint.y-pos.y)
125 distance = sqrt(xd+yd)
126
127 if distance > 5:
128 if self.canvas.toolWnd.currentLine == 'Free':
129 self.points.append((pos.x, pos.y))
130 self.lastPoint = pos
131 dc.DrawLines(self.points)
132 else:
133 dc.SetLogicalFunction(wx.INVERT)
134 dc.DrawLine(self.start.x, self.start.y, self.lastPoint.x, self.lastPoint.y)
135 dc.DrawLine(self.start.x, self.start.y, pos.x, pos.y)
136 dc.SetLogicalFunction(wx.COPY)
137 dc.DrawLines(self.points)
138 self.lastPoint = pos
139
140 dc.SetPen(wx.NullPen)
141
142 def OnLeftUp(self, pos):
143 if self.canvas.toolWnd.currentLine == 'Free' and len(self.points) > 2:
144 self.points.append((pos.x, pos.y))
145 self.canvas.zOrder['front'].append(MapLine(self.canvas, self.points[0], int(self.canvas.toolWnd.LineWidth.GetStringSelection()), self.canvas.whiteboardColor, self.points))
146 self.start = wx.Point(0,0)
147 self.points = []
148
149 def OnLeftDClick(self, pos):
150 if self.canvas.toolWnd.currentLine == 'Poly' and len(self.points) > 2:
151 self.points.append((pos.x, pos.y))
152 self.canvas.zOrder['front'].append(MapLine(self.canvas, self.points[0], int(self.canvas.toolWnd.LineWidth.GetStringSelection()), self.canvas.whiteboardColor, self.points))
153 self.points = []
154 self.start = wx.Point(0,0)
155
156 def _toxml(self, action="update"):
157 return ''