EditorMain.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. [](Canvas& 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& input = Input::getSingleton();
  80. if(input.getKey(KeyCode::kEscape) || m_editorUiNode->m_editorUi.m_quit)
  81. {
  82. quit = true;
  83. }
  84. return Error::kNone;
  85. }
  86. };
  87. ANKI_MAIN_FUNCTION(myMain)
  88. int myMain(int argc, char* argv[])
  89. {
  90. MyApp* app = new MyApp(argc, argv);
  91. const Error err = app->mainLoop();
  92. delete app;
  93. if(err)
  94. {
  95. ANKI_LOGE("Error reported. Bye!!");
  96. }
  97. else
  98. {
  99. ANKI_LOGI("Bye!!");
  100. }
  101. return 0;
  102. }