From 53c900b36e0d08815afc92b18b95301c8efd9c37 Mon Sep 17 00:00:00 2001 From: balloonpopper <5151242+balloonpopper@users.noreply.github.com> Date: Thu, 12 Jan 2023 22:38:06 +1100 Subject: [PATCH] Simplemouse fixes (#669) * fix: Simplemouse bounding box size fix * fix: Bug when exiting game with simplemouse with disconnecting non-connected gamepad * fix: issue-339. Simplemouse gives an error in the debug when you move on/off ESCItems * fix: tracks mouse position to allow for mouse tracking as well as on-demand tooltip updates on immediate target change, i.e. rendering doesn't lag a frame behind. * chore: decouple further by using a signal * fix: Correct action getting randomly lost with simplemouse * fix: deleted objects aren't always equated to null. * fix: clear the tooltip when an action is finished. * fix: clears tooltip only on successful event completion; debug code cleanup. * Broken room 5 * fix: Item collisions correctly determined when game resolution = screen resolution * Update addons/escoria-core/game/core-scripts/esc_item.gd Co-authored-by: Duncan Brown * Update addons/escoria-ui-simplemouse/game.gd Co-authored-by: Duncan Brown * fix: Reverted default UI to 9 verbs Co-authored-by: Balloonpopper Co-authored-by: Duncan Brown --- .../game/core-scripts/esc_item.gd | 2 +- .../game/core-scripts/esc_tooltip.gd | 51 +++++-------------- .../escoria-core/game/esc_inputs_manager.gd | 4 +- addons/escoria-ui-simplemouse/game.gd | 50 ++++++++++++------ addons/escoria-ui-simplemouse/game.tscn | 1 - .../tooltip/target_tooltip.tscn | 8 +-- .../tooltip/tooltip_target.gd | 16 +++++- game/characters/mark/mark.tscn | 1 - game/rooms/room05/room05.tscn | 6 +-- project.godot | 2 +- 10 files changed, 71 insertions(+), 70 deletions(-) diff --git a/addons/escoria-core/game/core-scripts/esc_item.gd b/addons/escoria-core/game/core-scripts/esc_item.gd index 294979ed..dceedddc 100644 --- a/addons/escoria-core/game/core-scripts/esc_item.gd +++ b/addons/escoria-core/game/core-scripts/esc_item.gd @@ -321,7 +321,7 @@ class HoverStackSorter: func _on_input_event(_viewport: Object, event: InputEvent, _shape_idx: int): if event is InputEventMouseMotion: var physics2d_dss: Physics2DDirectSpaceState = get_world_2d().direct_space_state - var colliding: Array = physics2d_dss.intersect_point(event.global_position, 32, [], 0x7FFFFFFF, true, true) + var colliding: Array = physics2d_dss.intersect_point(get_global_mouse_position(), 32, [], 0x7FFFFFFF, true, true) var colliding_nodes = [] for c in colliding: if c.collider.get("global_id") \ diff --git a/addons/escoria-core/game/core-scripts/esc_tooltip.gd b/addons/escoria-core/game/core-scripts/esc_tooltip.gd index 0f8248f2..8e87a5dc 100644 --- a/addons/escoria-core/game/core-scripts/esc_tooltip.gd +++ b/addons/escoria-core/game/core-scripts/esc_tooltip.gd @@ -45,6 +45,9 @@ var waiting_for_target2 = false # Node containing the debug white background var debug_texturerect_node: TextureRect +# Indicates whether the current room is loaded and ready +var _room_is_ready: bool = false + # Connect relevant functions func _ready(): @@ -58,7 +61,8 @@ func _ready(): # - p_color: the color to set the label func set_color(p_color: Color): color = p_color - update_tooltip_text() + if _room_is_ready: + update_tooltip_text() # Enable/disable debug mode of the label. If enabled, the label is displayed @@ -96,7 +100,8 @@ func set_debug_mode(p_debug_mode: bool): func set_target(target: String, needs_second_target: bool = false) -> void: current_target = target waiting_for_target2 = needs_second_target - update_tooltip_text() + if _room_is_ready: + update_tooltip_text() # Set the second target of the label @@ -105,7 +110,8 @@ func set_target(target: String, needs_second_target: bool = false) -> void: # - target2: String the second target to add to the label func set_target2(target2: String) -> void: current_target2 = target2 - update_tooltip_text() + if _room_is_ready: + update_tooltip_text() # Update the tooltip text. @@ -121,39 +127,7 @@ func update_size(): if not get_tree(): # We're not in the tree anymore. Return return - - 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() - - # if text is too long and is wrapped - var nblines = nb_visible_lines - if nblines >= 1: - 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 + rect_size = get_font("normal_font").get_string_size(current_target) # Calculate the offset of the label depending on its position. @@ -227,9 +201,12 @@ func clear(): # Called when the room is loaded to setup the label. func _on_room_ready(): escoria.main.current_scene.game.tooltip_node = self + _room_is_ready = true # Called when an action is selected in Escoria func _on_action_selected() -> void: current_action = escoria.action_manager.current_action - update_tooltip_text() + + if _room_is_ready: + update_tooltip_text() diff --git a/addons/escoria-core/game/esc_inputs_manager.gd b/addons/escoria-core/game/esc_inputs_manager.gd index 8c092823..f380bc68 100644 --- a/addons/escoria-core/game/esc_inputs_manager.gd +++ b/addons/escoria-core/game/esc_inputs_manager.gd @@ -185,7 +185,7 @@ func unset_hovered_node(item: ESCItem): if _hovered_element == item: _hovered_element.mouse_exited() _hovered_element = null - if not hover_stack.empty() and hover_stack.back(): + if hover_stack: set_hovered_node(hover_stack.pop_back()) else: hotspot_focused = "" @@ -211,7 +211,7 @@ func set_hovered_node(item: ESCItem) -> bool: return true # Else if the tested item is on top of hover stack (or null) # Set that item as hovered and call that item's mouse_entered() - if _hovered_element == null or hover_stack.back() != item: + if not is_instance_valid(_hovered_element) or hover_stack.back() != item: _hovered_element = item _hovered_element.mouse_entered() return true diff --git a/addons/escoria-ui-simplemouse/game.gd b/addons/escoria-ui-simplemouse/game.gd index 0a759baa..fd02daaa 100644 --- a/addons/escoria-ui-simplemouse/game.gd +++ b/addons/escoria-ui-simplemouse/game.gd @@ -67,6 +67,14 @@ const ESC_UI_CHANGE_VERB_ACTION = "esc_change_verb" # true when a gamepad is connected. var _is_gamepad_connected = false +# Tracks the mouse's current position onscreen. +var _current_mouse_pos = Vector2.ZERO + + +func _ready(): + $tooltip_layer/tooltip.connect("tooltip_size_updated", self, "update_tooltip_following_mouse_position") + + func _enter_tree(): var room_selector_parent = $CanvasLayer/ui/HBoxContainer/VBoxContainer @@ -93,14 +101,15 @@ func _enter_tree(): func _exit_tree(): escoria.inputs_manager.register_custom_input_handler(null) Input.disconnect("joy_connection_changed", self, "_on_joy_connection_changed") - _on_gamepad_disconnected() + if _is_gamepad_connected: + _on_gamepad_disconnected() func _input(event: InputEvent) -> void: if escoria.get_escoria().is_ready_for_inputs(): if event is InputEventMouseMotion: - escoria.main.current_scene.game. \ - update_tooltip_following_mouse_position(event.position) + _current_mouse_pos = get_global_mouse_position() + update_tooltip_following_mouse_position() # https://github.com/godotengine/godot-demo-projects/blob/3.4-585455e/misc/joypads/joypads.gd @@ -219,14 +228,22 @@ func element_unfocused() -> void: ## ITEMS ## - func left_click_on_item(item_global_id: String, event: InputEvent) -> void: + var target_obj = escoria.object_manager.get_object(item_global_id).node + + # current_action will be empty if an event completes between when you stop + # moving the mouse and when you click. + if escoria.action_manager.current_action == "": + if target_obj is ESCItem: + $mouse_layer/verbs_menu.set_by_name( + target_obj.default_action + ) + escoria.action_manager.do( escoria.action_manager.ACTION.ITEM_LEFT_CLICK, [item_global_id, event], true ) - func right_click_on_item(item_global_id: String, event: InputEvent) -> void: mousewheel_action(1) @@ -347,30 +364,29 @@ func get_custom_data() -> Dictionary: # Update the tooltip -# -# #### Parameters -# -# - p_position: Position of the mouse -func update_tooltip_following_mouse_position(p_position: Vector2): - var corrected_position = p_position +func update_tooltip_following_mouse_position(): + var corrected_position = _current_mouse_pos \ + - Vector2( + tooltip_node.rect_size.x / 2, + tooltip_node.rect_size.y / 2 + ) # clamp TOP - if tooltip_node.tooltip_distance_to_edge_top(p_position) <= mouse_tooltip_margin: + if tooltip_node.tooltip_distance_to_edge_top(_current_mouse_pos) <= 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: + if tooltip_node.tooltip_distance_to_edge_bottom(_current_mouse_pos + 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: + if tooltip_node.tooltip_distance_to_edge_left(_current_mouse_pos - 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: + if tooltip_node.tooltip_distance_to_edge_right(_current_mouse_pos + 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 @@ -378,10 +394,12 @@ func _on_action_finished(): $mouse_layer/verbs_menu.clear_tool_texture() $mouse_layer/verbs_menu.iterate_actions_cursor(0) + func _on_event_done(_return_code: int, _event_name: String): if _return_code == ESCExecution.RC_OK: escoria.action_manager.clear_current_action() $mouse_layer/verbs_menu.clear_tool_texture() + $tooltip_layer/tooltip.set_target("") func _on_MenuButton_pressed() -> void: diff --git a/addons/escoria-ui-simplemouse/game.tscn b/addons/escoria-ui-simplemouse/game.tscn index 3452ee96..666b2eb1 100644 --- a/addons/escoria-ui-simplemouse/game.tscn +++ b/addons/escoria-ui-simplemouse/game.tscn @@ -89,7 +89,6 @@ mouse_filter = 2 theme = ExtResource( 9 ) bbcode_text = "[center][color=#000000][/color][/center]" fit_content_height = true -offset_from_cursor = Vector2( 75, 10 ) [node name="mouse_layer" type="CanvasLayer" parent="."] layer = 2 diff --git a/addons/escoria-ui-simplemouse/tooltip/target_tooltip.tscn b/addons/escoria-ui-simplemouse/tooltip/target_tooltip.tscn index 2bd39427..32278a95 100644 --- a/addons/escoria-ui-simplemouse/tooltip/target_tooltip.tscn +++ b/addons/escoria-ui-simplemouse/tooltip/target_tooltip.tscn @@ -3,15 +3,9 @@ [ext_resource path="res://addons/escoria-ui-simplemouse/tooltip/tooltip_target.gd" type="Script" id=2] [node name="tooltip" type="RichTextLabel"] -margin_right = 200.0 -margin_bottom = 32.0 -rect_min_size = Vector2( 200, 32 ) bbcode_enabled = true bbcode_text = "[center][color=#ffffff][/color][/center]" scroll_active = false script = ExtResource( 2 ) -__meta__ = { -"_edit_use_anchors_": false -} color = Color( 1, 1, 1, 1 ) -offset_from_cursor = Vector2( 100, 10 ) +offset_from_cursor = Vector2( 0, 0 ) diff --git a/addons/escoria-ui-simplemouse/tooltip/tooltip_target.gd b/addons/escoria-ui-simplemouse/tooltip/tooltip_target.gd index 55aa388d..7874f8c6 100644 --- a/addons/escoria-ui-simplemouse/tooltip/tooltip_target.gd +++ b/addons/escoria-ui-simplemouse/tooltip/tooltip_target.gd @@ -1,6 +1,21 @@ extends ESCTooltip + +signal tooltip_size_updated + + func update_tooltip_text(): + # Need to update size of bbcode rect before updating the text itself otherwise on the + # first frame the text is wider than the default of 0 and ends up being really tall + # and setting the wrong vertical margin for the tooltip + update_size() + + # We signal this here since the processing in this class happens AFTER input + # processing. We signal here to avoid "lagging" behind a frame since + # tooltips are presently dependent on the size of the bounding box around + # the rendered string. + emit_signal("tooltip_size_updated") + bbcode_text = "[center]" bbcode_text += "[color=#" + color.to_html(false) + "]" bbcode_text += current_target @@ -11,4 +26,3 @@ func update_tooltip_text(): # append_bbcode(current_target) # pop() # pop() - update_size() diff --git a/game/characters/mark/mark.tscn b/game/characters/mark/mark.tscn index 4db13821..a3be9102 100644 --- a/game/characters/mark/mark.tscn +++ b/game/characters/mark/mark.tscn @@ -507,7 +507,6 @@ tooltip_name = "Me" default_action = "look" combine_when_selected_action_is_in = [ ] dialog_color = Color( 1, 1, 1, 1 ) -selectable = true animations = ExtResource( 8 ) [node name="sprite" type="AnimatedSprite" parent="."] diff --git a/game/rooms/room05/room05.tscn b/game/rooms/room05/room05.tscn index 8f7ab83b..678d6599 100644 --- a/game/rooms/room05/room05.tscn +++ b/game/rooms/room05/room05.tscn @@ -14,9 +14,9 @@ [ext_resource path="res://addons/escoria-core/game/core-scripts/esc_background.gd" type="Script" id=15] [sub_resource type="NavigationPolygon" id=1] -vertices = PoolVector2Array( 1187, 641, 1182.53, 588.863, 1269.59, 622.872, 1267, 801, 12, 706, 15, 646, 93, 610, 96, 642, 15, 802, 129.634, 615.792, 1155, 615 ) -polygons = [ PoolIntArray( 0, 1, 2, 3 ), PoolIntArray( 4, 5, 6, 7, 8 ), PoolIntArray( 9, 10, 0, 3, 8, 7 ) ] -outlines = [ PoolVector2Array( 12, 706, 15, 646, 93, 610, 96, 642, 129.634, 615.792, 1155, 615, 1187, 641, 1182.53, 588.863, 1269.59, 622.872, 1267, 801, 15, 802 ) ] +vertices = PoolVector2Array( 1187, 641, 1182.53, 588.863, 1269.59, 622.872, 1073, 686, 1267, 801, 1155, 615, 1067, 632, 15, 646, 93, 610, 96, 642, 857, 686, 15, 802, 12, 706, 129.634, 615.792, 857, 626 ) +polygons = [ PoolIntArray( 0, 1, 2 ), PoolIntArray( 3, 0, 2, 4 ), PoolIntArray( 5, 0, 3, 6 ), PoolIntArray( 7, 8, 9 ), PoolIntArray( 10, 3, 4, 11, 12 ), PoolIntArray( 10, 12, 7, 9 ), PoolIntArray( 10, 9, 13, 14 ), PoolIntArray( 14, 13, 5 ), PoolIntArray( 14, 5, 6 ) ] +outlines = [ PoolVector2Array( 12, 706, 15, 646, 93, 610, 96, 642, 129.634, 615.792, 1155, 615, 1187, 641, 1182.53, 588.863, 1269.59, 622.872, 1267, 801, 15, 802 ), PoolVector2Array( 857, 626, 857, 686, 1073, 686, 1067, 632 ) ] [sub_resource type="RectangleShape2D" id=2] extents = Vector2( 36, 33.5 ) diff --git a/project.godot b/project.godot index c6c624db..684ef88c 100644 --- a/project.godot +++ b/project.godot @@ -853,7 +853,7 @@ search_in_file_extensions=PoolStringArray( "gd", "shader", "esc" ) [editor_plugins] -enabled=PoolStringArray( "res://addons/escoria-core/plugin.cfg", "res://addons/escoria-dialog-simple/plugin.cfg", "res://addons/escoria-ui-9verbs/plugin.cfg", "res://addons/escoria-wizard/plugin.cfg" ) +enabled=PoolStringArray( "res://addons/escoria-core/plugin.cfg", "res://addons/escoria-dialog-simple/plugin.cfg", "res://addons/escoria-ui-simplemouse/plugin.cfg", "res://addons/escoria-wizard/plugin.cfg" ) [escoria]