gcsx_tilesetedit.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /* GCSx
  2. ** TILESETEDIT.CPP
  3. **
  4. ** Tileset support
  5. ** Expands basic TileSet to include editor functionality
  6. */
  7. /*****************************************************************************
  8. ** Copyright (C) 2003-2006 Janson
  9. **
  10. ** This program is free software; you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation; either version 2 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program; if not, write to the Free Software
  22. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  23. *****************************************************************************/
  24. #include "all.h"
  25. TileSetEdit::TileSetEdit(WorldEdit* myWorld, int newIsFont, int myId) : TileSet(myWorld, myId) { start_func
  26. if (myId) assert(myWorld);
  27. // Find an unused "Untitled" name
  28. if (myId) {
  29. string findName(newIsFont ? "font" : "images");
  30. int pos = 1;
  31. while (myWorld->findTileSet(findName)) {
  32. findName = newIsFont ? "font " : "images ";
  33. findName += intToStr(++pos);
  34. }
  35. findName[0] = toupper(findName[0]);
  36. name = findName;
  37. headerModified = contentModified = 1;
  38. }
  39. else headerModified = contentModified = 0;
  40. nameL = name;
  41. toLower(nameL);
  42. browserNode = NULL;
  43. isFont = newIsFont;
  44. if (newIsFont) {
  45. defaultTransparent = 1;
  46. numTilesPerLine = TILE_FONTSET_PERLINE;
  47. }
  48. disassociated = 0;
  49. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_CREATE, this, 0, 0);
  50. }
  51. TileSetEdit::~TileSetEdit() { start_func
  52. // @TODO: doesn't appear to be needed; no way to delete accidentally
  53. // if (!disassociated) desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_DELETE, this, 0, 0);
  54. // World should be the one deleting us, which will delete our node also
  55. }
  56. void TileSetEdit::setInfo(WorldEdit* myWorld, int newId) { start_func
  57. world = myWorld;
  58. id = newId;
  59. }
  60. void TileSetEdit::disassociate() throw_File { start_func
  61. // Ensure not cached- our exception point
  62. cacheLoad();
  63. // Drop browser node
  64. dropBrowser();
  65. // Notify objects
  66. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_DELETE, this, 0, 0);
  67. disassociated = 1;
  68. }
  69. void TileSetEdit::reassociate(TreeView* node) { start_func
  70. assert(disassociated);
  71. // Notify objects
  72. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_CREATE, this, 0, 0);
  73. // Browser node
  74. addToBrowser(node, 0);
  75. // Ensure written to file
  76. headerModified = contentModified = 1;
  77. disassociated = 0;
  78. }
  79. void TileSetEdit::blitTile(int tileNum, SDL_Surface* surface, int dX, int dY) const { start_func
  80. assert(surface);
  81. assert(tileNum >= 0);
  82. assert(!cached);
  83. if (tileNum <= 0) return;
  84. // Tile too high?
  85. if (tileNum > numTiles) {
  86. drawRect(dX, dY, width, height, guiPacked[COLOR_TEXTBOX], surface);
  87. SDL_Surface* textSurface = createText(intToStr(tileNum), guiRGB[COLOR_TEXT], 1, FONT_TOOLTIP);
  88. if (textSurface) {
  89. blit(0, 0, textSurface, dX, dY, surface, width, height);
  90. }
  91. SDL_FreeSurface(textSurface);
  92. return;
  93. }
  94. SDL_SetAlpha(tiles, SDL_SRCALPHA, 255);
  95. blit(tileNum % tsPerLine * width, tileNum / tsPerLine * height, tiles, dX, dY, surface, width, height);
  96. SDL_SetAlpha(tiles, 0, 255);
  97. }
  98. void TileSetEdit::blitCollFx(int collNum, SDL_Surface* surface, int dX, int dY, int dim, int mirror, int flip, int rotate90, Uint32 color) const { start_func
  99. assert(surface);
  100. assert(collNum >= 0);
  101. assert(!cached);
  102. if (collNum <= 0) return;
  103. // Coll too high? @TODO
  104. if (collNum > numCollisionMaps) {
  105. return;
  106. }
  107. }
  108. void TileSetEdit::blitTileFx(int tileNum, SDL_Surface* surface, int dX, int dY, int alpha, int mirror, int flip, int rotate90, int color) const { start_func
  109. assert(surface);
  110. assert(tileNum >= 0);
  111. assert(!cached);
  112. if (tileNum <= 0) return;
  113. // Tile too high? @TODO: show flip/mirror/rotate/alpha/dim, put text in white if needed
  114. if (tileNum > numTiles) {
  115. drawRect(dX, dY, width, height, SDL_MapRGB(surface->format, (color & 0xF) << 4, color & 0xF0, (color & 0xF00) >> 4), surface);
  116. SDL_Surface* textSurface = createText(intToStr(tileNum), guiRGB[COLOR_TEXT], 1, FONT_TOOLTIP);
  117. if (textSurface) {
  118. blit(0, 0, textSurface, dX, dY, surface, width, height);
  119. }
  120. SDL_FreeSurface(textSurface);
  121. return;
  122. }
  123. alphaBlitFx(tileNum % tsPerLine * width, tileNum / tsPerLine * height, tiles, dX, dY,
  124. surface, width, height, mirror, flip, rotate90, color & 0xF,
  125. (color & 0xF0) >> Image::COLOR_BITDEPTH,
  126. (color & 0xF00) >> (Image::COLOR_BITDEPTH * 2),
  127. (1 << Image::COLOR_BITDEPTH) - 1, alpha, Image::ALPHA_DEFAULT);
  128. }
  129. void TileSetEdit::loadTile(int tileNum, SDL_Surface* surface) { start_func
  130. assert(surface);
  131. assert(tileNum > 0);
  132. assert(tileNum <= numTiles);
  133. assert(surface->w >= width);
  134. assert(surface->h >= height);
  135. assert(surface->format->BytesPerPixel == 4);
  136. assert(!cached);
  137. blit(tileNum % tsPerLine * width, tileNum / tsPerLine * height, tiles, 0, 0, surface, width, height);
  138. }
  139. void TileSetEdit::loadColl(int collNum, SDL_Surface* surface) { start_func
  140. assert(surface);
  141. assert(collNum > 0);
  142. assert(collNum <= numCollisionMaps);
  143. assert(surface->w >= width);
  144. assert(surface->h >= height);
  145. assert(surface->format->BytesPerPixel == 4);
  146. assert(!cached);
  147. Uint32* rootSrc = collisionMaps + cmWidth * cmHeight * collNum;
  148. Uint32* dest = (Uint32*)surface->pixels;
  149. for (int y = 0; y < height; ++y) {
  150. Uint32* src = rootSrc + y;
  151. Uint32 read = *src;
  152. Uint32 bit = 1;
  153. for (int x = 0; x < width; ++x) {
  154. // (overflow to next column)
  155. if (!bit) {
  156. bit = 1;
  157. src += cmHeight;
  158. read = *src;
  159. }
  160. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  161. *dest = (read & bit) ? 0xffffffff : 0x000000ff;
  162. #else
  163. *dest = (read & bit) ? 0xffffffff : 0xff000000;
  164. #endif
  165. bit <<= 1;
  166. ++dest;
  167. }
  168. // Next row
  169. dest += surface->pitch / 4 - width;
  170. }
  171. }
  172. void TileSetEdit::saveTile(int tileNum, SDL_Surface* surface, Window* exWin) { start_func
  173. assert(surface);
  174. assert(tileNum > 0);
  175. assert(tileNum <= numTiles);
  176. assert(surface->w >= width);
  177. assert(surface->h >= height);
  178. assert(!cached);
  179. setContentModified();
  180. blit(0, 0, surface, tileNum % tsPerLine * width, tileNum / tsPerLine * height, tiles, width, height);
  181. // Send out update event
  182. // @TODO: shouldn't send out on import
  183. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_TILE, this, tileNum, tileNum, exWin);
  184. }
  185. void TileSetEdit::saveColl(int collNum, SDL_Surface* surface, Window* exWin) { start_func
  186. assert(surface);
  187. assert(collNum > 0);
  188. assert(collNum <= numCollisionMaps);
  189. assert(surface->w >= width);
  190. assert(surface->h >= height);
  191. assert(surface->format->BytesPerPixel == 4);
  192. assert(!cached);
  193. setContentModified();
  194. Uint32* rootDest = collisionMaps + cmWidth * cmHeight * collNum;
  195. Uint32* src = (Uint32*)surface->pixels;
  196. for (int y = 0; y < height; ++y) {
  197. Uint32* dest = rootDest + y;
  198. Uint32 result = 0;
  199. Uint32 bit = 1;
  200. for (int x = 0; x < width; ++x) {
  201. // (overflow to next column)
  202. if (!bit) {
  203. bit = 1;
  204. *dest = result;
  205. dest += cmHeight;
  206. result = 0;
  207. }
  208. if (*src == 0xffffffff) result |= bit;
  209. bit <<= 1;
  210. ++src;
  211. }
  212. *dest = result;
  213. // Next row
  214. src += surface->pitch / 4 - width;
  215. }
  216. // Send out update event
  217. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_COLL, this, collNum, collNum, exWin);
  218. }
  219. void TileSetEdit::setGlyphWidth(int tileNum, int newWidth, Window* exWin) { start_func
  220. assert(tileNum > 0);
  221. assert(tileNum <= numTiles);
  222. assert(isFont);
  223. assert(newWidth >= 0);
  224. assert(newWidth <= width);
  225. assert(fontWidths);
  226. setContentModified();
  227. fontWidths[tileNum] = newWidth;
  228. // Send out update event
  229. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_GLYPHW, this, tileNum, 0, exWin);
  230. }
  231. const Uint8* TileSetEdit::viewTileData(int tile) const { start_func
  232. assert(tile > 0);
  233. assert(tile <= numTiles);
  234. assert(!cached);
  235. return (Uint8*)tiles->pixels + tile / tsPerLine * height * tiles->pitch + tile % tsPerLine * width * 4;
  236. }
  237. const Uint32* TileSetEdit::viewCollData(int coll) const { start_func
  238. assert(coll > 0);
  239. assert(coll <= numCollisionMaps);
  240. assert(!cached);
  241. return collisionMaps + coll * cmWidth * cmHeight;
  242. }
  243. int TileSetEdit::viewTilePitch() const { start_func
  244. assert(!cached);
  245. assert(tiles);
  246. return tiles->pitch;
  247. }
  248. Uint8* TileSetEdit::editTileData(int tile) { start_func
  249. assert(tile > 0);
  250. assert(tile <= numTiles);
  251. assert(!cached);
  252. assert(tiles);
  253. return (Uint8*)tiles->pixels + tile / tsPerLine * height * tiles->pitch + tile % tsPerLine * width * 4;
  254. }
  255. Uint32* TileSetEdit::editCollData(int coll) { start_func
  256. assert(coll > 0);
  257. assert(coll <= numCollisionMaps);
  258. assert(!cached);
  259. return collisionMaps + coll * cmWidth * cmHeight;
  260. }
  261. void TileSetEdit::editTileDone(int tile, Window* exWin) { start_func
  262. assert(tile > 0);
  263. assert(tile <= numTiles);
  264. assert(!cached);
  265. setContentModified();
  266. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_TILE, this, tile, tile, exWin);
  267. }
  268. void TileSetEdit::editCollDone(int coll, Window* exWin) { start_func
  269. assert(coll > 0);
  270. assert(coll <= numCollisionMaps);
  271. assert(!cached);
  272. setContentModified();
  273. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_COLL, this, coll, coll, exWin);
  274. }
  275. void TileSetEdit::setHeaderModified() { start_func
  276. if (!headerModified) headerModified = 1;
  277. }
  278. void TileSetEdit::setContentModified() { start_func
  279. if (!contentModified) {
  280. contentModified = 1;
  281. // No possibility of returning to cache
  282. delete cacheFile;
  283. cacheFile = NULL;
  284. }
  285. }
  286. void TileSetEdit::dropBrowser() { start_func
  287. browserNode = NULL;
  288. }
  289. void TileSetEdit::addToBrowser(TreeView* node, int makeCurrent) { start_func
  290. browserNode = new TreeView(name, this, 0, treeviewEventWrap);
  291. browserNode->setIcon(isFont ? 4 : (numCollisionMaps ? 9 : 5));
  292. node->insert(browserNode, makeCurrent);
  293. }
  294. int TileSetEdit::treeviewEvent(int code, int command, int check) { start_func
  295. if (check) {
  296. if (command == EDIT_DELETE) return Window::COMMAND_ENABLE;
  297. return Window::COMMAND_HIDE;
  298. }
  299. switch (command) {
  300. case LV_SELECT:
  301. openBrowseWindow();
  302. return 1;
  303. case LV_RCLICK:
  304. // @TODO: Should actually be a popup with this as an item
  305. propertiesDialog(0, desktop->findPreviousFocusWindow());
  306. return 1;
  307. case EDIT_DELETE:
  308. case LV_DELETE:
  309. // Handles any error conditions/cancellation for us
  310. assert(world);
  311. getWorldEdit()->deleteTileset(this, desktop->findPreviousFocusWindow());
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. int TileSetEdit::treeviewEventWrap(void* ptr, int code, int command, int check) { start_func
  317. return ((TileSetEdit*)ptr)->treeviewEvent(code, command, check);
  318. }
  319. void TileSetEdit::setSize(int newWidth, int newHeight, int newCount, int newCollisionCount, Window* srcWin, Window* exWin, SDL_Surface** undo1, Uint32** undo2, Uint32** undo3) throw_Undo { start_func
  320. // No change?
  321. if ((newWidth == width) && (newHeight == height) && (newCount == numTiles) && (newCollisionCount == numCollisionMaps)) return;
  322. // This loads it if cached
  323. setHeaderModified();
  324. setContentModified();
  325. // Undo method?
  326. if (undo1) {
  327. assert(undo2);
  328. swap(*undo1, tiles);
  329. swap(*undo2, fontWidths);
  330. swap(*undo3, collisionMaps);
  331. if (newCount) {
  332. int sW, sH, newPerLine;
  333. calculateTileSurfaceSizing(newWidth, newHeight, newCount, sW, sH, newPerLine);
  334. tsPerLine = newPerLine;
  335. }
  336. }
  337. // (not undo)
  338. else {
  339. // (previous NULL surface allowed if no tiles)
  340. assert(tiles || (numTiles == 0));
  341. assert(collisionMaps || (numCollisionMaps == 0));
  342. SDL_Surface* newSurface = NULL;
  343. Uint32* newFontWidths = NULL;
  344. Uint32* newCollisionMaps = NULL;
  345. int sW, sH;
  346. int newPerLine = 0;
  347. try {
  348. // No count now = no surface
  349. if (newCount != 0) {
  350. // If we've gotten to this function, should already be verified
  351. assert(newCount == calculateTileSurfaceSizing(newWidth, newHeight, newCount, sW, sH, newPerLine));
  352. calculateTileSurfaceSizing(newWidth, newHeight, newCount, sW, sH, newPerLine);
  353. // Allocate new surface (if exception thrown, no harm done to us)
  354. newSurface = createSurface32(sW, sH);
  355. // Fill with transparent black or solid black
  356. drawRect(0, 0, sW, sH, mapColor32(0, 0, 0, defaultTransparent ? 0 : 255), newSurface);
  357. // Copy tiles if any
  358. int toCopy = min(newCount, numTiles) + 1;
  359. if (toCopy > 1) {
  360. int blitHeight = min(newHeight, height);
  361. int blitWidth = min(newWidth, width);
  362. int x = 0;
  363. int y = -newHeight; // Since we immediately increase it at pos 0
  364. int oldX = 0;
  365. int oldY = -height; // Since we immediately increase it at pos 0
  366. for (int pos = 0; pos < toCopy; ++pos) {
  367. // Move positions
  368. if (pos % tsPerLine) oldX += width;
  369. else {
  370. oldX = 0;
  371. oldY += height;
  372. }
  373. if (pos % newPerLine) x += newWidth;
  374. else {
  375. x = 0;
  376. y += newHeight;
  377. }
  378. blit(oldX, oldY, tiles, x, y, newSurface, blitWidth, blitHeight);
  379. }
  380. }
  381. // Also allocate new font sizing?
  382. if (isFont) {
  383. newFontWidths = new Uint32[newCount + 1];
  384. if (toCopy > 1) {
  385. // Ensure glyph widths aren't larger than tile size
  386. for (int pos = 0; pos < toCopy; ++pos) {
  387. if (fontWidths[pos] > (Uint32)newWidth) newFontWidths[pos] = newWidth;
  388. else newFontWidths[pos] = fontWidths[pos];
  389. }
  390. }
  391. if (toCopy < newCount + 1) {
  392. memset(newFontWidths + toCopy, 0, (newCount + 1 - toCopy) * sizeof(Uint32));
  393. newFontWidths[0] = 0;
  394. }
  395. }
  396. }
  397. // No count now = no surface
  398. if (newCollisionCount != 0) {
  399. sH = newHeight;
  400. sW = newWidth >> 5;
  401. if (newWidth & 31) ++sW;
  402. int size = (newCollisionCount + 2) * sH * sW;
  403. newCollisionMaps = new Uint32[size];
  404. memset(newCollisionMaps, 0, size * sizeof(Uint32));
  405. int toCopy = min(newCollisionCount, numCollisionMaps) + 1;
  406. if (toCopy > 1) {
  407. // @TODO: Copy over old collision data
  408. }
  409. }
  410. if (world) {
  411. getWorldEdit()->undo.storeUndoTileSize(id, width, height, numTiles, numCollisionMaps, tiles, fontWidths, collisionMaps, srcWin);
  412. }
  413. else {
  414. if (tiles) SDL_FreeSurface(tiles);
  415. delete[] fontWidths;
  416. delete[] collisionMaps;
  417. }
  418. tiles = newSurface;
  419. fontWidths = newFontWidths;
  420. collisionMaps = newCollisionMaps;
  421. tsPerLine = newPerLine;
  422. }
  423. catch (...) {
  424. if (newSurface) SDL_FreeSurface(newSurface);
  425. delete[] newFontWidths;
  426. delete[] newCollisionMaps;
  427. throw;
  428. }
  429. }
  430. int typeModified = 0;
  431. if (width != newWidth) typeModified |= OBJMOD_WIDTH;
  432. if (height != newHeight) typeModified |= OBJMOD_HEIGHT;
  433. // @TODO: All layers that utilize this set, even those not open, need to be notified of this
  434. // this notification may cause changes that also need to be undone; either that, or
  435. // layers get to keep invalid tile references (may be cleaner)
  436. if (numTiles != newCount) typeModified |= OBJMOD_COUNT;
  437. if (numCollisionMaps != newCollisionCount) typeModified |= OBJMOD_COUNTCOLL;
  438. width = newWidth;
  439. height = newHeight;
  440. calculateCmSizes();
  441. numTiles = newCount;
  442. numCollisionMaps = newCollisionCount;
  443. if (typeModified) desktop->broadcastObjChange(OBJ_TILESET | typeModified, this, 0, 0, exWin);
  444. // Update node icon
  445. if (browserNode) browserNode->setIcon(isFont ? 4 : (numCollisionMaps ? 9 : 5));
  446. }
  447. /* (unused)- if used, be sure to integrate into undo/add exWin?
  448. void TileSetEdit::clear(int w, int h, int count) { start_func
  449. if (tiles) SDL_FreeSurface(tiles);
  450. delete[] fontWidths;
  451. setHeaderModified();
  452. setContentModified();
  453. isFont = 0;
  454. fontWidths = NULL;
  455. numTiles = 0;
  456. tiles = NULL;
  457. setSize(w, h, count); // THROWS UNDO
  458. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_TILE, this, 1, count);
  459. }
  460. */
  461. void TileSetEdit::setName(const string& newName, Window* srcWin, Window* exWin) throw_Undo { start_func
  462. if (name != newName) {
  463. setHeaderModified();
  464. if (world) {
  465. getWorldEdit()->undo.storeUndoName(UndoBuffer::UNDO_TILENAME, id, name, newName, srcWin);
  466. world->deindexTileSet(this);
  467. }
  468. name = newName;
  469. nameL = newName;
  470. toLower(nameL);
  471. if (world) world->indexTileSet(this);
  472. if (browserNode) browserNode->changeName(newName);
  473. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_NAME, this, 0, 0, exWin);
  474. }
  475. }
  476. /* (unused)- if used, be sure to integrate into undo; also destroy collision data; also add "exWin" param
  477. void TileSetEdit::setIsFont(int newIsFont) { start_func
  478. if (((isFont) && (!newIsFont)) || ((!isFont) && (newIsFont))) {
  479. setHeaderModified();
  480. if ((newIsFont) && (numTiles)) {
  481. fontWidths = new int[numTiles + 1];
  482. memset(fontWidths, 0, (numTiles + 1) * sizeof(int));
  483. }
  484. else {
  485. delete[] fontWidths;
  486. fontWidths = NULL;
  487. }
  488. isFont = newIsFont;
  489. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_ISFONT, this, 0, 0);
  490. }
  491. }
  492. */
  493. void TileSetEdit::setTilesPerLine(int newTilesPerLine, Window* srcWin, Window* exWin) throw_Undo { start_func
  494. if (newTilesPerLine != numTilesPerLine) {
  495. setHeaderModified();
  496. if (world) getWorldEdit()->undo.storeUndoTilePerLine(id, numTilesPerLine, srcWin);
  497. numTilesPerLine = newTilesPerLine;
  498. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_PERLINE, this, 0, 0, exWin);
  499. }
  500. }
  501. void TileSetEdit::setDefaultTransparent(int newDefaultTransparent, Window* srcWin, Window* exWin) throw_Undo { start_func
  502. if (newDefaultTransparent != defaultTransparent) {
  503. setHeaderModified();
  504. if (world) getWorldEdit()->undo.storeUndoTileTrans(id, defaultTransparent, srcWin);
  505. Uint32 changeFrom = mapColor32(0, 0, 0, defaultTransparent ? 0 : 255);
  506. Uint32 changeTo = mapColor32(0, 0, 0, newDefaultTransparent ? 0 : 255);
  507. defaultTransparent = newDefaultTransparent;
  508. // All empty tiles should change (tile 0 is technically irrelevant)
  509. if (numTiles) {
  510. int firstChanged = -1;
  511. int lastChanged = -1;
  512. int x = 0;
  513. int y = -height; // Since we immediately increase it at pos 0
  514. int pitch = tiles->pitch / 4;
  515. for (int pos = 0; pos <= numTiles; ++pos) {
  516. // Move position
  517. if (pos % tsPerLine) x += width;
  518. else {
  519. x = 0;
  520. y += height;
  521. }
  522. // Check tile to see if it's entirely the old transparency color
  523. Uint32* tile = (Uint32*)tiles->pixels + x + y * pitch;
  524. int found = 0;
  525. for (int yP = 0; (yP < height) && (!found); ++yP) {
  526. for (int xP = 0; xP < width; ++xP) {
  527. if (*tile++ != changeFrom) {
  528. found = 1;
  529. break;
  530. }
  531. }
  532. tile += pitch - width;
  533. }
  534. // Entirely old color?
  535. if (!found) {
  536. if (firstChanged < 0) firstChanged = pos;
  537. lastChanged = pos;
  538. drawRect(x, y, width, height, changeTo, tiles);
  539. }
  540. }
  541. // (don't care about/broadcast tile 0 changing)
  542. if (lastChanged >= 1) {
  543. if (firstChanged == 0) firstChanged = 1;
  544. setContentModified();
  545. // Don't cascade exWin- this may be an unexpected change
  546. assert(firstChanged <= lastChanged);
  547. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_TILE, this, firstChanged, lastChanged);
  548. }
  549. }
  550. desktop->broadcastObjChange(OBJ_TILESET | OBJMOD_DEFTRANS, this, 0, 0, exWin);
  551. }
  552. }
  553. void TileSetEdit::openTilePaintWindow(int tile) { start_func
  554. assert(tile > 0);
  555. if (!numTiles) return;
  556. if (tile > numTiles) tile = numTiles;
  557. try {
  558. new TilePaint(this, tile);
  559. }
  560. catch (FileException& e) {
  561. guiErrorBox(string(e.details), errorTitleFile);
  562. }
  563. }
  564. void TileSetEdit::openCollPaintWindow(int coll) { start_func
  565. assert(coll > 0);
  566. if (!numCollisionMaps) return;
  567. if (coll > numCollisionMaps) coll = numCollisionMaps;
  568. try {
  569. new TilePaint(this, coll, 1);
  570. }
  571. catch (FileException& e) {
  572. guiErrorBox(string(e.details), errorTitleFile);
  573. }
  574. }
  575. void TileSetEdit::openBrowseWindow() { start_func
  576. try {
  577. new TileSetBrowse(this);
  578. }
  579. catch (FileException& e) {
  580. guiErrorBox(string(e.details), errorTitleFile);
  581. }
  582. }
  583. int TileSetEdit::propertiesDialog(int newTileSet, Window* srcWin, Window* exWin) { start_func
  584. int newWidth = width;
  585. int newHeight = height;
  586. int newCount = numTiles;
  587. int newMaps = numCollisionMaps;
  588. int newPerLine = numTilesPerLine;
  589. int newDefaultTransparent = defaultTransparent;
  590. string newName = name;
  591. if ((newTileSet) && (newCount == 0)) newCount = TILE_DEFAULT_COUNT;
  592. if ((newTileSet) && (newMaps == 0)) newMaps = COLLISION_DEFAULT_COUNT;
  593. int result;
  594. if (isFont) result = TileSetPropertiesDialog::createFontSet()->run(&newName, &newWidth, &newHeight, &newCount, &newMaps, &newPerLine, &newDefaultTransparent, this);
  595. else result = TileSetPropertiesDialog::createTileSet()->run(&newName, &newWidth, &newHeight, &newCount, &newMaps, &newPerLine, &newDefaultTransparent, this);
  596. if (result) {
  597. try {
  598. markLock();
  599. }
  600. catch (FileException& e) {
  601. guiErrorBox(string(e.details), errorTitleFile);
  602. return 0;
  603. }
  604. try {
  605. if (world) getWorldEdit()->undo.preUndoBlock();
  606. setName(newName, srcWin, exWin);
  607. setSize(newWidth, newHeight, newCount, newMaps, srcWin, exWin);
  608. setTilesPerLine(newPerLine, srcWin, exWin);
  609. setDefaultTransparent(newDefaultTransparent, srcWin, exWin);
  610. if (world) getWorldEdit()->undo.postUndoBlock();
  611. }
  612. catch (UndoException& e) {
  613. markUnlock();
  614. return 0;
  615. }
  616. markUnlock();
  617. return 1;
  618. }
  619. return 0;
  620. }
  621. int TileSetEdit::calculateTileSurfaceSizing(int w, int h, int count, int& sWidth, int& sHeight, int& perLine) { start_func
  622. // One extra blank tile
  623. ++count;
  624. // Determine how many columns minimum we require
  625. int maxRows = TILE_SURFACE_MAX / h;
  626. perLine = count / maxRows;
  627. if (count % maxRows) ++perLine;
  628. // Determine width
  629. sWidth = perLine * w;
  630. // Too large?
  631. if (sWidth > TILE_SURFACE_MAX) {
  632. // Limit size
  633. perLine = TILE_SURFACE_MAX / w;
  634. // Adjust count
  635. assert(perLine * maxRows < count);
  636. count = perLine * maxRows;
  637. // New width
  638. sWidth = perLine * w;
  639. }
  640. // Determine how many rows we want, aiming to balance the columns evenly
  641. int numRows = count / perLine;
  642. if (count % perLine) ++numRows;
  643. assert(numRows <= maxRows);
  644. // Determine height
  645. sHeight = numRows * h;
  646. assert(sHeight <= TILE_SURFACE_MAX);
  647. return count - 1;
  648. }
  649. int TileSetEdit::isHeaderModified() const { start_func
  650. return headerModified;
  651. }
  652. int TileSetEdit::isContentModified() const { start_func
  653. return contentModified;
  654. }
  655. Uint32 TileSetEdit::saveHeader(FileWrite* file) throw_File { start_func
  656. assert(world);
  657. assert(id);
  658. file->writeStr(name);
  659. file->writeInt(width);
  660. file->writeInt(height);
  661. file->writeInt(numTiles);
  662. file->writeInt(numCollisionMaps);
  663. file->writeInt(numTilesPerLine);
  664. file->writeInt(tsPerLine);
  665. file->writeInt(defaultTransparent);
  666. file->writeInt(isFont);
  667. file->writeInt(id);
  668. return 1;
  669. }
  670. void TileSetEdit::saveContent(FileWrite* file) throw_File { start_func
  671. assert((tiles) || (numTiles == 0));
  672. assert(world);
  673. assert(id);
  674. if (numTiles) {
  675. file->writeInt(tiles->w);
  676. file->writeInt(tiles->h);
  677. file->writeInt(tiles->w * tiles->h * 4);
  678. // If pitch of created surface is "incorrect", we need to do extra work
  679. if (tiles->pitch == tiles->w * 4) {
  680. file->write(tiles->pixels, tiles->w * tiles->h * 4);
  681. }
  682. else {
  683. Uint8* src = (Uint8*)tiles->pixels;
  684. for (int row = tiles->h; row > 0; --row) {
  685. file->write(src, tiles->w * 4);
  686. src += tiles->pitch;
  687. }
  688. }
  689. if (isFont) file->writeIntBulk(fontWidths + 1, numTiles);
  690. }
  691. if (numCollisionMaps) {
  692. file->writeIntBulk(collisionMaps + cmWidth * cmHeight,
  693. (numCollisionMaps + 1) * cmWidth * cmHeight);
  694. }
  695. }
  696. void TileSetEdit::saveSuccess() { start_func
  697. headerModified = 0;
  698. contentModified = 0;
  699. }
  700. void TileSetEdit::cachedContent(FileRead* file, int oldData) { start_func
  701. // If already cached..
  702. if (cached) {
  703. delete cacheFile;
  704. cacheFile = file;
  705. }
  706. else if (oldData) {
  707. delete file;
  708. }
  709. else {
  710. // Cache ourselves?
  711. if (!lockCount) {
  712. delete cacheFile;
  713. cacheFile = file;
  714. if (tiles) SDL_FreeSurface(tiles);
  715. tiles = NULL;
  716. delete[] fontWidths;
  717. fontWidths = NULL;
  718. delete[] collisionMaps;
  719. collisionMaps = NULL;
  720. cached = 1;
  721. }
  722. else {
  723. delete file;
  724. }
  725. }
  726. }