comparison API/Commands/RemoveUnitCommand.cs @ 473:0d032c04210e

Re #359: Add "only contained" attribute to unit types * Make sure remove handles sub-units as well Note: warning the user that the unit has sub-units rather than just removing them is left to the UI
author IBBoard <dev@ibboard.co.uk>
date Mon, 16 Apr 2012 20:47:14 +0100
parents 3c4a6403a88c
children
comparison
equal deleted inserted replaced
472:e6c93ceba119 473:0d032c04210e
4 4
5 using System; 5 using System;
6 using IBBoard.Commands; 6 using IBBoard.Commands;
7 using IBBoard.Lang; 7 using IBBoard.Lang;
8 using IBBoard.WarFoundry.API.Objects; 8 using IBBoard.WarFoundry.API.Objects;
9 using System.Collections.Generic;
9 10
10 namespace IBBoard.WarFoundry.API.Commands 11 namespace IBBoard.WarFoundry.API.Commands
11 { 12 {
12 /// <summary> 13 /// <summary>
13 /// Summary description for RemoveUnitCommand. 14 /// Summary description for RemoveUnitCommand.
14 /// </summary> 15 /// </summary>
15 public class RemoveUnitCommand : Command 16 public class RemoveUnitCommand : Command
16 { 17 {
17 private Unit unit; 18 private Unit unit;
19 private Unit parentUnit;
18 private ArmyCategory cat; 20 private ArmyCategory cat;
21 private List<RemoveUnitCommand> subCommands;
19 22
20 public RemoveUnitCommand(Unit toRemove) 23 public RemoveUnitCommand(Unit toRemove)
21 { 24 {
22 unit = toRemove; 25 unit = toRemove;
23 cat = unit.Category; 26 cat = unit.Category;
27 parentUnit = unit.ParentUnit;
24 } 28 }
25 29
26 public override bool CanExecute() 30 public override bool CanExecute()
27 { 31 {
28 return (unit != null); 32 return (unit != null);
42 { 46 {
43 return Translation.GetTranslation("removeUnitCommandUndoDescription", "re-add {0} to the army", unit.Name); 47 return Translation.GetTranslation("removeUnitCommandUndoDescription", "re-add {0} to the army", unit.Name);
44 } 48 }
45 } 49 }
46 50
51 private void Prepare()
52 {
53 subCommands = new List<RemoveUnitCommand>();
54 foreach (Unit containedUnit in unit.ContainedUnits)
55 {
56 RemoveUnitCommand removeUnitCommand = new RemoveUnitCommand(containedUnit);
57 removeUnitCommand.Prepare();
58 subCommands.Add(removeUnitCommand);
59 }
60 }
47 public override bool Execute() 61 public override bool Execute()
48 { 62 {
49 this.Redo(); 63 Prepare();
64 Redo();
50 return true; 65 return true;
51 } 66 }
52 67
53 public override void Redo() 68 public override void Redo()
54 { 69 {
70 unit.ParentUnit = null;
55 cat.RemoveUnit(unit); 71 cat.RemoveUnit(unit);
72
73 foreach (RemoveUnitCommand subCommand in subCommands)
74 {
75 subCommand.Redo();
76 }
56 } 77 }
57 78
58 public override void Undo() 79 public override void Undo()
59 { 80 {
81 unit.ParentUnit = parentUnit;
60 cat.AddUnit(unit); 82 cat.AddUnit(unit);
83
84 foreach (RemoveUnitCommand subCommand in subCommands)
85 {
86 subCommand.Undo();
87 }
61 } 88 }
62 89
63 public override string Name 90 public override string Name
64 { 91 {
65 get { return "Remove unit"; } 92 get { return "Remove unit"; }