gcsx_imgselect.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. /* GCSx
  2. ** IMGSELECT.CPP
  3. **
  4. ** Image/tile/sprite selection toolbar
  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. // Image struct
  25. Image::Image() { start_func
  26. // Ensure our constants match layer constants
  27. assert((int)COLOR_BITDEPTH == (int)Layer::LAYER_TILE_COLOR_BITDEPTH);
  28. assert((int)COLOR_DEFAULT == (int)(Layer::LAYER_TILE_COLOR >> Layer::LAYER_TILE_COLOR_SHIFT));
  29. assert((int)ALPHA_BITDEPTH == (int)Layer::LAYER_EXT_ALPHA_BITDEPTH);
  30. assert((int)ALPHA_DEFAULT == (int)(Layer::LAYER_EXT_ALPHA >> Layer::LAYER_EXT_ALPHA_SHIFT));
  31. index = 0;
  32. color = COLOR_DEFAULT;
  33. alpha = ALPHA_DEFAULT;
  34. effects = 0;
  35. }
  36. // Pattern struct
  37. Pattern::Pattern() : images() { start_func
  38. count = 0;
  39. width = 0;
  40. height = 0;
  41. }
  42. // Shown Images struct
  43. ShownImages::ShownImages(const TileSetEdit* tiles) : strip(), recent() { start_func
  44. if ((tiles) && (tiles->getCount())) strip.index = 1;
  45. }
  46. // ImageSelect
  47. const string ImageSelect::wtButtonPrevH("l");
  48. const string ImageSelect::wtButtonNextH("k");
  49. const string ImageSelect::wtButtonPrevV("m");
  50. const string ImageSelect::wtButtonNextV("n");
  51. const string ImageSelect::wtButtonExpand("q");
  52. int ImageSelect::buttonWidth = 0;
  53. map<const void*,ShownImages>* ImageSelect::shownBank = NULL;
  54. void ImageSelect::destroyGlobals() { start_func
  55. if (shownBank) {
  56. delete shownBank;
  57. shownBank = NULL;
  58. }
  59. }
  60. ImageSelect::ImageSelect(TileSetEdit* tiles, const ColorStore* cStorage, int useExtendedFeatures) throw_File : Window(), shown(tiles) { start_func
  61. myFrame = NULL;
  62. buttonWidth = max(fontWidth(wtButtonExpand, FONT_WIDGET), fontHeight(FONT_WIDGET)) + 8;
  63. colors = cStorage;
  64. extendedFeatures = useExtendedFeatures;
  65. if (extendedFeatures) selectedType = SELECTED_NONE;
  66. else selectedType = SELECTED_STRIP;
  67. dragType = DRAG_NONE;
  68. selectedIndex = 0;
  69. useAlpha = 0;
  70. numImagesShownCount = IMAGE_COUNT;
  71. numImagesShownAcross = IMAGE_ACROSS;
  72. tileset = NULL;
  73. changeSet(tiles);
  74. }
  75. FrameWindow* ImageSelect::createWindowed() { start_func
  76. // Prevent duplication
  77. if (myFrame) {
  78. return myFrame;
  79. }
  80. // We remember the frame pointer even though it'll delete itself
  81. myFrame = new FrameWindow("Images", FrameWindow::RESIZING_SNAP, FrameWindow::FRAMETYPE_DIALOG, this, FrameWindow::TITLEBAR_TOOL, 0);
  82. return myFrame;
  83. }
  84. ImageSelect::~ImageSelect() { start_func
  85. if (tileset) tileset->markUnlock();
  86. }
  87. void ImageSelect::allowAlpha(int newUseAlpha) { start_func
  88. useAlpha = newUseAlpha;
  89. colorRefresh();
  90. }
  91. void ImageSelect::changeSet(TileSetEdit* tiles, int oldDeleted) throw_File { start_func
  92. if (tiles) {
  93. tiles->markLock(); // Exception point
  94. numImages = tiles->getCount();
  95. imageWidth = tiles->getWidth();
  96. imageHeight = tiles->getHeight();
  97. }
  98. else {
  99. numImages = 0;
  100. imageWidth = 32;
  101. imageHeight = 32;
  102. }
  103. // Unlock previous tileset
  104. if ((tileset) && (!oldDeleted)) tileset->markUnlock();
  105. // Always work as if tiles could be NULL
  106. tileset = tiles;
  107. zoomWidth = imageWidth;
  108. zoomHeight = imageHeight;
  109. if (zoomWidth > IMAGE_MAX_SIZE) {
  110. zoomHeight = imageHeight * IMAGE_MAX_SIZE / imageWidth;
  111. zoomWidth = IMAGE_MAX_SIZE;
  112. }
  113. if (zoomHeight > IMAGE_MAX_SIZE) {
  114. zoomWidth = imageWidth * IMAGE_MAX_SIZE / imageHeight;
  115. zoomHeight = IMAGE_MAX_SIZE;
  116. }
  117. paddedWidth = zoomWidth;
  118. paddedHeight = zoomHeight;
  119. if (paddedWidth < IMAGE_MIN_SIZE) paddedWidth = IMAGE_MIN_SIZE;
  120. if (paddedHeight < IMAGE_MIN_SIZE) paddedHeight = IMAGE_MIN_SIZE;
  121. // Find associated default values
  122. if (tiles) {
  123. if (!shownBank) shownBank = new map<const void*,ShownImages>;
  124. map<const void*,ShownImages>::iterator pos = shownBank->find(tiles);
  125. if (pos == shownBank->end()) {
  126. // No default values yet- create blanks
  127. ShownImages newShown(tiles);
  128. shown = newShown;
  129. shownBank->insert(pair<const void*,ShownImages>(tiles, newShown));
  130. }
  131. else {
  132. shown = (*pos).second;
  133. }
  134. }
  135. else {
  136. ShownImages newShown(NULL);
  137. shown = newShown;
  138. }
  139. resize(width, height);
  140. setDirty();
  141. }
  142. void ImageSelect::colorRefresh() { start_func
  143. int color = colors->fg.r | (colors->fg.g << 4) | (colors->fg.b << 8);
  144. int alpha = colors->fg.a;
  145. if (!useAlpha) alpha = Image::ALPHA_DEFAULT;
  146. shown.strip.color = color;
  147. shown.strip.alpha = alpha;
  148. if ((selectedType == SELECTED_RECENT) && (selectedIndex < ShownImages::PATTERN_RECENT_MAX)) {
  149. vector<Pattern::PatternEntry>::iterator pos;
  150. vector<Pattern::PatternEntry>::iterator end = shown.recent[selectedIndex].images.end();
  151. for (pos = shown.recent[selectedIndex].images.begin(); pos != end; ++pos) {
  152. (*pos).image.color = color;
  153. (*pos).image.alpha = alpha;
  154. }
  155. }
  156. saveDef();
  157. setDirty();
  158. }
  159. void ImageSelect::getDataExt(Uint32& data, Uint32& ext, Uint32& fx) const { start_func
  160. const Image* current;
  161. data = 0;
  162. if (selectedType == SELECTED_STRIP) current = &shown.strip;
  163. else if ((selectedType == SELECTED_RECENT) && (selectedIndex < ShownImages::PATTERN_RECENT_MAX)) {
  164. if (shown.recent[selectedIndex].count == 1) current = &shown.recent[selectedIndex].images[0].image;
  165. else {
  166. // @TODO: patterns
  167. data = Layer::LAYER_TILE_DEFAULT;
  168. ext = Layer::LAYER_EXT_DEFAULT;
  169. fx = Layer::LAYER_FX_DEFAULT;
  170. return;
  171. }
  172. }
  173. else {
  174. data = Layer::LAYER_TILE_DEFAULT;
  175. ext = Layer::LAYER_EXT_DEFAULT;
  176. fx = Layer::LAYER_FX_DEFAULT;
  177. return;
  178. }
  179. // image
  180. data = current->index;
  181. if (selectedType == SELECTED_STRIP) {
  182. data += selectedIndex;
  183. if (numImages) data = (data - 1) % numImages + 1;
  184. }
  185. // color
  186. data |= current->color << Layer::LAYER_TILE_COLOR_SHIFT;
  187. // orientation
  188. if (current->effects & Image::EFFECTS_FLIP) data |= Layer::LAYER_TILE_FLIP;
  189. if (current->effects & Image::EFFECTS_MIRROR) data |= Layer::LAYER_TILE_MIRROR;
  190. if (current->effects & Image::EFFECTS_ROTATE) data |= Layer::LAYER_TILE_ROTATE;
  191. // alpha
  192. ext |= current->alpha << Layer::LAYER_EXT_ALPHA_SHIFT;
  193. // @TODO: not determined: effects, collision data, animation
  194. fx = Layer::LAYER_FX_DEFAULT;
  195. }
  196. void ImageSelect::dragLocation(int mX, int mY, int& type, int& index) const { start_func
  197. type = DRAG_NONE;
  198. int imgSize = paddedWidth;
  199. int imgAcross = paddedHeight;
  200. if (showVertical) {
  201. swap(mX, mY);
  202. imgSize = paddedHeight;
  203. imgAcross = paddedWidth;
  204. }
  205. // Buttons have the 'max' height for now
  206. if (mY < IMAGE_SEPARATION) return;
  207. if (mY >= IMAGE_SEPARATION + buttonHeight) return;
  208. if (mX < buttonWidth) {
  209. type = DRAG_STRIP_MORE;
  210. return;
  211. }
  212. mX -= buttonWidth + IMAGE_SEPARATION;
  213. if (mX < 0) return;
  214. if (mX < buttonWidth) {
  215. type = DRAG_STRIP_PREV;
  216. return;
  217. }
  218. mX -= buttonWidth + IMAGE_SEPARATION;
  219. if (mX < 0) return;
  220. // Images as a grid
  221. int col = mX / (IMAGE_BEVEL * 2 + imgSize + IMAGE_SEPARATION);
  222. int colI = mX % (IMAGE_BEVEL * 2 + imgSize + IMAGE_SEPARATION);
  223. if (col < numImagesShownCount) {
  224. if (colI >= IMAGE_BEVEL * 2 + imgSize) return;
  225. int row = (mY - IMAGE_SEPARATION) / (IMAGE_BEVEL * 2 + imgAcross + IMAGE_SEPARATION);
  226. int rowI = (mY - IMAGE_SEPARATION) % (IMAGE_BEVEL * 2 + imgAcross + IMAGE_SEPARATION);
  227. if (row >= numImagesShownAcross) return;
  228. if (rowI >= IMAGE_BEVEL * 2 + imgAcross) return;
  229. type = SELECTED_STRIP;
  230. index = col + row * numImagesShownCount;
  231. return;
  232. }
  233. mX -= (IMAGE_BEVEL * 2 + imgSize + IMAGE_SEPARATION) * numImagesShownCount;
  234. if (mX < 0) return;
  235. if (mX < buttonWidth) {
  236. type = DRAG_STRIP_NEXT;
  237. return;
  238. }
  239. // (strip only?)
  240. if (!extendedFeatures) return;
  241. mX -= buttonWidth + IMAGE_GUTTER;
  242. if (mX < 0) return;
  243. if (mX < IMAGE_BEVEL * 2 + imgSize) {
  244. // ("none" is centered- ensure only center area is clicked)
  245. if ((mY >= blankTileCenter + IMAGE_SEPARATION) && (mY < blankTileCenter + IMAGE_BEVEL * 2 + imgAcross + IMAGE_SEPARATION)) type = SELECTED_NONE;
  246. return;
  247. }
  248. mX -= IMAGE_BEVEL * 2 + imgSize + IMAGE_GUTTER;
  249. if (mX < 0) return;
  250. if (mX < buttonWidth) {
  251. type = DRAG_RECENT_MORE;
  252. return;
  253. }
  254. mX -= buttonWidth + IMAGE_SEPARATION;
  255. if (mX < 0) return;
  256. // patterns as a grid
  257. col = mX / (IMAGE_BEVEL * 2 + imgSize + IMAGE_SEPARATION);
  258. colI = mX % (IMAGE_BEVEL * 2 + imgSize + IMAGE_SEPARATION);
  259. if (col < numPatternsShown) {
  260. if (colI >= IMAGE_BEVEL * 2 + imgSize) return;
  261. int row = (mY - IMAGE_SEPARATION) / (IMAGE_BEVEL * 2 + imgAcross + IMAGE_SEPARATION);
  262. int rowI = (mY - IMAGE_SEPARATION) % (IMAGE_BEVEL * 2 + imgAcross + IMAGE_SEPARATION);
  263. if (row >= numImagesShownAcross) return;
  264. if (rowI >= IMAGE_BEVEL * 2 + imgAcross) return;
  265. index = col + row * numPatternsShown;
  266. if (index >= ShownImages::PATTERN_RECENT_MAX) index = 0;
  267. else type = SELECTED_RECENT;
  268. return;
  269. }
  270. }
  271. int ImageSelect::event(int hasFocus, const SDL_Event* event) { start_func
  272. switch (event->type) {
  273. case SDL_MOUSEBUTTONDOWN:
  274. case SDL_MOUSEBUTTONDBL: {
  275. // @TODO: Double click special?
  276. if (event->button.button == SDL_BUTTON_LEFT) {
  277. int newType;
  278. int newIndex;
  279. dragLocation(event->button.x, event->button.y, newType, newIndex);
  280. if (newType >= DRAG_STRIP_PREV) {
  281. prevDrag = dragType = newType;
  282. if (dragType == DRAG_STRIP_PREV) prevImage();
  283. else if (dragType == DRAG_STRIP_NEXT) nextImage();
  284. }
  285. else {
  286. prevDrag = dragType = DRAG_NONE;
  287. if (newType != DRAG_NONE) {
  288. if (newType == SELECTED_STRIP) {
  289. selectedType = SELECTED_STRIP;
  290. selectedIndex = newIndex;
  291. }
  292. else if (newType == SELECTED_NONE) {
  293. selectedType = SELECTED_NONE;
  294. }
  295. else if (newType == SELECTED_RECENT) {
  296. selectedType = SELECTED_RECENT;
  297. selectedIndex = newIndex;
  298. }
  299. }
  300. }
  301. setDirty();
  302. return 1;
  303. }
  304. if (event->button.button == SDL_BUTTON_WHEELUP) {
  305. prevImage();
  306. return 1;
  307. }
  308. if (event->button.button == SDL_BUTTON_WHEELDOWN) {
  309. nextImage();
  310. return 1;
  311. }
  312. break;
  313. }
  314. case SDL_MOUSEBUTTONUP: {
  315. if (dragType != DRAG_NONE) {
  316. if (dragType == DRAG_RECENT_MORE) {
  317. // @TODO:
  318. }
  319. else if ((dragType == DRAG_STRIP_MORE) && (numImages)) {
  320. // @TODO: doesn't apply current color to image chooser
  321. // @TODO: doesn't appear in a good area for vertical
  322. ImageChooser* imgChoose = new ImageChooser(tileset);
  323. int chosenTile = imgChoose->runModal(getScreenX(), getScreenY());
  324. delete imgChoose;
  325. if (chosenTile > 0) selectImage(chosenTile);
  326. }
  327. dragType = DRAG_NONE;
  328. setDirty();
  329. return 1;
  330. }
  331. break;
  332. }
  333. case SDL_MOUSEMOTION: {
  334. if (event->motion.state & SDL_BUTTON_LMASK) {
  335. int newType;
  336. int newIndex;
  337. dragLocation(event->button.x, event->button.y, newType, newIndex);
  338. if (newType == prevDrag) {
  339. dragType = prevDrag;
  340. }
  341. else {
  342. dragType = DRAG_NONE;
  343. }
  344. setDirty();
  345. return 1;
  346. }
  347. break;
  348. }
  349. }
  350. return 0;
  351. }
  352. void ImageSelect::nextImage() { start_func
  353. if (shown.strip.index >= numImages) shown.strip.index = 1;
  354. else ++shown.strip.index;
  355. saveDef();
  356. setDirty();
  357. }
  358. void ImageSelect::prevImage() { start_func
  359. if (shown.strip.index <= 1) shown.strip.index = numImages;
  360. else --shown.strip.index;
  361. saveDef();
  362. setDirty();
  363. }
  364. void ImageSelect::selectImage(int tile) { start_func
  365. selectedType = SELECTED_STRIP;
  366. // Reset to middle-est image
  367. selectedIndex = numImagesShownCount / 2 + (numImagesShownAcross / 2) * numImagesShownCount;
  368. if (numImages) {
  369. shown.strip.index = tile - selectedIndex;
  370. while (shown.strip.index <= 0) {
  371. shown.strip.index += numImages;
  372. }
  373. }
  374. else {
  375. shown.strip.index = 0;
  376. }
  377. saveDef();
  378. setDirty();
  379. }
  380. void ImageSelect::saveDef() { start_func
  381. // Save defaults
  382. if ((tileset) && (shownBank)) {
  383. map<const void*,ShownImages>::iterator pos = shownBank->find(tileset);
  384. if (pos != shownBank->end()) (*pos).second = shown;
  385. }
  386. }
  387. void ImageSelect::display(SDL_Surface* destSurface, Rect& toDisplay, const Rect& clipArea, int xOffset, int yOffset) { start_func
  388. assert(destSurface);
  389. if (visible) {
  390. // If dirty, redraw all
  391. if (dirty) {
  392. getRect(toDisplay);
  393. toDisplay.x += xOffset;
  394. toDisplay.y += yOffset;
  395. dirty = 0;
  396. intersectRects(toDisplay, clipArea);
  397. }
  398. // Anything to draw?
  399. if (toDisplay.w) {
  400. SDL_SetClipRect(destSurface, &toDisplay);
  401. xOffset += x;
  402. yOffset += y;
  403. int imgSize = paddedWidth;
  404. int imgAcross = paddedHeight;
  405. if (showVertical) {
  406. imgSize = paddedHeight;
  407. imgAcross = paddedWidth;
  408. }
  409. // This toolbar only redraws images that are part of the dirty area
  410. SDL_FillRect(destSurface, &toDisplay, guiPacked[COLOR_FILL]);
  411. // First image row- more, prev, images in grid, next
  412. int xPos = 0;
  413. int yPos = 0;
  414. xPos += drawButton(destSurface, xOffset, yOffset, xPos, yPos + IMAGE_SEPARATION, wtButtonExpand, dragType == DRAG_STRIP_MORE);
  415. xPos += IMAGE_SEPARATION;
  416. xPos += drawButton(destSurface, xOffset, yOffset, xPos, yPos + IMAGE_SEPARATION, showVertical ? wtButtonPrevV : wtButtonPrevH, dragType == DRAG_STRIP_PREV);
  417. Image img = shown.strip;
  418. int startX = xPos;
  419. for (int pos = 0; pos < numImagesShownCount * numImagesShownAcross; ++pos) {
  420. if (numImages) {
  421. img.index = (shown.strip.index + pos - 1) % numImages + 1;
  422. xPos += drawPattern(destSurface, xOffset, yOffset, xPos, yPos, (selectedType == SELECTED_STRIP) && (selectedIndex == pos), &img, NULL);
  423. }
  424. else {
  425. xPos += drawBlankImage(destSurface, xOffset, yOffset, xPos, yPos, (selectedType == SELECTED_STRIP) && (selectedIndex == pos));
  426. }
  427. if ((pos % numImagesShownCount) == (numImagesShownCount - 1)) {
  428. xPos = startX;
  429. yPos += imgAcross + IMAGE_SEPARATION + IMAGE_BEVEL * 2;
  430. }
  431. }
  432. // Reset to "topmost"
  433. yPos = 0;
  434. xPos += (imgSize + IMAGE_SEPARATION + IMAGE_BEVEL * 2) * numImagesShownCount;
  435. // (next button)
  436. xPos += IMAGE_SEPARATION;
  437. xPos += drawButton(destSurface, xOffset, yOffset, xPos, yPos + IMAGE_SEPARATION, showVertical ? wtButtonNextV : wtButtonNextH, dragType == DRAG_STRIP_NEXT);
  438. if (extendedFeatures) {
  439. // Blank image (centered)
  440. xPos += IMAGE_GUTTER - IMAGE_SEPARATION;
  441. xPos += drawBlankImage(destSurface, xOffset, yOffset, xPos, yPos + blankTileCenter, selectedType == SELECTED_NONE);
  442. // Second image row- more, recents (grid)
  443. xPos += IMAGE_GUTTER;
  444. xPos += drawButton(destSurface, xOffset, yOffset, xPos, yPos + IMAGE_SEPARATION, wtButtonExpand, dragType == DRAG_RECENT_MORE);
  445. int startX = xPos;
  446. for (int pos = 0; pos < numPatternsShown * numImagesShownAcross; ++pos) {
  447. if (pos >= ShownImages::PATTERN_RECENT_MAX) break;
  448. xPos += drawPattern(destSurface, xOffset, yOffset, xPos, yPos, (selectedType == SELECTED_RECENT) && (selectedIndex == pos), NULL, &shown.recent[pos]);
  449. if ((pos % numPatternsShown) == (numPatternsShown - 1)) {
  450. xPos = startX;
  451. yPos += imgAcross + IMAGE_SEPARATION + IMAGE_BEVEL * 2;
  452. }
  453. }
  454. // (remember to reset to "topmost" if we ever have anything after patterns
  455. }
  456. }
  457. }
  458. }
  459. int ImageSelect::drawButton(SDL_Surface* destSurface, int baseX, int baseY, int offsX, int offsY, const string& wText, int pressed) const { start_func
  460. // @TODO: check if button intercepts clip area
  461. int offset = 0;
  462. int w, h, x, y;
  463. if (showVertical) {
  464. w = buttonHeight;
  465. h = buttonWidth;
  466. x = baseX + offsY;
  467. y = baseY + offsX;
  468. }
  469. else {
  470. w = buttonWidth;
  471. h = buttonHeight;
  472. x = baseX + offsX;
  473. y = baseY + offsY;
  474. }
  475. if (pressed) {
  476. // Non-gradient: SDL_FillRect(...);
  477. drawGradient(x, y, w, h, guiRGB[COLOR_BUTTONDOWN1], guiRGB[COLOR_BUTTONDOWN2], destSurface);
  478. drawGuiBoxInvert(x, y, w, h, 2, destSurface);
  479. offset = 1;
  480. }
  481. else {
  482. drawGuiBoxInvert(x, y, w, h, 1, destSurface);
  483. drawGuiBox(x + 1, y + 1, w - 2, h - 2, 2, destSurface);
  484. // Non-gradient: nothing
  485. drawGradient(x + 3, y + 3, w - 6, h - 6, guiRGB[COLOR_BUTTONFILL1], guiRGB[COLOR_BUTTONFILL2], destSurface);
  486. }
  487. drawText(wText, guiRGB[COLOR_TEXT], x + (w - fontWidth(wText, FONT_WIDGET)) / 2 + offset, y + (h - fontHeight(FONT_WIDGET)) / 2 + offset, destSurface, FONT_WIDGET);
  488. return buttonWidth;
  489. }
  490. int ImageSelect::drawBlankImage(SDL_Surface* destSurface, int baseX, int baseY, int offsX, int offsY, int selected) const { start_func
  491. // @TODO: check if button intercepts clip area
  492. int x, y;
  493. if (showVertical) {
  494. x = baseX + offsY;
  495. y = baseY + offsX;
  496. }
  497. else {
  498. x = baseX + offsX;
  499. y = baseY + offsY;
  500. }
  501. if (selected) {
  502. drawGuiBoxInvert(x, y,
  503. paddedWidth + IMAGE_SEPARATION * 2 + IMAGE_BEVEL * 2,
  504. paddedHeight + IMAGE_SEPARATION * 2 + IMAGE_BEVEL * 2,
  505. IMAGE_BEVEL, destSurface);
  506. drawGradient(x + IMAGE_BEVEL, y + IMAGE_BEVEL,
  507. paddedWidth + IMAGE_SEPARATION * 2,
  508. paddedHeight + IMAGE_SEPARATION * 2,
  509. guiRGB[COLOR_SELECTION1], guiRGB[COLOR_SELECTION2], destSurface);
  510. }
  511. else {
  512. drawGuiBoxInvert(x + IMAGE_SEPARATION, y + IMAGE_SEPARATION,
  513. paddedWidth + IMAGE_BEVEL * 2, paddedHeight + IMAGE_BEVEL * 2,
  514. IMAGE_BEVEL, destSurface);
  515. }
  516. x += IMAGE_SEPARATION + IMAGE_BEVEL;
  517. y += IMAGE_SEPARATION + IMAGE_BEVEL;
  518. drawRect(x, y,
  519. paddedWidth, paddedHeight,
  520. guiPacked[COLOR_TRANSPARENT1], destSurface);
  521. drawLine(x, y, x + paddedWidth - 1, y + paddedHeight - 1,
  522. guiPacked[COLOR_TRANSPARENT2], destSurface);
  523. drawLine(x + paddedWidth - 1, y, x, y + paddedHeight - 1,
  524. guiPacked[COLOR_TRANSPARENT2], destSurface);
  525. return (showVertical ? paddedHeight : paddedWidth) + IMAGE_SEPARATION + IMAGE_BEVEL * 2;
  526. }
  527. int ImageSelect::drawPattern(SDL_Surface* destSurface, int baseX, int baseY, int offsX, int offsY, int selected, Image* image, Pattern* pattern) const { start_func
  528. // @TODO: check if pattern intercepts clip area
  529. assert(pattern || (image && (image->index)));
  530. int x, y;
  531. if (showVertical) {
  532. x = baseX + offsY;
  533. y = baseY + offsX;
  534. }
  535. else {
  536. x = baseX + offsX;
  537. y = baseY + offsY;
  538. }
  539. if (selected) {
  540. drawGuiBoxInvert(x, y,
  541. paddedWidth + IMAGE_SEPARATION * 2 + IMAGE_BEVEL * 2,
  542. paddedHeight + IMAGE_SEPARATION * 2 + IMAGE_BEVEL * 2,
  543. IMAGE_BEVEL, destSurface);
  544. drawGradient(x + IMAGE_BEVEL, y + IMAGE_BEVEL,
  545. paddedWidth + IMAGE_SEPARATION * 2,
  546. paddedHeight + IMAGE_SEPARATION * 2,
  547. guiRGB[COLOR_SELECTION1], guiRGB[COLOR_SELECTION2], destSurface);
  548. }
  549. else {
  550. drawGuiBoxInvert(x + IMAGE_SEPARATION, y + IMAGE_SEPARATION,
  551. paddedWidth + IMAGE_BEVEL * 2, paddedHeight + IMAGE_BEVEL * 2,
  552. IMAGE_BEVEL, destSurface);
  553. }
  554. drawRect(x + IMAGE_SEPARATION + IMAGE_BEVEL, y + IMAGE_SEPARATION + IMAGE_BEVEL,
  555. paddedWidth, paddedHeight,
  556. SDL_MapRGB(destSurface->format, 0, 0, 0), destSurface);
  557. if ((pattern) && (pattern->count == 1)) image = &pattern->images[0].image;
  558. if (image) {
  559. tileset->blitTileFx(image->index, destSurface,
  560. x + IMAGE_SEPARATION + IMAGE_BEVEL, y + IMAGE_SEPARATION + IMAGE_BEVEL,
  561. image->alpha, image->effects & Image::EFFECTS_MIRROR,
  562. image->effects & Image::EFFECTS_FLIP,
  563. image->effects & Image::EFFECTS_ROTATE, image->color);
  564. }
  565. else {
  566. // @TODO: show pattern here
  567. }
  568. return (showVertical ? paddedHeight : paddedWidth) + IMAGE_SEPARATION + IMAGE_BEVEL * 2;
  569. }
  570. Window::WindowType ImageSelect::windowType() const { start_func
  571. return WINDOW_CLIENT;
  572. }
  573. const char* ImageSelect::tooltip(int xPos, int yPos) const { start_func
  574. return "@TODO: tooltips (account for vertical)";
  575. }
  576. void ImageSelect::resize(int newWidth, int newHeight, int newViewWidth, int newViewHeight, int fromParent) { start_func
  577. if (newViewWidth == -1) newViewWidth = viewWidth;
  578. if (newViewWidth != -1) newWidth = newViewWidth;
  579. if (newViewHeight == -1) newViewHeight = viewHeight;
  580. if (newViewHeight != -1) newHeight = newViewHeight;
  581. int minReq = 0;
  582. if (newHeight > newWidth) showVertical = 1;
  583. else showVertical = 0;
  584. // Swap?
  585. int imgSize = paddedWidth;
  586. int imgAcross = paddedHeight;
  587. if (showVertical) {
  588. swap(newWidth, newHeight);
  589. imgSize = paddedHeight;
  590. imgAcross = paddedWidth;
  591. }
  592. if (extendedFeatures) {
  593. int patternArea = newWidth;
  594. // layout- btn|sep|btn|sep|(pad|sep x1+)|btn|gutter|pad|gutter|btn|(sep|pad x0+)
  595. // All required elements (4 buttons, blank image, all required seps and gutters)
  596. minReq = buttonWidth * 4 + IMAGE_GUTTER * 2 + imgSize + IMAGE_BEVEL * 2 + IMAGE_SEPARATION * 2;
  597. patternArea -= minReq;
  598. // First set of images goes to regular images
  599. numImagesShownCount = patternArea / (imgSize + IMAGE_BEVEL * 2 + IMAGE_SEPARATION);
  600. if (numImagesShownCount < 1) numImagesShownCount = 1;
  601. // Any extras go to patterns
  602. if (numImagesShownCount > IMAGE_COUNT) {
  603. numPatternsShown = numImagesShownCount - IMAGE_COUNT;
  604. numImagesShownCount = IMAGE_COUNT;
  605. }
  606. else {
  607. numPatternsShown = 0;
  608. }
  609. }
  610. else {
  611. int imagesArea = newWidth;
  612. minReq = buttonWidth * 3 + IMAGE_SEPARATION * 2;
  613. imagesArea -= minReq;
  614. numImagesShownCount = imagesArea / (imgSize + IMAGE_BEVEL * 2 + IMAGE_SEPARATION);
  615. if (numImagesShownCount < 1) numImagesShownCount = 1;
  616. numPatternsShown = 0;
  617. }
  618. numImagesShownAcross = (newHeight - IMAGE_SEPARATION) / (imgAcross + IMAGE_BEVEL * 2 + IMAGE_SEPARATION);
  619. if (numImagesShownAcross < 1) numImagesShownAcross = 1;
  620. newWidth = minReq + (numPatternsShown + numImagesShownCount) * (imgSize + IMAGE_BEVEL * 2 + IMAGE_SEPARATION);
  621. newHeight = IMAGE_SEPARATION + numImagesShownAcross * (imgAcross + IMAGE_BEVEL * 2 + IMAGE_SEPARATION);
  622. buttonHeight = newHeight - IMAGE_SEPARATION * 2;
  623. blankTileCenter = (newHeight - imgAcross) / 2 - IMAGE_BEVEL - IMAGE_SEPARATION;
  624. // @TODO: attempt to retain the same selected item overall?
  625. if (selectedType == SELECTED_STRIP) {
  626. if (selectedIndex >= numImagesShownCount * numImagesShownAcross)
  627. selectedIndex = numImagesShownCount * numImagesShownAcross - 1;
  628. }
  629. else if (selectedType == SELECTED_RECENT) {
  630. if (selectedIndex >= numPatternsShown * numImagesShownAcross)
  631. selectedIndex = numPatternsShown * numImagesShownAcross - 1;
  632. }
  633. // Swap back?
  634. if (showVertical) swap(newWidth, newHeight);
  635. // Recurse back to parent until we get a match
  636. if ((newWidth != width) || (newHeight != height)) fromParent = 0;
  637. Window::resize(newWidth, newHeight, newViewWidth, newViewHeight, fromParent);
  638. }