Skip to content

Commit

Permalink
Expand tool tips to almost every element.
Browse files Browse the repository at this point in the history
  • Loading branch information
MyreMylar committed Apr 23, 2023
1 parent 326dde6 commit 206ec68
Show file tree
Hide file tree
Showing 33 changed files with 138 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/source/theme_reference/theme_button.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Misc
- "**shape_corner_radius**" - Only used if our shape is 'rounded_rectangle'. It sets the radius used for the rounded corners.
- "**border_width**" - the width in pixels of the border around the button. Defaults to 1.
- "**shadow_width**" - the width in pixels of the shadow behind the button. Defaults to 2.
- "**tool_tip_delay**" - time in seconds before a the buttons tool sip (if it has one) will appear. Default is "1.0".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".
- "**text_horiz_alignment**" - Set to "left", "right" or "center". Controls the horizontal placement of the button text, if this button has any text. Default is "center".
- "**text_vert_alignment**" - Set to "top", "bottom or "center". Controls the vertical placement of the button text, if this button has any text. Default is "center".
- "**text_horiz_alignment_padding**" - If horizontal alignment is set to 'left' or 'right' this value will control the buffer between the edge of the button and where we start placing the text. Default is "1".
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_drop_down_menu.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Misc
- "**border_width**" - the width in pixels of the border around the drop down menu. Defaults to "1".
- "**shadow_width**" - the width in pixels of the shadow behind the button. Defaults to "1".
- "**open_button_width**" - the width of the open/close button on the right hand side of the drop down. Defaults to "20" (pixels). Set it to "0" to remove the open/close button.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Misc
- "**border_width**" - the width in pixels of the border around the bar. Defaults to 1.
- "**shadow_width**" - the width in pixels of the shadow behind the bar. Defaults to 1.
- "**enable_arrow_buttons**" - Enables or disables the arrow buttons for the scroll bar. "1" is enabled, "0" is disabled. Defaults to "1".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_horizontal_slider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Misc
- "**shadow_width**" - the width in pixels of the shadow behind the slider. Defaults to 1.
- "**enable_arrow_buttons**" - Enables or disables the arrow buttons for the slider. "1" is enabled, "0" is disabled. Defaults to "1".
- "**sliding_button_width**" - Sets the width of the sliding button. Defaults to "20".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_label.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ Misc
- "**text_vert_alignment**" - Set to "top", "bottom or "center". Controls the vertical placement of the label text. Default is "center".
- "**text_horiz_alignment_padding**" - If horizontal alignment is set to 'left' or 'right' this value will control the buffer between the edge of the label rectangle and where we start placing the text. Default is "0".
- "**text_vert_alignment_padding**" - If vertical alignment is set to 'top' or 'bottom' this value will control the buffer between the edge of the label rectangle and where we start placing the text. Default is "0".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Example
-------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_panel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Misc
- "**shape_corner_radius**" - Only used if our shape is 'rounded_rectangle'. It sets the radius used for the rounded corners. Defaults to "2".
- "**border_width**" - The width of the border around the element in pixels. Defaults to "1".
- "**shadow_width**" - The width of the shadow around the element in pixels. Defaults to "2".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".


Example
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_progress_bar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Misc
- "**shape_corner_radius**" - Only used if our shape is 'rounded_rectangle'. It sets the radius used for the rounded corners.
- "**border_width**" - the width in pixels of the border around the bar. Defaults to 1.
- "**shadow_width**" - the width in pixels of the shadow behind the bar. Defaults to 1.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".


Font
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Misc
- "**shape_corner_radius**" - Only used if our shape is 'rounded_rectangle'. It sets the radius used for the rounded corners.
- "**border_width**" - the width in pixels of the border around the bar. Defaults to 1.
- "**shadow_width**" - the width in pixels of the shadow behind the bar. Defaults to 1.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".


Font
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_selection_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Misc
- "**border_width**" - The width of the border around the element in pixels. Defaults to "1".
- "**shadow_width**" - The width of the shadow around the element in pixels. Defaults to "2".
- "**list_item_height**" - The pixel height of a the items in the list. Defaults to "20".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_status_bar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Misc
- "**follow_sprite_offset**" - The x,y offset values for when the bar follows a sprite. Defaults to "0,0".
- "**border_width**" - The width of the border around the health bar. Defaults to "1". Can be "0" to remove the border.
- "**shadow_width**" - The width of the border around the health bar. Defaults to "1". Can be "0" to remove the border.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Example
-------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_text_box.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Misc
- "**text_horiz_alignment_padding**" - If horizontal alignment is set to 'left' or 'right' this value will control the buffer between the edge of the box padding and where we start placing the text. Default is "0". Using "padding" is better but these parameters are included for completeness.
- "**text_vert_alignment_padding**" - If vertical alignment is set to 'top' or 'bottom' this value will control the buffer between the edge of the box padding and where we start placing the text. Default is "0". Using "padding" is better but these parameters are included for completeness.
- "**line_spacing**" - Sets the spacing of lines of text. The default is 1.25.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_text_entry_box.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Misc
- "**text_horiz_alignment_padding**" - If horizontal alignment is set to 'left' or 'right' this value will control the buffer between the edge of the box padding and where we start placing the text. Default is "0". Using "padding" is better but these parameters are included for completeness.
- "**text_vert_alignment_padding**" - If vertical alignment is set to 'top' or 'bottom' this value will control the buffer between the edge of the box padding and where we start placing the text. Default is "0". Using "padding" is better but these parameters are included for completeness.
- "**line_spacing**" - Sets the spacing of lines of text. The default is 1.25.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_text_entry_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Misc
- "**border_width**" - the width of the border around the element in pixels. Defaults to "1".
- "**shadow_width**" - the width of the shadow around the element in pixels. Defaults to "1".
- "**padding**" - the horizontal and vertical 'padding' between the border and where we render the text. Defaults to "4,2".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Example
-------
Expand Down
1 change: 0 additions & 1 deletion docs/source/theme_reference/theme_tooltip.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Misc

- "**rect_width**" - The width of the rectangle around the tool tip in pixels, including any shadows or borders. The height is determined dynamically.


Sub-elements
--------------

Expand Down
1 change: 1 addition & 0 deletions docs/source/theme_reference/theme_vertical_scroll_bar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Misc
- "**border_width**" - the width in pixels of the border around the bar. Defaults to 1.
- "**shadow_width**" - the width in pixels of the shadow behind the bar. Defaults to 1.
- "**enable_arrow_buttons**" - Enables or disables the arrow buttons for the scroll bar. "1" is enabled, "0" is disabled. Defaults to "1".
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Sub-elements
--------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Misc
- "**follow_sprite_offset**" - The x,y offset values the bar relative to the sprite. Defaults to "0,0".
- "**border_width**" - The width of the border around the health bar. Defaults to "1". Can be "0" to remove the border.
- "**shadow_width**" - The width of the border around the health bar. Defaults to "1". Can be "0" to remove the border.
- "**tool_tip_delay**" - time in seconds before the button's tool tip (if it has one) will appear. Default is "1.0".

