2
0

PolycodeEntityEditor.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "PolycodeEntityEditor.h"
  20. #include "PolycodeFrame.h"
  21. extern UIGlobalMenu *globalMenu;
  22. extern PolycodeFrame *globalFrame;
  23. EntityEditorMainView::EntityEditorMainView() {
  24. processInputEvents = true;
  25. mainScene = new Scene(Scene::SCENE_3D, true);
  26. renderTexture = new SceneRenderTexture(mainScene, mainScene->getDefaultCamera(), 512, 512);
  27. mainScene->clearColor.setColor(0.2, 0.2, 0.2, 1.0);
  28. mainScene->useClearColor = true;
  29. mainScene->rootEntity.processInputEvents = true;
  30. renderTextureShape = new UIRect(256, 256);
  31. renderTextureShape->setAnchorPoint(-1.0, -1.0, 0.0);
  32. renderTextureShape->setTexture(renderTexture->getTargetTexture());
  33. addChild(renderTextureShape);
  34. renderTextureShape->setPosition(0, 30);
  35. headerBg = new UIRect(10,10);
  36. addChild(headerBg);
  37. headerBg->setAnchorPoint(-1.0, -1.0, 0.0);
  38. headerBg->color.setColorHexFromString(CoreServices::getInstance()->getConfig()->getStringValue("Polycode", "uiHeaderBgColor"));
  39. sideBar = new Entity();
  40. addChild(sideBar);
  41. sideBar->setPosition(0, 0);
  42. sideBar->processInputEvents = true;
  43. mainScene->getDefaultCamera()->setPosition(10, 10, 10);
  44. mainScene->getDefaultCamera()->lookAt(Vector3());
  45. grid = new EditorGrid();
  46. mainScene->addChild(grid);
  47. sceneObjectRoot = new Entity();
  48. sceneObjectRoot->processInputEvents = true;
  49. mainScene->addChild(sceneObjectRoot);
  50. transformGizmo = new TransformGizmo(mainScene, mainScene->getDefaultCamera());
  51. mainScene->addChild(transformGizmo);
  52. trackballCamera = new TrackballCamera(mainScene->getDefaultCamera(), renderTextureShape);
  53. addEntityButton = new UIImageButton("entityEditor/add_entity.png", 1.0, 24, 24);
  54. sideBar->addChild(addEntityButton);
  55. addEntityButton->setPosition(4, 2);
  56. addEntityButton->addEventListener(this, UIEvent::CLICK_EVENT);
  57. transformGizmoMenu = new TransformGizmoMenu(transformGizmo);
  58. sideBar->addChild(transformGizmoMenu);
  59. transformGizmoMenu->setPositionX(40);
  60. }
  61. void EntityEditorMainView::Update() {
  62. }
  63. void EntityEditorMainView::setEditorProps(Entity *entity) {
  64. entity->processInputEvents = true;
  65. entity->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  66. SceneMesh *sceneMesh = dynamic_cast<SceneMesh*>(entity);
  67. if(sceneMesh) {
  68. sceneMesh->wireFrameColor = Color(1.0, 0.8, 0.3, 1.0);
  69. sceneMesh->setLineWidth(CoreServices::getInstance()->getRenderer()->getBackingResolutionScaleX());
  70. }
  71. }
  72. void EntityEditorMainView::addEntityFromMenu(String command) {
  73. if(command == "add_primitive") {
  74. ScenePrimitive *newPrimitive = new ScenePrimitive(ScenePrimitive::TYPE_BOX, 1.0, 1.0, 1.0);
  75. sceneObjectRoot->addChild(newPrimitive);
  76. setEditorProps(newPrimitive);
  77. newPrimitive->setPosition(cursorPosition);
  78. return;
  79. }
  80. if(command == "add_mesh") {
  81. assetSelectType = "mesh";
  82. globalFrame->assetBrowser->addEventListener(this, UIEvent::OK_EVENT);
  83. std::vector<String> extensions;
  84. extensions.push_back("mesh");
  85. globalFrame->showAssetBrowser(extensions);
  86. return;
  87. }
  88. }
  89. void EntityEditorMainView::handleEvent(Event *event) {
  90. if(event->getDispatcher() == globalFrame->assetBrowser) {
  91. if(event->getEventCode() == UIEvent::OK_EVENT) {
  92. if(assetSelectType == "mesh") {
  93. SceneMesh *newMesh = new SceneMesh(globalFrame->assetBrowser->getFullSelectedAssetPath());
  94. sceneObjectRoot->addChild(newMesh);
  95. setEditorProps(newMesh);
  96. newMesh->setPosition(cursorPosition);
  97. }
  98. globalFrame->assetBrowser->removeAllHandlersForListener(this);
  99. globalFrame->hideModal();
  100. }
  101. } else if(event->getDispatcher() == addEntityMenu) {
  102. addEntityMenu->removeAllHandlersForListener(this);
  103. String command = addEntityMenu->getSelectedItem()->getMenuItemID();
  104. addEntityFromMenu(command);
  105. } else if(event->getDispatcher() == addEntityButton) {
  106. addEntityMenu = globalMenu->showMenuAtMouse(150);
  107. addEntityMenu->addOption("Add Primitive", "add_primitive");
  108. addEntityMenu->addOption("Add Mesh", "add_mesh");
  109. addEntityMenu->addOption("Add Entity", "add_entity");
  110. addEntityMenu->addOption("Add Sprite", "add_sprite");
  111. addEntityMenu->addOption("Add Label", "add_label");
  112. addEntityMenu->addDivider();
  113. addEntityMenu->addOption("Add Light", "add_light");
  114. addEntityMenu->addOption("Add Particle System", "add_particles");
  115. addEntityMenu->addOption("Add Sound", "add_sound");
  116. addEntityMenu->addOption("Add Camera", "add_camera");
  117. addEntityMenu->addDivider();
  118. addEntityMenu->addOption("Add Empty", "add_empty");
  119. addEntityMenu->fitToScreenVertical();
  120. addEntityMenu->addEventListener(this, UIEvent::OK_EVENT);
  121. } else {
  122. if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN ) {
  123. InputEvent *inputEvent = (InputEvent*) event;
  124. if(inputEvent->mouseButton == CoreInput::MOUSE_BUTTON2) {
  125. Entity* targetEntity = (Entity*) event->getDispatcher();
  126. for(int i=0; i < selectedEntities.size(); i++) {
  127. SceneMesh *sceneMesh = dynamic_cast<SceneMesh*>(selectedEntities[i]);
  128. if(sceneMesh) {
  129. sceneMesh->overlayWireframe = false;
  130. }
  131. }
  132. selectedEntities.clear();
  133. selectedEntities.push_back(targetEntity);
  134. transformGizmo->setTransformSelection(selectedEntities);
  135. SceneMesh *sceneMesh = dynamic_cast<SceneMesh*>(targetEntity);
  136. if(sceneMesh) {
  137. sceneMesh->overlayWireframe = true;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. EntityEditorMainView::~EntityEditorMainView() {
  144. }
  145. void EntityEditorMainView::Resize(Number width, Number height) {
  146. headerBg->Resize(width, 30);
  147. mainScene->sceneMouseAdjust.x = renderTextureShape->getScreenPositionForMainCamera().x;
  148. mainScene->sceneMouseAdjust.y = CoreServices::getInstance()->getCore()->getYRes() - (renderTextureShape->getScreenPositionForMainCamera().y + renderTextureShape->getHeight());
  149. renderTexture->resizeRenderTexture(width, height-30);
  150. renderTextureShape->setTexture(renderTexture->getTargetTexture());
  151. renderTextureShape->Resize(width, height-30);
  152. }
  153. PolycodeEntityEditor::PolycodeEntityEditor() : PolycodeEditor(true){
  154. mainSizer = new UIHSizer(300, 300, 200, false);
  155. addChild(mainSizer);
  156. mainView = new EntityEditorMainView();
  157. mainSizer->addLeftChild(mainView);
  158. }
  159. PolycodeEntityEditor::~PolycodeEntityEditor() {
  160. }
  161. bool PolycodeEntityEditor::openFile(OSFileEntry filePath) {
  162. PolycodeEditor::openFile(filePath);
  163. return true;
  164. }
  165. void PolycodeEntityEditor::Resize(int x, int y) {
  166. mainSizer->Resize(x, y);
  167. PolycodeEditor::Resize(x,y);
  168. }