181 lines
6.9 KiB
Python
181 lines
6.9 KiB
Python
import pygame
|
|
|
|
# importing an auxiliary functions
|
|
from ui.auxiliary_decorator import _return_itself
|
|
from ui.text_box import get_fixed_rgb
|
|
|
|
# importing parent class
|
|
from ui.text_box import TextBox
|
|
|
|
|
|
class Button(TextBox):
|
|
# constructor that can be used to set parameters of Button instance
|
|
def __init__(
|
|
self,
|
|
position,
|
|
dimensions,
|
|
text="",
|
|
box_color=(195, 195, 195),
|
|
font=pygame.font.get_default_font(),
|
|
font_color=(50, 50, 50),
|
|
font_size=None,
|
|
is_visible=True,
|
|
is_active=True,
|
|
fit_text=True,
|
|
outline=True,
|
|
irregular_outline=False,
|
|
outline_additional_pixel=False,
|
|
outline_hover_inclusive=True,
|
|
outline_thickness=None,
|
|
outline_color=(50, 50, 50),
|
|
box_transform_hover=1.16,
|
|
box_transform_inactive=0.9,
|
|
font_transform_hover=1.24,
|
|
font_transform_inactive=0.85,
|
|
outline_transform_hover=1,
|
|
outline_transform_inactive=0.9
|
|
):
|
|
# calling base class constructor
|
|
super().__init__(
|
|
position=position,
|
|
dimensions=dimensions,
|
|
text=text,
|
|
box_color=box_color,
|
|
font=font,
|
|
font_color=font_color,
|
|
font_size=font_size,
|
|
is_visible=is_visible,
|
|
fit_text=fit_text,
|
|
outline=outline,
|
|
irregular_outline=irregular_outline,
|
|
outline_additional_pixel=outline_additional_pixel,
|
|
outline_hover_inclusive=outline_hover_inclusive,
|
|
outline_thickness=outline_thickness,
|
|
outline_color=outline_color
|
|
)
|
|
|
|
# setting attributes
|
|
self.is_active = is_active
|
|
self.box_transform_hover = box_transform_hover
|
|
self.box_transform_inactive = box_transform_inactive
|
|
self.font_transform_hover = font_transform_hover
|
|
self.font_transform_inactive = font_transform_inactive
|
|
self.outline_transform_hover = outline_transform_hover
|
|
self.outline_transform_inactive = outline_transform_inactive
|
|
|
|
# counting colors on: hover, inactive
|
|
self.box_hover_color = get_fixed_rgb(_transform_rgb(self.box_color, self.box_transform_hover))
|
|
self.box_inactive_color = get_fixed_rgb(_transform_rgb(self.box_color, self.box_transform_inactive))
|
|
self.font_hover_color = get_fixed_rgb(_transform_rgb(self.font_color, self.font_transform_hover))
|
|
self.font_inactive_color = get_fixed_rgb(_transform_rgb(self.font_color, self.font_transform_inactive))
|
|
self.outline_hover_color = get_fixed_rgb(_transform_rgb(self.outline_color, self.outline_transform_hover))
|
|
self.outline_inactive_color = get_fixed_rgb(_transform_rgb(self.outline_color, self.outline_transform_inactive))
|
|
|
|
# draws the Button instance
|
|
@_return_itself
|
|
def draw(self, window, mouse_position, *args, **kwargs):
|
|
# saving attribute values
|
|
box_color_attribute_value = self.box_color
|
|
text_color_attribute_value = self.font_color
|
|
outline_attribute_value = self.outline_color
|
|
|
|
# temporarily changing box and font color if necessary
|
|
# is active and mouse is over
|
|
if self.is_active and self.is_over(mouse_position):
|
|
self.box_color = self.box_hover_color
|
|
self.font_color = self.font_hover_color
|
|
self.outline_color = self.outline_hover_color
|
|
|
|
# is not active
|
|
else:
|
|
self.box_color = self.box_inactive_color
|
|
self.font_color = self.font_inactive_color
|
|
self.outline_color = self.outline_inactive_color
|
|
|
|
# calling the draw function of base class
|
|
super().draw(window)
|
|
|
|
# resetting colors to original values
|
|
self.box_color = box_color_attribute_value
|
|
self.font_color = text_color_attribute_value
|
|
self.outline_color = outline_attribute_value
|
|
|
|
# returns True if the Button is clicked, or False otherwise
|
|
def is_clicked(self, mouse_position, events):
|
|
# if is_active=True and mouse is over the Button - checking if mouse is clicked
|
|
if self.is_active and self.is_over(mouse_position):
|
|
|
|
# if mouse is clicked returning True, otherwise False
|
|
for event in events:
|
|
if event.type == pygame.MOUSEBUTTONUP:
|
|
return True
|
|
|
|
return False
|
|
|
|
# sets chosen color attributes
|
|
def set_colors(
|
|
self,
|
|
box_color=None,
|
|
font_color=None,
|
|
outline_color=None,
|
|
box_transform_hover=None,
|
|
box_transform_inactive=None,
|
|
font_transform_hover=None,
|
|
font_transform_inactive=None
|
|
):
|
|
super().set_colors(box_color=box_color, font_color=font_color, outline_color=outline_color)
|
|
|
|
if box_transform_hover is not None:
|
|
self.box_transform_hover = box_transform_hover
|
|
|
|
if box_transform_inactive is not None:
|
|
self.box_transform_inactive = box_transform_inactive
|
|
|
|
if font_transform_hover is not None:
|
|
self.font_transform_hover = font_transform_hover
|
|
|
|
if font_transform_inactive is not None:
|
|
self.font_transform_inactive = font_transform_inactive
|
|
|
|
self.box_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.box_color, self.box_transform_hover)))
|
|
self.box_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.box_color, self.box_transform_inactive)))
|
|
self.font_hover_color = get_fixed_rgb(tuple(_transform_rgb(self.font_color, self.font_transform_hover)))
|
|
self.font_inactive_color = get_fixed_rgb(tuple(_transform_rgb(self.font_color, self.font_transform_inactive)))
|
|
|
|
# sets chosen flags
|
|
def set_flags(
|
|
self,
|
|
is_visible=None,
|
|
is_active=None,
|
|
outline=None,
|
|
irregular_outline=None,
|
|
outline_additional_pixel=None,
|
|
outline_hover_inclusive=None
|
|
):
|
|
super().set_flags(
|
|
is_visible=is_visible,
|
|
outline=outline,
|
|
irregular_outline=irregular_outline,
|
|
outline_additional_pixel=outline_additional_pixel,
|
|
outline_hover_inclusive=outline_hover_inclusive
|
|
)
|
|
|
|
if is_active is not None:
|
|
self.is_active = is_active
|
|
|
|
|
|
def _transform_rgb(iterable, transform_value):
|
|
if isinstance(transform_value, int) or isinstance(transform_value, float):
|
|
return tuple([x * transform_value for x in iterable])
|
|
|
|
elif isinstance(transform_value, tuple) or isinstance(transform_value, list):
|
|
if len(transform_value) == 3 and all([isinstance(x, int) or isinstance(x, float) for x in transform_value]):
|
|
rgb_list = [x for x in iterable]
|
|
|
|
for i in range(3):
|
|
rgb_list[i] += transform_value[i]
|
|
|
|
return tuple(rgb_list)
|
|
|
|
return [0, 0, 0]
|