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