From cdf85cd1c5c8b6ad4c5170a325213a9721c719ed Mon Sep 17 00:00:00 2001 From: oier Date: Sun, 8 Oct 2023 03:14:13 +0200 Subject: [PATCH] Outline item component --- .../esc_item_with_tooltip.gd | 54 ++----------------- .../esc_player_with_tooltip.gd | 2 + .../escoria-ui-return-monkey-island/game.gd | 6 +-- .../item_components/ESCItemComponent.gd | 1 - .../ESCItemComponentOutline.gd | 41 ++++++++++++++ project.godot | 8 ++- 6 files changed, 57 insertions(+), 55 deletions(-) create mode 100644 gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponentOutline.gd diff --git a/gymkhana/addons/escoria-ui-return-monkey-island/esc_item_with_tooltip.gd b/gymkhana/addons/escoria-ui-return-monkey-island/esc_item_with_tooltip.gd index 618b3ae0..2b939b03 100644 --- a/gymkhana/addons/escoria-ui-return-monkey-island/esc_item_with_tooltip.gd +++ b/gymkhana/addons/escoria-ui-return-monkey-island/esc_item_with_tooltip.gd @@ -44,62 +44,12 @@ var custom_data: Dictionary = {} # ] export(Array) var count_textures = [] -var outline: ItemOutline - -var isHighlighted: bool -var lastHighlightState: bool - -var active: bool - -# React to the mouse entering the item by emitting the respective signal -func mouse_entered(): - active = true - if escoria.action_manager.is_object_actionable(global_id): - emit_signal("mouse_entered_item", self) - - -# React to the mouse exiting the item by emitting the respective signal -func mouse_exited(): - active = false - emit_signal("mouse_exited_item", self) func _ready(): - if collision is CollisionPolygon2D: - outline = ItemOutline.new() - outline.polygon = collision.get("polygon") - outline.color = Color(1,1,1,0.2) - outline.set_outline_width(2.0) - outline.set_outline_color(Color(1,1,1,1)) - ##outline.offset = Vector2(1,1) - collision.add_child(outline) - outline.hide() register_components() - -func _process(_delta) -> void: - if(is_interactive == false): - return - - if isHighlighted != lastHighlightState: - if isHighlighted: - outline.show() - else: - outline.hide() - lastHighlightState = isHighlighted - pass - -func highlight(value: bool): - isHighlighted = value - func set_tooltip(action: String, text: String): tooltips[action] = text - -func _input(event): - if(event.is_action_pressed("ui_show_hints")): - highlight(true) - elif (event.is_action_released("ui_show_hints")): - if(!active): - highlight(false) func has_component(key: String)->bool: return components.has(key) @@ -110,8 +60,12 @@ func get_component(key: String): return null func register_components(): + autoload_components() for child in get_children(): if(child is ESCItemComponent): child = child as ESCItemComponent components[child.get_component_type()] = child child.register(custom_data) + +func autoload_components(): + add_child(ESCItemComponentOutline.new()) \ No newline at end of file diff --git a/gymkhana/addons/escoria-ui-return-monkey-island/esc_player_with_tooltip.gd b/gymkhana/addons/escoria-ui-return-monkey-island/esc_player_with_tooltip.gd index 93f0dd38..c9778851 100644 --- a/gymkhana/addons/escoria-ui-return-monkey-island/esc_player_with_tooltip.gd +++ b/gymkhana/addons/escoria-ui-return-monkey-island/esc_player_with_tooltip.gd @@ -8,8 +8,10 @@ export(bool) var selectable = false # A player is always movable func _init(): + ._init() is_movable = true _force_registration = true + # Ready function diff --git a/gymkhana/addons/escoria-ui-return-monkey-island/game.gd b/gymkhana/addons/escoria-ui-return-monkey-island/game.gd index 9fe98e05..32de9539 100644 --- a/gymkhana/addons/escoria-ui-return-monkey-island/game.gd +++ b/gymkhana/addons/escoria-ui-return-monkey-island/game.gd @@ -252,9 +252,9 @@ func element_focused(element_id: String) -> void: if target_obj is ESCItem or ESCItemWithTooltip: $tooltip_layer/tooltip.set_target(target_obj.tooltip_name) $tooltip_layer/tooltip.set_target_object(target_obj) - target_obj.highlight(true) + target_obj.get_component('outline').highlight(true) if last_target != null: - last_target.highlight(false) + last_target.get_component('outline').highlight(false) last_target = target_obj @@ -262,7 +262,7 @@ func element_unfocused() -> void: $tooltip_layer/tooltip.set_target("") $tooltip_layer/tooltip.set_target_object(null) if(last_target != null): - last_target.highlight(false) + last_target.get_component('outline').highlight(false) last_target = null diff --git a/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponent.gd b/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponent.gd index 578dbbcc..c6eae3bb 100644 --- a/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponent.gd +++ b/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponent.gd @@ -11,4 +11,3 @@ func get_component_type(): func register(custom_data: Dictionary): pass - diff --git a/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponentOutline.gd b/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponentOutline.gd new file mode 100644 index 00000000..53895f5c --- /dev/null +++ b/gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponentOutline.gd @@ -0,0 +1,41 @@ +extends ESCItemComponent +class_name ESCItemComponentOutline + +var outline: ItemOutline + +var isHighlighted: bool +var lastHighlightState: bool + +func get_component_type(): + return "outline" + +func _ready(): + var collision = get_parent().collision + if collision is CollisionPolygon2D: + outline = ItemOutline.new() + outline.polygon = collision.get("polygon") + outline.color = Color(1,1,1,0.2) + outline.set_outline_width(2.0) + outline.set_outline_color(Color(1,1,1,1)) + collision.add_child(outline) + outline.hide() + + +func highlight(value: bool): + isHighlighted = value + +func _process(_delta: float): + if not escoria.action_manager.is_object_actionable(get_global_id()): + return + if isHighlighted != lastHighlightState: + if isHighlighted: + outline.show() + else: + outline.hide() + lastHighlightState = isHighlighted + +func _input(event): + if(event.is_action_pressed("ui_show_hints")): + highlight(true) + elif (event.is_action_released("ui_show_hints")): + highlight(false) \ No newline at end of file diff --git a/project.godot b/project.godot index c40307ff..aa607a9b 100644 --- a/project.godot +++ b/project.godot @@ -204,7 +204,7 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_options_chooser.gd" }, { -"base": "StateMachine", +"base": "Node", "class": "ESCDialogPlayer", "language": "GDScript", "path": "res://addons/escoria-core/game/scenes/dialogs/esc_dialog_player.gd" @@ -289,6 +289,11 @@ _global_script_classes=[ { "language": "GDScript", "path": "res://gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponent.gd" }, { +"base": "ESCItemComponent", +"class": "ESCItemComponentOutline", +"language": "GDScript", +"path": "res://gymkhana/addons/escoria-ui-return-monkey-island/item_components/ESCItemComponentOutline.gd" +}, { "base": "Resource", "class": "ESCItemCountManager", "language": "GDScript", @@ -791,6 +796,7 @@ _global_script_class_icons={ "ESCInventoryManager": "", "ESCItem": "res://addons/escoria-core/design/esc_item.svg", "ESCItemComponent": "", +"ESCItemComponentOutline": "", "ESCItemCountManager": "", "ESCItemWithTooltip": "res://addons/escoria-core/design/esc_item.svg", "ESCLocation": "res://addons/escoria-core/design/esc_location.svg",