summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSimon Robertshaw <simon@hardwired.org.uk>2011-06-03 12:58:48 (GMT)
committer Simon Robertshaw <simon@hardwired.org.uk>2011-06-03 12:58:48 (GMT)
commit3f072d9bc309be57141b0cc9a6c777bf883977cb (patch)
tree9d0e8260a44b6f1ec350898fe9a3e5e00b21ea45 /src
parente3ef7f05f00f67906ef75eaac518294ec7622b45 (diff)
downloadpowder-3f072d9bc309be57141b0cc9a6c777bf883977cb.zip
powder-3f072d9bc309be57141b0cc9a6c777bf883977cb.tar.gz
Better step function registration
Diffstat (limited to 'src')
-rw-r--r--src/luaconsole.c51
1 files changed, 41 insertions, 10 deletions
diff --git a/src/luaconsole.c b/src/luaconsole.c
index e4c541f..26978fa 100644
--- a/src/luaconsole.c
+++ b/src/luaconsole.c
@@ -4,7 +4,7 @@
#include <luaconsole.h>
lua_State *l;
-char *step_function = "";
+int step_functions[6] = {0, 0, 0, 0, 0, 0};
void luacon_open(){
const static struct luaL_reg tptluaapi [] = {
{"test", &luatpt_test},
@@ -29,6 +29,7 @@ void luacon_open(){
{"set_shortcuts", &luatpt_set_shortcuts},
{"delete", &luatpt_delete},
{"register_step", &luatpt_register_step},
+ {"unregister_step", &luatpt_unregister_step},
{NULL,NULL}
};
@@ -37,8 +38,8 @@ void luacon_open(){
luaL_openlib(l, "tpt", tptluaapi, 0);
}
int luacon_step(int mx, int my, int mb, int mbq, char key){
- int tempret = 0;
- if(step_function && step_function[0]){
+ int tempret = 0, tempb, i;
+ if(step_functions[0]){
//Set mouse globals
lua_pushinteger(l, mbq);
lua_pushinteger(l, mb);
@@ -48,13 +49,22 @@ int luacon_step(int mx, int my, int mb, int mbq, char key){
lua_setfield(l, LUA_GLOBALSINDEX, "mousey");
lua_setfield(l, LUA_GLOBALSINDEX, "mouseb");
lua_setfield(l, LUA_GLOBALSINDEX, "mousebq");
- lua_getfield(l, LUA_GLOBALSINDEX, step_function);
- lua_call(l, 0, 1);
- if(lua_isboolean(l, -1)){
- tempret = lua_toboolean(l, -1);
- lua_pop(l, 1);
- return tempret;
+ for(i = 0; i<6; i++){
+ if(step_functions[i]){
+ lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
+ lua_call(l, 0, 1);
+ if(lua_isboolean(l, -1)){
+ tempb = lua_toboolean(l, -1);
+ lua_pop(l, 1);
+ if(tempb){ //Mouse click has been handled, set the global for future calls
+ lua_pushinteger(l, mb);
+ lua_setfield(l, LUA_GLOBALSINDEX, "mouseb");
+ }
+ tempret |= tempb;
+ }
+ }
}
+ return tempret;
}
return 0;
}
@@ -582,7 +592,28 @@ int luatpt_delete(lua_State* l)
int luatpt_register_step(lua_State* l)
{
- step_function = luaL_optstring(l, 1, "");
+ int ref, i;
+ if(lua_isfunction(l, 1)){
+ for(i = 0; i<6; i++){
+ if(!step_functions[i]){
+ ref = luaL_ref(l, LUA_REGISTRYINDEX);
+ step_functions[i] = ref;
+ break;
+ } else { //Supposed to prevent the registration of 2 functions, but this isn't working TODO: FIX!
+ lua_rawgeti(l, LUA_REGISTRYINDEX, step_functions[i]);
+ if(lua_equal(l, 1, lua_gettop(l))){
+ lua_pushstring(l, "function already registered");
+ lua_error(l);
+ return -1;
+ }
+ }
+ }
+ }
+ return 0;
+}
+int luatpt_unregister_step(lua_State* l)
+{
+ //step_function = luaL_optstring(l, 1, "");
return 0;
}
#endif