PolycodeScreenEditor.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #pragma once
  20. #include "PolycodeEditor.h"
  21. #include <Polycode.h>
  22. #include <PolycodeUI.h>
  23. #include "PolycodeProps.h"
  24. #if defined(__APPLE__) && defined(__MACH__)
  25. #define COPYMOD_1 KEY_RALT
  26. #define COPYMOD_2 KEY_LALT
  27. #elif defined _WINDOWS
  28. #define COPYMOD_1 KEY_RCTRL
  29. #define COPYMOD_2 KEY_LCTRL
  30. #else
  31. #define COPYMOD_1 KEY_RCTRL
  32. #define COPYMOD_2 KEY_LCTRL
  33. #endif
  34. using namespace Polycode;
  35. #ifdef _WINDOWS
  36. #define round (int)
  37. #endif
  38. class EntityBrowserData {
  39. public:
  40. Entity *entity;
  41. };
  42. class EntityTreeView : public UIElement {
  43. public:
  44. EntityTreeView(Entity *rootEntity);
  45. ~EntityTreeView();
  46. void Resize(Number width, Number height);
  47. void handleEvent(Event *event);
  48. void syncNodeToEntity(UITree *node, Entity *entity);
  49. void Refresh();
  50. Entity *selectedEntity;
  51. Entity *targetLayer;
  52. UIImageButton *newLayerButton;
  53. UIImageButton *targetLayerButton;
  54. Entity *rootEntity;
  55. protected:
  56. bool dontSendSelectionEvent;
  57. UITreeContainer *treeContainer;
  58. ScreenShape *headerBg;
  59. ScreenShape *bg;
  60. };
  61. class PropertiesSizer : public UIElement {
  62. public:
  63. PropertiesSizer();
  64. ~PropertiesSizer();
  65. void Resize(Number width, Number height);
  66. UIVSizer *sizer;
  67. };
  68. class ScreenEntityNameDisplay : public ScreenEntity {
  69. public:
  70. ScreenEntityNameDisplay(ScreenEntity *targetEntity);
  71. ~ScreenEntityNameDisplay();
  72. void Update();
  73. ScreenLabel *label;
  74. ScreenLabel *tagsLabel;
  75. ScreenEntity *targetEntity;
  76. };
  77. class PolycodeScreenEditorMain : public UIElement {
  78. public:
  79. PolycodeScreenEditorMain();
  80. virtual ~PolycodeScreenEditorMain();
  81. void Resize(Number width, Number height);
  82. void syncTransformToSelected();
  83. ScreenEntity *addNewLayer(String layerName);
  84. void updateCursor();
  85. void selectEntity(ScreenEntity *entity);
  86. void resetSelectedEntityTransforms();
  87. void setMode(int newMode);
  88. void handleEvent(Event *event);
  89. void resizePreviewScreen();
  90. void handleDroppedFile(OSFileEntry file, Number x, Number y);
  91. bool hasSelected(ScreenEntity *entity);
  92. void applyEditorOnly(ScreenEntity *entity);
  93. void applyEditorProperties(ScreenEntity *entity);
  94. void processEventForEntity(ScreenEntity *childEntity, InputEvent *inputEvent);
  95. void createParticleRef(ScreenParticleEmitter *target);
  96. void createSoundRef(ScreenSound *target);
  97. void createEntityRef(ScreenEntity *entity);
  98. void getCenterAndSizeForSelected(Vector2 *center, Number *width, Number *height);
  99. void handleMouseDown(Vector2 position);
  100. void handleMouseMove(Vector2 position);
  101. void handleMouseUp(Vector2 position);
  102. void setGrid(int gridSize);
  103. String Copy(void **data);
  104. void Paste(void *data, String clipboardType);
  105. void destroyClipboardData(void *data, String type);
  106. static const int MODE_SELECT = 0;
  107. static const int MODE_SHAPE = 1;
  108. static const int MODE_ZOOM = 2;
  109. static const int MODE_PAN = 3;
  110. static const int MODE_IMAGE = 4;
  111. static const int MODE_TEXT = 5;
  112. static const int MODE_PARENT = 6;
  113. static const int MODE_ENTITY = 7;
  114. static const int MODE_SOUND = 8;
  115. static const int MODE_LINK = 9;
  116. static const int MODE_SPRITE = 10;
  117. static const int MODE_PARTICLES = 11;
  118. std::vector<ScreenEntity*> layers;
  119. ScreenEntity *currentLayer;
  120. PropList *entityProps;
  121. ScreenEntity *layerBaseEntity;
  122. ScreenEntity *selectedEntity;
  123. std::vector<ScreenEntity*> selectedEntities;
  124. Transform2DSheet *transform2dSheet;
  125. EntitySheet *entitySheet;
  126. ShapeSheet *shapeSheet;
  127. ScreenImageSheet *imageSheet;
  128. ScreenLabelSheet *labelSheet;
  129. SoundSheet *soundSheet;
  130. ScreenEntityInstanceSheet *instanceSheet;
  131. ScreenSpriteSheet *spriteSheet;
  132. ScreenParticleSheet *particleSheet;
  133. EntityTreeView *treeView;
  134. ScreenEntity *objectBaseEntity;
  135. ScreenEntity *placingPreviewEntity;
  136. bool multiSelect;
  137. protected:
  138. int gridSize;
  139. bool gridSnap;
  140. ScreenShape *previewShape;
  141. ScreenImage *previewImage;
  142. ScreenEntityInstance *previewInstance;
  143. ScreenLabel *previewLabel;
  144. ScreenEntity *previewEntity;
  145. ScreenParticleEmitter *previewEmitter;
  146. ScreenSound *previewSound;
  147. ScreenSprite *previewSprite;
  148. ScreenMesh *grid;
  149. bool firstResize;
  150. bool firstMove;
  151. int placementCount;
  152. Number previewAspectRatio;
  153. bool placing;
  154. bool moving;
  155. bool scalingY;
  156. bool scalingX;
  157. bool rotating;
  158. bool panning;
  159. bool zooming;
  160. bool parenting;
  161. ScreenEntity *parentingChild;
  162. ScreenLine *parentingLine;
  163. int lastMode;
  164. Vector2 basePanPosition;
  165. Number baseZoomScale;
  166. Number baseRotateAngle;
  167. int selectedEntityPositionMode;
  168. Vector3 baseScale;
  169. Vector2 mouseBase;
  170. std::vector<Vector2> baseEntityPositions;
  171. std::vector<Vector2> baseEntityScales;
  172. std::vector<Number> baseRotateAngles;
  173. Vector2 groupCenterPoint;
  174. Number groupRoll;
  175. Number baseAngle;
  176. ScreenShape *placingShape;
  177. Vector2 placingAnchor;
  178. UIColorBox *entityColorBox;
  179. UIWindow *entityInfoWindow;
  180. ScreenShape *screenPreviewShape;
  181. Vector2 dragOffset;
  182. bool isDraggingEntity;
  183. bool isScalingEntity;
  184. ScreenEntity *baseEntity;
  185. ScreenEntity *screenTransform;
  186. ScreenShape *screenTransformShape;
  187. ScreenImage *transformRotator;
  188. ScreenImage *transformScalerY;
  189. ScreenImage *transformScalerX;
  190. ScreenImage *centerImage;
  191. ScreenEntity *properties;
  192. ScreenShape *propertiesBg;
  193. ScreenEntity *viewOptions;
  194. ScreenShape *viewOptionsBg;
  195. UITextInput *gridSizeInput;
  196. UICheckBox *gridCheckBox;
  197. UICheckBox *pixelSnapBox;
  198. UICheckBox *gridSnapBox;
  199. UITextInput *scaleInput;
  200. ScreenEntity *toolPalette;
  201. ScreenShape *toolPaletteBg;
  202. UIButton *resetZoom;
  203. UIImageButton *arrowToolButton;
  204. UIImageButton *shapeToolButton;
  205. UIImageButton *zoomToolButton;
  206. UIImageButton *panToolButton;
  207. UIImageButton *imageToolButton;
  208. UIImageButton *textToolButton;
  209. UIImageButton *parentToolButton;
  210. UIImageButton *entityToolButton;
  211. UIImageButton *soundToolButton;
  212. UIImageButton *spriteToolButton;
  213. UIImageButton *particlesToolButton;
  214. UIImageButton *linkToolButton;
  215. UIComboBox *aspectComboBox;
  216. UIComboBox *zoomComboBox;
  217. UIButton *moveUpButton;
  218. UIButton *moveDownButton;
  219. UIButton *moveTopButton;
  220. UIButton *moveBottomButton;
  221. UIButton *unparentButton;
  222. Number zooms[16];
  223. Number aspects[16];
  224. ScreenImage *selectorImage;
  225. int mode;
  226. };
  227. class ScreenEntityClipboardData {
  228. public:
  229. ScreenEntityClipboardData(){}
  230. std::vector<ScreenEntity*> entities;
  231. };
  232. class PolycodeScreenEditor : public PolycodeEditor {
  233. public:
  234. PolycodeScreenEditor();
  235. virtual ~PolycodeScreenEditor();
  236. bool openFile(OSFileEntry filePath);
  237. void Resize(int x, int y);
  238. String Copy(void **data);
  239. void Paste(void *data, String clipboardType);
  240. void destroyClipboardData(void *data, String type);
  241. void Activate();
  242. void saveCurveToObject(ObjectEntry *entry, BezierCurve *curve);
  243. void saveEntityToObjectEntry(ScreenEntity *entity, ObjectEntry *entry);
  244. void saveFile();
  245. void handleDroppedFile(OSFileEntry file, Number x, Number y);
  246. void handleEvent(Event *event);
  247. protected:
  248. UIHSizer *mainSizer;
  249. PropertiesSizer *propSizer;
  250. EntityTreeView *treeView;
  251. PolycodeScreenEditorMain *editorMain;
  252. };
  253. class PolycodeScreenEditorFactory : public PolycodeEditorFactory {
  254. public:
  255. PolycodeScreenEditorFactory() : PolycodeEditorFactory() { extensions.push_back("entity2d"); }
  256. PolycodeEditor *createEditor() { return new PolycodeScreenEditor(); }
  257. };