Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.

Commit 7f8699a

Browse files
author
Levin G
committed
Merge pull request #52 from groud/use-iterators
Replaced for-loops with foreach to improve code readability
2 parents fc180bc + 028babc commit 7f8699a

File tree

4 files changed

+97
-106
lines changed

4 files changed

+97
-106
lines changed

Node_Editor/Framework/Canvas Save Objects/Transition.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public void Delete ()
8787
/// </summary>
8888
public bool conditionsMet ()
8989
{
90-
for (int condCnt = 0; condCnt < conditions.Count; condCnt++)
90+
foreach (TransitionCondition condition in conditions)
9191
{
92-
if (!conditions[condCnt].Invoke (this))
92+
if (!condition.Invoke (this))
9393
return false;
9494
}
9595
return true;

Node_Editor/Framework/Node.cs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,28 +54,26 @@ public void Delete ()
5454
throw new UnityException ("The Node " + name + " does not exist on the Canvas " + NodeEditor.curNodeCanvas.name + "!");
5555
NodeEditorCallbacks.IssueOnDeleteNode (this);
5656
NodeEditor.curNodeCanvas.nodes.Remove (this);
57-
for (int outCnt = 0; outCnt < Outputs.Count; outCnt++)
57+
foreach (NodeOutput output in Outputs)
5858
{
59-
NodeOutput output = Outputs [outCnt];
6059
while (output.connections.Count != 0)
6160
output.connections[0].RemoveConnection ();
6261
DestroyImmediate (output, true);
6362
}
64-
for (int inCnt = 0; inCnt < Inputs.Count; inCnt++)
63+
foreach (NodeInput input in Inputs)
6564
{
66-
NodeInput input = Inputs [inCnt];
6765
if (input.connection != null)
6866
input.connection.connections.Remove (input);
6967
DestroyImmediate (input, true);
7068
}
71-
for (int knobCnt = 0; knobCnt < nodeKnobs.Count; knobCnt++)
69+
foreach (NodeKnob knob in nodeKnobs)
7270
{ // Inputs/Outputs need specific treatment, unfortunately
73-
if (nodeKnobs[knobCnt] != null)
74-
DestroyImmediate (nodeKnobs[knobCnt], true);
71+
if (knob != null)
72+
DestroyImmediate (knob, true);
7573
}
76-
for (int transCnt = 0; transCnt < transitions.Count; transCnt++)
74+
foreach (Transition transition in transitions)
7775
{
78-
transitions [transCnt].Delete ();
76+
transition.Delete ();
7977
}
8078
DestroyImmediate (this, true);
8179
}
@@ -244,8 +242,8 @@ protected internal virtual void DrawNode ()
244242
protected internal virtual void DrawKnobs ()
245243
{
246244
CheckNodeKnobMigration ();
247-
for (int knobCnt = 0; knobCnt < nodeKnobs.Count; knobCnt++)
248-
nodeKnobs[knobCnt].DrawKnob ();
245+
foreach (NodeKnob knob in nodeKnobs)
246+
knob.DrawKnob ();
249247
}
250248

