PolycodeEntityEditor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "PolyUIElement.h"
  22. #include <Polycode.h>
  23. #include "PolycodeUI.h"
  24. #include "EntityEditorPropertyView.h"
  25. #include "EntityEditorTreeView.h"
  26. #include "EntityEditorSettingsView.h"
  27. #include "TrackballCamera.h"
  28. #include "TransformGizmo.h"
  29. #include "EditorGrid.h"
  30. using namespace Polycode;
  31. class EntityEditorClipboardData {
  32. public:
  33. EntityEditorClipboardData(){}
  34. std::vector<Entity*> entities;
  35. };
  36. class PolycodeSceneEditorActionDataEntry {
  37. public:
  38. PolycodeSceneEditorActionDataEntry(){
  39. entity = NULL;
  40. parentEntity = NULL;
  41. }
  42. PolycodeSceneEditorActionDataEntry(Vector3 vec3, Number number);
  43. PolycodeSceneEditorActionDataEntry(Quaternion quat);
  44. PolycodeSceneEditorActionDataEntry(Vector3 vec3);
  45. PolycodeSceneEditorActionDataEntry(Vector3 vec3, Vector3 vec3Secondary);
  46. PolycodeSceneEditorActionDataEntry(Quaternion quat, Vector3 vec3);
  47. PolycodeSceneEditorActionDataEntry(Number number);
  48. PolycodeSceneEditorActionDataEntry(Entity *entity);
  49. Vector3 vec3;
  50. Vector3 vec3Secondary;
  51. Quaternion quat;
  52. Number number;
  53. Entity *entity;
  54. Entity *parentEntity;
  55. };
  56. class PolycodeSceneEditorActionData : public PolycodeEditorActionData {
  57. public:
  58. PolycodeSceneEditorActionData() {
  59. reverse = true;
  60. deleteEntitiesInDestructor = false;
  61. }
  62. ~PolycodeSceneEditorActionData() {
  63. if(deleteEntitiesInDestructor) {
  64. for(int i=0; i < entries.size(); i++) {
  65. if(!entries[i].entity->getParentEntity()) {
  66. delete entries[i].entity;
  67. }
  68. }
  69. }
  70. }
  71. std::vector<PolycodeSceneEditorActionDataEntry> entries;
  72. bool deleteEntitiesInDestructor;
  73. bool reverse;
  74. };
  75. class LightDisplay : public Entity {
  76. public:
  77. LightDisplay(SceneLight *light);
  78. ~LightDisplay();
  79. void Update();
  80. private:
  81. ScenePrimitive *spotSpot;
  82. SceneMesh *fovSceneMesh;
  83. Mesh *fovMesh;
  84. SceneLight *light;
  85. };
  86. class CameraDisplay : public Entity {
  87. public:
  88. CameraDisplay(Camera *camera);
  89. ~CameraDisplay();
  90. void Update();
  91. private:
  92. SceneMesh *fovSceneMesh;
  93. Mesh *fovMesh;
  94. Camera *camera;
  95. };
  96. class CameraPreviewWindow : public UIElement {
  97. public:
  98. CameraPreviewWindow();
  99. ~CameraPreviewWindow();
  100. void handleEvent(Event *event);
  101. void Resize(Number width, Number height);
  102. void setCamera(Scene *scene, Camera *camera);
  103. bool isPinned();
  104. private:
  105. SceneRenderTexture *renderTexture;
  106. Camera *camera;
  107. Scene *scene;
  108. UIRect *bgRect;
  109. UIRect *previewRect;
  110. bool pinned;
  111. bool cameraSelected;
  112. UIComboBox *aspectCombo;
  113. UIButton *pinButton;
  114. };
  115. class SceneMeshSettings {
  116. public:
  117. Material *material;
  118. bool backfaceCulled;
  119. ShaderBinding *shaderBinding;
  120. };
  121. class MultiselectorEntry {
  122. public:
  123. Entity *entity;
  124. Number distance;
  125. };
  126. class EntityDistanceSorter : public PolyBase {
  127. public:
  128. bool operator() (MultiselectorEntry i,MultiselectorEntry j);
  129. };
  130. class EntityEditorMainView : public UIElement {
  131. public:
  132. EntityEditorMainView(PolycodeEditor *editor);
  133. ~EntityEditorMainView();
  134. void createIcon(Entity *entity, String iconFile);
  135. void setEditorProps(Entity *entity);
  136. void selectEntity(Entity *targetEntity, bool addToSelection = false, bool doAction = true);
  137. void handleEvent(Event *event);
  138. void Resize(Number width, Number height);
  139. void Update();
  140. void addEntityFromMenu(String command);
  141. void doEntityDeselect(Entity *targetEntity);
  142. void doEntitySelect(Entity *targetEntity);
  143. void setEditorMode(int newMode);
  144. Entity *getSelectedEntity();
  145. std::vector<Entity*> getSelectedEntities();
  146. void setOverlayWireframeRecursive(Entity *targetEntity, bool val);
  147. void setLinkedEntityPropsRecursive(SceneEntityInstance *parentInstance, Entity *entity);
  148. void Paste(EntityEditorClipboardData *data);
  149. void moveSelectedUp();
  150. void moveSelectedDown();
  151. void moveSelectedTop();
  152. void moveSelectedBottom();
  153. void disableLighting(bool disable);
  154. void selectNone(bool doAction);
  155. void selectAll(bool doAction);
  156. SceneRenderTexture *getRenderTexture();
  157. void onGainFocus();
  158. void onLoseFocus();
  159. void deleteSelected(bool doAction);
  160. void doAction(String actionName, PolycodeEditorActionData *data);
  161. void didPlaceEntity(Entity *entity);
  162. void setBBox();
  163. Entity *getObjectRoot();
  164. void setObjectRoot(SceneEntityInstance *entity);
  165. Scene *getMainScene();
  166. void setEditorPropsRecursive(Entity *entity);
  167. static const int EDITOR_MODE_3D = 0;
  168. static const int EDITOR_MODE_2D = 1;
  169. protected:
  170. bool selectingNewEntities();
  171. CoreInput *input;
  172. PolycodeSceneEditorActionData *beforeData;
  173. bool lightsDisabled;
  174. int editorMode;
  175. Entity *topBar;
  176. Entity *bottomBar;
  177. UIRect *headerBg;
  178. UIRect *footerBg;
  179. PolycodeEditor *editor;
  180. unsigned int multiselectIndex;
  181. std::vector<Entity*> selectedEntities;
  182. std::vector<MultiselectorEntry> entitiesToSelect;
  183. std::vector<MultiselectorEntry> lastEntitiesToSelect;
  184. EntityDistanceSorter distanceSorter;
  185. Scene *mainScene;
  186. Entity *sceneObjectRoot;
  187. SceneLight *customLight1;
  188. SceneLight *customLight2;
  189. ScenePrimitive *bBoxVis;
  190. Entity *objectRootBase;
  191. Entity *iconBase;
  192. SceneEntityInstance *objectRootInstance;
  193. SceneRenderTexture *renderTexture;
  194. UIRect *renderTextureShape;
  195. TransformGizmo *transformGizmo;
  196. TransformGizmoMenu *transformGizmoMenu;
  197. TrackballCamera *trackballCamera;
  198. EditorGrid *grid;
  199. UIImageButton *addEntityButton;
  200. UIComboBox *modeSwitchDropdown;
  201. UIMenu *addEntityMenu;
  202. UIIconSelector *shadeModeSelector;
  203. UIIconSelector *lightingModeSelector;
  204. UIIconSelector *iconVisibilitySelector;
  205. UIImageButton *moveUpButton;
  206. UIImageButton *moveTopButton;
  207. UIImageButton *moveDownButton;
  208. UIImageButton *moveBottomButton;
  209. UIImageButton *gridSettingsButton;
  210. EditorGridSettingsWindow *gridSettings;
  211. UIIconSelector *snapSelector;
  212. CameraPreviewWindow *cameraPreview;
  213. std::vector<ScenePrimitive*> icons;
  214. Vector3 cursorPosition;
  215. String assetSelectType;
  216. };
  217. class EntityEditorPropertyContainer : public UIElement {
  218. public:
  219. EntityEditorPropertyContainer(PolycodeEditor *editor);
  220. ~EntityEditorPropertyContainer();
  221. void Resize(Number width, Number height);
  222. void handleEvent(Event *event);
  223. UIElement *currentView;
  224. EntityEditorPropertyView *propertyView;
  225. EntityEditorTreeView *treeView;
  226. EntityEditorSettingsView *settingsView;
  227. UIIconSelector *propIconSelector;
  228. private:
  229. PolycodeEditor *editor;
  230. };
  231. class PolycodeEntityEditor : public PolycodeEditor {
  232. public:
  233. PolycodeEntityEditor();
  234. virtual ~PolycodeEntityEditor();
  235. bool openFile(OSFileEntry filePath);
  236. void Resize(int x, int y);
  237. void Activate();
  238. void Deactivate();
  239. void saveFile();
  240. void saveCurveToObject(ObjectEntry *entry, BezierCurve *curve);
  241. void saveShaderOptionsToEntry(ObjectEntry *entry, Material *material, ShaderBinding *binding);
  242. void saveEntityToObjectEntry(Entity *entity, ObjectEntry *entry);
  243. void doAction(String actionName, PolycodeEditorActionData *data);
  244. String Copy(void **data);
  245. void Paste(void *data, String clipboardType);
  246. void selectAll();
  247. void destroyClipboardData(void *data, String type);
  248. void handleEvent(Event *event);
  249. protected:
  250. EntityEditorPropertyContainer *propertyContainer;
  251. EntityEditorMainView *mainView;
  252. EntityEditorPropertyView *propertyView;
  253. EntityEditorTreeView *treeView;
  254. EntityEditorSettingsView *settingsView;
  255. SceneEntityInstance *loadedInstance;
  256. UIHSizer *mainSizer;
  257. };
  258. class PolycodeEntityEditorFactory : public PolycodeEditorFactory {
  259. public:
  260. PolycodeEntityEditorFactory() : PolycodeEditorFactory() { extensions.push_back("entity"); }
  261. PolycodeEditor *createEditor() { return new PolycodeEntityEditor(); }
  262. };