2 # This just unifies some logic.
3 # There is no implied container / window system (yet)
5 import pygame.locals as pgl
7 from .loader import loader
8 from .constants import FONTS
9 from .transforms import NullTransform
14 def __init__(self, size, name=None, pos=None, padding=10):
16 self._padding = padding
25 def position(self, pos):
28 self._min_x = pos[0] - self._padding
29 self._max_x = pos[0] + self._size[0] + self._padding
30 self._min_y = pos[1] - self._padding
31 self._max_y = pos[1] + self._size[1] + self._padding
39 def render(self, surface):
42 def pressed(self, ev):
44 # Unplaced buttons can't be pressed
46 if ev.type == pgl.MOUSEBUTTONDOWN and ev.button == 1:
47 if self._min_x < ev.pos[0] < self._max_x:
48 if self._min_y < ev.pos[1] < self._max_y:
53 class TextButton(Button):
55 def __init__(self, text, colour, name=None, pos=None, padding=10):
56 font = loader.load_font(FONTS['sans'], size=24)
57 self._text = font.render(text, True, colour)
58 super(TextButton, self).__init__(self._text.get_size(), name,
61 def render(self, surface):
62 surface.blit(self._text, self._pos, None)
65 class ImageButton(Button):
67 def __init__(self, *imgparts, **kwargs):
68 transform = kwargs.pop("transform", NullTransform())
69 self._img = kwargs.pop("image", None)
71 self._img = loader.load_image(*imgparts, transform=transform)
72 name = kwargs.get('name')
73 pos = kwargs.get('pos')
74 padding = kwargs.get('padding', 0)
75 super(ImageButton, self).__init__(self._img.get_size(), name,
78 def update_image(self, *imgparts, **kwargs):
79 transform = kwargs.pop("transform", NullTransform())
80 self._img = loader.load_image(*imgparts, transform=transform)
82 def render(self, surface):
83 surface.blit(self._img, self._pos, None)