@papercraftgames/estragon 0.1.0-beta.23 → 0.1.0-beta.24

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.
@@ -197,6 +197,9 @@ func _description(node: Model.Component, indentation: String) -> String:
197
197
  children.append(_arguments[child].name if child is Model.GdExpression else _description(child,indentation))
198
198
  var native_type := _string(node.runtime_class) if ClassDB.class_exists(node.runtime_class) else node.runtime_class
199
199
  _line("var %s := %s.new(%s, {%s}, {%s}, [%s], %s, %s, %s)" % [variable,_element_class,native_type,", ".join(props),", ".join(events),", ".join(children),_string("%s:%d:%d" % [_file.name,node.span.start.line,node.span.start.column]),key,keyed],node.span,indentation)
200
+ for attribute in node.attributes:
201
+ if attribute.kind=="placement" and not expanded: _line(variable+".origins["+_string(attribute.name)+"] = "+_string("%s:%d:%d" % [_file.name,attribute.name_span.start.line,attribute.name_span.start.column]),attribute.name_span,indentation)
202
+
200
203
  if expanded: _line(variable+".expand(["+", ".join(ordered)+"])",node.span,indentation)
201
204
  if not bindings.is_empty(): _line(variable+".bindings = {"+", ".join(bindings)+"}",node.span,indentation)
202
205
  return variable
@@ -5,6 +5,7 @@ const Element = preload("../runtime/composition/element.gd")
5
5
  const Model = preload("model.gd")
6
6
  const Source = preload("source.gd")
7
7
  const Resolver = preload("native/resolver.gd")
8
+ const GridDefinition = preload("../runtime/node/internal/grid_definition.gd")
8
9
  const StackContract = preload("../runtime/node/internal/stack_contract.gd")
9
10
  const VisualStyle = preload("../runtime/node/internal/visual_style_2d.gd")
10
11
  const Length = preload("../runtime/node/internal/length.gd")
@@ -93,6 +94,9 @@ func _component(node: Model.Component) -> void:
93
94
  _error("Inline anonymous GGX handlers are not supported; use a named handler/Callable", token.span)
94
95
  return
95
96
  index = token.span.end.index
97
+ elif name in GridDefinition.PLACEMENT and not properties.has(name):
98
+ attribute.kind = "placement"
99
+ attribute.type_name = "String" if attribute.value is Model.Literal and name=="area" else "Variant"
96
100
  elif properties.has(name) and NativeAttributes.writable(resolved.native,resolved.script,name,properties[name]):
97
101
  attribute.type_name = "Variant" if Length.registered(resolved.script,name) or VisualStyle.registered(resolved.script,name) else _native_type(properties[name])
98
102
  if attribute.type_name.is_empty():
@@ -106,11 +110,23 @@ func _component(node: Model.Component) -> void:
106
110
  if not message.is_empty():
107
111
  _error(message,attribute.value.span)
108
112
  return
113
+ if not expanded and GridDefinition.marked(resolved.script) and attribute.value is Model.Literal:
114
+ if name in ["horizontal_align","vertical_align"] and attribute.value.value not in ["start","center","end","stretch"]:
115
+ _error("Grid per-cell "+name+" requires start/center/end/stretch",attribute.value.span)
116
+ return
117
+ if name=="template":
118
+ var parsed := GridDefinition.resolve(null,null,[],str(attribute.value.value))
119
+ if parsed.has("error"):
120
+ _error("Grid template: "+parsed.error,attribute.value.span)
121
+ return
109
122
  if name=="align_self" and StackContract.self_registered(resolved.script) and attribute.value is Model.Literal:
110
123
  var message := StackContract.error(name,attribute.value.value,"")
111
124
  if not message.is_empty():
112
125
  _error(message,attribute.value.span)
113
126
  return
127
+ if GridDefinition.marked(resolved.script) and name in ["horizontal_align","vertical_align"] and attribute.value is Model.Literal and attribute.value.value not in ["start","center","end","stretch"]:
128
+ _error("Grid "+name+" requires start, center, end or stretch",attribute.value.span)
129
+ return
114
130
  var is_color := VisualStyle.registered(resolved.script,name)
115
131
  if is_color:
116
132
  _color(attribute)
@@ -140,6 +156,11 @@ func _component(node: Model.Component) -> void:
140
156
  _error("Node GGX children must be newly declared Node elements; expression adoption is unsupported", child.span)
141
157
  return
142
158
  _node(child)
159
+ if not GridDefinition.marked(resolved.script):
160
+ for attribute in child.attributes:
161
+ if attribute.kind=="placement":
162
+ _error("GGX placement "+attribute.name+" requires a Grid parent",attribute.name_span)
163
+ return
143
164
  if child is Model.Component and not child.attributes.any(func(item): return item.kind=="spread"):
144
165
  for attribute in child.attributes:
145
166
  if attribute.name=="name" and attribute.value is Model.Literal:
@@ -450,6 +450,15 @@ func definition(text: String, source: String, symbol: String) -> String:
450
450
  for item in _project_items():
451
451
  if item.path == target: return item.source
452
452
  return target
453
+ func _grid_type(type: String, aliases: Dictionary, visited: Array = []) -> bool:
454
+ var name: String = aliases.get(type,type)
455
+ if name in visited or visited.size()>=12: return false
456
+ var grid := Catalog.named("EstragonGrid")
457
+ if not grid.is_empty() and name==Catalog.path(grid): return true
458
+ if not name.begins_with("res://") or not FileAccess.file_exists(name): return false
459
+ var declarations := _script(name)
460
+ return _grid_type(declarations.base,declarations.aliases,visited+[name])
461
+
453
462
  func candidates(text: String, source: String, lexical: RefCounted, row: int, column: int) -> Array:
454
463
  _index(text,source)
455
464
  lexical.update(text)
