gcsx_undo.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. /* GCSx
  2. ** UNDO.CPP
  3. **
  4. ** Editor undo and redo functionality
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. int UndoBuffer::undoHeaderSize = (sizeof(UndoBuffer::UndoHeader) + sizeof(Uint32) - 1) / sizeof(Uint32); // Size in Uint32s
  25. #ifndef NDEBUG
  26. int UndoBuffer::undoThresh = -1;
  27. void UndoBuffer::setUndoThreshold(int level) { start_func
  28. if (level) undoThresh = level - 1;
  29. else undoThresh = -1;
  30. }
  31. #endif
  32. UndoBuffer::UndoBuffer(WorldEdit* myWorld) { start_func
  33. world = myWorld;
  34. requestedSize = UNDO_DEFAULT_BUFFER;
  35. undoSizeAlloc = 0;
  36. undoBuffer = NULL;
  37. undoPos = 0;
  38. inUndo = 0;
  39. suspendUndo = 0;
  40. undoBlock = 0;
  41. undoBlockDidFirst = 0;
  42. undoBlockFirstPos = 0;
  43. undoNonUndoable = 0;
  44. }
  45. UndoBuffer::~UndoBuffer() { start_func
  46. // Delete allocated objects etc.
  47. clearUndo();
  48. }
  49. void UndoBuffer::disableUndo() { start_func
  50. suspendUndo = 1;
  51. }
  52. void UndoBuffer::enableUndo() { start_func
  53. suspendUndo = 0;
  54. }
  55. int UndoBuffer::bufferSize(int requested) { start_func
  56. // Check range
  57. if (requested < 0) requested = 0;
  58. if (requested > UNDO_MAX_BUFFER) requested = UNDO_MAX_BUFFER;
  59. if (requested != requestedSize) {
  60. clearUndo();
  61. requestedSize = requested;
  62. }
  63. return requestedSize;
  64. }
  65. Uint32* UndoBuffer::createUndoSpace(UndoType chunkType, int id, int subid, int xPos, int yPos, int xSize, int ySize, Window* srcWin, Uint32 data1, Uint32 data2, Uint32 data3, void* item1, void* item2, void* item3) throw_Undo { start_func
  66. Uint32* result;
  67. UndoHeader* header;
  68. // If in an undo, "cancel" any further undos by returning NULL
  69. if (inUndo) return NULL;
  70. // If in a block, cancel further undos if user was already warned
  71. if ((undoBlock) && (undoNonUndoable)) return NULL;
  72. // Cancel if undo has been explicitly disabled
  73. if (!requestedSize) return NULL;
  74. if (suspendUndo) {
  75. clearUndo();
  76. return NULL;
  77. }
  78. #ifndef NDEBUG
  79. // Threshold enabled?
  80. if (undoThresh == 0) {
  81. warnUser();
  82. return NULL;
  83. }
  84. if (undoThresh > 0) --undoThresh;
  85. #endif
  86. if (undoBuffer == NULL) {
  87. // 256 = number of Uint32s in 1k
  88. undoBuffer = new(nothrow) Uint32[requestedSize * 256];
  89. if (undoBuffer == NULL) {
  90. warnUser();
  91. return NULL;
  92. }
  93. undoSizeAlloc = requestedSize * 256;
  94. undoPos = 0;
  95. header = (UndoHeader*)undoBuffer;
  96. header->sourceWindow = 0;
  97. header->prevChunkSize = 0;
  98. header->type = UNDO_EMPTY;
  99. header->id = 0;
  100. header->xSize = 0;
  101. header->ySize = 0;
  102. }
  103. // Too big?
  104. if (xSize * ySize + undoHeaderSize * 2 >= undoSizeAlloc) {
  105. warnUser();
  106. return NULL;
  107. }
  108. // Too big to fit with the rest of our merged data?
  109. if ((undoBlock) && (undoBlockDidFirst) && (xSize * ySize + undoHeaderSize * 2 + undoPos - undoBlockFirstPos >= undoSizeAlloc)) {
  110. warnUser();
  111. return NULL;
  112. }
  113. // If we add at undoPos, we need to fit there the entire new chunk,
  114. // plus header, plus header of empty chunk; find out how much needs to be deleted
  115. int deletingAt = 0;
  116. while (xSize * ySize + undoHeaderSize * 2 + undoPos - deletingAt > undoSizeAlloc) {
  117. int isMerged = 1;
  118. // Delete any merged chunks as well
  119. while (isMerged) {
  120. // Delete first item in list, retry
  121. header = (UndoHeader*)(undoBuffer + deletingAt);
  122. deleteUndoBlock(deletingAt);
  123. // Move down to delete initial chunk
  124. deletingAt += header->xSize * header->ySize + undoHeaderSize;
  125. // New initial chunk's "previous size" is now 0
  126. header = (UndoHeader*)(undoBuffer + deletingAt);
  127. header->prevChunkSize = 0;
  128. // Is next block merged with the one we just did?
  129. isMerged = header->type & UNDO_MERGE;
  130. }
  131. }
  132. // Move data to finalize deletion
  133. if (deletingAt) {
  134. memmove(undoBuffer, undoBuffer + deletingAt, (undoPos + undoHeaderSize - deletingAt) * 4);
  135. undoPos -= deletingAt;
  136. }
  137. // Delete any redo chunks here or later
  138. clearRedo();
  139. // There's now room for the new chunk- create it.
  140. // Current 'blank' or redo chunk at undoPos already has proper "previous size" value
  141. // This also overwrites any existing redo- no redo allowed after an action
  142. result = undoBuffer + undoPos + undoHeaderSize;
  143. header = (UndoHeader*)(undoBuffer + undoPos);
  144. header->sourceWindow = srcWin ? srcWin->getId() : 0;
  145. header->type = chunkType;
  146. // (part of a block?)
  147. if (undoBlock) {
  148. if (undoBlockDidFirst) header->type = (UndoType)(header->type | UNDO_MERGE);
  149. else {
  150. undoBlockFirstPos = undoPos;
  151. undoBlockDidFirst = 1;
  152. }
  153. }
  154. header->id = id;
  155. header->subid = subid;
  156. header->xSize = xSize;
  157. header->ySize = ySize;
  158. header->xPos = xPos;
  159. header->yPos = yPos;
  160. header->data1 = data1;
  161. header->data2 = data2;
  162. header->data3 = data3;
  163. header->item1 = item1;
  164. header->item2 = item2;
  165. header->item3 = item3;
  166. // Create new 'blank' chunk
  167. undoPos += xSize * ySize + undoHeaderSize;
  168. header = (UndoHeader*)(undoBuffer + undoPos);
  169. header->sourceWindow = 0;
  170. header->prevChunkSize = xSize * ySize + undoHeaderSize;
  171. header->type = UNDO_EMPTY;
  172. header->id = 0;
  173. header->xSize = 0;
  174. header->ySize = 0;
  175. // Done
  176. return result;
  177. }
  178. void UndoBuffer::preUndoBlock() { start_func
  179. if ((inUndo) || (suspendUndo)) return;
  180. if (!undoBlock) {
  181. undoBlockDidFirst = 0;
  182. undoBlockFirstPos = 0;
  183. undoNonUndoable = 0;
  184. }
  185. ++undoBlock;
  186. debugWrite(DEBUG_UNDO, "undo: pre-block (%d)", undoBlock);
  187. }
  188. void UndoBuffer::postUndoBlock() { start_func
  189. if ((inUndo) || (suspendUndo)) return;
  190. // Could already be at zero if blocks were nested and the inner one
  191. // threw an exception
  192. if (undoBlock) {
  193. debugWrite(DEBUG_UNDO, "undo: post-block (%d)", undoBlock);
  194. --undoBlock;
  195. }
  196. }
  197. void UndoBuffer::cancelLastUndo() { start_func
  198. UndoHeader* header = (UndoHeader*)(undoBuffer + undoPos);
  199. header->type = (UndoType)(header->type & ~UNDO_MERGE);
  200. undoPos -= header->prevChunkSize;
  201. clearRedo();
  202. }
  203. void UndoBuffer::cancelUndoBlock() { start_func
  204. // Must be in a block
  205. if (undoBlock) {
  206. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: cancel block (%d)", undoBlock);
  207. // (no longer in a block)
  208. undoBlock = 0;
  209. if (undoBlockDidFirst) {
  210. undoPerform();
  211. clearRedo();
  212. }
  213. }
  214. }
  215. void UndoBuffer::storeUndoLayerTile(int layerId, int x, int y, int w, int h, Window* srcWin) throw_Undo { start_func
  216. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer tiles");
  217. // Find layer
  218. LayerEdit* layer = world->findLayer(layerId);
  219. assert(layer);
  220. assert(layer->getType() == Layer::LAYER_TILE);
  221. int layerWidth = layer->getXSize();
  222. int layerHeight = layer->getYSize();
  223. // Bound
  224. if ((x >= layerWidth) || (y >= layerHeight) || (x + w <= 0) || (y + h <= 0)) return;
  225. if (x < 0) {
  226. w += x;
  227. x = 0;
  228. }
  229. if (y < 0) {
  230. h += y;
  231. y = 0;
  232. }
  233. if (x + w > layerWidth) w = layerWidth - x;
  234. if (y + h > layerHeight) h = layerHeight - y;
  235. // Fx/Coll present?
  236. int ext = layer->getUsesExt();
  237. int fx = layer->getUsesFx();
  238. if ((fx) || (ext)) preUndoBlock();
  239. Uint32* bufferExt = NULL;
  240. Uint32* bufferFx = NULL;
  241. Uint32* buffer = createUndoSpace(UNDO_LAYERTILE, layerId, 0, x, y, w, h, srcWin);
  242. if (!buffer) {
  243. if ((fx) || (ext)) postUndoBlock();
  244. return;
  245. }
  246. if (ext) {
  247. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer extended data");
  248. bufferExt = createUndoSpace(UNDO_LAYERTILEEXT, layerId, 0, x, y, w, h, srcWin);
  249. if (!bufferExt) {
  250. postUndoBlock();
  251. return;
  252. }
  253. }
  254. if (fx) {
  255. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer effects");
  256. bufferFx = createUndoSpace(UNDO_LAYERTILEFX, layerId, 0, x, y, w, h, srcWin);
  257. if (!bufferFx) {
  258. postUndoBlock();
  259. return;
  260. }
  261. }
  262. Rect rect = { x, y, w, h };
  263. layer->loadLayer(buffer, bufferExt, bufferFx, w, rect);
  264. if ((fx) || (ext)) postUndoBlock();
  265. }
  266. void UndoBuffer::storeUndoTile(int tileSetId, int tileNum, int x, int y, int w, int h, Window* srcWin) throw_Undo { start_func
  267. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tile");
  268. // Find tileset
  269. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(tileSetId));
  270. assert(tiles);
  271. int tileWidth = tiles->getWidth();
  272. int tileHeight = tiles->getHeight();
  273. // Bound
  274. if ((x >= tileWidth) || (y >= tileHeight) || (x + w <= 0) || (y + h <= 0)) return;
  275. if (x < 0) {
  276. w += x;
  277. x = 0;
  278. }
  279. if (y < 0) {
  280. h += y;
  281. y = 0;
  282. }
  283. if (x + w > tileWidth) w = tileWidth - x;
  284. if (y + h > tileHeight) h = tileHeight - y;
  285. Uint32* buffer;
  286. buffer = createUndoSpace(UNDO_TILE, tileSetId, tileNum, x, y, w, h, srcWin);
  287. if (!buffer) return;
  288. int pitch;
  289. const Uint8* source;
  290. int lineSize = w * 4;
  291. pitch = tiles->viewTilePitch();
  292. source = tiles->viewTileData(tileNum) + y * pitch + x * 4;
  293. // @TODO: matrixCopy()
  294. for (; h > 0; --h) {
  295. memcpy(buffer, source, lineSize);
  296. buffer += w;
  297. source += pitch;
  298. }
  299. }
  300. void UndoBuffer::storeUndoColl(int tileSetId, int collNum, int x, int y, int w, int h, Window* srcWin) throw_Undo { start_func
  301. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store collision map");
  302. // Find tileset
  303. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(tileSetId));
  304. assert(tiles);
  305. int tileWidth = tiles->getWidth();
  306. int tileHeight = tiles->getHeight();
  307. // Bound
  308. if ((x >= tileWidth) || (y >= tileHeight) || (x + w <= 0) || (y + h <= 0)) return;
  309. if (x < 0) {
  310. w += x;
  311. x = 0;
  312. }
  313. if (y < 0) {
  314. h += y;
  315. y = 0;
  316. }
  317. if (x + w > tileWidth) w = tileWidth - x;
  318. if (y + h > tileHeight) h = tileHeight - y;
  319. // Everything is in chunks 32 wide
  320. if (x & 31) {
  321. w += x & 31;
  322. x -= x & 31;
  323. }
  324. x /= 32;
  325. w = (w + 31) / 32;
  326. Uint32* buffer;
  327. buffer = createUndoSpace(UNDO_TILECOLL, tileSetId, collNum, x, y, w, h, srcWin);
  328. if (!buffer) return;
  329. const Uint32* source;
  330. source = tiles->viewCollData(collNum) + w * tileHeight + y;
  331. // @TODO: matrixCopy()
  332. for (; w > 0; --w) {
  333. memcpy(buffer, source, h);
  334. buffer += h;
  335. source += tileHeight;
  336. }
  337. }
  338. void UndoBuffer::storeUndoLayerSwap(int sceneId, int layer1, int layer2, Window* srcWin) throw_Undo { start_func
  339. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer swap");
  340. createUndoSpace(UNDO_LAYERMOVE, sceneId, 0, layer1, layer2, 0, 0, srcWin);
  341. }
  342. void UndoBuffer::storeUndoTileGlyph(int tileSetId, int tileNum, int oldWidth, Window* srcWin) throw_Undo { start_func
  343. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tile glyph");
  344. createUndoSpace(UNDO_TILEGLYPH, tileSetId, tileNum, oldWidth, 0, 0, 0, srcWin);
  345. }
  346. void UndoBuffer::storeUndoLayerType(int layerId, int oldType, Window* srcWin) throw_Undo { start_func
  347. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer type");
  348. createUndoSpace(UNDO_LAYERTYPE, layerId, 0, oldType, 0, 0, 0, srcWin);
  349. }
  350. void UndoBuffer::storeUndoLayerSize(int layerId, int oldWidth, int oldHeight, Uint32* data, Uint32* dataExt, Uint32* dataFx, Window* srcWin) throw_Undo { start_func
  351. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer size");
  352. // If undo space not created, we delete saved buffers now, unless we're "in undo"
  353. if ((!createUndoSpace(UNDO_LAYERSIZE, layerId, 0, oldWidth, oldHeight, 0, 0, srcWin, 0, 0, 0, data, dataExt, dataFx)) && (!inUndo)) {
  354. delete[] data;
  355. delete[] dataExt;
  356. delete[] dataFx;
  357. }
  358. }
  359. void UndoBuffer::storeUndoLayerFx(int layerId, int oldUsesFx, Window* srcWin) throw_Undo { start_func
  360. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer effects flag");
  361. createUndoSpace(UNDO_LAYERFX, layerId, 0, oldUsesFx, 0, 0, 0, srcWin);
  362. }
  363. void UndoBuffer::storeUndoLayerExt(int layerId, int oldUsesExt, Window* srcWin) throw_Undo { start_func
  364. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer extended data flag");
  365. createUndoSpace(UNDO_LAYEREXT, layerId, 0, oldUsesExt, 0, 0, 0, srcWin);
  366. }
  367. void UndoBuffer::storeUndoLayerTileSet(int layerId, int oldTileSet, Window* srcWin) throw_Undo { start_func
  368. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer tileset");
  369. createUndoSpace(UNDO_LAYERTILESET, layerId, 0, oldTileSet, 0, 0, 0, srcWin);
  370. }
  371. void UndoBuffer::storeUndoLayerSpawnSelection(set<int>* selectionData, Window* srcWin) throw_Undo { start_func
  372. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn selection");
  373. assert(selectionData);
  374. set<int>* newData = new set<int>(*selectionData);
  375. try {
  376. Uint32* buffer = createUndoSpace(UNDO_LAYERSPAWNSEL, 0, 0, 0, 0, 0, 0, srcWin, 0, 0, 0, selectionData, newData);
  377. if (!buffer) {
  378. delete newData;
  379. return;
  380. }
  381. }
  382. catch (...) {
  383. delete newData;
  384. throw;
  385. }
  386. }
  387. void UndoBuffer::storeUndoLayerTileSelection(Uint8** selectionData, int x, int y, int w, int h, int pitch, int selectXOffs, int selectYOffs, int selectType, Window* srcWin) throw_Undo { start_func
  388. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer selection");
  389. assert(selectionData);
  390. assert((x + w) / 8 <= pitch);
  391. // place x in byte terms, round down, alter width to cover
  392. w += x % 8;
  393. x = x / 8;
  394. // break width into byte width and 32bit width
  395. int bytewidth = w / 8;
  396. if (w % 8) ++bytewidth;
  397. int width32 = w / 32;
  398. if (w % 32) ++width32;
  399. Uint32* buffer = createUndoSpace(UNDO_LAYERTILESEL, bytewidth, pitch, x, y, width32, h, srcWin, selectXOffs, selectYOffs, selectType, selectionData);
  400. if (!buffer) return;
  401. matrixCopy(*selectionData + x + y * pitch, buffer, bytewidth, h, pitch, width32 * 4);
  402. }
  403. void UndoBuffer::storeUndoLayerTileCur(Uint32 layerAffect, Uint32 layerView, Uint32 layerDim, Window* srcWin) throw_Undo { start_func
  404. createUndoSpace(UNDO_LAYERTILECUR, 0, 0, 0, 0, 0, 0, srcWin, layerAffect, layerView, layerDim);
  405. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer affected/view");
  406. }
  407. void UndoBuffer::storeUndoLayerTileTemp(Uint32** data, Uint32** dataExt, Uint32** dataFx, int x, int y, int w, int h, int pitch, Window* srcWin) throw_Undo { start_func
  408. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer floating selection");
  409. assert(data);
  410. assert(x + w <= pitch);
  411. // NULL ptr at *dataFx also means no Fx data; same for ext
  412. if (dataExt) {
  413. if (!*dataExt) dataExt = NULL;
  414. }
  415. if (dataFx) {
  416. if (!*dataFx) dataFx = NULL;
  417. }
  418. Uint32* buffer = createUndoSpace(UNDO_LAYERTILETMP, 0, pitch, x, y, w, h * (1 + (dataFx ? 1 : 0) + (dataExt ? 1 : 0)), srcWin, 0, 0, 0, data, dataExt, dataFx);
  419. if (!buffer) return;
  420. matrixCopy(*data + x + y * pitch, buffer, w * 4, h, pitch * 4, w * 4);
  421. if (dataExt) matrixCopy(*dataExt + x + y * pitch, buffer + w * h, w * 4, h, pitch * 4, w * 4);
  422. if (dataFx) matrixCopy(*dataFx + x + y * pitch, buffer + (w * h) * (dataExt ? 2 : 1), w * 4, h, pitch * 4, w * 4);
  423. }
  424. void UndoBuffer::storeUndoPaintSelection(SDL_Surface** selectionData, int x, int y, int w, int h, int selectXOffs, int selectYOffs, int selectType, Window* srcWin) throw_Undo { start_func
  425. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tile selection");
  426. assert(selectionData);
  427. int tileWidth = (*selectionData)->w;
  428. int tileHeight = (*selectionData)->h;
  429. // Bound
  430. if ((x >= tileWidth) || (y >= tileHeight) || (x + w <= 0) || (y + h <= 0)) return;
  431. if (x < 0) {
  432. w += x;
  433. x = 0;
  434. }
  435. if (y < 0) {
  436. h += y;
  437. y = 0;
  438. }
  439. if (x + w > tileWidth) w = tileWidth - x;
  440. if (y + h > tileHeight) h = tileHeight - y;
  441. Uint32* buffer = createUndoSpace(UNDO_PAINTSEL, 0, 0, x, y, w, h, srcWin, selectXOffs, selectYOffs, selectType, selectionData);
  442. if (!buffer) return;
  443. int pitch = (*selectionData)->pitch;
  444. const Uint8* source = (Uint8*)((*selectionData)->pixels) + y * pitch + x * 4;
  445. int lineSize = w * 4;
  446. // @TODO: matrixCopy()
  447. for (; h > 0; --h) {
  448. memcpy(buffer, source, lineSize);
  449. buffer += w;
  450. source += pitch;
  451. }
  452. }
  453. void UndoBuffer::storeUndoName(UndoType type, int id, const string& oldName, const string& newName, Window* srcWin) throw_Undo { start_func
  454. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store name");
  455. assert((type == UNDO_FOLDERNAME) || (type == UNDO_SPAWNNAME) || (type == UNDO_WORLDNAME) || (type == UNDO_SCENENAME) || (type == UNDO_TILENAME) || (type == UNDO_LAYERNAME) || (type == UNDO_ANIMGROUPNAME) || (type == UNDO_SCRIPTNAME));
  456. Uint32* buffer;
  457. int size = (max(oldName.size(), newName.size()) + 3) / 4;
  458. buffer = createUndoSpace(type, id, 0, oldName.size(), 0, size, 1, srcWin);
  459. if (!buffer) return;
  460. memcpy(buffer, oldName.c_str(), oldName.size());
  461. }
  462. int UndoBuffer::storeUndoLayerDelete(int id, int sceneId, int pos, LayerEdit* object, Window* srcWin) throw_Undo { start_func
  463. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer deletion");
  464. // If undo space not created, we delete object now, unless we're "in undo"
  465. if ((!createUndoSpace(UNDO_LAYERDELETE, id, sceneId, pos, 0, 0, 0, srcWin, 0, 0, 0, object)) && (!inUndo)) {
  466. return 0;
  467. }
  468. return 1;
  469. }
  470. int UndoBuffer::storeUndoLayerCreate(int id, int sceneId, int pos, Window* srcWin) throw_Undo { start_func
  471. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store layer creation");
  472. if (createUndoSpace(UNDO_LAYERCREATE, id, sceneId, pos, 0, 0, 0, srcWin)) return 1;
  473. return 0;
  474. }
  475. int UndoBuffer::storeUndoSpawnDelete(int id, int layerId, SpawnEdit* object, Window* srcWin) throw_Undo { start_func
  476. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn deletion");
  477. // If undo space not created, we delete object now, unless we're "in undo"
  478. if ((!createUndoSpace(UNDO_SPAWNDELETE, id, layerId, 0, 0, 0, 0, srcWin, 0, 0, 0, object)) && (!inUndo)) {
  479. return 0;
  480. }
  481. return 1;
  482. }
  483. int UndoBuffer::storeUndoSpawnCreate(int id, int layerId, Window* srcWin) throw_Undo { start_func
  484. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn creation");
  485. if (createUndoSpace(UNDO_SPAWNCREATE, id, layerId, 0, 0, 0, 0, srcWin)) return 1;
  486. return 0;
  487. }
  488. int UndoBuffer::storeUndoCreate(UndoType type, int id, Window* srcWin) throw_Undo { start_func
  489. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store creation");
  490. assert((type == UNDO_FOLDERCREATE) || (type == UNDO_SCENECREATE) || (type == UNDO_TILECREATE) || (type == UNDO_SCENECREATE) || (type == UNDO_ANIMGROUPCREATE) || (type == UNDO_SCRIPTCREATE));
  491. if (createUndoSpace(type, id, 0, 0, 0, 0, 0, srcWin)) return 1;
  492. return 0;
  493. }
  494. int UndoBuffer::storeUndoDelete(UndoType type, int id, void* object, Window* srcWin) throw_Undo { start_func
  495. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store deletion");
  496. assert((type == UNDO_FOLDERDELETE) || (type == UNDO_TILEDELETE) || (type == UNDO_SCENEDELETE) || (type == UNDO_ANIMGROUPDELETE) || (type == UNDO_SCRIPTDELETE));
  497. // If undo space not created, we delete object now, unless we're "in undo"
  498. if ((!createUndoSpace(type, id, 0, 0, 0, 0, 0, srcWin, 0, 0, 0, object)) && (!inUndo)) {
  499. return 0;
  500. }
  501. return 1;
  502. }
  503. void UndoBuffer::storeUndoSpawnMove(int id, int layerId, int oldX, int oldY, Window* srcWin) throw_Undo { start_func
  504. createUndoSpace(UNDO_SPAWNMOVE, id, layerId, oldX, oldY, 0, 0, srcWin);
  505. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn move");
  506. }
  507. void UndoBuffer::storeUndoSpawnSprite(int id, int layerId, int animId, int tileId, int subId, Window* srcWin) throw_Undo { start_func
  508. createUndoSpace(UNDO_SPAWNSPRITE, id, layerId, 0, 0, 0, 0, srcWin, animId, tileId, subId);
  509. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn image");
  510. }
  511. void UndoBuffer::storeUndoSpawnScript(int id, int layerId, int scriptId, Window* srcWin) throw_Undo { start_func
  512. createUndoSpace(UNDO_SPAWNSCRIPT, id, layerId, 0, 0, 0, 0, srcWin, scriptId);
  513. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store spawn script");
  514. }
  515. void UndoBuffer::storeUndoFolderAdd(int id, SaveLoad* item, int pos, Window* srcWin) throw_Undo { start_func
  516. assert(item);
  517. assert(item->getId());
  518. createUndoSpace(UNDO_FOLDERADD, id, item->getBlockType(), 0, 0, 0, 0, srcWin, item->getId(), pos);
  519. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store folder add");
  520. }
  521. void UndoBuffer::storeUndoFolderRemove(int id, SaveLoad* item, int pos, Window* srcWin) throw_Undo { start_func
  522. assert(item);
  523. assert(item->getId());
  524. createUndoSpace(UNDO_FOLDERREMOVE, id, item->getBlockType(), 0, 0, 0, 0, srcWin, item->getId(), pos);
  525. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store folder add");
  526. }
  527. void UndoBuffer::storeUndoTileSize(int tileSetId, int oldWidth, int oldHeight, int oldCount, int oldMaps, SDL_Surface* surface, Uint32* fontWidths, Uint32* collisionMaps, Window* srcWin) throw_Undo { start_func
  528. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tileset size");
  529. // If undo space not created, we delete saved buffers now, unless we're "in undo"
  530. if ((!createUndoSpace(UNDO_TILESIZE, tileSetId, 0, oldWidth, oldHeight, 0, 0, srcWin, oldCount, oldMaps, 0, surface, fontWidths, collisionMaps)) && (!inUndo)) {
  531. if (surface) SDL_FreeSurface(surface);
  532. delete[] fontWidths;
  533. delete[] collisionMaps;
  534. }
  535. }
  536. void UndoBuffer::storeUndoTilePerLine(int tileSetId, int oldPerLine, Window* srcWin) throw_Undo { start_func
  537. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tileset per line");
  538. createUndoSpace(UNDO_TILEPERLINE, tileSetId, 0, oldPerLine, 0, 0, 0, srcWin);
  539. }
  540. void UndoBuffer::storeUndoTileTrans(int tileSetId, int oldDefaultTransparent, Window* srcWin) throw_Undo { start_func
  541. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store tileset transparency flag");
  542. createUndoSpace(UNDO_TILETRANS, tileSetId, 0, oldDefaultTransparent, 0, 0, 0, srcWin);
  543. }
  544. void UndoBuffer::storeUndoWorldStart(int oldStart, Window* srcWin) throw_Undo { start_func
  545. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store world starting scene");
  546. createUndoSpace(UNDO_WORLDSTART, 0, 0, oldStart, 0, 0, 0, srcWin);
  547. }
  548. void UndoBuffer::storeUndoScriptDefault(int id, int animId, int tileId, int subId, Window* srcWin) throw_Undo { start_func
  549. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store script default image");
  550. createUndoSpace(UNDO_SCRIPTDEFAULT, id, 0, 0, 0, 0, 0, srcWin, animId, tileId, subId);
  551. }
  552. void UndoBuffer::storeUndoScriptModify(int id, const list<string>& source, int lineNum, int count, EditBox* editbox, Window* srcWin) throw_Undo { start_func
  553. storeUndoScript(UNDO_SCRIPTMODIFY, id, source, lineNum, count, editbox, srcWin);
  554. }
  555. void UndoBuffer::storeUndoScript(UndoType type, int id, const list<string>& source, int lineNum, int count, EditBox* editbox, Window* srcWin) throw_Undo { start_func
  556. assert(lineNum >= 0);
  557. assert(count > 0);
  558. assert((type == UNDO_SCRIPTMODIFY) || (type == UNDO_SCRIPTREMOVE));
  559. // Shortcut to prevent extra work and iterating to already-deleted data
  560. if (inUndo) return;
  561. if (type == UNDO_SCRIPTMODIFY) debugWrite(DEBUG_UNDO, "undo: store script modify");
  562. else debugWrite(DEBUG_UNDO, "undo: store script remove");
  563. list<string>::const_iterator begin = source.begin();
  564. for (int pos = 0; pos < lineNum; ++pos) {
  565. ++begin;
  566. }
  567. list<string>::const_iterator end = begin;
  568. for (int pos = 0; pos < count; ++pos) {
  569. ++end;
  570. }
  571. list<string>* copyScript = new list<string>(begin, end);
  572. // Is most recent undo block also a MODIFY of the same settings exactly? (and not merged)
  573. //@TODO: only combine same type of event (type, delete, etc.) this can be done once we
  574. // add undo descriptions
  575. if ((type == UNDO_SCRIPTMODIFY) && (undoAvailable())) {
  576. UndoHeader* header = (UndoHeader*)(undoBuffer + undoPos);
  577. int prevPos = undoPos - header->prevChunkSize;
  578. header = (UndoHeader*)(undoBuffer + prevPos);
  579. if ((header->type == UNDO_SCRIPTMODIFY) && (header->id == id) &&
  580. (header->xPos == lineNum) && (header->yPos == count) &&
  581. (header->sourceWindow == (srcWin ? srcWin->getId() : 0)) && (header->item2 == editbox)) {
  582. // If so, store nothing new; all done!
  583. delete copyScript;
  584. return;
  585. }
  586. }
  587. Uint32* buffer = NULL;
  588. try {
  589. buffer = createUndoSpace(type, id, 0, lineNum, count, sizeof(EditBox::TextPoint[3]) / sizeof(Uint32) + 1, 1, srcWin, 0, 0, 0, copyScript, editbox);
  590. if (!buffer) {
  591. delete copyScript;
  592. return;
  593. }
  594. }
  595. catch (...) {
  596. delete copyScript;
  597. throw;
  598. }
  599. if (editbox) {
  600. EditBox::TextPoint* tp = (EditBox::TextPoint*)buffer;
  601. editbox->getCursorData(tp[0], tp[1], tp[2]);
  602. }
  603. }
  604. void UndoBuffer::storeUndoScriptRemove(int id, const list<string>& source, int lineNum, int count, EditBox* editbox, Window* srcWin) throw_Undo { start_func
  605. storeUndoScript(UNDO_SCRIPTREMOVE, id, source, lineNum, count, editbox, srcWin);
  606. }
  607. void UndoBuffer::storeUndoScriptInsert(int id, int lineNum, int count, EditBox* editbox, Window* srcWin) throw_Undo { start_func
  608. assert(lineNum >= 0);
  609. assert(count > 0);
  610. if (!inUndo) debugWrite(DEBUG_UNDO, "undo: store script insert");
  611. Uint32* buffer = createUndoSpace(UNDO_SCRIPTINSERT, id, 0, lineNum, count, sizeof(EditBox::TextPoint[3]) / sizeof(Uint32) + 1, 1, srcWin, 0, 0, 0, NULL, editbox);
  612. if ((buffer) && (editbox)) {
  613. EditBox::TextPoint* tp = (EditBox::TextPoint*)buffer;
  614. editbox->getCursorData(tp[0], tp[1], tp[2]);
  615. }
  616. }
  617. void UndoBuffer::clearUndo() { start_func
  618. debugWrite(DEBUG_UNDO, "undo: clear all");
  619. if (undoBuffer) {
  620. // Scan up to end of any redos
  621. UndoHeader* header = (UndoHeader*)(undoBuffer + undoPos);
  622. while (header->type != UNDO_EMPTY) {
  623. undoPos += header->xSize * header->ySize + undoHeaderSize;
  624. header = (UndoHeader*)(undoBuffer + undoPos);
  625. }
  626. // Must clear any allocated objects
  627. while (undoPos > 0) {
  628. header = (UndoHeader*)(undoBuffer + undoPos);
  629. undoPos -= header->prevChunkSize;
  630. deleteUndoBlock(undoPos);
  631. }
  632. delete[] undoBuffer;
  633. undoBuffer = NULL;
  634. undoSizeAlloc = 0;
  635. undoPos = 0;
  636. }
  637. }
  638. void UndoBuffer::deleteUndoBlock(int offset) { start_func
  639. UndoHeader* header = (UndoHeader*)(undoBuffer + offset);
  640. int type = header->type & ~UNDO_MERGE;
  641. switch (type) {
  642. case UNDO_SCRIPTREMOVE:
  643. case UNDO_SCRIPTMODIFY:
  644. delete (list<string>*)header->item1;
  645. break;
  646. case UNDO_TILEDELETE:
  647. delete (TileSetEdit*)header->item1;
  648. break;
  649. case UNDO_SCENEDELETE:
  650. delete (SceneEdit*)header->item1;
  651. break;
  652. case UNDO_ANIMGROUPDELETE:
  653. delete (AnimGroupEdit*)header->item1;
  654. break;
  655. case UNDO_FOLDERDELETE:
  656. delete (FolderEdit*)header->item1;
  657. break;
  658. case UNDO_SCRIPTDELETE:
  659. delete (ScriptEdit*)header->item1;
  660. break;
  661. case UNDO_LAYERDELETE:
  662. delete (LayerEdit*)header->item1;
  663. break;
  664. case UNDO_SPAWNDELETE:
  665. delete (SpawnEdit*)header->item1;
  666. break;
  667. case UNDO_TILESIZE:
  668. if (header->item1) SDL_FreeSurface((SDL_Surface*)header->item1);
  669. delete[] (Uint32*)header->item2;
  670. delete[] (Uint32*)header->item3;
  671. break;
  672. case UNDO_LAYERSIZE:
  673. delete[] (Uint32*)header->item1;
  674. delete[] (Uint32*)header->item2;
  675. delete[] (Uint32*)header->item3;
  676. break;
  677. case UNDO_LAYERSPAWNSEL:
  678. delete (set<int>*)header->item2;
  679. break;
  680. }
  681. header->item1 = NULL;
  682. header->item2 = NULL;
  683. header->item3 = NULL;
  684. }
  685. void UndoBuffer::warnUser() throw_Undo { start_func
  686. if (guiConfirmBox("The undo buffer cannot store this action and will be cleared- continue without ability to undo?", "Confirm Permanent Action")) {
  687. clearUndo();
  688. // (cancel any further pieces of a current block)
  689. if (undoBlock) undoNonUndoable = 1;
  690. return;
  691. }
  692. debugWrite(DEBUG_UNDO, "undo: cancel undo");
  693. // Undo anything done as part of a block
  694. if (undoBlock) {
  695. if (undoBlockDidFirst) {
  696. undoPerform();
  697. }
  698. undoBlock = 0;
  699. // Delete any redo chunks here or later
  700. clearRedo();
  701. }
  702. throw UndoException();
  703. }
  704. void UndoBuffer::clearRedo() { start_func
  705. if (undoBuffer) {
  706. int redoPos = undoPos;
  707. UndoHeader* header = (UndoHeader*)(undoBuffer + redoPos);
  708. while (header->type != UNDO_EMPTY) {
  709. deleteUndoBlock(redoPos);
  710. redoPos += header->xSize * header->ySize + undoHeaderSize;
  711. // Actually delete each block from buffer
  712. header->sourceWindow = 0;
  713. header->type = UNDO_EMPTY;
  714. header->id = 0;
  715. header->xSize = 0;
  716. header->ySize = 0;
  717. header = (UndoHeader*)(undoBuffer + redoPos);
  718. }
  719. }
  720. }
  721. int UndoBuffer::undoAvailable() const { start_func
  722. // If undo pos isn't pointing at first item, there's undo available
  723. if ((undoBuffer) && (undoPos > 0)) return 1;
  724. return 0;
  725. }
  726. int UndoBuffer::redoAvailable() const { start_func
  727. // If undo pos isn't pointing at an empty item, there's redo available
  728. if (undoBuffer) {
  729. UndoHeader* header = (UndoHeader*)(undoBuffer + undoPos);
  730. if (header->type != UNDO_EMPTY) return 1;
  731. }
  732. return 0;
  733. }
  734. void UndoBuffer::redoPerform() { start_func
  735. undoPerform(1);
  736. }
  737. void UndoBuffer::undoPerform(int doRedo) { start_func
  738. UndoHeader* header = (UndoHeader*)(undoBuffer + undoPos);
  739. int didWindowFocus = 0;
  740. int didWindowQuery = 0;
  741. int windowDidntExist = 0;
  742. // Repeat undo/redo?
  743. int isMerged = 1;
  744. int cancelUndo = 0;
  745. inUndo = 1;
  746. while (isMerged) {
  747. if (doRedo) {
  748. debugWrite(DEBUG_UNDO, "undo: perform redo");
  749. if (!redoAvailable()) {
  750. inUndo = 0;
  751. return;
  752. }
  753. }
  754. else {
  755. debugWrite(DEBUG_UNDO, "undo: perform undo");
  756. if (!undoAvailable()) {
  757. inUndo = 0;
  758. return;
  759. }
  760. // Move undo pos backward an item
  761. undoPos -= header->prevChunkSize;
  762. header = (UndoHeader*)(undoBuffer + undoPos);
  763. isMerged = header->type & UNDO_MERGE;
  764. }
  765. const int type = header->type & ~UNDO_MERGE;
  766. // Verify target window exists, but don't query user if a selection action
  767. if (!header->sourceWindow) windowDidntExist = 1;
  768. else if (!didWindowQuery) {
  769. // Verify existence
  770. if (!desktop->verifyWindow(header->sourceWindow)) {
  771. windowDidntExist = 1;
  772. if ((type != UNDO_LAYERTILETMP) && (type != UNDO_LAYERTILECUR) &&
  773. (type != UNDO_LAYERTILESEL) && (type != UNDO_PAINTSEL) &&
  774. (type != UNDO_LAYERSPAWNSEL)) {
  775. if (!guiConfirmBox("You have already closed the window the original action occurred in- still undo?", "Confirm Undo")) cancelUndo = 1;
  776. }
  777. }
  778. }
  779. didWindowQuery = 1;
  780. // Used later in notify
  781. const int id = header->id;
  782. const int subid = header->subid;
  783. const int w = header->xSize;
  784. const int h = header->ySize;
  785. const int x = header->xPos;
  786. const int y = header->yPos;
  787. const int d1 = header->data1;
  788. const int d2 = header->data2;
  789. const int d3 = header->data3;
  790. Uint32* buffer = undoBuffer + undoPos + undoHeaderSize;
  791. Uint32* temp = NULL;
  792. if (!cancelUndo) {
  793. try {
  794. // Type of item?
  795. switch (type) {
  796. case UNDO_SCRIPTMODIFY:
  797. case UNDO_SCRIPTREMOVE:
  798. case UNDO_SCRIPTINSERT: {
  799. ScriptEdit* script = dynamic_cast<ScriptEdit*>(world->findScript(id));
  800. assert(script);
  801. script->markLock(); // Exception point
  802. list<string>::iterator cPos = script->getSource().begin();
  803. for (int pos = 0; pos < x; ++pos) {
  804. ++cPos;
  805. }
  806. EditBox* edit = (EditBox*)header->item2;
  807. EditBox::TextPoint ins, selBegin, selEnd;
  808. if ((edit) && (!windowDidntExist)) {
  809. EditBox::TextPoint* tp = (EditBox::TextPoint*)buffer;
  810. edit->getCursorData(ins, selBegin, selEnd);
  811. swap(ins, tp[0]);
  812. swap(selBegin, tp[1]);
  813. swap(selEnd, tp[2]);
  814. }
  815. if (type == UNDO_SCRIPTMODIFY) {
  816. debugWrite(DEBUG_UNDO, "undo: modify script modify");
  817. script->codeModifiedPre(ScriptEdit::SCRIPT_MODIFY_LINE, x, y);
  818. script->codeModifiedPre(ScriptEdit::SCRIPT_MODIFY_DONE, 0, 0);
  819. list<string>::iterator mPos = ((list<string>*)header->item1)->begin();
  820. for (int pos = 0; pos < y; ++pos) {
  821. swap(*cPos, *mPos);
  822. ++cPos;
  823. ++mPos;
  824. }
  825. script->codeModifiedPost(ScriptEdit::SCRIPT_MODIFY_LINE, x, y);
  826. }
  827. else if (type == UNDO_SCRIPTINSERT) {
  828. debugWrite(DEBUG_UNDO, "undo: modify script insert");
  829. script->codeModifiedPre(ScriptEdit::SCRIPT_REMOVE_LINES, x, y);
  830. script->codeModifiedPre(ScriptEdit::SCRIPT_MODIFY_DONE, 0, 0);
  831. list<string>::iterator cEnd = cPos;
  832. for (int pos = 0; pos < y; ++pos) {
  833. ++cEnd;
  834. }
  835. list<string>* inserted = new list<string>(cPos, cEnd);
  836. script->getSource().erase(cPos, cEnd);
  837. header->type = (UndoType)(UNDO_SCRIPTREMOVE | (header->type & UNDO_MERGE));
  838. header->item1 = inserted;
  839. script->codeModifiedPost(ScriptEdit::SCRIPT_REMOVE_LINES, x, y);
  840. }
  841. else {
  842. debugWrite(DEBUG_UNDO, "undo: modify script remove");
  843. script->codeModifiedPre(ScriptEdit::SCRIPT_INSERT_LINES, x, y);
  844. script->codeModifiedPre(ScriptEdit::SCRIPT_MODIFY_DONE, 0, 0);
  845. list<string>* deleted = (list<string>*)header->item1;
  846. assert((int)deleted->size() == y);
  847. script->getSource().splice(cPos, *deleted);
  848. delete deleted;
  849. header->type = (UndoType)(UNDO_SCRIPTINSERT | (header->type & UNDO_MERGE));
  850. header->item1 = NULL;
  851. script->codeModifiedPost(ScriptEdit::SCRIPT_INSERT_LINES, x, y);
  852. }
  853. script->codeModifiedPost(ScriptEdit::SCRIPT_MODIFY_DONE, 0, 0);
  854. if ((edit) && (!windowDidntExist)) edit->setCursorData(ins, selBegin, selEnd);
  855. script->markUnlock();
  856. break;
  857. }
  858. case UNDO_LAYERTILESET: {
  859. debugWrite(DEBUG_UNDO, "undo: modify layer tileset");
  860. LayerEdit* layer = world->findLayer(id);
  861. assert(layer);
  862. layer->markLock(); // Exception point
  863. // (have to account for 0 tileset)
  864. TileSet* tileset = layer->getTileSet();
  865. if (tileset) header->xPos = tileset->getId();
  866. else header->xPos = 0;
  867. try {
  868. layer->setTileset(x); // Another exception point
  869. }
  870. catch (...) {
  871. layer->markUnlock();
  872. throw;
  873. }
  874. layer->markUnlock();
  875. break;
  876. }
  877. case UNDO_LAYERSIZE: {
  878. debugWrite(DEBUG_UNDO, "undo: modify layer size");
  879. LayerEdit* layer = world->findLayer(id);
  880. assert(layer);
  881. layer->markLock(); // Exception point
  882. header->xPos = layer->getXSize();
  883. header->yPos = layer->getYSize();
  884. layer->setSize(x, y, NULL, NULL, (Uint32**)(&header->item1), (Uint32**)(&header->item2), (Uint32**)(&header->item3));
  885. layer->markUnlock();
  886. break;
  887. }
  888. case UNDO_LAYERFX: {
  889. debugWrite(DEBUG_UNDO, "undo: modify layer effects flag");
  890. LayerEdit* layer = world->findLayer(id);
  891. assert(layer);
  892. layer->markLock(); // Exception point
  893. header->xPos = layer->getUsesFx();
  894. layer->setEffects(x);
  895. layer->markUnlock();
  896. break;
  897. }
  898. case UNDO_LAYEREXT: {
  899. debugWrite(DEBUG_UNDO, "undo: modify layer extended data flag");
  900. LayerEdit* layer = world->findLayer(id);
  901. assert(layer);
  902. layer->markLock(); // Exception point
  903. header->xPos = layer->getUsesExt();
  904. layer->setExtended(x);
  905. layer->markUnlock();
  906. break;
  907. }
  908. case UNDO_LAYERTYPE: {
  909. debugWrite(DEBUG_UNDO, "undo: modify layer type");
  910. LayerEdit* layer = world->findLayer(id);
  911. assert(layer);
  912. layer->markLock(); // Exception point
  913. header->xPos = layer->getType();
  914. layer->setType((Layer::LayerType)x);
  915. layer->markUnlock();
  916. break;
  917. }
  918. case UNDO_LAYERTILECUR: {
  919. // Verify window still exists (if not, this is a "no op"
  920. if (desktop->verifyWindow(header->sourceWindow)) {
  921. debugWrite(DEBUG_UNDO, "undo: modify layer affected/view");
  922. // Ensure proper layers are selected
  923. SceneEditLayer* win = dynamic_cast<SceneEditLayer*>(dynamic_cast<FrameWindow*>(desktop->verifyWindow(header->sourceWindow))->getClient());
  924. win->refreshLayers(header->data1, header->data2, header->data3);
  925. }
  926. else {
  927. debugWrite(DEBUG_UNDO, "undo: skip layer affected/view (window closed)");
  928. }
  929. break;
  930. }
  931. case UNDO_LAYERTILETMP: {
  932. // Verify window still exists (if not, this is a "no op"
  933. if (desktop->verifyWindow(header->sourceWindow)) {
  934. debugWrite(DEBUG_UNDO, "undo: modify layer floating selection");
  935. // Swap data here with data in layer storage
  936. Uint32* data = *((Uint32**)(header->item1));
  937. data += y * subid + x;
  938. // Temp storage for swapping
  939. temp = new Uint32[w];
  940. int lineSize = w * 4;
  941. for (int pos = h; pos > 0; --pos) {
  942. memcpy(temp, data, lineSize);
  943. memcpy(data, buffer, lineSize);
  944. memcpy(buffer, temp, lineSize);
  945. data += subid;
  946. buffer += w;
  947. }
  948. if (header->item3) {
  949. Uint32* dataExt = *((Uint32**)(header->item3));
  950. dataExt += y * subid + x;
  951. for (int pos = h; pos > 0; --pos) {
  952. memcpy(temp, dataExt, lineSize);
  953. memcpy(dataExt, buffer, lineSize);
  954. memcpy(buffer, temp, lineSize);
  955. dataExt += subid;
  956. buffer += w;
  957. }
  958. }
  959. if (header->item2) {
  960. Uint32* dataFx = *((Uint32**)(header->item2));
  961. dataFx += y * subid + x;
  962. for (int pos = h; pos > 0; --pos) {
  963. memcpy(temp, dataFx, lineSize);
  964. memcpy(dataFx, buffer, lineSize);
  965. memcpy(buffer, temp, lineSize);
  966. dataFx += subid;
  967. buffer += w;
  968. }
  969. }
  970. // UNDO_LAYERTILESEL will alert window
  971. }
  972. else {
  973. debugWrite(DEBUG_UNDO, "undo: skip layer floating selection (window closed)");
  974. }
  975. break;
  976. }
  977. case UNDO_LAYERTILESEL: {
  978. // Verify window still exists (if not, this is a "no op"
  979. if (desktop->verifyWindow(header->sourceWindow)) {
  980. debugWrite(DEBUG_UNDO, "undo: modify layer selection");
  981. // Swap data here with data in selection surface
  982. Uint8* selData = *((Uint8**)(header->item1));
  983. // Temp storage for swapping
  984. temp = new Uint32[w];
  985. Uint8* dest = selData + y * subid + x;
  986. for (int pos = h; pos > 0; --pos) {
  987. memcpy(temp, dest, id);
  988. memcpy(dest, buffer, id);
  989. memcpy(buffer, temp, id);
  990. buffer += w;
  991. dest += subid;
  992. }
  993. // alert window and swap selection parameters
  994. SceneEditLayer* win = dynamic_cast<SceneEditLayer*>(dynamic_cast<FrameWindow*>(desktop->verifyWindow(header->sourceWindow))->getClient());
  995. win->swapSelectionParameters(header->data1, header->data2, header->data3);
  996. }
  997. else {
  998. debugWrite(DEBUG_UNDO, "undo: skip layer selection (window closed)");
  999. }
  1000. break;
  1001. }
  1002. case UNDO_LAYERSPAWNSEL: {
  1003. // Verify window still exists (if not, this is a "no op"
  1004. if (desktop->verifyWindow(header->sourceWindow)) {
  1005. debugWrite(DEBUG_UNDO, "undo: modify spawn selection");
  1006. // Swap data here with data in selection surface
  1007. set<int>* liveData = (set<int>*)(header->item1);
  1008. set<int>* undoData = (set<int>*)(header->item2);
  1009. liveData->swap(*undoData);
  1010. // alert window
  1011. SceneEditLayer* win = dynamic_cast<SceneEditLayer*>(dynamic_cast<FrameWindow*>(desktop->verifyWindow(header->sourceWindow))->getClient());
  1012. win->updatedSpawnSelection();
  1013. }
  1014. else {
  1015. debugWrite(DEBUG_UNDO, "undo: skip spawn selection (window closed)");
  1016. }
  1017. break;
  1018. }
  1019. case UNDO_PAINTSEL: {
  1020. // Verify window still exists (if not, this is a "no op"
  1021. if (desktop->verifyWindow(header->sourceWindow)) {
  1022. debugWrite(DEBUG_UNDO, "undo: modify tile selection");
  1023. // Swap data here with data in selection surface
  1024. SDL_Surface* surface = *((SDL_Surface**)(header->item1));
  1025. // Temp storage for swapping
  1026. temp = new Uint32[w];
  1027. // @TODO: reallocate surface if not large enough
  1028. int pitch = surface->pitch;
  1029. int lineSize = w * 4;
  1030. Uint8* dest = ((Uint8*)surface->pixels) + y * pitch + x * 4;
  1031. for (int pos = h; pos > 0; --pos) {
  1032. memcpy(temp, dest, lineSize);
  1033. memcpy(dest, buffer, lineSize);
  1034. memcpy(buffer, temp, lineSize);
  1035. buffer += w;
  1036. dest += pitch;
  1037. }
  1038. // alert window and swap selection parameters
  1039. TilePaint* win = dynamic_cast<TilePaint*>(dynamic_cast<FrameWindow*>(desktop->verifyWindow(header->sourceWindow))->getClient());
  1040. win->swapSelectionParameters(header->data1, header->data2, header->data3);
  1041. }
  1042. else {
  1043. debugWrite(DEBUG_UNDO, "undo: skip tile selection (window closed)");
  1044. }
  1045. break;
  1046. }
  1047. case UNDO_LAYERTILE:
  1048. case UNDO_LAYERTILEEXT:
  1049. case UNDO_LAYERTILEFX: {
  1050. // Swap data here with data in layer
  1051. LayerEdit* layer = world->findLayer(id);
  1052. assert(layer);
  1053. layer->markLock(); // Exception point
  1054. Rect rect = { x, y, w, h };
  1055. if (type == UNDO_LAYERTILE) {
  1056. debugWrite(DEBUG_UNDO, "undo: modify layer tiles");
  1057. layer->swapLayer(buffer, NULL, NULL, w, rect);
  1058. }
  1059. else if (type == UNDO_LAYERTILEFX) {
  1060. debugWrite(DEBUG_UNDO, "undo: modify layer effects");
  1061. layer->swapLayer(NULL, NULL, buffer, w, rect);
  1062. }
  1063. else {
  1064. debugWrite(DEBUG_UNDO, "undo: modify layer extended data");
  1065. layer->swapLayer(NULL, buffer, NULL, w, rect);
  1066. }
  1067. layer->markUnlock();
  1068. break;
  1069. }
  1070. case UNDO_TILE: {
  1071. debugWrite(DEBUG_UNDO, "undo: modify tile");
  1072. // Swap data here with data on tile
  1073. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1074. assert(tiles);
  1075. tiles->markLock(); // Exception point
  1076. int pitch = tiles->viewTilePitch();
  1077. int lineSize = w * 4;
  1078. Uint8* dest = tiles->editTileData(subid) + y * pitch + x * 4;
  1079. // Temp storage for swapping
  1080. temp = new Uint32[w];
  1081. for (int pos = h; pos > 0; --pos) {
  1082. memcpy(temp, dest, lineSize);
  1083. memcpy(dest, buffer, lineSize);
  1084. memcpy(buffer, temp, lineSize);
  1085. buffer += w;
  1086. dest += pitch;
  1087. }
  1088. // Store change and notify
  1089. tiles->editTileDone(subid);
  1090. tiles->markUnlock();
  1091. break;
  1092. }
  1093. case UNDO_TILECOLL: {
  1094. debugWrite(DEBUG_UNDO, "undo: modify collision map");
  1095. // Swap data here with data on map
  1096. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1097. assert(tiles);
  1098. tiles->markLock(); // Exception point
  1099. int pitch = tiles->getHeight();
  1100. Uint32* dest = tiles->editCollData(subid) + y * pitch + x * 4;
  1101. // Temp storage for swapping
  1102. temp = new Uint32[w];
  1103. for (int pos = w; pos > 0; --pos) {
  1104. memcpy(temp, dest, h);
  1105. memcpy(dest, buffer, h);
  1106. memcpy(buffer, temp, h);
  1107. buffer += h;
  1108. dest += pitch;
  1109. }
  1110. // Store change and notify
  1111. tiles->editCollDone(subid);
  1112. tiles->markUnlock();
  1113. break;
  1114. }
  1115. case UNDO_TILEGLYPH: {
  1116. debugWrite(DEBUG_UNDO, "undo: modify tile glyph");
  1117. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1118. assert(tiles);
  1119. tiles->markLock(); // Exception point
  1120. header->xPos = tiles->getGlyphWidth(subid);
  1121. tiles->setGlyphWidth(subid, x, NULL);
  1122. tiles->markUnlock();
  1123. break;
  1124. }
  1125. case UNDO_WORLDNAME:
  1126. case UNDO_LAYERNAME:
  1127. case UNDO_TILENAME:
  1128. case UNDO_SCENENAME:
  1129. case UNDO_ANIMGROUPNAME:
  1130. case UNDO_SCRIPTNAME:
  1131. case UNDO_SPAWNNAME:
  1132. case UNDO_FOLDERNAME: {
  1133. debugWrite(DEBUG_UNDO, "undo: modify name");
  1134. string tempName;
  1135. string newName((char*)buffer, x);
  1136. // Grab current and new names
  1137. // Store change and notify
  1138. if (type == UNDO_LAYERNAME) {
  1139. LayerEdit* layer = world->findLayer(id);
  1140. assert(layer);
  1141. tempName = layer->getName();
  1142. layer->setName(newName);
  1143. }
  1144. else if (type == UNDO_SPAWNNAME) {
  1145. SpawnEdit* spawn = world->findSpawn(id);
  1146. assert(spawn);
  1147. tempName = spawn->getName();
  1148. spawn->setName(newName);
  1149. }
  1150. else if (type == UNDO_TILENAME) {
  1151. TileSetEdit* tiles = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1152. assert(tiles);
  1153. tempName = tiles->getName();
  1154. tiles->setName(newName);
  1155. }
  1156. else if (type == UNDO_SCENENAME) {
  1157. SceneEdit* scene = dynamic_cast<SceneEdit*>(world->findScene(id));
  1158. assert(scene);
  1159. tempName = scene->getName();
  1160. scene->setName(newName);
  1161. }
  1162. else if (type == UNDO_SCRIPTNAME) {
  1163. ScriptEdit* script = dynamic_cast<ScriptEdit*>(world->findScript(id));
  1164. assert(script);
  1165. tempName = script->getName();
  1166. script->setName(newName);
  1167. }
  1168. else if (type == UNDO_WORLDNAME) {
  1169. tempName = world->getTitle();
  1170. world->setTitle(newName);
  1171. }
  1172. else if (type == UNDO_FOLDERNAME) {
  1173. FolderEdit* folder = dynamic_cast<FolderEdit*>(world->findFolder(id));
  1174. assert(folder);
  1175. tempName = folder->getName();
  1176. folder->setName(newName);
  1177. }
  1178. else {
  1179. assert(type == UNDO_ANIMGROUPNAME);
  1180. AnimGroupEdit* animgroup = dynamic_cast<AnimGroupEdit*>(world->findAnimGroup(id));
  1181. assert(animgroup);
  1182. tempName = animgroup->getName();
  1183. animgroup->setName(newName);
  1184. }
  1185. // Place old name back into buffer
  1186. memcpy(buffer, tempName.c_str(), tempName.size());
  1187. header->xPos = tempName.size();
  1188. break;
  1189. }
  1190. case UNDO_LAYERMOVE: {
  1191. debugWrite(DEBUG_UNDO, "undo: undo layer swap");
  1192. SceneEdit* scene = dynamic_cast<SceneEdit*>(world->findScene(id));
  1193. assert(scene);
  1194. scene->swapLayer(x, y);
  1195. break;
  1196. }
  1197. case UNDO_SPAWNCREATE: {
  1198. debugWrite(DEBUG_UNDO, "undo: undo spawn creation");
  1199. LayerEdit* layer = world->findLayer(subid);
  1200. assert(layer); // @TODO: non-layer spawns (subid == 0)
  1201. layer->markLock(); // Exception point
  1202. try {
  1203. SpawnEdit* spawn = world->findSpawn(id);
  1204. assert(spawn);
  1205. layer->deleteSpawn(spawn);
  1206. header->type = (UndoType)(UNDO_SPAWNDELETE | (header->type & UNDO_MERGE));
  1207. header->item1 = spawn;
  1208. layer->markUnlock();
  1209. }
  1210. catch (...) {
  1211. layer->markUnlock();
  1212. throw;
  1213. }
  1214. break;
  1215. }
  1216. case UNDO_LAYERCREATE: {
  1217. debugWrite(DEBUG_UNDO, "undo: undo layer creation");
  1218. LayerEdit* layer = world->findLayer(id);
  1219. assert(layer);
  1220. SceneEdit* scene = dynamic_cast<SceneEdit*>(world->findScene(subid));
  1221. assert(scene);
  1222. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1223. if (!scene->deleteLayer(x)) { // Exception point
  1224. cancelUndo = 1;
  1225. break;
  1226. }
  1227. header->type = (UndoType)(UNDO_LAYERDELETE | (header->type & UNDO_MERGE));
  1228. header->item1 = layer;
  1229. break;
  1230. }
  1231. case UNDO_SCENECREATE: {
  1232. debugWrite(DEBUG_UNDO, "undo: undo scene creation");
  1233. SceneEdit* scene = dynamic_cast<SceneEdit*>(world->findScene(id));
  1234. assert(scene);
  1235. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1236. if (!world->deleteScene(scene)) {
  1237. cancelUndo = 1;
  1238. break;
  1239. }
  1240. header->type = (UndoType)(UNDO_SCENEDELETE | (header->type & UNDO_MERGE));
  1241. header->item1 = scene;
  1242. break;
  1243. }
  1244. case UNDO_ANIMGROUPCREATE: {
  1245. debugWrite(DEBUG_UNDO, "undo: undo anim group creation");
  1246. AnimGroupEdit* animgroup = dynamic_cast<AnimGroupEdit*>(world->findAnimGroup(id));
  1247. assert(animgroup);
  1248. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1249. if (!world->deleteAnimGroup(animgroup)) {
  1250. cancelUndo = 1;
  1251. break;
  1252. }
  1253. header->type = (UndoType)(UNDO_ANIMGROUPDELETE | (header->type & UNDO_MERGE));
  1254. header->item1 = animgroup;
  1255. break;
  1256. }
  1257. case UNDO_FOLDERCREATE: {
  1258. debugWrite(DEBUG_UNDO, "undo: undo folder creation");
  1259. FolderEdit* folder = dynamic_cast<FolderEdit*>(world->findFolder(id));
  1260. assert(folder);
  1261. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1262. if (!world->deleteFolder(folder)) {
  1263. cancelUndo = 1;
  1264. break;
  1265. }
  1266. header->type = (UndoType)(UNDO_FOLDERDELETE | (header->type & UNDO_MERGE));
  1267. header->item1 = folder;
  1268. break;
  1269. }
  1270. case UNDO_SCRIPTCREATE: {
  1271. debugWrite(DEBUG_UNDO, "undo: undo script creation");
  1272. ScriptEdit* script = dynamic_cast<ScriptEdit*>(world->findScript(id));
  1273. assert(script);
  1274. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1275. if (!world->deleteScript(script)) {
  1276. cancelUndo = 1;
  1277. break;
  1278. }
  1279. header->type = (UndoType)(UNDO_SCRIPTDELETE | (header->type & UNDO_MERGE));
  1280. header->item1 = script;
  1281. break;
  1282. }
  1283. case UNDO_TILECREATE: {
  1284. debugWrite(DEBUG_UNDO, "undo: undo tileset creation");
  1285. TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1286. assert(tileset);
  1287. // (delete shouldn't ever "fail", but if it does, we just cancel)
  1288. if (!world->deleteTileset(tileset)) {
  1289. cancelUndo = 1;
  1290. break;
  1291. }
  1292. header->type = (UndoType)(UNDO_TILEDELETE | (header->type & UNDO_MERGE));
  1293. header->item1 = tileset;
  1294. break;
  1295. }
  1296. case UNDO_SPAWNDELETE: {
  1297. debugWrite(DEBUG_UNDO, "undo: undo spawn deletion");
  1298. // Undelete
  1299. LayerEdit* layer = world->findLayer(subid);
  1300. assert(layer); // @TODO: non-layer spawns (subid == 0)
  1301. layer->markLock(); // Exception point
  1302. try {
  1303. SpawnEdit* spawn = (SpawnEdit*)header->item1;
  1304. assert(spawn);
  1305. layer->addSpawn(spawn);
  1306. header->type = (UndoType)(UNDO_SPAWNCREATE | (header->type & UNDO_MERGE));
  1307. header->item1 = NULL;
  1308. layer->markUnlock();
  1309. }
  1310. catch (...) {
  1311. layer->markUnlock();
  1312. throw;
  1313. }
  1314. break;
  1315. }
  1316. case UNDO_LAYERDELETE: {
  1317. debugWrite(DEBUG_UNDO, "undo: undo layer deletion");
  1318. // Undelete
  1319. LayerEdit* layer = (LayerEdit*)header->item1;
  1320. assert(layer);
  1321. SceneEdit* scene = dynamic_cast<SceneEdit*>(world->findScene(subid));
  1322. assert(scene);
  1323. scene->insertLayer(layer, x, NULL, NULL, 1);
  1324. header->type = (UndoType)(UNDO_LAYERCREATE | (header->type & UNDO_MERGE));
  1325. header->item1 = NULL;
  1326. break;
  1327. }
  1328. case UNDO_SCENEDELETE: {
  1329. debugWrite(DEBUG_UNDO, "undo: undo scene deletion");
  1330. // Undelete
  1331. SceneEdit* scene = (SceneEdit*)header->item1;
  1332. assert(scene);
  1333. world->readdScene(scene);
  1334. header->type = (UndoType)(UNDO_SCENECREATE | (header->type & UNDO_MERGE));
  1335. header->item1 = NULL;
  1336. break;
  1337. }
  1338. case UNDO_SCRIPTDELETE: {
  1339. debugWrite(DEBUG_UNDO, "undo: undo script deletion");
  1340. // Undelete
  1341. ScriptEdit* script = (ScriptEdit*)header->item1;
  1342. assert(script);
  1343. world->readdScript(script);
  1344. header->type = (UndoType)(UNDO_SCRIPTCREATE | (header->type & UNDO_MERGE));
  1345. header->item1 = NULL;
  1346. break;
  1347. }
  1348. case UNDO_ANIMGROUPDELETE: {
  1349. debugWrite(DEBUG_UNDO, "undo: undo anim group deletion");
  1350. // Undelete
  1351. AnimGroupEdit* animgroup = (AnimGroupEdit*)header->item1;
  1352. assert(animgroup);
  1353. world->readdAnimGroup(animgroup);
  1354. header->type = (UndoType)(UNDO_ANIMGROUPCREATE | (header->type & UNDO_MERGE));
  1355. header->item1 = NULL;
  1356. break;
  1357. }
  1358. case UNDO_FOLDERDELETE: {
  1359. debugWrite(DEBUG_UNDO, "undo: undo folder deletion");
  1360. // Undelete
  1361. FolderEdit* folder = (FolderEdit*)header->item1;
  1362. assert(folder);
  1363. world->readdFolder(folder);
  1364. header->type = (UndoType)(UNDO_FOLDERCREATE | (header->type & UNDO_MERGE));
  1365. header->item1 = NULL;
  1366. break;
  1367. }
  1368. case UNDO_TILEDELETE: {
  1369. debugWrite(DEBUG_UNDO, "undo: undo tileset deletion");
  1370. // Undelete
  1371. TileSetEdit* tileset = (TileSetEdit*)header->item1;
  1372. assert(tileset);
  1373. world->readdTileset(tileset);
  1374. header->type = (UndoType)(UNDO_TILECREATE | (header->type & UNDO_MERGE));
  1375. header->item1 = NULL;
  1376. break;
  1377. }
  1378. case UNDO_TILESIZE: {
  1379. debugWrite(DEBUG_UNDO, "undo: modify tileset size");
  1380. TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1381. assert(tileset);
  1382. tileset->markLock(); // Exception point
  1383. header->xPos = tileset->getWidth();
  1384. header->yPos = tileset->getHeight();
  1385. header->data1 = tileset->getCount();
  1386. header->data2 = tileset->getCollisionCount();
  1387. tileset->setSize(x, y, d1, d2, NULL, NULL, (SDL_Surface**)(&header->item1), (Uint32**)(&header->item2), (Uint32**)(&header->item3));
  1388. tileset->markUnlock();
  1389. break;
  1390. }
  1391. case UNDO_TILEPERLINE: {
  1392. debugWrite(DEBUG_UNDO, "undo: modify tileset per line");
  1393. TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1394. assert(tileset);
  1395. header->xPos = tileset->getTilesPerLine();
  1396. tileset->setTilesPerLine(x);
  1397. break;
  1398. }
  1399. case UNDO_TILETRANS: {
  1400. debugWrite(DEBUG_UNDO, "undo: modify tileset transparency flag");
  1401. TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(id));
  1402. assert(tileset);
  1403. header->xPos = tileset->getDefaultTransparent();
  1404. tileset->setDefaultTransparent(x);
  1405. break;
  1406. }
  1407. case UNDO_WORLDSTART: {
  1408. debugWrite(DEBUG_UNDO, "undo: modify world starting scene");
  1409. header->xPos = world->getStartScene();
  1410. world->setStartScene(x);
  1411. break;
  1412. }
  1413. case UNDO_SCRIPTDEFAULT: {
  1414. debugWrite(DEBUG_UNDO, "undo: modify script default image");
  1415. ScriptEdit* script = dynamic_cast<ScriptEdit*>(world->findScript(id));
  1416. assert(script);
  1417. if (script->getDefaultAnimgroup()) header->data1 = script->getDefaultAnimgroup()->getId();
  1418. else header->data1 = 0;
  1419. if (script->getDefaultTileset()) header->data2 = script->getDefaultTileset()->getId();
  1420. else header->data2 = 0;
  1421. header->data3 = script->getDefaultId();
  1422. script->setDefault(world->findAnimGroup(d1),
  1423. world->findTileSet(d2),
  1424. d3);
  1425. break;
  1426. }
  1427. case UNDO_SPAWNMOVE: {
  1428. debugWrite(DEBUG_UNDO, "undo: modify spawn move");
  1429. LayerEdit* layer = world->findLayer(subid);
  1430. assert(layer); // @TODO: non-layer spawns (subid == 0)
  1431. layer->markLock(); // Exception point
  1432. try {
  1433. SpawnEdit* spawn = world->findSpawn(id);
  1434. assert(spawn);
  1435. header->xPos = spawn->getX();
  1436. header->yPos = spawn->getY();
  1437. spawn->setPos(x, y);
  1438. layer->markUnlock();
  1439. }
  1440. catch (...) {
  1441. layer->markUnlock();
  1442. throw;
  1443. }
  1444. break;
  1445. }
  1446. case UNDO_SPAWNSPRITE: {
  1447. debugWrite(DEBUG_UNDO, "undo: modify spawn image");
  1448. LayerEdit* layer = world->findLayer(subid);
  1449. assert(layer); // @TODO: non-layer spawns (subid == 0)
  1450. layer->markLock(); // Exception point
  1451. try {
  1452. SpawnEdit* spawn = world->findSpawn(id);
  1453. assert(spawn);
  1454. if (spawn->getAnimgroup()) header->data1 = spawn->getAnimgroup()->getId();
  1455. else header->data1 = 0;
  1456. if (spawn->getTileset()) header->data2 = spawn->getTileset()->getId();
  1457. else header->data2 = 0;
  1458. header->data3 = spawn->getSubid();
  1459. spawn->setSprite(world->findAnimGroup(d1),
  1460. world->findTileSet(d2),
  1461. d3);
  1462. layer->markUnlock();
  1463. }
  1464. catch (...) {
  1465. layer->markUnlock();
  1466. throw;
  1467. }
  1468. break;
  1469. }
  1470. case UNDO_SPAWNSCRIPT: {
  1471. debugWrite(DEBUG_UNDO, "undo: modify spawn script");
  1472. LayerEdit* layer = world->findLayer(subid);
  1473. assert(layer); // @TODO: non-layer spawns (subid == 0)
  1474. layer->markLock(); // Exception point
  1475. try {
  1476. SpawnEdit* spawn = world->findSpawn(id);
  1477. assert(spawn);
  1478. if (spawn->getScript()) header->data1 = spawn->getScript()->getId();
  1479. else header->data1 = 0;
  1480. spawn->setScript(world->findScript(d1));
  1481. layer->markUnlock();
  1482. }
  1483. catch (...) {
  1484. layer->markUnlock();
  1485. throw;
  1486. }
  1487. break;
  1488. }
  1489. case UNDO_FOLDERADD: {
  1490. debugWrite(DEBUG_UNDO, "undo: undo folder add");
  1491. FolderEdit* folder = world->findFolder(id);
  1492. assert(folder);
  1493. SaveLoad* item = world->findItem(d1, subid);
  1494. assert(item);
  1495. folder->removeItem(item);
  1496. header->type = (UndoType)(UNDO_FOLDERREMOVE | (header->type & UNDO_MERGE));
  1497. break;
  1498. }
  1499. case UNDO_FOLDERREMOVE: {
  1500. debugWrite(DEBUG_UNDO, "undo: undo folder remove");
  1501. FolderEdit* folder = world->findFolder(id);
  1502. assert(folder);
  1503. SaveLoad* item = world->findItem(d1, subid);
  1504. assert(item);
  1505. folder->addItem(item, d2);
  1506. header->type = (UndoType)(UNDO_FOLDERREMOVE | (header->type & UNDO_MERGE));
  1507. break;
  1508. }
  1509. }
  1510. }
  1511. catch (UndoException& e) {
  1512. // Since we were "inUndo", the above code should NEVER throw this
  1513. assert(0);
  1514. }
  1515. catch (FileException& e) {
  1516. // Likely from locking/uncaching
  1517. cancelUndo = 1;
  1518. guiErrorBox(string(e.details), errorTitleFile);
  1519. }
  1520. }
  1521. if (cancelUndo) {
  1522. inUndo = 0;
  1523. delete[] temp;
  1524. // Move undo pos back
  1525. if (!doRedo) undoPos += w * h + undoHeaderSize;
  1526. return;
  1527. }
  1528. // Notify window
  1529. if ((header->sourceWindow) && (!didWindowFocus)) {
  1530. // Verify existence
  1531. Window* win = desktop->verifyWindow(header->sourceWindow);
  1532. if (win) {
  1533. desktop->bringToTop(win);
  1534. win->undoNotify(type, id, subid, x, y, w, h);
  1535. didWindowFocus = 1;
  1536. }
  1537. }
  1538. delete[] temp;
  1539. if (doRedo) {
  1540. // Move undo pos forward an item
  1541. undoPos += w * h + undoHeaderSize;
  1542. header = (UndoHeader*)(undoBuffer + undoPos);
  1543. isMerged = header->type & UNDO_MERGE;
  1544. }
  1545. }
  1546. inUndo = 0;
  1547. }