aboutsummaryrefslogtreecommitdiff
path: root/game/src/Game/GameSession/Tooltip.gd
blob: f079e4c5f9905e8036b3505f464ffacdaadd9949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
extends GUINode

var _tooltip_label : GUILabel

func _ready() -> void:
   add_gui_element("core", "ToolTip")

   _tooltip_label = get_gui_label_from_nodepath(^"./ToolTip")
   if _tooltip_label:
      _update_tooltip_max_size()
      _tooltip_label.set_auto_adjust_to_content_size(true)

      MenuSingleton.update_tooltip.connect(update_tooltip)

   hide()

func _notification(what : int) -> void:
   match what:
      NOTIFICATION_RESIZED:
         _update_tooltip_max_size()

func _update_tooltip_max_size() -> void:
   if _tooltip_label:
      var max_size : Vector2 = _tooltip_label.get_base_max_size()
      var window_size : Vector2 = get_size()
      _tooltip_label.set_max_size(Vector2(min(max_size.x, window_size.x), window_size.y))

func update_tooltip(text : String, substitution_dict : Dictionary, position : Vector2) -> void:
   if text and _tooltip_label:
      _tooltip_label.set_text(text)
      _tooltip_label.set_substitution_dict(substitution_dict)
      _tooltip_label.force_update_lines()

      var adjusted_rect : Rect2 = _tooltip_label.get_adjusted_rect()

      # Shift position so that the tooltip doesn't go past the bottom or right sides of the window
      var bottom_right : Vector2 = position + adjusted_rect.position + adjusted_rect.size - get_size()
      if bottom_right.x > 0:
         position.x -= bottom_right.x
      if bottom_right.y > 0:
         position.y -= bottom_right.y

      # Shift position so that the tooltip doesn't go past the top or left sides of the window
      var top_left : Vector2 = position + adjusted_rect.position
      if top_left.x < 0:
         position.x -= top_left.x
      if top_left.y < 0:
         position.y -= top_left.y

      _tooltip_label.set_position(position)

      show()
   else:
      hide()