2023-11-22 02:44:07 +02:00
|
|
|
extends Node
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Prefix for the function names to be called
|
2023-11-22 02:44:07 +02:00
|
|
|
const FN_PREFIX = "_on_"
|
2024-03-17 01:14:31 +02:00
|
|
|
## Prefix for the signal names to be emitted
|
2023-11-22 02:44:07 +02:00
|
|
|
const SIGNAL_PREFIX = "on_"
|
2024-03-17 01:14:31 +02:00
|
|
|
## Tick rate for the slow tick event
|
2024-01-30 18:47:38 +02:00
|
|
|
const SLOW_TICK = 0.1
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is clicked
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_click(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
|
|
|
|
## Emitted when a node is pressed down
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_press_down(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is moved while pressed
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_press_move(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is released
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_press_up(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
|
|
|
|
## Emitted when a node is grabbed
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_grab_down(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is moved while grabbed
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_grab_move(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is released from being grabbed
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_grab_up(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
|
|
|
|
## Emitted when a node is hovered
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_ray_enter(event: EventPointer)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a node is no longer hovered
|
2023-11-27 01:23:19 +02:00
|
|
|
signal on_ray_leave(event: EventPointer)
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a key on the virtual keyboard is pressed
|
2023-11-22 02:44:07 +02:00
|
|
|
signal on_key_down(event: EventKey)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a key on the virtual keyboard is released
|
2023-11-22 02:44:07 +02:00
|
|
|
signal on_key_up(event: EventKey)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a button on the controller is pressed
|
2024-01-15 17:47:09 +02:00
|
|
|
signal on_action_down(event: EventAction)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a button on the controller is released
|
2024-01-15 17:47:09 +02:00
|
|
|
signal on_action_up(event: EventAction)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a value changes on the controller (e.g. joystick)
|
2024-01-15 17:47:09 +02:00
|
|
|
signal on_action_value(event: EventAction)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when the node gains focus
|
2023-11-22 18:35:54 +02:00
|
|
|
signal on_focus_in(event: EventFocus)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when the node loses focus
|
2023-11-22 18:35:54 +02:00
|
|
|
signal on_focus_out(event: EventFocus)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a finger enters a TouchArea
|
2023-11-28 00:46:05 +02:00
|
|
|
signal on_touch_enter(event: EventTouch)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a finger moves inside a TouchArea
|
2023-11-28 00:46:05 +02:00
|
|
|
signal on_touch_move(event: EventTouch)
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a finger leaves a TouchArea
|
2023-11-28 00:46:05 +02:00
|
|
|
signal on_touch_leave(event: EventTouch)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when a message is sent to the user
|
2023-12-13 23:22:52 +02:00
|
|
|
signal on_notify(event: EventNotify)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emitted when the slow tick event occurs
|
2024-01-30 18:47:38 +02:00
|
|
|
signal on_slow_tick(delta: float)
|
|
|
|
|
|
|
|
var _slow_tick: float = 0.0
|
|
|
|
func _physics_process(delta):
|
|
|
|
_slow_tick += delta
|
|
|
|
if _slow_tick >= SLOW_TICK:
|
|
|
|
on_slow_tick.emit(_slow_tick)
|
|
|
|
_slow_tick -= SLOW_TICK
|
|
|
|
|
2023-11-23 00:59:46 +02:00
|
|
|
var _active_node: Node = null
|
2023-11-22 18:35:54 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Emits an event to the node tree
|
2023-11-22 02:44:07 +02:00
|
|
|
func emit(type: String, event: Event):
|
|
|
|
if event is EventBubble:
|
|
|
|
_bubble_call(type, event.target, event)
|
|
|
|
else:
|
|
|
|
_root_call(type, event)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Shortcut for sending a notification
|
|
|
|
func notify(message: String, type:=EventNotify.Type.INFO):
|
2023-12-13 23:22:52 +02:00
|
|
|
var event = EventNotify.new()
|
|
|
|
event.message = message
|
|
|
|
event.type = type
|
|
|
|
emit("notify", event)
|
|
|
|
|
2024-05-22 19:38:28 +03:00
|
|
|
## Helper for emitting controller actions
|
|
|
|
func emit_action(name: String, value, right_controller: bool=true):
|
|
|
|
var event = EventAction.new()
|
|
|
|
event.name = name
|
|
|
|
event.value = value
|
|
|
|
event.right_controller = right_controller
|
|
|
|
|
|
|
|
match typeof(value):
|
|
|
|
TYPE_BOOL:
|
|
|
|
EventSystem.emit("action_down" if value else "action_up", event)
|
|
|
|
TYPE_FLOAT, TYPE_VECTOR2:
|
|
|
|
EventSystem.emit("action_value", event)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
## Returns true when the node is focused
|
2023-11-23 00:59:46 +02:00
|
|
|
func is_focused(node: Node):
|
|
|
|
return _active_node == node
|
2023-11-22 18:35:54 +02:00
|
|
|
|
2023-11-28 15:20:38 +02:00
|
|
|
func _handle_focus(target: Variant, event: EventBubble):
|
|
|
|
if target != null:
|
|
|
|
if target.is_in_group("ui_focus_skip"):
|
|
|
|
return false
|
|
|
|
if target.is_in_group("ui_focus_stop"):
|
|
|
|
return true
|
2023-11-22 18:35:54 +02:00
|
|
|
|
2024-05-10 02:00:31 +03:00
|
|
|
if is_instance_valid(_active_node) == false:
|
|
|
|
_active_node = null
|
|
|
|
|
2023-11-23 00:59:46 +02:00
|
|
|
var event_focus = EventFocus.new()
|
|
|
|
event_focus.previous_target = _active_node
|
2023-11-28 00:46:05 +02:00
|
|
|
event_focus.target = target
|
2024-04-25 12:45:14 +03:00
|
|
|
event_focus.bubbling = false
|
2023-11-22 18:35:54 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
if _active_node != null&&_active_node.has_method(FN_PREFIX + "focus_out"):
|
2024-04-25 12:45:14 +03:00
|
|
|
emit("focus_out", event_focus)
|
2023-11-23 00:59:46 +02:00
|
|
|
_active_node.call(FN_PREFIX + "focus_out", event_focus)
|
|
|
|
on_focus_out.emit(event_focus)
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
if target == null||target.is_in_group("ui_focus") == false:
|
2023-11-23 00:59:46 +02:00
|
|
|
_active_node = null
|
2023-11-28 15:20:38 +02:00
|
|
|
return false
|
2023-11-23 00:59:46 +02:00
|
|
|
|
2023-11-28 00:46:05 +02:00
|
|
|
_active_node = target
|
2023-11-23 00:59:46 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
if _active_node != null&&_active_node.has_method(FN_PREFIX + "focus_in"):
|
2023-11-23 00:59:46 +02:00
|
|
|
_active_node.call(FN_PREFIX + "focus_in", event_focus)
|
|
|
|
on_focus_in.emit(event_focus)
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2023-11-28 15:20:38 +02:00
|
|
|
return true
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
func _bubble_call(type: String, target: Variant, event: EventBubble, focused=false):
|
2023-11-22 02:44:07 +02:00
|
|
|
if target == null:
|
2023-11-22 18:35:54 +02:00
|
|
|
return false
|
2023-11-22 02:44:07 +02:00
|
|
|
|
|
|
|
if target.has_method(FN_PREFIX + type):
|
|
|
|
var updated_event = target.call(FN_PREFIX + type, event)
|
|
|
|
|
|
|
|
if updated_event is EventBubble:
|
|
|
|
updated_event.merge(event)
|
|
|
|
event = updated_event
|
|
|
|
|
|
|
|
if event.bubbling == false:
|
2023-11-22 18:35:54 +02:00
|
|
|
return false
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
if (type == "press_down"||type == "touch_enter")&&focused == false:
|
2023-11-28 15:20:38 +02:00
|
|
|
focused = _handle_focus(target, event)
|
2023-11-28 00:46:05 +02:00
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
for child in target.get_children():
|
2024-03-17 01:14:31 +02:00
|
|
|
if child is Function&&child.has_method(FN_PREFIX + type):
|
2023-11-22 02:44:07 +02:00
|
|
|
child.call(FN_PREFIX + type, event)
|
|
|
|
|
|
|
|
var parent = target.get_parent()
|
|
|
|
|
2024-03-17 01:14:31 +02:00
|
|
|
if parent != null&&parent is Node:
|
2023-11-28 15:20:38 +02:00
|
|
|
_bubble_call(type, parent, event, focused)
|
2023-11-22 02:44:07 +02:00
|
|
|
else:
|
|
|
|
# in case the top has been reached
|
|
|
|
_root_call(type, event)
|
|
|
|
|
2023-11-22 18:35:54 +02:00
|
|
|
return true
|
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
func _root_call(type: String, event: Event):
|
|
|
|
get(SIGNAL_PREFIX + type).emit(event)
|