Example
-------
Expand Down
2 changes: 2 additions & 0 deletions pygame_gui/core/interfaces/manager_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def create_tool_tip(self,
parent_element: IUIElementInterface,
object_id: ObjectID,
*,
wrap_width: Optional[int] = None,
text_kwargs: Optional[Dict[str, str]] = None) -> IUITooltipInterface:
"""
Creates a tool tip ands returns it.
Expand All @@ -240,6 +241,7 @@ def create_tool_tip(self,
:param hover_distance: The distance we should hover away from our target position.
:param parent_element: The UIElement that spawned this tool tip.
:param object_id: the object_id of the tooltip.
:param wrap_width: an optional width for the tool tip, will overwrite any value from the theme file.
:param text_kwargs: a dictionary of variable arguments to pass to the translated string
useful when you have multiple translations that need variables inserted
in the middle.
Expand Down
55 changes: 48 additions & 7 deletions pygame_gui/core/ui_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[int, int, int, int]],
self.border_width = None # type: Union[None, int]
self.shape_corner_radius = None # type: Union[None, int]

self.tool_tip_text = None
self.tool_tip_text_kwargs = None
self.tool_tip_object_id = None
self.tool_tip_delay = 1.0
self.tool_tip_wrap_width = None
self.tool_tip = None

self._setup_container(container)
Expand Down Expand Up @@ -758,6 +763,8 @@ def kill(self):
"""
Overriding regular sprite kill() method to remove the element from it's container.
"""
if self.tool_tip is not None:
self.tool_tip.kill()
self.ui_container.remove_element(self)
self.remove_element_from_focus_set(self)
super().kill()
Expand Down Expand Up @@ -811,23 +818,42 @@ def on_fresh_drawable_shape_ready(self):

def on_hovered(self):
"""
A stub to override. Called when this UI element first enters the 'hovered' state.
Called when this UI element first enters the 'hovered' state.
"""
self.hover_time = 0.0

def on_unhovered(self):
"""
A stub to override. Called when this UI element leaves the 'hovered' state.
Called when this UI element leaves the 'hovered' state.
"""
if self.tool_tip is not None:
self.tool_tip.kill()
self.tool_tip = None

def while_hovering(self, time_delta: float, mouse_pos: pygame.math.Vector2):
def while_hovering(self, time_delta: float,
mouse_pos: Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]]):
"""
A stub method to override. Called when this UI element is currently hovered.
Called while we are in the hover state. It will create a tool tip if we've been in the
hover state for a while, the text exists to create one and we haven't created one already.
:param time_delta: A float, the time in seconds between the last call to this function
and now (roughly).
:param mouse_pos: The current position of the mouse as 2D Vector.
:param time_delta: Time in seconds between calls to update.
:param mouse_pos: The current position of the mouse.
"""
if (self.tool_tip is None and self.tool_tip_text is not None and
self.hover_time > self.tool_tip_delay):
hover_height = int(self.rect.height / 2)
self.tool_tip = self.ui_manager.create_tool_tip(text=self.tool_tip_text,
position=(mouse_pos[0],
self.rect.centery),
hover_distance=(0,
hover_height),
parent_element=self,
object_id=self.tool_tip_object_id,
wrap_width=self.tool_tip_wrap_width,
text_kwargs=self.tool_tip_text_kwargs)

self.hover_time += time_delta

def can_hover(self) -> bool:
"""
Expand Down Expand Up @@ -1186,3 +1212,18 @@ def update_theming(self, new_theming_data: str):
"""
self.ui_theme.update_single_element_theming(self.most_specific_combined_id, new_theming_data)
self.rebuild_from_changed_theme_data()

def set_tooltip(self, text: Optional[str] = None, object_id: Optional[ObjectID] = None,
text_kwargs: Optional[Dict[str, str]] = None, delay: Optional[float] = None,
wrap_width: Optional[int] = None):
self.tool_tip_text = text
self.tool_tip_text_kwargs = {}
if text_kwargs is not None:
self.tool_tip_text_kwargs = text_kwargs
self.tool_tip_object_id = object_id

if delay is not None:
self.tool_tip_delay = delay

self.tool_tip_wrap_width = wrap_width

48 changes: 4 additions & 44 deletions pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame

self.dynamic_dimensions_orig_top_left = rel_rect.topleft
# support for an optional 'tool tip' element attached to this button
self.tool_tip_text = tool_tip_text
self.tool_tip_text_kwargs = {}
if tool_tip_text_kwargs is not None:
self.tool_tip_text_kwargs = tool_tip_text_kwargs
self.tool_tip_object_id = tool_tip_object_id
self.set_tooltip(tool_tip_text, tool_tip_object_id, tool_tip_text_kwargs)
self.ui_root_container = self.ui_manager.get_root_container()

# Some different states our button can be in, could use a state machine for this
Expand All @@ -100,9 +96,6 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
# Used to check button pressed without going through pygame.Event system
self.pressed_event = False

# time the hovering
self.hover_time = 0.0

# timer for double clicks
self.last_click_button = None
self.allow_double_clicks = allow_double_clicks
Expand All @@ -128,8 +121,6 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
self.selected_image = None
self.disabled_image = None

self.tool_tip_delay = 1.0

self.text_horiz_alignment = 'center'
self.text_vert_alignment = 'center'
self.text_horiz_alignment_padding = 0
Expand Down Expand Up @@ -202,8 +193,6 @@ def kill(self):
Overrides the standard sprite kill method to also kill any tooltips belonging to
this button.
"""
if self.tool_tip is not None:
self.tool_tip.kill()
super().kill()

