EditorMain.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/AnKi.h>
  6. using namespace anki;
  7. ANKI_CVAR(StringCVar, Editor, Scene, "", "Load this scene at startup")
  8. class EditorUiNode : public SceneNode
  9. {
  10. public:
  11. EditorUi m_editorUi;
  12. EditorUiNode(CString name)
  13. : SceneNode(name)
  14. {
  15. UiComponent* uic = newComponent<UiComponent>();
  16. uic->init(
  17. [](UiCanvas& canvas, void* ud) {
  18. static_cast<EditorUiNode*>(ud)->m_editorUi.draw(canvas);
  19. },
  20. this);
  21. }
  22. };
  23. class MyApp : public App
  24. {
  25. public:
  26. U32 m_argc = 0;
  27. Char** m_argv = nullptr;
  28. EditorUiNode* m_editorUiNode = nullptr;
  29. String m_sceneLuaFname;
  30. MyApp(U32 argc, Char** argv)
  31. : App("AnKiEditor")
  32. , m_argc(argc)
  33. , m_argv(argv)
  34. {
  35. }
  36. Error userPreInit() override
  37. {
  38. g_cvarWindowFullscreen = false;
  39. g_cvarWindowMaximized = true;
  40. g_cvarWindowBorderless = true;
  41. ANKI_CHECK(CVarSet::getSingleton().setFromCommandLineArguments(m_argc - 1, m_argv + 1));
  42. if(CString(g_cvarEditorScene) != "")
  43. {
  44. ANKI_CHECK(walkDirectoryTree(g_cvarEditorScene, [this](WalkDirectoryArgs& args) {
  45. if(!args.m_isDirectory && args.m_path.find("Scene.lua") != CString::kNpos)
  46. {
  47. m_sceneLuaFname = args.m_path;
  48. args.m_stopSearch = true;
  49. }
  50. return Error::kNone;
  51. }));
  52. if(m_sceneLuaFname)
  53. {
  54. String dataPaths = CString(g_cvarRsrcDataPaths);
  55. dataPaths += ":";
  56. dataPaths += CString(g_cvarEditorScene);
  57. g_cvarRsrcDataPaths = dataPaths;
  58. }
  59. else
  60. {
  61. ANKI_LOGE("Failed to find a Scene.lua");
  62. }
  63. }
  64. return Error::kNone;
  65. }
  66. Error userPostInit() override
  67. {
  68. m_editorUiNode = SceneGraph::getSingleton().newSceneNode<EditorUiNode>("MainUi");
  69. if(m_sceneLuaFname)
  70. {
  71. ANKI_LOGI("Will load: %s", m_sceneLuaFname.cstr());
  72. ScriptResourcePtr script;
  73. ANKI_CHECK(ResourceManager::getSingleton().loadResource(m_sceneLuaFname, script));
  74. ANKI_CHECK(ScriptManager::getSingleton().evalString(script->getSource()));
  75. }
  76. return Error::kNone;
  77. }
  78. Error userMainLoop(Bool& quit, [[maybe_unused]] Second elapsedTime) override
  79. {
  80. Input& in = Input::getSingleton();
  81. SceneGraph& scene = SceneGraph::getSingleton();
  82. if(m_editorUiNode->m_editorUi.m_quit)
  83. {
  84. quit = true;
  85. }
  86. static Vec2 mousePosOn1stClick = in.getMousePositionNdc();
  87. if(in.getMouseButton(MouseButton::kRight) == 1)
  88. {
  89. // Re-init mouse pos
  90. mousePosOn1stClick = in.getMousePositionNdc();
  91. }
  92. if(in.getMouseButton(MouseButton::kRight) > 0)
  93. {
  94. in.hideCursor(true);
  95. // move the camera
  96. static SceneNode* mover = &scene.getActiveCameraNode();
  97. constexpr F32 kRotateAngle = toRad(2.5f);
  98. constexpr F32 kMouseSensitivity = 5.0f;
  99. if(in.getKey(KeyCode::kUp) > 0)
  100. {
  101. mover->rotateLocalX(kRotateAngle);
  102. }
  103. if(in.getKey(KeyCode::kDown) > 0)
  104. {
  105. mover->rotateLocalX(-kRotateAngle);
  106. }
  107. if(in.getKey(KeyCode::kLeft) > 0)
  108. {
  109. mover->rotateLocalY(kRotateAngle);
  110. }
  111. if(in.getKey(KeyCode::kRight) > 0)
  112. {
  113. mover->rotateLocalY(-kRotateAngle);
  114. }
  115. const F32 moveDistance = 0.1f;
  116. if(in.getKey(KeyCode::kA) > 0)
  117. {
  118. mover->moveLocalX(-moveDistance);
  119. }
  120. if(in.getKey(KeyCode::kD) > 0)
  121. {
  122. mover->moveLocalX(moveDistance);
  123. }
  124. if(in.getKey(KeyCode::kQ) > 0)
  125. {
  126. mover->moveLocalY(-moveDistance);
  127. }
  128. if(in.getKey(KeyCode::kE) > 0)
  129. {
  130. mover->moveLocalY(moveDistance);
  131. }
  132. if(in.getKey(KeyCode::kW) > 0)
  133. {
  134. mover->moveLocalZ(-moveDistance);
  135. }
  136. if(in.getKey(KeyCode::kS) > 0)
  137. {
  138. mover->moveLocalZ(moveDistance);
  139. }
  140. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  141. in.moveMouseNdc(mousePosOn1stClick);
  142. if(velocity != Vec2(0.0))
  143. {
  144. Euler angles(mover->getLocalRotation().getRotationPart());
  145. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * kMouseSensitivity;
  146. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  147. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * kMouseSensitivity;
  148. angles.z() = 0.0f;
  149. mover->setLocalRotation(Mat3(angles));
  150. }
  151. }
  152. else
  153. {
  154. in.hideCursor(false);
  155. }
  156. return Error::kNone;
  157. }
  158. };
  159. ANKI_MAIN_FUNCTION(myMain)
  160. int myMain(int argc, char* argv[])
  161. {
  162. MyApp* app = new MyApp(argc, argv);
  163. const Error err = app->mainLoop();
  164. delete app;
  165. if(err)
  166. {
  167. ANKI_LOGE("Error reported. Bye!!");
  168. }
  169. else
  170. {
  171. ANKI_LOGI("Bye!!");
  172. }
  173. return 0;
  174. }