Mercurial > skaapsteker
comparison skaapsteker/widgets/text.py @ 257:3f8ce3452cd6
Add support for selecting dialogue choices (and for moving TextChoice around the screen).
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Fri, 08 Apr 2011 01:36:44 +0200 |
parents | 30ae3c681507 |
children | a5a57abd5472 |
comparison
equal
deleted
inserted
replaced
256:e43e66f3f647 | 257:3f8ce3452cd6 |
---|---|
103 When selected, calls everything registered in callbacks with (index, data) | 103 When selected, calls everything registered in callbacks with (index, data) |
104 """ | 104 """ |
105 def __init__(self, options, pos, **kwargs): | 105 def __init__(self, options, pos, **kwargs): |
106 self.options = [] | 106 self.options = [] |
107 self.option_widgets = [] | 107 self.option_widgets = [] |
108 self.rect = pygame.Rect(pos, (0, 0)) | 108 if isinstance(pos, pygame.Rect): |
109 self.selector = Text(u'» ', pos, **kwargs) | 109 self.rect = pos |
110 else: | |
111 self.rect = pygame.Rect(pos, (0, 0)) | |
112 pos = self.rect | |
113 self.selector = Text(u'» ', pos.move(0, 0), **kwargs) | |
110 self.selected = 0 | 114 self.selected = 0 |
111 self.callbacks = [] | 115 self.callbacks = [] |
112 | 116 |
113 pos = self.rect.move(self.selector.rect.width, 0) | |
114 for option in options: | 117 for option in options: |
115 if not isinstance(option, tuple): | 118 if not isinstance(option, tuple): |
116 option = (option, None) | 119 option = (option, None) |
117 self.options.append(option) | 120 self.options.append(option) |
118 text, data = option | 121 text, data = option |
119 t = Text(text, pos, **kwargs) | 122 t = Text(text, pos.move(0, 0), **kwargs) |
120 pos = pos.move(0, t.rect.height) | |
121 self.option_widgets.append(t) | 123 self.option_widgets.append(t) |
122 | 124 |
123 self.rect.width = max(line.rect.width for line in self.option_widgets | 125 self.rect.width = max(line.rect.width for line in self.option_widgets |
124 ) + self.selector.rect.width | 126 ) + self.selector.rect.width |
125 self.rect.height = sum(line.rect.height for line in self.option_widgets) | 127 self.rect.height = sum(line.rect.height for line in self.option_widgets) |
128 | |
129 def _update_rects(self): | |
130 pos = self.rect.move(self.selector.rect.width, 0) | |
131 for t in self.option_widgets: | |
132 t.rect.topleft = pos.topleft | |
133 pos = pos.move(0, t.rect.height) | |
134 self.selector.rect.top = self.option_widgets[self.selected].rect.top | |
135 self.selector.rect.left = self.rect.left | |
126 | 136 |
127 def dispatch(self, ev): | 137 def dispatch(self, ev): |
128 if ev.type is KEYDOWN: | 138 if ev.type is KEYDOWN: |
129 if ev.key == K_UP: | 139 if ev.key == K_UP: |
130 self.selected -= 1 | 140 self.selected -= 1 |
133 elif ev.key == K_RETURN: | 143 elif ev.key == K_RETURN: |
134 for callback in self.callbacks: | 144 for callback in self.callbacks: |
135 callback(self.selected, self.options[self.selected][1]) | 145 callback(self.selected, self.options[self.selected][1]) |
136 | 146 |
137 self.selected %= len(self.option_widgets) | 147 self.selected %= len(self.option_widgets) |
138 self.selector.rect.top = self.option_widgets[self.selected].rect.top | |
139 | 148 |
140 def draw(self, surface): | 149 def draw(self, surface): |
150 self._update_rects() | |
141 for option in self.option_widgets: | 151 for option in self.option_widgets: |
142 option.draw(surface) | 152 option.draw(surface) |
143 self.selector.draw(surface) | 153 self.selector.draw(surface) |
144 | 154 |
145 | 155 |