@@ -15,15 +15,21 @@ const INVISIBLE_RECT: Rect2 = Rect2()
15
15
signal visibility_update_required
16
16
17
17
@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 == "" )
20
25
21
26
@export_category ("Appereance" )
27
+ ## Will cause the tooltip to automatically choose a direction, ignores the popup_dir property while active.
22
28
@export var auto_choose_direction : bool = true
23
29
24
30
@export var popup_dir : PopupDirections = PopupDirections .UP
25
31
26
- @export var text : String = "Placeholder" :
32
+ @export_multiline var text : String = "Placeholder" :
27
33
set (value ):
28
34
if label :
29
35
text = value
@@ -46,7 +52,6 @@ var pinned: bool = false:
46
52
pinned = val
47
53
visibility_update_required .emit ()
48
54
49
- # var keyword_rect_dict: Dictionary
50
55
var panel := Panel .new ()
51
56
var label := RichTextLabel .new ()
52
57
@@ -89,7 +94,7 @@ func _ready() -> void:
89
94
update_input_processing ()
90
95
return
91
96
92
- sub_tooltip = add_to_control (label , true )
97
+ sub_tooltip = Tooltip . add_to_control (label , true )
93
98
94
99
pinned = pinned
95
100
connect_target_signals (get_target ())
@@ -115,6 +120,34 @@ func get_target() -> Control:
115
120
return get_parent () if get_parent () is Control else null
116
121
117
122
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
+
118
151
func get_auto_direction () -> PopupDirections :
119
152
var viewport_rect : Rect2 = get_viewport_rect ()
120
153
@@ -140,6 +173,8 @@ func get_auto_direction() -> PopupDirections:
140
173
var closest_dir_to_border : PopupDirections
141
174
var smallest_value : float = INF
142
175
176
+ print (direction_dist_dict )
177
+
143
178
for direction : PopupDirections in direction_dist_dict :
144
179
var value : float = direction_dist_dict [direction ]
145
180
if value < smallest_value :
@@ -160,32 +195,52 @@ func get_auto_direction() -> PopupDirections:
160
195
return popup_dir
161
196
162
197
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 ()
165
200
166
201
match direction :
167
202
PopupDirections .UP :
168
- position = Vector2 (target_size .x / 2 , 0 )
169
- panel .grow_horizontal = Control .GROW_DIRECTION_BOTH
203
+ position .y = 0
170
204
panel .grow_vertical = Control .GROW_DIRECTION_BEGIN
171
205
172
206
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
175
208
panel .grow_vertical = Control .GROW_DIRECTION_END
176
209
177
210
PopupDirections .LEFT :
178
- position = Vector2 ( 0 , target_size . y / 2 )
211
+ position . x = 0
179
212
panel .grow_horizontal = Control .GROW_DIRECTION_BEGIN
180
- panel .grow_vertical = Control .GROW_DIRECTION_BOTH
181
213
182
214
PopupDirections .RIGHT :
183
- position = Vector2 ( target_size . x , target_size . y / 2 )
215
+ position . x = target_rect . size . x
184
216
panel .grow_horizontal = Control .GROW_DIRECTION_END
185
- panel .grow_vertical = Control .GROW_DIRECTION_BOTH
186
217
187
218
_ :
188
219
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
189
244
190
245
191
246
func update_size ():
@@ -200,12 +255,10 @@ func update_input_processing():
200
255
label .mouse_filter = Control .MOUSE_FILTER_PASS
201
256
else :
202
257
label .mouse_filter = Control .MOUSE_FILTER_IGNORE
203
- pass
204
258
205
259
206
260
func update_rich_text ():
207
261
var enriched_text : String
208
- # var word_ranges: Array[Vector2i]
209
262
210
263
var words : PackedStringArray = text .split (" " )
211
264
var resulting_words : PackedStringArray = words .duplicate ()
@@ -229,25 +282,6 @@ func update_rich_text():
229
282
label .parse_bbcode (enriched_text )
230
283
231
284
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
-
251
285
func on_target_gui_input (event : InputEvent ):
252
286
if event .is_action_pressed (pin_action ):
253
287
pinned = ! pinned
@@ -263,19 +297,21 @@ func on_visibility_update_required():
263
297
264
298
if pinned :
265
299
show ()
300
+ return
301
+
266
302
elif target_hovered :
267
303
show ()
304
+
268
305
elif not target_hovered :
269
306
hide ()
270
307
271
308
if auto_choose_direction :
272
309
popup_dir = get_auto_direction ()
273
310
274
- adjust_position (popup_dir )
311
+ adjust_expansion (popup_dir )
275
312
update_size ()
276
313
update_input_processing ()
277
314
update_rich_text ()
278
- # update_keyword_rects()
279
315
280
316
queue_redraw ()
281
317
label .queue_redraw ()
@@ -291,10 +327,10 @@ func on_label_meta_hover_change(meta, hovered: bool):
291
327
sub_tooltip .hide ()
292
328
293
329
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 :
295
331
var new_tooltip := Tooltip .new ()
296
- new_tooltip .sub = sub
297
- if sub :
332
+ new_tooltip .sub = is_sub
333
+ if is_sub :
298
334
target .add_child (new_tooltip , false , Node .INTERNAL_MODE_FRONT )
299
335
else :
300
336
target .add_child (new_tooltip )
0 commit comments