@papercraftgames/estragon 0.1.0-beta.29 → 0.1.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,5 +3,5 @@
3
3
  name="Estragon"
4
4
  description="Native Gogo/GGX highlighting, compilation and validation before Play."
5
5
  author="Resist Design"
6
- version="0.1.0-beta.29"
6
+ version="0.1.0-beta.30"
7
7
  script="plugin.gd"
@@ -1,8 +1,19 @@
1
1
  @tool
2
2
  class_name EstragonButton
3
3
  extends "text.gd"
4
- ## Native primary-pointer button. No Control widget or polling wrapper.
4
+ ## Native pointer/drawing with a transparent native GUI keyboard endpoint.
5
5
  signal pressed
6
+ signal focus_entered
7
+ signal focus_exited
8
+ const FocusControl = preload("internal/focus_control.gd")
9
+ const Focus = preload("internal/focus.tres")
10
+ @export_enum("None", "Click", "All") var focus_mode: int = Control.FOCUS_ALL:
11
+ set(value):
12
+ if value<Control.FOCUS_NONE or value>Control.FOCUS_ALL: return
13
+ focus_mode = value
14
+ _sync_focus()
15
+ var _focus_control: Control
16
+ var _keyboard_held := false
6
17
  const ESTRAGON_BUTTON_STYLE := true
7
18
  var hover_background_color: Variant = Color("#505050"):
8
19
  set(value):
@@ -33,12 +44,20 @@ func _get_property_list() -> Array[Dictionary]:
33
44
  if disabled == value: return
34
45
  disabled = value
35
46
  _cancel_pointer()
47
+ _sync_focus()
36
48
  _queue_style_redraw()
37
49
  var _router: WeakRef
38
50
  var _hovered: bool = false
39
51
  var _held: bool = false
40
52
  func _enter_tree() -> void:
41
53
  super._enter_tree()
54
+ if not Engine.is_editor_hint():
55
+ _focus_control = FocusControl.new()
56
+ _focus_control.name = "_EstragonFocus"
57
+ add_child(_focus_control,false,Node.INTERNAL_MODE_BACK)
58
+ layout_applied.connect(_sync_focus)
59
+ get_window().focus_exited.connect(_focus_control.cancel)
60
+ _sync_focus()
42
61
  _layout_scope_changed()
43
62
  visibility_changed.connect(_cancel_pointer)
44
63
  func _layout_scope_changed() -> void:
@@ -51,12 +70,36 @@ func _layout_scope_changed() -> void:
51
70
  _router = weakref(entry._get_pointer_router())
52
71
  _router.get_ref().register_button(self)
53
72
  func _exit_tree() -> void:
73
+ if is_instance_valid(_focus_control):
74
+ if get_window().focus_exited.is_connected(_focus_control.cancel): get_window().focus_exited.disconnect(_focus_control.cancel)
75
+ layout_applied.disconnect(_sync_focus)
76
+ remove_child(_focus_control)
77
+ _focus_control.free()
78
+ _focus_control = null
54
79
  if _router != null and _router.get_ref() != null: _router.get_ref().unregister_button(self)
55
80
  _router = null
56
81
  if visibility_changed.is_connected(_cancel_pointer): visibility_changed.disconnect(_cancel_pointer)
57
82
  _hovered = false
58
83
  _held = false
59
84
  super._exit_tree()
85
+ func _composition_is_private(child: Node) -> bool:
86
+ return child==_focus_control or super._composition_is_private(child)
87
+ func _sync_focus(_size: Vector2 = Vector2.ZERO) -> void:
88
+ if not is_instance_valid(_focus_control): return
89
+ _focus_control.position = Vector2.ZERO
90
+ _focus_control.size = layout_size
91
+ _focus_control.focus_mode = Control.FOCUS_NONE if disabled else focus_mode
92
+ if not Focus.eligible(_focus_control):
93
+ _focus_control.cancel()
94
+ _focus_control.release_focus()
95
+ func grab_focus() -> void:
96
+ if is_instance_valid(_focus_control) and Focus.eligible(_focus_control): _focus_control.grab_focus()
97
+ func release_focus() -> void:
98
+ if is_instance_valid(_focus_control): _focus_control.release_focus()
99
+ func has_focus() -> bool:
100
+ return is_instance_valid(_focus_control) and _focus_control.has_focus()
101
+ func get_focus_control() -> Control:
102
+ return _focus_control
60
103
  func _cancel_pointer() -> void:
