refactor: changing focus naming convention to stage based metaphor

This commit is contained in:
betalars 2023-07-11 15:27:44 +02:00
parent 67cde4ca17
commit 8f53ed664d
9 changed files with 74 additions and 72 deletions

View File

@ -1,6 +1,6 @@
extends Area3D 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 var has_mouse: bool = false
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.

View File

@ -20,7 +20,7 @@ var current_mode: int = Modes.FREEZE:
func start(): func start():
$light_animation.play("light_up") $light_animation.play("light_up")
$AudioPlayer.play("intro") $AudioPlayer.play("intro")
State.pass_focus_to($PlayerController) State.pass_stage_to($PlayerController)
current_mode = Modes.WALKING current_mode = Modes.WALKING
func _update_scene(new_mode) -> int: func _update_scene(new_mode) -> int:
@ -35,4 +35,4 @@ func _unhandled_input(event):
if event is InputEventMouseButton: if event is InputEventMouseButton:
if event.pressed: if event.pressed:
print("passed") print("passed")
#State.pass_focus_to($PlayerController) #State.pass_stage_to($PlayerController)

View File

@ -1050,8 +1050,8 @@ light_energy = 0.5
light_size = 0.499 light_size = 0.499
light_specular = 0.0 light_specular = 0.0
shadow_enabled = true shadow_enabled = true
shadow_bias = 0.0 shadow_bias = 0.01
shadow_normal_bias = 6.753 shadow_normal_bias = 5.78
shadow_blur = 0.5 shadow_blur = 0.5
omni_attenuation = 1.51572 omni_attenuation = 1.51572

View File

@ -3,14 +3,14 @@ extends PanelContainer
var area_dict = {} var area_dict = {}
enum ui_context {DROPZONE, POST_IT_LIST, ASSIGN_POST_IT} enum ui_context {DROPZONE, POST_IT_LIST, ASSIGN_POST_IT}
var has_focus = false: var has_stage = false:
set(focus): set(focus):
if focus != has_focus: if focus != has_stage:
if focus: if focus:
has_focus = true has_stage = true
self.mouse_filter = Control.MOUSE_FILTER_PASS self.mouse_filter = Control.MOUSE_FILTER_PASS
else: else:
has_focus = false has_stage = false
self.mouse_filter = Control.MOUSE_FILTER_IGNORE self.mouse_filter = Control.MOUSE_FILTER_IGNORE
@onready var dropzone = $HBoxContainer/dropzone @onready var dropzone = $HBoxContainer/dropzone
@ -41,7 +41,7 @@ func _ready():
func _process(delta): func _process(delta):
# Reset information about Areas being dragged, if the mouse is not longer pressed. # 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. # 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 currently_dragged_area.is_dragged = false
is_area_dragged = false is_area_dragged = false
currently_dragged_area = null currently_dragged_area = null
@ -200,7 +200,7 @@ func reorder_areas(reorder: String):
# Takes the inputs for control inputs # Takes the inputs for control inputs
func _input(event): func _input(event):
# Return, if the input is a mouse event (mouse events are handled separately) # 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 if event.is_action_pressed("ui_up"): # up to select an element above
match active_context: match active_context:

View File

@ -11,9 +11,9 @@ enum {
@onready var debug_board:Control = $"board of devs" @onready var debug_board:Control = $"board of devs"
var has_focus = false: var has_stage = false:
set(focus): set(focus):
if not focus == has_focus: if not focus == has_stage:
if focus: if focus:
for player in anim_players: player.play("reveal") for player in anim_players: player.play("reveal")
selection_state = CARDS # fixme selection_state = CARDS # fixme
@ -21,9 +21,9 @@ var has_focus = false:
self.mouse_filter = Control.MOUSE_FILTER_IGNORE self.mouse_filter = Control.MOUSE_FILTER_IGNORE
else: else:
self.mouse_filter = Control.MOUSE_FILTER_STOP self.mouse_filter = Control.MOUSE_FILTER_STOP
has_focus = focus has_stage = focus
visible = has_focus visible = has_stage
var selection_state = INI var selection_state = INI
var input_locked = true var input_locked = true
@ -76,7 +76,7 @@ func fill_post_slots():
options[i].replace_with(post_its[i]) options[i].replace_with(post_its[i])
func _unhandled_input(event): 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"): 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 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"): 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: for card in output:
out_str.append(card.name) out_str.append(card.name)
emit_signal("cards_picked", out_str) emit_signal("cards_picked", out_str)
State.drop_focus(self) State.leave_stage(self)
func handle_hover(new_highlight): func handle_hover(new_highlight):
if not input_locked: if not input_locked:

View File

@ -18,14 +18,14 @@ class_name Collectable_Ui
collapsed = collapse collapsed = collapse
@export var is_story: bool = false @export var is_story: bool = false
@export var has_focus: bool = false: @export var has_stage: bool = false:
set(focused): set(focused):
print("set focus of card to ", focused) print("set focus of card to ", focused)
if has_focus == focused: return if has_stage == focused: return
if focused: if focused:
has_focus = true has_stage = true
print(visible) print(visible)
if not visible: show() if not visible: show()
collapsed = false collapsed = false
@ -33,8 +33,8 @@ class_name Collectable_Ui
$Panel/Content/Buttons/VBoxContainer/put_back.grab_focus() $Panel/Content/Buttons/VBoxContainer/put_back.grab_focus()
else: else:
$Panel/Content/Buttons/VBoxContainer/collect_or_listen.grab_focus() $Panel/Content/Buttons/VBoxContainer/collect_or_listen.grab_focus()
elif has_focus: elif has_stage:
has_focus = false has_stage = false
get_viewport().gui_release_focus() get_viewport().gui_release_focus()
#hide() #hide()
@ -111,11 +111,11 @@ func show():
func _yoink_focus(): func _yoink_focus():
return # fixme return # fixme
if not has_focus: if not has_stage:
State.request_focus_for(self, true) State.take_stage(self, true)
func _on_pick_button_pressed(): func _on_pick_button_pressed():
hide() hide()
print("card collected!") print("card collected!")
emit_signal("card_collected") emit_signal("card_collected")
State.drop_focus(self) State.leave_stage(self)

View File

@ -1,9 +1,9 @@
extends Panel extends Panel
@onready var has_focus = true: @onready var has_stage = true:
set(focus): set(focus):
if focus: if focus:
has_focus = State.request_focus(self) has_stage = State.request_focus(self)
else: else:
has_focus = false has_stage = false
State.drop_own_focus(self) State.drop_own_focus(self)

View File

@ -1,10 +1,10 @@
extends RigidBody3D extends RigidBody3D
var has_focus: bool = false: var has_stage: bool = false:
set(focused): set(focused):
if has_focus != focused: if has_stage != focused:
if focused: if focused:
has_focus = true has_stage = true
if is_inside_tree(): if is_inside_tree():
camera.make_current() camera.make_current()
get_viewport().gui_release_focus() get_viewport().gui_release_focus()
@ -12,20 +12,20 @@ var has_focus: bool = false:
var jitter_tween: Tween = create_tween() var jitter_tween: Tween = create_tween()
jitter_tween.tween_property(self, "jitter_strength", 1, 1) jitter_tween.tween_property(self, "jitter_strength", 1, 1)
if has_entered: emit_signal("ui_entered") if has_entered: emit_signal("ui_entered")
elif has_focus: elif has_stage:
camera.current = true camera.current = true
jitter_strength = 1 jitter_strength = 1
else: else:
if is_inside_tree() and has_focus: if is_inside_tree() and has_stage:
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
var jitter_tween: Tween = create_tween() var jitter_tween: Tween = create_tween()
jitter_tween.tween_property(self, "jitter_strength", 0, 0.5) jitter_tween.tween_property(self, "jitter_strength", 0, 0.5)
if has_entered: emit_signal("ui_exited") if has_entered: emit_signal("ui_exited")
else: else:
jitter_strength = 0 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_speed: float = 3
@export_range (0, 10) var max_acceleration: float = 5 @export_range (0, 10) var max_acceleration: float = 5
@ -76,10 +76,10 @@ func _process(_delta):
emit_signal("ui_exited") emit_signal("ui_exited")
has_entered = false has_entered = false
if Input.is_action_just_pressed("ui_accept"): 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): func _physics_process(delta:float):
if has_focus: if has_stage:
_handle_movement(delta) _handle_movement(delta)
_handle_rotation(delta) _handle_rotation(delta)
if jitter_strength > 0: _handle_jitter(delta) if jitter_strength > 0: _handle_jitter(delta)
@ -144,7 +144,7 @@ func _handle_mouse_input(event:InputEventMouseMotion):
current_mouse_rotation = event.relative current_mouse_rotation = event.relative
func _input(event:InputEvent): func _input(event:InputEvent):
if has_focus: if has_stage:
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED: if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
_handle_mouse_input(event) _handle_mouse_input(event)
get_viewport().set_input_as_handled() get_viewport().set_input_as_handled()
@ -152,11 +152,11 @@ func _input(event:InputEvent):
State.free_focus() State.free_focus()
get_viewport().set_input_as_handled() get_viewport().set_input_as_handled()
#if event.is_action_pressed("ui_accept"): #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() # get_viewport().set_input_as_handled()
func _on_empty_click(): func _on_empty_click():
State.request_focus_for(self, true) State.take_stage(self, true)
func _on_bed_enter(_body): func _on_bed_enter(_body):
if not (crouched or on_crouch_cooldown): if not (crouched or on_crouch_cooldown):