def hover_point(self, hover_x: int, hover_y: int) -> bool:
Expand Down Expand Up @@ -239,8 +228,8 @@ def on_hovered(self):
Called when we enter the hover state, it sets the colours and image of the button
to the appropriate values and redraws it.
"""
super().on_hovered()
self.drawable_shape.set_active_state('hovered')
self.hover_time = 0.0

# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_ON_HOVERED),
Expand All @@ -252,40 +241,14 @@ def on_hovered(self):
'ui_object_id': self.most_specific_combined_id}
pygame.event.post(pygame.event.Event(UI_BUTTON_ON_HOVERED, event_data))

def while_hovering(self, time_delta: float,
mouse_pos: Union[pygame.math.Vector2, Tuple[int, int], Tuple[float, float]]):
"""
Called while we are in the hover state. It will create a tool tip if we've been in the
hover state for a while, the text exists to create one and we haven't created one already.
:param time_delta: Time in seconds between calls to update.
:param mouse_pos: The current position of the mouse.
"""
if (self.tool_tip is None and self.tool_tip_text is not None and
self.hover_time > self.tool_tip_delay):
hover_height = int(self.rect.height / 2)
self.tool_tip = self.ui_manager.create_tool_tip(text=self.tool_tip_text,
position=(mouse_pos[0],
self.rect.centery),
hover_distance=(0,
hover_height),
parent_element=self,
object_id=self.tool_tip_object_id,
text_kwargs=self.tool_tip_text_kwargs)

self.hover_time += time_delta

def on_unhovered(self):
"""
Called when we leave the hover state. Resets the colours and images to normal and kills any
tooltip that was created while we were hovering the button.
"""
super().on_unhovered()
if self.drawable_shape is not None:
self.drawable_shape.set_active_state(self._get_appropriate_state_name())
if self.tool_tip is not None:
self.tool_tip.kill()
self.tool_tip = None

# old event to remove in 0.8.0
event_data = {'user_type': OldType(UI_BUTTON_ON_UNHOVERED),
Expand Down Expand Up @@ -381,10 +344,7 @@ def process_event(self, event: pygame.event.Event) -> bool:
self.hovered = False
self.on_unhovered()
self._set_active()
self.hover_time = 0.0
if self.tool_tip is not None:
self.tool_tip.kill()
self.tool_tip = None

consumed_event = True
if event.type == pygame.MOUSEBUTTONUP and event.button in self.generate_click_events_from:
scaled_mouse_pos = self.ui_manager.calculate_scaled_mouse_position(event.pos)
Expand Down
5 changes: 5 additions & 0 deletions pygame_gui/elements/ui_drop_down_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,11 @@ def rebuild_from_changed_theme_data(self):
casting_func=int):
has_any_changed = True

if self._check_misc_theme_data_changed(attribute_name='tool_tip_delay',
default_value=1.0,
casting_func=float):
has_any_changed = True

if self._check_shape_theming_changed(defaults={'border_width': 1,
'shadow_width': 2,
'shape_corner_radius': 2}):
Expand Down
Loading

0 comments on commit 206ec68

Please sign in to comment.