Module game_qu.gui_components.button
Expand source code
from game_qu.gui_components.text_box import TextBox
from game_qu.base.events import TimedEvent
class Button(TextBox):
""" Essentially a 'TextBox,' but with other useful features: mainly it can change color when the user hovers their
mouse over it and/or clicks it"""
default_color = None
hover_color = None
clicked_color = None
switch_back_to_default_color_timed_event = None
def __init__(self, text, font_size, background_color, text_color, is_centered):
""" Initializes the object
Args:
text (str): the text that is displayed
font_size (int): the size of the font
background_color (tuple): the (Red, Green, Blue) values of the text box's background
text_color (tuple): the (Red, Green, Blue) values of the text's color
is_centered (bool): whether the text inside the text box is centered
Returns:
None
"""
super().__init__(text, font_size, background_color, text_color, is_centered)
self.set_colors(background_color, background_color, background_color) # So the button doesn't change color
self.switch_back_to_default_color_timed_event = TimedEvent(float("inf"), False)
self.set_mouse_functions(self.mouse_enter, self.mouse_exit)
def set_colors(self, default_color, hover_color, clicked_color):
"""Sets the colors of the Button
Args:
default_color: (list[int]): {red, green, blue}; The color of the button when the button isn't clicked or hovered over
hover_color: (list[int]): {red, green, blue}; The color of the button when it is hovered over
clicked_color: (list[int]): {red, green, blue}; The color of the button after it is clicked"""
self.default_color = default_color
self.hover_color = hover_color
self.clicked_color = clicked_color
def set_click_color_change_duration(self, duration):
"""Sets the duration that the button changes color to 'clicked_color' after it is clicked"""
self.switch_back_to_default_color_timed_event.set_time_needed(duration)
def run(self):
"""Runs everything necessary for the Button to function properly"""
super().run()
self.switch_back_to_default_color_timed_event.run()
if self.got_clicked():
self.set_background_color(self.clicked_color)
self.switch_back_to_default_color_timed_event.start()
if self.switch_back_to_default_color_timed_event.is_done():
self.set_background_color(self.default_color)
self.switch_back_to_default_color_timed_event.reset()
def mouse_enter(self):
"""Runs what happens when the mouse enters the button: the button changing color to the 'hover_color'"""
self.set_background_color(self.hover_color)
def mouse_exit(self):
"""Runs what happens when the mouse exits the button: the button changing color to the 'default_color'"""
self.set_background_color(self.default_color)
Classes
-
Essentially a 'TextBox,' but with other useful features: mainly it can change color when the user hovers their mouse over it and/or clicks it
Initializes the object
Args
text
:str
- the text that is displayed
font_size
:int
- the size of the font
background_color
:tuple
- the (Red, Green, Blue) values of the text box's background
text_color
:tuple
- the (Red, Green, Blue) values of the text's color
is_centered
:bool
- whether the text inside the text box is centered
Returns
None
Expand source code
class Button(TextBox): """ Essentially a 'TextBox,' but with other useful features: mainly it can change color when the user hovers their mouse over it and/or clicks it""" default_color = None hover_color = None clicked_color = None switch_back_to_default_color_timed_event = None def __init__(self, text, font_size, background_color, text_color, is_centered): """ Initializes the object Args: text (str): the text that is displayed font_size (int): the size of the font background_color (tuple): the (Red, Green, Blue) values of the text box's background text_color (tuple): the (Red, Green, Blue) values of the text's color is_centered (bool): whether the text inside the text box is centered Returns: None """ super().__init__(text, font_size, background_color, text_color, is_centered) self.set_colors(background_color, background_color, background_color) # So the button doesn't change color self.switch_back_to_default_color_timed_event = TimedEvent(float("inf"), False) self.set_mouse_functions(self.mouse_enter, self.mouse_exit) def set_colors(self, default_color, hover_color, clicked_color): """Sets the colors of the Button Args: default_color: (list[int]): {red, green, blue}; The color of the button when the button isn't clicked or hovered over hover_color: (list[int]): {red, green, blue}; The color of the button when it is hovered over clicked_color: (list[int]): {red, green, blue}; The color of the button after it is clicked""" self.default_color = default_color self.hover_color = hover_color self.clicked_color = clicked_color def set_click_color_change_duration(self, duration): """Sets the duration that the button changes color to 'clicked_color' after it is clicked""" self.switch_back_to_default_color_timed_event.set_time_needed(duration) def run(self): """Runs everything necessary for the Button to function properly""" super().run() self.switch_back_to_default_color_timed_event.run() if self.got_clicked(): self.set_background_color(self.clicked_color) self.switch_back_to_default_color_timed_event.start() if self.switch_back_to_default_color_timed_event.is_done(): self.set_background_color(self.default_color) self.switch_back_to_default_color_timed_event.reset() def mouse_enter(self): """Runs what happens when the mouse enters the button: the button changing color to the 'hover_color'""" self.set_background_color(self.hover_color) def mouse_exit(self): """Runs what happens when the mouse exits the button: the button changing color to the 'default_color'""" self.set_background_color(self.default_color)
Ancestors
Class variables
Methods
-
Runs what happens when the mouse enters the button: the button changing color to the 'hover_color'
Expand source code
def mouse_enter(self): """Runs what happens when the mouse enters the button: the button changing color to the 'hover_color'""" self.set_background_color(self.hover_color)
-
Runs what happens when the mouse exits the button: the button changing color to the 'default_color'
Expand source code
def mouse_exit(self): """Runs what happens when the mouse exits the button: the button changing color to the 'default_color'""" self.set_background_color(self.default_color)
-
Runs everything necessary for the Button to function properly
Expand source code
def run(self): """Runs everything necessary for the Button to function properly""" super().run() self.switch_back_to_default_color_timed_event.run() if self.got_clicked(): self.set_background_color(self.clicked_color) self.switch_back_to_default_color_timed_event.start() if self.switch_back_to_default_color_timed_event.is_done(): self.set_background_color(self.default_color) self.switch_back_to_default_color_timed_event.reset()
-
Sets the duration that the button changes color to 'clicked_color' after it is clicked
Expand source code
def set_click_color_change_duration(self, duration): """Sets the duration that the button changes color to 'clicked_color' after it is clicked""" self.switch_back_to_default_color_timed_event.set_time_needed(duration)
-
Sets the colors of the Button
Args
default_color
- (list[int]): {red, green, blue}; The color of the button when the button isn't clicked or hovered over
hover_color
- (list[int]): {red, green, blue}; The color of the button when it is hovered over
clicked_color
- (list[int]): {red, green, blue}; The color of the button after it is clicked
Expand source code
def set_colors(self, default_color, hover_color, clicked_color): """Sets the colors of the Button Args: default_color: (list[int]): {red, green, blue}; The color of the button when the button isn't clicked or hovered over hover_color: (list[int]): {red, green, blue}; The color of the button when it is hovered over clicked_color: (list[int]): {red, green, blue}; The color of the button after it is clicked""" self.default_color = default_color self.hover_color = hover_color self.clicked_color = clicked_color
Inherited members
TextBox
:bottom_edge
get_is_rendering_background
get_scaled_dimensions
got_clicked
horizontal_midpoint
mouse_enter_function
mouse_exit_function
number_set_dimensions
percentage_set_dimensions
render
right_edge
set_background_color
set_color
set_is_rendering_background
set_mouse_enter_function
set_mouse_exit_function
set_mouse_functions
set_text
vertical_midpoint