-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextWidget.py
282 lines (256 loc) · 10.2 KB
/
TextWidget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#! /usr/bin/env python
# TextWidget - A simple text widget for PyGame
# Copyright (C) 2006 Mark Mruss <selsine@gmail.com>
# http://www.learningpython.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# If you find any bugs or have any suggestions email: selsine@gmail.com
# URL: http://www.learningpython.com
#
# Feel free to use this in whatever manner you want, but if you do end up using
# it, I would appreciate it if you sent me an email and let me know.
__author__ = "Mark Mruss <selsine@gmail.com>"
__version__ = "0.0"
__date__ = "Date: 2006/12/13"
__copyright__ = "Copyright (c) 2006 Mark Mruss"
__license__ = "GPL"
import pygame
from pygame.locals import *
TEXT_WIDGET_CLICK = USEREVENT + 1
class TextWidget(object):
""" This is a helper class for handling text in PyGame. It performs
some basic highlighting and tells you when the text has been clicked.
This is just one of the many ways to handle your text.
This is a new-style class and I am somewhat new to them so hopefully it
all works.
"""
# Hand Cursor
__hand_cursor_string = (
" XX ",
" X..X ",
" X..X ",
" X..X ",
" X..XXXXX ",
" X..X..X.XX ",
" XX X..X..X.X.X ",
"X..XX.........X ",
"X...X.........X ",
" X.....X.X.X..X ",
" X....X.X.X..X ",
" X....X.X.X.X ",
" X...X.X.X.X ",
" X.......X ",
" X....X.X ",
" XXXXX XX ")
__hcurs, __hmask = pygame.cursors.compile(__hand_cursor_string, ".", "X")
__hand = ((16, 16), (5, 1), __hcurs, __hmask)
# Text
def __get_text(self):
return self.__m_text
def __set_text(self, text):
if (self.__m_text != text):
self.__m_text = text
self.update_surface()
def __del_text(self):
del self.__m_text
def __doc_text(self):
return "The text to be displayed by the text widget"
text = property(__get_text, __set_text, __del_text, __doc_text)
# Colour
def __get_colour(self):
return self.__m_colour
def __set_colour(self, colour):
if (self.__m_colour != colour):
self.__m_colour = colour
self.update_surface()
colour = property(__get_colour, __set_colour)
# Size
def __get_size(self):
return self.__m_size
def __set_size(self, size):
if (self.__m_size != size):
self.__m_size = size
self.create_font()
size = property(__get_size, __set_size)
# Font Filename
def __get_font_filename(self):
return self.__m_font_filename
def __set_font_filename(self, font_filename):
if (self.__m_font_filename != font_filename):
self.__m_font_filename = font_filename
self.create_font()
font_filename = property(__get_font_filename, __set_font_filename)
# Highlight
def __get_highlight(self):
return self.__m_highlight
def __set_highlight(self, highlight):
if (not(self.__m_highlight == highlight)):
# Save the bold_rect
if (self.__m_highlight):
self.bold_rect = self.rect.copy()
self.__m_highlight = highlight
# update the cursor
self.update_cursor()
if (highlight):
self.size += self.highlight_increase
else:
self.size -= self.highlight_increase
if (self.highlight_increase == 0):
self.create_font()
highlight = property(__get_highlight, __set_highlight)
# Show Highlight Cursor
def __get_highlight_cursor(self):
return self.__m_highlight_cursor
def __set_highlight_cursor(self, highlight_cursor):
if (self.__m_highlight_cursor != highlight_cursor):
self.__m_highlight_cursor = highlight_cursor
self.update_cursor()
highlight_cursor = property(__get_highlight_cursor, __set_highlight_cursor)
def __init__(self, text="", colour=(0,0,0), size=32
, highlight_increase = 20, font_filename=None
, show_highlight_cursor = True):
""" Initialize the TextWidget
@param text = "" - string - The text for the text widget
@param colour = (0,0,0) - The colour of the text
@param size = 32 - number - The size of the text
@param highlight_increase - number - How large do we want the
text to grow when it is highlighted?
@param font_filename = None - string the patht to the font file
to use, None to use the default pygame font.
@param show_highlight_cursor = True - boolean - Whether or not to change
the cursor when the text is highlighted. The cursor will turn into
a hand if this is true.
"""
# inits
self.bold_rect = None
self.highlight_increase = highlight_increase
self.tracking = False
self.rect = None
# property inits
self.__m_text = None
self.__m_colour = None
self.__m_size = None
self.__m_font_filename = None
self.__m_highlight = False
self.__m_font = None
self.__m_highlight_cursor = False
self.__m_rect = None
self.text = text
self.colour = colour
self.size = size
self.font_filename = font_filename
self.highlight = False
self.highlight_cursor = show_highlight_cursor
self.create_font()
def __str__(self):
return "TextWidget: %s at %s" % (self.text, self.rect)
def update_cursor(self):
if (self.highlight_cursor):
if (self.highlight):
pygame.mouse.set_cursor(*self.__hand)
else:
pygame.mouse.set_cursor(*pygame.cursors.arrow)
def create_font(self):
""" Create the internal font, using the current settings
"""
if (self.size):
self.__m_font = pygame.font.Font(self.font_filename
, self.size)
self.update_surface()
def update_surface(self):
""" Update the current surface, basically render the
text using the current settings.
"""
if (self.__m_font):
self.__m_font.set_bold(self.highlight)
self.image = self.__m_font.render(self.text
, True
, self.colour)
if (self.rect):
# Used the current rects center point
self.rect = self.image.get_rect(center=self.rect.center)
else:
self.rect = self.image.get_rect()
def draw(self, screen):
""" Draw yourself text widget
@param screen - pygame.Surface - The surface that we will draw to
@returns - pygame.rect - If drawing has occurred this is the
rect that we drew to. None if no drawing has occurerd."""
rect_return = None
if ((self.image) and (self.rect)):
rect_return = self.rect
# Draw the text
screen.blit(self.image, rect_return)
return rect_return
def erase(self, screen, background):
""" Erase yourself text widget
@param screen - pygame.Surface - The surface that we will erase from
@param background - pygame.Surface - The background we will draw with
@returns - pygame.rect - If drawing has occurred this is the
rect that we drew to. None if no drawing has occurerd."""
rect_return = None
if ((self.image) and (self.rect)):
if (self.bold_rect):
# We may need to overwrite the bold text size
# This gets rid of leftover text when moving from
# bold text to non-bold text.
rect_return = self.bold_rect
# Set to None, since we only need to do this
# once.
self.bold_rect = None
else:
rect_return = self.rect
# Draw the background
screen.blit(background, rect_return, rect_return)
return rect_return
def on_mouse_button_down(self, event):
""" Called by the main application when the
MOUSEBUTTONDOWN event fires.
@param event - Pygame Event object
MOUSEBUTTONDOWN pos, button
"""
# Check for collision
self.tracking = False
if (self.rect.collidepoint(event.pos)):
self.tracking = True
def on_mouse_button_up(self, event):
""" Called by the main application when the
MOUSEBUTTONUP event fires.
@param event - Pygame Event object
MOUSEBUTTONUP pos, button
"""
# Check for collision
if ((self.tracking) and (self.rect.collidepoint(event.pos))):
# Not Tracking anymore
self.tracking = False
self.on_mouse_click(event)
def on_mouse_click(self, event):
""" Called by the main application when the
MOUSEBUTTONDOWN event fires, and the text widget
has been clicked on. You can either let
this post the event (default) or you can override this
function call in your app.
ie. myTextWidget.on_mouse_click = my_click_handler
@param event - Pygame Event object
MOUSEBUTTONDOWN pos, button
"""
# Create the TEXT_WIDGET_CLICK event
event_attrib = {}
event_attrib["button"] = event.button
event_attrib["pos"] = event.pos
event_attrib["text_widget"] = self
e = pygame.event.Event(TEXT_WIDGET_CLICK, event_attrib)
pygame.event.post(e)