refactoring focus handling in global state
This commit is contained in:
parent
3b8752cf96
commit
2e8f22f1d2
|
|
@ -12,57 +12,54 @@ var allow_skipping: bool = false
|
||||||
var focus_list:Array = []
|
var focus_list:Array = []
|
||||||
var lock_focus: bool = false
|
var lock_focus: bool = false
|
||||||
|
|
||||||
func request_focus(new_focus: Node, reclaim_focus: bool = false) -> bool:
|
# 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:
|
||||||
|
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)
|
||||||
|
|
||||||
assert(is_instance_valid(new_focus))
|
# 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:
|
||||||
if lock_focus or get_tree().paused:
|
if lock_focus or get_tree().paused:
|
||||||
push_warning(new_focus.name, " attempted to get focus while tree was paused or fokus had been locked.")
|
push_error(of, " wanted to drop focus while it was locked or tree is paused.")
|
||||||
|
|
||||||
|
if not dropObject: of.has_focus = false
|
||||||
|
|
||||||
|
focus_list.erase(of)
|
||||||
|
|
||||||
|
focus_list.front().has_focus = true
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
|
||||||
if not focus_list.size() == 0: _pass_focus_of(focus_list[0])
|
func get_current_focus(): return focus_list.front()
|
||||||
|
|
||||||
if reclaim_focus:
|
# Used to put a new target on top of the Focus Stack.
|
||||||
if focus_list.has(new_focus):
|
func pass_focus_to(target:Object) -> bool:
|
||||||
while not focus_list[0] == new_focus: focus_list.pop_front()
|
if "focus_forward" in target:
|
||||||
return true
|
pass_focus_to(target.focus_forward)
|
||||||
|
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:
|
||||||
|
push_error(target, " has no has focus method")
|
||||||
else:
|
else:
|
||||||
focus_list.push_front(new_focus)
|
if not focus_list.front() == null: focus_list.front().has_focus = false
|
||||||
push_warning(new_focus.name, " attempted to reclaim focus it did not previousely have.")
|
target.has_focus = true
|
||||||
return true
|
if target.has_focus:
|
||||||
else:
|
focus_list.push_front(target)
|
||||||
focus_list.append(new_focus)
|
assert(focus_list.size() < 100)
|
||||||
return true
|
return true
|
||||||
|
return false
|
||||||
|
|
||||||
func assign_focus_to(focusable: Node) -> bool:
|
# Currently focused element loses focus, but remains in stack.
|
||||||
if "focus_forward" in focusable:
|
func free_focus():
|
||||||
assign_focus_to(focusable.focus_forward)
|
if not focus_list.front() == null: focus_list.front().has_focus = false
|
||||||
if "has_focus" in focusable:
|
|
||||||
if not focus_list.size() == 0:
|
|
||||||
_pass_focus_of(focus_list[0])
|
|
||||||
focusable.has_focus = true
|
|
||||||
return true
|
|
||||||
else: return false
|
|
||||||
|
|
||||||
func pass_focus_to_unkown(from: Node):
|
func queue_for_focus(target: Object, index: int):
|
||||||
pass
|
focus_list.insert(index, target)
|
||||||
|
|
||||||
func drop_own_focus(node: Node):
|
|
||||||
if focus_list[0] == node:
|
|
||||||
focus_list.pop_front().has_focus = false
|
|
||||||
assert(focus_list.size() == 0)
|
|
||||||
focus_list[0].has_focus = true
|
|
||||||
else:
|
|
||||||
push_warning(node.name + " attempted to drop focus while not owning it.")
|
|
||||||
|
|
||||||
func clear_focus_stack_and_focus_on(focus: Node):
|
|
||||||
assert(is_instance_valid(focus))
|
|
||||||
if focus_list.size() > 0: focus_list[0].has_focus = false
|
|
||||||
focus_list = [focus]
|
|
||||||
focus.has_focus = true
|
|
||||||
|
|
||||||
func _pass_focus_of(previous_focus: Node):
|
|
||||||
previous_focus.has_focus = false
|
|
||||||
|
|
||||||
func focus_cleared(from: Node):
|
|
||||||
if from == focus_list[0]:
|
|
||||||
push_warning(from.name, " cleared focus without validating!")
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue