2
0

PolycodeSpriteEditor.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeSpriteEditor.h"
  20. #include "PolycodeFrame.h"
  21. extern UIColorPicker *globalColorPicker;
  22. extern PolycodeFrame *globalFrame;
  23. extern UIGlobalMenu *globalMenu;
  24. SpriteAnimationEntry::SpriteAnimationEntry(SpriteAnimation *animation) : UIElement() {
  25. this->animation = animation;
  26. removeButton = new UIImageButton("Images/remove_icon.png");
  27. removeButton->setPosition(0, 5);
  28. removeButton->addEventListener(this, UIEvent::CLICK_EVENT);
  29. addChild(removeButton);
  30. nameInput = new UITextInput(false, 82, 12);
  31. nameInput->setText(animation->name);
  32. nameInput->setPosition(20, 0);
  33. nameInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  34. addChild(nameInput);
  35. framesInput = new UITextInput(false, 142, 12);
  36. framesInput->setText(animation->frames);
  37. framesInput->setPosition(115, 0);
  38. framesInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  39. addChild(framesInput);
  40. speedInput = new UITextInput(false, 42, 12);
  41. speedInput->setText(String::NumberToString(animation->speed));
  42. speedInput->setPosition(270, 0);
  43. speedInput->addEventListener(this, UIEvent::CHANGE_EVENT);
  44. addChild(speedInput);
  45. playButton = new UIImageButton("Images/play_icon.png");
  46. playButton->setPosition(330, 3);
  47. playButton->addEventListener(this, UIEvent::CLICK_EVENT);
  48. addChild(playButton);
  49. }
  50. void SpriteAnimationEntry::handleEvent(Event *event) {
  51. if(event->getEventCode() == UIEvent::CHANGE_EVENT && event->getEventType() == "UIEvent") {
  52. if(event->getDispatcher() == nameInput) {
  53. animation->name = nameInput->getText();
  54. }
  55. if(event->getDispatcher() == framesInput) {
  56. animation->setOffsetsFromFrameString(framesInput->getText());
  57. }
  58. if(event->getDispatcher() == speedInput) {
  59. animation->speed = atof(speedInput->getText().c_str());
  60. }
  61. }
  62. if(event->getEventCode() == UIEvent::CLICK_EVENT && event->getEventType() == "UIEvent") {
  63. if(event->getDispatcher() == removeButton) {
  64. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  65. }
  66. if(event->getDispatcher() == playButton) {
  67. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  68. }
  69. }
  70. }
  71. SpriteAnimationEntry::~SpriteAnimationEntry() {
  72. delete nameInput;
  73. delete framesInput;
  74. delete speedInput;
  75. delete removeButton;
  76. delete playButton;
  77. }
  78. PolycodeSpriteEditor::PolycodeSpriteEditor() : PolycodeEditor(true){
  79. headerBg = new ScenePrimitive(ScenePrimitive::TYPE_VPLANE,10,10);
  80. addChild(headerBg);
  81. headerBg->setAnchorPoint(-1.0, -1.0, 0.0);
  82. headerBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  83. initialLoad = false;
  84. propList = new PropList("SPRITE EDITOR");
  85. addChild(propList);
  86. propList->setPosition(0, 0);
  87. PropSheet *baseProps = new PropSheet("IMAGE OPTIONS", "");
  88. propList->addPropSheet(baseProps);
  89. textureProp = new TextureProp("Base image");
  90. baseProps->addProp(textureProp);
  91. widthProp = new NumberProp("Frame width");
  92. widthProp->set(32);
  93. baseProps->addProp(widthProp);
  94. heightProp = new NumberProp("Frame height");
  95. heightProp->set(32);
  96. baseProps->addProp(heightProp);
  97. widthProp->addEventListener(this, Event::CHANGE_EVENT);
  98. textureProp->addEventListener(this, Event::CHANGE_EVENT);
  99. heightProp->addEventListener(this, Event::CHANGE_EVENT);
  100. baseProps->propHeight = 180;
  101. SceneLabel *label = new SceneLabel("PREVIEW", 18, "section", Label::ANTIALIAS_FULL);
  102. label->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderFontColor"));
  103. addChild(label);
  104. label->setPosition(390, 36);
  105. PropSheet *animationProps = new PropSheet("ANIMATIONS", "");
  106. propList->addPropSheet(animationProps);
  107. SceneLabel *animHelpLabel = new SceneLabel("Comma separated frames, ranges or repeats (e.g. 1,2,3-7,8x5)", 11);
  108. animHelpLabel->color.a = 0.4;
  109. animationProps->addChild(animHelpLabel);
  110. animHelpLabel->setPosition(5, 40);
  111. animationProps->propHeight = 230;
  112. animationsAnchor = new UIElement();
  113. animationProps->contents->addChild(animationsAnchor);
  114. addAnimationButton = new UIButton("Add Animation", 100);
  115. animationsAnchor->addChild(addAnimationButton);
  116. addAnimationButton->addEventListener(this, UIEvent::CLICK_EVENT);
  117. propList->updateProps();
  118. zoomBox = new UIComboBox(globalMenu, 100);
  119. addChild(zoomBox);
  120. zoomBox->setPosition(490, 37);
  121. zoomBox->addEventListener(this, UIEvent::CHANGE_EVENT);
  122. zoomBox->addComboItem("No Zoom");
  123. zoomBox->addComboItem("Zoom 2x");
  124. zoomBox->addComboItem("Zoom 4x");
  125. zoomBox->addComboItem("Zoom 8x");
  126. zoomBox->addComboItem("Zoom 16x");
  127. }
  128. void PolycodeSpriteEditor::refreshAnimationEntries() {
  129. for(int i=0; i < animationEntries.size(); i++) {
  130. animationEntries[i]->setPosition(0, 30 + (30*i));
  131. }
  132. addAnimationButton->setPosition(0, 30 + (animationEntries.size() * 30) + 10);
  133. }
  134. void PolycodeSpriteEditor::handleEvent(Event *event) {
  135. if(event->getDispatcher() == zoomBox) {
  136. switch (zoomBox->getSelectedIndex()) {
  137. case 1:
  138. previewSprite->setScale(2.0, 2.0);
  139. break;
  140. case 2:
  141. previewSprite->setScale(4.0, 4.0);
  142. break;
  143. case 3:
  144. previewSprite->setScale(8.0, 8.0);
  145. break;
  146. case 4:
  147. previewSprite->setScale(16.0, 16.0);
  148. break;
  149. default:
  150. previewSprite->setScale(1.0, 1.0);
  151. break;
  152. }
  153. }
  154. if(!initialLoad) {
  155. if(event->getDispatcher() == textureProp) {
  156. previewSprite->setTexture(textureProp->get());
  157. previewSprite->recalculateSpriteDimensions();
  158. previewSprite->getTexture()->reloadOnFileModify = true;
  159. }
  160. if(event->getDispatcher() == widthProp) {
  161. previewSprite->setSpriteSize(widthProp->get(), heightProp->get());
  162. previewSprite->setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, widthProp->get(), heightProp->get());
  163. previewSprite->recalculateSpriteDimensions();
  164. }
  165. if(event->getDispatcher() == heightProp) {
  166. previewSprite->setSpriteSize(widthProp->get(), heightProp->get());
  167. previewSprite->setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, widthProp->get(), heightProp->get());
  168. previewSprite->recalculateSpriteDimensions();
  169. }
  170. }
  171. if(event->getDispatcher() == addAnimationButton) {
  172. SpriteAnimationEntry *newEntry = new SpriteAnimationEntry(previewSprite->addAnimation("default", "0", 0.5));
  173. newEntry->addEventListener(this, Event::CHANGE_EVENT);
  174. newEntry->addEventListener(this, Event::CANCEL_EVENT);
  175. animationsAnchor->addChild(newEntry);
  176. animationEntries.push_back(newEntry);
  177. refreshAnimationEntries();
  178. }
  179. bool doRefresh = false;
  180. for(int i=0; i < animationEntries.size(); i++) {
  181. if(event->getDispatcher() == animationEntries[i]) {
  182. if(event->getEventCode() == Event::CANCEL_EVENT) {
  183. animationsAnchor->removeChild(animationEntries[i]);
  184. animationEntries.erase(animationEntries.begin()+i);
  185. doRefresh = true;
  186. break;
  187. }
  188. if(event->getEventCode() == Event::CHANGE_EVENT) {
  189. previewSprite->playAnimation(animationEntries[i]->nameInput->getText(), 0, false);
  190. }
  191. }
  192. }
  193. if(doRefresh) {
  194. refreshAnimationEntries();
  195. }
  196. }
  197. PolycodeSpriteEditor::~PolycodeSpriteEditor() {
  198. for(int i=0; i < animationEntries.size(); i++) {
  199. delete animationEntries[i];
  200. }
  201. delete animationsAnchor;
  202. delete addAnimationButton;
  203. delete propList;
  204. delete headerBg;
  205. delete zoomBox;
  206. delete textureProp;
  207. delete widthProp;
  208. delete heightProp;
  209. delete previewSprite;
  210. }
  211. bool PolycodeSpriteEditor::openFile(OSFileEntry filePath) {
  212. initialLoad = true;
  213. previewSprite = new SceneSprite(filePath.fullPath);
  214. addChild(previewSprite);
  215. previewSprite->setAnchorPoint(-1.0, -1.0, 0.0);
  216. previewSprite->setPosition(400, 80);
  217. zoomBox->setSelectedIndex(0);
  218. previewSprite->getTexture()->reloadOnFileModify = true;
  219. for(int i=0;i < previewSprite->getNumAnimations(); i++) {
  220. SpriteAnimation *animation = previewSprite->getAnimationAtIndex(i);
  221. SpriteAnimationEntry *newEntry = new SpriteAnimationEntry(animation);
  222. newEntry->addEventListener(this, Event::CHANGE_EVENT);
  223. newEntry->addEventListener(this, Event::CANCEL_EVENT);
  224. animationsAnchor->addChild(newEntry);
  225. animationEntries.push_back(newEntry);
  226. }
  227. if(previewSprite->getNumAnimations() > 0) {
  228. previewSprite->playAnimation(previewSprite->getAnimationAtIndex(0)->name, 0, false);
  229. }
  230. widthProp->set(previewSprite->getSpriteSize().x);
  231. heightProp->set(previewSprite->getSpriteSize().y);
  232. textureProp->set(previewSprite->getTexture());
  233. refreshAnimationEntries();
  234. PolycodeEditor::openFile(filePath);
  235. initialLoad = false;
  236. return true;
  237. }
  238. void PolycodeSpriteEditor::saveFile() {
  239. Object saveObject;
  240. saveObject.root.name = "sprite";
  241. ObjectEntry *image = saveObject.root.addChild("image");
  242. image->addChild("frameWidth", widthProp->get());
  243. image->addChild("frameHeight", heightProp->get());
  244. image->addChild("fileName", previewSprite->getTexture()->getResourcePath());
  245. ObjectEntry *animations = saveObject.root.addChild("animations");
  246. for(int i=0; i < animationEntries.size(); i++) {
  247. ObjectEntry *animation = animations->addChild("animation");
  248. animation->addChild("name", animationEntries[i]->nameInput->getText());
  249. animation->addChild("frames", animationEntries[i]->framesInput->getText());
  250. animation->addChild("speed", atof(animationEntries[i]->speedInput->getText().c_str()));
  251. }
  252. saveObject.saveToXML(filePath);
  253. }
  254. void PolycodeSpriteEditor::Resize(int x, int y) {
  255. headerBg->setPrimitiveOptions(ScenePrimitive::TYPE_VPLANE, x, 30);
  256. propList->Resize(370, y);
  257. propList->updateProps();
  258. PolycodeEditor::Resize(x,y);
  259. }