61
104
  if _router != null and _router.get_ref() != null: _router.get_ref().cancel_button(self)
62
105
  func _set_pointer_state(hovered: bool, held: bool) -> void:
@@ -71,12 +114,14 @@ func _pointer_contains(position_in_viewport: Vector2) -> bool:
71
114
  return Rect2(Vector2.ZERO,layout_size).has_point(transform.affine_inverse()*position_in_viewport)
72
115
  func get_background_color() -> Color:
73
116
  if disabled: return _visual_style.colors.disabled_background_color
74
- if _held and _hovered: return _visual_style.colors.pressed_background_color
117
+ if _held and _hovered or _keyboard_held: return _visual_style.colors.pressed_background_color
75
118
  return _visual_style.colors.hover_background_color if _hovered else super.get_background_color()
76
119
  func get_label_origin() -> Vector2:
77
120
  var content := get_content_rect()
78
121
  var remaining := (content.size-_measure_label())*0.5
79
122
  return content.position+Vector2(maxf(0,remaining.x),maxf(0,remaining.y))
80
123
  func _draw() -> void:
81
- if get_content_rect().size.x<=0 or get_content_rect().size.y<=0: return
82
- _draw_label(get_label_origin(),Color(1,1,1,0.5) if disabled else Color.WHITE)
124
+ if get_content_rect().size.x>0 and get_content_rect().size.y>0:
125
+ _draw_label(get_label_origin(),Color(1,1,1,0.5) if disabled else Color.WHITE)
126
+ if has_focus() and layout_size.x>4 and layout_size.y>4:
127
+ draw_rect(Rect2(Vector2(2,2),layout_size-Vector2(4,4)),Color("#9cdcff"),false,2)
@@ -6,6 +6,7 @@ func _is_layout_entry() -> bool:
6
6
  return true
7
7
 
8
8
  const Pointer = preload("internal/pointer_2d.gd")
9
+ const Focus = preload("internal/focus.tres")
9
10
  var _pointer = Pointer.new(self)
10
11
 
11
12
  func _get_pointer_router():
@@ -14,12 +15,25 @@ func _get_pointer_router():
14
15
  func _enter_tree() -> void:
15
16
  _pointer.start()
16
17
  super._enter_tree()
18
+ Focus.add(self)
17
19
 
18
20
  func _exit_tree() -> void:
21
+ Focus.remove(self)
19
22
  _pointer.stop()
20
23
  super._exit_tree()
21
24
 
22
25
  func _input(event: InputEvent) -> void:
23
26
  super._input(event)
24
27
  if get_viewport().is_input_handled(): return
28
+ if Focus.key(get_viewport(),event): get_viewport().set_input_as_handled()
29
+ # Native GUI may consume a release before unhandled delivery. Drop any old
30
+ # capture after that event without routing a click through a native Control.
31
+ if event is InputEventMouseButton and not event.pressed and event.button_index==MOUSE_BUTTON_LEFT and _pointer.has_capture(): _pointer.cancel_if_captured.call_deferred()
32
+ func _unhandled_input(event: InputEvent) -> void:
33
+ if Engine.is_editor_hint() or get_viewport().is_input_handled(): return
34
+ if event is InputEventMouse and Focus.pointer_entry(get_viewport(),event.position)!=self:
35
+ if event is InputEventMouseMotion: _pointer.cancel()
36
+ return
25
37
  if _pointer.input(event): get_viewport().set_input_as_handled()
38
+ func _shortcut_input(event: InputEvent) -> void:
39
+ if not Engine.is_editor_hint() and Focus.native_key(get_viewport(),event): get_viewport().set_input_as_handled()
@@ -126,6 +126,9 @@ func _sync() -> void:
126
126
  if _mode == Mode.COMPOSITION and managed_root != null and managed_root.get_ref() != null: children.append(managed_root.get_ref())
