Skip to content

Commit

Permalink
Merge pull request #383 from MyreMylar/add-object-ids-to-button-tool-…
Browse files Browse the repository at this point in the history
…tips

Expand create tool tip method w. parent element & ObjectID option
  • Loading branch information
MyreMylar authored Dec 23, 2022
2 parents d615c14 + 1855a22 commit 4872df2
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 22 deletions.
18 changes: 17 additions & 1 deletion pygame_gui/core/interfaces/element_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,23 @@ def get_element_ids(self) -> List[str]:
"""
A list of all the element IDs in this element's theming/event hierarchy.
:return: a list of strings, one ofr each element in the hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""

@abstractmethod
def get_class_ids(self) -> List[str]:
"""
A list of all the class IDs in this element's theming/event hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""

@abstractmethod
def get_object_ids(self) -> List[str]:
"""
A list of all the object IDs in this element's theming/event hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""

@abstractmethod
Expand Down
5 changes: 5 additions & 0 deletions pygame_gui/core/interfaces/manager_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pygame_gui.core.interfaces.container_interface import IUIContainerInterface
from pygame_gui.core.interfaces.window_stack_interface import IUIWindowStackInterface
from pygame_gui.core.interfaces.tool_tip_interface import IUITooltipInterface
from pygame_gui.core.object_id import ObjectID


class IUIManagerInterface(metaclass=ABCMeta):
Expand Down Expand Up @@ -227,6 +228,8 @@ def create_tool_tip(self,
text: str,
position: Tuple[int, int],
hover_distance: Tuple[int, int],
parent_element: IUIElementInterface,
object_id: ObjectID,
*,
text_kwargs: Optional[Dict[str, str]] = None) -> IUITooltipInterface:
"""
Expand All @@ -235,6 +238,8 @@ def create_tool_tip(self,
:param text: The tool tips text, can utilise the HTML subset used in all UITextBoxes.
:param position: The screen position to create the tool tip for.
: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 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
9 changes: 9 additions & 0 deletions pygame_gui/core/object_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from sys import version_info
from collections import namedtuple

if version_info.minor >= 7:
ObjectID = namedtuple('ObjectID',
field_names=('object_id', 'class_id'),
defaults=(None, None))
else:
ObjectID = namedtuple('ObjectID', field_names=('object_id', 'class_id'))
42 changes: 25 additions & 17 deletions pygame_gui/core/ui_element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from sys import version_info

import warnings
from collections import namedtuple

from typing import List, Union, Tuple, Dict, Any, Callable, Set, Optional
from typing import TYPE_CHECKING

Expand All @@ -13,18 +11,11 @@
from pygame_gui.core.utility import basic_blit
from pygame_gui.core.layered_gui_group import GUISprite
from pygame_gui.core.utility import get_default_manager

from pygame_gui.core.object_id import ObjectID

if TYPE_CHECKING:
from pygame_gui.core.drawable_shapes.drawable_shape import DrawableShape

if version_info.minor >= 7:
ObjectID = namedtuple('ObjectID',
field_names=('object_id', 'class_id'),
defaults=(None, None))
else:
ObjectID = namedtuple('ObjectID', field_names=('object_id', 'class_id'))


class UIElement(GUISprite, IUIElementInterface):
"""
Expand Down Expand Up @@ -285,13 +276,29 @@ def get_element_ids(self) -> List[str]:
"""
A list of all the element IDs in this element's theming/event hierarchy.
:return: a list of strings, one ofr each element in the hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""
return self.element_ids

def get_class_ids(self) -> List[str]:
"""
A list of all the class IDs in this element's theming/event hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""
return self.class_ids

def get_object_ids(self) -> List[str]:
"""
A list of all the object IDs in this element's theming/event hierarchy.
:return: a list of strings, one for each element in the hierarchy.
"""
return self.object_ids

def _create_valid_ids(self,
container: Union[IContainerLikeInterface, None],
parent_element: Union[None, 'UIElement'],
parent_element: Union[None, IUIElementInterface],
object_id: Union[ObjectID, str, None],
element_id: str):
"""
Expand All @@ -308,9 +315,10 @@ def _create_valid_ids(self,
:param element_id: A string ID representing this element's class.
"""
id_parent: Union[IContainerLikeInterface, IUIElementInterface, None] = None
if parent_element is None and container is not None:
id_parent = container
else:
elif parent_element is not None:
id_parent = parent_element

if isinstance(object_id, str):
Expand All @@ -326,13 +334,13 @@ def _create_valid_ids(self,
class_id = None

if id_parent is not None:
self.element_ids = id_parent.element_ids.copy()
self.element_ids = id_parent.get_element_ids().copy()
self.element_ids.append(element_id)

self.class_ids = id_parent.class_ids.copy()
self.class_ids = id_parent.get_class_ids().copy()
self.class_ids.append(class_id)

self.object_ids = id_parent.object_ids.copy()
self.object_ids = id_parent.get_object_ids().copy()
self.object_ids.append(obj_id)
else:
self.element_ids = [element_id]
Expand Down
4 changes: 4 additions & 0 deletions pygame_gui/elements/ui_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
generate_click_events_from: Iterable[int] = frozenset([pygame.BUTTON_LEFT]),
visible: int = 1,
*,
tool_tip_object_id: Optional[ObjectID] = None,
text_kwargs: Optional[Dict[str, str]] = None,
tool_tip_text_kwargs: Optional[Dict[str, str]] = None,
):
Expand Down Expand Up @@ -91,6 +92,7 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
if tool_tip_text_kwargs is not None:
self.tool_tip_text_kwargs = tool_tip_text_kwargs
self.tool_tip = None
self.tool_tip_object_id = tool_tip_object_id
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 Down Expand Up @@ -272,6 +274,8 @@ def while_hovering(self, time_delta: float,
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
Expand Down
4 changes: 2 additions & 2 deletions pygame_gui/elements/ui_tool_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pygame

from pygame_gui.core import ObjectID
from pygame_gui.core.interfaces import IUIManagerInterface, IUITooltipInterface
from pygame_gui.core.interfaces import IUIManagerInterface, IUITooltipInterface, IUIElementInterface
from pygame_gui.core.ui_element import UIElement

from pygame_gui.elements.ui_text_box import UITextBox
Expand Down Expand Up @@ -39,7 +39,7 @@ def __init__(self,
html_text: str,
hover_distance: Tuple[int, int],
manager: Optional[IUIManagerInterface] = None,
parent_element: Optional[UIElement] = None,
parent_element: Optional[IUIElementInterface] = None,
object_id: Optional[Union[ObjectID, str]] = None,
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
*,
Expand Down
12 changes: 10 additions & 2 deletions pygame_gui/ui_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pygame_gui.core.resource_loaders import IResourceLoader, BlockingThreadedResourceLoader
from pygame_gui.core.utility import PackageResource, get_default_manager, set_default_manager
from pygame_gui.core.layered_gui_group import LayeredGUIGroup
from pygame_gui.core import ObjectID

from pygame_gui.elements import UITooltip

Expand Down Expand Up @@ -526,8 +527,12 @@ def get_universal_empty_surface(self) -> pygame.surface.Surface:
"""
return self.universal_empty_surface

def create_tool_tip(self, text: str, position: Tuple[int, int],
def create_tool_tip(self,
text: str,
position: Tuple[int, int],
hover_distance: Tuple[int, int],
parent_element: IUIElementInterface,
object_id: ObjectID,
*,
text_kwargs: Optional[Dict[str, str]] = None) -> IUITooltipInterface:
"""
Expand All @@ -537,13 +542,16 @@ def create_tool_tip(self, text: str, position: Tuple[int, int],
:param text: The tool tips text, can utilise the HTML subset used in all UITextBoxes.
:param position: The screen position to create the tool tip for.
: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 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.
:return: A tool tip placed somewhere on the screen.
"""
tool_tip = UITooltip(text, hover_distance, self, text_kwargs=text_kwargs)
tool_tip = UITooltip(text, hover_distance, self, text_kwargs=text_kwargs,
parent_element=parent_element, object_id=object_id)
tool_tip.find_valid_position(pygame.math.Vector2(position[0], position[1]))
return tool_tip

Expand Down

0 comments on commit 4872df2

Please sign in to comment.