From 8f53ed664d1eb4c1da7425a247c06634eccc449f Mon Sep 17 00:00:00 2001 From: betalars Date: Tue, 11 Jul 2023 15:27:44 +0200 Subject: [PATCH] refactor: changing focus naming convention to stage based metaphor --- .../youth_room/Collectable.gd | 2 +- .../youth_room/room_handle.gd | 4 +- .../youth_room/youth_room.tscn | 4 +- src/logic-scenes/board/card-board.gd | 12 ++-- src/logic-scenes/card_picker/card_picker.gd | 12 ++-- .../collectable/collectable_ui.gd | 16 ++--- src/logic-scenes/main menu/Main Menu.gd | 6 +- .../player_controller/player_controller.gd | 24 +++---- src/singletons/global_state.gd | 66 ++++++++++--------- 9 files changed, 74 insertions(+), 72 deletions(-) diff --git a/src/base-environments/youth_room/Collectable.gd b/src/base-environments/youth_room/Collectable.gd index 6278ac4..b740652 100644 --- a/src/base-environments/youth_room/Collectable.gd +++ b/src/base-environments/youth_room/Collectable.gd @@ -1,6 +1,6 @@ extends Area3D -@onready var focus_forward = $UiWrapper/UiSprite/SubViewport/Collectable_ui +@onready var pass_to_actor = $UiWrapper/UiSprite/SubViewport/Collectable_ui var has_mouse: bool = false # Called when the node enters the scene tree for the first time. diff --git a/src/base-environments/youth_room/room_handle.gd b/src/base-environments/youth_room/room_handle.gd index d1eed4f..7c32223 100644 --- a/src/base-environments/youth_room/room_handle.gd +++ b/src/base-environments/youth_room/room_handle.gd @@ -20,7 +20,7 @@ var current_mode: int = Modes.FREEZE: func start(): $light_animation.play("light_up") $AudioPlayer.play("intro") - State.pass_focus_to($PlayerController) + State.pass_stage_to($PlayerController) current_mode = Modes.WALKING func _update_scene(new_mode) -> int: @@ -35,4 +35,4 @@ func _unhandled_input(event): if event is InputEventMouseButton: if event.pressed: print("passed") - #State.pass_focus_to($PlayerController) + #State.pass_stage_to($PlayerController) diff --git a/src/base-environments/youth_room/youth_room.tscn b/src/base-environments/youth_room/youth_room.tscn index 27020da..d0403bc 100644 --- a/src/base-environments/youth_room/youth_room.tscn +++ b/src/base-environments/youth_room/youth_room.tscn @@ -1050,8 +1050,8 @@ light_energy = 0.5 light_size = 0.499 light_specular = 0.0 shadow_enabled = true -shadow_bias = 0.0 -shadow_normal_bias = 6.753 +shadow_bias = 0.01 +shadow_normal_bias = 5.78 shadow_blur = 0.5 omni_attenuation = 1.51572 diff --git a/src/logic-scenes/board/card-board.gd b/src/logic-scenes/board/card-board.gd index 9aaf4a9..bc2b4ca 100644 --- a/src/logic-scenes/board/card-board.gd +++ b/src/logic-scenes/board/card-board.gd @@ -3,14 +3,14 @@ extends PanelContainer var area_dict = {} enum ui_context {DROPZONE, POST_IT_LIST, ASSIGN_POST_IT} -var has_focus = false: +var has_stage = false: set(focus): - if focus != has_focus: + if focus != has_stage: if focus: - has_focus = true + has_stage = true self.mouse_filter = Control.MOUSE_FILTER_PASS else: - has_focus = false + has_stage = false self.mouse_filter = Control.MOUSE_FILTER_IGNORE @onready var dropzone = $HBoxContainer/dropzone @@ -41,7 +41,7 @@ func _ready(): func _process(delta): # Reset information about Areas being dragged, if the mouse is not longer pressed. # Needed because otherwise it can happen that the areas don't register it if you stop clicking on them. - if has_focus and !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and is_area_dragged: + if has_stage and !Input.is_mouse_button_pressed(MOUSE_BUTTON_LEFT) and is_area_dragged: currently_dragged_area.is_dragged = false is_area_dragged = false currently_dragged_area = null @@ -200,7 +200,7 @@ func reorder_areas(reorder: String): # Takes the inputs for control inputs func _input(event): # Return, if the input is a mouse event (mouse events are handled separately) - if event is InputEventMouse or !has_focus: return + if event is InputEventMouse or !has_stage: return if event.is_action_pressed("ui_up"): # up to select an element above match active_context: diff --git a/src/logic-scenes/card_picker/card_picker.gd b/src/logic-scenes/card_picker/card_picker.gd index df4e888..f2ebef4 100644 --- a/src/logic-scenes/card_picker/card_picker.gd +++ b/src/logic-scenes/card_picker/card_picker.gd @@ -11,9 +11,9 @@ enum { @onready var debug_board:Control = $"board of devs" -var has_focus = false: +var has_stage = false: set(focus): - if not focus == has_focus: + if not focus == has_stage: if focus: for player in anim_players: player.play("reveal") selection_state = CARDS # fixme @@ -21,9 +21,9 @@ var has_focus = false: self.mouse_filter = Control.MOUSE_FILTER_IGNORE else: self.mouse_filter = Control.MOUSE_FILTER_STOP - has_focus = focus + has_stage = focus - visible = has_focus + visible = has_stage var selection_state = INI var input_locked = true @@ -76,7 +76,7 @@ func fill_post_slots(): options[i].replace_with(post_its[i]) func _unhandled_input(event): - if has_focus and not input_locked: + if has_stage and not input_locked: if event.is_action_pressed("ui_up") or event.is_action_pressed("ui_left") or event.is_action_pressed("ui_focus_next"): curr_selection_id -= 1 elif event.is_action_pressed("ui_down") or event.is_action_pressed("ui_right") or event.is_action_pressed("ui_focus_prev"): @@ -141,7 +141,7 @@ func pick(id: int): for card in output: out_str.append(card.name) emit_signal("cards_picked", out_str) - State.drop_focus(self) + State.leave_stage(self) func handle_hover(new_highlight): if not input_locked: diff --git a/src/logic-scenes/collectable/collectable_ui.gd b/src/logic-scenes/collectable/collectable_ui.gd index d7847d0..4e605cf 100644 --- a/src/logic-scenes/collectable/collectable_ui.gd +++ b/src/logic-scenes/collectable/collectable_ui.gd @@ -18,14 +18,14 @@ class_name Collectable_Ui collapsed = collapse @export var is_story: bool = false -@export var has_focus: bool = false: +@export var has_stage: bool = false: set(focused): print("set focus of card to ", focused) - if has_focus == focused: return + if has_stage == focused: return if focused: - has_focus = true + has_stage = true print(visible) if not visible: show() collapsed = false @@ -33,8 +33,8 @@ class_name Collectable_Ui $Panel/Content/Buttons/VBoxContainer/put_back.grab_focus() else: $Panel/Content/Buttons/VBoxContainer/collect_or_listen.grab_focus() - elif has_focus: - has_focus = false + elif has_stage: + has_stage = false get_viewport().gui_release_focus() #hide() @@ -111,11 +111,11 @@ func show(): func _yoink_focus(): return # fixme - if not has_focus: - State.request_focus_for(self, true) + if not has_stage: + State.take_stage(self, true) func _on_pick_button_pressed(): hide() print("card collected!") emit_signal("card_collected") - State.drop_focus(self) + State.leave_stage(self) diff --git a/src/logic-scenes/main menu/Main Menu.gd b/src/logic-scenes/main menu/Main Menu.gd index 62ce336..6b9e129 100644 --- a/src/logic-scenes/main menu/Main Menu.gd +++ b/src/logic-scenes/main menu/Main Menu.gd @@ -1,9 +1,9 @@ extends Panel -@onready var has_focus = true: +@onready var has_stage = true: set(focus): if focus: - has_focus = State.request_focus(self) + has_stage = State.request_focus(self) else: - has_focus = false + has_stage = false State.drop_own_focus(self) diff --git a/src/logic-scenes/player_controller/player_controller.gd b/src/logic-scenes/player_controller/player_controller.gd index da94a9a..e04d169 100644 --- a/src/logic-scenes/player_controller/player_controller.gd +++ b/src/logic-scenes/player_controller/player_controller.gd @@ -1,10 +1,10 @@ extends RigidBody3D -var has_focus: bool = false: +var has_stage: bool = false: set(focused): - if has_focus != focused: + if has_stage != focused: if focused: - has_focus = true + has_stage = true if is_inside_tree(): camera.make_current() get_viewport().gui_release_focus() @@ -12,20 +12,20 @@ var has_focus: bool = false: var jitter_tween: Tween = create_tween() jitter_tween.tween_property(self, "jitter_strength", 1, 1) if has_entered: emit_signal("ui_entered") - elif has_focus: + elif has_stage: camera.current = true jitter_strength = 1 else: - if is_inside_tree() and has_focus: + if is_inside_tree() and has_stage: Input.mouse_mode = Input.MOUSE_MODE_VISIBLE var jitter_tween: Tween = create_tween() jitter_tween.tween_property(self, "jitter_strength", 0, 0.5) if has_entered: emit_signal("ui_exited") else: jitter_strength = 0 - has_focus = false + has_stage = false - sleeping = has_focus + sleeping = has_stage @export_range (0, 10) var max_speed: float = 3 @export_range (0, 10) var max_acceleration: float = 5 @@ -76,10 +76,10 @@ func _process(_delta): emit_signal("ui_exited") has_entered = false if Input.is_action_just_pressed("ui_accept"): - State.pass_focus_to(focus_ray.get_collider()) + State.pass_stage_to(focus_ray.get_collider()) func _physics_process(delta:float): - if has_focus: + if has_stage: _handle_movement(delta) _handle_rotation(delta) if jitter_strength > 0: _handle_jitter(delta) @@ -144,7 +144,7 @@ func _handle_mouse_input(event:InputEventMouseMotion): current_mouse_rotation = event.relative func _input(event:InputEvent): - if has_focus: + if has_stage: if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: _handle_mouse_input(event) get_viewport().set_input_as_handled() @@ -152,11 +152,11 @@ func _input(event:InputEvent): State.free_focus() get_viewport().set_input_as_handled() #if event.is_action_pressed("ui_accept"): - # State.pass_focus_to(focus_ray.get_collider()) + # State.pass_stage_to(focus_ray.get_collider()) # get_viewport().set_input_as_handled() func _on_empty_click(): - State.request_focus_for(self, true) + State.take_stage(self, true) func _on_bed_enter(_body): if not (crouched or on_crouch_cooldown): diff --git a/src/singletons/global_state.gd b/src/singletons/global_state.gd index df2991f..3f220f1 100644 --- a/src/singletons/global_state.gd +++ b/src/singletons/global_state.gd @@ -9,65 +9,67 @@ var show_content_notes: bool = false var provide_summaries: bool = false var allow_skipping: bool = false -var focus_list:Array = [] +var stage_list:Array = [] var lock_focus: bool = false func _ready(): for child in get_parent().get_children(): - if "has_focus" in child: - pass_focus_to(child) + if "has_stage" in child: + pass_stage_to(child) -# Intented for use when me wants focus for itself, can reclaim focus, thus dropping the stack that focused. -func request_focus_for(me: Object, reclaim: bool = false) -> bool: +# Meta: due to conflicting names with the internal focus handling of godot, a "stage-based" Metaphor is being used to refer to focus handling. + +# Intented for use when an actor wants focus for itself, can reclaim focus, thus dropping the stack that focused. +func take_stage(actor: Object, reclaim: bool = false) -> bool: if reclaim: - focus_list.front().has_focus = false - if focus_list.has(me): - while focus_list.pop_front() != me: break - me.has_focus = true - return me.has_focus - push_warning(me, " wanted to reclaim focus, but was not on list.") - return pass_focus_to(me) + stage_list.front().has_stage = false + if stage_list.has(actor): + while stage_list.pop_front() != actor: break + actor.has_stage = true + return actor.has_stage + push_warning(actor, " wanted to reclaim focus, but was not on list.") + return pass_stage_to(actor) # Element no longer wants focus, if Element itself is also dropped, this option can be chosen aswell. -func drop_focus(of:Object, dropObject: bool = false) -> bool: +func leave_stage(actor:Object, dropObject: bool = false) -> bool: if lock_focus or get_tree().paused: - push_error(of, " wanted to drop focus while it was locked or tree is paused.") + push_error(actor, " wanted to drop focus while it was locked or tree is paused.") - if not dropObject: of.has_focus = false + if not dropObject: actor.has_stage = false - focus_list.erase(of) + stage_list.erase(actor) - focus_list.front().has_focus = true + stage_list.front().has_stage = true return false -func get_current_focus(): return focus_list.front() +func get_current_actor(): return stage_list.front() # Used to put a new target on top of the Focus Stack. -func pass_focus_to(target:Object) -> bool: - if "focus_forward" in target: - pass_focus_to(target.focus_forward) +func pass_stage_to(target:Object) -> bool: + if "pass_to_actor" in target: + pass_stage_to(target.pass_to_actor) if lock_focus or get_tree().paused: push_error(target, " requested focus while it was locked or tree is paused.") elif !is_instance_valid(target): push_error("Focus instance not valid") - elif !"has_focus" in target: + elif !"has_stage" in target: push_error(target, " has no has focus method.") - if focus_list.size() > 0: - if focus_list.front() == target: + if stage_list.size() > 0: + if stage_list.front() == target: push_warning(target, " is already target. Abort passing focus.") else: - if not focus_list.size() == 0: focus_list.front().has_focus = false - target.has_focus = true - if target.has_focus: - focus_list.push_front(target) - assert(focus_list.size() < 100) + if not stage_list.size() == 0: stage_list.front().has_stage = false + target.has_stage = true + if target.has_stage: + stage_list.push_front(target) + assert(stage_list.size() < 100) return true return false # Currently focused element loses focus, but remains in stack. func free_focus(): - if not focus_list.front() == null: focus_list.front().has_focus = false + if not stage_list.front() == null: stage_list.front().has_stage = false -func queue_for_focus(target: Object, index: int): - focus_list.insert(index, target) +func queue_for_stage(target: Object, index: int): + stage_list.insert(index, target)