Merge lp:~codeforger/simplegc/label into lp:simplegc

Proposed by Michael Rochester
Status: Merged
Approved by: Sam Bull
Approved revision: 209
Merged at revision: 216
Proposed branch: lp:~codeforger/simplegc/label
Merge into: lp:simplegc
Diff against target: 227 lines (+126/-44) (has conflicts)
2 files modified
sgc/example/test.py (+9/-2)
sgc/widgets/label.py (+117/-42)
Text conflict in sgc/example/test.py
To merge this branch: bzr merge lp:~codeforger/simplegc/label
Reviewer Review Type Date Requested Status
Sam Bull Approve
Review via email: mp+100381@code.launchpad.net

Description of the change

NOt an up to date branch, I couldent work out how )':

To post a comment you must log in.
Revision history for this message
Sam Bull (dreamsorcerer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'sgc/example/test.py'
2--- sgc/example/test.py 2012-03-31 20:18:37 +0000
3+++ sgc/example/test.py 2012-04-02 10:33:19 +0000
4@@ -49,8 +49,8 @@
5
6
7 # Title
8-title = sgc.widgets.Label(text="Simple Game Code " + ver_no,
9- font=sgc.Font["title"], color=sgc.Font.col)
10+title = sgc.widgets.Label(label="Simple Game Code " + ver_no,
11+ font=sgc.Font["title"], label_col=sgc.Font.col,)
12 title.rect.center = (screen.rect.w/2, 40)
13 title.add()
14
15@@ -108,10 +108,17 @@
16 radio_box = sgc.widgets.VBox(widgets=(radio1, radio2, radio3), pos=(40,320))
17 radio_box.add(2)
18
19+<<<<<<< TREE
20 # Toggle Button
21 toggle = sgc.widgets.Toggle(label="Toggle", pos=(200,320))
22 toggle.add(3)
23
24+=======
25+# Selectable Label
26+label1 = sgc.widgets.Label(label="Your confirmation code is:AD14-F4GH-SW33",
27+ pos=(160,320), selectable=True)
28+label1.add()
29+>>>>>>> MERGE-SOURCE
30 while True:
31 time = clock.tick(30)
32 for event in pygame.event.get():
33
34=== modified file 'sgc/widgets/label.py'
35--- sgc/widgets/label.py 2012-03-26 13:32:19 +0000
36+++ sgc/widgets/label.py 2012-04-02 10:33:19 +0000
37@@ -1,73 +1,148 @@
38 #!/usr/bin/env python
39
40-# Copyright (C) 2010-2012 Sam Bull
41+# Copyright (C) 2010-2012 Michael Rochester
42
43 """
44 Label to display information to the user.
45
46 """
47
48+import pygame.mouse
49+from pygame.locals import *
50+
51 from _locals import *
52 from base_widget import Simple
53
54+
55 class Label(Simple):
56
57 """
58 Label
59
60 Attributes:
61- text: Text displayed in label. Can be assigned as a shortcut for
62- ``config(text=)`` with no second paramenter.
63-
64+ label: ``str`` displayed in label. Can be assigned as a shortcut for
65+ ``config(label=)`` with no second paramenter.
66 """
67-
68- _settings_default = {"text": "", "color": Font.col, "font": Font["widget"]}
69+ _settings_default = {"label": "", "label_col": Font.col,
70+ "font": Font["widget"],
71+ "col_selection": (118, 45, 215),
72+ "selectable": False}
73
74 def _config(self, **kwargs):
75 """
76- text: Either ``str`` containing text to be displayed or
77- ``tuple`` containing two strings. First string is text to
78- be displayed, second string is rect attribute to be used
79- for position. Defaults to 'topleft' if not passing a tuple.
80- color: ``tuple`` (r,g,b) Text colour.
81+ label: Either ``str`` containing text to be displayed.
82+ label_col: ``tuple`` (r,g,b) Text colour.
83 font: Font object the label will render with.
84-
85+ selectable ``bool`` if true, the text will be selectable.
86+ col_selection ``tuple`` (r,g,b) colour of selection box.
87 """
88- if "text" in kwargs:
89- if isinstance(kwargs["text"], str):
90- self._settings["text"] = kwargs["text"]
91- else:
92- self._settings["text"] = kwargs["text"][0]
93- self._temp_pos = kwargs["text"][1]
94- if "color" in kwargs:
95- self._settings["color"] = kwargs["color"]
96+ if "label" in kwargs:
97+ self._settings["label"] = [kwargs["label"],None]
98+ if "label_col" in kwargs:
99+ self._settings["label_col"] = kwargs["label_col"]
100 if "font" in kwargs:
101 self._settings["font"] = kwargs["font"]
102+ if "selectable" in kwargs:
103+ self._settings["selectable"] = kwargs["selectable"]
104+ self._can_focus = True
105+ if "col_selection" in kwargs:
106+ self._settings["col_selection"] = kwargs["col_selection"]
107
108+ strings = pygame.cursors.textmarker_strings
109+ cursor = pygame.cursors.compile(strings)
110+ size = (len(strings[0]), len(strings))
111+ hotspot = (size[0]/2, size[1]/2)
112+ self.mouse = (cursor, size, hotspot,pygame.mouse.get_cursor())
113+ self._draw()
114+
115 def _draw(self, draw):
116- if hasattr(self, "_temp_pos"):
117- pos = getattr(self.rect, self._temp_pos)
118- elif hasattr(self, "image"):
119- pos = self.rect.topleft
120+ label = Simple(self._settings["font"].render\
121+ (self._settings["label"][0], False,
122+ self._settings["label_col"]))
123+
124+ self._settings["label"][1] = label
125+
126+ if not hasattr(self, "image"):
127+ self._create_base_images((label.rect.w+6, label.rect.h))
128+
129+ self._images["image"].fill(0)
130+
131+ if self._settings["selectable"]:
132+ self.char_width = [3]
133+ for c in self._settings["label"][0]:
134+ char = Simple(self._settings["font"].render\
135+ (c, True,self._settings["label_col"]))
136+ self.char_width.append(self.char_width[-1] + char.rect.w)
137+
138+ def _event(self, event):
139+ if event.type == MOUSEBUTTONDOWN and event.button == 1:
140+ self.mouse_x = event.pos[0] - self.pos_abs[0]
141+ self.selected = None
142+ for x in self.char_width:
143+ if x > self.mouse_x and self.selected is None:
144+ self.selected = [self.char_width.index(x)-1,
145+ self.char_width.index(x)-1]
146+
147+ elif event.type == MOUSEMOTION and event.buttons == (1,0,0):
148+ self.mouse_x = event.pos[0] - self.pos_abs[0]
149+ for x in self.char_width:
150+ if x > self.mouse_x and\
151+ self.char_width[self.char_width.index(x)-1] < self.mouse_x:
152+ self.selected[1] = self.char_width.index(x)\
153+ if self.selected[1] >\
154+ self.selected[0] else\
155+ self.char_width.index(x) - 1
156+ elif event.type == KEYDOWN:
157+ if event.unicode:
158+ if event.mod & KMOD_CTRL:
159+ if event.key == K_a: # Select all
160+ self.selected = [0,-1]
161+ elif event.key == K_c and\
162+ self.selected is not None: # Copy
163+ string = "".join(self._settings["label"][0]\
164+ [self.selected[0]:self.selected[1]])
165+ try:
166+ pygame.scrap.put(SCRAP_TEXT, string)
167+ except pygame.error:
168+ print "Please run 'pygame.scrap.init()'" \
169+ " to use the clipboard."
170+
171+ def update(self, time):
172+ """Update the Label each frame."""
173+ if self.rect_abs.collidepoint(pygame.mouse.get_pos()):
174+ pygame.mouse.set_cursor(self.mouse[1],
175+ self.mouse[2],
176+ *self.mouse[0])
177 else:
178- pos = None
179-
180- text = self._settings["font"].render(self._settings["text"], True,
181- self._settings["color"])
182- self._create_base_images(text)
183-
184- # Copy position attribute over
185- if pos is not None:
186- if hasattr(self, "_temp_pos"):
187- setattr(self.rect, self._temp_pos, pos)
188- del self._temp_pos
189- else:
190- self.rect.topleft = pos
191+ pygame.mouse.set_cursor(*self.mouse[3])
192+ draw = self.get_draw()
193+ self._images["image"].blit(self._settings["label"][1].image, (3,0))
194+ self.image = self._images["image"].copy()
195+ if self.has_focus() and self._settings["selectable"]\
196+ and self.selected is not None:
197+ # Semi-transparent selection rectangle
198+ w=abs(self.char_width[self.selected[1]] -\
199+ self.char_width[self.selected[0]])
200+ selection = Simple((w, self.rect.h-2))
201+ selection.pos = (self.char_width[self.selected[0]], 1)
202+ selection.image.fill(self._settings["col_selection"])
203+ selection.image.set_alpha(100)
204+ # Border around selection rectangle
205+ selection_b = Simple((selection.rect.w+1,\
206+ selection.rect.h+1))
207+ draw.rect(selection_b.image, self._settings["col_selection"],
208+ selection_b.rect, 1)
209+ if self.selected[0] < self.selected[1]or self.selected[0] == 0:
210+ pos = (selection.rect.x - 1, selection.rect.y)
211+ else:
212+ pos = (selection.rect.x - w - 1, selection.rect.y)
213+ self.image.blit(selection.image, pos)
214+ self.image.blit(selection_b.image, (pos[0]-1, pos[1]-1))
215
216 @property
217- def text(self):
218- return self._settings["text"]
219- @text.setter
220- def text(self, value):
221- self._settings["text"] = value
222+ def label(self):
223+ return self._settings["label"]
224+ @label.setter
225+ def label(self, value):
226+ self._settings["label"] = value
227 self._draw()

Subscribers

People subscribed via source and target branches