distinguish between local and remote scripting

This commit is contained in:
Emil Axelsson
2016-09-16 14:53:20 +02:00
parent 41fafcb5df
commit 13610b390d
24 changed files with 419 additions and 273 deletions
+28 -5
View File
@@ -269,8 +269,10 @@ void InteractionHandler::keyboardCallback(Key key, KeyModifier modifier, KeyActi
// iterate over key bindings
auto ret = _keyLua.equal_range({ key, modifier });
for (auto it = ret.first; it != ret.second; ++it) {
//OsEng.scriptEngine()->runScript(it->second);
OsEng.scriptEngine().queueScript(it->second);
OsEng.scriptEngine().queueScript(it->second.first,
it->second.second ?
scripting::ScriptEngine::RemoteScripting::Yes :
scripting::ScriptEngine::RemoteScripting::No);
}
}
}
@@ -378,12 +380,20 @@ void InteractionHandler::resetKeyBindings() {
_keyLua.clear();
}
void InteractionHandler::bindKeyLocal(Key key, KeyModifier modifier, std::string lua) {
_keyLua.insert({
{ key, modifier },
std::make_pair(lua, false)
});
}
void InteractionHandler::bindKey(Key key, KeyModifier modifier, std::string lua) {
_keyLua.insert({
{ key, modifier },
lua
std::make_pair(lua, true)
});
}
void InteractionHandler::writeKeyboardDocumentation(const std::string& type, const std::string& file)
{
@@ -391,8 +401,14 @@ void InteractionHandler::writeKeyboardDocumentation(const std::string& type, con
std::ofstream f(absPath(file));
for (const auto& p : _keyLua) {
std::string remoteScriptingInfo;
bool remoteScripting = p.second.second;
if (!remoteScripting) {
remoteScriptingInfo = " (LOCAL)";
}
f << std::to_string(p.first) << ": " <<
p.second << std::endl;
p.second.first << remoteScriptingInfo << std::endl;
}
}
else {
@@ -417,7 +433,14 @@ scripting::LuaLibrary InteractionHandler::luaLibrary() {
"bindKey",
&luascriptfunctions::bindKey,
"string, string",
"Binds a key by name to a lua string command"
"Binds a key by name to a lua string command to execute both locally "
"and to broadcast to clients if this is the host of a parallel session"
},
{
"bindKeyLocal",
&luascriptfunctions::bindKeyLocal,
"string, string",
"Binds a key by name to a lua string command to execute only locally"
},
{
"setInteractionMode",
+39 -114
View File
@@ -60,7 +60,9 @@ int setOrigin(lua_State* L) {
/**
* \ingroup LuaScripts
* bindKey():
* Binds a key to Lua command
* Binds a key to Lua command to both execute locally
* and broadcast to all clients if this node is hosting
* a parallel connection.
*/
int bindKey(lua_State* L) {
using ghoul::lua::luaTypeToString;
@@ -93,6 +95,42 @@ int bindKey(lua_State* L) {
return 0;
}
/**
* \ingroup LuaScripts
* bindKey():
* Binds a key to Lua command to execute only locally
*/
int bindKeyLocal(lua_State* L) {
using ghoul::lua::luaTypeToString;
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");
openspace::KeyWithModifier iKey = openspace::stringToKey(key);
if (iKey.key == openspace::Key::Unknown) {
LERRORC("lua.bindKey", "Could not find key '" << key << "'");
return 0;
}
OsEng.interactionHandler().bindKeyLocal(
iKey.key,
iKey.modifier,
command
);
return 0;
}
/**
* \ingroup LuaScripts
* clearKeys():
@@ -176,120 +214,7 @@ int resetCameraDirection(lua_State* L) {
OsEng.interactionHandler().resetCameraDirection();
}
#ifdef USE_OLD_INTERACTIONHANDLER
/**
* \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<float>(d1), static_cast<float>(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<float>(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;
}
#endif USE_OLD_INTERACTIONHANDLER
} // namespace luascriptfunctions
} // namespace openspace
+44 -4
View File
@@ -58,6 +58,7 @@ LuaConsole::LuaConsole()
, _filename("")
, _autoCompleteInfo({NoAutoComplete, false, ""})
, _isVisible(false)
, _remoteScripting(true)
{
// _commands.push_back("");
// _activeCommand = _commands.size() - 1;
@@ -89,6 +90,13 @@ void LuaConsole::initialize() {
}
_commands.push_back("");
_activeCommand = _commands.size() - 1;
OsEng.parallelConnection().connectionEvent()->subscribe("luaConsole",
"statusChanged", [this]() {
network::Status status = OsEng.parallelConnection().status();
parallelConnectionChanged(status);
});
}
void LuaConsole::deinitialize() {
@@ -102,6 +110,8 @@ void LuaConsole::deinitialize() {
file.write(s.c_str(), length);
}
}
OsEng.parallelConnection().connectionEvent()->unsubscribe("luaConsole");
}
void LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction action) {
@@ -169,7 +179,8 @@ void LuaConsole::keyboardCallback(Key key, KeyModifier modifier, KeyAction actio
else {
std::string cmd = _commands.at(_activeCommand);
if (cmd != "") {
OsEng.scriptEngine().queueScript(cmd);
OsEng.scriptEngine().queueScript(cmd,
_remoteScripting ? scripting::ScriptEngine::RemoteScripting::Yes : scripting::ScriptEngine::RemoteScripting::No);
// Only add the current command to the history if it hasn't been
// executed before. We don't want two of the same commands in a row
@@ -309,13 +320,28 @@ void LuaConsole::render() {
startY = startY - font_size * 15.0f * 2.0f;
const glm::vec4 red(1, 0, 0, 1);
const glm::vec4 lightBlue(0.4, 0.4, 1, 1);
const glm::vec4 green(0, 1, 0, 1);
const glm::vec4 white(1, 1, 1, 1);
std::shared_ptr<ghoul::fontrendering::Font> font = OsEng.fontManager().font("Mono", font_size);
using ghoul::fontrendering::RenderFont;
RenderFont(*font, glm::vec2(15.f, startY), red, "$");
if (_remoteScripting) {
int nClients = OsEng.parallelConnection().nConnections() - 1;
if (nClients == 1) {
RenderFont(*font, glm::vec2(15.f, startY + 20.0f), red, "Broadcasting script to 1 client");
} else {
RenderFont(*font, glm::vec2(15.f, startY + 20.0f), red, ("Broadcasting script to " + std::to_string(nClients) + " clients").c_str());
}
RenderFont(*font, glm::vec2(15.f, startY), red, "$");
}
else {
if (OsEng.parallelConnection().isHost()) {
RenderFont(*font, glm::vec2(15.f, startY + 20.0f), lightBlue, "Local script execution");
}
RenderFont(*font, glm::vec2(15.f, startY), lightBlue, "$");
}
RenderFont(*font, glm::vec2(15.f + font_size, startY), white, "%s", _commands.at(_activeCommand).c_str());
size_t n = std::count(_commands.at(_activeCommand).begin(), _commands.at(_activeCommand).begin() + _inputPosition, '\n');
@@ -390,10 +416,24 @@ void LuaConsole::setVisible(bool visible) {
_isVisible = visible;
}
void LuaConsole::toggleVisibility() {
_isVisible = !_isVisible;
void LuaConsole::toggleMode() {
if (_isVisible) {
if (_remoteScripting) {
_remoteScripting = false;
} else {
_isVisible = false;
}
} else {
_remoteScripting = OsEng.parallelConnection().isHost();
_isVisible = true;
}
}
void LuaConsole::parallelConnectionChanged(const network::Status& status) {
_remoteScripting = status == network::Status::Host;
}
scripting::LuaLibrary LuaConsole::luaLibrary() {
return {
"console",
+1 -1
View File
@@ -64,7 +64,7 @@ int toggle(lua_State* L) {
if (nArguments != 0)
return luaL_error(L, "Expected %i arguments, got %i", 0, nArguments);
OsEng.console().toggleVisibility();
OsEng.console().toggleMode();
return 0;
}