You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

337 lines
9.1 KiB

  1. // Amplify Shader Editor - Visual Shader Editing Tool
  2. // Copyright (c) Amplify Creations, Lda <info@amplify.pt>
  3. using System;
  4. using UnityEngine;
  5. namespace AmplifyShaderEditor
  6. {
  7. public class ActionData
  8. {
  9. public virtual void ExecuteForward() { }
  10. public virtual void ExecuteReverse() { }
  11. }
  12. // NODES
  13. // Create node
  14. public class CreateNodeActionData : ActionData
  15. {
  16. private int m_nodeId;
  17. private System.Type m_nodeType;
  18. private Vector2 m_nodePos;
  19. public CreateNodeActionData( ParentNode node )
  20. {
  21. m_nodeId = node.UniqueId;
  22. m_nodePos = node.Vec2Position;
  23. m_nodeType = node.GetType();
  24. }
  25. public CreateNodeActionData( int nodeId, System.Type nodeType, Vector2 nodePos )
  26. {
  27. m_nodeId = nodeId;
  28. m_nodePos = nodePos;
  29. m_nodeType = nodeType;
  30. }
  31. public override void ExecuteForward()
  32. {
  33. UIUtils.CreateNode( m_nodeType, false, m_nodePos, m_nodeId );
  34. }
  35. public override void ExecuteReverse()
  36. {
  37. UIUtils.DestroyNode( m_nodeId );
  38. }
  39. public override string ToString()
  40. {
  41. return "Create Node - Type: " + m_nodeType + " Node: " + m_nodeId + " Position: " + m_nodePos;
  42. }
  43. }
  44. // Destroy node
  45. public class DestroyNodeActionData : ActionData
  46. {
  47. private int m_nodeId;
  48. private System.Type m_nodeType;
  49. private Vector2 m_nodePos;
  50. public DestroyNodeActionData( ParentNode node )
  51. {
  52. m_nodeId = node.UniqueId;
  53. m_nodePos = node.Vec2Position;
  54. m_nodeType = node.GetType();
  55. }
  56. public DestroyNodeActionData( int nodeId, System.Type nodeType, Vector2 nodePos )
  57. {
  58. m_nodeId = nodeId;
  59. m_nodePos = nodePos;
  60. m_nodeType = nodeType;
  61. }
  62. public override void ExecuteForward()
  63. {
  64. UIUtils.DestroyNode( m_nodeId );
  65. }
  66. public override void ExecuteReverse()
  67. {
  68. UIUtils.CreateNode( m_nodeType, false, m_nodePos, m_nodeId );
  69. }
  70. public override string ToString()
  71. {
  72. return "Destroy Node - Type: " + m_nodeType + " Node: " + m_nodeId + " Position: " + m_nodePos;
  73. }
  74. }
  75. // Move node
  76. public class MoveNodeActionData : ActionData
  77. {
  78. private int m_nodeId;
  79. private Vector2 m_nodeInitalPos;
  80. private Vector2 m_nodeFinalPos;
  81. public MoveNodeActionData( int nodeId, Vector2 nodeInitialPos, Vector2 nodeFinalPos )
  82. {
  83. m_nodeId = nodeId;
  84. m_nodeInitalPos = nodeInitialPos;
  85. m_nodeFinalPos = nodeFinalPos;
  86. }
  87. public override void ExecuteForward()
  88. {
  89. ParentNode node = UIUtils.GetNode( m_nodeId );
  90. if ( node != null )
  91. node.Vec2Position = m_nodeFinalPos;
  92. }
  93. public override void ExecuteReverse()
  94. {
  95. ParentNode node = UIUtils.GetNode( m_nodeId );
  96. if ( node != null )
  97. node.Vec2Position = m_nodeInitalPos;
  98. }
  99. public override string ToString()
  100. {
  101. return "Move Node - Node: " + m_nodeId + " Initial Position: " + m_nodeInitalPos + " Final Position: " + m_nodeFinalPos;
  102. }
  103. }
  104. // CONNECTIONS
  105. // Create connection
  106. public class CreateConnectionActionData : ActionData
  107. {
  108. private int m_inputNodeId;
  109. private int m_inputPortId;
  110. private int m_outputNodeId;
  111. private int m_outputPortId;
  112. public CreateConnectionActionData( int inputNodeId, int inputPortId, int outputNodeId, int outputPortId )
  113. {
  114. m_inputNodeId = inputNodeId;
  115. m_inputPortId = inputPortId;
  116. m_outputNodeId = outputNodeId;
  117. m_outputPortId = outputPortId;
  118. }
  119. public override void ExecuteForward()
  120. {
  121. UIUtils.ConnectInputToOutput( m_inputNodeId, m_inputPortId, m_outputNodeId, m_outputPortId );
  122. }
  123. public override void ExecuteReverse()
  124. {
  125. UIUtils.DeleteConnection( true, m_inputNodeId, m_inputPortId, false, true );
  126. }
  127. public override string ToString()
  128. {
  129. return "Create Connection Node - Input Node: " + m_inputNodeId + " Input Port: " + m_inputPortId + " Output Node: " + m_outputNodeId + " Output Port: " + m_outputPortId;
  130. }
  131. }
  132. // Destroy connection
  133. public class DestroyConnectionActionData : ActionData
  134. {
  135. private int m_inputNodeId;
  136. private int m_inputPortId;
  137. private int m_outputNodeId;
  138. private int m_outputPortId;
  139. public DestroyConnectionActionData( int inputNodeId, int inputPortId, int outputNodeId, int outputPortId )
  140. {
  141. m_inputNodeId = inputNodeId;
  142. m_inputPortId = inputPortId;
  143. m_outputNodeId = outputNodeId;
  144. m_outputPortId = outputPortId;
  145. }
  146. public override void ExecuteForward()
  147. {
  148. UIUtils.DeleteConnection( true, m_inputNodeId, m_inputPortId, false, true );
  149. }
  150. public override void ExecuteReverse()
  151. {
  152. UIUtils.ConnectInputToOutput( m_inputNodeId, m_inputPortId, m_outputNodeId, m_outputPortId );
  153. }
  154. public override string ToString()
  155. {
  156. return "Destroy Connection Node - Input Node: " + m_inputNodeId + " Input Port: " + m_inputPortId + " Output Node: " + m_outputNodeId + " Output Port: " + m_outputPortId;
  157. }
  158. }
  159. // Move connection
  160. public class MoveInputConnectionActionData : ActionData
  161. {
  162. private int m_oldInputNodeId;
  163. private int m_oldInputNodePortId;
  164. private int m_newInputNodeId;
  165. private int m_newInputNodePortId;
  166. private int m_outputNodeId;
  167. private int m_outputPortId;
  168. public MoveInputConnectionActionData( int oldInputNodeId, int oldInputPortId, int newInputNodeId, int newInputPortId, int outputNodeId, int outputPortId )
  169. {
  170. m_oldInputNodeId = oldInputNodeId;
  171. m_oldInputNodePortId = oldInputPortId;
  172. m_newInputNodeId = newInputNodeId;
  173. m_newInputNodePortId = newInputPortId;
  174. m_outputNodeId = outputNodeId;
  175. m_outputPortId = outputPortId;
  176. }
  177. public override void ExecuteForward()
  178. {
  179. UIUtils.DeleteConnection( true, m_oldInputNodeId, m_oldInputNodePortId, false, true );
  180. UIUtils.ConnectInputToOutput( m_newInputNodeId, m_newInputNodePortId, m_outputNodeId, m_outputPortId );
  181. }
  182. public override void ExecuteReverse()
  183. {
  184. base.ExecuteReverse();
  185. UIUtils.DeleteConnection( true, m_newInputNodeId, m_newInputNodePortId, false, true );
  186. UIUtils.ConnectInputToOutput( m_oldInputNodeId, m_oldInputNodePortId, m_outputNodeId, m_outputPortId );
  187. }
  188. public override string ToString()
  189. {
  190. return "Move Input Connection Node - Old Input Node: " + m_oldInputNodeId + " Old Input Port: " + m_oldInputNodePortId + " New Input Node: " + m_newInputNodeId + " New Input Port: " + m_newInputNodePortId + " Output Node: " + m_outputNodeId + " Output Port: " + m_outputPortId;
  191. }
  192. }
  193. public class MoveOutputConnectionActionData : ActionData
  194. {
  195. private int m_inputNodeId;
  196. private int m_inputPortId;
  197. private int m_newOutputNodeId;
  198. private int m_newOutputPortId;
  199. private int m_oldOutputNodeId;
  200. private int m_oldOutputPortId;
  201. public MoveOutputConnectionActionData( int inputNodeId, int inputPortId, int newOutputNodeId, int newOutputPortId, int oldOutputNodeId, int oldOutputPortId )
  202. {
  203. m_inputNodeId = inputNodeId;
  204. m_inputPortId = inputPortId;
  205. m_newOutputNodeId = newOutputNodeId;
  206. m_newOutputPortId = newOutputPortId;
  207. m_oldOutputNodeId = oldOutputNodeId;
  208. m_oldOutputPortId = oldOutputPortId;
  209. }
  210. public override void ExecuteForward()
  211. {
  212. UIUtils.DeleteConnection( false, m_oldOutputNodeId, m_oldOutputNodeId, false, true );
  213. UIUtils.ConnectInputToOutput( m_inputNodeId, m_inputPortId, m_newOutputNodeId, m_newOutputPortId );
  214. }
  215. public override void ExecuteReverse()
  216. {
  217. base.ExecuteReverse();
  218. UIUtils.DeleteConnection( false, m_newOutputNodeId, m_newOutputPortId, false, true );
  219. UIUtils.ConnectInputToOutput( m_inputNodeId, m_inputPortId, m_oldOutputNodeId, m_oldOutputPortId );
  220. }
  221. public override string ToString()
  222. {
  223. return "Move Input Connection Node - Input Node: " + m_inputNodeId + " Input Port: " + m_inputPortId + " Old Output Node: " + m_oldOutputNodeId + " Old Output Port: " + m_oldOutputPortId + " New Output Node: " + m_newOutputNodeId + " New Output Port: " + m_newOutputPortId;
  224. }
  225. }
  226. public class CreateNewGraphActionData : ActionData
  227. {
  228. private string m_name;
  229. public CreateNewGraphActionData( string name )
  230. {
  231. m_name = name;
  232. }
  233. public override void ExecuteForward()
  234. {
  235. UIUtils.CreateNewGraph( m_name );
  236. }
  237. }
  238. public class ChangeNodePropertiesActionData : ActionData
  239. {
  240. private string m_originalProperties;
  241. private string m_newProperties;
  242. private int m_nodeId;
  243. public ChangeNodePropertiesActionData( ParentNode node, string originalProperties )
  244. {
  245. m_nodeId = node.UniqueId;
  246. m_originalProperties = originalProperties;
  247. m_newProperties = string.Empty;
  248. string trash = string.Empty;
  249. node.WriteToString( ref m_newProperties, ref trash );
  250. }
  251. public ChangeNodePropertiesActionData( int nodeId, string originalProperties )
  252. {
  253. m_nodeId = nodeId;
  254. m_originalProperties = originalProperties;
  255. m_newProperties = string.Empty;
  256. string trash = string.Empty;
  257. UIUtils.GetNode( nodeId ).WriteToString( ref m_newProperties, ref trash );
  258. Debug.Log( m_originalProperties + '\n' + m_newProperties );
  259. }
  260. public override void ExecuteForward()
  261. {
  262. string[] properties = m_newProperties.Split( IOUtils.FIELD_SEPARATOR );
  263. UIUtils.GetNode( m_nodeId ).ReadFromString( ref properties );
  264. }
  265. public override void ExecuteReverse()
  266. {
  267. string[] properties = m_originalProperties.Split( IOUtils.FIELD_SEPARATOR );
  268. UIUtils.GetNode( m_nodeId ).ReadFromString( ref properties );
  269. }
  270. public override string ToString()
  271. {
  272. return "Change Node Propertie - Node: " + m_nodeId + "\nOriginal Properties:\n" + m_originalProperties + "\nNew Properties:\n" + m_newProperties;
  273. }
  274. }
  275. }