diff --git a/include/openspace/interaction/controller.h b/include/openspace/interaction/controller.h index 6675b1acb2..357d1bfa45 100644 --- a/include/openspace/interaction/controller.h +++ b/include/openspace/interaction/controller.h @@ -44,24 +44,6 @@ public: void setHandler(InteractionHandler* handler); protected: - SceneGraphNode* focusNode() const; - - Camera* camera() const; - - - double deltaTime() const; - - void orbitDelta(const glm::quat& rotation); - - void rotateDelta(const glm::quat& rotation); - - void distanceDelta(const PowerScaledScalar& distance); - - void lookAt(const glm::quat& rotation); - - void setRotation(const glm::quat& rotation); - -private: InteractionHandler* _handler; }; diff --git a/include/openspace/interaction/interactionhandler.h b/include/openspace/interaction/interactionhandler.h index c9fbde65f5..0313a5c989 100644 --- a/include/openspace/interaction/interactionhandler.h +++ b/include/openspace/interaction/interactionhandler.h @@ -62,6 +62,19 @@ public: void mousePositionCallback(int x, int y); void mouseScrollWheelCallback(int pos); + double deltaTime() const; + + void orbitDelta(const glm::quat& rotation); + + void rotateDelta(const glm::quat& rotation); + + void distanceDelta(const PowerScaledScalar& distance); + + void lookAt(const glm::quat& rotation); + + void setRotation(const glm::quat& rotation); + + private: friend class Controller; diff --git a/include/openspace/interaction/keyboardcontroller.h b/include/openspace/interaction/keyboardcontroller.h index 3b8d1b979f..045fbcc0c1 100644 --- a/include/openspace/interaction/keyboardcontroller.h +++ b/include/openspace/interaction/keyboardcontroller.h @@ -34,17 +34,20 @@ namespace interaction { class KeyboardController : public Controller { public: - virtual void keyPressed(KeyAction action, Keys key) = 0; + virtual void keyPressed(KeyAction action, Key key, KeyModifier modifier) = 0; }; class KeyboardControllerFixed : public KeyboardController { public: - void keyPressed(KeyAction action, Keys key); + void keyPressed(KeyAction action, Key key, KeyModifier modifier); }; class KeyboardControllerLua : public KeyboardController { public: - void keyPressed(KeyAction action, Keys key); + void keyPressed(KeyAction action, Key key, KeyModifier modifier); + +protected: + std::string keyToString(Key key, KeyModifier mod) const; }; } // namespace interaction diff --git a/include/openspace/interaction/keys.h b/include/openspace/interaction/keys.h index 19e722577e..b380a7b789 100644 --- a/include/openspace/interaction/keys.h +++ b/include/openspace/interaction/keys.h @@ -36,7 +36,15 @@ enum class KeyAction { Repeat = SGCT_REPEAT }; -enum class Keys { +enum class KeyModifier { + None = 0, + Shift = GLFW_MOD_SHIFT, + Control = GLFW_MOD_CONTROL, + Alt = GLFW_MOD_ALT, + Super = GLFW_MOD_SUPER +}; + +enum class Key { Unknown = SGCT_KEY_UNKNOWN, Space = SGCT_KEY_SPACE, Apostrophe = SGCT_KEY_APOSTROPHE, @@ -157,11 +165,7 @@ enum class Keys { RightAlt = SGCT_KEY_RIGHT_ALT, RightSuper = SGCT_KEY_RIGHT_SUPER, Menu = SGCT_KEY_MENU, - Last = SGCT_KEY_LAST, - Shift = LeftShift | RightShift, - Control = LeftControl | RightControl, - Alt = LeftAlt | RightAlt, - Super = LeftSuper | RightSuper + Last = SGCT_KEY_LAST }; } // namespace interaction diff --git a/include/openspace/scripting/scriptengine.h b/include/openspace/scripting/scriptengine.h index 648bc0ca12..5d5a76eead 100644 --- a/include/openspace/scripting/scriptengine.h +++ b/include/openspace/scripting/scriptengine.h @@ -26,6 +26,7 @@ #define __SCRIPTENGINE_H__ #include + #include /** @@ -50,19 +51,24 @@ public: }; ScriptEngine(); - + bool initialize(); void deinitialize(); - bool addLibrary(const LuaLibrary& library); + void initializeLuaState(lua_State* state); + + void addLibrary(const LuaLibrary& library); bool hasLibrary(const std::string& name); bool runScript(const std::string& script); bool runScriptFile(const std::string& filename); + private: + bool registerLuaLibrary(lua_State* state, const LuaLibrary& library); + void addLibraryFunctions(lua_State* state, const LuaLibrary& library, bool replace); + bool isLibraryNameAllowed(const std::string& name); - void addLibraryFunctions(const LuaLibrary& library, bool replace); void addBaseLibrary(); void remapPrintFunction(); diff --git a/scripts/default_keybinding.lua b/scripts/default_keybinding.lua index 9ebf558a60..56943efb65 100644 --- a/scripts/default_keybinding.lua +++ b/scripts/default_keybinding.lua @@ -1,4 +1,4 @@ return { - w = function() print("w") end, - s = function() print("s") end + W = function() print("w") end, + S = function() print("s") end } diff --git a/src/engine/openspaceengine.cpp b/src/engine/openspaceengine.cpp index 44601aa0d1..f5e642530a 100644 --- a/src/engine/openspaceengine.cpp +++ b/src/engine/openspaceengine.cpp @@ -283,14 +283,14 @@ bool OpenSpaceEngine::initialize() { FactoryManager::initialize(); - scriptEngine().initialize(); - // Register Lua script functions LDEBUG("Registering Lua libraries"); scriptEngine().addLibrary(RenderEngine::luaLibrary()); scriptEngine().addLibrary(SceneGraph::luaLibrary()); scriptEngine().addLibrary(Time::luaLibrary()); + scriptEngine().initialize(); + // Load scenegraph SceneGraph* sceneGraph = new SceneGraph; _renderEngine.setSceneGraph(sceneGraph); diff --git a/src/interaction/controller.cpp b/src/interaction/controller.cpp index aee74159f9..311fdfc6ac 100644 --- a/src/interaction/controller.cpp +++ b/src/interaction/controller.cpp @@ -34,98 +34,6 @@ void Controller::setHandler(InteractionHandler* handler) _handler = handler; } -SceneGraphNode* Controller::focusNode() const -{ - assert(_handler); - return _handler->_focusNode; -} - -Camera* Controller::camera() const -{ - assert(_handler); - return _handler->_camera; -} - -double Controller::deltaTime() const -{ - assert(_handler); - return _handler->_deltaTime; -} - -void openspace::interaction::Controller::orbitDelta(const glm::quat& rotation) -{ - assert(_handler); - _handler->lockControls(); - - // the camera position - psc relative = _handler->_camera->position(); - - // should be changed to something more dynamic =) - psc origin; - if (_handler->_focusNode) { - origin = _handler->_focusNode->worldPosition(); - } - - psc relative_origin_coordinate = relative - origin; - //glm::mat4 rotation_matrix = glm::mat4_cast(glm::inverse(rotation)); - //relative_origin_coordinate = relative_origin_coordinate.vec4() * glm::inverse(rotation); - relative_origin_coordinate = glm::inverse(rotation) * relative_origin_coordinate.vec4(); - relative = relative_origin_coordinate + origin; - - _handler->_camera->setPosition(relative); - //camera_->rotate(rotation); - //camera_->setRotation(glm::mat4_cast(rotation)); - - glm::mat4 la = glm::lookAt(_handler->_camera->position().vec3(), origin.vec3(), glm::rotate(rotation, _handler->_camera->lookUpVector())); - _handler->_camera->setRotation(la); - //camera_->setLookUpVector(); - - _handler->unlockControls(); -} - -void openspace::interaction::Controller::rotateDelta(const glm::quat& rotation) -{ - assert(_handler); - _handler->lockControls(); - _handler->_camera->rotate(rotation); - _handler->unlockControls(); -} - -void openspace::interaction::Controller::distanceDelta(const PowerScaledScalar& distance) -{ - assert(_handler); - _handler->lockControls(); - - psc relative = _handler->_camera->position(); - const psc origin = (_handler->_focusNode) ? _handler->_focusNode->worldPosition() : psc(); - - psc relative_origin_coordinate = relative - origin; - const glm::vec3 dir(relative_origin_coordinate.direction()); - glm::vec3 newdir = dir * distance[0]; - relative_origin_coordinate = newdir; - relative_origin_coordinate[3] = distance[1]; - relative = relative + relative_origin_coordinate; - - relative_origin_coordinate = relative - origin; - newdir = relative_origin_coordinate.direction(); - - // update only if on the same side of the origin - if (glm::angle(newdir, dir) < 90.0f) - _handler->_camera->setPosition(relative); - - _handler->unlockControls(); -} - -void openspace::interaction::Controller::lookAt(const glm::quat& rotation) -{ - assert(_handler); -} - -void openspace::interaction::Controller::setRotation(const glm::quat& rotation) -{ - assert(_handler); -} - } // namespace interaction } // namespace openspace diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index 926a1b858f..7802c73de4 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -99,7 +99,7 @@ const Camera* const InteractionHandler::camera() const { void InteractionHandler::keyboardCallback(int key, int action) { if (_keyboardController) { auto start = ghoul::HighResClock::now(); - _keyboardController->keyPressed(KeyAction(action), Keys(key)); + _keyboardController->keyPressed(KeyAction(action), Key(key), KeyModifier::None); auto end = ghoul::HighResClock::now(); LINFO("Keyboard timing: " << std::chrono::duration_cast(end - start).count() << "ns"); } @@ -121,6 +121,79 @@ void InteractionHandler::mouseScrollWheelCallback(int pos) { _mouseController->scrollWheel(float(pos)); } +void InteractionHandler::orbitDelta(const glm::quat& rotation) +{ + lockControls(); + + // the camera position + psc relative = _camera->position(); + + // should be changed to something more dynamic =) + psc origin; + if (_focusNode) { + origin = _focusNode->worldPosition(); + } + + psc relative_origin_coordinate = relative - origin; + //glm::mat4 rotation_matrix = glm::mat4_cast(glm::inverse(rotation)); + //relative_origin_coordinate = relative_origin_coordinate.vec4() * glm::inverse(rotation); + relative_origin_coordinate = glm::inverse(rotation) * relative_origin_coordinate.vec4(); + relative = relative_origin_coordinate + origin; + + _camera->setPosition(relative); + //camera_->rotate(rotation); + //camera_->setRotation(glm::mat4_cast(rotation)); + + glm::mat4 la = glm::lookAt(_camera->position().vec3(), origin.vec3(), glm::rotate(rotation, _camera->lookUpVector())); + _camera->setRotation(la); + //camera_->setLookUpVector(); + + unlockControls(); +} + +void InteractionHandler::rotateDelta(const glm::quat& rotation) +{ + lockControls(); + _camera->rotate(rotation); + unlockControls(); +} + +void InteractionHandler::distanceDelta(const PowerScaledScalar& distance) +{ + lockControls(); + + psc relative = _camera->position(); + const psc origin = (_focusNode) ? _focusNode->worldPosition() : psc(); + + psc relative_origin_coordinate = relative - origin; + const glm::vec3 dir(relative_origin_coordinate.direction()); + glm::vec3 newdir = dir * distance[0]; + relative_origin_coordinate = newdir; + relative_origin_coordinate[3] = distance[1]; + relative = relative + relative_origin_coordinate; + + relative_origin_coordinate = relative - origin; + newdir = relative_origin_coordinate.direction(); + + // update only if on the same side of the origin + if (glm::angle(newdir, dir) < 90.0f) + _camera->setPosition(relative); + + unlockControls(); +} + +void InteractionHandler::lookAt(const glm::quat& rotation) +{ +} + +void InteractionHandler::setRotation(const glm::quat& rotation) +{ +} + +double InteractionHandler::deltaTime() const { + return _deltaTime; +} + } // namespace interaction } // namespace openspace diff --git a/src/interaction/keyboardcontroller.cpp b/src/interaction/keyboardcontroller.cpp index ec3ba81399..e3788d7a77 100644 --- a/src/interaction/keyboardcontroller.cpp +++ b/src/interaction/keyboardcontroller.cpp @@ -35,86 +35,86 @@ namespace openspace { namespace interaction { -void KeyboardControllerFixed::keyPressed(KeyAction action, Keys key) { +void KeyboardControllerFixed::keyPressed(KeyAction action, Key key, KeyModifier modifier) { // TODO package in script const double speed = 2.75; - const double dt = deltaTime(); + const double dt = _handler->deltaTime(); if(action == KeyAction::Press|| action == KeyAction::Repeat) { - if (key == Keys::S) { + if (key == Key::S) { glm::vec3 euler(speed * dt, 0.0, 0.0); glm::quat rot = glm::quat(euler); - orbitDelta(rot); + _handler->orbitDelta(rot); } - if (key == Keys::W) { + if (key == Key::W) { glm::vec3 euler(-speed * dt, 0.0, 0.0); glm::quat rot = glm::quat(euler); - orbitDelta(rot); + _handler->orbitDelta(rot); } - if (key == Keys::A) { + if (key == Key::A) { glm::vec3 euler(0.0, -speed * dt, 0.0); glm::quat rot = glm::quat(euler); - orbitDelta(rot); + _handler->orbitDelta(rot); } - if (key == Keys::D) { + if (key == Key::D) { glm::vec3 euler(0.0, speed * dt, 0.0); glm::quat rot = glm::quat(euler); - orbitDelta(rot); + _handler->orbitDelta(rot); } - if (key == Keys::Q) { + if (key == Key::Q) { Time::ref().advanceTime(dt); } - if (key == Keys::Right) { + if (key == Key::Right) { glm::vec3 euler(0.0, speed * dt, 0.0); glm::quat rot = glm::quat(euler); - rotateDelta(rot); + _handler->rotateDelta(rot); } - if (key == Keys::Left) { + if (key == Key::Left) { glm::vec3 euler(0.0, -speed * dt, 0.0); glm::quat rot = glm::quat(euler); - rotateDelta(rot); + _handler->rotateDelta(rot); } - if (key == Keys::Down) { + if (key == Key::Down) { glm::vec3 euler(speed * dt, 0.0, 0.0); glm::quat rot = glm::quat(euler); - rotateDelta(rot); + _handler->rotateDelta(rot); } - if (key == Keys::Up) { + if (key == Key::Up) { glm::vec3 euler(-speed * dt, 0.0, 0.0); glm::quat rot = glm::quat(euler); - rotateDelta(rot); + _handler->rotateDelta(rot); } - if (key == Keys::R) { + if (key == Key::R) { PowerScaledScalar dist(-speed * dt, 0.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } - if (key == Keys::F) { + if (key == Key::F) { PowerScaledScalar dist(speed * dt, 0.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } - if (key == Keys::T) { + if (key == Key::T) { PowerScaledScalar dist(-speed * pow(10, 11) * dt, 0.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } //if (key == Keys::G) { // acc += 0.001; // PowerScaledScalar dist(speed * pow(10, 8 * acc) * dt, 0.0); // distanceDelta(dist); //} - if (key == Keys::Y) { + if (key == Key::Y) { PowerScaledScalar dist(-speed * 100.0 * dt, 6.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } - if (key == Keys::H) { + if (key == Key::H) { PowerScaledScalar dist(speed * 100.0 * dt, 6.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } - if (key == Keys::KeypadSubtract) { + if (key == Key::KeypadSubtract) { glm::vec2 s = OsEng.renderEngine().camera()->scaling(); s[1] -= 0.5; OsEng.renderEngine().camera()->setScaling(s); } - if (key == Keys::KeypadAdd) { + if (key == Key::KeypadAdd) { glm::vec2 s = OsEng.renderEngine().camera()->scaling(); s[1] += 0.5; OsEng.renderEngine().camera()->setScaling(s); @@ -148,7 +148,7 @@ void KeyboardControllerFixed::keyPressed(KeyAction action, Keys key) { */ } -void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) { +void KeyboardControllerLua::keyPressed(KeyAction action, Key key, KeyModifier modifier) { std::string _loggerCat = "KeyboardControllerLua"; lua_State* s = luaL_newstate(); @@ -167,10 +167,11 @@ void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) { auto start = ghoul::HighResClock::now(); - - lua_pushstring(s, "w"); - lua_gettable(s, -2); - lua_pcall(s, 0, 0, 0); + lua_getfield(s, -1, keyToString(key, modifier).c_str()); + if (!lua_isnil(s, -1)) + lua_pcall(s, 0, 0, 0); + else + LINFO("Key not found"); auto end = ghoul::HighResClock::now(); LINFO("Keyboard timing: " << std::chrono::duration_cast(end - start).count() << "ns"); @@ -178,5 +179,144 @@ void KeyboardControllerLua::keyPressed(KeyAction action, Keys key) { } +std::string KeyboardControllerLua::keyToString(Key key, KeyModifier mod) const { + std::string result = ""; + int intMod = static_cast(mod); + if (intMod & static_cast(KeyModifier::Control)) + result += "CTRL + "; + if (intMod & static_cast(KeyModifier::Super)) + result += "SUPER + "; + if (intMod & static_cast(KeyModifier::Alt)) + result += "ALT + "; + if (intMod & static_cast(KeyModifier::Shift)) + result += "SHIFT + "; + + switch (key) { + case Key::Unknown: result += "Unknown"; break; + case Key::Space: result += "Space"; break; + case Key::Apostrophe: result += "Apostrophe"; break; + case Key::Comma: result += "Comma"; break; + case Key::Minus: result += "Minus"; break; + case Key::Period: result += "Period"; break; + case Key::Slash: result += "Slash"; break; + case Key::Num0: result += "0"; break; + case Key::Num1: result += "1"; break; + case Key::Num2: result += "2"; break; + case Key::Num3: result += "3"; break; + case Key::Num4: result += "4"; break; + case Key::Num5: result += "5"; break; + case Key::Num6: result += "6"; break; + case Key::Num7: result += "7"; break; + case Key::Num8: result += "8"; break; + case Key::Num9: result += "9"; break; + case Key::SemiColon: result += "SemiColon"; break; + case Key::Equal: result += "Equal"; break; + case Key::A: result += "A"; break; + case Key::B: result += "B"; break; + case Key::C: result += "C"; break; + case Key::D: result += "D"; break; + case Key::E: result += "E"; break; + case Key::F: result += "F"; break; + case Key::G: result += "G"; break; + case Key::H: result += "H"; break; + case Key::I: result += "I"; break; + case Key::J: result += "J"; break; + case Key::K: result += "K"; break; + case Key::L: result += "L"; break; + case Key::M: result += "M"; break; + case Key::N: result += "N"; break; + case Key::O: result += "O"; break; + case Key::P: result += "P"; break; + case Key::Q: result += "Q"; break; + case Key::R: result += "R"; break; + case Key::S: result += "S"; break; + case Key::T: result += "T"; break; + case Key::U: result += "U"; break; + case Key::V: result += "V"; break; + case Key::W: result += "W"; break; + case Key::X: result += "X"; break; + case Key::Y: result += "Y"; break; + case Key::Z: result += "Z"; break; + case Key::LeftBracket: result += "LeftBracket"; break; + case Key::BackSlash: result += "BackSlash"; break; + case Key::RightBracket: result += "RightBracket"; break; + case Key::GraveAccent: result += "GraveAccent"; break; + case Key::World1: result += "World1"; break; + case Key::World2: result += "World2"; break; + case Key::Escape: result += "Escape"; break; + case Key::Enter: result += "Enter"; break; + case Key::Tab: result += "Tab"; break; + case Key::BackSpace: result += "BackSpace"; break; + case Key::Insert: result += "Insert"; break; + case Key::Delete: result += "Delete"; break; + case Key::Right: result += "Right"; break; + case Key::Left: result += "Left"; break; + case Key::Down: result += "Down"; break; + case Key::Up: result += "Up"; break; + case Key::PageUp: result += "PageUp"; break; + case Key::PageDown: result += "PageDown"; break; + case Key::Home: result += "Home"; break; + case Key::End: result += "End"; break; + case Key::CapsLock: result += "CapsLock"; break; + case Key::ScrollLock: result += "ScrollLock"; break; + case Key::NumLock: result += "NumLock"; break; + case Key::PrintScreen: result += "PrintScreen"; break; + case Key::Pause: result += "Pause"; break; + case Key::F1: result += "F1"; break; + case Key::F2: result += "F2"; break; + case Key::F3: result += "F3"; break; + case Key::F4: result += "F4"; break; + case Key::F5: result += "F5"; break; + case Key::F6: result += "F6"; break; + case Key::F7: result += "F7"; break; + case Key::F8: result += "F8"; break; + case Key::F9: result += "F9"; break; + case Key::F10: result += "F10"; break; + case Key::F11: result += "F11"; break; + case Key::F12: result += "F12"; break; + case Key::F13: result += "F13"; break; + case Key::F14: result += "F14"; break; + case Key::F15: result += "F15"; break; + case Key::F16: result += "F16"; break; + case Key::F17: result += "F17"; break; + case Key::F18: result += "F18"; break; + case Key::F19: result += "F19"; break; + case Key::F20: result += "F20"; break; + case Key::F21: result += "F21"; break; + case Key::F22: result += "F22"; break; + case Key::F23: result += "F23"; break; + case Key::F24: result += "F24"; break; + case Key::F25: result += "F25"; break; + case Key::Keypad0: result += "Keypad0"; break; + case Key::Keypad1: result += "Keypad1"; break; + case Key::Keypad2: result += "Keypad2"; break; + case Key::Keypad3: result += "Keypad3"; break; + case Key::Keypad4: result += "Keypad4"; break; + case Key::Keypad5: result += "Keypad5"; break; + case Key::Keypad6: result += "Keypad6"; break; + case Key::Keypad7: result += "Keypad7"; break; + case Key::Keypad8: result += "Keypad8"; break; + case Key::Keypad9: result += "Keypad9"; break; + case Key::KeypadDecimal: result += "KeypadDecimal"; break; + case Key::KeypadDivide: result += "KeypadDivide"; break; + case Key::KeypadMultiply: result += "KeypadMultiply"; break; + case Key::KeypadSubtract: result += "KeypadSubtract"; break; + case Key::KeypadAdd: result += "KeypadAdd"; break; + case Key::KeypadEnter: result += "KeypadEnter"; break; + case Key::LeftShift: result += "LeftShift"; break; + case Key::LeftControl: result += "LeftControl"; break; + case Key::LeftAlt: result += "LeftAlt"; break; + case Key::LeftSuper: result += "LeftSuper"; break; + case Key::RightShift: result += "RightShift"; break; + case Key::RightControl: result += "RightControl"; break; + case Key::RightAlt: result += "RightAlt"; break; + case Key::RightSuper: result += "RightSuper"; break; + case Key::Menu: result += "Menu"; break; + default: + assert(false); + } + return std::move(result); +} + } // namespace interaction } // namespace openspace \ No newline at end of file diff --git a/src/interaction/mousecontroller.cpp b/src/interaction/mousecontroller.cpp index 35a58c93f7..9d089f4d15 100644 --- a/src/interaction/mousecontroller.cpp +++ b/src/interaction/mousecontroller.cpp @@ -24,6 +24,8 @@ #include +#include + namespace openspace { namespace interaction { @@ -54,8 +56,8 @@ glm::vec3 MouseController::mapToCamera(glm::vec3 trackballPos) { // return glm::vec3((sgct::Engine::instance()->getActiveViewMatrix() * glm::vec4(trackballPos,0))); //Get x,y,z axis vectors of current camera view - glm::vec3 currentViewYaxis = glm::normalize(camera()->lookUpVector()); - psc viewDir = camera()->position() - focusNode()->worldPosition(); + glm::vec3 currentViewYaxis = glm::normalize(_handler->camera()->lookUpVector()); + psc viewDir = _handler->camera()->position() - _handler->focusNode()->worldPosition(); glm::vec3 currentViewZaxis = glm::normalize(viewDir.vec3()); glm::vec3 currentViewXaxis = glm::normalize(glm::cross(currentViewYaxis, currentViewZaxis)); @@ -87,7 +89,7 @@ void MouseController::trackballRotate(int x, int y) { if (curTrackballPos != _lastTrackballPos) { // calculate rotation angle (in radians) float rotationAngle = glm::angle(curTrackballPos, _lastTrackballPos); - rotationAngle *= deltaTime() * 100.0f; + rotationAngle *= _handler->deltaTime() * 100.0f; // Map trackballpos to camera // glm::vec3 trackballMappedToCamera = mapToCamera(_lastTrackballPos - curTrackballPos); @@ -100,7 +102,7 @@ void MouseController::trackballRotate(int x, int y) { glm::quat quaternion = glm::angleAxis(rotationAngle, rotationAxis); // Apply quaternion to camera - orbitDelta(quaternion); + _handler->orbitDelta(quaternion); _lastTrackballPos = curTrackballPos; } @@ -128,14 +130,14 @@ void TrackballMouseController::move(float x, float y) { void TrackballMouseController::scrollWheel(int pos) { const double speed = 4.75; - const double dt = deltaTime(); + const double dt = _handler->deltaTime(); if (pos < 0) { PowerScaledScalar dist(speed * dt, 0.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } else if (pos > 0) { PowerScaledScalar dist(-speed * dt, 0.0); - distanceDelta(dist); + _handler->distanceDelta(dist); } } diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 098b543cc6..006e51d431 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -182,23 +182,20 @@ ScriptEngine::ScriptEngine() } bool ScriptEngine::initialize() { + LDEBUG("Adding base functions"); + addBaseLibrary(); + _state = luaL_newstate(); LDEBUG("Creating Lua state"); if (_state == nullptr) { LFATAL("Error creating new Lua state: Memory allocation error"); return false; } + LDEBUG("Open Lua libraries"); luaL_openlibs(_state); - - LDEBUG("Add OpenSpace modules"); - - LDEBUG("Create openspace base library"); - lua_newtable(_state); - lua_setglobal(_state, _openspaceLibraryName.c_str()); - - LDEBUG("Adding base functions"); - addBaseLibrary(); + + initializeLuaState(_state); LDEBUG("Remap print function"); remapPrintFunction(); @@ -211,42 +208,8 @@ void ScriptEngine::deinitialize() { _state = nullptr; } -bool ScriptEngine::addLibrary(const ScriptEngine::LuaLibrary& library) { - assert(_state); - if (library.functions.empty()) { - LERROR("Lua library '" << library.name << "' does not have any functions"); - return false; - } - - //ghoul::lua::logStack(_state); - lua_getglobal(_state, _openspaceLibraryName.c_str()); - //ghoul::lua::logStack(_state); - if (library.name.empty()) { - //ghoul::lua::logStack(_state); - addLibraryFunctions(library, true); - //ghoul::lua::logStack(_state); - lua_pop(_state, 1); - //ghoul::lua::logStack(_state); - } - else { - const bool allowed = isLibraryNameAllowed(library.name); - if (!allowed) - return false; - - //ghoul::lua::logStack(_state); - - lua_pushstring(_state, library.name.c_str()); - //ghoul::lua::logStack(_state); - lua_newtable(_state); - //ghoul::lua::logStack(_state); - addLibraryFunctions(library, false); - lua_settable(_state, _setTableOffset); - //ghoul::lua::logStack(_state); - - _registeredLibraries.insert(library); - } - - return true; +void ScriptEngine::addLibrary(const ScriptEngine::LuaLibrary& library) { + _registeredLibraries.insert(library); } bool ScriptEngine::runScript(const std::string& script) { @@ -350,26 +313,26 @@ bool ScriptEngine::isLibraryNameAllowed(const std::string& name) return result; } -void ScriptEngine::addLibraryFunctions(const LuaLibrary& library, bool replace) -{ +void ScriptEngine::addLibraryFunctions(lua_State* state, const LuaLibrary& library, bool replace) { + assert(state); for (LuaLibrary::Function p : library.functions) { if (!replace) { //ghoul::lua::logStack(_state); - lua_getfield(_state, -1, p.name.c_str()); + lua_getfield(state, -1, p.name.c_str()); //ghoul::lua::logStack(_state); - const bool isNil = lua_isnil(_state, -1); + const bool isNil = lua_isnil(state, -1); if (!isNil) { LERROR("Function name '" << p.name << "' was already assigned"); return; } - lua_pop(_state, 1); + lua_pop(state, 1); } //ghoul::lua::logStack(_state); - lua_pushstring(_state, p.name.c_str()); + lua_pushstring(state, p.name.c_str()); //ghoul::lua::logStack(_state); - lua_pushcfunction(_state, p.function); + lua_pushcfunction(state, p.function); //ghoul::lua::logStack(_state); - lua_settable(_state, _setTableOffset); + lua_settable(state, _setTableOffset); //ghoul::lua::logStack(_state); } } @@ -436,6 +399,54 @@ void ScriptEngine::remapPrintFunction() { // lua_settable(_state, _setTableOffset); //ghoul::lua::logStack(_state); } - + +void ScriptEngine::initializeLuaState(lua_State* state) { + LDEBUG("Create openspace base library"); + lua_newtable(_state); + lua_setglobal(_state, _openspaceLibraryName.c_str()); + + LDEBUG("Add OpenSpace modules"); + for (auto lib : _registeredLibraries) + registerLuaLibrary(state, lib); +} + +bool ScriptEngine::registerLuaLibrary(lua_State* state, const LuaLibrary& library) { + assert(state); + if (library.functions.empty()) { + LERROR("Lua library '" << library.name << "' does not have any functions"); + return false; + } + + //ghoul::lua::logStack(_state); + lua_getglobal(state, _openspaceLibraryName.c_str()); + //ghoul::lua::logStack(_state); + if (library.name.empty()) { + //ghoul::lua::logStack(_state); + addLibraryFunctions(state, library, true); + //ghoul::lua::logStack(_state); + lua_pop(state, 1); + //ghoul::lua::logStack(_state); + } + else { + const bool allowed = isLibraryNameAllowed(library.name); + if (!allowed) + return false; + + //ghoul::lua::logStack(_state); + + lua_pushstring(state, library.name.c_str()); + //ghoul::lua::logStack(_state); + lua_newtable(state); + //ghoul::lua::logStack(_state); + addLibraryFunctions(state, library, false); + lua_settable(state, _setTableOffset); + //ghoul::lua::logStack(_state); + + _registeredLibraries.insert(library); + } + return true; +} + + } // namespace scripting } // namespace openspace