Skip to content

Commit 1005aba

Browse files
committed
Initial
1 parent a9e259a commit 1005aba

File tree

6 files changed

+154
-45
lines changed

6 files changed

+154
-45
lines changed

classes/Tooltip.gd

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,21 @@ const INVISIBLE_RECT: Rect2 = Rect2()
1515
signal visibility_update_required
1616

1717
@export_category("Settings")
18-
@export var allow_sub_tooltips: bool = true
19-
@export var pin_action: String = "ui_accept"
18+
# TODO: when we update to 4.3.1, i'll add proper sub-tooltip support. The current approach works like ass. So in the meantime this will remain off by default
19+
@export var allow_sub_tooltips: bool = false
20+
@export var pin_action: String = "j_pin_tooltip":
21+
set(val):
22+
pin_action = val
23+
#Disable input if the pin_action is ""
24+
set_process_input(not pin_action == "")
2025

2126
@export_category("Appereance")
27+
## Will cause the tooltip to automatically choose a direction, ignores the popup_dir property while active.
2228
@export var auto_choose_direction: bool = true
2329

2430
@export var popup_dir: PopupDirections = PopupDirections.UP
2531

26-
@export var text: String = "Placeholder":
32+
@export_multiline var text: String = "Placeholder":
2733
set(value):
2834
if label:
2935
text = value
@@ -46,7 +52,6 @@ var pinned: bool = false:
4652
pinned = val
4753
visibility_update_required.emit()
4854

49-
#var keyword_rect_dict: Dictionary
5055
var panel := Panel.new()
5156
var label := RichTextLabel.new()
5257

@@ -89,7 +94,7 @@ func _ready() -> void:
8994
update_input_processing()
9095
return
9196

92-
sub_tooltip = add_to_control(label, true)
97+
sub_tooltip = Tooltip.add_to_control(label, true)
9398

9499
pinned = pinned
95100
connect_target_signals(get_target())
@@ -115,6 +120,34 @@ func get_target() -> Control:
115120
return get_parent() if get_parent() is Control else null
116121

117122

123+
func get_distance_to_border(direction: PopupDirections) -> float:
124+
var viewport_rect: Rect2 = get_viewport_rect()
125+
match direction:
126+
PopupDirections.UP:
127+
return global_position.distance_to(
128+
Vector2(
129+
viewport_rect.position.x + viewport_rect.size.x / 2, viewport_rect.position.y
130+
)
131+
)
132+
PopupDirections.DOWN:
133+
return global_position.distance_to(
134+
Vector2(viewport_rect.position.x + viewport_rect.size.x / 2, viewport_rect.end.y)
135+
)
136+
PopupDirections.LEFT:
137+
return global_position.distance_to(
138+
Vector2(
139+
viewport_rect.position.x, viewport_rect.position.y + viewport_rect.size.y / 2
140+
)
141+
)
142+
PopupDirections.RIGHT:
143+
return global_position.distance_to(
144+
Vector2(viewport_rect.end.x, viewport_rect.position.y + viewport_rect.size.y / 2)
145+
)
146+
_:
147+
push_error("Invalid direction.")
148+
return 0
149+
150+
118151
func get_auto_direction() -> PopupDirections:
119152
var viewport_rect: Rect2 = get_viewport_rect()
120153

@@ -140,6 +173,8 @@ func get_auto_direction() -> PopupDirections:
140173
var closest_dir_to_border: PopupDirections
141174
var smallest_value: float = INF
142175

176+
print(direction_dist_dict)
177+
143178
for direction: PopupDirections in direction_dist_dict:
144179
var value: float = direction_dist_dict[direction]
145180
if value < smallest_value:
@@ -160,32 +195,52 @@ func get_auto_direction() -> PopupDirections:
160195
return popup_dir
161196

162197

