-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathActorNode.cs
380 lines (342 loc) · 6.48 KB
/
ActorNode.cs
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using System;
using System.IO;
using System.Collections.Generic;
using Nima.Math2D;
namespace Nima
{
public class ActorNode : ActorComponent
{
protected List<ActorNode> m_Children;
//protected List<ActorNode> m_Dependents;
protected Mat2D m_Transform = new Mat2D();
protected Mat2D m_WorldTransform = new Mat2D();
protected Vec2D m_Translation = new Vec2D();
protected float m_Rotation = 0.0f;
protected Vec2D m_Scale = new Vec2D(1.0f, 1.0f);
protected float m_Opacity = 1.0f;
protected float m_RenderOpacity = 1.0f;
private bool m_OverrideWorldTransform = false;
protected bool m_IsCollapsedVisibility = false;
private bool m_RenderCollapsed = false;
private IList<ActorConstraint> m_Constraints;
const byte TransformDirty = 1<<0;
const byte WorldTransformDirty = 1<<1;
public ActorNode()
{
}
public ActorNode(Actor actor) : base(actor)
{
}
public Mat2D Transform
{
get
{
return m_Transform;
}
}
public Mat2D WorldTransformOverride
{
get
{
return m_OverrideWorldTransform ? m_WorldTransform : null;
}
set
{
if(value == null)
{
m_OverrideWorldTransform = false;
}
else
{
m_OverrideWorldTransform = true;
Mat2D.Copy(m_WorldTransform, value);
}
MarkTransformDirty();
}
}
public Mat2D WorldTransform
{
get
{
return m_WorldTransform;
}
// N.B. this should only be done if you really know what you're doing. Generally you want to manipulate the local translation, rotation, and scale of a Node.
set
{
Mat2D.Copy(m_WorldTransform, value);
}
}
public float X
{
get
{
return m_Translation[0];
}
set
{
if(m_Translation[0] == value)
{
return;
}
m_Translation[0] = value;
MarkTransformDirty();
}
}
public float Y
{
get
{
return m_Translation[1];
}
set
{
if(m_Translation[1] == value)
{
return;
}
m_Translation[1] = value;
MarkTransformDirty();
}
}
public Vec2D Translation
{
get
{
return new Vec2D(m_Translation);
}
set
{
Vec2D.Copy(m_Translation, value);
MarkTransformDirty();
}
}
public float Rotation
{
get
{
return m_Rotation;
}
set
{
if(m_Rotation == value)
{
return;
}
m_Rotation = value;
MarkTransformDirty();
}
}
public float ScaleX
{
get
{
return m_Scale[0];
}
set
{
if(m_Scale[0] == value)
{
return;
}
m_Scale[0] = value;
MarkTransformDirty();
}
}
public float ScaleY
{
get
{
return m_Scale[1];
}
set
{
if(m_Scale[1] == value)
{
return;
}
m_Scale[1] = value;
MarkTransformDirty();
}
}
public float Opacity
{
get
{
return m_Opacity;
}
set
{
if(m_Opacity == value)
{
return;
}
m_Opacity = value;
MarkTransformDirty();
}
}
public float RenderOpacity
{
get
{
return m_RenderOpacity;
}
}
public bool RenderCollapsed
{
get
{
return m_RenderCollapsed;
}
}
public bool CollapsedVisibility
{
get
{
return m_IsCollapsedVisibility;
}
set
{
if(m_IsCollapsedVisibility != value)
{
m_IsCollapsedVisibility = value;
MarkTransformDirty();
}
}
}
public void MarkTransformDirty()
{
if(m_Actor == null)
{
// Still loading?
return;
}
if(!m_Actor.AddDirt(this, TransformDirty))
{
return;
}
m_Actor.AddDirt(this, WorldTransformDirty, true);
}
private void UpdateTransform()
{
Mat2D.FromRotation(m_Transform, m_Rotation);
m_Transform[4] = m_Translation[0];
m_Transform[5] = m_Translation[1];
Mat2D.Scale(m_Transform, m_Transform, m_Scale);
}
public Vec2D GetWorldTranslation(Vec2D vec)
{
vec[0] = m_WorldTransform[4];
vec[1] = m_WorldTransform[5];
return vec;
}
private void UpdateWorldTransform()
{
m_RenderOpacity = m_Opacity;
if(m_Parent != null)
{
m_RenderCollapsed = m_IsCollapsedVisibility || m_Parent.m_RenderCollapsed;
m_RenderOpacity *= m_Parent.m_RenderOpacity;
if(!m_OverrideWorldTransform)
{
Mat2D.Multiply(m_WorldTransform, m_Parent.m_WorldTransform, m_Transform);
}
}
else
{
Mat2D.Copy(m_WorldTransform, m_Transform);
}
}
public static ActorNode Read(Actor actor, BinaryReader reader, ActorNode node = null)
{
if(node == null)
{
node = new ActorNode();
}
ActorComponent.Read(actor, reader, node);
Actor.ReadFloat32Array(reader, node.m_Translation.Values);
node.m_Rotation = reader.ReadSingle();
Actor.ReadFloat32Array(reader, node.m_Scale.Values);
node.m_Opacity = reader.ReadSingle();
if(actor.Version >= 13)
{
node.m_IsCollapsedVisibility = reader.ReadByte() == 1;
}
return node;
}
public void AddChild(ActorNode node)
{
if(node.m_Parent != null)
{
node.m_Parent.m_Children.Remove(node);
}
node.m_Parent = this;
if(m_Children == null)
{
m_Children = new List<ActorNode>();
}
m_Children.Add(node);
}
public IList<ActorNode> Children
{
get
{
return m_Children;
}
}
public override ActorComponent MakeInstance(Actor resetActor)
{
ActorNode instanceNode = new ActorNode();
instanceNode.Copy(this, resetActor);
return instanceNode;
}
public void Copy(ActorNode node, Actor resetActor)
{
base.Copy(node, resetActor);
m_Transform = new Mat2D(node.m_Transform);
m_WorldTransform = new Mat2D(node.m_WorldTransform);
m_Translation = new Vec2D(node.m_Translation);
m_Scale = new Vec2D(node.m_Scale);
m_Rotation = node.m_Rotation;
m_Opacity = node.m_Opacity;
m_RenderOpacity = node.m_RenderOpacity;
m_OverrideWorldTransform = node.m_OverrideWorldTransform;
}
public override void OnDirty(byte dirt)
{
}
public bool AddConstraint(ActorConstraint constraint)
{
if(m_Constraints == null)
{
m_Constraints = new List<ActorConstraint>();
}
if(m_Constraints.Contains(constraint))
{
return false;
}
m_Constraints.Add(constraint);
return true;
}
public override void Update(byte dirt)
{
if((dirt & TransformDirty) == TransformDirty)
{
UpdateTransform();
}
if((dirt & WorldTransformDirty) == WorldTransformDirty)
{
UpdateWorldTransform();
if(m_Constraints != null)
{
foreach(ActorConstraint constraint in m_Constraints)
{
if(constraint.IsEnabled)
{
constraint.Constrain(this);
}
}
}
}
}
}
}