datablockEditorUndo.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //---------------------------------------------------------------------------------------------
  23. function DatablockEditorPlugin::createUndo( %this, %class, %desc )
  24. {
  25. pushInstantGroup();
  26. %action = new UndoScriptAction()
  27. {
  28. class = %class;
  29. superClass = BaseDatablockEdAction;
  30. actionName = %desc;
  31. editor = DatablockEditorPlugin;
  32. treeview = DatablockEditorTree;
  33. inspector = DatablockEditorInspector;
  34. };
  35. popInstantGroup();
  36. return %action;
  37. }
  38. //---------------------------------------------------------------------------------------------
  39. function DatablockEditorPlugin::submitUndo( %this, %action )
  40. {
  41. %action.addToManager( Editor.getUndoManager() );
  42. }
  43. //=============================================================================================
  44. // BaseDatablockEdAction.
  45. //=============================================================================================
  46. //---------------------------------------------------------------------------------------------
  47. function BaseDatablockEdAction::redo( %this )
  48. {
  49. }
  50. //---------------------------------------------------------------------------------------------
  51. function BaseDatablockEdAction::undo( %this )
  52. {
  53. }
  54. //=============================================================================================
  55. // ActionCreateDatablock.
  56. //=============================================================================================
  57. //---------------------------------------------------------------------------------------------
  58. function ActionCreateDatablock::redo( %this )
  59. {
  60. %db = %this.db;
  61. %db.name = %this.dbName;
  62. %this.editor.PM.setDirty( %db, %this.fname );
  63. %this.editor.addExistingItem( %db );
  64. %this.editor.selectDatablock( %db );
  65. %this.editor.flagInspectorAsDirty( true );
  66. UnlistedDatablocks.remove( %id );
  67. }
  68. //---------------------------------------------------------------------------------------------
  69. function ActionCreateDatablock::undo( %this )
  70. {
  71. %db = %this.db;
  72. %itemId = %this.treeview.findItemByName( %db.name );
  73. if( !%itemId )
  74. %itemId = %this.treeview.findItemByName( %db.name @ " *" );
  75. %this.treeview.removeItem( %itemId );
  76. %this.editor.resetSelectedDatablock();
  77. %this.editor.PM.removeDirty( %db );
  78. %this.dbName = %db.name;
  79. %db.name = "";
  80. UnlistedDatablocks.add( %this.db );
  81. }
  82. //=============================================================================================
  83. // ActionDeleteDatablock.
  84. //=============================================================================================
  85. //---------------------------------------------------------------------------------------------
  86. function ActionDeleteDatablock::redo( %this )
  87. {
  88. %db = %this.db;
  89. %itemId = %this.treeview.findItemByName( %db.name );
  90. if( !%itemId )
  91. %itemId = %this.treeview.findItemByName( %db.name @ " *" );
  92. // Remove from tree and file.
  93. %this.treeview.removeItem( %db );
  94. %this.editor.resetSelectedDatablock();
  95. if( %db.getFileName() !$= "" )
  96. %this.editor.PM.removeObjectFromFile( %db );
  97. // Unassign name.
  98. %this.dbName = %db.name;
  99. %db.name = "";
  100. // Add to unlisted.
  101. UnlistedDatablocks.add( %db );
  102. }
  103. //---------------------------------------------------------------------------------------------
  104. function ActionDeleteDatablock::undo( %this )
  105. {
  106. %db = %this.db;
  107. // Restore name.
  108. %db.name = %this.dbName;
  109. // Add to tree and select.
  110. %this.editor.addExistingItem( %db, true );
  111. %this.editor.selectDatablock( %db );
  112. // Mark as dirty.
  113. %this.editor.PM.setDirty( %db, %this.fname );
  114. %this.editor.syncDirtyState();
  115. // Remove from unlisted.
  116. UnlistedDatablocks.remove( %id );
  117. }