public void Test1() { var i = 1; var sc = new StackCommand(() => i++, () => i--); sc.Execute(); Assert.Equal(2, i); Assert.True(sc.CanUndo); Assert.False(sc.CanRedo); sc.Execute(); Assert.Equal(3, i); Assert.True(sc.CanUndo); Assert.False(sc.CanRedo); sc.Undo(); Assert.Equal(2, i); Assert.True(sc.CanUndo); Assert.True(sc.CanRedo); sc.Undo(); Assert.Equal(1, i); Assert.False(sc.CanUndo); Assert.True(sc.CanRedo); sc.Redo(); Assert.Equal(2, i); Assert.True(sc.CanUndo); Assert.True(sc.CanRedo); sc.Redo(); Assert.Equal(3, i); Assert.True(sc.CanUndo); Assert.False(sc.CanRedo); }
public void Test3() { var i = 1; var sc = new StackCommand(() => i++, () => i--); sc.Execute(); sc.Execute(); sc.Execute(); Assert.True(sc.CanUndo); Assert.False(sc.CanRedo); sc.Undo(); sc.Clear(); Assert.False(sc.CanUndo); Assert.False(sc.CanRedo); }
public static void Stack(User user, string directionAndAmount = "1", int offset = 0) { try { StackCommand command = new StackCommand(user, directionAndAmount, offset); if (command.Invoke()) { user.Player.MsgLoc($"{command.BlocksChanged} blocks changed."); } } catch (WorldEditCommandException e) { user.Player.ErrorLocStr(e.Message); } catch (Exception e) { Log.WriteError(Localizer.Do($"{e}")); } }
void MouseInput() { if (Input.GetMouseButtonDown(0)) { if (selected != null) { ResetCard(selected); } //get origin mouse position for drag calculation mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)); RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit) { if (hit.collider.CompareTag("Card")) { Card(hit.collider.gameObject); } if (hit.collider.CompareTag("Foundation")) { selected = null; } } else { selected = null; } } //when dragging, card moves with mouse if (Input.GetMouseButton(0)) { float deltaX = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)).x - mousePosition.x; float deltaY = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)).y - mousePosition.y; if (selected != null) { selected.transform.position = new Vector3(originPosition.x + deltaX, originPosition.y + deltaY, -0.35f); } } if (Input.GetMouseButtonUp(0)) { if (selected != null && selected.transform.position != originPosition) { Debug.Log("mouse up"); mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)); RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit) { //when card drag to another card if (hit.collider.CompareTag("Card")) { Debug.Log(hit.collider.name); if (Stackable(selected, hit.collider.gameObject)) { ICommand command = new StackCommand(selected, hit.collider.gameObject, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } //when card drag to foundation area if (hit.collider.CompareTag("Foundation")) { int foundationIndx = solitaire.IntoAllFoundations(selected); if (foundationIndx == -1) { ResetCard(selected); } else { ICommand command = new FoundationCommand(selected, originPosition, foundationIndx); CommandInvoker.AddCommand(command); } CardColorWhite(selected); selected = null; } //when card drag to an empty cascade if (hit.collider.CompareTag("Empty Cascade")) { //check if the cascade is really empty if (hit.collider.transform.childCount == 0) { ICommand command = new EmptyCascadeCommand(selected, hit.collider.gameObject, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } //when card drag to free cell if (hit.collider.CompareTag("Free Cell")) { if (selected.transform.childCount == 0) { int freeCellIndx = hit.collider.transform.GetSiblingIndex(); if (solitaire.freeCells[freeCellIndx] == "") { ////remove from the free cell if the card is in it ICommand command = new FreeCellCommand(freeCellIndx, selected, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } } } else { ResetCard(selected); //selected = null; } if (selected != null) { ResetCard(selected, false); } } } }
public StackOperation(StackCommand command, object obj = null) { Command = command; Object = obj; }
public Controller() { main_data_structure = GroupStructure.Instance; cmdStack = new StackCommand(); }