From 0244fa47c747ff7c68bba61328aea7a6ac9d82c8 Mon Sep 17 00:00:00 2001 From: Neil Date: Sat, 10 Sep 2016 22:31:02 +0200 Subject: [PATCH] Add enable / disabled toggle to TextButton --- tabakrolletjie/widgets.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tabakrolletjie/widgets.py b/tabakrolletjie/widgets.py index 156cb6c..c1bbc94 100644 --- a/tabakrolletjie/widgets.py +++ b/tabakrolletjie/widgets.py @@ -6,7 +6,7 @@ import pygame.locals as pgl from .loader import loader from .constants import FONTS -from .transforms import NullTransform +from .transforms import NullTransform, Multiply class Button(object): @@ -16,6 +16,13 @@ class Button(object): self._padding = padding self.position = pos self.name = name + self.enabled = True + + def enable(self): + self.enabled = True + + def disable(self): + self.enabled = False @property def position(self): @@ -40,6 +47,8 @@ class Button(object): pass def pressed(self, ev): + if not self.enabled: + return False if self._pos is None: # Unplaced buttons can't be pressed return False @@ -63,11 +72,16 @@ class TextButton(Button): def __init__(self, text, colour, name=None, pos=None, padding=10): font = loader.load_font(FONTS['sans'], size=24) self._text = font.render(text, True, colour) + self._disabled_text = self._text.copy() + Multiply(colour=(80, 80, 80)).apply(self._disabled_text) super(TextButton, self).__init__(self._text.get_size(), name, pos, padding) def render(self, surface): - surface.blit(self._text, self._pos, None) + if self.enabled: + surface.blit(self._text, self._pos, None) + else: + surface.blit(self._disabled_text, self._pos, None) class ImageButton(Button): @@ -86,6 +100,7 @@ class ImageButton(Button): def update_image(self, *imgparts, **kwargs): transform = kwargs.pop("transform", NullTransform()) self._img = loader.load_image(*imgparts, transform=transform) + Multiply((128, 128, 128)).apply(self._disabled_img) def render(self, surface): surface.blit(self._img, self._pos, None) -- 2.34.1