comparison orpg/map/_circles.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 MapCircle(MapObject):
11 def __init__(self, canvas, center=wx.Point(0,0), radius=0, color="#000000"):
12 MapObject.__init__(self, canvas=canvas)
13 self.start = center
14 self.radius = int(radius)
15 self.color = color
16
17 r, g, b = self.RGBHex.rgb_tuple(self.color)
18 self.hcolor = self.RGBHex.hexstring(r^255, g^255, b^255)
19
20 self.id = 'circle-' + self.canvas.GetNewObjectId()
21
22
23 def Draw(self, dc):
24 path = dc.CreatePath()
25
26 if not self.highlighed:
27 c = self.color
28 else:
29 c = self.hcolor
30 r, g, b = self.RGBHex.rgb_tuple(c)
31
32 pen = wx.TRANSPARENT_PEN
33 brush = wx.TRANSPARENT_BRUSH
34 if self.IsShown():
35 brush = wx.Brush(wx.Color(r, g, b, 128))
36 pen = wx.Pen(wx.Color(r, g, b, 128))
37 elif self.canvas.toolWnd.gmToolBar.IsShown():
38 brush = wx.Brush(wx.Color(r, g, b, 40))
39 pen = wx.Pen(wx.Color(r, g, b, 40))
40 font = wx.Font(12, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
41 dc.SetFont(font, wx.RED)
42 w, h = dc.GetTextExtent("Hidden")
43 dc.DrawText("Hidden", self.start.x-(w/2), self.start.y-(h/2), dc.CreateBrush(wx.WHITE_BRUSH))
44
45 dc.SetBrush(brush)
46 dc.SetPen(pen)
47
48 path.AddCircle(self.start.x, self.start.y, self.radius)
49 path.CloseSubpath()
50 dc.DrawPath(path)
51
52 dc.SetBrush(wx.NullBrush)
53 dc.SetPen(wx.NullPen)
54
55 if self.selected:
56 self.DrawSelection(dc)
57
58 def DrawSelection(self, dc):
59 dc.SetBrush(wx.GREEN_BRUSH)
60 dc.SetPen(wx.GREEN_PEN)
61 path = dc.CreatePath()
62
63 path.AddRectangle(self.start.x-self.radius, self.start.y-self.radius, 5, 5)
64 path.AddRectangle(self.start.x-self.radius, self.start.y+self.radius, 5, 5)
65 path.AddRectangle(self.start.x+self.radius, self.start.y-self.radius, 5, 5)
66 path.AddRectangle(self.start.x+self.radius, self.start.y+self.radius, 5, 5)
67
68 path.MoveToPoint(self.start.x, self.start.y)
69 path.AddLineToPoint(self.start.x-10, self.start.y)
70 path.MoveToPoint(self.start.x, self.start.y)
71 path.AddLineToPoint(self.start.x, self.start.y+10)
72 path.MoveToPoint(self.start.x, self.start.y)
73 path.AddLineToPoint(self.start.x+10, self.start.y)
74 path.MoveToPoint(self.start.x, self.start.y)
75 path.AddLineToPoint(self.start.x, self.start.y-10)
76
77 dc.DrawPath(path)
78
79 dc.SetBrush(wx.NullBrush)
80 dc.SetPen(wx.NullPen)
81
82 def InObject(self, pos):
83 xd = (self.start.x-pos.x)*(self.start.x-pos.x)
84 yd = (self.start.y-pos.y)*(self.start.y-pos.y)
85 distance = sqrt(xd+yd)
86
87 if distance <= self.radius:
88 return True
89
90 return False
91
92 def GetName(self):
93 return 'Circle: ' + str(self.id) + ' Radius:' + str(self.radius) + ' Color:' + self.color
94
95 def ShowProperties(self, event):
96 dlg = wx.Dialog(self.canvas, wx.ID_ANY, "Circle Properties")
97 sizer = wx.BoxSizer(wx.HORIZONTAL)
98
99 radius = wx.TextCtrl(dlg, wx.ID_ANY)
100 radius.SetValue(str(self.radius))
101
102 colorbtn = wx.Button(dlg, wx.ID_ANY, "Color")
103 colorbtn.SetForegroundColour(self.hcolor)
104
105 def ColorBtn(event):
106 newcolor = self.RGBHex.do_hex_color_dlg(self.canvas)
107 if newcolor == None:
108 return
109
110 colorbtn.SetForegroundColour(newcolor)
111 dlg.Unbind(wx.EVT_BUTTON)
112
113 dlg.Bind(wx.EVT_BUTTON, ColorBtn, colorbtn)
114
115 sizer.Add(wx.StaticText(dlg, wx.ID_ANY, "Radius:"), 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 2)
116 sizer.Add(radius, 0, wx.EXPAND|wx.ALL, 3)
117 sizer.Add(colorbtn, 0, wx.ALL, 2)
118 sizer.Add(wx.Button(dlg, wx.ID_OK), 0, wx.ALL, 3)
119
120 dlg.SetSizer(sizer)
121 dlg.SetAutoLayout(True)
122 dlg.Fit()
123 dlg.Show()
124
125 if dlg.ShowModal() == wx.ID_OK:
126 self.radius = int(radius.GetValue())
127 r,g,b = colorbtn.GetForegroundColour().Get()
128 self.color = self.RGBHex.hexstring(r, g, b)
129 self.hcolor = self.RGBHex.hexstring(r^255, g^255, b^255)
130 self.Update(send=True, action="update")
131
132
133 def OnLeftDown(self, pos):
134 self.start = pos
135 self.lastRadius = 0
136 self.radius = 0
137
138 def OnMotion(self, pos):
139 dc = wx.ClientDC(self.canvas)
140 self.canvas.PrepareDC(dc)
141 dc.SetLogicalFunction(wx.EQUIV)
142 dc.SetUserScale(self.canvas.zoomScale, self.canvas.zoomScale)
143
144
145 if self.radius > 0:
146 dc.DrawCircle(self.start.x, self.start.y, self.radius)
147
148 xd = (self.start.x-pos.x)*(self.start.x-pos.x)
149 yd = (self.start.y-pos.y)*(self.start.y-pos.y)
150 self.radius = sqrt(xd+yd)
151
152 #self.lastRadius = self.radius
153 dc.DrawCircle(self.start.x, self.start.y, self.radius)
154
155 def OnLeftUp(self, pos):
156 xd = (self.start.x-pos.x)*(self.start.x-pos.x)
157 yd = (self.start.y-pos.y)*(self.start.y-pos.y)
158 radius = sqrt(xd+yd)
159
160 if radius > 15:
161 self.canvas.zOrder['front'].append(MapCircle(self.canvas, self.start, radius, self.canvas.whiteboardColor))
162 self.Update(send=True, action='new')
163 self.lastRadius = 0
164 self.start = wx.Point(0,0)
165 self.radius = 0
166
167 def _toxml(self, action="update"):
168 return ''