2
0

GameObjectUndo.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.CompilerServices;
  6. using bs;
  7. namespace bs.Editor
  8. {
  9. /** @addtogroup Utility-Editor
  10. * @{
  11. */
  12. /// <summary>
  13. /// Handles undo & redo operations for changes made on game objects. Game objects can be recorded just before a change
  14. /// is made and the system will calculate the difference between that state and the state at the end of current frame.
  15. /// This difference will then be recorded as a undo/redo operation. The undo/redo operation will also take care of
  16. /// selecting the object & field it is acting upon.
  17. /// </summary>
  18. internal class GameObjectUndo
  19. {
  20. /// <summary>
  21. /// Contains information about a component that needs its diff recorded.
  22. /// </summary>
  23. private struct ComponentToRecord
  24. {
  25. private Component obj;
  26. private string path;
  27. private SerializedObject orgState;
  28. /// <summary>
  29. /// Creates a new object instance, recording the current state of the component.
  30. /// </summary>
  31. /// <param name="obj">Component to record the state of.</param>
  32. /// <param name="path">
  33. /// Path to the field which should be focused when performing the undo/redo operation. This should be the path
  34. /// as provided by <see cref="InspectableField"/>.
  35. /// </param>
  36. internal ComponentToRecord(Component obj, string path)
  37. {
  38. this.obj = obj;
  39. this.path = path;
  40. orgState = SerializedObject.Create(obj);
  41. }
  42. /// <summary>
  43. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  44. /// an undo command is recorded.
  45. /// </summary>
  46. internal void RecordCommand()
  47. {
  48. if (obj.IsDestroyed)
  49. return;
  50. SerializedObject newState = SerializedObject.Create(obj);
  51. SerializedDiff oldToNew = SerializedDiff.Create(orgState, newState);
  52. if (oldToNew == null || oldToNew.IsEmpty)
  53. return;
  54. SerializedDiff newToOld = SerializedDiff.Create(newState, orgState);
  55. UndoRedo.Global.RegisterCommand(new RecordComponentUndo(obj, path, oldToNew, newToOld));
  56. }
  57. }
  58. /// <summary>
  59. /// Contains information about scene objects that needs their diff recorded. Note this will not record the entire
  60. /// scene object, but rather just its name, transform, active state and potentially other similar properties.
  61. /// It's components as well as hierarchy state are ignored.
  62. /// </summary>
  63. private struct SceneObjectHeaderToRecord
  64. {
  65. private SceneObject[] objs;
  66. private string path;
  67. private SceneObjectState[] orgStates;
  68. /// <summary>
  69. /// Creates a new object instance, recording the current state of the scene object header.
  70. /// </summary>
  71. /// <param name="obj">Scene object to record the state of.</param>
  72. /// <param name="path">
  73. /// Path to the field which should be focused when performing the undo/redo operation.
  74. /// </param>
  75. internal SceneObjectHeaderToRecord(SceneObject obj, string path)
  76. {
  77. this.objs = new [] { obj };
  78. this.path = path;
  79. orgStates = new [] { SceneObjectState.Create(obj) };
  80. }
  81. /// <summary>
  82. /// Creates a new object instance, recording the current state of the scene object header for multiple scene
  83. /// objects.
  84. /// </summary>
  85. /// <param name="objs">Scene objects to record the state of.</param>
  86. /// <param name="path">
  87. /// Path to the field which should be focused when performing the undo/redo operation.
  88. /// </param>
  89. internal SceneObjectHeaderToRecord(SceneObject[] objs, string path)
  90. {
  91. this.objs = objs;
  92. this.path = path;
  93. orgStates = new SceneObjectState[objs.Length];
  94. for(int i = 0; i < orgStates.Length; i++)
  95. orgStates[i] = SceneObjectState.Create(objs[i]);
  96. }
  97. /// <summary>
  98. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  99. /// an undo command is recorded.
  100. /// </summary>
  101. internal void RecordCommand()
  102. {
  103. if (objs == null)
  104. return;
  105. List<SceneObjectHeaderUndo> headers = new List<SceneObjectHeaderUndo>();
  106. for (int i = 0; i < objs.Length; i++)
  107. {
  108. SceneObject obj = objs[i];
  109. SceneObjectState orgState = orgStates[i];
  110. if (obj.IsDestroyed)
  111. continue;
  112. SceneObjectDiff oldToNew = SceneObjectDiff.Create(orgState, SceneObjectState.Create(obj));
  113. if (oldToNew.flags == 0)
  114. continue;
  115. SceneObjectDiff newToOld = SceneObjectDiff.Create(SceneObjectState.Create(obj), orgState);
  116. headers.Add(new SceneObjectHeaderUndo(obj, newToOld, oldToNew));
  117. }
  118. if (headers.Count > 0)
  119. UndoRedo.Global.RegisterCommand(new RecordSceneObjectHeaderUndo(headers, path));
  120. }
  121. }
  122. /// <summary>
  123. /// Contains information about a scene object that needs its diff recorded. Unlike
  124. /// <see cref="SceneObjectHeaderToRecord"/> this will record the entire scene object,
  125. /// including its components and optionally the child hierarchy.
  126. /// </summary>
  127. private struct SceneObjectToRecord
  128. {
  129. private SceneObject obj;
  130. private string description;
  131. private SerializedSceneObject orgState;
  132. /// <summary>
  133. /// Creates a new object instance, recording the current state of the scene object.
  134. /// </summary>
  135. /// <param name="obj">Scene object to record the state of.</param>
  136. /// <param name="hierarchy">If true, the child scene objects will be recorded as well.</param>
  137. /// <param name="description">
  138. /// Optional description that describes the change that is happening.
  139. /// </param>
  140. internal SceneObjectToRecord(SceneObject obj, bool hierarchy, string description)
  141. {
  142. this.obj = obj;
  143. this.description = description;
  144. orgState = new SerializedSceneObject(obj, hierarchy);
  145. }
  146. /// <summary>
  147. /// Generates the diff from the previously recorded state and the current state. If there is a difference
  148. /// an undo command is recorded.
  149. /// </summary>
  150. internal void RecordCommand()
  151. {
  152. if (obj.IsDestroyed)
  153. return;
  154. var newState = new SerializedSceneObject(obj);
  155. UndoRedo.Global.RegisterCommand(new RecordSceneObjectUndo(obj, orgState, newState, description));
  156. }
  157. }
  158. /// <summary>
  159. /// Contains information about a newly created scene object and its state immediately following the creation.
  160. /// </summary>
  161. private struct NewSceneObjectToRecord
  162. {
  163. private SceneObject obj;
  164. /// <summary>
  165. /// Creates a new object instance, recording that a new scene object has been created.
  166. /// </summary>
  167. /// <param name="obj">Newly created scene object.</param>
  168. internal NewSceneObjectToRecord(SceneObject obj)
  169. {
  170. this.obj = obj;
  171. }
  172. /// <summary>
  173. /// Records the current scene object state.
  174. /// </summary>
  175. internal void RecordCommand()
  176. {
  177. if (obj.IsDestroyed)
  178. return;
  179. var state = new SerializedSceneObject(obj);
  180. UndoRedo.Global.RegisterCommand(new NewSceneObjectUndo(obj, state));
  181. }
  182. }
  183. private static List<ComponentToRecord> components = new List<ComponentToRecord>();
  184. private static List<SceneObjectHeaderToRecord> sceneObjectHeaders = new List<SceneObjectHeaderToRecord>();
  185. private static List<SceneObjectToRecord> sceneObjects = new List<SceneObjectToRecord>();
  186. private static List<NewSceneObjectToRecord> newSceneObjects = new List<NewSceneObjectToRecord>();
  187. /// <summary>
  188. /// Records the current state of the provided component, and generates a diff with the next state at the end of the
  189. /// frame. If change is detected an undo operation will be recorded. Generally you want to call this just before
  190. /// you are about to make a change to the component.
  191. /// </summary>
  192. /// <param name="obj">Component to record the state of.</param>
  193. /// <param name="fieldPath">
  194. /// Path to the field which should be focused when performing the undo/redo operation. This should be the path
  195. /// as provided by <see cref="InspectableField"/>.
  196. /// </param>
  197. public static void RecordComponent(Component obj, string fieldPath)
  198. {
  199. ComponentToRecord cmp = new ComponentToRecord(obj, fieldPath);
  200. components.Add(cmp);
  201. }
  202. /// <summary>
  203. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  204. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  205. /// just before you are about to make a change to the scene object header.
  206. ///
  207. /// Note this will not record the entire scene object, but rather just its name, transform, active state and
  208. /// potentially other similar properties. It's components as well as hierarchy state are ignored.
  209. /// </summary>
  210. /// <param name="obj">Scene object to record the state of.</param>
  211. /// <param name="fieldName">
  212. /// Name to the field which should be focused when performing the undo/redo operation.
  213. /// </param>
  214. public static void RecordSceneObjectHeader(SceneObject obj, string fieldName)
  215. {
  216. SceneObjectHeaderToRecord so = new SceneObjectHeaderToRecord(obj, fieldName);
  217. sceneObjectHeaders.Add(so);
  218. }
  219. /// <summary>
  220. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  221. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  222. /// just before you are about to make a change to the scene object header.
  223. ///
  224. /// Note this will not record the entire scene object, but rather just its name, transform, active state and
  225. /// potentially other similar properties. It's components as well as hierarchy state are ignored.
  226. /// </summary>
  227. /// <param name="objs">Scene objects to record the state of.</param>
  228. public static void RecordSceneObjectHeader(SceneObject[] objs)
  229. {
  230. SceneObjectHeaderToRecord so = new SceneObjectHeaderToRecord(objs, null);
  231. sceneObjectHeaders.Add(so);
  232. }
  233. /// <summary>
  234. /// Records the current state of the provided scene object header, and generates a diff with the next state at the
  235. /// end of the frame. If change is detected an undo operation will be recorded. Generally you want to call this
  236. /// just before you are about to make a change to the scene object.
  237. ///
  238. /// Note this records the complete state of a scene object, including its header, its components and optionally its
  239. /// child hierarchy.
  240. /// </summary>
  241. /// <param name="obj">Scene object to record.</param>
  242. /// <param name="hierarchy">
  243. /// If true the child objects will be recorded as well, otherwise just the provided object.
  244. /// </param>
  245. /// <param name="description">Optional description specifying the type of changes about to be made.</param>
  246. public static void RecordSceneObject(SceneObject obj, bool hierarchy, string description)
  247. {
  248. SceneObjectToRecord so = new SceneObjectToRecord(obj, hierarchy, description);
  249. sceneObjects.Add(so);
  250. }
  251. /// <summary>
  252. /// Creates a brand new scene object and begins <see cref="RecordSceneObject(SceneObject,bool,string)"/> operation
  253. /// on the newly created scene object. Both the creation and the initially recorded set of data will be recorded
  254. /// as a single undo/redo command. Generally you want to call this when creating a new scene object and immediately
  255. /// make some initial changes to it (such as adding component or modifying their properties).
  256. /// </summary>
  257. /// <param name="obj">Newly created scene object on which to initiate the record operation on.</param>
  258. public static void RecordNewSceneObject(SceneObject obj)
  259. {
  260. if (obj == null || obj.IsDestroyed)
  261. return;
  262. NewSceneObjectToRecord record = new NewSceneObjectToRecord(obj);
  263. newSceneObjects.Add(record);
  264. Selection.SceneObject = obj;
  265. }
  266. /// <summary>
  267. /// Generates diffs for any objects that were previously recorded using any of the Record* methods. The diff is
  268. /// generated by comparing the state at the time Record* was called, compared to the current object state.
  269. /// </summary>
  270. public static void ResolveDiffs()
  271. {
  272. foreach (var entry in components)
  273. entry.RecordCommand();
  274. foreach (var entry in sceneObjectHeaders)
  275. entry.RecordCommand();
  276. foreach (var entry in sceneObjects)
  277. entry.RecordCommand();
  278. foreach (var entry in newSceneObjects)
  279. entry.RecordCommand();
  280. components.Clear();
  281. sceneObjectHeaders.Clear();
  282. sceneObjects.Clear();
  283. newSceneObjects.Clear();
  284. }
  285. }
  286. /// <summary>
  287. /// Contains information about scene object state, excluding information about its components and hierarchy.
  288. /// </summary>
  289. internal struct SceneObjectState
  290. {
  291. internal string name;
  292. internal Vector3 position;
  293. internal Quaternion rotation;
  294. internal Vector3 scale;
  295. internal bool active;
  296. /// <summary>
  297. /// Initializes the state from a scene object.
  298. /// </summary>
  299. /// <param name="so">Scene object to initialize the state from.</param>
  300. /// <returns>New state object.</returns>
  301. internal static SceneObjectState Create(SceneObject so)
  302. {
  303. SceneObjectState state = new SceneObjectState();
  304. state.name = so.Name;
  305. state.position = so.LocalPosition;
  306. state.rotation = so.LocalRotation;
  307. state.scale = so.LocalScale;
  308. state.active = so.Active;
  309. return state;
  310. }
  311. }
  312. /// <summary>
  313. /// Contains the difference between two <see cref="SceneObjectState"/> objects and allows the changes to be applied to
  314. /// a <see cref="SceneObject"/>. The value of the different fields is stored as its own state, while the flags field
  315. /// specified which of the properties is actually different.
  316. /// </summary>
  317. internal struct SceneObjectDiff
  318. {
  319. internal SceneObjectState state;
  320. internal SceneObjectDiffFlags flags;
  321. /// <summary>
  322. /// Creates a diff object storing the difference between two <see cref="SceneObjectState"/> objects.
  323. /// </summary>
  324. /// <param name="oldState">State of the scene object to compare from.</param>
  325. /// <param name="newState">State of the scene object to compare to.</param>
  326. /// <returns>Difference between the two scene object states.</returns>
  327. internal static SceneObjectDiff Create(SceneObjectState oldState, SceneObjectState newState)
  328. {
  329. SceneObjectDiff diff = new SceneObjectDiff();
  330. diff.state = new SceneObjectState();
  331. if (oldState.name != newState.name)
  332. {
  333. diff.state.name = newState.name;
  334. diff.flags |= SceneObjectDiffFlags.Name;
  335. }
  336. if (oldState.position != newState.position)
  337. {
  338. diff.state.position = newState.position;
  339. diff.flags |= SceneObjectDiffFlags.Position;
  340. }
  341. if (oldState.rotation != newState.rotation)
  342. {
  343. diff.state.rotation = newState.rotation;
  344. diff.flags |= SceneObjectDiffFlags.Rotation;
  345. }
  346. if (oldState.scale != newState.scale)
  347. {
  348. diff.state.scale = newState.scale;
  349. diff.flags |= SceneObjectDiffFlags.Scale;
  350. }
  351. if (oldState.active != newState.active)
  352. {
  353. diff.state.active = newState.active;
  354. diff.flags |= SceneObjectDiffFlags.Active;
  355. }
  356. return diff;
  357. }
  358. /// <summary>
  359. /// Applies the diff to an actual scene object.
  360. /// </summary>
  361. /// <param name="sceneObject">Scene object to apply the diff to.</param>
  362. internal void Apply(SceneObject sceneObject)
  363. {
  364. if (flags.HasFlag(SceneObjectDiffFlags.Name))
  365. sceneObject.Name = state.name;
  366. if (flags.HasFlag(SceneObjectDiffFlags.Position))
  367. sceneObject.LocalPosition = state.position;
  368. if (flags.HasFlag(SceneObjectDiffFlags.Rotation))
  369. sceneObject.LocalRotation = state.rotation;
  370. if (flags.HasFlag(SceneObjectDiffFlags.Scale))
  371. sceneObject.LocalScale = state.scale;
  372. if (flags.HasFlag(SceneObjectDiffFlags.Active))
  373. sceneObject.Active = state.active;
  374. }
  375. }
  376. /// <summary>
  377. /// Contains information about two separate states of a scene object header.
  378. /// </summary>
  379. internal struct SceneObjectHeaderUndo
  380. {
  381. internal SceneObject obj;
  382. internal SceneObjectDiff newToOld;
  383. internal SceneObjectDiff oldToNew;
  384. internal SceneObjectHeaderUndo(SceneObject obj, SceneObjectDiff newToOld, SceneObjectDiff oldToNew)
  385. {
  386. this.obj = obj;
  387. this.newToOld = newToOld;
  388. this.oldToNew = oldToNew;
  389. }
  390. }
  391. /// <summary>
  392. /// Stores the field changes in a <see cref="SceneObject"/> as a difference between two states. Allows those changes to
  393. /// be reverted and re-applied. Does not record changes to scene object components or hierarchy, but just the fields
  394. /// considered its header (such as name, local transform and active state).
  395. /// </summary>
  396. [SerializeObject]
  397. internal class RecordSceneObjectHeaderUndo : UndoableCommand
  398. {
  399. private string fieldPath;
  400. private List<SceneObjectHeaderUndo> headers;
  401. private RecordSceneObjectHeaderUndo() { }
  402. /// <summary>
  403. /// Creates the new scene object header undo command.
  404. /// </summary>
  405. /// <param name="headers">Information about recorded states of scene object headers.</param>
  406. /// <param name="fieldPath">
  407. /// Optional path that controls which is the field being modified and should receive input focus when the command
  408. /// is executed. Note that the diffs applied have no restriction on how many fields they can modify at once, but
  409. /// only one field will receive focus.</param>
  410. public RecordSceneObjectHeaderUndo(List<SceneObjectHeaderUndo> headers, string fieldPath)
  411. {
  412. this.headers = headers;
  413. this.fieldPath = fieldPath;
  414. }
  415. /// <inheritdoc/>
  416. protected override void Commit()
  417. {
  418. if (headers == null)
  419. return;
  420. foreach (var header in headers)
  421. {
  422. if (header.obj == null)
  423. continue;
  424. if (header.obj.IsDestroyed)
  425. {
  426. Debug.LogWarning("Attempting to commit state on a destroyed game-object.");
  427. continue;
  428. }
  429. header.oldToNew.Apply(header.obj);
  430. }
  431. FocusOnField();
  432. RefreshInspector();
  433. }
  434. /// <inheritdoc/>
  435. protected override void Revert()
  436. {
  437. if (headers == null)
  438. return;
  439. foreach (var header in headers)
  440. {
  441. if (header.obj == null)
  442. continue;
  443. if (header.obj.IsDestroyed)
  444. {
  445. Debug.LogWarning("Attempting to revert state on a destroyed game-object.");
  446. continue;
  447. }
  448. header.newToOld.Apply(header.obj);
  449. }
  450. FocusOnField();
  451. RefreshInspector();
  452. }
  453. /// <summary>
  454. /// Selects the component's scene object and focuses on the specific field in the inspector, if the inspector
  455. /// window is open.
  456. /// </summary>
  457. private void FocusOnField()
  458. {
  459. if (headers != null && headers.Count > 0)
  460. {
  461. if (headers.Count == 1)
  462. {
  463. if (Selection.SceneObject != headers[0].obj)
  464. Selection.SceneObject = headers[0].obj;
  465. }
  466. else
  467. {
  468. List<SceneObject> objs = new List<SceneObject>();
  469. foreach (var header in headers)
  470. {
  471. if(header.obj != null)
  472. objs.Add(header.obj);
  473. }
  474. Selection.SceneObjects = objs.ToArray();
  475. }
  476. if (!string.IsNullOrEmpty(fieldPath))
  477. {
  478. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  479. inspectorWindow?.FocusOnField(headers[0].obj.UUID, fieldPath);
  480. }
  481. }
  482. }
  483. /// <summary>
  484. /// Updates the values of the fields displayed in the inspector window.
  485. /// </summary>
  486. private void RefreshInspector()
  487. {
  488. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  489. inspectorWindow?.RefreshSceneObjectFields(true);
  490. }
  491. }
  492. /// <summary>
  493. /// Records the creation of a new <see cref="SceneObject"/> and its recorded state immediately following its creation.
  494. /// Undo will destroy the created object, while redo will restore the object with the recorded state.
  495. /// </summary>
  496. [SerializeObject]
  497. internal class NewSceneObjectUndo : UndoableCommand
  498. {
  499. private SceneObject obj;
  500. private SerializedSceneObject state;
  501. private NewSceneObjectUndo() { }
  502. /// <summary>
  503. /// Creates the new scene object undo command.
  504. /// </summary>
  505. /// <param name="obj">Newly created scene object on which to apply the command.</param>
  506. /// <param name="state">Recorded state of the scene object.</param>
  507. public NewSceneObjectUndo(SceneObject obj, SerializedSceneObject state)
  508. {
  509. this.obj = obj;
  510. this.state = state;
  511. }
  512. /// <inheritdoc/>
  513. protected override void Commit()
  514. {
  515. state?.Restore();
  516. if(obj != null && !obj.IsDestroyed)
  517. Selection.SceneObject = obj;
  518. FocusOnObject();
  519. RefreshInspector();
  520. }
  521. /// <inheritdoc/>
  522. protected override void Revert()
  523. {
  524. obj.Destroy(true);
  525. }
  526. /// <summary>
  527. /// Selects the scene object if not already selected.
  528. /// </summary>
  529. private void FocusOnObject()
  530. {
  531. if (obj != null && !obj.IsDestroyed)
  532. {
  533. if (Selection.SceneObject != obj)
  534. Selection.SceneObject = obj;
  535. }
  536. }
  537. /// <summary>
  538. /// Updates the values of the fields displayed in the inspector window.
  539. /// </summary>
  540. private void RefreshInspector()
  541. {
  542. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  543. inspectorWindow?.RefreshSceneObjectFields(true);
  544. }
  545. }
  546. /// <summary>
  547. /// Stores the field changes in a <see cref="SceneObject"/> as a difference between two states. Allows those changes to
  548. /// be reverted and re-applied. Unlike <see cref="bs.Editor.RecordSceneObjectHeaderUndo"/> this command records the
  549. /// full scene object state, including changes to its components and optionally any child objects.
  550. /// </summary>
  551. [SerializeObject]
  552. internal class RecordSceneObjectUndo : UndoableCommand
  553. {
  554. private SceneObject obj;
  555. private SerializedSceneObject oldState;
  556. private SerializedSceneObject newState;
  557. private RecordSceneObjectUndo() { }
  558. /// <summary>
  559. /// Creates the new scene object undo command.
  560. /// </summary>
  561. /// <param name="obj">Scene object on which to apply the performed changes.</param>
  562. /// <param name="oldState">Recorded original state of the scene object(s).</param>
  563. /// <param name="newState">Recorded new state of the scene object(s).</param>
  564. /// <param name="description">
  565. /// Optional description of the changes made to the scene object between the two states.
  566. /// </param>
  567. public RecordSceneObjectUndo(SceneObject obj, SerializedSceneObject oldState, SerializedSceneObject newState,
  568. string description)
  569. {
  570. this.obj = obj;
  571. this.oldState = oldState;
  572. this.newState = newState;
  573. }
  574. /// <inheritdoc/>
  575. protected override void Commit()
  576. {
  577. newState?.Restore();
  578. FocusOnObject();
  579. RefreshInspector();
  580. }
  581. /// <inheritdoc/>
  582. protected override void Revert()
  583. {
  584. oldState?.Restore();
  585. FocusOnObject();
  586. RefreshInspector();
  587. }
  588. /// <summary>
  589. /// Selects the scene object if not already selected.
  590. /// </summary>
  591. private void FocusOnObject()
  592. {
  593. if (obj != null)
  594. {
  595. if (Selection.SceneObject != obj)
  596. Selection.SceneObject = obj;
  597. }
  598. }
  599. /// <summary>
  600. /// Updates the values of the fields displayed in the inspector window.
  601. /// </summary>
  602. private void RefreshInspector()
  603. {
  604. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  605. inspectorWindow?.RefreshSceneObjectFields(true);
  606. }
  607. }
  608. /// <summary>
  609. /// Stores the field changes in a <see cref="Component"/> as a difference between two states. Allows those changes to
  610. /// be reverted and re-applied.
  611. /// </summary>
  612. [SerializeObject]
  613. internal class RecordComponentUndo : UndoableCommand
  614. {
  615. private Component obj;
  616. private string fieldPath;
  617. private SerializedDiff newToOld;
  618. private SerializedDiff oldToNew;
  619. private RecordComponentUndo() { }
  620. /// <summary>
  621. /// Creates the new component undo command.
  622. /// </summary>
  623. /// <param name="obj">Component on which to apply the performed changes.</param>
  624. /// <param name="fieldPath">
  625. /// Optional path that controls which is the field being modified and should receive input focus when the command
  626. /// is executed. Note that the diffs applied have no restriction on how many fields they can modify at once, but
  627. /// only one field will receive focus.</param>
  628. /// <param name="oldToNew">
  629. /// Difference that can be applied to the old object in order to get the new object state.
  630. /// </param>
  631. /// <param name="newToOld">
  632. /// Difference that can be applied to the new object in order to get the old object state.
  633. /// </param>
  634. public RecordComponentUndo(Component obj, string fieldPath, SerializedDiff oldToNew, SerializedDiff newToOld)
  635. {
  636. this.obj = obj;
  637. this.fieldPath = fieldPath;
  638. this.oldToNew = oldToNew;
  639. this.newToOld = newToOld;
  640. }
  641. /// <inheritdoc/>
  642. protected override void Commit()
  643. {
  644. if (oldToNew == null || obj == null)
  645. return;
  646. if (obj.IsDestroyed)
  647. {
  648. Debug.LogWarning("Attempting to commit state on a destroyed game-object.");
  649. return;
  650. }
  651. oldToNew.Apply(obj);
  652. FocusOnField();
  653. RefreshInspector();
  654. }
  655. /// <inheritdoc/>
  656. protected override void Revert()
  657. {
  658. if (newToOld == null || obj == null)
  659. return;
  660. if (obj.IsDestroyed)
  661. {
  662. Debug.LogWarning("Attempting to revert state on a destroyed game-object.");
  663. return;
  664. }
  665. newToOld.Apply(obj);
  666. FocusOnField();
  667. RefreshInspector();
  668. }
  669. /// <summary>
  670. /// Selects the component's scene object and focuses on the specific field in the inspector, if the inspector
  671. /// window is open.
  672. /// </summary>
  673. private void FocusOnField()
  674. {
  675. SceneObject so = obj.SceneObject;
  676. if (so != null)
  677. {
  678. if (Selection.SceneObject != so)
  679. Selection.SceneObject = so;
  680. if (!string.IsNullOrEmpty(fieldPath))
  681. {
  682. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  683. inspectorWindow?.FocusOnField(obj.UUID, fieldPath);
  684. }
  685. }
  686. }
  687. /// <summary>
  688. /// Updates the values of the fields displayed in the inspector window.
  689. /// </summary>
  690. private void RefreshInspector()
  691. {
  692. InspectorWindow inspectorWindow = EditorWindow.GetWindow<InspectorWindow>();
  693. inspectorWindow?.RefreshComponentFields(obj);
  694. }
  695. }
  696. /** @} */
  697. }