gcsx_layerprop.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* GCSx
  2. ** LAYERPROP.CPP
  3. **
  4. ** Layer 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. LayerPropertiesRadioButton::LayerPropertiesRadioButton(int rId, const string& rLabel, int* rSetting, int rValue) : WRadioButton(rId, rLabel, rSetting, rValue) { start_func
  25. }
  26. int LayerPropertiesRadioButton::state(int checked, int fromLoad) { start_func
  27. if ((checked > 0) && (parent)) {
  28. Dialog* myParent = dynamic_cast<Dialog*>(parent);
  29. if (value == Layer::LAYER_EMPTY) {
  30. myParent->findWidget(LayerPropertiesDialog::ID_WIDTH)->disable();
  31. myParent->findWidget(LayerPropertiesDialog::ID_HEIGHT)->disable();
  32. myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 0)->disable();
  33. myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 1)->disable();
  34. }
  35. else {
  36. myParent->findWidget(LayerPropertiesDialog::ID_WIDTH)->enable();
  37. myParent->findWidget(LayerPropertiesDialog::ID_HEIGHT)->enable();
  38. myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 0)->enable();
  39. myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 1)->enable();
  40. }
  41. if (value == Layer::LAYER_TILE) {
  42. myParent->findWidget(LayerPropertiesDialog::ID_TILESET)->enable();
  43. myParent->findWidget(LayerPropertiesDialog::ID_EXTENDED)->enable();
  44. myParent->findWidget(LayerPropertiesDialog::ID_EFFECTS)->enable();
  45. dynamic_cast<WStatic*>(myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 0))->changeText("tiles");
  46. dynamic_cast<WStatic*>(myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 1))->changeText("tiles");
  47. }
  48. else {
  49. myParent->findWidget(LayerPropertiesDialog::ID_TILESET)->disable();
  50. myParent->findWidget(LayerPropertiesDialog::ID_EXTENDED)->disable();
  51. myParent->findWidget(LayerPropertiesDialog::ID_EFFECTS)->disable();
  52. dynamic_cast<WStatic*>(myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 0))->changeText("pixels");
  53. dynamic_cast<WStatic*>(myParent->findWidget(LayerPropertiesDialog::ID_SIZELABEL, 1))->changeText("pixels");
  54. }
  55. }
  56. return WRadioButton::state(checked, fromLoad);
  57. }
  58. LayerPropertiesDialog* LayerPropertiesDialog::dialog = NULL;
  59. LayerPropertiesDialog* LayerPropertiesDialog::create() { start_func
  60. if (!dialog) {
  61. dialog = new LayerPropertiesDialog();
  62. }
  63. return dialog;
  64. }
  65. void LayerPropertiesDialog::destroy() { start_func
  66. if (dialog) {
  67. delete dialog;
  68. dialog = NULL;
  69. }
  70. }
  71. LayerPropertiesDialog::LayerPropertiesDialog() : Dialog("Layer Properties") { start_func
  72. initialized = 0;
  73. }
  74. Dialog::ButtonAction LayerPropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
  75. if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;
  76. // Another layer with same name?
  77. string lName = dynamic_cast<WTextBox*>(findWidget(ID_NAME))->state();
  78. toLower(lName);
  79. int found = layer->getScene()->findLayer(lName);
  80. if ((found >= 0) && (layer->getScene()->getLayer(found) != layer)) {
  81. guiErrorBox("A layer by that name already exists in this scene- please choose another.", errorTitleDuplicateName);
  82. return BUTTON_NOTHING;
  83. }
  84. if (lName.empty()) {
  85. guiErrorBox("Please enter a name for this layer.", errorTitleMissingName);
  86. return BUTTON_NOTHING;
  87. }
  88. // @TODO: image/font layers may have different sizing requirements
  89. // @TODO: Reduction in size causes warning if not new
  90. return Dialog::verifyEntry(buttonId, buttonType);
  91. }
  92. int LayerPropertiesDialog::run(int allowTypeChange, Layer::LayerType* type, string* title, int* xSize, int* ySize, int* tilesetId, int* usesExtended, int* usesEffects, const LayerEdit* whichLayer) { start_func
  93. int typeInt = *type;
  94. layer = whichLayer;
  95. *usesExtended = *usesExtended ? 1 : 0;
  96. *usesEffects = *usesEffects ? 1 : 0;
  97. if (!initialized) {
  98. Widget* w = NULL;
  99. w = new WStatic(ID_LABEL, "\tName:");
  100. w->addTo(this);
  101. w = new WTextBox(ID_NAME, title, 0);
  102. w->addTo(this);
  103. w = new LayerPropertiesRadioButton(ID_TYPE, "\tEmpty layer", &typeInt, Layer::LAYER_EMPTY);
  104. w->addTo(this);
  105. w = new LayerPropertiesRadioButton(ID_TYPE, "\tTile layer", &typeInt, Layer::LAYER_TILE);
  106. w->addTo(this);
  107. w = new LayerPropertiesRadioButton(ID_TYPE, "\tImage layer", &typeInt, Layer::LAYER_IMAGE);
  108. w->addTo(this);
  109. w = new LayerPropertiesRadioButton(ID_TYPE, "\tFont layer", &typeInt, Layer::LAYER_FONT);
  110. w->addTo(this);
  111. w = new WCheckBox(ID_EXTENDED, "E\txtended data", usesExtended, 1);
  112. w->setToolTip("Extended data allows customizing collision maps, alpha (translucency), and animation");
  113. w->addTo(this);
  114. w = new WCheckBox(ID_EFFECTS, "E\tffects data", usesEffects, 1);
  115. w->setToolTip("Effects data is currently unused");
  116. w->addTo(this);
  117. w = new WStatic(ID_LABEL, "\tLibrary:");
  118. w->addTo(this);
  119. w = new WListBox(ID_TILESET, tilesetId);
  120. w->addTo(this);
  121. makePretty();
  122. w = new WStatic(ID_LABEL, "\tWidth:");
  123. w->addTo(this);
  124. w = new WNumberBox(ID_WIDTH, xSize, 1, MAX_LAYERTILESIZE);
  125. w->addTo(this);
  126. w = new WStatic(ID_SIZELABEL, "tiles");
  127. w->addTo(this);
  128. w = new WStatic(ID_LABEL, "\tHeight:");
  129. w->addTo(this);
  130. w = new WNumberBox(ID_HEIGHT, ySize, 1, MAX_LAYERTILESIZE);
  131. w->addTo(this);
  132. w = new WStatic(ID_SIZELABEL, "tiles");
  133. w->addTo(this);
  134. w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
  135. w->addTo(this);
  136. w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
  137. w->addTo(this);
  138. makePretty(3, 1, 1);
  139. initialized = 1;
  140. }
  141. else {
  142. findWidget(ID_NAME)->changeStorage(title);
  143. for (int pos = 0; pos < 4; ++pos) {
  144. findWidget(ID_TYPE, pos)->changeStorage(&typeInt);
  145. }
  146. findWidget(ID_WIDTH)->changeStorage(xSize);
  147. findWidget(ID_HEIGHT)->changeStorage(ySize);
  148. findWidget(ID_EXTENDED)->changeStorage(usesExtended);
  149. findWidget(ID_EFFECTS)->changeStorage(usesEffects);
  150. findWidget(ID_TILESET)->changeStorage(tilesetId);
  151. }
  152. // Fill listbox with tilesets (non-font)
  153. WListBox* l = dynamic_cast<WListBox*>(findWidget(ID_TILESET));
  154. const WorldEdit* world = layer->getWorldEdit();
  155. l->clear();
  156. int found = 0;
  157. World::TileSetIndex::const_iterator end = world->endTileSet();
  158. for (World::TileSetIndex::const_iterator pos = world->beginTileSet(); pos != end; ++pos) {
  159. const TileSetEdit* ts = dynamic_cast<TileSetEdit*>((*pos).second);
  160. if (!ts->getIsFont()) {
  161. ++found;
  162. l->addItem(ListEntry(ts->getName(), ts->getId()));
  163. }
  164. }
  165. // No tilesets? disallow tileset option
  166. int allowTileLayer = 1;
  167. if (!found) {
  168. if (typeInt == Layer::LAYER_TILE) {
  169. // Should never get here with LAYER_TILE type unless a new layer,
  170. // if there's no tile sets
  171. assert(allowTypeChange);
  172. typeInt = Layer::LAYER_EMPTY;
  173. }
  174. allowTileLayer = 0;
  175. }
  176. else {
  177. l->sortItems();
  178. }
  179. for (int pos = 0; pos < 4; ++pos) {
  180. WRadioButton* radio = dynamic_cast<WRadioButton*>(findWidget(ID_TYPE, pos));
  181. int value = radio->getValue();
  182. // Disable tile layers specifically, in some circumstances
  183. // Disable all layer types if not allowing type changes
  184. if (((value == Layer::LAYER_TILE) && (!allowTileLayer)) || (!allowTypeChange)) radio->disable();
  185. else radio->enable();
  186. }
  187. // Hit OK?
  188. if (runModal() == ID_OK) {
  189. *type = (Layer::LayerType)typeInt;
  190. return 1;
  191. }
  192. return 0;
  193. }