CurveControlCommands.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //-----------------------------------------------------------------------------
  2. // CurveControlCommands.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. namespace Xna.Tools
  11. {
  12. /// <summary>
  13. /// Command that saves old and new EditCurveKey selection.
  14. /// </summary>
  15. public class SelectCommand : ICommand
  16. {
  17. public SelectCommand(EditCurve curve, EditCurveKeySelection newSelection,
  18. EditCurveKeySelection oldSelection)
  19. {
  20. this.curve = curve;
  21. this.oldSelection = oldSelection;
  22. this.newSelection = newSelection;
  23. }
  24. #region ICommand Members
  25. public void Execute()
  26. {
  27. curve.ApplySelection(newSelection, false);
  28. }
  29. public void Unexecute()
  30. {
  31. curve.ApplySelection(oldSelection, false);
  32. }
  33. #endregion
  34. EditCurve curve;
  35. EditCurveKeySelection oldSelection;
  36. EditCurveKeySelection newSelection;
  37. }
  38. /// <summary>
  39. /// Command that saves add or remove EditCurveKey information.
  40. /// </summary>
  41. public class EditCurveKeyAddRemoveCommand : ICommand
  42. {
  43. public EditCurveKeyAddRemoveCommand(EditCurve curve,
  44. ICollection<EditCurveKey> deleteKeys)
  45. {
  46. this.curve = curve;
  47. addKey = false;
  48. keys = new List<EditCurveKey>(deleteKeys.Count);
  49. foreach ( EditCurveKey key in deleteKeys )
  50. keys.Add(key.Clone());
  51. }
  52. public EditCurveKeyAddRemoveCommand(EditCurve curve, EditCurveKey addKey,
  53. EditCurveKeySelection selection)
  54. {
  55. this.curve = curve;
  56. this.addKey = true;
  57. this.selection = selection.Clone();
  58. keys = new List<EditCurveKey>();
  59. keys.Add(addKey.Clone());
  60. }
  61. #region ICommand Members
  62. public void Execute()
  63. {
  64. if (addKey)
  65. AddKeys();
  66. else
  67. RemoveKeys();
  68. }
  69. public void Unexecute()
  70. {
  71. if (addKey)
  72. RemoveKeys();
  73. else
  74. AddKeys();
  75. }
  76. #endregion
  77. #region Private Methods
  78. private void AddKeys()
  79. {
  80. foreach (EditCurveKey savedKey in keys)
  81. {
  82. EditCurveKey addingKey = savedKey.Clone();
  83. curve.Keys.Add(addingKey);
  84. // Removing key requires re-compute neighbor keys tangents.
  85. curve.ComputeTangents(curve.Keys.IndexOf(addingKey));
  86. }
  87. curve.ApplySelection(new EditCurveKeySelection(keys), false);
  88. }
  89. private void RemoveKeys()
  90. {
  91. foreach (EditCurveKey savedKey in keys)
  92. {
  93. long keyId = savedKey.Id;
  94. EditCurveKey key;
  95. curve.Keys.TryGetValue(keyId, out key);
  96. // Remember key index.
  97. int idx = curve.Keys.IndexOf(key);
  98. // Remove key from keys.
  99. curve.Keys.Remove(key);
  100. // Removing key requires re-compute neighbor keys tangents.
  101. curve.ComputeTangents(idx);
  102. }
  103. if ( selection != null )
  104. curve.ApplySelection(selection, false);
  105. }
  106. #endregion
  107. EditCurve curve;
  108. EditCurveKeySelection selection;
  109. List<EditCurveKey> keys;
  110. bool addKey;
  111. }
  112. /// <summary>
  113. /// Command that saves multiple EditCurveKey values.
  114. /// </summary>
  115. public class EditCurveKeyUpdateCommand : ICommand
  116. {
  117. public EditCurveKeyUpdateCommand(EditCurve curve,
  118. ICollection<EditCurveKey> oldKeyValues,
  119. ICollection<EditCurveKey> newKeyValues)
  120. {
  121. this.curve = curve;
  122. this.oldKeyValues = oldKeyValues;
  123. this.newKeyValues = newKeyValues;
  124. }
  125. #region ICommand Members
  126. public void Execute()
  127. {
  128. curve.ApplyKeyValues(newKeyValues);
  129. }
  130. public void Unexecute()
  131. {
  132. curve.ApplyKeyValues(oldKeyValues);
  133. }
  134. #endregion
  135. EditCurve curve;
  136. ICollection<EditCurveKey> oldKeyValues;
  137. ICollection<EditCurveKey> newKeyValues;
  138. }
  139. /// <summary>
  140. /// Command that saves EditCurveState.
  141. /// </summary>
  142. public class EditCurveStateChangeCommand : ICommand
  143. {
  144. public EditCurveStateChangeCommand(EditCurve curve, EditCurveState oldState,
  145. EditCurveState newState)
  146. {
  147. if (oldState == null) throw new ArgumentNullException("oldState");
  148. if (newState == null) throw new ArgumentNullException("newState");
  149. this.curve = curve;
  150. this.oldState = (EditCurveState)oldState.Clone();
  151. this.newState = (EditCurveState)newState.Clone();
  152. }
  153. #region ICommand Members
  154. public void Execute()
  155. {
  156. curve.ApplyState(newState);
  157. }
  158. public void Unexecute()
  159. {
  160. curve.ApplyState(oldState);
  161. }
  162. #endregion
  163. EditCurve curve;
  164. EditCurveState newState;
  165. EditCurveState oldState;
  166. }
  167. }