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

Proposed by Michael Rochester
Status: Merged
Approved by: Sam Bull
Approved revision: no longer in the source branch.
Merged at revision: 205
Proposed branch: lp:~codeforger/simplegc/simplegc
Merge into: lp:simplegc
Diff against target: 234 lines (+208/-0)
3 files modified
sgc/example/test.py (+14/-0)
sgc/widgets/__init__.py (+1/-0)
sgc/widgets/radio_button.py (+193/-0)
To merge this branch: bzr merge lp:~codeforger/simplegc/simplegc
Reviewer Review Type Date Requested Status
Sam Bull Approve
Review via email: mp+98650@code.launchpad.net

Description of the change

Added Radio buttons and changed __init__ to load it

To post a comment you must log in.
lp:~codeforger/simplegc/simplegc updated
202. By Sam Bull

Refactored and documented InputBox.

203. By Sam Bull

Refactored and documented Label.

204. By Sam Bull

Renamed event attribute from 'type_gui' to 'gui_type'.

Revision history for this message
Sam Bull (dreamsorcerer) :
review: Approve
lp:~codeforger/simplegc/simplegc updated
205. By Sam Bull

Added radio buttons.

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-19 12:30:40 +0000
3+++ sgc/example/test.py 2012-03-21 15:26:20 +0000
4@@ -54,6 +54,20 @@
5 title.rect.center = (screen.rect.w/2, 40)
6 title.add()
7
8+radio1 = sgc.widgets.Radio(group="group1", label="radio1", pos=(40,350), active=True)
9+radio1.add(10)
10+radio2 = sgc.widgets.Radio(group="group1", label="radio2", pos=(40,390))
11+radio2.add(11)
12+radio3 = sgc.widgets.Radio(group="group1", label="radio3", pos=(40,430))
13+radio3.add(12)
14+
15+radio21 = sgc.widgets.Radio(group="group2", label="radio21", pos=(190,350))
16+radio21.add(13)
17+radio22 = sgc.widgets.Radio(group="group2", label="radio22", pos=(190,390), active=True)
18+radio22.add(14)
19+radio23 = sgc.widgets.Radio(group="group2", label="radio23", pos=(190,430))
20+radio23.add(15)
21+
22 # Create input_box
23 input_box = sgc.widgets.InputBox(label="Input Box", default="default text...")
24 input_box.config(pos=(30,120))
25
26=== modified file 'sgc/widgets/__init__.py'
27--- sgc/widgets/__init__.py 2012-03-18 23:26:51 +0000
28+++ sgc/widgets/__init__.py 2012-03-21 15:26:20 +0000
29@@ -32,5 +32,6 @@
30 from input_box import InputBox
31 from label import Label
32 from menu import Menu
33+from radio_button import Radio
34 from scroll_box import ScrollBox
35 from settings import Keys
36
37=== added file 'sgc/widgets/radio_button.py'
38--- sgc/widgets/radio_button.py 1970-01-01 00:00:00 +0000
39+++ sgc/widgets/radio_button.py 2012-03-21 15:26:20 +0000
40@@ -0,0 +1,193 @@
41+#!/usr/bin/env python
42+
43+# Copyright (C) 2012 Michael Rochester
44+
45+"""
46+Radio Button widget, allows user to select a discrete option in a group.
47+
48+"""
49+
50+import pygame
51+from pygame.locals import *
52+
53+from _locals import *
54+from _locals import focus
55+from base_widget import Simple
56+
57+class Radio(Simple):
58+
59+ """
60+ A clickable radio button
61+
62+ Images:
63+ 'image': The default, unactive button state.
64+ 'over': The image used when the cursor is hovering over the button.
65+ 'active': The image used for the active button in a group
66+ (if applicable).
67+
68+ """
69+
70+ _can_focus = True
71+ _available_images = ("over", "active")
72+ _settings_default = {"label": "", "col":(118, 45, 215),
73+ "label_col": Font.col, "radius": 7}
74+
75+ _state = "off"
76+ _draw_rect = False
77+
78+ groups = {}
79+ group_list = {}
80+
81+ def _config(self, **kwargs):
82+ """
83+ group: ``str`` name of the group to be added to
84+ label: ``str`` Text to be displayed to the right of the button
85+ col: ``tuple`` (r,g,b) The colour to be used for the 'over' image
86+ label_col: ``tuple`` (r,g,b) The colour for the buttons label
87+ active: ``True`` if present this will make this the active button
88+ in the group
89+ radius ``int`` radius of the button.
90+ """
91+ if "group" in kwargs:
92+ if kwargs["group"] not in self.groups:
93+ self.groups[kwargs["group"]] = None
94+ self.group_list[kwargs["group"]] = []
95+ self._settings["group"] = kwargs["group"]
96+ self.group_list[self._settings["group"]].append(self)
97+ # Label to the right of the button
98+ if "label" in kwargs:
99+ # Save string as new label
100+ self._settings["label"] = kwargs["label"]
101+ # Colour of 'over' image
102+ if "col" in kwargs:
103+ self._settings["col"] = kwargs["col"]
104+ # Colour of label
105+ if "label_col" in kwargs:
106+ self._settings["label_col"] = kwargs["labe_col"]
107+ if "radius" in kwargs:
108+ self._settings["radius"] = kwargs["radius"]
109+ assert self._settings["group"] is not None
110+ if "active" in kwargs:
111+ self._draw()
112+ self._activate()
113+
114+ def _draw(self, draw):
115+ r = self._settings["radius"]
116+ # Render text
117+ label = Simple(Font["widget"].render(self._settings["label"], True,
118+ self._settings["label_col"]))
119+ if not hasattr(self, "image"):
120+ self._create_base_images((r*2+10+label.rect.w,
121+ max(label.rect.height,r*2)))
122+
123+ cent = self.rect.h/2
124+ # Set all to transparent
125+ self._images["image"].fill(0)
126+ self._images["over"].fill(0)
127+ self._images["active"].fill(0)
128+ # Draw all base circles
129+ draw.circle(self._images["image"],(255,255,255),(r,cent), r)
130+ draw.circle(self._images["over"],self._settings["col"],(r,cent), r)
131+ # Draw all the rings
132+ draw.circle(self._images["image"],(0,0,1), (r,cent), r, 1)
133+ draw.circle(self._images["over"],(0,0,1), (r,cent), r, 1)
134+ # Draw the central dot for 'active'
135+ draw.circle(self._images["active"],(0,0,1), (r,cent), r*2/3)
136+ # Blit text
137+ self._images["image"].blit(label.image, (r*2+10,cent-(label.rect.h/2)))
138+ self._images["over"].blit(label.image, (r*2+10,cent-(label.rect.h/2)))
139+ self._draw_button()
140+
141+ def update(self, time):
142+ """Update the button each frame."""
143+ if self.rect_abs.collidepoint(pygame.mouse.get_pos()):
144+ if self._state != "over":
145+ # Draw over state
146+ self._state = "over"
147+ self._draw_button()
148+ elif self._state != "off":
149+ # Draw normal state
150+ self._state = "off"
151+ self._draw_button()
152+
153+ def _event(self, event):
154+ """Respond to events."""
155+ if event.type == MOUSEBUTTONUP and event.button == 1:
156+ self._state = None
157+ # If releasing mouse on button, call function
158+ if self.rect_abs.collidepoint(event.pos):
159+ self._activate()
160+ elif event.type == KEYDOWN:
161+ if event.key == K_UP:
162+ if self.group_list[self._settings["group"]].index(self) != 0:
163+ group = self.group_list[self._settings["group"]]
164+ group[(group.index(self)-1)]._activate()
165+ focus.add(1,self.groups[self._settings["group"]])
166+ elif event.key == K_DOWN:
167+ if self.group_list[self._settings["group"]].index(self) \
168+ < len(self.group_list[self._settings["group"]]) -1:
169+ group = self.group_list[self._settings["group"]]
170+ group[(group.index(self)+1)]._activate()
171+ focus.add(1,self.groups[self._settings["group"]])
172+ elif event.type == KEYUP:
173+ if event.key in (13,32): # Enter or space key
174+ self._activate()
175+
176+ def _focus_enter(self, focus):
177+ """Draw rectangle when focus is gained from keyboard."""
178+ if focus == 1:
179+ self._draw_rect = True
180+ self._draw_button()
181+
182+ def _focus_exit(self):
183+ """Stop drawing rectangle when focus is lost."""
184+ self._draw_rect = False
185+ self._draw_button()
186+
187+ def _draw_button(self):
188+ """Draw the button."""
189+ if self._state == "off":
190+ self.image = self._images["image"].copy()
191+ elif self._state == "over":
192+ self.image = self._images["over"].copy()
193+ if self.groups[self._settings["group"]] is self:
194+ self.image.blit(self._images["active"],(0,0))
195+ # Draw dotted rectangle to show keyboard focus
196+ if self._draw_rect:
197+ self._dotted_rect()
198+
199+ def _activate(self):
200+ old = self.groups[self._settings["group"]]
201+ self.groups[self._settings["group"]] = self
202+ if old is not None:
203+ old._draw_button()
204+ self._draw_button()
205+
206+ def clear(self, group=None):
207+ if group is None: group = self._settings["group"]
208+ old = self.groups[group]
209+ self.groups[group] = None
210+ if old is not None:
211+ old._draw_button()
212+
213+ @property
214+ def active(self):
215+ return self is self.groups[self._settings["group"]]
216+
217+
218+
219+
220+
221+
222+
223+
224+
225+
226+
227+
228+
229+
230+
231+
232+
233+
234\ No newline at end of file

Subscribers

People subscribed via source and target branches