127
127
  children = CompositionHosts.children(host,children)
128
128
  for child in children:
129
+ # Native child-entered signals can precede internal-child bookkeeping.
130
+ # Runtime GUI endpoints/decorations never become layout participants.
131
+ if host.has_method("_composition_is_private") and host._composition_is_private(child): continue
129
132
  if _dimension==2 and model.strategy in ["wrap","grid"] and child is CanvasItem and not child.is_visible_in_tree(): continue
130
133
  var found = participant(child,true)
131
134
  if found != null and found._mode != Mode.ENTRY:
@@ -0,0 +1,138 @@
1
+ @tool
2
+ extends Resource
3
+ ## One native GUI focus owner. This registry only joins native GUI roots for Tab.
4
+ ## Weak Entries; no retained focus graph, Node metadata or frame callbacks.
5
+ const Pointer = preload("pointer_2d.gd")
6
+ var viewports: Dictionary = {}
7
+ var refreshes := 0
8
+ var queued := false
9
+ func add(entry: Node) -> void:
10
+ if Engine.is_editor_hint(): return
11
+ var viewport := entry.get_viewport()
12
+ var id := viewport.get_instance_id()
13
+ if not viewports.has(id): viewports[id] = {"viewport":weakref(viewport),"entries":[]}
14
+ for reference in viewports[id].entries:
15
+ if reference.get_ref()==entry: return
16
+ viewports[id].entries.append(weakref(entry))
17
+ var tree := entry.get_tree()
18
+ if not tree.tree_changed.is_connected(refresh): tree.tree_changed.connect(refresh)
19
+ refresh()
20
+ func remove(entry: Node) -> void:
21
+ var tree := entry.get_tree()
22
+ for id in viewports.keys():
23
+ var refs: Array = viewports[id].entries
24
+ for index in range(refs.size()-1,-1,-1):
25
+ if refs[index].get_ref()==null or refs[index].get_ref()==entry: refs.remove_at(index)
26
+ if refs.is_empty(): viewports.erase(id)
27
+ if viewports.is_empty() and tree.tree_changed.is_connected(refresh): tree.tree_changed.disconnect(refresh)
28
+ func native_key(viewport: Viewport, event: InputEvent) -> bool:
29
+ # Shortcut delivery follows native GUI processing and precedes game
30
+ # unhandled keys. Consume native BaseButton's unhandled accept without
31
+ # suppressing its activation or forwarding/re-emitting a second event.
32
+ var owner := viewport.gui_get_focus_owner()
33
+ return owner is BaseButton and contains(owner) and event is InputEventKey and event.is_action("ui_accept")
34
+ func refresh() -> void:
35
+ if queued: return
36
+ queued = true
37
+ _validate.call_deferred()
38
+ func _validate() -> void:
39
+ queued = false
40
+ refreshes += 1
41
+ for record in viewports.values():
42
+ var viewport = record.viewport.get_ref()
43
+ if viewport==null: continue
44
+ var owner = viewport.gui_get_focus_owner()
45
+ if owner!=null and contains(owner) and not eligible(owner): owner.release_focus()
46
+ func contains(control: Control) -> bool:
47
+ var record: Dictionary = viewports.get(control.get_viewport().get_instance_id(),{})
48
+ for reference in record.get("entries",[]):
49
+ var entry = reference.get_ref()
50
+ if entry!=null and (control==entry or entry.is_ancestor_of(control)): return true
51
+ return false
52
+ static func owns(control: Control) -> bool:
53
+ return control.has_method("_estragon_focus_host")
54
+ static func rectangle(control: Control) -> PackedVector2Array:
55
+ var transform := control.get_global_transform_with_canvas()
56
+ var extent := control.size
57
+ return PackedVector2Array([transform*Vector2.ZERO,transform*Vector2(extent.x,0),transform*extent,transform*Vector2(0,extent.y)])
58
+ static func intersect(region: Array, boundary: PackedVector2Array) -> Array:
59
+ var result: Array = []
60
+ for polygon in region:
61
+ for clipped in Geometry2D.intersect_polygons(polygon,boundary):
62
+ # Touching clip edges have no focusable area.
63
+ var area := 0.0
64
+ for index in range(clipped.size()): area += clipped[index].cross(clipped[(index+1)%clipped.size()])
65
+ if not is_zero_approx(area): result.append(clipped)
66
+ return result
67
+ static func eligible(control: Control, tab: bool = false) -> bool:
68
+ if not is_instance_valid(control) or not control.is_inside_tree() or control.is_queued_for_deletion() or not control.is_visible_in_tree() or not control.can_process(): return false
69
+ if control.focus_mode==Control.FOCUS_NONE or tab and control.focus_mode!=Control.FOCUS_ALL: return false
70
+ if control is BaseButton and control.disabled: return false
71
+ if owns(control):
72
+ var host = control._estragon_focus_host()
73
+ if host==null or host.disabled or host.is_queued_for_deletion(): return false
74
+ if control.size.x<=0 or control.size.y<=0: return false
75
+ var transform := control.get_global_transform_with_canvas()
76
+ if is_zero_approx(transform.x.cross(transform.y)): return false
77
+ var visible := control.get_viewport_rect()
78
+ var boundary := PackedVector2Array([visible.position,Vector2(visible.end.x,visible.position.y),visible.end,Vector2(visible.position.x,visible.end.y)])
79
+ var region := intersect([rectangle(control)],boundary)
80
+ if region.is_empty(): return false
81
+ var parent := control.get_parent()
82
+ while parent!=null:
83
+ if parent.is_queued_for_deletion(): return false
84
+ if parent is Control and parent.clip_contents:
85
+ region = intersect(region,rectangle(parent))
86
+ if region.is_empty(): return false
87
+ parent = parent.get_parent()
88
+ return true
89
+ func _collect(node: Node, viewport: Viewport, output: Array) -> void:
90
+ if node.get_viewport()!=viewport: return
91
+ if node is Control and eligible(node,true) and not output.has(node): output.append(node)
92
+ for child in node.get_children(true): _collect(child,viewport,output)
93
+ func candidates(viewport: Viewport) -> Array:
94
+ var output: Array = []
95
+ var record: Dictionary = viewports.get(viewport.get_instance_id(),{})
96
+ for reference in record.get("entries",[]):
97
+ var entry = reference.get_ref()
98
+ if entry!=null and entry.is_inside_tree(): _collect(entry,viewport,output)
99
+ output.sort_custom(func(a,b): return b.is_greater_than(a))
100
+ return output
101
+ func key(viewport: Viewport, event: InputEvent) -> bool:
102
+ if not event is InputEventKey or event.keycode!=KEY_TAB or event.alt_pressed or event.ctrl_pressed or event.meta_pressed: return false
103
+ var list := candidates(viewport)
104
+ var owner := viewport.gui_get_focus_owner()
105
+ # Native Controls outside the participating Entry keep their native behavior.
106
+ if owner!=null and not contains(owner): return false
107
+ if list.is_empty(): return false
108
+ if not event.pressed or event.echo: return true
109
+ var path := NodePath()
110
+ if owner!=null: path = owner.focus_previous if event.shift_pressed else owner.focus_next
111
+ if owner!=null and not path.is_empty():
112
+ var explicit = owner.get_node_or_null(path)
113
+ if explicit is Control and eligible(explicit,true): explicit.grab_focus(); return true
114
+ var index := list.find(owner)
115
+ if index<0 and owner!=null:
116
+ var ordered := list.duplicate()
117
+ if event.shift_pressed: ordered.reverse()
118
+ for candidate in ordered:
119
+ if owner.is_greater_than(candidate) if event.shift_pressed else candidate.is_greater_than(owner):
120
+ candidate.grab_focus()
121
+ return true
122
+ if index<0: index = 0 if event.shift_pressed else -1
123
+ list[posmod(index+(-1 if event.shift_pressed else 1),list.size())].grab_focus()
124
+ return true
125
+ func pointer_entry(viewport: Viewport, point: Vector2) -> Node:
126
+ var found
127
+ var result
128
+ var record: Dictionary = viewports.get(viewport.get_instance_id(),{})
129
+ for reference in record.get("entries",[]):
130
+ var entry = reference.get_ref()
131
+ if entry==null or not entry.is_inside_tree(): continue
132
+ var pointer = entry._get_pointer_router()
133
+ if pointer.has_capture(): return entry
134
+ var button = pointer.pick(point)
135
+ if button!=null and (found==null or pointer.above(button,found)):
136
+ found = button
137
+ result = entry
138
+ return result
@@ -0,0 +1 @@
1
+ uid://c2xwpsv0u0ns
@@ -0,0 +1,6 @@
1
+ [gd_resource type="Resource" script_class="" load_steps=2 format=3]
2
+
3
+ [ext_resource type="Script" path="focus.gd" id="1"]
4
+
5
+ [resource]
6
+ script = ExtResource("1")
@@ -0,0 +1,48 @@
1
+ @tool
2
+ extends Control
3
+ ## Transparent native keyboard/focus endpoint, never a layout or mouse owner.
4
+ const Focus = preload("focus.tres")
5
+ var held_key := 0
6
+ func _estragon_focus_host(): return get_parent()
7
+ func _ready() -> void:
8
+ mouse_filter = Control.MOUSE_FILTER_IGNORE
9
+ focus_entered.connect(_entered)
10
+ focus_exited.connect(_exited)
11
+ visibility_changed.connect(_visibility)
12
+ func _entered() -> void:
13
+ var host = _estragon_focus_host()
14
+ if not Focus.eligible(self): release_focus(); return
15
+ host._queue_style_redraw()
16
+ host.focus_entered.emit()
17
+ func _exited() -> void:
18
+ cancel()
19
+ var host = _estragon_focus_host()
20
+ host._cancel_pointer()
21
+ host._queue_style_redraw()
22
+ host.focus_exited.emit()
23
+ func _visibility() -> void:
24
+ if not is_visible_in_tree(): cancel()
25
+ func cancel() -> void:
26
+ held_key = 0
27
+ var host = _estragon_focus_host()
28
+ host._keyboard_held = false
29
+ host._queue_style_redraw()
30
+ func _gui_input(event: InputEvent) -> void:
31
+ if not event is InputEventKey: return
32
+ if not Focus.eligible(self): cancel(); release_focus(); return
33
+ if event.is_action("ui_cancel"):
34
+ cancel()
35
+ accept_event()
36
+ return
37
+ if not event.is_action("ui_accept"): return
38
+ accept_event()
39
+ var host = _estragon_focus_host()
40
+ if event.echo: return
41
+ if event.pressed:
42
+ if held_key==0:
43
+ held_key = event.keycode
44
+ host._keyboard_held = true
45
+ host._queue_style_redraw()
46
+ elif held_key==event.keycode:
47
+ cancel()
48
+ if Focus.eligible(self) and has_focus(): host.pressed.emit()
@@ -0,0 +1 @@
1
+ uid://dgewlcavc2fjg
@@ -32,14 +32,28 @@ func cancel() -> void:
32
32
  func cancel_button(button: Node2D) -> void:
33
33
  if _captured != null and _captured.get_ref() == button: cancel()
34
34
  button._set_pointer_state(false,false)
35
+ func has_capture() -> bool:
36
+ return _captured!=null and _captured.get_ref()!=null
37
+ func cancel_if_captured() -> void:
38
+ if has_capture(): cancel()
35
39
  func _z(button: CanvasItem) -> int:
36
40
  var result: int = button.z_index
37
- var parent: CanvasItem = button.get_parent_item()
38
- while button.z_as_relative and parent != null:
41
+ var parent := button.get_parent()
42
+ while not button.is_set_as_top_level() and button.z_as_relative and parent is CanvasItem:
39
43
  result += parent.z_index
40
44
  button = parent
41
- parent = button.get_parent_item()
45
+ parent = button.get_parent()
42
46
  return result
47
+ func _layer(item: CanvasItem) -> int:
48
+ var parent := item.get_parent()
49
+ while parent!=null and not parent is Viewport:
50
+ if parent is CanvasLayer: return parent.layer
51
+ parent = parent.get_parent()
52
+ return 0
53
+ func above(item: CanvasItem, other: CanvasItem) -> bool:
54
+ if _layer(item)!=_layer(other): return _layer(item)>_layer(other)
55
+ if _z(item)!=_z(other): return _z(item)>_z(other)
56
+ return item.is_greater_than(other)
43
57
  static func inside_clips(item: CanvasItem, position: Vector2, exclude_bars: bool = true) -> bool:
44
58
  var parent := item.get_parent()
45
59
  while parent!=null:
@@ -55,7 +69,7 @@ func pick(position: Vector2):
55
69
  for ref in _buttons:
56
70
  var button = ref.get_ref()
57
71
  if button == null or button.is_queued_for_deletion() or not button.is_inside_tree() or not button.is_visible_in_tree() or not button._pointer_contains(position) or not inside_clips(button,position): continue
58
- if found == null or _z(button) > _z(found) or _z(button) == _z(found) and button.is_greater_than(found): found = button
72
+ if found == null or above(button,found): found = button
59
73
  return found
60
74
  func input(event: InputEvent) -> bool:
61
75
  if not event is InputEventMouse: return false
@@ -69,6 +83,7 @@ func input(event: InputEvent) -> bool:
69
83
  if not event is InputEventMouseButton or event.button_index != MOUSE_BUTTON_LEFT: return false
70
84
  if event.pressed:
71
85
  if held == null and hovered != null and not hovered.disabled:
86
+ hovered.grab_focus()
72
87
  _captured = weakref(hovered)
73
88
  hovered._set_pointer_state(true,true)
74
89
  return hovered != null
@@ -3,6 +3,7 @@ extends RefCounted
3
3
  ## Lazy native viewport decoration. One owner coordinator; no child layout policy.
