Skip to content

Commit 4872df2

Browse files
authored
Merge pull request #383 from MyreMylar/add-object-ids-to-button-tool-tips
Expand create tool tip method w. parent element & ObjectID option
2 parents d615c14 + 1855a22 commit 4872df2

File tree

7 files changed

+72
-22
lines changed

7 files changed

+72
-22
lines changed

pygame_gui/core/interfaces/element_interface.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,23 @@ def get_element_ids(self) -> List[str]:
3434
"""
3535
A list of all the element IDs in this element's theming/event hierarchy.
3636
37-
:return: a list of strings, one ofr each element in the hierarchy.
37+
:return: a list of strings, one for each element in the hierarchy.
38+
"""
39+
40+
@abstractmethod
41+
def get_class_ids(self) -> List[str]:
42+
"""
43+
A list of all the class IDs in this element's theming/event hierarchy.
44+
45+
:return: a list of strings, one for each element in the hierarchy.
46+
"""
47+
48+
@abstractmethod
49+
def get_object_ids(self) -> List[str]:
50+
"""
51+
A list of all the object IDs in this element's theming/event hierarchy.
52+
53+
:return: a list of strings, one for each element in the hierarchy.
3854
"""
3955

4056
@abstractmethod

pygame_gui/core/interfaces/manager_interface.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pygame_gui.core.interfaces.container_interface import IUIContainerInterface
99
from pygame_gui.core.interfaces.window_stack_interface import IUIWindowStackInterface
1010
from pygame_gui.core.interfaces.tool_tip_interface import IUITooltipInterface
11+
from pygame_gui.core.object_id import ObjectID
1112

1213

1314
class IUIManagerInterface(metaclass=ABCMeta):
@@ -227,6 +228,8 @@ def create_tool_tip(self,
227228
text: str,
228229
position: Tuple[int, int],
229230
hover_distance: Tuple[int, int],
231+
parent_element: IUIElementInterface,
232+
object_id: ObjectID,
230233
*,
231234
text_kwargs: Optional[Dict[str, str]] = None) -> IUITooltipInterface:
232235
"""
@@ -235,6 +238,8 @@ def create_tool_tip(self,
235238
:param text: The tool tips text, can utilise the HTML subset used in all UITextBoxes.
236239
:param position: The screen position to create the tool tip for.
237240
:param hover_distance: The distance we should hover away from our target position.
241+
:param parent_element: The UIElement that spawned this tool tip.
242+
:param object_id: the object_id of the tooltip.
238243
:param text_kwargs: a dictionary of variable arguments to pass to the translated string
239244
useful when you have multiple translations that need variables inserted
240245
in the middle.

pygame_gui/core/object_id.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from sys import version_info
2+
from collections import namedtuple
3+
4+
if version_info.minor >= 7:
5+
ObjectID = namedtuple('ObjectID',
6+
field_names=('object_id', 'class_id'),
7+
defaults=(None, None))
8+
else:
9+
ObjectID = namedtuple('ObjectID', field_names=('object_id', 'class_id'))

pygame_gui/core/ui_element.py

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from sys import version_info
2-
31
import warnings
4-
from collections import namedtuple
2+
53
from typing import List, Union, Tuple, Dict, Any, Callable, Set, Optional
64
from typing import TYPE_CHECKING
75

@@ -13,18 +11,11 @@
1311
from pygame_gui.core.utility import basic_blit
1412
from pygame_gui.core.layered_gui_group import GUISprite
1513
from pygame_gui.core.utility import get_default_manager
16-
14+
from pygame_gui.core.object_id import ObjectID
1715

1816
if TYPE_CHECKING:
1917
from pygame_gui.core.drawable_shapes.drawable_shape import DrawableShape
2018

21-
if version_info.minor >= 7:
22-
ObjectID = namedtuple('ObjectID',
23-
field_names=('object_id', 'class_id'),
24-
defaults=(None, None))
25-
else:
26-
ObjectID = namedtuple('ObjectID', field_names=('object_id', 'class_id'))
27-
2819

2920
class UIElement(GUISprite, IUIElementInterface):
3021
"""
@@ -285,13 +276,29 @@ def get_element_ids(self) -> List[str]:
285276
"""
286277
A list of all the element IDs in this element's theming/event hierarchy.
287278
288-
:return: a list of strings, one ofr each element in the hierarchy.
279+
:return: a list of strings, one for each element in the hierarchy.
289280
"""
290281
return self.element_ids
291282

283+
def get_class_ids(self) -> List[str]:
284+
"""
285+
A list of all the class IDs in this element's theming/event hierarchy.
286+
287+
:return: a list of strings, one for each element in the hierarchy.
288+
"""
289+
return self.class_ids
290+
291+
def get_object_ids(self) -> List[str]:
292+
"""
293+
A list of all the object IDs in this element's theming/event hierarchy.
294+
295+
:return: a list of strings, one for each element in the hierarchy.
296+
"""
297+
return self.object_ids
298+
292299
def _create_valid_ids(self,
293300
container: Union[IContainerLikeInterface, None],
294-
parent_element: Union[None, 'UIElement'],
301+
parent_element: Union[None, IUIElementInterface],
295302
object_id: Union[ObjectID, str, None],
296303
element_id: str):
297304
"""
@@ -308,9 +315,10 @@ def _create_valid_ids(self,
308315
:param element_id: A string ID representing this element's class.
309316
310317
"""
318+
id_parent: Union[IContainerLikeInterface, IUIElementInterface, None] = None
311319
if parent_element is None and container is not None:
312320
id_parent = container
313-
else:
321+
elif parent_element is not None:
314322
id_parent = parent_element
315323

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

