c5ba07f37a4eb490010155d5b334f0e82c26ed14
[naja.git] / naja / widgets / selector.py
1 import pygame.locals as pgl
2
3 from naja.constants import KEYS
4 from naja.widgets.base import Container
5 from naja.resources import resources
6 from naja.resources.mutators import EIGHT_BIT, R270
7
8
9 class SelectorWidget(Container):
10     def __init__(self, *args, **kwargs):
11         super(SelectorWidget, self).__init__(*args, **kwargs)
12         self.position = 0
13         self.selector = resources.get_image('bits', 'arrow_on.png',
14                                             transforms=(R270, EIGHT_BIT))
15
16     def render(self, surface):
17         super(SelectorWidget, self).render(surface)
18         pos = self.selector.get_rect()
19         selected = self.widgets[self.position]
20         pos = pos.move(selected.pos)
21         pos = pos.move(-pos.width * 1.5, (selected.size[1] - pos.height) / 2)
22         surface.blit(self.selector, pos)
23
24     def handle_event(self, ev):
25         if ev.type == pgl.KEYDOWN:
26             if ev.key in KEYS.UP + KEYS.DOWN:
27                 if ev.key in KEYS.DOWN:
28                     self.position += 1
29                 else:
30                     self.position -= 1
31                 self.position %= len(self.widgets)
32                 return True
33             elif ev.key in KEYS.SELECT:
34                 return self.widgets[self.position].callback('click')
35
36         return super(SelectorWidget, self).handle_event(ev)