From 2bb2b906ff9ffbe52f404d3f0150fec014b52317 Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Wed, 20 May 2015 12:46:29 +0200 Subject: [PATCH] Outsource Lua functions into their own inline files --- src/interaction/interactionhandler.cpp | 196 +----------------- src/interaction/interactionhandler_lua.inl | 221 +++++++++++++++++++++ src/interaction/luaconsole.cpp | 48 +---- src/interaction/luaconsole_lua.inl | 73 +++++++ src/rendering/renderengine.cpp | 134 +------------ src/rendering/renderengine_lua.inl | 154 ++++++++++++++ src/scene/scene.cpp | 92 +-------- src/scene/scene_lua.inl | 112 +++++++++++ src/scripting/scriptengine.cpp | 128 +----------- src/scripting/scriptengine_lua.inl | 153 ++++++++++++++ src/util/time.cpp | 130 +----------- src/util/time_lua.inl | 154 ++++++++++++++ 12 files changed, 879 insertions(+), 716 deletions(-) create mode 100644 src/interaction/interactionhandler_lua.inl create mode 100644 src/interaction/luaconsole_lua.inl create mode 100644 src/rendering/renderengine_lua.inl create mode 100644 src/scene/scene_lua.inl create mode 100644 src/scripting/scriptengine_lua.inl create mode 100644 src/util/time_lua.inl diff --git a/src/interaction/interactionhandler.cpp b/src/interaction/interactionhandler.cpp index f51c9f8b7e..f3d702cb4c 100644 --- a/src/interaction/interactionhandler.cpp +++ b/src/interaction/interactionhandler.cpp @@ -182,202 +182,10 @@ namespace { } } +#include "interactionhandler_lua.inl" + namespace openspace { -namespace luascriptfunctions { - -/** - * \ingroup LuaScripts - * setOrigin(): - * Set the origin of the camera - */ -int setOrigin(lua_State* L) { - using ghoul::lua::luaTypeToString; - const std::string _loggerCat = "lua.setOrigin"; - - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - const int type = lua_type(L, -1); - if (type != LUA_TSTRING) - return luaL_error(L, "Expected string, got %i", type); - - std::string s = luaL_checkstring(L, -1); - - SceneGraphNode* node = sceneGraphNode(s); - if (!node) { - LWARNING("Could not find a node in scenegraph called '" << s <<"'"); - return 0; - } - - OsEng.interactionHandler()->setFocusNode(node); - - return 0; -} - -/** -* \ingroup LuaScripts -* bindKey(): -* Binds a key to Lua command -*/ -int bindKey(lua_State* L) { - using ghoul::lua::luaTypeToString; - const std::string _loggerCat = "lua.bindKey"; - - int nArguments = lua_gettop(L); - if (nArguments != 2) - return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); - - - std::string key = luaL_checkstring(L, -2); - std::string command = luaL_checkstring(L, -1); - - if (command.empty()) - return luaL_error(L, "Command string is empty"); - - int iKey = stringToKey(key); - - if (iKey == SGCT_KEY_UNKNOWN) { - LERROR("Could not find key '"<< key <<"'"); - return 0; - } - - - OsEng.interactionHandler()->bindKey(iKey, command); - - return 0; -} - -/** -* \ingroup LuaScripts -* clearKeys(): -* Clears all key bindings -*/ -int clearKeys(lua_State* L) { - using ghoul::lua::luaTypeToString; - const std::string _loggerCat = "lua.clearKeys"; - - int nArguments = lua_gettop(L); - if (nArguments != 0) - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - - OsEng.interactionHandler()->resetKeyBindings(); - - return 0; -} - -/** -* \ingroup LuaScripts -* dt(bool): -* Get current frame time -*/ -int dt(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - - lua_pushnumber(L,OsEng.interactionHandler()->deltaTime()); - return 1; -} - -/** -* \ingroup LuaScripts -* distance(double, double): -* Change distance to origin -*/ -int distance(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 2) - return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); - - double d1 = luaL_checknumber(L, -2); - double d2 = luaL_checknumber(L, -1); - PowerScaledScalar dist(static_cast(d1), static_cast(d2)); - OsEng.interactionHandler()->distanceDelta(dist); - return 0; -} - -/** - * \ingroup LuaScripts - * setInteractionSensitivity(double): - * Changes the global interaction sensitivity to the passed value - */ -int setInteractionSensitivity(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - float sensitivity = static_cast(luaL_checknumber(L, -1)); - OsEng.interactionHandler()->setInteractionSensitivity(sensitivity); - return 0; -} - -/** - * \ingroup LuaScripts - * interactionSensitivity(): - * Returns the current, global interaction sensitivity - */ -int interactionSensitivity(lua_State* L) { - float sensitivity = OsEng.interactionHandler()->interactionSensitivity(); - lua_pushnumber(L, sensitivity); - return 1; -} - -/** - * \ingroup LuaScripts - * setInvertRoll(bool): - * Determines if the roll movement is inverted - */ -int setInvertRoll(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - bool invert = lua_toboolean(L, -1) == 1; - OsEng.interactionHandler()->setInvertRoll(invert); - return 0; -} - -/** - * \ingroup LuaScripts - * invertRoll(): - * Returns the current setting for inversion of roll movement - */ -int invertRoll(lua_State* L) { - bool invert = OsEng.interactionHandler()->invertRoll(); - lua_pushboolean(L, invert); - return 1; -} - -/** - * \ingroup LuaScripts - * setInvertRotation(bool): - * Determines if the rotation movement is inverted - */ -int setInvertRotation(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - bool invert = lua_toboolean(L, -1) == 1; - OsEng.interactionHandler()->setInvertRotation(invert); - return 0; -} - -/** - * \ingroup LuaScripts - * invertRotation(): - * Returns the current setting for inversion of rotation movement - */ -int invertRotation(lua_State* L) { - bool invert = OsEng.interactionHandler()->invertRotation(); - lua_pushboolean(L, invert); - return 1; -} - -} // namespace luascriptfunctions - //InteractionHandler::InteractionHandler() { // // initiate pointers // _camera = nullptr; diff --git a/src/interaction/interactionhandler_lua.inl b/src/interaction/interactionhandler_lua.inl new file mode 100644 index 0000000000..ee87c72341 --- /dev/null +++ b/src/interaction/interactionhandler_lua.inl @@ -0,0 +1,221 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + +/** + * \ingroup LuaScripts + * setOrigin(): + * Set the origin of the camera + */ +int setOrigin(lua_State* L) { + using ghoul::lua::luaTypeToString; + const std::string _loggerCat = "lua.setOrigin"; + + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + const int type = lua_type(L, -1); + if (type != LUA_TSTRING) + return luaL_error(L, "Expected string, got %i", type); + + std::string s = luaL_checkstring(L, -1); + + SceneGraphNode* node = sceneGraphNode(s); + if (!node) { + LWARNING("Could not find a node in scenegraph called '" << s <<"'"); + return 0; + } + + OsEng.interactionHandler()->setFocusNode(node); + + return 0; +} + +/** +* \ingroup LuaScripts +* bindKey(): +* Binds a key to Lua command +*/ +int bindKey(lua_State* L) { + using ghoul::lua::luaTypeToString; + const std::string _loggerCat = "lua.bindKey"; + + int nArguments = lua_gettop(L); + if (nArguments != 2) + return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); + + + std::string key = luaL_checkstring(L, -2); + std::string command = luaL_checkstring(L, -1); + + if (command.empty()) + return luaL_error(L, "Command string is empty"); + + int iKey = stringToKey(key); + + if (iKey == SGCT_KEY_UNKNOWN) { + LERROR("Could not find key '"<< key <<"'"); + return 0; + } + + + OsEng.interactionHandler()->bindKey(iKey, command); + + return 0; +} + +/** +* \ingroup LuaScripts +* clearKeys(): +* Clears all key bindings +*/ +int clearKeys(lua_State* L) { + using ghoul::lua::luaTypeToString; + const std::string _loggerCat = "lua.clearKeys"; + + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + + OsEng.interactionHandler()->resetKeyBindings(); + + return 0; +} + +/** +* \ingroup LuaScripts +* dt(bool): +* Get current frame time +*/ +int dt(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + + lua_pushnumber(L,OsEng.interactionHandler()->deltaTime()); + return 1; +} + +/** +* \ingroup LuaScripts +* distance(double, double): +* Change distance to origin +*/ +int distance(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 2) + return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); + + double d1 = luaL_checknumber(L, -2); + double d2 = luaL_checknumber(L, -1); + PowerScaledScalar dist(static_cast(d1), static_cast(d2)); + OsEng.interactionHandler()->distanceDelta(dist); + return 0; +} + +/** + * \ingroup LuaScripts + * setInteractionSensitivity(double): + * Changes the global interaction sensitivity to the passed value + */ +int setInteractionSensitivity(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + float sensitivity = static_cast(luaL_checknumber(L, -1)); + OsEng.interactionHandler()->setInteractionSensitivity(sensitivity); + return 0; +} + +/** + * \ingroup LuaScripts + * interactionSensitivity(): + * Returns the current, global interaction sensitivity + */ +int interactionSensitivity(lua_State* L) { + float sensitivity = OsEng.interactionHandler()->interactionSensitivity(); + lua_pushnumber(L, sensitivity); + return 1; +} + +/** + * \ingroup LuaScripts + * setInvertRoll(bool): + * Determines if the roll movement is inverted + */ +int setInvertRoll(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + bool invert = lua_toboolean(L, -1) == 1; + OsEng.interactionHandler()->setInvertRoll(invert); + return 0; +} + +/** + * \ingroup LuaScripts + * invertRoll(): + * Returns the current setting for inversion of roll movement + */ +int invertRoll(lua_State* L) { + bool invert = OsEng.interactionHandler()->invertRoll(); + lua_pushboolean(L, invert); + return 1; +} + +/** + * \ingroup LuaScripts + * setInvertRotation(bool): + * Determines if the rotation movement is inverted + */ +int setInvertRotation(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + bool invert = lua_toboolean(L, -1) == 1; + OsEng.interactionHandler()->setInvertRotation(invert); + return 0; +} + +/** + * \ingroup LuaScripts + * invertRotation(): + * Returns the current setting for inversion of rotation movement + */ +int invertRotation(lua_State* L) { + bool invert = OsEng.interactionHandler()->invertRotation(); + lua_pushboolean(L, invert); + return 1; +} + +} // namespace luascriptfunctions + +} // namespace openspace diff --git a/src/interaction/luaconsole.cpp b/src/interaction/luaconsole.cpp index 59387bf8c3..920d7a7f24 100644 --- a/src/interaction/luaconsole.cpp +++ b/src/interaction/luaconsole.cpp @@ -44,54 +44,10 @@ namespace { const int NoAutoComplete = -1; } +#include "luaconsole_lua.inl" + namespace openspace { -namespace luascriptfunctions { - -/** - * \ingroup LuaScripts - * show(): - * Shows the console - */ -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.console()->setVisible(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.console()->setVisible(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.console()->toggleVisibility(); - return 0; -} - -} - LuaConsole::LuaConsole() : _inputPosition(0) , _activeCommand(0) diff --git a/src/interaction/luaconsole_lua.inl b/src/interaction/luaconsole_lua.inl new file mode 100644 index 0000000000..368855d411 --- /dev/null +++ b/src/interaction/luaconsole_lua.inl @@ -0,0 +1,73 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + +/** + * \ingroup LuaScripts + * show(): + * Shows the console + */ +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.console()->setVisible(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.console()->setVisible(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.console()->toggleVisibility(); + return 0; +} + +} // namespace luascriptfunctions + +} // namespace openspace diff --git a/src/rendering/renderengine.cpp b/src/rendering/renderengine.cpp index d0e2392256..354c40088b 100644 --- a/src/rendering/renderengine.cpp +++ b/src/rendering/renderengine.cpp @@ -44,7 +44,6 @@ #include #include #include -#include #include #include @@ -70,11 +69,7 @@ #define ABUFFER_FIXED 2 #define ABUFFER_DYNAMIC 3 -//#ifdef __APPLE__ -//#define ABUFFER_IMPLEMENTATION ABUFFER_FRAMEBUFFER -//#else -//#define ABUFFER_IMPLEMENTATION ABUFFER_SINGLE_LINKED -//#endif +#include "renderengine_lua.inl" namespace { const std::string _loggerCat = "RenderEngine"; @@ -92,133 +87,6 @@ namespace openspace { const std::string RenderEngine::PerformanceMeasurementSharedData = "OpenSpacePerformanceMeasurementSharedData"; -namespace luascriptfunctions { - -int changeCoordinateSystem(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - std::string newCenter = std::string(lua_tostring(L, -1)); - OsEng.renderEngine()->changeViewPoint(newCenter); - return 1; -} - -/** - * \ingroup LuaScripts - * takeScreenshot(): - * Save the rendering to an image file - */ -int takeScreenshot(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 0) - return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); - OsEng.renderEngine()->takeScreenshot(); - return 0; -} - -/** -* \ingroup LuaScripts -* visualizeABuffer(bool): -* Toggle the visualization of the ABuffer -*/ -int visualizeABuffer(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - const int type = lua_type(L, -1); - if (type != LUA_TBOOLEAN) - return luaL_error(L, "Expected argument of type 'bool'"); - bool b = lua_toboolean(L, -1) != 0; - OsEng.renderEngine()->toggleVisualizeABuffer(b); - return 0; -} - -/** -* \ingroup LuaScripts -* visualizeABuffer(bool): -* Toggle the visualization of the ABuffer -*/ -int showRenderInformation(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - const int type = lua_type(L, -1); - if (type != LUA_TBOOLEAN) - return luaL_error(L, "Expected argument of type 'bool'"); - bool b = lua_toboolean(L, -1) != 0; - OsEng.renderEngine()->toggleInfoText(b); - return 0; -} - -/** - * \ingroup LuaScripts - * showSGCTRenderStatistics(bool): - * Set the rendering of the SGCTRenderStatistics - */ -int showSGCTRenderStatistics(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - const int type = lua_type(L, -1); - if (type != LUA_TBOOLEAN) - return luaL_error(L, "Expected argument of type 'bool'"); - bool b = lua_toboolean(L, -1) != 0; - OsEng.renderEngine()->setSGCTRenderStatistics(b); - return 0; -} - -/** -* \ingroup LuaScripts -* visualizeABuffer(bool): -* Toggle the visualization of the ABuffer -*/ -int setPerformanceMeasurement(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - bool b = lua_toboolean(L, -1) != 0; - OsEng.renderEngine()->setPerformanceMeasurements(b); - return 0; -} - -/** -* \ingroup LuaScripts -* fadeIn(float): -* start a global fadein over (float) seconds -*/ -int fadeIn(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - double t = luaL_checknumber(L, -1); - - OsEng.renderEngine()->startFading(1, static_cast(t)); - return 0; -} -/** -* \ingroup LuaScripts -* fadeIn(float): -* start a global fadeout over (float) seconds -*/ -int fadeOut(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - double t = luaL_checknumber(L, -1); - - OsEng.renderEngine()->startFading(-1, static_cast(t)); - return 0; -} - -} // namespace luascriptfunctions - RenderEngine::RenderEngine() : _mainCamera(nullptr) , _sceneGraph(nullptr) diff --git a/src/rendering/renderengine_lua.inl b/src/rendering/renderengine_lua.inl new file mode 100644 index 0000000000..eb664afee7 --- /dev/null +++ b/src/rendering/renderengine_lua.inl @@ -0,0 +1,154 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + +int changeCoordinateSystem(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + std::string newCenter = std::string(lua_tostring(L, -1)); + OsEng.renderEngine()->changeViewPoint(newCenter); + return 1; +} + +/** + * \ingroup LuaScripts + * takeScreenshot(): + * Save the rendering to an image file + */ +int takeScreenshot(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 0) + return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments); + OsEng.renderEngine()->takeScreenshot(); + return 0; +} + +/** +* \ingroup LuaScripts +* visualizeABuffer(bool): +* Toggle the visualization of the ABuffer +*/ +int visualizeABuffer(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + const int type = lua_type(L, -1); + if (type != LUA_TBOOLEAN) + return luaL_error(L, "Expected argument of type 'bool'"); + bool b = lua_toboolean(L, -1) != 0; + OsEng.renderEngine()->toggleVisualizeABuffer(b); + return 0; +} + +/** +* \ingroup LuaScripts +* visualizeABuffer(bool): +* Toggle the visualization of the ABuffer +*/ +int showRenderInformation(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + const int type = lua_type(L, -1); + if (type != LUA_TBOOLEAN) + return luaL_error(L, "Expected argument of type 'bool'"); + bool b = lua_toboolean(L, -1) != 0; + OsEng.renderEngine()->toggleInfoText(b); + return 0; +} + +/** + * \ingroup LuaScripts + * showSGCTRenderStatistics(bool): + * Set the rendering of the SGCTRenderStatistics + */ +int showSGCTRenderStatistics(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + const int type = lua_type(L, -1); + if (type != LUA_TBOOLEAN) + return luaL_error(L, "Expected argument of type 'bool'"); + bool b = lua_toboolean(L, -1) != 0; + OsEng.renderEngine()->setSGCTRenderStatistics(b); + return 0; +} + +/** +* \ingroup LuaScripts +* visualizeABuffer(bool): +* Toggle the visualization of the ABuffer +*/ +int setPerformanceMeasurement(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + bool b = lua_toboolean(L, -1) != 0; + OsEng.renderEngine()->setPerformanceMeasurements(b); + return 0; +} + +/** +* \ingroup LuaScripts +* fadeIn(float): +* start a global fadein over (float) seconds +*/ +int fadeIn(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + double t = luaL_checknumber(L, -1); + + OsEng.renderEngine()->startFading(1, static_cast(t)); + return 0; +} +/** +* \ingroup LuaScripts +* fadeIn(float): +* start a global fadeout over (float) seconds +*/ +int fadeOut(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + double t = luaL_checknumber(L, -1); + + OsEng.renderEngine()->startFading(-1, static_cast(t)); + return 0; +} + +} // namespace luascriptfunctions + +}// namespace openspace diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp index 000a70b2dd..4486a34668 100644 --- a/src/scene/scene.cpp +++ b/src/scene/scene.cpp @@ -53,6 +53,8 @@ #include #include +#include "scene_lua.inl" + namespace { const std::string _loggerCat = "SceneGraph"; const std::string _moduleExtension = ".mod"; @@ -62,95 +64,7 @@ namespace { namespace openspace { -namespace luascriptfunctions { - -/** - * \ingroup LuaScripts - * setPropertyValue(string, *): - * Sets the property identified by the URI in the first argument to the value passed to - * the second argument. The type of the second argument is arbitrary, but it must agree - * with the type the denoted Property expects - */ -int property_setValue(lua_State* L) { - static const std::string _loggerCat = "property_setValue"; - using ghoul::lua::errorLocation; - using ghoul::lua::luaTypeToString; - - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS(L, 2, nArguments); - - std::string uri = luaL_checkstring(L, -2); - const int type = lua_type(L, -1); - - openspace::properties::Property* prop = property(uri); - if (!prop) { - LERROR(errorLocation(L) << "Property with URI '" << uri << "' was not found"); - return 0; - } - - - if (type != prop->typeLua()) { - LERROR(errorLocation(L) << "Property '" << uri << - "' does not accept input of type '" << luaTypeToString(type) << - "'. Requested type: '" << luaTypeToString(prop->typeLua()) << "'"); - return 0; - } - else - prop->setLua(L); - - return 0; -} - -/** - * \ingroup LuaScripts - * getPropertyValue(string): - * Returns the value of the property identified by the passed URI as a Lua object that can - * be passed to the setPropertyValue method. - */ -int property_getValue(lua_State* L) { - static const std::string _loggerCat = "property_getValue"; - using ghoul::lua::errorLocation; - - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); - - std::string uri = luaL_checkstring(L, -1); - - openspace::properties::Property* prop = property(uri); - if (!prop) { - LERROR(errorLocation(L) << "Property with URL '" << uri << "' was not found"); - return 0; - } - else - prop->getLua(L); - return 1; -} - -/** - * \ingroup LuaScripts - * getPropertyValue(string): - * Returns the value of the property identified by the passed URI as a Lua object that can - * be passed to the setPropertyValue method. - */ -int loadScene(lua_State* L) { - static const std::string _loggerCat = "loadScene"; - - int nArguments = lua_gettop(L); - SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); - - std::string sceneFile = luaL_checkstring(L, -1); - - OsEng.renderEngine()->scene()->scheduleLoadSceneFile(sceneFile); - - return 0; -} - -} // namespace luascriptfunctions - -Scene::Scene() - : _focus(SceneGraphNode::RootNodeName) -{ -} +Scene::Scene() : _focus(SceneGraphNode::RootNodeName) {} Scene::~Scene() { deinitialize(); diff --git a/src/scene/scene_lua.inl b/src/scene/scene_lua.inl new file mode 100644 index 0000000000..c08347e1d5 --- /dev/null +++ b/src/scene/scene_lua.inl @@ -0,0 +1,112 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + +/** + * \ingroup LuaScripts + * setPropertyValue(string, *): + * Sets the property identified by the URI in the first argument to the value passed to + * the second argument. The type of the second argument is arbitrary, but it must agree + * with the type the denoted Property expects + */ +int property_setValue(lua_State* L) { + static const std::string _loggerCat = "property_setValue"; + using ghoul::lua::errorLocation; + using ghoul::lua::luaTypeToString; + + int nArguments = lua_gettop(L); + SCRIPT_CHECK_ARGUMENTS(L, 2, nArguments); + + std::string uri = luaL_checkstring(L, -2); + const int type = lua_type(L, -1); + + openspace::properties::Property* prop = property(uri); + if (!prop) { + LERROR(errorLocation(L) << "Property with URI '" << uri << "' was not found"); + return 0; + } + + + if (type != prop->typeLua()) { + LERROR(errorLocation(L) << "Property '" << uri << + "' does not accept input of type '" << luaTypeToString(type) << + "'. Requested type: '" << luaTypeToString(prop->typeLua()) << "'"); + return 0; + } + else + prop->setLua(L); + + return 0; +} + +/** + * \ingroup LuaScripts + * getPropertyValue(string): + * Returns the value of the property identified by the passed URI as a Lua object that can + * be passed to the setPropertyValue method. + */ +int property_getValue(lua_State* L) { + static const std::string _loggerCat = "property_getValue"; + using ghoul::lua::errorLocation; + + int nArguments = lua_gettop(L); + SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); + + std::string uri = luaL_checkstring(L, -1); + + openspace::properties::Property* prop = property(uri); + if (!prop) { + LERROR(errorLocation(L) << "Property with URL '" << uri << "' was not found"); + return 0; + } + else + prop->getLua(L); + return 1; +} + +/** + * \ingroup LuaScripts + * getPropertyValue(string): + * Returns the value of the property identified by the passed URI as a Lua object that can + * be passed to the setPropertyValue method. + */ +int loadScene(lua_State* L) { + static const std::string _loggerCat = "loadScene"; + + int nArguments = lua_gettop(L); + SCRIPT_CHECK_ARGUMENTS(L, 1, nArguments); + + std::string sceneFile = luaL_checkstring(L, -1); + + OsEng.renderEngine()->scene()->scheduleLoadSceneFile(sceneFile); + + return 0; +} + +} // namespace luascriptfunctions + +} // namespace openspace diff --git a/src/scripting/scriptengine.cpp b/src/scripting/scriptengine.cpp index 771b00670a..83cc1bc49e 100644 --- a/src/scripting/scriptengine.cpp +++ b/src/scripting/scriptengine.cpp @@ -32,134 +32,10 @@ #include #include +#include "scriptengine_lua.inl" + namespace openspace { -namespace luascriptfunctions { - - int printInternal(ghoul::logging::LogManager::LogLevel level, lua_State* L) { - using ghoul::lua::luaTypeToString; - const std::string _loggerCat = "print"; - - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - const int type = lua_type(L, -1); - switch (type) { - case LUA_TNONE: - case LUA_TLIGHTUSERDATA: - case LUA_TTABLE: - case LUA_TFUNCTION: - case LUA_TUSERDATA: - case LUA_TTHREAD: - LOG(level, "Function parameter was of type '" << - luaTypeToString(type) << "'"); - case LUA_TNIL: - break; - case LUA_TBOOLEAN: - LOG(level, lua_toboolean(L, -1)); - break; - case LUA_TNUMBER: - LOG(level, lua_tonumber(L, -1)); - break; - case LUA_TSTRING: - LOG(level, lua_tostring(L, -1)); - break; - } - return 0; - } - - /** - * \ingroup LuaScripts - * printDebug(*): - * Logs the passed value to the installed LogManager with a LogLevel of 'Debug'. - * For Boolean, numbers, and strings, the internal values are printed, for all other - * types, the type is printed instead - */ - int printDebug(lua_State* L) { - return printInternal(ghoul::logging::LogManager::LogLevel::Debug, L); - } - - /** - * \ingroup LuaScripts - * printInfo(*): - * Logs the passed value to the installed LogManager with a LogLevel of 'Info'. - * For Boolean, numbers, and strings, the internal values are printed, for all other - * types, the type is printed instead - */ - int printInfo(lua_State* L) { - return printInternal(ghoul::logging::LogManager::LogLevel::Info, L); - } - - /** - * \ingroup LuaScripts - * printWarning(*): - * Logs the passed value to the installed LogManager with a LogLevel of 'Warning'. - * For Boolean, numbers, and strings, the internal values are printed, for all other - * types, the type is printed instead - */ - int printWarning(lua_State* L) { - return printInternal(ghoul::logging::LogManager::LogLevel::Warning, L); - } - - /** - * \ingroup LuaScripts - * printError(*): - * Logs the passed value to the installed LogManager with a LogLevel of 'Error'. - * For Boolean, numbers, and strings, the internal values are printed, for all other - * types, the type is printed instead - */ - int printError(lua_State* L) { - return printInternal(ghoul::logging::LogManager::LogLevel::Error, L); - } - - /** - * \ingroup LuaScripts - * printFatal(*): - * Logs the passed value to the installed LogManager with a LogLevel of 'Fatal'. - * For Boolean, numbers, and strings, the internal values are printed, for all other - * types, the type is printed instead - */ - int printFatal(lua_State* L) { - return printInternal(ghoul::logging::LogManager::LogLevel::Fatal, L); - } - - /** - * \ingroup LuaScripts - * absPath(string): - * Passes the argument to FileSystem::absolutePath, which resolves occuring path - * tokens and returns the absolute path. - */ - int absolutePath(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %d arguments, got %d", 1, nArguments); - - std::string path = luaL_checkstring(L, -1); - path = absPath(path); - lua_pushstring(L, path.c_str()); - return 1; - } - - /** - * \ingroup LuaScripts - * setPathToken(string, string): - * Registers the path token provided by the first argument to the path in the second - * argument. If the path token already exists, it will be silently overridden. - */ - int setPathToken(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 2) - return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); - - std::string pathToken = luaL_checkstring(L, -1); - std::string path = luaL_checkstring(L, -2); - FileSys.registerPathToken(pathToken, path, true); - return 0; - } - -} // namespace luascriptfunctions - namespace scripting { namespace { diff --git a/src/scripting/scriptengine_lua.inl b/src/scripting/scriptengine_lua.inl new file mode 100644 index 0000000000..869b5efb37 --- /dev/null +++ b/src/scripting/scriptengine_lua.inl @@ -0,0 +1,153 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + + int printInternal(ghoul::logging::LogManager::LogLevel level, lua_State* L) { + using ghoul::lua::luaTypeToString; + const std::string _loggerCat = "print"; + + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + const int type = lua_type(L, -1); + switch (type) { + case LUA_TNONE: + case LUA_TLIGHTUSERDATA: + case LUA_TTABLE: + case LUA_TFUNCTION: + case LUA_TUSERDATA: + case LUA_TTHREAD: + LOG(level, "Function parameter was of type '" << + luaTypeToString(type) << "'"); + case LUA_TNIL: + break; + case LUA_TBOOLEAN: + LOG(level, lua_toboolean(L, -1)); + break; + case LUA_TNUMBER: + LOG(level, lua_tonumber(L, -1)); + break; + case LUA_TSTRING: + LOG(level, lua_tostring(L, -1)); + break; + } + return 0; + } + + /** + * \ingroup LuaScripts + * printDebug(*): + * Logs the passed value to the installed LogManager with a LogLevel of 'Debug'. + * For Boolean, numbers, and strings, the internal values are printed, for all other + * types, the type is printed instead + */ + int printDebug(lua_State* L) { + return printInternal(ghoul::logging::LogManager::LogLevel::Debug, L); + } + + /** + * \ingroup LuaScripts + * printInfo(*): + * Logs the passed value to the installed LogManager with a LogLevel of 'Info'. + * For Boolean, numbers, and strings, the internal values are printed, for all other + * types, the type is printed instead + */ + int printInfo(lua_State* L) { + return printInternal(ghoul::logging::LogManager::LogLevel::Info, L); + } + + /** + * \ingroup LuaScripts + * printWarning(*): + * Logs the passed value to the installed LogManager with a LogLevel of 'Warning'. + * For Boolean, numbers, and strings, the internal values are printed, for all other + * types, the type is printed instead + */ + int printWarning(lua_State* L) { + return printInternal(ghoul::logging::LogManager::LogLevel::Warning, L); + } + + /** + * \ingroup LuaScripts + * printError(*): + * Logs the passed value to the installed LogManager with a LogLevel of 'Error'. + * For Boolean, numbers, and strings, the internal values are printed, for all other + * types, the type is printed instead + */ + int printError(lua_State* L) { + return printInternal(ghoul::logging::LogManager::LogLevel::Error, L); + } + + /** + * \ingroup LuaScripts + * printFatal(*): + * Logs the passed value to the installed LogManager with a LogLevel of 'Fatal'. + * For Boolean, numbers, and strings, the internal values are printed, for all other + * types, the type is printed instead + */ + int printFatal(lua_State* L) { + return printInternal(ghoul::logging::LogManager::LogLevel::Fatal, L); + } + + /** + * \ingroup LuaScripts + * absPath(string): + * Passes the argument to FileSystem::absolutePath, which resolves occuring path + * tokens and returns the absolute path. + */ + int absolutePath(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %d arguments, got %d", 1, nArguments); + + std::string path = luaL_checkstring(L, -1); + path = absPath(path); + lua_pushstring(L, path.c_str()); + return 1; + } + + /** + * \ingroup LuaScripts + * setPathToken(string, string): + * Registers the path token provided by the first argument to the path in the second + * argument. If the path token already exists, it will be silently overridden. + */ + int setPathToken(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 2) + return luaL_error(L, "Expected %i arguments, got %i", 2, nArguments); + + std::string pathToken = luaL_checkstring(L, -1); + std::string path = luaL_checkstring(L, -2); + FileSys.registerPathToken(pathToken, path, true); + return 0; + } + +} // namespace luascriptfunctions + +} // namespace openspace diff --git a/src/util/time.cpp b/src/util/time.cpp index 4590c08064..8bbf95b81c 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -35,140 +35,14 @@ #include #include +#include "time_lua.inl" + namespace { const std::string _loggerCat = "Time"; } namespace openspace { -namespace luascriptfunctions { - -/** - * \ingroup LuaScripts - * setDeltaTime(number): - * Sets the delta time by calling the Time::setDeltaTime method - */ -int time_setDeltaTime(lua_State* L) { - const bool isFunction = (lua_isfunction(L, -1) != 0); - if (isFunction) { - // If the top of the stack is a function, it is ourself - const char* msg = lua_pushfstring(L, "method called without argument"); - return luaL_error(L, "bad argument (%s)", msg); - } - - const bool isNumber = (lua_isnumber(L, -1) != 0); - if (isNumber) { - double value = lua_tonumber(L, -1); - openspace::Time::ref().setDeltaTime(value); - return 0; - } - else { - const char* msg = lua_pushfstring(L, "%s expected, got %s", - lua_typename(L, LUA_TNUMBER), luaL_typename(L, -1)); - return luaL_error(L, "bad argument #%d (%s)", 1, msg); - } - -} - -/** - * \ingroup LuaScripts - * deltaTime(): - * Returns the delta time by calling the Time::deltaTime method - */ -int time_deltaTime(lua_State* L) { - lua_pushnumber(L, openspace::Time::ref().deltaTime()); - return 1; -} - -/** - * \ingroup LuaScripts - * togglePause(): - * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards - */ -int time_togglePause(lua_State* L) { - openspace::Time::ref().togglePause(); - return 0; -} - -/** - * \ingroup LuaScripts - * togglePause(): - * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards - */ -int time_setPause(lua_State* L) { - int nArguments = lua_gettop(L); - if (nArguments != 1) - return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); - - bool pause = lua_toboolean(L, -1) == 1; - openspace::Time::ref().setPause(pause); - return 0; -} - - -/** - * \ingroup LuaScripts - * setTime({number, string}): - * Sets the simulation time to the passed value. If the parameter is a number, it is - * interpreted as the number of seconds past the J2000 epoch and the - * Time::setTime(double) method is called. If the parameter is a string, it is - * interpreted as a structured date string and the Time::setTime(std::string) method - * is called - */ -int time_setTime(lua_State* L) { - const bool isFunction = (lua_isfunction(L, -1) != 0); - if (isFunction) { - // If the top of the stack is a function, it is ourself - const char* msg = lua_pushfstring(L, "method called without argument"); - return luaL_error(L, "bad argument (%s)", 1, msg); - } - - const bool isNumber = (lua_isnumber(L, -1) != 0); - const bool isString = (lua_isstring(L, -1) != 0); - if (!isNumber && !isString) { - const char* msg = lua_pushfstring(L, "%s or %s expected, got %s", - lua_typename(L, LUA_TNUMBER), - lua_typename(L, LUA_TSTRING), luaL_typename(L, -1)); - return luaL_error(L, "bad argument #%d (%s)", 1, msg); - } - if (isNumber) { - double value = lua_tonumber(L, -1); - openspace::Time::ref().setTime(value); - return 0; - } - if (isString) { - const char* time = lua_tostring(L, -1); - openspace::Time::ref().setTime(time); - return 0; - } - return 0; -} - -/** - * \ingroup LuaScripts - * currentTime(): - * Returns the current simulation time as the number of seconds past the J2000 epoch. - * It is returned by calling the Time::currentTime method. - */ -int time_currentTime(lua_State* L) { - lua_pushnumber(L, openspace::Time::ref().currentTime()); - return 1; -} - -/** - * \ingroup LuaScripts - * currentTimeUTC(): - * Returns the current simulation time as a structured ISO 8601 string using the UTC - * timezone by calling the Time::currentTimeUTC method - */ -int time_currentTimeUTC(lua_State* L) { - lua_pushstring(L, openspace::Time::ref().currentTimeUTC().c_str()); - return 1; -} - -} // namespace luascriptfunctions - - Time* Time::_instance = nullptr; Time::Time() diff --git a/src/util/time_lua.inl b/src/util/time_lua.inl new file mode 100644 index 0000000000..384da5bec9 --- /dev/null +++ b/src/util/time_lua.inl @@ -0,0 +1,154 @@ +/***************************************************************************************** + * * + * OpenSpace * + * * + * Copyright (c) 2014-2015 * + * * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this * + * software and associated documentation files (the "Software"), to deal in the Software * + * without restriction, including without limitation the rights to use, copy, modify, * + * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * + * permit persons to whom the Software is furnished to do so, subject to the following * + * conditions: * + * * + * The above copyright notice and this permission notice shall be included in all copies * + * or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * + * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * + * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF * + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * + * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * + ****************************************************************************************/ + +namespace openspace { + +namespace luascriptfunctions { + +/** + * \ingroup LuaScripts + * setDeltaTime(number): + * Sets the delta time by calling the Time::setDeltaTime method + */ +int time_setDeltaTime(lua_State* L) { + const bool isFunction = (lua_isfunction(L, -1) != 0); + if (isFunction) { + // If the top of the stack is a function, it is ourself + const char* msg = lua_pushfstring(L, "method called without argument"); + return luaL_error(L, "bad argument (%s)", msg); + } + + const bool isNumber = (lua_isnumber(L, -1) != 0); + if (isNumber) { + double value = lua_tonumber(L, -1); + openspace::Time::ref().setDeltaTime(value); + return 0; + } + else { + const char* msg = lua_pushfstring(L, "%s expected, got %s", + lua_typename(L, LUA_TNUMBER), luaL_typename(L, -1)); + return luaL_error(L, "bad argument #%d (%s)", 1, msg); + } + +} + +/** + * \ingroup LuaScripts + * deltaTime(): + * Returns the delta time by calling the Time::deltaTime method + */ +int time_deltaTime(lua_State* L) { + lua_pushnumber(L, openspace::Time::ref().deltaTime()); + return 1; +} + +/** + * \ingroup LuaScripts + * togglePause(): + * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards + */ +int time_togglePause(lua_State* L) { + openspace::Time::ref().togglePause(); + return 0; +} + +/** + * \ingroup LuaScripts + * togglePause(): + * Toggles a pause functionm i.e. setting the delta time to 0 and restoring it afterwards + */ +int time_setPause(lua_State* L) { + int nArguments = lua_gettop(L); + if (nArguments != 1) + return luaL_error(L, "Expected %i arguments, got %i", 1, nArguments); + + bool pause = lua_toboolean(L, -1) == 1; + openspace::Time::ref().setPause(pause); + return 0; +} + + +/** + * \ingroup LuaScripts + * setTime({number, string}): + * Sets the simulation time to the passed value. If the parameter is a number, it is + * interpreted as the number of seconds past the J2000 epoch and the + * Time::setTime(double) method is called. If the parameter is a string, it is + * interpreted as a structured date string and the Time::setTime(std::string) method + * is called + */ +int time_setTime(lua_State* L) { + const bool isFunction = (lua_isfunction(L, -1) != 0); + if (isFunction) { + // If the top of the stack is a function, it is ourself + const char* msg = lua_pushfstring(L, "method called without argument"); + return luaL_error(L, "bad argument (%s)", 1, msg); + } + + const bool isNumber = (lua_isnumber(L, -1) != 0); + const bool isString = (lua_isstring(L, -1) != 0); + if (!isNumber && !isString) { + const char* msg = lua_pushfstring(L, "%s or %s expected, got %s", + lua_typename(L, LUA_TNUMBER), + lua_typename(L, LUA_TSTRING), luaL_typename(L, -1)); + return luaL_error(L, "bad argument #%d (%s)", 1, msg); + } + if (isNumber) { + double value = lua_tonumber(L, -1); + openspace::Time::ref().setTime(value); + return 0; + } + if (isString) { + const char* time = lua_tostring(L, -1); + openspace::Time::ref().setTime(time); + return 0; + } + return 0; +} + +/** + * \ingroup LuaScripts + * currentTime(): + * Returns the current simulation time as the number of seconds past the J2000 epoch. + * It is returned by calling the Time::currentTime method. + */ +int time_currentTime(lua_State* L) { + lua_pushnumber(L, openspace::Time::ref().currentTime()); + return 1; +} + +/** + * \ingroup LuaScripts + * currentTimeUTC(): + * Returns the current simulation time as a structured ISO 8601 string using the UTC + * timezone by calling the Time::currentTimeUTC method + */ +int time_currentTimeUTC(lua_State* L) { + lua_pushstring(L, openspace::Time::ref().currentTimeUTC().c_str()); + return 1; +} + +} // namespace luascriptfunctions + +} // namespace openspace