widgets start arriving
[tabakrolletjie.git] / tabakrolletjie / widgets.py
1 # Simple widgets
2 # This just unifies some logic.
3 # There is no implied container / window system (yet)
4
5 import pygame.locals as pgl
6
7 from .loader import loader
8 from .constants import FONTS
9
10
11 class Button(object):
12
13     def __init__(self, size, pos=None, padding=10):
14         self._size = size
15         self._padding = padding
16         self.position = pos
17
18     @property
19     def position(self):
20         return self._pos
21
22     @position.setter
23     def position(self, pos):
24         self._pos = pos
25         if pos is not None:
26             self._min_x = pos[0] - self._padding
27             self._max_x = pos[0] + self._size[0] + self._padding
28             self._min_y = pos[1] - self._padding
29             self._max_y = pos[1] + self._size[1] + self._padding
30
31     def get_width(self):
32         return self._size[0]
33
34     def get_height(self):
35         return self._size[1]
36
37     def render(self, surface):
38         pass
39
40     def pressed(self, ev):
41         if self._pos is None:
42             # Unplaced buttons can't be pressed
43             return False
44         if ev.type == pgl.MOUSEBUTTONDOWN and ev.button == 1:
45             if self._min_x < ev.pos[0] < self._max_x:
46                 if self._min_y < ev.pos[1] < self._max_y:
47                     return True
48         return False
49
50
51 class TextButton(Button):
52
53     def __init__(self, text, colour, pos=None, padding=10):
54         font = loader.load_font(FONTS['sans'], size=24)
55         self._text = font.render(text, True, colour)
56         super(TextButton, self).__init__(self._text.get_size(), pos, padding)
57
58     def render(self, surface):
59         surface.blit(self._text, self._pos, None)