Implemented tooltip follows mouse (not perfect).

Fixed bug when using mousewheel action on inventory items.
Added a debug mode for tooltip following mouse
This commit is contained in:
Julian Murgia
2021-02-19 07:53:45 +01:00
parent 52d19c34bd
commit 38f554b496
24 changed files with 421 additions and 174 deletions

View File

Before

Width:  |  Height:  |  Size: 174 B

After

Width:  |  Height:  |  Size: 174 B

View File

@@ -7,6 +7,7 @@ func get_class():
export(float) var mouse_tooltip_margin = 50.0
var tooltip_node : Object
### EDITOR TOOLS ###
enum EDITOR_GAME_DEBUG_DISPLAY {
@@ -16,6 +17,9 @@ enum EDITOR_GAME_DEBUG_DISPLAY {
export(EDITOR_GAME_DEBUG_DISPLAY) var editor_debug_mode = EDITOR_GAME_DEBUG_DISPLAY.NONE setget set_editor_debug_mode
func _ready():
escoria.esc_runner.connect("event_done", self, "_on_event_done")
func set_editor_debug_mode(p_editor_debug_mode : int) -> void:
editor_debug_mode = p_editor_debug_mode
@@ -104,5 +108,29 @@ func show_ui():
func _on_event_done(event_name: String):
pass
func _on_tooltip_position_update_required(p_position : Vector2):
pass
## FUNCTIONS BELOW THIS POINT DON'T NEED TO BE REIMPLEMENTED BY USER
## (Although they can be, if required)
# This function is called if Project setting escoria/ui/tooltip_follows_mouse = true
func update_tooltip_following_mouse_position(p_position : Vector2):
var corrected_position = p_position
# clamp TOP
if tooltip_node.tooltip_distance_to_edge_top(p_position) <= mouse_tooltip_margin:
corrected_position.y = mouse_tooltip_margin
# clamp BOTTOM
if tooltip_node.tooltip_distance_to_edge_bottom(p_position + tooltip_node.rect_size) <= mouse_tooltip_margin:
corrected_position.y = escoria.game_size.y - mouse_tooltip_margin - tooltip_node.rect_size.y
# clamp LEFT
if tooltip_node.tooltip_distance_to_edge_left(p_position - tooltip_node.rect_size/2) <= mouse_tooltip_margin:
corrected_position.x = mouse_tooltip_margin
# clamp RIGHT
if tooltip_node.tooltip_distance_to_edge_right(p_position + tooltip_node.rect_size/2) <= mouse_tooltip_margin:
corrected_position.x = escoria.game_size.x - mouse_tooltip_margin - tooltip_node.rect_size.x
tooltip_node.anchor_right = 0.2
tooltip_node.rect_position = corrected_position + tooltip_node.offset_from_cursor

View File

@@ -0,0 +1,153 @@
tool
extends RichTextLabel
class_name ESCTooltip
func get_class():
return "ESCTooltip"
# Infinitive verb
var current_action : String
# Target item/hotspot
var current_target : String setget set_target
# Preposition: on, with...
var current_prep : String = "with"
# Target 2 item/hotspot
var current_target2 : String
# True if tooltip is waiting for a click on second target (use x with y)
var waiting_for_target2 = false
export(Color) var color setget set_color
export(Vector2) var offset_from_cursor = Vector2(10,0)
export(bool) var debug_mode = false setget set_debug_mode
var debug_texturerect_node : TextureRect
const MAX_WIDTH = 200
const MIN_HEIGHT = 30
const MAX_HEIGHT = 500
const ONE_LINE_HEIGHT = 16
func _ready():
escoria.call_deferred("register_object", self)
escoria.esc_runner.connect("action_changed", self, "on_action_selected")
func set_color(p_color : Color):
color = p_color
update_tooltip_text()
func set_debug_mode(p_debug_mode : bool):
debug_mode = p_debug_mode
if debug_mode:
# Add a white TextureRect behind the RTL to see its actual size
debug_texturerect_node = TextureRect.new()
add_child(debug_texturerect_node)
debug_texturerect_node.texture = load("res://addons/escoria-core/game/assets/images/white.png")
debug_texturerect_node.expand = true
debug_texturerect_node.stretch_mode = TextureRect.STRETCH_TILE
debug_texturerect_node.size_flags_horizontal = SIZE_EXPAND_FILL
debug_texturerect_node.size_flags_vertical = SIZE_EXPAND_FILL
debug_texturerect_node.show_behind_parent = true
debug_texturerect_node.anchor_right = 1.0
debug_texturerect_node.anchor_bottom = 1.0
debug_texturerect_node.mouse_filter = Control.MOUSE_FILTER_IGNORE
move_child(debug_texturerect_node, 2)
else:
remove_child(debug_texturerect_node)
func on_action_selected() -> void:
current_action = escoria.esc_runner.current_action
update_tooltip_text()
func set_target(target : String, needs_second_target : bool = false) -> void:
current_target = target
if needs_second_target:
waiting_for_target2 = true
update_tooltip_text()
func set_target2(target2 : String) -> void:
current_target2 = target2
update_tooltip_text()
func update_tooltip_text():
"""
Overriden method. Should not be called.
"""
pass
func update_size():
## RECT_SIZE ##
var rtl_width = rect_size.x
var rtl_height = rect_size.y
var content_height = get_content_height()
var nb_visible_characters = visible_characters
var nb_visible_lines = get_visible_line_count()
printt("BEFORE", "text_height", content_height, "rtl_height", rect_size.y)
# if text is too long and is wrapped
# var nblines = float(get_content_height()) / float(ONE_LINE_HEIGHT)
var nblines = nb_visible_lines
if nblines >= 1:
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")
var text_height = get_content_height()
if text_height > MAX_HEIGHT:
text_height = MAX_HEIGHT
if text_height <= MIN_HEIGHT:
text_height = MIN_HEIGHT
var parent_width = rect_size.x
# first, try to increase width until it goes above max_width
while parent_width < MAX_WIDTH && float(text_height) / float(ONE_LINE_HEIGHT) > 1.0:
rect_size.x += 1
parent_width = rect_size.x
rect_size.y = text_height
if rect_size.x >= MAX_WIDTH:
rect_size.x = MAX_WIDTH
## END RECT_SIZE ##
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
anchor_left = 0.0
printt("AFTER", "text_height", get_content_height(), "rtl_height", rect_size.y)
func _offset(position):
var center_offset_x = rect_size.x / 2
var offset_y = 5
position.x -= center_offset_x
position.y += offset_y
return position
func tooltip_distance_to_edge_top(position : Vector2):
return position.y
func tooltip_distance_to_edge_bottom(position: Vector2):
return escoria.game_size.y - position.y
func tooltip_distance_to_edge_left(position : Vector2):
return position.x
func tooltip_distance_to_edge_right(position : Vector2):
return escoria.game_size.x - position.x

View File

@@ -20,6 +20,11 @@ func _ready():
connect("mouse_exited", self, "_on_inventory_item_mouse_exit")
func _on_inventory_item_gui_input(event : InputEvent):
if event.is_action_pressed("switch_action_verb"):
if event.button_index == BUTTON_WHEEL_UP:
escoria.inputs_manager._on_mousewheel_action(-1)
elif event.button_index == BUTTON_WHEEL_DOWN:
escoria.inputs_manager._on_mousewheel_action(1)
if event is InputEventMouseButton:
# var p = get_global_mouse_position()
if event.doubleclick:

View File

@@ -79,6 +79,10 @@ func register_object(object : Object):
if object is ESCInventory:
inventory = object
if object is ESCTooltip:
if main.current_scene:
main.current_scene.game.tooltip_node = object
"""

View File

@@ -16,7 +16,7 @@ func _input(event):
if ProjectSettings.get_setting("escoria/ui/tooltip_follows_mouse"):
if !hotspot_focused.empty():
if event is InputEventMouseMotion:
escoria.main.current_scene.game.update_tooltip_position(event.position)
escoria.main.current_scene.game.update_tooltip_following_mouse_position(event.position)
###################################################################################

View File

@@ -20,7 +20,7 @@ func _ready():
# Add a white TextureRect behind the RTL to see its actual size
var texturerect_node = TextureRect.new()
get_node(path_to_richtextlabel).add_child(texturerect_node)
texturerect_node.texture = load("res://addons/escoria-core/testing/white.png")
texturerect_node.texture = load("res://addons/escoria-core/game/assets/images/white.png")
texturerect_node.expand = true
texturerect_node.stretch_mode = TextureRect.STRETCH_TILE
texturerect_node.size_flags_horizontal = SIZE_EXPAND_FILL
@@ -64,17 +64,17 @@ func _on_new_text_pressed():
emit_signal("text_selected", pressed_button.text)
func tooltip_distance_to_edge_top(position_y):
return position_y
func tooltip_distance_to_edge_top(position : Vector2):
return position.y
func tooltip_distance_to_edge_bottom(position_y):
return screen_height - position_y
func tooltip_distance_to_edge_bottom(position: Vector2):
return screen_height - position.y
func tooltip_distance_to_edge_left(position_x):
return position_x
func tooltip_distance_to_edge_left(position : Vector2):
return position.x
func tooltip_distance_to_edge_right(position_x):
return screen_width - position_x
func tooltip_distance_to_edge_right(position : Vector2):
return screen_width - position.x
func _on_Control_mouse_moved(mouse_pos):
# printt("mousepos", mouse_pos)
@@ -84,19 +84,19 @@ func _on_Control_mouse_moved(mouse_pos):
var corrected_position = mouse_pos
# clamp TOP
if tooltip_distance_to_edge_top(mouse_pos.y) <= global_distance_to_clamp:
if tooltip_distance_to_edge_top(mouse_pos) <= global_distance_to_clamp:
corrected_position.y = global_distance_to_clamp
# clamp BOTTOM
if tooltip_distance_to_edge_bottom(mouse_pos.y + get_node(path_to_richtextlabel).rect_size.y) <= global_distance_to_clamp:
if tooltip_distance_to_edge_bottom(mouse_pos + get_node(path_to_richtextlabel).rect_size) <= global_distance_to_clamp:
corrected_position.y = screen_height - global_distance_to_clamp - get_node(path_to_richtextlabel).rect_size.y
# clamp LEFT
if tooltip_distance_to_edge_left(mouse_pos.x - get_node(path_to_richtextlabel).rect_size.x/2) <= global_distance_to_clamp:
if tooltip_distance_to_edge_left(mouse_pos - get_node(path_to_richtextlabel).rect_size/2) <= global_distance_to_clamp:
corrected_position.x = global_distance_to_clamp
# clamp RIGHT
if tooltip_distance_to_edge_right(mouse_pos.x + get_node(path_to_richtextlabel).rect_size.x/2) <= global_distance_to_clamp:
if tooltip_distance_to_edge_right(mouse_pos + get_node(path_to_richtextlabel).rect_size/2) <= global_distance_to_clamp:
corrected_position.x = screen_width - global_distance_to_clamp - get_node(path_to_richtextlabel).rect_size.x
get_node(path_to_richtextlabel).anchor_right = 0.2
@@ -111,11 +111,6 @@ func _on_clamp_distance_text_changed(new_text):
global_distance_to_clamp = int(new_text)
update_line2d()
func _on_rtl_sizex_text_changed(text):
pass
get_node(path_to_richtextlabel).rect_size.x = float(text)
update_size()
func _offset(position):
var center_offset_x = rect_size.x / 2

View File

@@ -54,7 +54,7 @@ margin_right = 470.0
margin_bottom = 380.0
[node name="foo" type="CheckBox" parent="HBoxContainer2"]
margin_right = 624.0
margin_right = 588.0
margin_bottom = 24.0
pressed = true
group = SubResource( 1 )
@@ -62,14 +62,14 @@ text = "Foo"
[node name="foobar" type="CheckBox" parent="HBoxContainer2"]
margin_top = 28.0
margin_right = 624.0
margin_right = 588.0
margin_bottom = 52.0
group = SubResource( 1 )
text = "Foo bar"
[node name="whatisit" type="CheckBox" parent="HBoxContainer2"]
margin_top = 56.0
margin_right = 624.0
margin_right = 588.0
margin_bottom = 80.0
group = SubResource( 1 )
text = "A super extremely long sentence to test<br>the behaviour of that RichTextLabel node..."
@@ -80,8 +80,8 @@ margin_top = 131.18
margin_right = 638.815
margin_bottom = 231.18
rect_min_size = Vector2( 400, 0 )
bbcode_text = "Tooltip content"
text = "Tooltip content"
bbcode_text = "[center][color=#200606][/color][/center]"
text = "[center][color=#200606][/color][/center]"
[connection signal="mouse_moved" from="." to="." method="_on_Control_mouse_moved"]
[connection signal="text_selected" from="." to="." method="_on_Control_text_selected"]

View File

@@ -1,34 +0,0 @@
[remap]
importer="texture"
type="StreamTexture"
path="res://.import/white.png-e746adb12fed1043fc3bcc319345fecb.stex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://addons/escoria-core/testing/white.png"
dest_files=[ "res://.import/white.png-e746adb12fed1043fc3bcc319345fecb.stex" ]
[params]
compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/bptc_ldr=0
compress/normal_map=0
flags/repeat=0
flags/filter=true
flags/mipmaps=false
flags/anisotropic=false
flags/srgb=2
process/fix_alpha_border=true
process/premult_alpha=false
process/HDR_as_SRGB=false
process/invert_color=false
stream=false
size_limit=0
detect_3d=true
svg/scale=1.0