diff --git a/include/openspace/engine/gui.h b/include/openspace/engine/gui.h index aeaacfe8fd..1df5293589 100644 --- a/include/openspace/engine/gui.h +++ b/include/openspace/engine/gui.h @@ -25,6 +25,8 @@ #ifndef __GUI_H__ #define __GUI_H__ +#include + #include #include @@ -43,6 +45,11 @@ public: GUI(); ~GUI(); + bool isEnabled() const; + void setEnabled(bool enabled); + + void initialize(); + void initializeGL(); void deinitializeGL(); @@ -56,9 +63,14 @@ public: void startFrame(float deltaTime, const glm::vec2& windowSize, const glm::vec2& mousePos, bool mouseButtonsPressed[2]); void endFrame(); + + static scripting::ScriptEngine::LuaLibrary luaLibrary(); + private: void renderPropertyWindow(); + bool _isEnabled; + std::set _boolProperties; std::set _intProperties; std::set _floatProperties; diff --git a/include/openspace/engine/openspaceengine.h b/include/openspace/engine/openspaceengine.h index 6699477ff1..a0b38eb422 100644 --- a/include/openspace/engine/openspaceengine.h +++ b/include/openspace/engine/openspaceengine.h @@ -29,7 +29,7 @@ #include #include #include - +#include #include namespace openspace { @@ -59,7 +59,7 @@ public: scripting::ScriptEngine& scriptEngine(); LuaConsole& console(); - GUI* gui() { return _gui; } + GUI& gui(); // SGCT callbacks bool initializeGL(); @@ -97,7 +97,7 @@ private: scripting::ScriptEngine _scriptEngine; ghoul::cmdparser::CommandlineParser _commandlineParser; LuaConsole _console; - GUI* _gui; + GUI _gui; SyncBuffer* _syncBuffer; diff --git a/openspace.cfg b/openspace.cfg index 0df8a1f236..3d29b863fc 100644 --- a/openspace.cfg +++ b/openspace.cfg @@ -33,7 +33,6 @@ return { Type = "text", File = "${BASE_PATH}/LuaScripting.txt" }, - EnableGUI = true, SGCTConfig = "${SGCT}/single.xml", --SGCTConfig = "${SGCT}/two_nodes.xml", --SGCTConfig = "${SGCT}/single_sbs_stereo.xml", diff --git a/src/engine/gui.cpp b/src/engine/gui.cpp index 8e524f2925..d1a640f59e 100644 --- a/src/engine/gui.cpp +++ b/src/engine/gui.cpp @@ -24,6 +24,7 @@ #include +#include #include #include @@ -135,7 +136,24 @@ static void ImImpl_RenderDrawLists(ImDrawList** const commandLists, int nCommand namespace openspace { -GUI::GUI() { +GUI::GUI() + : _isEnabled(false) +{ +} + +GUI::~GUI() { + ImGui::Shutdown(); +} + +bool GUI::isEnabled() const { + return _isEnabled; +} + +void GUI::setEnabled(bool enabled) { + _isEnabled = enabled; +} + +void GUI::initialize() { std::string cachedFile; FileSys.cacheManager()->getCachedFile(configurationFile, "", cachedFile, true); @@ -147,31 +165,27 @@ GUI::GUI() { //io.IniSavingRate = 5.f; io.DeltaTime = 1.f / 60.f; io.PixelCenterOffset = 0.5f; - io.KeyMap[ImGuiKey_Tab] = SGCT_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. - io.KeyMap[ImGuiKey_LeftArrow] = SGCT_KEY_LEFT; - io.KeyMap[ImGuiKey_RightArrow] = SGCT_KEY_RIGHT; - io.KeyMap[ImGuiKey_UpArrow] = SGCT_KEY_UP; - io.KeyMap[ImGuiKey_DownArrow] = SGCT_KEY_DOWN; - io.KeyMap[ImGuiKey_Home] = SGCT_KEY_HOME; - io.KeyMap[ImGuiKey_End] = SGCT_KEY_END; - io.KeyMap[ImGuiKey_Delete] = SGCT_KEY_DELETE; - io.KeyMap[ImGuiKey_Backspace] = SGCT_KEY_BACKSPACE; - io.KeyMap[ImGuiKey_Enter] = SGCT_KEY_ENTER; - io.KeyMap[ImGuiKey_Escape] = SGCT_KEY_ESCAPE; - io.KeyMap[ImGuiKey_A] = SGCT_KEY_A; - io.KeyMap[ImGuiKey_C] = SGCT_KEY_C; - io.KeyMap[ImGuiKey_V] = SGCT_KEY_V; - io.KeyMap[ImGuiKey_X] = SGCT_KEY_X; - io.KeyMap[ImGuiKey_Y] = SGCT_KEY_Y; - io.KeyMap[ImGuiKey_Z] = SGCT_KEY_Z; + io.KeyMap[ImGuiKey_Tab] = SGCT_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array. + io.KeyMap[ImGuiKey_LeftArrow] = SGCT_KEY_LEFT; + io.KeyMap[ImGuiKey_RightArrow] = SGCT_KEY_RIGHT; + io.KeyMap[ImGuiKey_UpArrow] = SGCT_KEY_UP; + io.KeyMap[ImGuiKey_DownArrow] = SGCT_KEY_DOWN; + io.KeyMap[ImGuiKey_Home] = SGCT_KEY_HOME; + io.KeyMap[ImGuiKey_End] = SGCT_KEY_END; + io.KeyMap[ImGuiKey_Delete] = SGCT_KEY_DELETE; + io.KeyMap[ImGuiKey_Backspace] = SGCT_KEY_BACKSPACE; + io.KeyMap[ImGuiKey_Enter] = SGCT_KEY_ENTER; + io.KeyMap[ImGuiKey_Escape] = SGCT_KEY_ESCAPE; + io.KeyMap[ImGuiKey_A] = SGCT_KEY_A; + io.KeyMap[ImGuiKey_C] = SGCT_KEY_C; + io.KeyMap[ImGuiKey_V] = SGCT_KEY_V; + io.KeyMap[ImGuiKey_X] = SGCT_KEY_X; + io.KeyMap[ImGuiKey_Y] = SGCT_KEY_Y; + io.KeyMap[ImGuiKey_Z] = SGCT_KEY_Z; io.RenderDrawListsFn = ImImpl_RenderDrawLists; - //io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; // @TODO implement? ---abock - //io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; // @TODO implement? ---abock -} - -GUI::~GUI() { - ImGui::Shutdown(); + //io.SetClipboardTextFn = ImImpl_SetClipboardTextFn; // @TODO implement? ---abock + //io.GetClipboardTextFn = ImImpl_GetClipboardTextFn; // @TODO implement? ---abock } void GUI::initializeGL() { @@ -467,4 +481,76 @@ void GUI::renderPropertyWindow() { } +namespace { + +/** + * \ingroup LuaScripts + * show(): + * Shows the GUI + */ +int show(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + + OsEng.gui().setEnabled(true); + return 0; +} + +/** + * \ingroup LuaScripts + * hide(): + * Hides the console + */ +int hide(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + + OsEng.gui().setEnabled(false); + return 0; +} + +/** + * \ingroup LuaScripts + * toggle(): + * Toggles the console + */ +int toggle(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + + OsEng.gui().setEnabled(!OsEng.gui().isEnabled()); + return 0; +} + +} + +scripting::ScriptEngine::LuaLibrary GUI::luaLibrary() { + return { + "gui", + { + { + "show", + &show, + "", + "Shows the console" + }, + { + "hide", + &hide, + "", + "Hides the console" + }, + { + "toggle", + &toggle, + "", + "Toggles the console" + } + } + }; +} + } // namespace openspace diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index aeb456efd1..4c2b20e998 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -24,7 +24,6 @@ #include -#include #include #include #include @@ -83,7 +82,6 @@ OpenSpaceEngine* OpenSpaceEngine::_engine = nullptr; OpenSpaceEngine::OpenSpaceEngine(std::string programName) : _commandlineParser(programName, true) - , _gui(nullptr) , _syncBuffer(nullptr) { // initialize OpenSpace helpers @@ -402,6 +400,7 @@ bool OpenSpaceEngine::initialize() { _scriptEngine.addLibrary(Time::luaLibrary()); _scriptEngine.addLibrary(interaction::InteractionHandler::luaLibrary()); _scriptEngine.addLibrary(LuaConsole::luaLibrary()); + _scriptEngine.addLibrary(GUI::luaLibrary()); // TODO: Maybe move all scenegraph and renderengine stuff to initializeGL scriptEngine().initialize(); @@ -446,12 +445,7 @@ bool OpenSpaceEngine::initialize() { // Load a light and a monospaced font loadFonts(); - using constants::configurationmanager::keyEnableGui; - bool enableGUI = false; - configurationManager().getValue(keyEnableGui, enableGUI); - if (enableGUI) { - _gui = new GUI; - } + _gui.initialize(); return true; } @@ -476,10 +470,14 @@ LuaConsole& OpenSpaceEngine::console() { return _console; } +GUI& OpenSpaceEngine::gui() { + return _gui; +} + bool OpenSpaceEngine::initializeGL() { bool success = _renderEngine.initializeGL(); - if (_gui) - _gui->initializeGL(); + if (_gui.isEnabled()) + _gui.initializeGL(); return success; } @@ -499,7 +497,7 @@ void OpenSpaceEngine::preSynchronization() { void OpenSpaceEngine::postSynchronizationPreDraw() { _renderEngine.postSynchronizationPreDraw(); - if (_gui) { + if (sgct::Engine::instance()->isMaster() && _gui.isEnabled()) { double posX, posY; sgct::Engine::instance()->getMousePos(0, &posX, &posY); @@ -511,21 +509,23 @@ void OpenSpaceEngine::postSynchronizationPreDraw() { bool buttons[2] = { button0 != 0, button1 != 0 }; double dt = std::max(sgct::Engine::instance()->getDt(), 1.0/60.0); - _gui->startFrame(dt, glm::vec2(glm::ivec2(x,y)), glm::vec2(posX, posY), buttons); + _gui.startFrame(dt, glm::vec2(glm::ivec2(x,y)), glm::vec2(posX, posY), buttons); } } void OpenSpaceEngine::render() { _renderEngine.render(); - // If currently writing a command, render it to screen - sgct::SGCTWindow* w = sgct::Engine::instance()->getActiveWindowPtr(); - if (sgct::Engine::instance()->isMaster() && !w->isUsingFisheyeRendering() && _console.isVisible()) { - _console.render(); - } + if (sgct::Engine::instance()->isMaster()) { + // If currently writing a command, render it to screen + sgct::SGCTWindow* w = sgct::Engine::instance()->getActiveWindowPtr(); + if (sgct::Engine::instance()->isMaster() && !w->isUsingFisheyeRendering() && _console.isVisible()) { + _console.render(); + } - if (_gui) - _gui->endFrame(); + if (_gui.isEnabled()) + _gui.endFrame(); + } } void OpenSpaceEngine::postDraw() { @@ -537,8 +537,8 @@ void OpenSpaceEngine::postDraw() { void OpenSpaceEngine::keyboardCallback(int key, int action) { if (sgct::Engine::instance()->isMaster()) { - if (_gui) { - bool isConsumed = _gui->keyCallback(key, action); + if (_gui.isEnabled()) { + bool isConsumed = _gui.keyCallback(key, action); if (isConsumed) return; } @@ -556,39 +556,47 @@ void OpenSpaceEngine::keyboardCallback(int key, int action) { } void OpenSpaceEngine::charCallback(unsigned int codepoint) { - if (_gui) { - bool isConsumed = _gui->charCallback(codepoint); - if (isConsumed) - return; - } + if (sgct::Engine::instance()->isMaster()) { + if (_gui.isEnabled()) { + bool isConsumed = _gui.charCallback(codepoint); + if (isConsumed) + return; + } - if (_console.isVisible()) { - _console.charCallback(codepoint); + if (_console.isVisible()) { + _console.charCallback(codepoint); + } } } void OpenSpaceEngine::mouseButtonCallback(int key, int action) { - if (_gui) { - bool isConsumed = _gui->mouseButtonCallback(key, action); - if (isConsumed && action != SGCT_RELEASE) - return; + if (sgct::Engine::instance()->isMaster()) { + if (_gui.isEnabled()) { + bool isConsumed = _gui.mouseButtonCallback(key, action); + if (isConsumed && action != SGCT_RELEASE) + return; + } + + _interactionHandler.mouseButtonCallback(key, action); } - - _interactionHandler.mouseButtonCallback(key, action); } void OpenSpaceEngine::mousePositionCallback(int x, int y) { - _interactionHandler.mousePositionCallback(x, y); + if (sgct::Engine::instance()->isMaster()) { + _interactionHandler.mousePositionCallback(x, y); + } } void OpenSpaceEngine::mouseScrollWheelCallback(int pos) { - if (_gui) { - bool isConsumed = _gui->mouseWheelCallback(pos); - if (isConsumed) - return; - } + if (sgct::Engine::instance()->isMaster()) { + if (_gui.isEnabled()) { + bool isConsumed = _gui.mouseWheelCallback(pos); + if (isConsumed) + return; + } - _interactionHandler.mouseScrollWheelCallback(pos); + _interactionHandler.mouseScrollWheelCallback(pos); + } } void OpenSpaceEngine::encode() { diff --git a/src/scenegraph/scenegraph.cpp b/src/scenegraph/scenegraph.cpp index 1641adfa92..97cca49847 100644 --- a/src/scenegraph/scenegraph.cpp +++ b/src/scenegraph/scenegraph.cpp @@ -464,7 +464,7 @@ bool SceneGraph::loadSceneInternal(const std::string& sceneDescriptionFilePath) for (auto node : _nodes) { std::vector&& properties = node->propertiesRecursive(); for (auto p : properties) { - OsEng.gui()->registerProperty(p); + OsEng.gui().registerProperty(p); } }