@@ -491,6 +500,8 @@ func candidates(text: String, source: String, lexical: RefCounted, row: int, col
491
500
  else:
492
501
  if not _node_type(context.tag,_aliases): return []
493
502
  var members := _members(context.tag,_aliases)
503
+ if not context.tags.is_empty() and _grid_type(context.tags[-1],_aliases):
504
+ for name in ["area","row","column","row_span","column_span"]: offered[name] = {"kind":CodeEdit.KIND_MEMBER,"origin":"Grid child placement"}
494
505
  for name in members:
495
506
  if members[name].kind in [CodeEdit.KIND_MEMBER,CodeEdit.KIND_SIGNAL] and members[name].get("writable",true) and name not in ["owner","script"]: offered[name] = members[name]
496
507
  if _node_type(context.tag,_aliases): offered.key = {"kind":CodeEdit.KIND_MEMBER}
@@ -2,6 +2,7 @@
2
2
  extends RefCounted
3
3
  ## Trusted installed resources, not guessed global classes or installation roots.
4
4
  const ITEMS := [
5
+ {"alias":"Grid","canonical":"EstragonGrid","script":preload("../runtime/node/grid.gd"),"dimension":2,"origin":"native layout"},
5
6
  {"alias":"Spacer","canonical":"EstragonSpacer","script":preload("../runtime/node/spacer.gd"),"dimension":2,"origin":"native layout"},
6
7
  {"alias":"Box","canonical":"EstragonBox","script":preload("../runtime/node/box.gd"),"dimension":2,"origin":"native layout"},
7
8
  {"alias":"Text","canonical":"EstragonText","script":preload("../runtime/node/text.gd"),"dimension":2,"origin":"native widget"},
@@ -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.23"
6
+ version="0.1.0-beta.24"
7
7
  script="plugin.gd"
@@ -1,4 +1,6 @@
1
1
  extends RefCounted
2
+ const GridDefinition = preload("../node/internal/grid_definition.gd")
3
+ const GRID_CONFIG := ["columns","rows","areas","template","horizontal_align","vertical_align"]
2
4
  const StackContract = preload("../node/internal/stack_contract.gd")
3
5
  const VisualStyle = preload("../node/internal/visual_style_2d.gd")
4
6
  const Length = preload("../node/internal/length.gd")
@@ -164,7 +166,8 @@ func _normalize(value, ancestors: Array = [], level: int = 0):
164
166
  var next := ancestors.duplicate()
165
167
  next.append(value)
166
168
  if Profile.is_enabled(): Profile.add("normalized_elements")
167
- var result := Element.new(value.type,value.properties,value.handlers,[],value.source,value.key,value.keyed)
169
+ var result := Element.new(value.type,value.properties.duplicate(),value.handlers,[],value.source,value.key,value.keyed)
170
+ result.placement = value.placement.duplicate(true)
168
171
  result.origins = value.origins.duplicate()
169
172
  result.spread_properties = value.spread_properties.duplicate()
170
173
  result.bindings = value.bindings.duplicate()
@@ -342,13 +345,17 @@ func _apply(desired: Element, record: Mounted) -> void:
342
345
  writes += 1
343
346
  if Profile.is_enabled(): Profile.add("property_writes")
344
347
  binding.attach(node,self)
348
+ Hosts.set_placement(node,desired.placement)
349
+ var grid := GridDefinition.marked(desired.type)
350
+ if grid:
351
+ node._apply_grid(_grid_config(desired))
345
352
  var stack := StackContract.registered(desired.type)
346
353
  if stack:
347
354
  var policy := resets.duplicate()
348
355
  policy.merge(desired.properties,true)
349
356
  node._apply_stack_policy(policy)
350
357
  for key in resets:
351
- if not stack or key not in ["direction","horizontal_align","vertical_align"]: node.set(key,resets[key])
358
+ if (not stack or key not in ["direction","horizontal_align","vertical_align"]) and (not grid or key not in GRID_CONFIG): node.set(key,resets[key])
352
359
  writes += 1
353
360
  if Profile.is_enabled(): Profile.add("property_writes")
354
361
  # An omitted previously declared border side removes that local override;
@@ -526,6 +533,12 @@ func _order(children: Array, parent: Node) -> void:
526
533
  if Profile.is_enabled(): Profile.add("moves")
527
534
  anchor = actual
528
535
 
536
+ func _grid_config(desired: Element) -> Dictionary:
537
+ var result := {"columns":null,"rows":null,"areas":[],"template":"","horizontal_align":"stretch","vertical_align":"stretch"}
538
+ for key in GRID_CONFIG:
539
+ if desired.properties.has(key): result[key] = desired.properties[key]
540
+ return result
541
+
529
542
  func _preflight(desired, previous, parent: Node, contracts: Variant = null) -> bool:
530
543
  if Profile.is_enabled(): Profile.add("preflights")
531
544
  if desired == null: return true
@@ -556,12 +569,52 @@ func _preflight(desired, previous, parent: Node, contracts: Variant = null) -> b
556
569
  policy.merge(desired.properties,true)
557
570
  var fixed := StackContract.preset(desired.type)
558
571
  var direction = policy.get("direction",fixed if not fixed.is_empty() else "row")
559
- for key in ["direction","horizontal_align","vertical_align"]:
572
+ for key in ["direction","horizontal_align","vertical_align","align_content"]:
560
573
  var value = policy.get(key,direction if key=="direction" else "start")
561
574
  var message := StackContract.error(key,value,str(direction),fixed)
562
575
  if not message.is_empty():
563
576
  validation_error = desired.location(key)+": "+message+"; previous tree retained"
564
577
  return false
578
+ if GridDefinition.marked(desired.type):
579
+ var config := _grid_config(desired)
580
+ var grid_matches := _matches(desired.children,previous.children if previous!=null and _compatible(desired,previous) else [])
581
+ var placements: Array = []
582
+ var grid_children: Array = []
583
+ for child in desired.children:
584
+ if child==null: continue
585
+ if child.borrowed==null:
586
+ for key in GridDefinition.PLACEMENT:
587
+ if child.properties.has(key):
588
+ child.placement[key] = child.properties[key]
589
+ child.properties.erase(key)
590
+ child.spread_properties.erase(key)
591
+ var native: String = child.type if child.type is String else child.type.get_instance_base_type()
592
+ if not ClassDB.is_parent_class(native,"Control") and not (ClassDB.is_parent_class(native,"Node2D") and (Length.registered(child.type,"width") or GridDefinition.component(child.type))):
593
+ return _invalid_attribute(child,"area","Grid child requires a native 2D layout participant, Control or Gogo component")
594
+ if child.borrowed!=null:
595
+ var borrowed: Node = child.borrowed.get_ref()
596
+ if not borrowed is Control and not (borrowed is Node2D and (Length.registered(borrowed.get_script(),"width") or GridDefinition.component(borrowed.get_script()))): return _invalid_attribute(child,"area","Grid borrowed child has no bounded 2D layout participant")
597
+ var native: String = child.type if child.type is String else (child.borrowed.get_ref().get_class() if child.borrowed!=null else child.type.get_instance_base_type())
598
+ if ClassDB.is_parent_class(native,"Control"):
599
+ var values := {}
600
+ var old = grid_matches[desired.children.find(child)]
601
+ var existing = child.borrowed.get_ref() if child.borrowed!=null else (old.node.get_ref() if old!=null else null)
602
+ if existing!=null:
603
+ for key in ["anchor_left","anchor_right","anchor_top","anchor_bottom","rotation","scale","pivot_offset","top_level"]: values[key] = existing.get(key) if key!="top_level" else existing.is_set_as_top_level()
604
+ values.merge(child.properties,true)
605
+ var message := GridDefinition.control_error(values)
606
+ if not message.is_empty():
607
+ var field := "area"
608
+ for key in ["anchor_left","anchor_right","anchor_top","anchor_bottom","rotation","scale","pivot_offset","top_level"]:
609
+ if child.properties.has(key): field = key
610
+ return _invalid_attribute(child,field,message)
611
+ placements.append(child.placement)
612
+ grid_children.append(child)
613
+ var parsed := GridDefinition.configuration(config,placements)
614
+ if parsed.has("error"):
615
+ var origin = grid_children[parsed.item] if parsed.has("item") else desired
616
+ var key: String = str(origin.placement.keys()[0]) if parsed.has("item") and not origin.placement.is_empty() else parsed.get("field","columns")
617
+ return _invalid_attribute(origin,key,parsed.error)
565
618
  if VisualStyle.rect_conflict(desired.type,desired.properties.keys()):
566
619
  validation_error = desired.location("fill")+": Rect fill and background_color conflict; previous tree retained"
567
620
  return false
@@ -639,6 +692,7 @@ func _free_managed(record: Mounted, rescue: Node = null) -> void:
639
692
  if child != null: _free_managed(child,rescue)
640
693
  var node = record.node.get_ref()
641
694
  if node == null:
695
+ Hosts.clear_placement(record.id)
642
696
  state.get_ref().managed.erase(record.id)
643
697
  return
644
698
  for binding in record.bindings.values(): binding.disconnect_edit()
@@ -655,6 +709,7 @@ func _free_managed(record: Mounted, rescue: Node = null) -> void:
655
709
  owner.add_child(child)
656
710
  if persisted_owner != null and persisted_owner.is_ancestor_of(child): child.owner = persisted_owner
657
711
  var id: int = node.get_instance_id()
712
+ Hosts.set_placement(node,{})
658
713
  node.free()
659
714
  if manager != null: manager.managed.erase(id)
660
715
  frees += 1
@@ -692,4 +747,5 @@ func _projection_tree(record, projected: Dictionary, ordering: Dictionary) -> vo
692
747
  func _unindex(record) -> void:
693
748
  if record == null or record.borrowed != null: return
694
749
  state.get_ref().managed.erase(record.id)
750
+ Hosts.clear_placement(record.id)
695
751
  for child in record.children: _unindex(child)
@@ -6,6 +6,7 @@ const Profile = preload("profile.tres")
6
6
  var borrowed: WeakRef
7
7
  var type: Variant
8
8
  var properties: Dictionary
9
+ var placement: Dictionary = {}
9
10
  var bindings: Dictionary = {}
10
11
  var handlers: Dictionary
11
12
  var children: Array
@@ -3,6 +3,20 @@ class_name EstragonBox
3
3
  extends "component_2d.gd"
4
4
  ## Constructible horizontal Box. VBox selects the vertical axis.
5
5
  const ESTRAGON_STACK_POLICY := true
6
+ @export var wrap: bool = false:
7
+ set(value):
8
+ if value==wrap: return
9
+ wrap = value
10
+ _changed()
11
+ @export_enum("start","center","end","space-between","space-around","space-evenly","stretch") var align_content: String = "start":
12
+ set(value):
13
+ if value==align_content: return
14
+ var message := StackContract.error("align_content",value,"")
15
+ if not message.is_empty():
16
+ push_error("Estragon Box: "+message)
17
+ return
18
+ align_content = value
19
+ _changed()
6
20
  var _policy_commit := false
7
21
  const StackContract = preload("internal/stack_contract.gd")
8
22
  @export_enum("row", "column") var direction: String = "row":
@@ -54,4 +68,6 @@ func _layout_axis() -> int:
54
68
  func _layout_options() -> Dictionary:
55
69
  var result := super._layout_options()
56
70
  result["alignment"] = [horizontal_align,vertical_align,"start"]
71
+ result["strategy"] = "wrap" if wrap else "stack"
72
+ result["align_content"] = align_content
57
73
  return result
@@ -7,6 +7,17 @@ signal layout_applied(size: Vector2)
7
7
  const Coordinator = preload("internal/coordinator.gd")
8
8
  const ESTRAGON_LENGTHS := true
9
9
  const ESTRAGON_MINIMUMS_2D := true
10
+ const ESTRAGON_GAPS_2D := true
11
+ var row_gap: Variant = "auto":
12
+ set(value):
13
+ if is_same(row_gap,value) or not lengths.assign("row_gap",value): return
14
+ row_gap = value
15
+ _changed()
16
+ var column_gap: Variant = "auto":
17
+ set(value):
18
+ if is_same(column_gap,value) or not lengths.assign("column_gap",value): return
19
+ column_gap = value
20
+ _changed()
10
21
  const ESTRAGON_ALIGN_SELF_2D := true
11
22
  const AlignmentContract = preload("internal/stack_contract.gd")
12
23
  const VisualStyle = preload("internal/visual_style_2d.gd")
@@ -169,6 +180,7 @@ var _layout_size := Vector2.ZERO
169
180
  var layout = Coordinator.new(self, 2, Coordinator.Mode.ENTRY if _is_layout_entry() else Coordinator.Mode.PARTICIPANT)
170
181
  func _get_property_list() -> Array[Dictionary]:
171
182
  var properties := Length.properties(["width","height","min_width","min_height","x","y","gap","padding","padding_top","padding_right","padding_bottom","padding_left","margin","margin_top","margin_right","margin_bottom","margin_left"])
183
+ properties.append_array(Length.properties(["row_gap","column_gap"]))
172
184
  if VisualStyle.supported(get_script()): properties.append_array(Length.properties(["border_width","border_top_width","border_right_width","border_bottom_width","border_left_width","background_color","border_color"]))
173
185
  return properties
174
186
  static func valid_length(value) -> bool:
@@ -0,0 +1,73 @@
1
+ @tool
2
+ class_name EstragonGrid
3
+ extends "component_2d.gd"
4
+ ## Native 2D parent-owned track layout, using the shared mathematical core.
5
+ const ESTRAGON_GRID := true
6
+ const Definition = preload("internal/grid_definition.gd")
7
+ const CONFIG := ["columns","rows","areas","template","horizontal_align","vertical_align"]
8
+ var _committing := false
9
+ var columns: Variant = null:
10
+ set(value):
11
+ if typeof(columns)==typeof(value) and columns==value: return
12
+ if not _committing and not _accept("columns",value): return
13
+ columns = value.duplicate(true) if value is Array else value
14
+ if not _committing: _changed()
15
+ var rows: Variant = null:
16
+ set(value):
17
+ if typeof(rows)==typeof(value) and rows==value: return
18
+ if not _committing and not _accept("rows",value): return
19
+ rows = value.duplicate(true) if value is Array else value
20
+ if not _committing: _changed()
21
+ @export var areas: Array = []:
22
+ set(value):
23
+ if areas==value: return
24
+ if not _committing and not _accept("areas",value): return
25
+ areas = value.duplicate(true)
26
+ if not _committing: _changed()
27
+ @export_multiline var template: String = "":
28
+ set(value):
29
+ if template==value: return
30
+ if not _committing and not _accept("template",value): return
31
+ template = value
32
+ if not _committing: _changed()
33
+ @export_enum("start","center","end","stretch") var horizontal_align: String = "stretch":
34
+ set(value):
35
+ if value==horizontal_align: return
36
+ if not _committing and not _accept("horizontal_align",value): return
37
+ horizontal_align = value
38
+ if not _committing: _changed()
39
+ @export_enum("start","center","end","stretch") var vertical_align: String = "stretch":
40
+ set(value):
41
+ if value==vertical_align: return
42
+ if not _committing and not _accept("vertical_align",value): return
43
+ vertical_align = value
44
+ if not _committing: _changed()
45
+ func _config() -> Dictionary:
46
+ return {"columns":columns,"rows":rows,"areas":areas,"template":template,"horizontal_align":horizontal_align,"vertical_align":vertical_align}
47
+ func _accept(key: String, value) -> bool:
48
+ var config := _config()
49
+ config[key] = value
50
+ var placements: Array = []
51
+ for child in _get_layout_coordinator().model.children: placements.append(child.placement)
52
+ var parsed := Definition.configuration(config,placements)
53
+ if parsed.has("error"): push_error("Estragon Grid: "+parsed.error)
54
+ return not parsed.has("error")
55
+ # Whole-description preflight validated the final config and all placements.
56
+ func _apply_grid(values: Dictionary) -> void:
57
+ var before := _config()
58
+ _committing = true
59
+ for key in CONFIG:
60
+ if values.has(key): set(key,values[key])
61
+ _committing = false
62
+ if before!=_config(): _changed()
63
+ func _layout_options() -> Dictionary:
64
+ var result := super._layout_options()
65
+ result.strategy = "grid"
66
+ result.grid = _config()
67
+ result.alignment = [horizontal_align,vertical_align,"start"]
68
+ return result
69
+
70
+ func _get_property_list() -> Array[Dictionary]:
71
+ var result := super._get_property_list()
72
+ for key in ["columns","rows"]: result.append({"name":key,"type":TYPE_NIL,"usage":PROPERTY_USAGE_DEFAULT|PROPERTY_USAGE_NIL_IS_VARIANT})
73
+ return result
@@ -0,0 +1 @@
1
+ uid://ch5su6sf182dt
@@ -157,3 +157,40 @@ func refresh_positions(root: RefCounted) -> void:
157
157
  var node = item.node.get_ref()
158
158
  var coordinator = _coordinator(node)
159
159
  if coordinator != null and coordinator._mode != coordinator.Mode.ENTRY and coordinator.scope_root() == root: coordinator._place(coordinator.model.offset)
160
+
161
+ # Authored Grid placements are external weak records, never foreign properties.
162
+ var placements: Dictionary = {}
163
+ func set_placement(node: Node, value: Dictionary) -> void:
164
+ var id := node.get_instance_id()
165
+ if value.is_empty():
166
+ clear_placement(id)
167
+ return
168
+ var entry: Array = placements.get(id,[])
169
+ if entry.is_empty():
170
+ var reference: WeakRef = weakref(node)
171
+ var callback: Callable = _placement_exited.bind(id,reference)
172
+ node.tree_exited.connect(callback)
173
+ entry = [reference,{},callback]
174
+ entry[1] = value.duplicate(true)
175
+ placements[id] = entry
176
+ var coordinator = _coordinator(node)
177
+ if coordinator!=null: coordinator.model.set_placement(value)
178
+ func clear_placement(id: int) -> void:
179
+ var entry: Array = placements.get(id,[])
180
+ placements.erase(id)
181
+ if entry.is_empty(): return
182
+ var node = entry[0].get_ref()
183
+ if node==null: return
184
+ if node.tree_exited.is_connected(entry[2]): node.tree_exited.disconnect(entry[2])
185
+ var coordinator = _coordinator(node)
186
+ if coordinator!=null: coordinator.model.set_placement({})
187
+ func _placement_exited(id: int, reference: WeakRef) -> void:
188
+ # Exit alone may be a reversible native remove/re-entry. Prune only after
189
+ # native destruction, without retaining the Node or polling idle frames.
190
+ _prune_placement.call_deferred(id,reference)
191
+ func _prune_placement(id: int, reference: WeakRef) -> void:
192
+ var entry: Array = placements.get(id,[])
193
+ if not entry.is_empty() and entry[0]==reference and reference.get_ref()==null: clear_placement(id)
194
+ func get_placement(node: Node) -> Dictionary:
195
+ var entry: Array = placements.get(node.get_instance_id(),[])
196
+ return entry[1] if not entry.is_empty() and entry[0].get_ref()==node else {}
@@ -73,6 +73,7 @@ func start() -> void:
73
73
  # PackedScene restores metadata after construction. Refresh the proof-only
74
74
  # weak registry at entry; C1 resolves the live script field instead.
75
75
  if _mode == Mode.PROOF: host.set_meta(META, weakref(self))
76
+ if _dimension==2 and host is CanvasItem: host.visibility_changed.connect(_visibility_changed)
76
77
  host.child_entered_tree.connect(_child_entered)
77
78
  host.child_exiting_tree.connect(_child_exiting)
78
79
  if host.has_signal("child_order_changed"): host.connect("child_order_changed", _sync)
@@ -91,6 +92,7 @@ func stop() -> void:
91
92
  if control_adapter != null: control_adapter.stop()
92
93
  model.set_children([])
93
94
  return
95
+ if _dimension==2 and host is CanvasItem and host.visibility_changed.is_connected(_visibility_changed): host.visibility_changed.disconnect(_visibility_changed)
94
96
  if host.child_entered_tree.is_connected(_child_entered): host.child_entered_tree.disconnect(_child_entered)
95
97
  if host.child_exiting_tree.is_connected(_child_exiting): host.child_exiting_tree.disconnect(_child_exiting)
96
98
  if host.has_signal("child_order_changed") and host.is_connected("child_order_changed", _sync): host.disconnect("child_order_changed", _sync)
@@ -100,6 +102,10 @@ func stop() -> void:
100
102
  var parent = parent_coordinator()
101
103
  if parent != null: parent._sync()
102
104
 
105
+ func _visibility_changed() -> void:
106
+ var parent = parent_coordinator()
107
+ if parent!=null and parent.model.strategy in ["wrap","grid"]: parent._sync()
108
+
103
109
  func _child_entered(child: Node) -> void:
104
110
  if child is Control: CompositionHosts.enter_control(child)
105
111
  _sync()
@@ -120,9 +126,11 @@ func _sync() -> void:
120
126
  if _mode == Mode.COMPOSITION and managed_root != null and managed_root.get_ref() != null: children.append(managed_root.get_ref())
121
127
  children = CompositionHosts.children(host,children)
122
128
  for child in children:
129
+ if _dimension==2 and model.strategy in ["wrap","grid"] and child is CanvasItem and not child.is_visible_in_tree(): continue
123
130
  var found = participant(child,true)
124
131
  if found != null and found._mode != Mode.ENTRY:
125
132
  if found._mode == Mode.CONTROL and not found.control_adapter.participates(): continue
133
+ found.model.set_placement(CompositionHosts.get_placement(child))
126
134
  values.append(found.model)
127
135
  var error := model.set_children(values)
128
136
  if error != OK: push_error("Estragon layout model ownership: " + error_string(error))
@@ -132,6 +140,7 @@ func changed() -> void:
132
140
  if host == null: return
133
141
  if host.has_method("_layout_options"):
134
142
  var options: Dictionary = host._layout_options()
143
+ model.set_strategy(options.get("strategy","stack") if _dimension==2 else "stack",options.get("align_content","start"),options.get("grid",{}))
135
144
  model.set_policy(options.get("alignment",["start","start","start"]))
136
145
  model.set_align_self(host.align_self if _dimension==2 and "align_self" in host else "auto")
137
146
  if "lengths" in host:
@@ -140,6 +149,7 @@ func changed() -> void:
140
149
  model.set_lengths(host.lengths.specs,host.grow,host.stretch,host.pixels_per_unit if _dimension==3 else 1.0,options.axis,host._default_padding() if host.has_method("_default_padding") else Vector4.ZERO)
141
150
  model.configure(host.requested_extent(),model.gap,host.center,options.axis,options.fill,options.intrinsic,model.padding if "lengths" in host else options.get("padding",Vector4.ZERO),model.margin if "lengths" in host else options.get("margin",Vector4.ZERO))
142
151
  else: model.configure(host.requested_extent(), host.gap, host.center)
152
+ if active: _sync()
143
153
 
144
154
  func _viewport_changed() -> void:
145
155
  var host = _host.get_ref()
@@ -1,6 +1,6 @@
1
1
  @tool
2
2
  extends RefCounted
3
- ## Pure future Grid input normalization, not an executable layout component.
3
+ ## Pure canonical Grid configuration, template and placement normalization.
4
4
  ## Serializable dictionaries only. Track fractions never enter Length dimensions.
5
5
  const Length = preload("length.gd")
6
6
  static func tracks(values) -> Dictionary:
@@ -23,22 +23,26 @@ static func tracks(values) -> Dictionary:
23
23
  return {"tracks":result}
24
24
  static func normalize(columns, rows, areas: Array = [], placements: Array = []) -> Dictionary:
25
25
  var cols := tracks(columns)
26
- if cols.has("error"): return cols
26
+ if cols.has("error"):
27
+ cols.field = "columns"
28
+ return cols
27
29
  var lines := tracks(rows)
28
- if lines.has("error"): return lines
30
+ if lines.has("error"):
31
+ lines.field = "rows"
32
+ return lines
29
33
  var matrix: Array = []
30
34
  var bounds := {}
31
- if not areas.is_empty() and areas.size()!=lines.tracks.size(): return {"error":"area row count must match row tracks"}
35
+ if not areas.is_empty() and areas.size()!=lines.tracks.size(): return {"error":"area row count must match row tracks","field":"areas"}
32
36
  for row in range(areas.size()):
33
- if not areas[row] is String: return {"error":"each area row must be a space-separated string"}
37
+ if not areas[row] is String: return {"error":"each area row must be a space-separated string","field":"areas"}
34
38
  var cells: Array = []
35
39
  for cell in areas[row].replace("\t"," ").split(" ",false): cells.append(cell)
36
- if cells.size()!=cols.tracks.size(): return {"error":"uneven area row or column track count mismatch"}
40
+ if cells.size()!=cols.tracks.size(): return {"error":"uneven area row or column track count mismatch","field":"areas"}
37
41
  matrix.append(cells)
38
42
  for col in range(cells.size()):
39
43
  var name: String = cells[col]
40
44
  if name==".": continue
41
- if not name.is_valid_identifier(): return {"error":"area names must be identifiers or ."}
45
+ if not name.is_valid_identifier(): return {"error":"area names must be identifiers or .","field":"areas"}
42
46
  if not bounds.has(name): bounds[name] = {"row":row,"column":col,"row_span":1,"column_span":1}
43
47
  var rect: Dictionary = bounds[name]
44
48
  rect.row_span = row-rect.row+1
@@ -49,27 +53,125 @@ static func normalize(columns, rows, areas: Array = [], placements: Array = [])
49
53
  var rect: Dictionary = bounds[name]
50
54
  for row in range(rect.row,rect.row+rect.row_span):
51
55
  for col in range(rect.column,rect.column+rect.column_span):
52
- if matrix[row][col]!=name: return {"error":"area '"+name+"' must form one rectangle"}
56
+ if matrix[row][col]!=name: return {"error":"area '"+name+"' must form one rectangle","field":"areas"}
57
+ var positioned := place(placements,cols.tracks.size(),lines.tracks.size(),bounds,false)
58
+ if positioned.has("error"): return positioned
59
+ return {"columns":cols.tracks,"rows":lines.tracks,"areas":matrix,"bounds":bounds,"placements":positioned.placements}
60
+
61
+ const PLACEMENT := ["area","row","column","row_span","column_span"]
62
+ static func marked(script) -> bool:
63
+ while script is Script:
64
+ if script.get_script_constant_map().get("ESTRAGON_GRID",false): return true
65
+ script = script.get_base_script()
66
+ return false
67
+
68
+ static func template(value: String) -> Dictionary:
69
+ var columns = null
70
+ var rows: Array = []
71
+ var areas: Array = []
72
+ for raw in value.split("\n"):
73
+ var line: String = raw.strip_edges()
74
+ if line.is_empty(): continue
75
+ if line.begins_with("\\"):
76
+ if columns!=null: return {"error":"template has more than one column declaration"}
77
+ columns = Array(line.substr(1).replace("\t"," ").split(" ",false))
78
+ else:
79
+ var pair := line.split(",")
80
+ if pair.size()>2: return {"error":"template row requires area names, optionally followed by one track"}
81
+ areas.append(pair[0].strip_edges())
82
+ rows.append(pair[1].strip_edges() if pair.size()==2 else "auto")
83
+ if columns==null or areas.is_empty(): return {"error":"template requires area rows and one backslash column declaration"}
84
+ return {"columns":columns,"rows":rows,"areas":areas}
85
+
86
+ static func resolve(columns = null, rows = null, areas: Array = [], text: String = "", placements: Array = []) -> Dictionary:
87
+ if not text.is_empty():
88
+ if columns!=null or rows!=null or not areas.is_empty(): return {"error":"template conflicts with explicit columns/rows/areas","field":"template"}
89
+ var parsed := template(text)
90
+ if parsed.has("error"):
91
+ parsed.field = "template"
92
+ return parsed
93
+ return resolve(parsed.columns,parsed.rows,parsed.areas,"",placements)
94
+ var cols := tracks(1 if columns==null else columns)
95
+ if cols.has("error"): return cols
96
+ var implicit := rows==null
97
+ var row_values = rows
98
+ if implicit:
99
+ row_values = []
100
+ for index in range(maxi(1,areas.size())): row_values.append("auto")
101
+ var canonical := normalize(columns if columns!=null else 1,row_values,areas,[])
102
+ if canonical.has("error"): return canonical
103
+ var positioned := place(placements,canonical.columns.size(),canonical.rows.size(),canonical.bounds,implicit)
104
+ if positioned.has("error"): return positioned
105
+ while canonical.rows.size()<positioned.row_count: canonical.rows.append({"unit":"auto","value":0.0})
106
+ canonical.placements = positioned.placements
107
+ canonical.implicit_rows = implicit
108
+ return canonical
109
+
110
+ static func place(values: Array, columns: int, rows: int, bounds: Dictionary, implicit: bool) -> Dictionary:
53
111
  var normalized: Array = []
112
+ normalized.resize(values.size())
54
113
  var occupied := {}
55
- for placement in placements:
56
- if not placement is Dictionary: return {"error":"placement must be a dictionary"}
57
- var rect: Dictionary
114
+ var row_count := rows
115
+ for index in range(values.size()):
116
+ var placement = values[index]
117
+ if not placement is Dictionary: return {"error":"placement must be a dictionary","item":index}
118
+ for key in placement:
119
+ if key not in PLACEMENT: return {"error":"unknown placement field: "+str(key),"item":index}
120
+ var rect := {"row":placement.get("row",0),"column":placement.get("column",0),"row_span":placement.get("row_span",1),"column_span":placement.get("column_span",1)}
58
121
  if placement.has("area"):
59
- if placement.size()!=1: return {"error":"area conflicts with explicit row/column placement"}
60
- if not bounds.has(placement.area): return {"error":"unknown placement area: "+str(placement.area)}
122
+ if placement.size()!=1: return {"error":"area conflicts with explicit row/column placement","item":index}
123
+ if not placement.area is String or not bounds.has(placement.area): return {"error":"unknown placement area: "+str(placement.area),"item":index}
61
124
  rect = bounds[placement.area].duplicate()
62
- else:
63
- for key in placement:
64
- if key not in ["row","column","row_span","column_span"]: return {"error":"unknown placement field: "+str(key)}
65
- rect = {"row":placement.get("row",0),"column":placement.get("column",0),"row_span":placement.get("row_span",1),"column_span":placement.get("column_span",1)}
66
- for key in rect:
67
- if not rect[key] is int or rect[key]<(1 if key.ends_with("span") else 0): return {"error":"placement indices must be nonnegative integers and spans positive"}
68
- if rect.row+rect.row_span>lines.tracks.size() or rect.column+rect.column_span>cols.tracks.size(): return {"error":"placement exceeds track bounds"}
69
- for row in range(rect.row,rect.row+rect.row_span):
70
- for col in range(rect.column,rect.column+rect.column_span):
71
- var key := str(row)+":"+str(col)
72
- if occupied.has(key): return {"error":"duplicate/conflicting child placement at "+key}
73
- occupied[key] = true
74
- normalized.append(rect)
75
- return {"columns":cols.tracks,"rows":lines.tracks,"areas":matrix,"bounds":bounds,"placements":normalized}
125
+ for key in rect:
126
+ if not rect[key] is int or rect[key]<(1 if key.ends_with("span") else 0) or rect[key]>256: return {"error":"placement indexes must be nonnegative integers and spans positive (maximum 256)","item":index}
127
+ if rect.column_span>columns: return {"error":"column span exceeds tracks","item":index}
128
+ if placement.has("area") or placement.has("row") or placement.has("column"):
129
+ if rect.column+rect.column_span>columns or rect.row+rect.row_span>256 or not implicit and rect.row+rect.row_span>rows: return {"error":"placement exceeds finite tracks","item":index}
130
+ if not _reserve(rect,occupied): return {"error":"Grid cells overlap","item":index}
131
+ normalized[index] = rect
132
+ row_count = maxi(row_count,rect.row+rect.row_span)
133
+ # All authored cells are reserved before any automatic item is considered.
134
+ for index in range(values.size()):
135
+ if normalized[index]!=null: continue
136
+ var rect := {"row":0,"column":0,"row_span":values[index].get("row_span",1),"column_span":values[index].get("column_span",1)}
137
+ var found := false
138
+ for row in range(256 if implicit else rows):
139
+ if row+rect.row_span>(256 if implicit else rows): break
140
+ rect.row = row
141
+ for col in range(columns-rect.column_span+1):
142
+ rect.column = col
143
+ if _reserve(rect,occupied):
144
+ found = true
145
+ break
146
+ if found: break
147
+ if not found: return {"error":"automatic placement exceeds finite tracks","item":index}
148
+ normalized[index] = rect
149
+ row_count = maxi(row_count,rect.row+rect.row_span)
150
+ return {"placements":normalized,"row_count":row_count}
151
+
152
+ static func _reserve(rect: Dictionary, occupied: Dictionary) -> bool:
153
+ for row in range(rect.row,rect.row+rect.row_span):
154
+ for col in range(rect.column,rect.column+rect.column_span):
155
+ if occupied.has(Vector2i(col,row)): return false
156
+ for row in range(rect.row,rect.row+rect.row_span):
157
+ for col in range(rect.column,rect.column+rect.column_span): occupied[Vector2i(col,row)] = true
158
+ return true
159
+
160
+ static func configuration(values: Dictionary, placements: Array = []) -> Dictionary:
161
+ for key in ["horizontal_align","vertical_align"]:
162
+ if values.get(key,"stretch") not in ["start","center","end","stretch"]: return {"error":key+" requires start, center, end or stretch","field":key}
163
+ if not values.get("areas",[]) is Array or not values.get("template","") is String: return {"error":"areas requires Array and template requires String"}
164
+ return resolve(values.get("columns"),values.get("rows"),values.get("areas",[]),values.get("template",""),placements)
165
+
166
+ static func component(script) -> bool:
167
+ while script is Script:
168
+ for method in script.get_script_method_list():
169
+ if method.name=="build_view": return true
170
+ script = script.get_base_script()
171
+ return false
172
+
173
+ static func control_error(values: Dictionary) -> String:
174
+ for key in ["anchor_left","anchor_right","anchor_top","anchor_bottom","rotation"]:
175
+ if values.get(key,0)!=0: return "Grid Control requires zero anchors and rotation"
176
+ if values.get("scale",Vector2.ONE)!=Vector2.ONE or values.get("pivot_offset",Vector2.ZERO)!=Vector2.ZERO or values.get("top_level",false): return "Grid Control requires native parent coordinates and an untransformed outer rectangle"
177
+ return ""
@@ -0,0 +1,68 @@
1
+ @tool
2
+ extends RefCounted
3
+ ## Pure track floors, fractional allocation and native border-box slots.
4
+ const Length = preload("length.gd")
5
+ const Stack = preload("stack_policy.gd")
6
+ static func tracks(specs: Array, items: Array, placements: Array, axis: int, reference: float, viewport: Vector2, gap: float) -> Array:
7
+ var sizes: Array = []
8
+ var flexible: Array = []
9
+ var weight := 0.0
10
+ for index in range(specs.size()):
11
+ var spec: Dictionary = specs[index]
12
+ sizes.append(0.0 if spec.unit in ["auto","fr"] else maxf(0,Length.resolve(spec,reference,viewport)))
13
+ if spec.unit=="fr":
14
+ flexible.append(index)
15
+ weight += spec.value
16
+ for i in range(items.size()):
17
+ var rect: Dictionary = placements[i]
18
+ var start: int = rect.column if axis==0 else rect.row
19
+ var span: int = rect.column_span if axis==0 else rect.row_span
20
+ var occupied := gap*(span-1)
21
+ var eligible: Array = []
22
+ for index in range(start,start+span):
23
+ occupied += sizes[index]
24
+ if specs[index].unit in ["auto","fr"]: eligible.append(index)
25
+ var deficit: float = maxf(0,maxf(items[i].get("contribution",items[i].preferred)[axis],items[i].minimum[axis])+items[i].outer[axis]-occupied)
26
+ for index in eligible: sizes[index] += deficit/eligible.size()
27
+ if reference>=0:
28
+ var budget := reference-gap*maxi(0,sizes.size()-1)
29
+ for index in range(sizes.size()):
30
+ if not flexible.has(index): budget -= sizes[index]
31
+ # Fractional tracks water-fill above intrinsic floors, never shrink them.
32
+ while not flexible.is_empty():
33
+ var constrained := false
34
+ for index in flexible.duplicate():
35
+ if sizes[index]>maxf(0,budget)*specs[index].value/weight:
36
+ budget -= sizes[index]
37
+ weight -= specs[index].value
38
+ flexible.erase(index)
39
+ constrained = true
40
+ if not constrained:
41
+ for index in flexible: sizes[index] = maxf(0,budget)*specs[index].value/weight
42
+ break
43
+ return sizes
44
+ static func layout(items: Array, definition: Dictionary, content: Vector3, gaps: Vector2, alignment: Array, viewport: Vector2) -> Dictionary:
45
+ var columns := tracks(definition.columns,items,definition.placements,0,content.x,viewport,gaps.x)
46
+ var rows := tracks(definition.rows,items,definition.placements,1,content.y,viewport,gaps.y)
47
+ var natural := Vector3(gaps.x*maxi(0,columns.size()-1),gaps.y*maxi(0,rows.size()-1),0)
48
+ for value in columns: natural.x += value
49
+ for value in rows: natural.y += value
50
+ var slots: Array = []
51
+ for index in range(items.size()):
52
+ var rect: Dictionary = definition.placements[index]
53
+ var origin := Vector3.ZERO
54
+ var cell := Vector3.ZERO
55
+ for col in range(rect.column): origin.x += columns[col]+gaps.x
56
+ for row in range(rect.row): origin.y += rows[row]+gaps.y
57
+ for col in range(rect.column,rect.column+rect.column_span): cell.x += columns[col]
58
+ for row in range(rect.row,rect.row+rect.row_span): cell.y += rows[row]
59
+ cell += Vector3(gaps.x*(rect.column_span-1),gaps.y*(rect.row_span-1),0)
60
+ var item: Dictionary = items[index]
61
+ var extent: Vector3 = item.preferred
62
+ var position: Vector3 = origin+item.leading
63
+ for axis in range(2):
64
+ extent[axis] = maxf(extent[axis],item.minimum[axis])
65
+ if item.requested[axis]<0 and alignment[axis]=="stretch": extent[axis] = maxf(item.minimum[axis],cell[axis]-item.outer[axis])
66
+ position[axis] += maxf(0,cell[axis]-extent[axis]-item.outer[axis])*Stack.factor(alignment[axis])
67
+ slots.append({"position":position,"extent":extent})
68
+ return {"extent":natural,"slots":slots,"columns":columns,"rows":rows,"placements":definition.placements}
@@ -0,0 +1 @@
1
+ uid://bdq2kibe1iir0
@@ -1,6 +1,9 @@
1
1
  @tool
2
2
  extends RefCounted
3
3
  const StackPolicy = preload("stack_policy.gd")
4
+ const WrapPolicy = preload("wrap_policy.gd")
5
+ const GridDefinition = preload("grid_definition.gd")
6
+ const GridPolicy = preload("grid_policy.gd")
4
7
  const Length = preload("length.gd")
5
8
  const Profile = preload("../../composition/profile.tres")
6
9
  ## Shared B2/C1 mathematical row layout. No Nodes, rendering or SceneTree dependencies.
@@ -23,7 +26,15 @@ var _forward_context: Array = []
23
26
  var _reported_unanchored := false
24
27
  var _reported_full_percent := false
25
28
  var _reported_fixed_growth := false
29
+ var measurement_allocation := Vector3(-1,-1,-1)
26
30
  var requested := Vector3(-1, -1, -1)
31
+ var placement: Dictionary = {}
32
+ var grid_config: Dictionary = {}
33
+ var grid_definition: Dictionary = {}
34
+ var policy_result: Dictionary = {}
35
+ var strategy := "stack"
36
+ var align_content := "start"
37
+ var physical_gaps := Vector2.ZERO
27
38
  var gap: float = 0.0
28
39
  var center: bool = false
29
40
  var axis: int = 0
@@ -68,6 +79,19 @@ func set_lengths(specs: Dictionary, growth: float, cross: int, scale: float, sta
68
79
  _length_context = []
69
80
  invalidate()
70
81
 
82
+ func set_placement(value: Dictionary) -> void:
83
+ if placement==value: return
84
+ placement = value.duplicate(true)
85
+ invalidate()
86
+
87
+ func set_strategy(value: String, content_alignment: String = "start", config: Dictionary = {}) -> void:
88
+ if strategy==value and align_content==content_alignment and grid_config==config: return
89
+ grid_config = config.duplicate(true)
90
+ grid_definition = {}
91
+ strategy = value
92
+ align_content = content_alignment
93
+ invalidate()
94
+
71
95
  func set_policy(values: Array) -> void:
72
96
  if alignment==values: return
73
97
  alignment = values.duplicate()
@@ -110,23 +134,30 @@ func prepare(reference: Vector3, viewport: Vector2) -> void:
110
134
  _measure_dirty = true
111
135
  _arrange_dirty = true
112
136
  _forward_context = [reference,viewport] if transparent else []
137
+ for index in range(3):
138
+ if parent_reference[index]!=reference[index] and measurement_allocation[index]>=0:
139
+ measurement_allocation[index] = -1
140
+ _measure_dirty = true
113
141
  parent_reference = reference
114
142
  viewport_size = viewport
115
143
  var context := [reference*_parent_axes,viewport*_viewport_axes]
116
144
  if _length_context==context: return
117
145
  _length_context = context
118
146
  resolution_count += 1
119
- var before := [requested,gap,padding,border,margin,length_offset,authored_minimum]
147
+ var before := [requested,gap,physical_gaps,padding,border,margin,length_offset,authored_minimum]
120
148
  if not length_specs.is_empty():
121
149
  for index in range(3):
122
150
  requested[index] = _length(["width","height","depth"][index],index,requested[index])
123
151
  length_offset[index] = _length(["x","y","z"][index],index,0)
124
152
  authored_minimum = Vector3(maxf(0,_length("min_width",0,0)),maxf(0,_length("min_height",1,0)),0)
125
153
  gap = _length("gap",axis,0)
154
+ physical_gaps = Vector2(_length("column_gap",0,-1),_length("row_gap",1,-1))
155
+ for index in range(2):
156
+ if physical_gaps[index]<0: physical_gaps[index] = _length("gap",index,0)
126
157
  padding = _edges("padding")
127
158
  border = _edges("border")
128
159
  margin = _edges("margin")
129
- if before!=[requested,gap,padding,border,margin,length_offset,authored_minimum]:
160
+ if before!=[requested,gap,physical_gaps,padding,border,margin,length_offset,authored_minimum]:
130
161
  _measure_dirty = true
131
162
  _arrange_dirty = true
132
163
 
@@ -144,7 +175,8 @@ func constrain(extent: Vector3) -> Vector3:
144
175
  func _content_basis() -> Vector3:
145
176
  var basis := Vector3(-1,-1,-1)
146
177
  for index in range(3):
147
- if requested[index]>=0: basis[index] = maxf(0,maxf(requested[index],minimum()[index])-leading(padding+border)[index]-trailing(padding+border)[index])
178
+ var allocated: float = requested[index] if requested[index]>=0 else measurement_allocation[index]
179
+ if allocated>=0: basis[index] = maxf(0,maxf(allocated,minimum()[index])-leading(padding+border)[index]-trailing(padding+border)[index])
148
180
  return basis
149
181
 
150
182
  static func valid_extent(value: Vector3) -> bool:
@@ -167,6 +199,7 @@ func configure(extent: Vector3, spacing: float, centered: bool, stack_axis: int
167
199
  intrinsic = content
168
200
  requested = extent
169
201
  gap = spacing
202
+ physical_gaps = Vector2(spacing,spacing)
170
203
  center = centered
171
204
  invalidate()
172
205
  return OK
@@ -186,6 +219,7 @@ func set_children(values: Array) -> Error:
186
219
  if not values.has(child):
187
220
  child._parent = null
188
221
  children = values.duplicate()
222
+ grid_definition = {}
189
223
  for child in children:
190
224
  child._parent = weakref(self)
191
225
  invalidate()
@@ -205,6 +239,75 @@ static func trailing(edges: Vector4) -> Vector3:
205
239
  func outer_extent() -> Vector3:
206
240
  return leading(margin)+trailing(margin)
207
241
 
242
+ # A wrapping item's intrinsic main floor is its largest unbreakable child,
243
+ # while ordinary stack preferred sizes stay unchanged. This lets a fractional
244
+ # Grid cell assign a narrower width before the bounded cross remeasurement.
245
+ func track_contribution() -> Vector3:
246
+ if transparent and children.size()==1: return children[0].track_contribution()
247
+ var result := measured
248
+ if strategy=="wrap" and requested[axis]<0:
249
+ result[axis] = intrinsic[axis]
250
+ for child in children: result[axis] = maxf(result[axis],maxf(child.measured[axis],child.minimum()[axis])+child.outer_extent()[axis])
251
+ result[axis] += leading(padding+border)[axis]+trailing(padding+border)[axis]
252
+ return result
253
+
254
+ func _items() -> Array:
255
+ var items: Array = []
256
+ for child in children:
257
+ items.append({"preferred":child.measured,"contribution":child.track_contribution(),"minimum":child.minimum(),"requested":child.requested,"grow":child.grow,"stretch":child.stretch,"align_self":child.align_self,"fill":child.fill,"outer":child.outer_extent(),"leading":leading(child.margin),"placement":child.placement})
258
+ return items
259
+
260
+ func _grid(items: Array, content: Vector3) -> Dictionary:
261
+ var placements: Array = []
262
+ for item in items: placements.append(item.placement)
263
+ if grid_definition.get("source_placements",[])!=placements or grid_definition.is_empty():
264
+ grid_definition = GridDefinition.configuration(grid_config,placements)
265
+ grid_definition.source_placements = placements.duplicate(true)
266
+ if grid_definition.has("error"): push_error("Estragon Grid: "+grid_definition.error+"; previous native slots retained")
267
+ if grid_definition.has("error"):
268
+ # Native children may exceed an explicitly bounded grid after direct edits.
269
+ # Reject the layout as a whole, preserving the previous allocated slots.
270
+ return {"error":grid_definition.error,"extent":measured,"slots":slots}
271
+ if not _reported_unanchored:
272
+ for index in range(2):
273
+ for track in grid_definition.columns if index==0 else grid_definition.rows:
274
+ if track.unit=="%" and content[index]<0:
275
+ _reported_unanchored = true
276
+ push_warning("Estragon Grid percentage track has no definite content reference and resolves to zero")
277
+ return GridPolicy.layout(items,grid_definition,content,physical_gaps,alignment,viewport_size)
278
+
279
+ # Allocation-dependent measurement is a bounded second pass in this same core.
280
+ # Only independently assigned axes may supply a basis; auto cycles stay absent.
281
+ func measure_allocated(extent: Vector3, authority: Vector3) -> void:
282
+ if transparent and children.size()==1:
283
+ children[0].measure_allocated(extent,authority)
284
+ _measure_dirty = true
285
+ elif strategy in ["wrap","grid"]:
286
+ var next := measurement_allocation
287
+ for index in range(2):
288
+ if requested[index]<0 and authority[index]>0: next[index] = extent[index]
289
+ if next==measurement_allocation: return
290
+ measurement_allocation = next
291
+ _measure_dirty = true
292
+ else: return
293
+ measure()
294
+
295
+ func _slots(items: Array, content: Vector3, definite: Vector3) -> Array:
296
+ if strategy=="grid": return _grid(items,content).slots
297
+ if strategy=="wrap": return WrapPolicy.layout(items,content,axis,physical_gaps,alignment,align_content,Vector2(definite.x,definite.y)).slots
298
+ return StackPolicy.slots(items,content,axis,physical_gaps[axis] if axis<2 and not length_specs.is_empty() else gap,alignment)
299
+
300
+ func _authority(child, item: Dictionary, basis: Vector3) -> Vector3:
301
+ var authority := Vector3.ZERO
302
+ for index in range(3):
303
+ if basis[index]<0 or child.requested[index]>=0: continue
304
+ if strategy=="grid":
305
+ if index<2 and alignment[index]=="stretch": authority[index] = 1
306
+ elif index==axis:
307
+ if child.grow>0 or child.fill[index]>0: authority[index] = 1
308
+ elif StackPolicy.cross_alignment(item,alignment[index],index)=="stretch": authority[index] = 1
309
+ return authority
310
+
208
311
  func measure() -> Vector3:
209
312
  if transparent and children.size()==1:
210
313
  var child = children[0]
@@ -222,6 +325,17 @@ func measure() -> Vector3:
222
325
  if not _measure_dirty: return measured
223
326
  measure_count += 1
224
327
  if Profile.is_enabled(): Profile.add("layout_measures")
328
+ if not transparent:
329
+ var basis := _content_basis()
330
+ for child in children:
331
+ child.prepare(basis,viewport_size)
332
+ child.measure()
333
+ if basis.x>=0 or basis.y>=0:
334
+ var items := _items()
335
+ var definite := Vector3(1 if basis.x>=0 else 0,1 if basis.y>=0 else 0,1 if basis.z>=0 else 0)
336
+ var allocations := _slots(items,basis,definite)
337
+ if allocations.size()==children.size():
338
+ for index in range(children.size()): children[index].measure_allocated(allocations[index].extent,_authority(children[index],items[index],basis))
225
339
  var natural := Vector3.ZERO
226
340
  for child in children:
227
341
  if not transparent: child.prepare(_content_basis(),viewport_size)
@@ -230,7 +344,12 @@ func measure() -> Vector3:
230
344
  for index in range(3):
231
345
  if index == axis: natural[index] += child_size[index]
232
346
  else: natural[index] = maxf(natural[index],child_size[index])
233
- if children.size() > 1: natural[axis] += gap * (children.size() - 1)
347
+ if children.size() > 1: natural[axis] += (physical_gaps[axis] if axis<2 and length_specs.has("column_gap") else gap) * (children.size() - 1)
348
+ if strategy=="wrap" and not transparent:
349
+ var basis := _content_basis()
350
+ natural = WrapPolicy.layout(_items(),basis,axis,physical_gaps,alignment,align_content,Vector2(1 if basis.x>=0 else 0,1 if basis.y>=0 else 0)).extent
351
+ if strategy=="grid" and not transparent:
352
+ natural = _grid(_items(),_content_basis()).extent
234
353
  for index in range(3): natural[index] = maxf(natural[index],intrinsic[index])
235
354
  if not transparent: natural += leading(padding+border)+trailing(padding+border)
236
355
  measured = natural
@@ -248,6 +367,7 @@ func independent_axis(index: int) -> bool:
248
367
  if parent == null: return false
249
368
  var allocated := grow>0 or fill[index]>0
250
369
  if index!=parent.axis: allocated = StackPolicy.cross_alignment({"align_self":align_self,"stretch":stretch,"fill":fill},parent.alignment[index],index)=="stretch"
370
+ if parent.strategy=="grid" and index<2: allocated = parent.alignment[index]=="stretch"
251
371
  return parent.independent_axis(index) if requested[index] < 0 and allocated else false
252
372
 
253
373
  func arrange(available: Vector3, external_axes: Vector3 = Vector3.ZERO) -> void:
@@ -303,12 +423,17 @@ func arrange(available: Vector3, external_axes: Vector3 = Vector3.ZERO) -> void:
303
423
  _reported_full_percent = true
304
424
  push_warning("Estragon "+main_name+"=100% consumes the full definite parent content axis, not remaining space; siblings may overflow. Use an auto-sized Spacer with grow=1 for remaining space")
305
425
  break
306
- var items: Array = []
307
- for child in children:
308
- items.append({"preferred":child.measured,"minimum":child.minimum(),"requested":child.requested,"grow":child.grow,"stretch":child.stretch,"align_self":child.align_self,"fill":child.fill,"outer":child.outer_extent(),"leading":leading(child.margin)})
426
+ var items := _items()
309
427
  var policy := alignment.duplicate()
310
428
  if center: policy[axis] = "center"
311
- slots = StackPolicy.slots(items,content,axis,gap,policy)
429
+ if strategy=="wrap":
430
+ slots = WrapPolicy.layout(items,content,axis,physical_gaps,policy,align_content,Vector2(1 if reference.x>=0 else 0,1 if reference.y>=0 else 0)).slots
431
+ else:
432
+ slots = StackPolicy.slots(items,content,axis,physical_gaps[axis] if axis<2 and length_specs.has("column_gap") else gap,policy)
433
+ if strategy=="grid":
434
+ policy_result = _grid(items,reference)
435
+ if policy_result.has("error"): return
436
+ slots = policy_result.slots
312
437
  for index in range(children.size()):
313
438
  var child = children[index]
314
439
  var slot: Dictionary = slots[index]
@@ -318,6 +443,7 @@ func arrange(available: Vector3, external_axes: Vector3 = Vector3.ZERO) -> void:
318
443
  child.placed.emit(next)
319
444
  var authority := Vector3.ZERO
320
445
  for cross in range(3):
321
- if cross!=axis and StackPolicy.cross_alignment(items[index],policy[cross],cross)=="stretch" and child.requested[cross]<0 and independent_axis(cross): authority[cross] = 1
446
+ if strategy=="grid" and cross<2 and policy[cross]=="stretch" and child.requested[cross]<0 and independent_axis(cross): authority[cross] = 1
447
+ elif strategy!="grid" and cross!=axis and StackPolicy.cross_alignment(items[index],policy[cross],cross)=="stretch" and child.requested[cross]<0 and independent_axis(cross): authority[cross] = 1
322
448
  child.arrange(slot.extent,authority)
323
449
  arranged.emit(size)
@@ -15,6 +15,7 @@ const EDGES := ["padding","padding_top","padding_right","padding_bottom","paddin
15
15
  var specs: Dictionary = {}
16
16
  var parse_count := 0
17
17
  static func domain(name: String, script = null) -> String:
18
+ if name in ["row_gap","column_gap"]: return "edge"
18
19
  if name=="padding" and VisualStyle.marked(script,"ESTRAGON_BUTTON_STYLE"): return "edge"
19
20
  if name in DIMENSIONS: return "extent"
20
21
  if name in MINIMUMS: return "edge"
@@ -26,6 +27,7 @@ static func domain(name: String, script = null) -> String:
26
27
  static func registered(type, name: String) -> bool:
27
28
  if domain(name).is_empty() or not type is Script: return false
28
29
  if name in MINIMUMS and not VisualStyle.marked(type,"ESTRAGON_MINIMUMS_2D"): return false
30
+ if name in ["row_gap","column_gap"] and not VisualStyle.marked(type,"ESTRAGON_GAPS_2D"): return false
29
31
  if name=="border_width" or name in BORDER_EDGES:
30
32
  if not VisualStyle.supported(type): return false
31
33
  while type != null:
@@ -82,10 +84,14 @@ static func dependencies(specs: Dictionary, stack_axis: int) -> Array:
82
84
  for name in specs:
83
85
  var unit: String = specs[name].unit
84
86
  if unit=="%":
85
- if name in ["width","min_width","x","margin_left","margin_right","padding_left","padding_right","border_left_width","border_right_width"]: parent_axes.x = 1
86
- elif name in ["height","min_height","y","margin_top","margin_bottom","padding_top","padding_bottom","border_top_width","border_bottom_width"]: parent_axes.y = 1
87
+ if name in ["width","min_width","x","column_gap","margin_left","margin_right","padding_left","padding_right","border_left_width","border_right_width"]: parent_axes.x = 1
88
+ elif name in ["height","min_height","y","row_gap","margin_top","margin_bottom","padding_top","padding_bottom","border_top_width","border_bottom_width"]: parent_axes.y = 1
87
89
  elif name in ["depth","z"]: parent_axes.z = 1
88
- elif name=="gap": parent_axes[stack_axis] = 1
90
+ elif name=="gap":
91
+ parent_axes[stack_axis] = 1
92
+ if specs.has("row_gap") or specs.has("column_gap"):
93
+ parent_axes.x = 1
94
+ parent_axes.y = 1
89
95
  else: parent_axes = Vector3(1,1,parent_axes.z)
90
96
  elif unit=="vw": viewport_axes.x = 1
91
97
  elif unit=="vh": viewport_axes.y = 1
@@ -19,7 +19,9 @@ static func preset(script) -> String:
19
19
  script = script.get_base_script()
20
20
  return ""
21
21
  static func error(name: String, value, direction: String, fixed: String = "") -> String:
22
- if name=="align_self":
22
+ if name=="align_content":
23
+ if value not in ["start","center","end","stretch"]+DISTRIBUTION: return "align_content requires start/center/end/space-between/around/evenly/stretch"
24
+ elif name=="align_self":
23
25
  if value not in SELF: return "align_self requires auto, start, center, end or stretch"
24
26
  elif name=="direction":
25
27
  if value not in ["row","column"]: return "direction requires row or column"
@@ -0,0 +1,59 @@
1
+ @tool
2
+ extends RefCounted
3
+ ## Pure line grouping and slot composition over the existing StackPolicy.
4
+ const Stack = preload("stack_policy.gd")
5
+ const CONTENT := ["start","center","end","space-between","space-around","space-evenly","stretch"]
6
+ static func layout(items: Array, content: Vector3, axis: int, gaps: Vector2, alignment: Array, align_content: String, definite: Vector2) -> Dictionary:
7
+ var cross := 1-axis
8
+ var lines: Array = []
9
+ var current: Array = []
10
+ var used := 0.0
11
+ for index in range(items.size()):
12
+ var item: Dictionary = items[index]
13
+ var main: float = maxf(item.preferred[axis],item.minimum[axis])+item.outer[axis]
14
+ if definite[axis]>0 and not current.is_empty() and used+gaps[axis]+main>content[axis]:
15
+ lines.append(current)
16
+ current = []
17
+ used = 0
18
+ if not current.is_empty(): used += gaps[axis]
19
+ current.append(index)
20
+ used += main
21
+ if not current.is_empty(): lines.append(current)
22
+ var heights: Array = []
23
+ var natural := Vector3.ZERO
24
+ for line in lines:
25
+ var width := gaps[axis]*maxi(0,line.size()-1)
26
+ var height := 0.0
27
+ for index in line:
28
+ var item: Dictionary = items[index]
29
+ width += maxf(item.preferred[axis],item.minimum[axis])+item.outer[axis]
30
+ height = maxf(height,maxf(item.preferred[cross],item.minimum[cross])+item.outer[cross])
31
+ natural[axis] = maxf(natural[axis],width)
32
+ natural[cross] += height
33
+ heights.append(height)
34
+ natural[cross] += gaps[cross]*maxi(0,lines.size()-1)
35
+ var free: float = maxf(0,content[cross]-natural[cross]) if definite[cross]>0 else 0.0
36
+ var offset := free*Stack.factor(align_content)
37
+ var added := 0.0
38
+ if align_content=="space-between" and lines.size()>1: added = free/(lines.size()-1)
39
+ elif align_content=="space-around" and not lines.is_empty():
40
+ added = free/lines.size()
41
+ offset = added/2
42
+ elif align_content=="space-evenly" and not lines.is_empty():
43
+ added = free/(lines.size()+1)
44
+ offset = added
45
+ var slots: Array = []
46
+ slots.resize(items.size())
47
+ for line_index in range(lines.size()):
48
+ var line: Array = lines[line_index]
49
+ var line_items: Array = []
50
+ for index in line: line_items.append(items[index])
51
+ var extent := content
52
+ extent[axis] = content[axis] if definite[axis]>0 else natural[axis]
53
+ extent[cross] = heights[line_index]+(free/lines.size() if align_content=="stretch" else 0.0)
54
+ var placed := Stack.slots(line_items,extent,axis,gaps[axis],alignment)
55
+ for i in range(line.size()):
56
+ placed[i].position[cross] += offset
57
+ slots[line[i]] = placed[i]
58
+ offset += extent[cross]+gaps[cross]+added
59
+ return {"extent":natural,"slots":slots,"lines":lines}
@@ -0,0 +1 @@
1
+ uid://hoprjw6ig7f
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.23"
32
+ "version": "0.1.0-beta.24"
33
33
  }