gcsx_spawnprop.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* GCSx
  2. ** SPAWNPROP.CPP
  3. **
  4. ** Spawn-point properties dialog
  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. // Properties dialog
  25. SpawnPropertiesDialog* SpawnPropertiesDialog::dialogWithSprite = NULL;
  26. SpawnPropertiesDialog* SpawnPropertiesDialog::dialogNoSprite = NULL;
  27. // @TODO: save as global setting
  28. int SpawnPropertiesDialog::autoSync = 1;
  29. SpawnPropertiesDialog* SpawnPropertiesDialog::createWithSprite() { start_func
  30. if (!dialogWithSprite) {
  31. dialogWithSprite = new SpawnPropertiesDialog(1);
  32. }
  33. return dialogWithSprite;
  34. }
  35. SpawnPropertiesDialog* SpawnPropertiesDialog::createNoSprite() { start_func
  36. if (!dialogNoSprite) {
  37. dialogNoSprite = new SpawnPropertiesDialog(0);
  38. }
  39. return dialogNoSprite;
  40. }
  41. void SpawnPropertiesDialog::destroy() { start_func
  42. if (dialogWithSprite) {
  43. delete dialogWithSprite;
  44. dialogWithSprite = NULL;
  45. }
  46. if (dialogNoSprite) {
  47. delete dialogNoSprite;
  48. dialogNoSprite = NULL;
  49. }
  50. }
  51. SpawnPropertiesDialog::SpawnPropertiesDialog(int isSprite) : ImageDialog() { start_func
  52. initialized = 0;
  53. tree = NULL;
  54. hasSprite = isSprite;
  55. }
  56. SpawnPropertiesDialog::~SpawnPropertiesDialog() { start_func
  57. if (tree) {
  58. WidgetScroll* ws = dynamic_cast<WidgetScroll*>(tree->getParent());
  59. if (ws) ws->newClient();
  60. delete tree;
  61. }
  62. }
  63. Dialog::ButtonAction SpawnPropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
  64. if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;
  65. // No script?
  66. TreeView* item = tree->findSelected();
  67. if ((!item) || (!wWorld->findScript(item->getCode()))) {
  68. if (hasSprite) {
  69. // No sprite?
  70. AnimGroup* animGroup = NULL;
  71. TileSet* tileSet = NULL;
  72. runComplete(&animGroup, &tileSet, NULL, 1);
  73. if ((!animGroup) && (!tileSet)) {
  74. guiErrorBox("You must select at least a script or an image for a sprite.", errorTitleInvalidEntry);
  75. return BUTTON_NOTHING;
  76. }
  77. }
  78. else {
  79. // A sprite-less spawn- must have a script
  80. guiErrorBox("You must select a script.", errorTitleInvalidEntry);
  81. return BUTTON_NOTHING;
  82. }
  83. }
  84. return Dialog::verifyEntry(buttonId, buttonType);
  85. }
  86. int SpawnPropertiesDialog::run(string* title, Script** script, AnimGroup** animGroup, TileSet** tileSet, int* animTileId, WorldEdit* whichWorld) { start_func
  87. assert(title);
  88. assert(script);
  89. assert(animGroup);
  90. assert(tileSet);
  91. assert(animTileId);
  92. assert(whichWorld);
  93. titleS = title;
  94. wWorld = whichWorld;
  95. // @TODO: Tooltips on this dialog
  96. if (!initialized) {
  97. Widget* w = NULL;
  98. w = new WCheckBox(ID_SYNC,"\tCopy settings from script", &autoSync, 1);
  99. w->addTo(this);
  100. w = new WStatic(ID_SCRIPTLABEL, "\tScript:");
  101. w->addTo(this);
  102. tree = new TreeView(blankString, NULL, 0, NULL, 1);
  103. tree->addTo(this, -1, 5);
  104. w = new WStatic(ID_LABEL, "\tName:");
  105. w->addTo(this);
  106. w = new WTextBox(ID_NAME, title, 0);
  107. w->addTo(this);
  108. if (hasSprite) {
  109. w = new WStatic(ID_LABEL, "\tLibrary:");
  110. w->addTo(this);
  111. addLibTree(ID_LIBRARY);
  112. makePretty(2, 0);
  113. w = findWidget(ID_LIBRARY);
  114. int x = w->getX() + w->getWidth() + GUI_DIALOG_GUTTERWIDTH;
  115. int h = w->getY() + w->getHeight();
  116. int y = findWidget(ID_SYNC)->getY();
  117. int y2 = findWidget(ID_SCRIPTLABEL)->getY();
  118. w = new WStatic(ID_LABEL, "\tImage:");
  119. w->addTo(this);
  120. w->move(x, y);
  121. ++lastWidgetArranged;
  122. addImgChoose(ID_IMAGE);
  123. w = findWidget(ID_IMAGE);
  124. w->move(x, y2);
  125. w->resize(w->getWidth(), h - y2);
  126. resize(width + w->getWidth() + GUI_DIALOG_GUTTERWIDTH, height);
  127. ++lastWidgetArranged;
  128. }
  129. w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
  130. w->addTo(this);
  131. w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
  132. w->addTo(this);
  133. makePretty(2, 0);
  134. setTitle("Object Properties");
  135. initialized = 1;
  136. }
  137. else {
  138. findWidget(ID_NAME)->changeStorage(title);
  139. findWidget(ID_SYNC)->changeStorage(&autoSync);
  140. }
  141. TreeView* item;
  142. tree->removeAll();
  143. if (hasSprite) tree->insert(new TreeView("(none)", this, 0, treeViewWrap), *script == NULL);
  144. World::ScriptIndex::const_iterator end = wWorld->endScript();
  145. for (World::ScriptIndex::const_iterator pos = wWorld->beginScript(); pos != end; ++pos) {
  146. ScriptEdit* toAdd = dynamic_cast<ScriptEdit*>((*pos).second);
  147. if (toAdd->getType() == Script::SCRIPT_CODE) {
  148. tree->insert(item = new TreeView(toAdd->getName(), this, toAdd->getId(), treeViewWrap), *script == toAdd);
  149. item->setIcon(toAdd->getDefaultId() ? 13 : 12);
  150. }
  151. }
  152. if (hasSprite) {
  153. runPrep(wWorld, *animGroup, *tileSet, *animTileId);
  154. }
  155. // Hit OK?
  156. if (runModal() == ID_OK) {
  157. // Script
  158. item = tree->findSelected();
  159. if (item) *script = wWorld->findScript(item->getCode());
  160. else *script = NULL;
  161. // Sprite
  162. if (hasSprite) {
  163. runComplete(animGroup, tileSet, animTileId);
  164. }
  165. return 1;
  166. }
  167. if (hasSprite) runComplete();
  168. return 0;
  169. }
  170. int SpawnPropertiesDialog::treeViewWrap(void* ptr, int code, int command, int check) { start_func
  171. return ((SpawnPropertiesDialog*)ptr)->treeView(code, command, check);
  172. }
  173. int SpawnPropertiesDialog::treeView(int code, int command, int check) { start_func
  174. if (check) {
  175. return Window::COMMAND_HIDE;
  176. }
  177. if ((command == LV_MOVE) && (open)) {
  178. findWidget(ID_SYNC)->apply();
  179. if (autoSync) {
  180. if (code) {
  181. Script* script = wWorld->findScript(code);
  182. if (script) {
  183. *titleS = script->getName();
  184. findWidget(ID_NAME)->load();
  185. selectImage(script->getDefaultAnimgroup(), script->getDefaultTileset(),
  186. script->getDefaultId());
  187. }
  188. }
  189. else {
  190. *titleS = blankString;
  191. findWidget(ID_NAME)->load();
  192. }
  193. }
  194. return 1;
  195. }
  196. return 0;
  197. }