163-
func adjust_position(direction: PopupDirections):
164-
var target_size: Vector2 = get_target().size
198+
func adjust_expansion(direction: PopupDirections):
199+
var target_rect: Rect2 = get_target().get_rect()
165200

166201
match direction:
167202
PopupDirections.UP:
168-
position = Vector2(target_size.x / 2, 0)
169-
panel.grow_horizontal = Control.GROW_DIRECTION_BOTH
203+
position.y = 0
170204
panel.grow_vertical = Control.GROW_DIRECTION_BEGIN
171205

172206
PopupDirections.DOWN:
173-
position = Vector2(target_size.x / 2, target_size.y)
174-
panel.grow_horizontal = Control.GROW_DIRECTION_BOTH
207+
position.y = target_rect.size.y
175208
panel.grow_vertical = Control.GROW_DIRECTION_END
176209

177210
PopupDirections.LEFT:
178-
position = Vector2(0, target_size.y / 2)
211+
position.x = 0
179212
panel.grow_horizontal = Control.GROW_DIRECTION_BEGIN
180-
panel.grow_vertical = Control.GROW_DIRECTION_BOTH
181213

182214
PopupDirections.RIGHT:
183-
position = Vector2(target_size.x, target_size.y / 2)
215+
position.x = target_rect.size.x
184216
panel.grow_horizontal = Control.GROW_DIRECTION_END
185-
panel.grow_vertical = Control.GROW_DIRECTION_BOTH
186217

187218
_:
188219
push_error("Invalid direction." + str(direction))
220+
return
221+
222+
#Adjust the remaining direction of growth.
223+
if direction == PopupDirections.UP or direction == PopupDirections.DOWN:
224+
#If close to the left border, extend to the right.
225+
if (
226+
get_distance_to_border(PopupDirections.LEFT)
227+
< get_distance_to_border(PopupDirections.RIGHT)
228+
):
229+
panel.grow_horizontal = Control.GROW_DIRECTION_END
230+
#If close to the right border...
231+
else:
232+
panel.grow_horizontal = Control.GROW_DIRECTION_BEGIN
233+
234+
elif direction == PopupDirections.LEFT or direction == PopupDirections.RIGHT:
235+
#If close to the upper border, extend down.
236+
if (
237+
get_distance_to_border(PopupDirections.UP)
238+
< get_distance_to_border(PopupDirections.DOWN)
239+
):
240+
panel.grow_vertical = Control.GROW_DIRECTION_END
241+
#If close to the down border...
242+
else:
243+
panel.grow_vertical = Control.GROW_DIRECTION_BEGIN
189244

190245

191246
func update_size():
@@ -200,12 +255,10 @@ func update_input_processing():
200255
label.mouse_filter = Control.MOUSE_FILTER_PASS
201256
else:
202257
label.mouse_filter = Control.MOUSE_FILTER_IGNORE
203-
pass
204258

205259

206260
func update_rich_text():
207261
var enriched_text: String
208-
#var word_ranges: Array[Vector2i]
209262

210263
var words: PackedStringArray = text.split(" ")
211264
var resulting_words: PackedStringArray = words.duplicate()
@@ -229,25 +282,6 @@ func update_rich_text():
229282
label.parse_bbcode(enriched_text)
230283

231284

232-
#func update_keyword_rects():
233-
#var to_fill: Array[Rect2]
234-
#
235-
#for keyword: String in sub_tooltips_to_display.keys():
236-
#var last_find_index: int = 0
237-
#while last_find_index != -1:
238-
#last_find_index = label.text.find(keyword, last_find_index + 1)
239-
#
240-
#if last_find_index == -1:
241-
#continue
242-
#
243-
#var text_rect: Rect2 = calculate_word_position(label, last_find_index, last_find_index + keyword.length())
244-
#
245-
#to_fill.append(text_rect)
246-
#
247-
#keyword_rect_dict[keyword] = to_fill
248-
#print(keyword_rect_dict)
249-
250-
251285
func on_target_gui_input(event: InputEvent):
252286
if event.is_action_pressed(pin_action):
253287
pinned = !pinned
@@ -263,19 +297,21 @@ func on_visibility_update_required():
263297

