EditorMain.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. ANKI_CHECK(CVarSet::getSingleton().setFromCommandLineArguments(m_argc - 1, m_argv + 1));
  39. g_cvarWindowBorderless = true;
  40. g_cvarWindowFullscreen = false;
  41. if(CString(g_cvarEditorScene) != "")
  42. {
  43. ANKI_CHECK(walkDirectoryTree(g_cvarEditorScene, [this](WalkDirectoryArgs& args) {
  44. if(!args.m_isDirectory && args.m_path.find("Scene.lua") != CString::kNpos)
  45. {
  46. m_sceneLuaFname = args.m_path;
  47. args.m_stopSearch = true;
  48. }
  49. return Error::kNone;
  50. }));
  51. if(m_sceneLuaFname)
  52. {
  53. String dataPaths = CString(g_cvarRsrcDataPaths);
  54. dataPaths += ":";
  55. dataPaths += CString(g_cvarEditorScene);
  56. g_cvarRsrcDataPaths = dataPaths;
  57. }
  58. else
  59. {
  60. ANKI_LOGE("Failed to find a Scene.lua");
  61. }
  62. }
  63. return Error::kNone;
  64. }
  65. Error userPostInit() override
  66. {
  67. m_editorUiNode = SceneGraph::getSingleton().newSceneNode<EditorUiNode>("MainUi");
  68. if(m_sceneLuaFname)
  69. {
  70. ANKI_LOGI("Will load: %s", m_sceneLuaFname.cstr());
  71. ScriptResourcePtr script;
  72. ANKI_CHECK(ResourceManager::getSingleton().loadResource(m_sceneLuaFname, script));
  73. ANKI_CHECK(ScriptManager::getSingleton().evalString(script->getSource()));
  74. }
  75. return Error::kNone;
  76. }
  77. Error userMainLoop(Bool& quit, [[maybe_unused]] Second elapsedTime) override
  78. {
  79. Input& in = Input::getSingleton();
  80. SceneGraph& scene = SceneGraph::getSingleton();
  81. if(in.getKey(KeyCode::kEscape) > 0 || m_editorUiNode->m_editorUi.m_quit)
  82. {
  83. quit = true;
  84. }
  85. static Vec2 mousePosOn1stClick = in.getMousePositionNdc();
  86. if(in.getMouseButton(MouseButton::kRight) == 1)
  87. {
  88. // Re-init mouse pos
  89. mousePosOn1stClick = in.getMousePositionNdc();
  90. }
  91. if(in.getMouseButton(MouseButton::kRight) > 0)
  92. {
  93. in.hideCursor(true);
  94. // move the camera
  95. static SceneNode* mover = &scene.getActiveCameraNode();
  96. constexpr F32 kRotateAngle = toRad(2.5f);
  97. constexpr F32 kMouseSensitivity = 5.0f;
  98. if(in.getKey(KeyCode::kUp) > 0)
  99. {
  100. mover->rotateLocalX(kRotateAngle);
  101. }
  102. if(in.getKey(KeyCode::kDown) > 0)
  103. {
  104. mover->rotateLocalX(-kRotateAngle);
  105. }
  106. if(in.getKey(KeyCode::kLeft) > 0)
  107. {
  108. mover->rotateLocalY(kRotateAngle);
  109. }
  110. if(in.getKey(KeyCode::kRight) > 0)
  111. {
  112. mover->rotateLocalY(-kRotateAngle);
  113. }
  114. const F32 moveDistance = 0.1f;
  115. if(in.getKey(KeyCode::kA) > 0)
  116. {
  117. mover->moveLocalX(-moveDistance);
  118. }
  119. if(in.getKey(KeyCode::kD) > 0)
  120. {
  121. mover->moveLocalX(moveDistance);
  122. }
  123. if(in.getKey(KeyCode::kQ) > 0)
  124. {
  125. mover->moveLocalY(-moveDistance);
  126. }
  127. if(in.getKey(KeyCode::kE) > 0)
  128. {
  129. mover->moveLocalY(moveDistance);
  130. }
  131. if(in.getKey(KeyCode::kW) > 0)
  132. {
  133. mover->moveLocalZ(-moveDistance);
  134. }
  135. if(in.getKey(KeyCode::kS) > 0)
  136. {
  137. mover->moveLocalZ(moveDistance);
  138. }
  139. const Vec2 velocity = in.getMousePositionNdc() - mousePosOn1stClick;
  140. in.moveMouseNdc(mousePosOn1stClick);
  141. if(velocity != Vec2(0.0))
  142. {
  143. Euler angles(mover->getLocalRotation().getRotationPart());
  144. angles.x() += velocity.y() * toRad(360.0f) * F32(elapsedTime) * kMouseSensitivity;
  145. angles.x() = clamp(angles.x(), toRad(-90.0f), toRad(90.0f)); // Avoid cycle in Y axis
  146. angles.y() += -velocity.x() * toRad(360.0f) * F32(elapsedTime) * kMouseSensitivity;
  147. angles.z() = 0.0f;
  148. mover->setLocalRotation(Mat3(angles));
  149. }
  150. }
  151. else
  152. {
  153. in.hideCursor(false);
  154. }
  155. return Error::kNone;
  156. }
  157. };
  158. ANKI_MAIN_FUNCTION(myMain)
  159. int myMain(int argc, char* argv[])
  160. {
  161. MyApp* app = new MyApp(argc, argv);
  162. const Error err = app->mainLoop();
  163. delete app;
  164. if(err)
  165. {
  166. ANKI_LOGE("Error reported. Bye!!");
  167. }
  168. else
  169. {
  170. ANKI_LOGI("Bye!!");
  171. }
  172. return 0;
  173. }