Add comments in UiButton.py

This commit is contained in:
Michał Czekański 2020-05-15 01:17:22 +02:00 committed by Marcin Kostrzewski
parent aab779d4e1
commit 7d71333558

View File

@ -1,15 +1,36 @@
from enum import Enum from enum import Enum
from typing import Tuple, List, Any, Callable
import pygame import pygame
from pygame.font import FontType
from src.ui.UiElement import UiElement from src.ui.UiElement import UiElement
class UiButton(UiElement): class UiButton(UiElement):
def __init__(self, rect: pygame.Rect, notClickedBtnColor=(125, 125, 125), clickedBtnColor=(255, 255, 255), functionsToInvokeWhenClicked: List[Tuple[Callable, Any]]
text="Click", textColor=(0, 0, 0), font=None, functionsToInvokeWhenClicked=[]): image: pygame.Surface
beingClicked: bool
text: str
clickedBtnColor: Tuple[int, int, int]
textColor: Tuple[int, int, int]
font: pygame.font.Font
def __init__(self, rect: pygame.Rect, notClickedBtnColor: Tuple[int, int, int] = (125, 125, 125),
clickedBtnColor: Tuple[int, int, int] = (255, 255, 255),
text: str = "Click", textColor: Tuple[int, int, int] = (0, 0, 0), font: pygame.font.Font = None,
functionsToInvokeWhenClicked: List[Tuple[Callable, Any]] = []):
""" """
:type functionsToInvokeWhenClicked : list of tuple(function, args*), args are function arguments Creates UiButton object.
:param rect: Rectangle on which button will be displayed.
:param notClickedBtnColor: Button color when it is not clicked.
:param clickedBtnColor: Button color when it is clicked.
:param text: Text to be displayed on button.
:param textColor:
:param font: Font for button text. If None is given then default font will be used.
:type functionsToInvokeWhenClicked : list of tuple(function, args*), args are function arguments.
""" """
super().__init__(rect) super().__init__(rect)
if font is None: if font is None:
@ -28,8 +49,14 @@ class UiButton(UiElement):
self.functionsToInvokeWhenClicked.extend(functionsToInvokeWhenClicked) self.functionsToInvokeWhenClicked.extend(functionsToInvokeWhenClicked)
def eventHandler(self, event): def eventHandler(self, event: pygame.event):
# change selected color if rectangle clicked """
Checks if this button was clicked based on given pygame event.
If yes, then it calls all functions that are on the list of functions to invoke.
:param event: pygame event, which will be examined to check if this button was clicked.
"""
# change selected color if this button's rectangle was clicked
if event.type == pygame.MOUSEBUTTONDOWN: if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1: if event.button == 1:
if self.rect.collidepoint(event.pos): # is mouse over button if self.rect.collidepoint(event.pos): # is mouse over button
@ -42,7 +69,11 @@ class UiButton(UiElement):
self.beingClicked = False self.beingClicked = False
self.image = self._images[ButtonImages.DEFAULT_IMAGE.value] self.image = self._images[ButtonImages.DEFAULT_IMAGE.value]
def __initBtnImages__(self): def __initBtnImages__(self) -> None:
"""
Creates button images which will be drawn, so that button is displayed.
"""
self._images = [ self._images = [
pygame.Surface((self.rect.width, self.rect.height)), pygame.Surface((self.rect.width, self.rect.height)),
pygame.Surface((self.rect.width, self.rect.height)), pygame.Surface((self.rect.width, self.rect.height)),
@ -55,13 +86,21 @@ class UiButton(UiElement):
self._images[0].blit(self.textSurface, self.textSurfaceDest) self._images[0].blit(self.textSurface, self.textSurfaceDest)
self._images[1].blit(self.textSurface, self.textSurfaceDest) self._images[1].blit(self.textSurface, self.textSurfaceDest)
def addFuncToInvoke(self, tupleOfFuncAndArgs): def addFuncToInvoke(self, tupleOfFuncAndArgs: Tuple[Callable, Any]) -> None:
""" """
Adds given function to list of functions, which will be invoked when this button is clicked.
:type tupleOfFuncAndArgs: tuple(function, *args) :type tupleOfFuncAndArgs: tuple(function, *args)
""" """
self.functionsToInvokeWhenClicked.append(tupleOfFuncAndArgs) self.functionsToInvokeWhenClicked.append(tupleOfFuncAndArgs)
class ButtonImages(Enum): class ButtonImages(Enum):
"""
This enum is used to display proper button images.
When button is not clicked, then default image is being drawn.
When button is clicked, clicking image is being drawn.
"""
DEFAULT_IMAGE = 0 DEFAULT_IMAGE = 0
CLICKING_IMAGE = 1 CLICKING_IMAGE = 1