264298
if pinned:
265299
show()
300+
return
301+
266302
elif target_hovered:
267303
show()
304+
268305
elif not target_hovered:
269306
hide()
270307

271308
if auto_choose_direction:
272309
popup_dir = get_auto_direction()
273310

274-
adjust_position(popup_dir)
311+
adjust_expansion(popup_dir)
275312
update_size()
276313
update_input_processing()
277314
update_rich_text()
278-
#update_keyword_rects()
279315

280316
queue_redraw()
281317
label.queue_redraw()
@@ -291,10 +327,10 @@ func on_label_meta_hover_change(meta, hovered: bool):
291327
sub_tooltip.hide()
292328

293329

294-
static func add_to_control(target: Control, sub: bool = false) -> Tooltip:
330+
static func add_to_control(target: Control, is_sub: bool = false) -> Tooltip:
295331
var new_tooltip := Tooltip.new()
296-
new_tooltip.sub = sub
297-
if sub:
332+
new_tooltip.sub = is_sub
333+
if is_sub:
298334
target.add_child(new_tooltip, false, Node.INTERNAL_MODE_FRONT)
299335
else:
300336
target.add_child(new_tooltip)

project.godot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ j_continue={
167167
, Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(133, 6),"global_position":Vector2(137, 47),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
168168
]
169169
}
170+
j_pin_tooltip={
171+
"deadzone": 0.5,
172+
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":4,"position":Vector2(200, 19),"global_position":Vector2(204, 60),"factor":1.0,"button_index":3,"canceled":false,"pressed":true,"double_click":false,"script":null)
173+
]
174+
}
170175

171176
[internationalization]
172177

scenes/player/gamemenu/GameMenu.tscn

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://dssmk0m8xsvxf"]
1+
[gd_scene load_steps=4 format=3 uid="uid://dssmk0m8xsvxf"]
22

33
[ext_resource type="Theme" uid="uid://dreboohjjcn2f" path="res://assets/themes/LoginPanelTheme.tres" id="1_yn42h"]
44
[ext_resource type="Script" path="res://scenes/player/gamemenu/GameMenu.gd" id="2_dg6jl"]
5+
[ext_resource type="Script" path="res://classes/Tooltip.gd" id="3_l1agu"]
56

67
[node name="GameMenu" type="PanelContainer"]
78
anchors_preset = 15
@@ -52,6 +53,12 @@ layout_mode = 2
5253
text = "I'm Stuck Help!
5354
"
5455

56+
[node name="Tooltip" type="Node2D" parent="MarginContainer/VBoxContainer/UnstuckButtonMarginContainer/UnstuckButton"]
57+
script = ExtResource("3_l1agu")
58+
pin_action = ""
59+
text = "Kills you instantly!
60+
This allows you to respawn at a proper, non-stuck location."
61+
5562
[node name="QuitButtonMarginContainer" type="MarginContainer" parent="MarginContainer/VBoxContainer"]
5663
layout_mode = 2
5764

scenes/player/shop/Shop.tscn

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=3 format=3 uid="uid://dy1r3n0fpuneq"]
1+
[gd_scene load_steps=4 format=3 uid="uid://dy1r3n0fpuneq"]
22

33
[ext_resource type="Script" path="res://scenes/player/shop/Shop.gd" id="1_wtd5h"]
44
[ext_resource type="PackedScene" uid="uid://dws3rqcdwrf43" path="res://scenes/player/shop/ShopPanel.tscn" id="2_g77pf"]
5+
[ext_resource type="Script" path="res://classes/Tooltip.gd" id="3_5m8cc"]
56

