gcsx_imgdialog.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* GCSx
  2. ** IMGDIALOG.CPP
  3. **
  4. ** Image selection dialog (from animgroup/tileset)
  5. ** Not a full dialog- used as a base for others
  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. ImageDialog::ImageDialog() : Dialog(blankString) { start_func
  26. libTree = NULL;
  27. imgChoose = NULL;
  28. world = NULL;
  29. libId = 0;
  30. }
  31. ImageDialog::~ImageDialog() { start_func
  32. if (libTree) {
  33. WidgetScroll* ws = dynamic_cast<WidgetScroll*>(libTree->getParent());
  34. if (ws) ws->newClient();
  35. delete libTree;
  36. }
  37. }
  38. void ImageDialog::addLibTree(int id) { start_func
  39. libTree = new TreeView(blankString, NULL, 0, NULL, 1);
  40. libTree->addTo(this, 0, 5, id);
  41. libId = id;
  42. }
  43. void ImageDialog::addImgChoose(int id) { start_func
  44. imgChoose = new ImageChooser(NULL);
  45. int w = -1;
  46. int h = -1;
  47. if (libId) {
  48. WidgetScroll* ws = dynamic_cast<WidgetScroll*>(findWidget(libId));
  49. if (ws) {
  50. w = ws->getClientWidth();
  51. h = ws->getClientHeight();
  52. }
  53. }
  54. imgChoose->addTo(this, w, h, id);
  55. }
  56. void ImageDialog::selectImage(AnimGroup* animGroup, TileSet* tileSet, int animTileId) { start_func
  57. TreeView* found = NULL;
  58. if (animGroup) {
  59. found = libTree->findRecursive(animGroup->getId(), treeViewWrapAnimGroup);
  60. }
  61. else if (tileSet) {
  62. found = libTree->findRecursive(tileSet->getId(), treeViewWrapTileSet);
  63. }
  64. if (found) {
  65. found->selectMe();
  66. imgChoose->cursorState(animTileId - 1);
  67. }
  68. }
  69. void ImageDialog::fillLibTree() { start_func
  70. libTree->removeAll();
  71. if (world) {
  72. libTree->insert(new TreeView("(no image)", this, 0, treeViewWrapAnimGroup), (animId == 0) && (tileId == 0));
  73. TreeView* item = NULL;
  74. World::AnimGroupIndex::const_iterator endAL = world->endAnimGroup();
  75. for (World::AnimGroupIndex::const_iterator pos = world->beginAnimGroup(); pos != endAL; ++pos) {
  76. AnimGroupEdit* pAnimGroup = dynamic_cast<AnimGroupEdit*>((*pos).second);
  77. libTree->insert(item = new TreeView(pAnimGroup->getName(), this, pAnimGroup->getId(), treeViewWrapAnimGroup), pAnimGroup->getId() == animId);
  78. item->setIcon(7);
  79. }
  80. World::TileSetIndex::const_iterator endTS = world->endTileSet();
  81. for (World::TileSetIndex::const_iterator pos = world->beginTileSet(); pos != endTS; ++pos) {
  82. TileSetEdit* pTileSet = dynamic_cast<TileSetEdit*>((*pos).second);
  83. libTree->insert(item = new TreeView(pTileSet->getName(), this, pTileSet->getId(), treeViewWrapTileSet), pTileSet->getId() == tileId);
  84. item->setIcon(pTileSet->getIsFont() ? 4 : (pTileSet->getCollisionCount() ? 9 : 5));
  85. }
  86. }
  87. }
  88. int ImageDialog::treeViewWrapTileSet(void* ptr, int code, int command, int check) { start_func
  89. return ((ImageDialog*)ptr)->treeViewTileSet(code, command, check);
  90. }
  91. int ImageDialog::treeViewTileSet(int code, int command, int check) { start_func
  92. if (check) {
  93. return Window::COMMAND_HIDE;
  94. }
  95. if (command == LV_MOVE) {
  96. TileSetEdit* tileset = dynamic_cast<TileSetEdit*>(world->findTileSet(code));
  97. if (tileset) {
  98. animId = 0;
  99. tileId = tileset->getId();
  100. imgChoose->setTileset(tileset);
  101. imgChoose->cursorState(subId - 1);
  102. }
  103. return 1;
  104. }
  105. return 0;
  106. }
  107. int ImageDialog::treeViewWrapAnimGroup(void* ptr, int code, int command, int check) { start_func
  108. return ((ImageDialog*)ptr)->treeViewAnimGroup(code, command, check);
  109. }
  110. int ImageDialog::treeViewAnimGroup(int code, int command, int check) { start_func
  111. if (check) {
  112. return Window::COMMAND_HIDE;
  113. }
  114. if (command == LV_MOVE) {
  115. if (code) {
  116. AnimGroupEdit* animgroup = dynamic_cast<AnimGroupEdit*>(world->findAnimGroup(code));
  117. if (animgroup) {
  118. tileId = 0;
  119. animId = animgroup->getId();
  120. imgChoose->setTileset(NULL);
  121. imgChoose->cursorState(subId - 1);
  122. }
  123. }
  124. else {
  125. tileId = 0;
  126. animId = 0;
  127. imgChoose->setTileset(NULL);
  128. imgChoose->cursorState(subId - 1);
  129. }
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. void ImageDialog::runComplete(AnimGroup** animGroup, TileSet** tileSet, int* animTileId, int justChecking) { start_func
  135. if ((animId) || (tileId))
  136. subId = imgChoose->cursorState() + 1;
  137. else
  138. subId = 0;
  139. AnimGroup* aLib = NULL;
  140. TileSet* tSet = NULL;
  141. if ((animId) && (subId > 0)) {
  142. aLib = world->findAnimGroup(animId);
  143. if (!aLib->getCount()) {
  144. subId = 0;
  145. aLib = NULL;
  146. }
  147. else assert(subId <= aLib->getCount());
  148. }
  149. else if ((tileId) && (subId > 0)) {
  150. tSet = world->findTileSet(tileId);
  151. if (!tSet->getCount()) {
  152. subId = 0;
  153. tSet = NULL;
  154. }
  155. else assert(subId <= tSet->getCount());
  156. }
  157. else {
  158. subId = 0;
  159. }
  160. if (animGroup) *animGroup = aLib;
  161. if (tileSet) *tileSet = tSet;
  162. if (animTileId) *animTileId = subId;
  163. if (!justChecking) {
  164. imgChoose->setTileset(NULL);
  165. world = NULL;
  166. }
  167. }
  168. void ImageDialog::runPrep(WorldEdit* nWorld, AnimGroup* animGroup, TileSet* tileSet, int animTileId) { start_func
  169. world = nWorld;
  170. animId = tileId = subId = 0;
  171. if (animGroup) {
  172. animId = animGroup->getId();
  173. subId = animTileId;
  174. }
  175. else if (tileSet) {
  176. tileId = tileSet->getId();
  177. subId = animTileId;
  178. }
  179. fillLibTree();
  180. }