Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Commit b79d63c

Browse files
Optimizing undos
1 parent e2ebc0c commit b79d63c

File tree

5 files changed

+87
-8
lines changed

5 files changed

+87
-8
lines changed

Assets/Scripts/ARStage.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,14 @@ private void UpdatePlacementPose()
121121
}
122122
public void Next()
123123
{
124-
index++;
125-
if (index >= stageData.size)
124+
Debug.Log(index);
125+
if (index > stageData.states.Count-1)
126126
{
127-
index = stageData.size - 1;
127+
index = stageData.states.Count - 1;
128128
return;
129129
}
130+
if(index < stageData.states.Count)
131+
index++;
130132
State args = stageData.states[index];
131133
Debug.Log(args.state_type);
132134
if (args.state_type == "comment")
@@ -175,12 +177,21 @@ public void Next()
175177
}
176178
public void Undo()
177179
{
180+
Debug.Log(index);
178181
if (index <= -1)
182+
{
183+
index = 0;
179184
return;
185+
}
186+
187+
if (index >= stageData.states.Count - 1)
188+
index = stageData.states.Count - 2;
189+
180190
State args = stageData.states[index];
181191
if (args.state_type == "comment")
182192
{
183-
index--;
193+
if(index >= 0)
194+
index--;
184195
return;
185196
}
186197

@@ -195,7 +206,8 @@ public void Undo()
195206

196207
BaseStructure currStructure = stageData.objectMap[id];
197208
currStructure.Undo(args);
198-
index--;
209+
if(index>=0)
210+
index--;
199211
}
200212
public void BackButton()
201213
{

Assets/Scripts/ARgorithm/Animations/QueueAnimator.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ public void Pop()
156156
arrow.position += new Vector3(arrow.scale.x * 1.25f, 0, 0);
157157
}
158158
var arrowFirst = queueOfArrows.First.Value;
159+
arrowFirst.arrow.transform.SetParent(null);
159160
Destroy(arrowFirst.arrow);
160161
queueOfArrows.RemoveFirst();
161162
}
@@ -220,5 +221,35 @@ IEnumerator LerpFunctionHighlight(Material materialToChange, Color endValue, flo
220221
}
221222
materialToChange.color = startValue;
222223
}
224+
225+
public void PopLast()
226+
{
227+
if (this.queueOfArrows.Count == 0)
228+
return;
229+
var arrowLast = queueOfArrows.Last.Value;
230+
arrowLast.arrow.transform.SetParent(null);
231+
Destroy(arrowLast.arrow);
232+
queueOfArrows.RemoveLast();
233+
}
234+
235+
public void PushFirst(ContentType element)
236+
{
237+
var arrow = new VariableArrow(element);
238+
if (queueOfArrows.Count == 0)
239+
{
240+
arrow.position = this.placeholder.transform.position;
241+
arrow.arrow.transform.SetParent(placeholder.transform);
242+
queueOfArrows.AddFirst(arrow);
243+
return;
244+
}
245+
foreach (var ar in queueOfArrows)
246+
{
247+
ar.position -= new Vector3(arrow.scale.x * 1.25f, 0, 0);
248+
}
249+
arrow.position = placeholder.transform.position;
250+
arrow.arrow.transform.SetParent(placeholder.transform);
251+
queueOfArrows.AddFirst(arrow);
252+
253+
}
223254
}
224255
}

Assets/Scripts/ARgorithm/Animations/StackAnimator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public VariableTile(ContentType value)
6767
{
6868
this.tile = (GameObject)Instantiate(Resources.Load("Tile") as GameObject);
6969
var cubeRenderer = this.tile.GetComponent<Renderer>();
70-
cubeRenderer.material.SetColor("_Color", Color.blue);
70+
cubeRenderer.material.SetColor("_Color", Color.cyan);
7171
this._scale = this.tile.transform.localScale;
7272
this.faceValue = value;
7373
}

Assets/Scripts/ARgorithm/Structure/QueueStructure.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,27 @@ private void Back(State state)
8282

8383
public override void Undo(State state)
8484
{
85-
base.Undo(state);
85+
// Called to undo a change enforced by `state`
86+
string funcType = state.state_type.Split('_').ToList()[1];
87+
switch (funcType)
88+
{
89+
case "declare":
90+
break;
91+
case "push":
92+
animator.PopLast();
93+
break;
94+
case "pop":
95+
ContentType element = new ContentType((JToken)state.state_def["element"]);
96+
animator.PushFirst(element);
97+
break;
98+
case "front":
99+
break;
100+
case "back":
101+
102+
break;
103+
default:
104+
break;
105+
}
86106
}
87107
}
88108
}

Assets/Scripts/ARgorithm/Structure/StackStructure.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,23 @@ private void Top(State state)
7272
}
7373
public override void Undo(State state)
7474
{
75-
base.Undo(state);
75+
string funcType = state.state_type.Split('_').ToList()[1];
76+
switch (funcType)
77+
{
78+
case "declare":
79+
break;
80+
case "push":
81+
animator.Pop();
82+
break;
83+
case "pop":
84+
var element = new ContentType((JToken)state.state_def["element"]);
85+
animator.Push(element);
86+
break;
87+
case "top":
88+
break;
89+
default:
90+
break;
91+
}
7692
}
7793
}
7894
}

0 commit comments

Comments
 (0)