67
[node name="Shop" type="Panel"]
78
offset_right = 269.0
@@ -120,3 +121,11 @@ offset_right = 20.0
120121
grow_horizontal = 2
121122
grow_vertical = 0
122123
metadata/_edit_lock_ = true
124+
125+
[node name="Tooltip" type="Node2D" parent="."]
126+
script = ExtResource("3_5m8cc")
127+
text = "Right click on a shop item to buy it. The number shown depicts the cost in gold."
128+
sub_tooltips_to_display = {
129+
"gold": "Currency, duh.",
130+
"help": ""
131+
}

scenes/player/shortcutmenu/ShortcutMenu.tscn

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=10 format=3 uid="uid://d261xncd2la2h"]
1+
[gd_scene load_steps=11 format=3 uid="uid://d261xncd2la2h"]
22

33
[ext_resource type="Script" path="res://scenes/player/shortcutmenu/ShortcutMenu.gd" id="1_faslu"]
44
[ext_resource type="Texture2D" uid="uid://dqw4dsidoi5wf" path="res://assets/images/icons/789_Lorc_RPG_icons/scaled/Icon.5_85.png" id="1_ryg2n"]
55
[ext_resource type="Texture2D" uid="uid://cwgm6exidxs08" path="res://assets/images/icons/789_Lorc_RPG_icons/scaled/Icon.3_83.png" id="2_481p7"]
6+
[ext_resource type="Script" path="res://classes/Tooltip.gd" id="3_01qcm"]
67
[ext_resource type="Texture2D" uid="uid://ceoisjqj6obhb" path="res://assets/images/icons/789_Lorc_RPG_icons/scaled/Icon.6_94.png" id="3_upl7j"]
78
[ext_resource type="Texture2D" uid="uid://5xbd6go3j1y6" path="res://assets/images/icons/789_Lorc_RPG_icons/scaled/Icon.6_37.png" id="4_pds2v"]
89

@@ -26,20 +27,44 @@ layout_mode = 2
2627
texture_normal = ExtResource("1_ryg2n")
2728
texture_pressed = SubResource("CanvasTexture_djxay")
2829

30+
[node name="Tooltip" type="Node2D" parent="HBoxContainer/GameMenuButton"]
31+
script = ExtResource("3_01qcm")
32+
auto_choose_direction = false
33+
popup_dir = 1
34+
text = "Main menu."
35+
2936
[node name="StatsButton" type="TextureButton" parent="HBoxContainer"]
3037
unique_name_in_owner = true
3138
layout_mode = 2
3239
texture_normal = ExtResource("2_481p7")
3340
texture_pressed = SubResource("CanvasTexture_05pst")
3441

42+
[node name="Tooltip" type="Node2D" parent="HBoxContainer/StatsButton"]
43+
script = ExtResource("3_01qcm")
44+
auto_choose_direction = false
45+
popup_dir = 1
46+
text = "Stats menu."
47+
3548
[node name="EquipmentButton" type="TextureButton" parent="HBoxContainer"]
3649
unique_name_in_owner = true
3750
layout_mode = 2
3851
texture_normal = ExtResource("3_upl7j")
3952
texture_pressed = SubResource("CanvasTexture_3vu0y")
4053

54+
[node name="Tooltip" type="Node2D" parent="HBoxContainer/EquipmentButton"]
55+
script = ExtResource("3_01qcm")
56+
auto_choose_direction = false
57+
popup_dir = 1
58+
text = "Equipment menu."
59+
4160
[node name="BagButton" type="TextureButton" parent="HBoxContainer"]
4261
unique_name_in_owner = true
4362
layout_mode = 2
4463
texture_normal = ExtResource("4_pds2v")
4564
texture_pressed = SubResource("CanvasTexture_x2us5")
65+
66+
[node name="Tooltip" type="Node2D" parent="HBoxContainer/BagButton"]
67+
script = ExtResource("3_01qcm")
68+
auto_choose_direction = false
69+
popup_dir = 1
70+
text = "Inventory."

0 commit comments

Comments
 (0)