1 import pygame.locals as pgl
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, R90
7 from naja.sound import sound
10 class SelectorWidget(Container):
11 def __init__(self, *args, **kwargs):
12 super(SelectorWidget, self).__init__(*args, **kwargs)
14 self.left_selector = resources.get_image('bits', 'arrow_on.png',
15 transforms=(R270, EIGHT_BIT))
16 self.right_selector = resources.get_image('bits', 'arrow_on.png',
17 transforms=(R90, EIGHT_BIT))
19 def render(self, surface):
20 super(SelectorWidget, self).render(surface)
21 pos = self.left_selector.get_rect()
22 selected = self.widgets[self.position]
23 if not selected.is_selectable():
24 selected = self.change_pos(1)
26 pos = pos.move(selected.pos)
27 left_pos = pos.move(-pos.width * 1.5,
28 (selected.size[1] - pos.height) / 2)
29 right_pos = pos.move(selected.size[0] + pos.width * 0.5,
30 (selected.size[1] - pos.height) / 2)
31 surface.blit(self.left_selector, left_pos)
32 surface.blit(self.right_selector, right_pos)
34 def change_pos(self, change):
35 self.position = (self.position + change) % len(self.widgets)
36 while not self.widgets[self.position].is_selectable():
37 self.position = (self.position + change) % len(self.widgets)
38 return self.widgets[self.position]
40 def handle_event(self, ev):
41 if ev.type == pgl.KEYDOWN:
42 if ev.key in KEYS.UP + KEYS.DOWN:
43 if ev.key in KEYS.DOWN:
47 sound.play_sound('zoop.ogg', volume=0.05)
49 elif ev.key in KEYS.SELECT:
50 return self.widgets[self.position].callback('click')
52 return super(SelectorWidget, self).handle_event(ev)