2021-05-04 17:04:43 +02:00
|
|
|
import pygame
|
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
# importing an auxiliary functions
|
|
|
|
from ui.auxiliary_decorator import _return_itself
|
|
|
|
from ui.text_box import get_fixed_rgb
|
2021-05-04 17:04:43 +02:00
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
# importing parent class
|
|
|
|
from ui.text_box import TextBox
|
|
|
|
|
|
|
|
|
|
|
|
class Button(TextBox):
|
2021-05-04 17:04:43 +02:00
|
|
|
# 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,
|
2021-05-07 13:54:42 +02:00
|
|
|
irregular_outline=False,
|
|
|
|
outline_additional_pixel=False,
|
|
|
|
outline_hover_inclusive=True,
|
2021-05-04 17:04:43 +02:00
|
|
|
outline_thickness=None,
|
|
|
|
outline_color=(50, 50, 50),
|
|
|
|
box_transform_hover=1.16,
|
|
|
|
box_transform_inactive=0.9,
|
|
|
|
font_transform_hover=1.24,
|
2021-05-07 13:54:42 +02:00
|
|
|
font_transform_inactive=0.85,
|
|
|
|
outline_transform_hover=1,
|
|
|
|
outline_transform_inactive=0.9
|
2021-05-04 17:04:43 +02:00
|
|
|
):
|
2021-05-07 13:54:42 +02:00
|
|
|
# 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
|
|
|
|
)
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
# 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
|
2021-05-07 13:54:42 +02:00
|
|
|
self.outline_transform_hover = outline_transform_hover
|
|
|
|
self.outline_transform_inactive = outline_transform_inactive
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
# counting colors on: hover, inactive
|
2021-05-07 13:54:42 +02:00
|
|
|
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))
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
# draws the Button instance
|
2021-05-07 13:54:42 +02:00
|
|
|
@_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
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
# 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
|
|
|
|
):
|
2021-05-07 13:54:42 +02:00
|
|
|
super().set_colors(box_color=box_color, font_color=font_color, outline_color=outline_color)
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
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)))
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
# sets chosen flags
|
2021-05-07 13:54:42 +02:00
|
|
|
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
|
|
|
|
)
|
2021-05-04 17:04:43 +02:00
|
|
|
|
|
|
|
if is_active is not None:
|
|
|
|
self.is_active = is_active
|
|
|
|
|
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
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])
|
2021-05-04 17:04:43 +02:00
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
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]
|
2021-05-04 17:04:43 +02:00
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
for i in range(3):
|
|
|
|
rgb_list[i] += transform_value[i]
|
2021-05-04 17:04:43 +02:00
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
return tuple(rgb_list)
|
2021-05-04 17:04:43 +02:00
|
|
|
|
2021-05-07 13:54:42 +02:00
|
|
|
return [0, 0, 0]
|