251249
/// <summary>
@@ -256,15 +254,13 @@ protected internal virtual void DrawConnections ()
256254
CheckNodeKnobMigration ();
257255
if (Event.current.type != EventType.Repaint)
258256
return;
259-
for (int outCnt = 0; outCnt < Outputs.Count; outCnt++)
257+
foreach (NodeOutput output in Outputs)
260258
{
261-
NodeOutput output = Outputs [outCnt];
262259
Vector2 startPos = output.GetGUIKnob ().center;
263260
Vector2 startDir = output.GetDirection ();
264261

265-
for (int conCnt = 0; conCnt < output.connections.Count; conCnt++)
262+
foreach (NodeInput input in output.connections)
266263
{
267-
NodeInput input = output.connections [conCnt];
268264
NodeEditorGUI.DrawConnection (startPos,
269265
startDir,
270266
input.GetGUIKnob ().center,
@@ -279,10 +275,10 @@ protected internal virtual void DrawConnections ()
279275
/// </summary>
280276
public void DrawTransitions ()
281277
{
282-
for (int cnt = 0; cnt < transitions.Count; cnt++)
278+
foreach (Transition transition in transitions)
283279
{
284-
if (transitions[cnt].startNode == this)
285-
transitions[cnt].DrawFromStartNode ();
280+
if (transition.startNode == this)
281+
transition.DrawFromStartNode ();
286282
}
287283
}
288284

@@ -301,9 +297,9 @@ public virtual void DrawNodePropertyEditor() { }
301297
/// </summary>
302298
protected internal bool allInputsReady ()
303299
{
304-
for (int inCnt = 0; inCnt < Inputs.Count; inCnt++)
300+
foreach (NodeInput input in Inputs)
305301
{
306-
if (Inputs[inCnt].connection == null || Inputs[inCnt].connection.IsValueNull)
302+
if (input.connection == null || input.connection.IsValueNull)
307303
return false;
308304
}
309305
return true;
@@ -313,8 +309,8 @@ protected internal bool allInputsReady ()
313309
/// </summary>
314310
protected internal bool hasUnassignedInputs ()
315311
{
316-
for (int inCnt = 0; inCnt < Inputs.Count; inCnt++)
317-
if (Inputs [inCnt].connection == null)
312+
foreach (NodeInput input in Inputs)
313+
if (input.connection == null)
318314
return true;
319315
return false;
320316
}
@@ -324,9 +320,9 @@ protected internal bool hasUnassignedInputs ()
324320
/// </summary>
325321
protected internal bool descendantsCalculated ()
326322
{
327-
for (int cnt = 0; cnt < Inputs.Count; cnt++)
323+
foreach (NodeInput input in Inputs)
328324
{
329-
if (Inputs [cnt].connection != null && !Inputs [cnt].connection.body.calculated)
325+
if (input.connection != null && !input.connection.body.calculated)
330326
return false;
331327
}
332328
return true;
@@ -337,8 +333,8 @@ protected internal bool descendantsCalculated ()
337333
/// </summary>
338334
protected internal bool isInput ()
339335
{
340-
for (int cnt = 0; cnt < Inputs.Count; cnt++)
341-
if (Inputs [cnt].connection != null)
336+
foreach (NodeInput input in Inputs)
337+
if (input.connection != null)
342338
return false;
343339
return true;
344340
}
@@ -386,10 +382,10 @@ protected void OutputKnob (int outputIdx)
386382
/// </summary>
387383
public NodeOutput GetOutputAtPos (Vector2 pos)
388384
{
389-
for (int outCnt = 0; outCnt < Outputs.Count; outCnt++)
385+
foreach (NodeOutput output in Outputs)
390386
{ // Search for an output at the position
391-
if (Outputs [outCnt].GetScreenKnob ().Contains (new Vector3 (pos.x, pos.y)))
392-
return Outputs [outCnt];
387+
if (output.GetScreenKnob ().Contains (new Vector3 (pos.x, pos.y)))
388+
return output;
393389
}
394390
return null;
395391
}
@@ -434,10 +430,10 @@ protected void InputKnob (int inputIdx)
434430
/// </summary>
435431
public NodeInput GetInputAtPos (Vector2 pos)
436432
{
437-
for (int inCnt = 0; inCnt < Inputs.Count; inCnt++)
433+
foreach (NodeInput input in Inputs)
438434
{ // Search for an input at the position
439-
if (Inputs [inCnt].GetScreenKnob ().Contains (new Vector3 (pos.x, pos.y)))
440-
return Inputs [inCnt];
435+
if (input.GetScreenKnob ().Contains (new Vector3 (pos.x, pos.y)))
436+
return input;
441437
}
442438
return null;
443439
}
@@ -454,9 +450,9 @@ public bool isChildOf (Node otherNode)
454450
if (otherNode == null || otherNode == this)
455451
return false;
456452
if (BeginRecursiveSearchLoop ()) return false;
457-
for (int cnt = 0; cnt < Inputs.Count; cnt++)
453+
foreach (NodeInput input in Inputs)
458454
{
459-
NodeOutput connection = Inputs [cnt].connection;
455+
NodeOutput connection = input.connection;
460456
if (connection != null)
461457
{
462458
if (connection.body != startRecursiveSearchNode)
@@ -479,9 +475,9 @@ public bool isChildOf (Node otherNode)
479475
internal bool isInLoop ()
480476
{
481477
if (BeginRecursiveSearchLoop ()) return this == startRecursiveSearchNode;
482-
for (int cnt = 0; cnt < Inputs.Count; cnt++)
478+
foreach (NodeInput input in Inputs)
483479
{
484-
NodeOutput connection = Inputs [cnt].connection;
480+
NodeOutput connection = input.connection;
485481
if (connection != null)
486482
{
487483
if (connection.body.isInLoop ())
@@ -507,9 +503,9 @@ internal bool allowsLoopRecursion (Node otherNode)
507503
if (otherNode == null)
508504
return false;
509505
if (BeginRecursiveSearchLoop ()) return false;
510-
for (int cnt = 0; cnt < Inputs.Count; cnt++)
506+
foreach (NodeInput input in Inputs)
511507
{
512-
NodeOutput connection = Inputs [cnt].connection;
508+
NodeOutput connection = input.connection;
513509
if (connection != null)
514510
{
515511
if (connection.body != startRecursiveSearchNode)
@@ -534,11 +530,10 @@ public void ClearCalculation ()
534530
{
535531
if (BeginRecursiveSearchLoop ()) return;
536532
calculated = false;
537-
for (int outCnt = 0; outCnt < Outputs.Count; outCnt++)
533+
foreach (NodeOutput output in Outputs)
538534
{
539-
NodeOutput output = Outputs [outCnt];
540-
for (int conCnt = 0; conCnt < output.connections.Count; conCnt++)
541-
output.connections [conCnt].body.ClearCalculation ();
535+
foreach (NodeInput connection in output.connections)
536+
connection.body.ClearCalculation ();
542537
}
543538
EndRecursiveSearchLoop ();
544539
}
@@ -608,4 +603,7 @@ public static void CreateTransition (Node fromNode, Node toNode)
608603

609604
#endregion
610605
}
606+
607+
608+
611609
}

Node_Editor/Framework/NodeEditor.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,16 @@ public static void DrawSubCanvas (NodeCanvas nodeCanvas, NodeEditorState editorS
217217
}
218218

219219
// Draw the transitions and connections. Has to be drawn before nodes as transitions originate from node centers
220-
for (int nodeCnt = 0; nodeCnt < curNodeCanvas.nodes.Count; nodeCnt++)
220+
221+
foreach (Node node in curNodeCanvas.nodes)
221222
{
222-
Node node = curNodeCanvas.nodes [nodeCnt];
223223
node.DrawTransitions ();
224224
node.DrawConnections ();
225225
}
226226

227227
// Draw the nodes
228-
for (int nodeCnt = 0; nodeCnt < curNodeCanvas.nodes.Count; nodeCnt++)
228+
foreach (Node node in curNodeCanvas.nodes)
229229
{
230-
Node node = curNodeCanvas.nodes [nodeCnt];
231230
node.DrawNode ();
232231
if (Event.current.type == EventType.Repaint)
233232
node.DrawKnobs ();
@@ -268,9 +267,9 @@ public static Node NodeAtPosition (NodeEditorState editorState, NodeCanvas nodeC
268267
Node node = nodeCanvas.nodes [nodeCnt];
269268
if (CanvasGUIToScreenRect (node.rect).Contains (pos)) // Node Body
270269
return node;
271-
for (int knobCnt = 0; knobCnt < node.nodeKnobs.Count; knobCnt++)
270+
foreach (NodeKnob knob in node.nodeKnobs)
272271
{ // Any edge control
273-
if (node.nodeKnobs[knobCnt].GetScreenKnob ().Contains (pos))
272+
if (knob.GetScreenKnob ().Contains (pos))
274273
return node;
275274
}
276275
}
@@ -323,9 +322,9 @@ private static bool ignoreInput (Vector2 mousePos)
323322
// Mouse outside of canvas rect or inside an ignoreInput rect
324323
if (!curEditorState.canvasRect.Contains (mousePos))
325324
return true;
326-
for (int ignoreCnt = 0; ignoreCnt < curEditorState.ignoreInput.Count; ignoreCnt++)
325+
foreach(Rect rect in curEditorState.ignoreInput)
327326
{
328-
if (curEditorState.ignoreInput [ignoreCnt].Contains (mousePos))
327+
if (rect.Contains (mousePos))
329328
return true;
330329
}
331330
return false;
@@ -433,9 +432,9 @@ public static void InputEvents ()
433432
{ // A connection is drawn, so provide a context menu with apropriate nodes to auto-connect
434433
foreach (Node node in NodeTypes.nodes.Keys)
435434
{ // Iterate through all nodes and check for compability
436-
for (int inputCnt = 0; inputCnt < node.Inputs.Count; inputCnt++)
435+
foreach (NodeInput input in node.Inputs)
437436
{
438-
if (node.Inputs[inputCnt].type == curEditorState.connectOutput.type)
437+
if (input.type == curEditorState.connectOutput.type)
439438
{
440439
menu.AddItem (new GUIContent ("Add " + NodeTypes.nodes[node].adress), false, ContextCallback, new NodeEditorMenuCallback (node.GetID, curNodeCanvas, curEditorState));
441440
break;
@@ -531,8 +530,8 @@ public static void InputEvents ()
531530
if (curEditorState.panWindow)
532531
{ // Scroll everything with the current mouse delta
533532
curEditorState.panOffset += e.delta * curEditorState.zoom;
534-
for (int nodeCnt = 0; nodeCnt < curNodeCanvas.nodes.Count; nodeCnt++)
535-
curNodeCanvas.nodes [nodeCnt].rect.position += e.delta * curEditorState.zoom;
533+
foreach (Node node in curNodeCanvas.nodes)
534+
node.rect.position += e.delta * curEditorState.zoom;
536535
e.delta = Vector2.zero;
537536
RepaintClients ();
538537
}
@@ -731,9 +730,8 @@ public static void StopTransitioning (NodeCanvas nodeCanvas)
731730
/// </summary>
732731
private static void UpdateTransitions ()
733732
{
734-
for (int cnt = 0; cnt < transitioningNodeCanvases.Count; cnt++)
733+
foreach (NodeCanvas nodeCanvas in transitioningNodeCanvases)
735734
{
736-
NodeCanvas nodeCanvas = transitioningNodeCanvases[cnt];
737735
if (!nodeCanvas.currentNode.AcceptsTranstitions || nodeCanvas.currentNode.transitions.Count == 0)
738736
{ // Error - this node should not have any transitions, in or out
739737
StopTransitioning (nodeCanvas);
@@ -787,9 +785,8 @@ public static bool isTransitioning (NodeCanvas nodeCanvas)
787785
/// </summary>
788786
private static Transition GetNextTransition (Node node)
789787
{
790-
for (int transCnt = 0; transCnt < node.transitions.Count; transCnt++)
788+
foreach (Transition trans in node.transitions)
791789
{
792-
Transition trans = node.transitions[transCnt];
793790
if (trans.startNode == node && trans.conditionsMet ())
794791
return trans;
795792
}
@@ -846,9 +843,9 @@ public static void StartCalculation ()
846843
for (int roundCnt = 0; !limitReached; roundCnt++)
847844
{ // Runs until every node possible is calculated
848845
limitReached = true;
849-
for (int workCnt = 0; workCnt < workList.Count; workCnt++)
846+
foreach (Node node in workList)
850847
{
851-
if (ContinueCalculation (workList [workCnt]))
848+
if (ContinueCalculation (node))
852849
limitReached = false;
853850
}
854851
}
@@ -870,11 +867,10 @@ public static bool ContinueCalculation (Node node)
870867
workList.Remove (node);
871868
if (node.ContinueCalculation && calculationCount < 1000)
872869
{
873-
for (int outCnt = 0; outCnt < node.Outputs.Count; outCnt++)
870+
foreach (NodeOutput output in node.Outputs)
874871
{
875-
NodeOutput output = node.Outputs [outCnt];
876-
for (int conCnt = 0; conCnt < output.connections.Count; conCnt++)
877-
ContinueCalculation (output.connections [conCnt].body);
872+
foreach (NodeInput connection in output.connections)
873+
ContinueCalculation(connection.body);
878874
}
879875
}
880876
else if (calculationCount >= 1000)

0 commit comments

Comments
 (0)