-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Manage lua scripts and change the callback system, to prepare for #519
- Loading branch information
1 parent
fcf913e
commit 1149da9
Showing
12 changed files
with
445 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "Callbacks.h" | ||
#include "Main.h" | ||
#include "../Context.h" | ||
#include "NamedLuaFunction.h" | ||
#include "LuaFunctionList.h" | ||
|
||
#include <iostream> | ||
extern "C" { | ||
#include <lua.h> | ||
#include <lauxlib.h> | ||
} | ||
|
||
namespace Lua { | ||
|
||
static LuaFunctionList lfl; | ||
|
||
/* List of functions to register */ | ||
static const luaL_Reg callback_functions[] = | ||
{ | ||
{ "onStartup", Callbacks::onStartup}, | ||
{ "onInput", Callbacks::onInput}, | ||
{ "onFrame", Callbacks::onFrame}, | ||
{ "onPaint", Callbacks::onPaint}, | ||
{ NULL, NULL } | ||
}; | ||
|
||
void Callbacks::registerFunctions(lua_State *L) | ||
{ | ||
luaL_newlib(L, callback_functions); | ||
lua_setglobal(L, "callback"); | ||
} | ||
|
||
void Callbacks::clear() | ||
{ | ||
lfl.clear(); | ||
} | ||
|
||
int Callbacks::onStartup(lua_State *L) | ||
{ | ||
lfl.add(L, NamedLuaFunction::CallbackStartup); | ||
return 0; | ||
} | ||
|
||
int Callbacks::onInput(lua_State *L) | ||
{ | ||
lfl.add(L, NamedLuaFunction::CallbackInput); | ||
return 0; | ||
} | ||
|
||
int Callbacks::onFrame(lua_State *L) | ||
{ | ||
lfl.add(L, NamedLuaFunction::CallbackFrame); | ||
return 0; | ||
} | ||
|
||
int Callbacks::onPaint(lua_State *L) | ||
{ | ||
lfl.add(L, NamedLuaFunction::CallbackPaint); | ||
return 0; | ||
} | ||
|
||
void Callbacks::call(NamedLuaFunction::CallbackType type) | ||
{ | ||
lfl.call(type); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef LIBTAS_LUACALLBACKS_H_INCLUDED | ||
#define LIBTAS_LUACALLBACKS_H_INCLUDED | ||
|
||
#include "NamedLuaFunction.h" | ||
|
||
extern "C" { | ||
#include <lua.h> | ||
} | ||
|
||
namespace Lua { | ||
|
||
namespace Callbacks { | ||
|
||
/* Register all functions */ | ||
void registerFunctions(lua_State *L); | ||
|
||
void clear(); | ||
|
||
int onStartup(lua_State *L); | ||
|
||
int onInput(lua_State *L); | ||
|
||
int onFrame(lua_State *L); | ||
|
||
int onPaint(lua_State *L); | ||
|
||
void call(NamedLuaFunction::CallbackType type); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "LuaFunctionList.h" | ||
|
||
namespace Lua { | ||
|
||
void LuaFunctionList::add(lua_State *L, NamedLuaFunction::CallbackType t) | ||
{ | ||
functions.emplace_back(L, t); | ||
} | ||
|
||
void LuaFunctionList::removeForFile(const std::string& file) | ||
{ | ||
functions.remove_if([&file](const NamedLuaFunction& nlf){ return 0 == file.compare(nlf.file); }); | ||
} | ||
|
||
void LuaFunctionList::call(NamedLuaFunction::CallbackType c) | ||
{ | ||
for (auto& nlf : functions) { | ||
if (nlf.type == c) { | ||
nlf.call(); | ||
} | ||
} | ||
} | ||
|
||
void LuaFunctionList::clear() | ||
{ | ||
functions.clear(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright 2015-2020 Clément Gallet <clement.gallet@ens-lyon.org> | ||
This file is part of libTAS. | ||
libTAS is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
libTAS is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with libTAS. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef LIBTAS_LUAFUNCTIONLIST_H_INCLUDED | ||
#define LIBTAS_LUAFUNCTIONLIST_H_INCLUDED | ||
|
||
#include "NamedLuaFunction.h" | ||
|
||
extern "C" { | ||
#include <lua.h> | ||
} | ||
#include <list> | ||
#include <string> | ||
|
||
namespace Lua { | ||
|
||
class LuaFunctionList { | ||
|
||
public: | ||
/* Add a named function from lua stack */ | ||
void add(lua_State *L, NamedLuaFunction::CallbackType t); | ||
|
||
/* Remove all callbacks from a file */ | ||
void removeForFile(const std::string& file); | ||
|
||
/* Call all callbacks from a type */ | ||
void call(NamedLuaFunction::CallbackType c); | ||
|
||
/* Clear all callbacks */ | ||
void clear(); | ||
|
||
private: | ||
std::list<NamedLuaFunction> functions; | ||
|
||
}; | ||
} | ||
|
||
#endif |
Oops, something went wrong.