View File

@ -9,65 +9,67 @@ var show_content_notes: bool = false
var provide_summaries: bool = false var provide_summaries: bool = false
var allow_skipping: bool = false var allow_skipping: bool = false
var focus_list:Array = [] var stage_list:Array = []
var lock_focus: bool = false var lock_focus: bool = false
func _ready(): func _ready():
for child in get_parent().get_children(): for child in get_parent().get_children():
if "has_focus" in child: if "has_stage" in child:
pass_focus_to(child) pass_stage_to(child)
# Intented for use when me wants focus for itself, can reclaim focus, thus dropping the stack that focused. # Meta: due to conflicting names with the internal focus handling of godot, a "stage-based" Metaphor is being used to refer to focus handling.
func request_focus_for(me: Object, reclaim: bool = false) -> bool:
# 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: if reclaim:
focus_list.front().has_focus = false stage_list.front().has_stage = false
if focus_list.has(me): if stage_list.has(actor):
while focus_list.pop_front() != me: break while stage_list.pop_front() != actor: break
me.has_focus = true actor.has_stage = true
return me.has_focus return actor.has_stage
push_warning(me, " wanted to reclaim focus, but was not on list.") push_warning(actor, " wanted to reclaim focus, but was not on list.")
return pass_focus_to(me) return pass_stage_to(actor)
# Element no longer wants focus, if Element itself is also dropped, this option can be chosen aswell. # 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: 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 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. # Used to put a new target on top of the Focus Stack.
func pass_focus_to(target:Object) -> bool: func pass_stage_to(target:Object) -> bool:
if "focus_forward" in target: if "pass_to_actor" in target:
pass_focus_to(target.focus_forward) pass_stage_to(target.pass_to_actor)
if lock_focus or get_tree().paused: if lock_focus or get_tree().paused:
push_error(target, " requested focus while it was locked or tree is paused.") push_error(target, " requested focus while it was locked or tree is paused.")
elif !is_instance_valid(target): elif !is_instance_valid(target):
push_error("Focus instance not valid") push_error("Focus instance not valid")
elif !"has_focus" in target: elif !"has_stage" in target:
push_error(target, " has no has focus method.") push_error(target, " has no has focus method.")
if focus_list.size() > 0: if stage_list.size() > 0:
if focus_list.front() == target: if stage_list.front() == target:
push_warning(target, " is already target. Abort passing focus.") push_warning(target, " is already target. Abort passing focus.")
else: else:
if not focus_list.size() == 0: focus_list.front().has_focus = false if not stage_list.size() == 0: stage_list.front().has_stage = false
target.has_focus = true target.has_stage = true
if target.has_focus: if target.has_stage:
focus_list.push_front(target) stage_list.push_front(target)
assert(focus_list.size() < 100) assert(stage_list.size() < 100)
return true return true
return false return false
# Currently focused element loses focus, but remains in stack. # Currently focused element loses focus, but remains in stack.
func free_focus(): 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): func queue_for_stage(target: Object, index: int):
focus_list.insert(index, target) stage_list.insert(index, target)