-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_multi.go
More file actions
200 lines (161 loc) · 4.95 KB
/
node_multi.go
File metadata and controls
200 lines (161 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package scaff
import "github.com/hajimehoshi/ebiten/v2"
// Struct for defining a new multi child node.
//
// ID should be a unique id for the node, but also probably be readable as it shows up in error messages.
//
// DefaultProps are the props as they are by default. Make sure to also specify any embedded struct pointers.
//
// PropsCreator should be the function passed in by users of your node (as in it should probably be an argument of the function creating your node).
//
// Create is the function actually specifying your node. You can overwrite all of the functions of the node interface there, with some exceptions that we implement for you.
type MultiNodeCreate[P ChildProps[NodeBuilder]] struct {
ID string
DefaultProps P
PropsCreator func(t *Tracker, props *P)
Create func(props *MultiChildProps[P])
}
// MultiNode lets you create a node with multiple children. Simply implement the ChildProps interface on the props you want to have for your node.
func MultiNode[P ChildProps[NodeBuilder]](create MultiNodeCreate[P]) NodeBuilder {
node := &MultiChildNode[P]{
id: create.ID,
multiProps: &MultiChildProps[P]{},
}
if create.Create != nil {
create.Create(node.multiProps)
}
return func(context *BuildContext) Node {
node.tracker = NewTracker()
node.context = context
// Fill the props
props := create.DefaultProps
if create.PropsCreator != nil {
create.PropsCreator(node.Tracker(), &props)
node.builders = props.GetBuilders()
}
node.props = props
// Call props change hook (in case registered)
if node.multiProps.OnPropsChanged != nil {
node.multiProps.OnPropsChanged(node)
}
return node
}
}
type MultiChildProps[P any] struct {
OnLoad func(node *MultiChildNode[P], parent Node)
OnUnload func(node *MultiChildNode[P])
OnPropsChanged func(node *MultiChildNode[P])
OnUpdate func(node *MultiChildNode[P], c *Context) error
OnHandleEvent func(node *MultiChildNode[P], c *Context, event Event) error
OnDraw func(node *MultiChildNode[P], c *Context, image *ebiten.Image)
}
// Just for making sure we implement the Node interface
var _ Node = &MultiChildNode[any]{}
type MultiChildNode[P any] struct {
parent Node
children []Node
builders []NodeBuilder
tracker *Tracker
context *BuildContext
id string
props P
multiProps *MultiChildProps[P]
}
func (s *MultiChildNode[P]) ID() string {
return s.id
}
func (s *MultiChildNode[P]) Props() P {
return s.props
}
func (s *MultiChildNode[P]) Load(parent Node) {
s.parent = parent
// Actually load the children and build them
if s.builders != nil {
s.children = make([]Node, len(s.builders))
for i, builder := range s.builders {
s.children[i] = builder(s.context)
s.children[i].Load(s)
}
}
if s.multiProps.OnLoad != nil {
s.multiProps.OnLoad(s, parent)
}
}
func (s *MultiChildNode[P]) PropsChanged() {
}
func (s *MultiChildNode[P]) HandleEvent(c *Context, event Event) TracedError {
// First handle event on this node
if s.multiProps.OnHandleEvent != nil {
if err := s.multiProps.OnHandleEvent(s, c, event); err != nil {
return NewTracedError(s, err)
}
}
// Then pass event to children
return s.HandleEventChild(c, event)
}
func (s *MultiChildNode[P]) Tracker() *Tracker {
return s.tracker
}
func (s *MultiChildNode[P]) Update(c *Context) TracedError {
// First call the update handler on the props for this node
if s.multiProps.OnUpdate != nil {
if err := s.multiProps.OnUpdate(s, c); err != nil {
return NewTracedError(s, err)
}
}
// Forward the update to the children
for _, child := range s.children {
if err := child.Update(c); err != nil {
return err
}
}
// If any of the children are dirty, rebuild them
for i, child := range s.children {
if child.Tracker().Changed() {
child.Unload()
s.children[i] = s.builders[i](s.context)
s.children[i].Load(s)
}
}
return nil
}
func (s *MultiChildNode[P]) Unload() {
if s.multiProps.OnUnload != nil {
s.multiProps.OnUnload(s)
}
// Unload the children properly
for _, child := range s.children {
child.Unload()
}
s.tracker.Clear()
s.tracker = nil // Cut tracker off from tree for GC
}
func (s *MultiChildNode[P]) Draw(c *Context, image *ebiten.Image) {
if s.multiProps.OnDraw != nil {
s.multiProps.OnDraw(s, c, image)
} else {
// Default implementation: just draw children
s.DrawChild(c, image)
}
}
func (s *MultiChildNode[P]) Parent() Node {
return s.parent
}
func (s *MultiChildNode[P]) Children() []Node {
return s.children
}
func (s *MultiChildNode[P]) HandleEventChild(c *Context, event Event) TracedError {
// Event should always be passed to the children as well so it doesn't get missed (even if already handled)
for _, child := range s.children {
if err := child.HandleEvent(c, event); err != nil {
return err
}
}
return nil
}
// Draw the children of the node
func (s *MultiChildNode[P]) DrawChild(c *Context, image *ebiten.Image) {
for _, child := range s.children {
child.Draw(c, image)
}
}