Changeset 109:982f34d0aac0


Ignore:
Timestamp:
Apr 4, 2011, 7:30:39 PM (9 years ago)
Author:
Stefano Rivera <stefano@…>
Branch:
default
rebase_source:
cfd6628c8c40e321a28ef98a4776b089a87f2269
Message:

Add optional data to identify TextChoice? options

Location:
skaapsteker
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • skaapsteker/menuscene.py

    r104 r109  
    1818        self.widgets.append(self.choice)
    1919
    20     def selected(self, option):
     20    def selected(self, option, data):
    2121        "Callback from menu TextChoice"
    2222        if option == 0:
  • skaapsteker/widgets/text.py

    r82 r109  
    4545
    4646class TextChoice(Widget):
     47    """Render a list of options, and a selector.
     48    options can be text or a tuple of (text, data).
     49    When selected, calls everything registered in callbacks with (index, data)
     50    """
    4751    def __init__(self, options, pos, **kwargs):
    4852        self.options = []
     53        self.option_widgets = []
    4954        self.rect = pygame.Rect(pos, (0, 0))
    5055        self.selector = Text(u'» ', pos, **kwargs)
     
    5459        pos = self.rect.move(self.selector.rect.width, 0)
    5560        for option in options:
    56             t = Text(option, pos, **kwargs)
     61            if not isinstance(option, tuple):
     62                option = (option, None)
     63            self.options.append(option)
     64            text, data = option
     65            t = Text(text, pos, **kwargs)
    5766            pos = pos.move(0, t.rect.height)
    58             self.options.append(t)
     67            self.option_widgets.append(t)
     68
     69        self.rect.width = max(line.rect.width for line in self.option_widgets
     70                             ) + self.selector.rect.width
     71        self.rect.height = sum(line.rect.height for line in self.option_widgets)
    5972
    6073    def dispatch(self, ev):
     
    6578                self.selected += 1
    6679            elif ev.key == K_RETURN:
    67                 all(callback(self.selected) for callback in self.callbacks)
     80                for callback in self.callbacks:
     81                    callback(self.selected, self.options[self.selected][1])
    6882
    69         self.selected %= len(self.options)
    70         self.selector.rect.top = self.options[self.selected].rect.top
     83        self.selected %= len(self.option_widgets)
     84        self.selector.rect.top = self.option_widgets[self.selected].rect.top
    7185
    7286    def draw(self, surface):
    73         for option in self.options:
     87        for option in self.option_widgets:
    7488            option.draw(surface)
    7589        self.selector.draw(surface)
Note: See TracChangeset for help on using the changeset viewer.