public void moveComponent(int index, bool up) { int next_position = FindNextValidPosition(index, up); if (up) // up arrow means down in the list { for (int i = index; i > next_position; i--) { VarTestsComponents.Reverse(i - 1, 2); } if (VarTestsComponents[next_position].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) { for (int i = next_position + 1; i < index; i++) { VarTestsComponents.Reverse(i, 2); } } } else { for (int i = index; i < next_position; i++) { VarTestsComponents.Reverse(i, 2); } if (VarTestsComponents[next_position].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) { for (int i = next_position - 1; i > index; i--) { VarTestsComponents.Reverse(i - 1, 2); } } } }
public void Remove(int index) { if (VarTestsComponents[index].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) { if (index > 0 && VarTestsComponents[index - 1].GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) { // remove TestOperator then VarOperation VarTestsComponents.RemoveAt(index - 1); VarTestsComponents.RemoveAt(index - 1); } else if (index < VarTestsComponents.Count - 1 && VarTestsComponents[index + 1].GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) { // remove VarOperation then TestOperator VarTestsComponents.RemoveAt(index); VarTestsComponents.RemoveAt(index); } else if (VarTestsComponents.Count == 1) { VarTestsComponents.RemoveAt(0); } else { // no operator before and after: items must be between parenthesis, we delete and run again VarTestsComponents.RemoveAt(index + 1); VarTestsComponents.RemoveAt(index - 1); Remove(index - 1); } } else if (VarTestsComponents[index].GetClassVarTestsComponentType() == VarTestsParenthesis.GetVarTestsComponentType()) { VarTestsParenthesis tmp = (VarTestsParenthesis)VarTestsComponents[index]; int other_parenthesis_index = -1; if (tmp.parenthesis == "(") { other_parenthesis_index = FindClosingParenthesis(index + 1); VarTestsComponents.RemoveAt(other_parenthesis_index); VarTestsComponents.RemoveAt(index); } else if (tmp.parenthesis == ")") { other_parenthesis_index = FindOpeningParenthesis(index - 1); VarTestsComponents.RemoveAt(index); VarTestsComponents.RemoveAt(other_parenthesis_index); } } }
public void Add(string str) { string[] part = str.Split(':'); if (part[0].Equals(VarTestsLogicalOperator.GetVarTestsComponentType())) { VarTestsComponents.Add(new VarTestsLogicalOperator(part[1])); } if (part[0].Equals(VarTestsParenthesis.GetVarTestsComponentType())) { VarTestsComponents.Add(new VarTestsParenthesis(part[1])); } if (part[0].Equals(VarOperation.GetVarTestsComponentType())) { VarTestsComponents.Add(new VarOperation(part[1])); } }
public static float AddEventVarConditionComponents(Transform parentTransform, float xOffset, float yOffset, ITestable testable, Action updateAction) { UIElement ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset, yOffset, 18, 1); ui.SetText(new StringKey("val", "X_COLON", CommonStringKeys.TESTS)); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 18f, yOffset, 1, 1); ui.SetText(CommonStringKeys.PLUS, Color.green); ui.SetButton(delegate { AddTestOp(testable, updateAction); }); new UIElementBorder(ui, Color.green); if (testable.Tests.VarTestsComponents.Count > 0) { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset, yOffset, 1, 1); ui.SetText(CommonStringKeys.PLUS, Color.green); ui.SetButton(delegate { SelectAddParenthesis(testable, updateAction); }); new UIElementBorder(ui, Color.green); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 1.0f, yOffset, 2, 1); ui.SetText("(...)"); } yOffset++; int component_index = 0; foreach (VarTestsComponent tc in testable.Tests.VarTestsComponents) { if (tc is VarOperation) { int tmp_index = component_index; // only display arrows if item can be moved if (component_index != (testable.Tests.VarTestsComponents.Count - 1) && testable.Tests.VarTestsComponents.Count > 1 && testable.Tests.VarTestsComponents.FindIndex(component_index + 1, x => x.GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) != -1 ) { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset, yOffset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, false); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } if (component_index != 0 && testable.Tests.VarTestsComponents.FindLastIndex(component_index - 1, x => x.GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) != -1 ) { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 1.0f, yOffset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, true); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } VarOperation tmp = (VarOperation)tc; ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 2f, yOffset, 8.5f, 1); ui.SetText(tmp.var); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 10.5f, yOffset, 2, 1); ui.SetText(tmp.operation); ui.SetButton(delegate { SetTestOpp(tmp, updateAction); }); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 12.5f, yOffset, 5.5f, 1); ui.SetText(tmp.value); ui.SetButton(delegate { SetValue(tmp, updateAction); }); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 18f, yOffset++, 1, 1); ui.SetText(CommonStringKeys.MINUS, Color.red); ui.SetButton(delegate { RemoveOp(testable.Tests, tmp_index, updateAction); }); new UIElementBorder(ui, Color.red); } if (tc is VarTestsLogicalOperator) { VarTestsLogicalOperator tmp = (VarTestsLogicalOperator)tc; ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 9.5f, yOffset, 4, 1); if (tmp.op.Equals("AND")) { ui.SetText(CommonStringKeys.AND); } else if (tmp.op.Equals("OR")) { ui.SetText(CommonStringKeys.OR); } ui.SetButton(delegate { tmp.NextLogicalOperator(); updateAction.Invoke(); }); new UIElementBorder(ui); yOffset++; } if (tc is VarTestsParenthesis) { int tmp_index = component_index; VarTestsParenthesis tp = (VarTestsParenthesis)tc; if (component_index != (testable.Tests.VarTestsComponents.Count - 1) && testable.Tests.VarTestsComponents.FindIndex(component_index + 1, x => x.GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) != -1 ) { if (tp.parenthesis == "(") { int valid_index = testable.Tests.FindNextValidPosition(component_index, false); if (valid_index != -1 && testable.Tests.FindClosingParenthesis(valid_index) != -1) { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset, yOffset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, false); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } } else if (tp.parenthesis == ")") { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset, yOffset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, false); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } } if (component_index != 0 && testable.Tests.VarTestsComponents.FindLastIndex(component_index - 1, x => x.GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) != -1 ) { if (tp.parenthesis == "(") { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 1f, yOffset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, true); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } else if (tp.parenthesis == ")") { int valid_index = testable.Tests.FindNextValidPosition(component_index, true); if (valid_index != -1 && testable.Tests.FindOpeningParenthesis(valid_index) != -1) { ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 1f, yOffset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { testable.Tests.moveComponent(tmp_index, true); updateAction.Invoke(); }); new UIElementBorder(ui, Color.yellow); } } } ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 2f, yOffset, 2, 1); ui.SetText(tp.parenthesis); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, parentTransform); ui.SetLocation(xOffset + 4f, yOffset, 1, 1); ui.SetText(CommonStringKeys.MINUS, Color.red); ui.SetButton(delegate { testable.Tests.Remove(tmp_index); updateAction.Invoke(); }); new UIElementBorder(ui, Color.red); yOffset++; } component_index++; } return(yOffset + 1); }
/// <summary> Search for the next valid position for parenthesis or varOperation </summary> /// <param name="index">index of current item to move</param> /// <param name="up">direction of requested movement (visually on UI)</param> /// <returns> index of next position</returns> public int FindNextValidPosition(int index, bool up) { if (VarTestsComponents[index].GetClassVarTestsComponentType() != VarTestsParenthesis.GetVarTestsComponentType() && VarTestsComponents[index].GetClassVarTestsComponentType() != VarOperation.GetVarTestsComponentType()) { return(-1); } int i = index; if (up) { i--; } else { i++; } if (VarTestsComponents[index].GetClassVarTestsComponentType() == VarTestsParenthesis.GetVarTestsComponentType()) { VarTestsParenthesis tmp = (VarTestsParenthesis)VarTestsComponents[index]; while (i >= 0 && i <= VarTestsComponents.Count - 1) { if ( // "(" should be before a varOperation or at the beginning a&(b a&((b (a&b (tmp.parenthesis == "(" && VarTestsComponents[i].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) || // ")" should be before a LogicalOperator or at the end a)&b a))&b a&b) (tmp.parenthesis == ")" && VarTestsComponents[i].GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) ) { if (up) { return(i); } else if (i != index + 1)// if going down, ignore first item found { return(i - 1); } } if (up) { i--; } else { i++; } } if (i >= VarTestsComponents.Count && tmp.parenthesis == ")") { return(VarTestsComponents.Count - 1); } else if (i <= 0 && tmp.parenthesis == "(") { return(0); } } else if (VarTestsComponents[index].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) { while (i >= 0 && i <= VarTestsComponents.Count - 1) { // varOperation should take the place of another varOperation if (VarTestsComponents[i].GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) { return(i); } if (up) { i--; } else { i++; } } } if (i < 0 || i >= VarTestsComponents.Count) { return(-1); } // this should not happen ValkyrieDebug.Log("Invalid test position"); return(-1); }
virtual public float AddEventVarConditionComponents(float offset) { UIElement ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(0.5f, offset, 18, 1); ui.SetText(new StringKey("val", "X_COLON", TESTS)); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(18.5f, offset, 1, 1); ui.SetText(CommonStringKeys.PLUS, Color.green); ui.SetButton(delegate { AddTestOp(); }); new UIElementBorder(ui, Color.green); if (component.tests.VarTestsComponents.Count > 0) { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(0.5f, offset, 1, 1); ui.SetText(CommonStringKeys.PLUS, Color.green); ui.SetButton(delegate { SelectAddParenthesis(); }); new UIElementBorder(ui, Color.green); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(1.5f, offset, 2, 1); ui.SetText("(...)"); } offset++; int component_index = 0; foreach (VarTestsComponent tc in component.tests.VarTestsComponents) { if (tc is VarOperation) { int tmp_index = component_index; // only display arrows if item can be moved if (component_index != (component.tests.VarTestsComponents.Count - 1) && component.tests.VarTestsComponents.Count > 1 && component.tests.VarTestsComponents.FindIndex(component_index + 1, x => x.GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) != -1 ) { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(0.5f, offset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, false); Update(); }); new UIElementBorder(ui, Color.yellow); } if (component_index != 0 && component.tests.VarTestsComponents.FindLastIndex(component_index - 1, x => x.GetClassVarTestsComponentType() == VarTestsLogicalOperator.GetVarTestsComponentType()) != -1 ) { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(1.5f, offset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, true); Update(); }); new UIElementBorder(ui, Color.yellow); } VarOperation tmp = (VarOperation)tc; ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(2.5f, offset, 8.5f, 1); ui.SetText(tmp.var); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(11f, offset, 2, 1); ui.SetText(tmp.operation); ui.SetButton(delegate { SetTestOpp(tmp); }); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(13f, offset, 5.5f, 1); ui.SetText(tmp.value); ui.SetButton(delegate { SetValue(tmp); }); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(18.5f, offset++, 1, 1); ui.SetText(CommonStringKeys.MINUS, Color.red); ui.SetButton(delegate { RemoveOp(tmp_index); }); new UIElementBorder(ui, Color.red); } if (tc is VarTestsLogicalOperator) { VarTestsLogicalOperator tmp = (VarTestsLogicalOperator)tc; ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(10f, offset, 4, 1); if (tmp.op.Equals("AND")) { ui.SetText(AND); } else if (tmp.op.Equals("OR")) { ui.SetText(OR); } ui.SetButton(delegate { tmp.NextLogicalOperator(); Update(); }); new UIElementBorder(ui); offset++; } if (tc is VarTestsParenthesis) { int tmp_index = component_index; VarTestsParenthesis tp = (VarTestsParenthesis)tc; if (component_index != (component.tests.VarTestsComponents.Count - 1) && component.tests.VarTestsComponents.FindIndex(component_index + 1, x => x.GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) != -1 ) { if (tp.parenthesis == "(") { int valid_index = component.tests.FindNextValidPosition(component_index, false); if (valid_index != -1 && component.tests.FindClosingParenthesis(valid_index) != -1) { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(0.5f, offset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, false); Update(); }); new UIElementBorder(ui, Color.yellow); } } else if (tp.parenthesis == ")") { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(0.5f, offset, 1, 1); ui.SetText(CommonStringKeys.DOWN, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, false); Update(); }); new UIElementBorder(ui, Color.yellow); } } if (component_index != 0 && component.tests.VarTestsComponents.FindLastIndex(component_index - 1, x => x.GetClassVarTestsComponentType() == VarOperation.GetVarTestsComponentType()) != -1 ) { if (tp.parenthesis == "(") { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(1.5f, offset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, true); Update(); }); new UIElementBorder(ui, Color.yellow); } else if (tp.parenthesis == ")") { int valid_index = component.tests.FindNextValidPosition(component_index, true); if (valid_index != -1 && component.tests.FindOpeningParenthesis(valid_index) != -1) { ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(1.5f, offset, 1, 1); ui.SetText(CommonStringKeys.UP, Color.yellow); ui.SetTextAlignment(TextAnchor.LowerCenter); ui.SetButton(delegate { component.tests.moveComponent(tmp_index, true); Update(); }); new UIElementBorder(ui, Color.yellow); } } } ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(2.5f, offset, 2, 1); ui.SetText(tp.parenthesis); new UIElementBorder(ui); ui = new UIElement(Game.EDITOR, scrollArea.GetScrollTransform()); ui.SetLocation(4.5f, offset, 1, 1); ui.SetText(CommonStringKeys.MINUS, Color.red); ui.SetButton(delegate { component.tests.Remove(tmp_index); Update(); }); new UIElementBorder(ui, Color.red); offset++; } component_index++; } return(offset + 1); }