Mercurial > mamba
annotate mamba/widgets/base.py @ 137:7fbeeb402685
PEP8 tidy mamba.widgets
author | Stefano Rivera <stefano@rivera.za.net> |
---|---|
date | Mon, 12 Sep 2011 13:09:09 +0200 |
parents | d5aa5f805f00 |
children | 79fdae806ca5 |
rev | line source |
---|---|
55
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
1 import collections |
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
2 |
23 | 3 import pygame |
115
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
4 from pygame.constants import K_UP, K_DOWN, K_RETURN |
58 | 5 from pygame.locals import MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN, KEYDOWN |
23 | 6 |
7 | |
8 class Widget(object): | |
9 | |
10 def __init__(self, rect): | |
38 | 11 if not isinstance(rect, pygame.Rect): |
41
3f44c30a1c39
Correct Text placement
Stefano Rivera <stefano@rivera.za.net>
parents:
38
diff
changeset
|
12 rect = pygame.Rect(rect, (0, 0)) |
3f44c30a1c39
Correct Text placement
Stefano Rivera <stefano@rivera.za.net>
parents:
38
diff
changeset
|
13 self.rect = rect |
58 | 14 self.focussable = False |
77 | 15 self.focussed = False |
106 | 16 self.parent = None |
55
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
17 self.callbacks = collections.defaultdict(list) |
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
18 |
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
19 def add_callback(self, eventtype, callback, *args): |
671d027f1b71
Callbacks for events in widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
43
diff
changeset
|
20 self.callbacks[eventtype].append((callback, args)) |
23 | 21 |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
22 def event(self, ev): |
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
23 for callback, args in self.callbacks[ev.type]: |
77 | 24 if callback(ev, self, *args): |
25 return True | |
26 return False | |
23 | 27 |
24
30d4f3e62bcf
Refactor widgets *again*, add container to Habitat
Stefano Rivera <stefano@rivera.za.net>
parents:
23
diff
changeset
|
28 def draw(self, surface): |
23 | 29 "Override me" |
30 pass | |
31 | |
32 | |
115
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
33 class Button(Widget): |
137
7fbeeb402685
PEP8 tidy mamba.widgets
Stefano Rivera <stefano@rivera.za.net>
parents:
115
diff
changeset
|
34 |
115
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
35 def event(self, ev): |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
36 if super(Button, self).event(ev): |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
37 return True |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
38 if (ev.type == MOUSEBUTTONDOWN |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
39 or (ev.type == KEYDOWN and ev.key == K_RETURN)): |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
40 for callback, args in self.callbacks['clicked']: |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
41 if callback(ev, self, *args): |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
42 return True |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
43 return False |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
44 |
d5aa5f805f00
Add Button class that buttons inherit from. It implements a 'clicked' callback
Stefano Rivera <stefano@rivera.za.net>
parents:
109
diff
changeset
|
45 |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
46 class Container(Widget): |
23 | 47 |
106 | 48 def __init__(self, rect): |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
49 super(Container, self).__init__(rect) |
23 | 50 self.children = [] |
77 | 51 self.focussed_child = None |
23 | 52 |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
53 def event(self, ev): |
77 | 54 """Push an event down through the tree, and fire our own event as a |
55 last resort | |
56 """ | |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
57 if ev.type in (MOUSEMOTION, MOUSEBUTTONUP, MOUSEBUTTONDOWN): |
58 | 58 for child in self.children: |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
59 if child.rect.collidepoint(ev.pos): |
109
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
60 if ev.type == MOUSEBUTTONDOWN and child.focussable: |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
61 root = self |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
62 while root.parent is not None: |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
63 root = root.parent |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
64 root.defocus() |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
65 widget = child |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
66 while widget.parent is not None: |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
67 parent = widget.parent |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
68 if isinstance(parent, Container): |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
69 idx = parent.children.index(widget) |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
70 parent.focussed_child = idx |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
71 widget = parent |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
72 child.focussed = True |
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
73 |
77 | 74 if child.event(ev): |
75 return True | |
109
dc0b0be7e2f6
Move mouse focussing to within Container
Stefano Rivera <stefano@rivera.za.net>
parents:
106
diff
changeset
|
76 |
59
3cc917814579
Container subclasses Widget
Stefano Rivera <stefano@rivera.za.net>
parents:
58
diff
changeset
|
77 if ev.type == KEYDOWN: |
58 | 78 for child in self.children: |
77 | 79 if child.focussed: |
80 if child.event(ev): | |
81 return True | |
82 if super(Container, self).event(ev): | |
83 return True | |
106 | 84 if (self.parent is None and ev.type == KEYDOWN |
85 and ev.key in (K_UP, K_DOWN)): | |
97
c65046b5bafd
eat focus adjusting key events
Stefano Rivera <stefano@rivera.za.net>
parents:
95
diff
changeset
|
86 return self.adjust_focus(1 if ev.key == K_DOWN else -1) |
23 | 87 |
88 def add(self, widget): | |
77 | 89 widget.parent = self |
23 | 90 self.children.append(widget) |
106 | 91 self.rect = self.rect.union(widget.rect) |
92 | |
93 def defocus(self): | |
94 if self.focussed_child is not None: | |
95 child = self.children[self.focussed_child] | |
96 if isinstance(child, Container): | |
97 child.defocus() | |
98 child.focussed = False | |
23 | 99 |
77 | 100 def adjust_focus(self, direction): |
101 """Try and adjust focus in direction (integer) | |
102 """ | |
103 if self.focussed_child is not None: | |
104 child = self.children[self.focussed_child] | |
105 if isinstance(child, Container): | |
106 if child.adjust_focus(direction): | |
107 return True | |
106 | 108 else: |
109 child.focussed = False | |
77 | 110 |
111 current = self.focussed_child | |
112 if current is None: | |
113 current = -1 if direction > 0 else len(self.children) | |
114 if direction > 0: | |
115 possibles = list(enumerate(self.children))[current + 1:] | |
116 else: | |
117 possibles = list(enumerate(self.children))[:current] | |
118 possibles.reverse() | |
119 for i, child in possibles: | |
120 if child.focussable: | |
121 child.focussed = True | |
106 | 122 self.focussed_child = i |
123 return True | |
124 if isinstance(child, Container): | |
125 if child.adjust_focus(direction): | |
77 | 126 self.focussed_child = i |
127 return True | |
128 else: | |
106 | 129 if self.parent is None: |
130 if self.focussed_child is not None: | |
99
2b6626f417f2
Handle root containers where nothing is focussable
Stefano Rivera <stefano@rivera.za.net>
parents:
97
diff
changeset
|
131 # At the end, mark the last one as focussed, again |
2b6626f417f2
Handle root containers where nothing is focussable
Stefano Rivera <stefano@rivera.za.net>
parents:
97
diff
changeset
|
132 child = self.children[self.focussed_child] |
2b6626f417f2
Handle root containers where nothing is focussable
Stefano Rivera <stefano@rivera.za.net>
parents:
97
diff
changeset
|
133 if isinstance(child, Container): |
2b6626f417f2
Handle root containers where nothing is focussable
Stefano Rivera <stefano@rivera.za.net>
parents:
97
diff
changeset
|
134 if child.adjust_focus(-direction): |
2b6626f417f2
Handle root containers where nothing is focussable
Stefano Rivera <stefano@rivera.za.net>
parents:
97
diff
changeset
|
135 return True |
106 | 136 else: |
137 child.focussed = True | |
138 return True | |
95
7a17c5b74148
Correctly handle nested Containers. Set focus on root container at first draw
Stefano Rivera <stefano@rivera.za.net>
parents:
77
diff
changeset
|
139 else: |
7a17c5b74148
Correctly handle nested Containers. Set focus on root container at first draw
Stefano Rivera <stefano@rivera.za.net>
parents:
77
diff
changeset
|
140 self.focussed_child = None |
77 | 141 return False |
142 | |
23 | 143 def draw(self, surface): |
106 | 144 if self.parent is None and not self.focussed: |
95
7a17c5b74148
Correctly handle nested Containers. Set focus on root container at first draw
Stefano Rivera <stefano@rivera.za.net>
parents:
77
diff
changeset
|
145 self.focussed = True |
7a17c5b74148
Correctly handle nested Containers. Set focus on root container at first draw
Stefano Rivera <stefano@rivera.za.net>
parents:
77
diff
changeset
|
146 self.adjust_focus(1) |
23 | 147 for child in self.children: |
24
30d4f3e62bcf
Refactor widgets *again*, add container to Habitat
Stefano Rivera <stefano@rivera.za.net>
parents:
23
diff
changeset
|
148 child.draw(surface) |