4
4
  const Hosts = preload("composition_hosts.tres")
5
5
  const Pointer = preload("pointer_2d.gd")
6
+ const Focus = preload("focus.tres")
6
7
  var owner_ref: WeakRef
7
8
  var host:
8
9
  get: return owner_ref.get_ref()
@@ -101,7 +102,7 @@ func enabled() -> bool:
101
102
  func clipping() -> bool:
102
103
  return host.clip_content or enabled()
103
104
  func _external_child(child: Node) -> void:
104
- if not is_private(child): _adopt_later.call_deferred(weakref(child))
105
+ if not is_private(child) and not host._composition_is_private(child): _adopt_later.call_deferred(weakref(child))
105
106
  func _adopt_later(reference: WeakRef) -> void:
106
107
  var child = reference.get_ref()
107
108
  if child!=null and host!=null and host.is_inside_tree(): _adopt_content(child)
@@ -144,6 +145,7 @@ func _update_scroll() -> void:
144
145
  _vertical.page = rect.size.y
145
146
  _vertical.value = host.scroll_offset.y
146
147
  _updating = false
148
+ Focus.refresh()
147
149
  func _horizontal_changed(value: float) -> void:
148
150
  if not _updating: host.scroll_offset = Vector2(value,host.scroll_offset.y)
149
151
  func _vertical_changed(value: float) -> void:
@@ -1,7 +1,7 @@
1
1
  @tool
2
2
  class_name EstragonToggle
3
3
  extends "button.gd"
4
- ## Controlled boolean input. Only pointer activation emits a user edit.
4
+ ## Controlled boolean input. Completed native pointer/keyboard activation edits.
5
5
  signal value_changed(value: bool)
6
6
  const ESTRAGON_TOGGLE_STYLE := true
7
7
  var checked_background_color: Variant = Color("#227a60"):
package/package.json CHANGED
@@ -29,5 +29,5 @@
29
29
  "tag": "beta",
30
30
  "registry": "https://registry.npmjs.org/"
31
31
  },
32
- "version": "0.1.0-beta.29"
32
+ "version": "0.1.0-beta.30"
33
33
  }