-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetPropertyCommand.cs
163 lines (138 loc) · 5.44 KB
/
SetPropertyCommand.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
//
// Cuboid (http://github.com/arjonagelhout/cuboid)
// Copyright (c) 2023 Arjo Nagelhout
//
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Cuboid
{
/// <summary>
/// Performs custom processing per value (x, y, z) in the command to determine
/// the new values and whether anything has changed
/// </summary>
public class SetPropertyCommandVector3 : SetPropertyCommand<Vector3>
{
public SetPropertyCommandVector3(List<IBinding<Vector3>> bindings, Vector3 previousValue, Vector3 newValue, Vector3[] previousValues = null) : base(bindings, previousValue, newValue, previousValues)
{
}
protected override void SetChangesAndNewValues(Vector3 previousValue, Vector3 newValue)
{
// get which one has been changed
bool changedX = previousValue.x != newValue.x;
bool changedY = previousValue.y != newValue.y;
bool changedZ = previousValue.z != newValue.z;
for (int i = 0; i < _targetBindings.Count; i++)
{
IBinding<Vector3> targetBinding = _targetBindings[i];
Vector3 newTargetValue = targetBinding.Value;
if (changedX) { newTargetValue.SetXMutating(newValue.x); }
if (changedY) { newTargetValue.SetYMutating(newValue.y); }
if (changedZ) { newTargetValue.SetZMutating(newValue.z); }
if (_previousValues[i] != newTargetValue)
{
_changes = true;
}
_newValues[i] = newTargetValue;
}
}
}
/// <summary>
/// Performs custom processing per value (x, y, z) in the command to determine
/// the new values and whether anything has changed
/// </summary>
public class SetPropertyCommandVector2 : SetPropertyCommand<Vector2>
{
public SetPropertyCommandVector2(List<IBinding<Vector2>> bindings, Vector2 previousValue, Vector2 newValue, Vector2[] previousValues = null) : base(bindings, previousValue, newValue, previousValues)
{
}
protected override void SetChangesAndNewValues(Vector2 previousValue, Vector2 newValue)
{
// get which one has been changed
bool changedX = previousValue.x != newValue.x;
bool changedY = previousValue.y != newValue.y;
for (int i = 0; i < _targetBindings.Count; i++)
{
IBinding<Vector2> targetBinding = _targetBindings[i];
Vector2 newTargetValue = targetBinding.Value;
if (changedX) { newTargetValue.SetXMutating(newValue.x); }
if (changedY) { newTargetValue.SetYMutating(newValue.y); }
if (_previousValues[i] != newTargetValue)
{
_changes = true;
}
_newValues[i] = newTargetValue;
}
}
}
/// <summary>
/// To be used via the Properties view to edit properties of a RealityObject
/// such as the Corner Radius, Quality or Color.
/// </summary>
public class SetPropertyCommand<T> : Command
{
protected List<IBinding<T>> _targetBindings;
protected T[] _previousValues;
protected T[] _newValues;
protected bool _changes = false;
/// <summary>
/// Override this method for custom changes and new values calculation
/// behaviour (e.g. for Vector2 and Vector3)
/// </summary>
/// <param name="previousValue"></param>
/// <param name="newValue"></param>
protected virtual void SetChangesAndNewValues(T previousValue, T newValue)
{
if (!previousValue.Equals(newValue))
{
_changes = true;
}
// just set the new values to the new value
for (int i = 0; i < _targetBindings.Count; i++)
{
_newValues[i] = newValue;
}
}
public SetPropertyCommand(List<IBinding<T>> bindings, T previousValue, T newValue, T[] previousValues = null)
{
_targetBindings = bindings;
_newValues = new T[_targetBindings.Count];
if (previousValues == null)
{
_previousValues = new T[_targetBindings.Count];
for (int i = 0; i < _targetBindings.Count; i++)
{
IBinding<T> binding = _targetBindings[i];
_previousValues[i] = binding.Value;
}
}
else
{
_previousValues = previousValues;
}
SetChangesAndNewValues(previousValue, newValue);
}
protected override void OnDo(out bool changes, out bool needsSaving)
{
needsSaving = _changes;
changes = _changes;
if (!changes) { return; }
// now set the new values
for (int i = 0; i < _targetBindings.Count; i++)
{
IBinding<T> targetBinding = _targetBindings[i];
targetBinding.Value = _newValues[i];
}
}
protected override void OnUndo()
{
// set back to the old values
for (int i = 0; i < _targetBindings.Count; i++)
{
IBinding<T> targetBinding = _targetBindings[i];
targetBinding.Value = _previousValues[i];
}
}
}
}