forked from ZelimDamian/frame3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransformManager.cs
443 lines (349 loc) · 14.8 KB
/
TransformManager.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace f3
{
public interface ITransformGizmo : SceneUIElement
{
List<SceneObject> Targets { get; }
bool SupportsFrameMode { get; }
FrameType CurrentFrameMode { get; set; }
bool SupportsReferenceObject { get; }
void SetReferenceObject(SceneObject so);
}
public interface ITransformGizmoBuilder
{
bool SupportsMultipleObjects { get; }
ITransformGizmo Build(FScene scene, List<SceneObject> targets);
}
/// <summary>
/// TransformManager handles Gizmos, like 3-axis transform gizmo, resizing handles, etc.
/// Basically these are 3D widgets that appear on selection, rather than
/// on starting a tool. For each Gizmo, a Builder is associated with a string, and then
/// the current default Gizmo can be selected via string.
///
/// When you would like to modify or disable the default Gizmos, eg inside a Tool, you can
/// use PushOverrideGizmoType/Pop. TransformManager.NoGizmoType is a built-in gizmo type
/// that does nothing, for this purpose.
///
/// To customize which gizmo appears for specific objects, pass GizmoTypeFilter
/// objects to AddTypeFilter(). Note that the current Override gizmo type still takes
/// precedence over the filtered gizmo type.
///
/// [TODO] currently type filters are only considered if there is a single selected SO
///
/// To just limit which SO's can be considered for a gizmo, use SetSelectionFilter()
///
/// </summary>
public class TransformManager
{
public FContext Context { get; set; }
ITransformGizmoBuilder activeBuilder;
ITransformGizmo activeGizmo;
Dictionary<string, ITransformGizmoBuilder> GizmoTypes;
string sActiveGizmoType;
string sOverrideGizmoType;
List<string> OverrideGizmoStack = new List<string>();
/// <summary>
/// Used to replace default gizmo tpe - see AddTypeFilter()
/// </summary>
public class GizmoTypeFilter
{
/// <summary> return Gizmo type name string, or null </summary>
public Func<SceneObject, string> FilterF;
}
List<GizmoTypeFilter> ObjectFilters = new List<GizmoTypeFilter>();
FrameType defaultFrameType;
Dictionary<SceneObject, FrameType> lastFrameTypeCache;
public const string NoGizmoType = "no_gizmo";
public const string DefaultGizmoType = "default";
Func<SceneObject, bool> SelectionFilterF = null;
public TransformManager( ITransformGizmoBuilder defaultBuilder )
{
activeBuilder = defaultBuilder;
activeGizmo = null;
GizmoTypes = new Dictionary<string, ITransformGizmoBuilder>();
RegisterGizmoType(NoGizmoType, new NoGizmoBuilder());
RegisterGizmoType(DefaultGizmoType, activeBuilder);
sActiveGizmoType = DefaultGizmoType;
defaultFrameType = FrameType.LocalFrame;
lastFrameTypeCache = new Dictionary<SceneObject, FrameType>();
ObjectFilters = new List<GizmoTypeFilter>();
sOverrideGizmoType = "";
}
public void Initialize(FContext manager)
{
Context = manager;
Context.Scene.SelectionChangedEvent += Scene_SelectionChangedEvent;
}
/// <summary>
/// Associate a new gizmo builder with an identifier
/// </summary>
public void RegisterGizmoType(string sType, ITransformGizmoBuilder builder)
{
if (GizmoTypes.ContainsKey(sType))
throw new ArgumentException("TransformManager.RegisterGizmoType : type " + sType + " already registered!");
GizmoTypes[sType] = builder;
}
/// <summary>
/// Current active default gizmo type/builder
/// </summary>
public string ActiveGizmoType {
get { return sActiveGizmoType; }
}
/// <summary>
/// Select the current default gizmo type, using identifier passed to RegisterGizmoType()
/// </summary>
public void SetActiveGizmoType(string sType)
{
if (sActiveGizmoType == sType)
return;
if (GizmoTypes.ContainsKey(sType) == false)
throw new ArgumentException("TransformManager.SetActiveGizmoType : type " + sType + " is not registered!");
activeBuilder = GizmoTypes[sType];
sActiveGizmoType = sType;
update_gizmo();
}
/// <summary>
/// Temporarily override the current active gizmo type
/// </summary>
public void PushOverrideGizmoType(string sType)
{
if (GizmoTypes.ContainsKey(sType) == false)
throw new ArgumentException("TransformManager.SetOverrideGizmoType : type " + sType + " is not registered!");
if (OverrideGizmoStack.Count > 10)
throw new Exception("TransformManager.PushOverrideGizmoType: stack is too large, probably a missing pop?");
OverrideGizmoStack.Add(this.sOverrideGizmoType);
this.sOverrideGizmoType = sType;
//update_gizmo();
Context.RegisterNextFrameAction(update_gizmo);
}
/// <summary>
/// Pop the override gizmo type stack
/// </summary>
public void PopOverrideGizmoType()
{
if (OverrideGizmoStack.Count == 0)
throw new Exception("TransformManager.PopOverrideGizmoType: tried to pop empty stack!");
this.sOverrideGizmoType = OverrideGizmoStack[OverrideGizmoStack.Count - 1];
OverrideGizmoStack.RemoveAt(OverrideGizmoStack.Count - 1);
// [RMS] defer this update to next frame, as we often do this inside a Tool
// and we should not immediately initialize gizmo...
//update_gizmo();
Context.RegisterNextFrameAction(update_gizmo);
}
/// <summary>
/// Pop all pushed override gizmo types
/// </summary>
public void PopAllOverrideGizmos()
{
while (OverrideGizmoStack.Count > 0) {
sOverrideGizmoType = OverrideGizmoStack[OverrideGizmoStack.Count - 1];
OverrideGizmoStack.RemoveAt(OverrideGizmoStack.Count - 1);
}
Context.RegisterNextFrameAction(update_gizmo);
}
/// <summary>
/// When the selection filter is set, only objects where filterF(so) == true
/// will be given the current gizmo.
/// </summary>
public void SetSelectionFilter(Func<SceneObject, bool> filterF)
{
SelectionFilterF = filterF;
}
/// <summary>
/// Discard current selection filter
/// </summary>
public void ClearSelectionFilter()
{
SelectionFilterF = null;
}
/// <summary>
/// TypeFilters will checked each time the selection changes. If the filter returns
/// a gizmo type identifier, we'll use that instead of the current default.
/// However override gizmos will still take precedence.
/// </summary>
public void AddTypeFilter(GizmoTypeFilter filter)
{
ObjectFilters.Add(filter);
}
/// <summary>
/// remove a previously-registered gizmo type filter
/// </summary>
public void RemoveTypeFilter(GizmoTypeFilter filter)
{
ObjectFilters.Remove(filter);
}
/// <summary>
/// remove all registered gizmo type filters
/// </summary>
public void ClearAllTypeFilters()
{
ObjectFilters.Clear();
}
void update_gizmo()
{
DismissActiveGizmo();
Scene_SelectionChangedEvent(null, null);
}
public bool HaveActiveGizmo
{
get { return activeGizmo != null; }
}
public ITransformGizmo ActiveGizmo
{
get { return activeGizmo; }
}
public event EventHandler OnActiveGizmoModified;
protected virtual void SendOnActiveGizmoModified() {
var tmp = OnActiveGizmoModified;
if (tmp != null)
tmp(this, new EventArgs());
}
public FrameType ActiveFrameType
{
get {
return (activeGizmo != null && activeGizmo.SupportsFrameMode) ?
activeGizmo.CurrentFrameMode : defaultFrameType;
}
set {
if (activeGizmo != null && activeGizmo.SupportsFrameMode) {
activeGizmo.CurrentFrameMode = value;
if ( activeGizmo.Targets.Count == 1 )
lastFrameTypeCache[activeGizmo.Targets[0]] = value;
//defaultFrameType = value; // always change default when we explicitly change type
SendOnActiveGizmoModified();
} else {
defaultFrameType = value;
SendOnActiveGizmoModified(); // not really right...but UI using it now
}
}
}
public void SetActiveReferenceObject(SceneObject so)
{
if (activeGizmo != null && activeGizmo.SupportsReferenceObject)
activeGizmo.SetReferenceObject(so);
}
public void DismissActiveGizmo()
{
FScene scene = Context.Scene;
if ( activeGizmo != null ) {
scene.RemoveUIElement(activeGizmo, true);
activeGizmo = null;
SendOnActiveGizmoModified();
}
}
//
FrameType initial_frame_type(SceneObject so)
{
return FrameType.LocalFrame;
}
protected void AddGizmo( List<SceneObject> targets )
{
// current default active gizmo builder
ITransformGizmoBuilder useBuilder = activeBuilder;
// apply gizmo type filters
// [TODO] support multiple here?
if (targets.Count == 1 && ObjectFilters.Count > 0) {
foreach (GizmoTypeFilter filter in ObjectFilters) {
string typename = filter.FilterF(targets[0]);
if (typename != null && GizmoTypes.ContainsKey(typename) == true)
useBuilder = GizmoTypes[typename];
}
}
// apply overrides
if (sOverrideGizmoType != null && sOverrideGizmoType != "")
useBuilder = GizmoTypes[sOverrideGizmoType];
// filter target count if builder only supports single object
List<SceneObject> useTargets = new List<SceneObject>(targets);
if (useTargets.Count > 0 && useBuilder.SupportsMultipleObjects == false)
useTargets.RemoveRange(1, useTargets.Count - 1);
// remove existing active gizmo
// [TODO] support multiple gizmos?
if (activeGizmo != null) {
if ( unordered_lists_equal(activeGizmo.Targets, useTargets) )
return; // same targets
DismissActiveGizmo();
}
if (targets != null) {
activeGizmo = useBuilder.Build(Context.Scene, useTargets);
if (activeGizmo == null)
return;
// set frame type. behavior here is a bit tricky...we have a default frame type
// and then a cached type for each object. However if we only cache type on explicit
// user changes, then if user changes default, all other gizmos inherit this default.
// This is currently a problem because we are also using default frame type to
// control things like snapping behavior (local=translate+rotate, world=translate-only).
// So then if we change that, we can change default, which then changes object gizmo
// behavior in unexpected ways. So right now we are initializing cache with a per-type
// default (always Local right now), which user can then change. This "feels" right-est...
if (activeGizmo.SupportsFrameMode) {
if (targets.Count == 1) {
if (lastFrameTypeCache.ContainsKey(useTargets[0]) == false)
lastFrameTypeCache[useTargets[0]] = initial_frame_type(useTargets[0]);
activeGizmo.CurrentFrameMode = lastFrameTypeCache[useTargets[0]];
} else
activeGizmo.CurrentFrameMode = defaultFrameType;
}
Context.Scene.AddUIElement(activeGizmo);
SendOnActiveGizmoModified();
}
}
public static bool unordered_lists_equal<T>(List<T> l1, List<T> l2)
{
if (l1.Count != l2.Count)
return false;
foreach(T o in l1) {
if (!l2.Contains(o))
return false;
}
return true;
}
private void Scene_SelectionChangedEvent(object sender, EventArgs e)
{
FScene scene = Context.Scene;
List<SceneObject> vSelected = new List<SceneObject>();
foreach ( SceneObject tso in scene.Selected ) {
if (tso != null) {
if ( SelectionFilterF == null || SelectionFilterF(tso) )
vSelected.Add(tso);
}
}
if (vSelected.Count == 0 && activeGizmo != null) {
// object de-selected, dismiss gizmo
DismissActiveGizmo();
return;
}
if (activeGizmo != null && unordered_lists_equal(vSelected, activeGizmo.Targets) == false) {
DismissActiveGizmo();
}
if (vSelected.Count > 0)
Context.RegisterNextFrameAction(add_gizmo_next_frame);
//AddGizmo(vSelected);
}
private void add_gizmo_next_frame()
{
FScene scene = Context.Scene;
List<SceneObject> vSelected = new List<SceneObject>();
foreach (SceneObject tso in scene.Selected) {
if (tso != null) {
if (SelectionFilterF == null || SelectionFilterF(tso))
vSelected.Add(tso);
}
}
if (vSelected.Count > 0)
AddGizmo(vSelected);
}
}
public class NoGizmoBuilder : ITransformGizmoBuilder
{
public bool SupportsMultipleObjects {
get { return true; }
}
public ITransformGizmo Build(FScene scene, List<SceneObject> targets)
{
return null;
}
}
}