328336
if id_parent is not None:
329-
self.element_ids = id_parent.element_ids.copy()
337+
self.element_ids = id_parent.get_element_ids().copy()
330338
self.element_ids.append(element_id)
331339

332-
self.class_ids = id_parent.class_ids.copy()
340+
self.class_ids = id_parent.get_class_ids().copy()
333341
self.class_ids.append(class_id)
334342

335-
self.object_ids = id_parent.object_ids.copy()
343+
self.object_ids = id_parent.get_object_ids().copy()
336344
self.object_ids.append(obj_id)
337345
else:
338346
self.element_ids = [element_id]

pygame_gui/elements/ui_button.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
5959
generate_click_events_from: Iterable[int] = frozenset([pygame.BUTTON_LEFT]),
6060
visible: int = 1,
6161
*,
62+
tool_tip_object_id: Optional[ObjectID] = None,
6263
text_kwargs: Optional[Dict[str, str]] = None,
6364
tool_tip_text_kwargs: Optional[Dict[str, str]] = None,
6465
):
@@ -91,6 +92,7 @@ def __init__(self, relative_rect: Union[pygame.Rect, Tuple[float, float], pygame
9192
if tool_tip_text_kwargs is not None:
9293
self.tool_tip_text_kwargs = tool_tip_text_kwargs
9394
self.tool_tip = None
95+
self.tool_tip_object_id = tool_tip_object_id
9496
self.ui_root_container = self.ui_manager.get_root_container()
9597

9698
# Some different states our button can be in, could use a state machine for this
@@ -272,6 +274,8 @@ def while_hovering(self, time_delta: float,
272274
self.rect.centery),
273275
hover_distance=(0,
274276
hover_height),
277+
parent_element=self,
278+
object_id=self.tool_tip_object_id,
275279
text_kwargs=self.tool_tip_text_kwargs)
276280

277281
self.hover_time += time_delta

pygame_gui/elements/ui_tool_tip.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pygame
66

77
from pygame_gui.core import ObjectID
8-
from pygame_gui.core.interfaces import IUIManagerInterface, IUITooltipInterface
8+
from pygame_gui.core.interfaces import IUIManagerInterface, IUITooltipInterface, IUIElementInterface
99
from pygame_gui.core.ui_element import UIElement
1010

1111
from pygame_gui.elements.ui_text_box import UITextBox
@@ -39,7 +39,7 @@ def __init__(self,
3939
html_text: str,
4040
hover_distance: Tuple[int, int],
4141
manager: Optional[IUIManagerInterface] = None,
42-
parent_element: Optional[UIElement] = None,
42+
parent_element: Optional[IUIElementInterface] = None,
4343
object_id: Optional[Union[ObjectID, str]] = None,
4444
anchors: Optional[Dict[str, Union[str, UIElement]]] = None,
4545
*,

pygame_gui/ui_manager.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from pygame_gui.core.resource_loaders import IResourceLoader, BlockingThreadedResourceLoader
1717
from pygame_gui.core.utility import PackageResource, get_default_manager, set_default_manager
1818
from pygame_gui.core.layered_gui_group import LayeredGUIGroup
19+
from pygame_gui.core import ObjectID
1920

2021
from pygame_gui.elements import UITooltip
2122

@@ -526,8 +527,12 @@ def get_universal_empty_surface(self) -> pygame.surface.Surface:
526527
"""
527528
return self.universal_empty_surface
528529

529-
def create_tool_tip(self, text: str, position: Tuple[int, int],
530+
def create_tool_tip(self,
531+
text: str,
532+
position: Tuple[int, int],
530533
hover_distance: Tuple[int, int],
534+
parent_element: IUIElementInterface,
535+
object_id: ObjectID,
531536
*,
532537
text_kwargs: Optional[Dict[str, str]] = None) -> IUITooltipInterface:
533538
"""
@@ -537,13 +542,16 @@ def create_tool_tip(self, text: str, position: Tuple[int, int],
537542
:param text: The tool tips text, can utilise the HTML subset used in all UITextBoxes.
538543
:param position: The screen position to create the tool tip for.
539544
:param hover_distance: The distance we should hover away from our target position.
545+
:param parent_element: The UIElement that spawned this tool tip.
546+
:param object_id: the object_id of the tooltip.
540547
:param text_kwargs: a dictionary of variable arguments to pass to the translated string
541548
useful when you have multiple translations that need variables inserted
542549
in the middle.
543550
544551
:return: A tool tip placed somewhere on the screen.
545552
"""
546-
tool_tip = UITooltip(text, hover_distance, self, text_kwargs=text_kwargs)
553+
tool_tip = UITooltip(text, hover_distance, self, text_kwargs=text_kwargs,
554+
parent_element=parent_element, object_id=object_id)
547555
tool_tip.find_valid_position(pygame.math.Vector2(position[0], position[1]))
548556
return tool_tip
549557

0 commit comments

Comments
 (0)