]> git.sesse.net Git - nageru/blobdiff - theme.cpp
Redo get_width/get_height to not work on a LiveInputWrapper, since we do not really...
[nageru] / theme.cpp
index 62e3640f509f2844df951df5db2b4e7476a8af1d..aed350f0973ac50b81307f99b128797d448351d0 100644 (file)
--- a/theme.cpp
+++ b/theme.cpp
@@ -90,6 +90,15 @@ Effect *get_effect(lua_State *L, int idx)
        return nullptr;
 }
 
+InputState *get_input_state(lua_State *L, int idx)
+{
+       if (luaL_testudata(L, idx, "InputState")) {
+               return (InputState *)lua_touserdata(L, idx);
+       }
+       luaL_error(L, "Error: Index #%d was not InputState\n", idx);
+       return nullptr;
+}
+
 bool checkbool(lua_State* L, int idx)
 {
        luaL_checktype(L, idx, LUA_TBOOLEAN);
@@ -259,6 +268,36 @@ int MixEffect_new(lua_State* L)
        return wrap_lua_object<MixEffect>(L, "MixEffect");
 }
 
+int InputState_gc(lua_State* L)
+{
+       assert(lua_gettop(L) == 1);
+       InputState *input_state = get_input_state(L, 1);
+       input_state->~InputState();
+       return 0;
+}
+
+int InputState_get_width(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputState *input_state = (InputState *)lua_touserdata(L, 1);
+       int signal_num = luaL_checknumber(L, 2);
+       BufferedFrame frame = input_state->buffered_frames[signal_num][0];
+       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+       lua_pushnumber(L, userdata->last_width[frame.field_number]);
+       return 1;
+}
+
+int InputState_get_height(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputState *input_state = (InputState *)lua_touserdata(L, 1);
+       int signal_num = luaL_checknumber(L, 2);
+       BufferedFrame frame = input_state->buffered_frames[signal_num][0];
+       const PBOFrameAllocator::Userdata *userdata = (const PBOFrameAllocator::Userdata *)frame.frame->userdata;
+       lua_pushnumber(L, userdata->last_height[frame.field_number]);
+       return 1;
+}
+
 int Effect_set_float(lua_State *L)
 {
        assert(lua_gettop(L) == 3);
@@ -401,6 +440,13 @@ const luaL_Reg MixEffect_funcs[] = {
        { NULL, NULL }
 };
 
+const luaL_Reg InputState_funcs[] = {
+       { "__gc", InputState_gc },
+       { "get_width", InputState_get_width },
+       { "get_height", InputState_get_height },
+       { NULL, NULL }
+};
+
 }  // namespace
 
 LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool override_bounce)
@@ -443,6 +489,7 @@ LiveInputWrapper::LiveInputWrapper(Theme *theme, EffectChain *chain, bool overri
 void LiveInputWrapper::connect_signal(int signal_num)
 {
        if (global_mixer == nullptr) {
+               // No data yet.
                return;
        }
 
@@ -473,6 +520,7 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
        register_class("OverlayEffect", OverlayEffect_funcs);
        register_class("ResizeEffect", ResizeEffect_funcs);
        register_class("MixEffect", MixEffect_funcs);
+       register_class("InputState", InputState_funcs);
 
        // Run script.
        lua_settop(L, 0);
@@ -499,12 +547,12 @@ Theme::Theme(const char *filename, ResourcePool *resource_pool, unsigned num_car
 void Theme::register_class(const char *class_name, const luaL_Reg *funcs)
 {
        assert(lua_gettop(L) == 0);
-       luaL_newmetatable(L, class_name);
+       luaL_newmetatable(L, class_name);  // mt = {}
        lua_pushlightuserdata(L, this);
-       luaL_setfuncs(L, funcs, 1);
+       luaL_setfuncs(L, funcs, 1);        // for (name,f in funcs) { mt[name] = f, with upvalue {theme} }
        lua_pushvalue(L, -1);
-       lua_setfield(L, -2, "__index");
-       lua_setglobal(L, class_name);
+       lua_setfield(L, -2, "__index");    // mt.__index = mt
+       lua_setglobal(L, class_name);      // ClassName = mt
        assert(lua_gettop(L) == 0);
 }
 
@@ -519,8 +567,9 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he
        lua_pushnumber(L, t);
        lua_pushnumber(L, width);
        lua_pushnumber(L, height);
+       wrap_lua_object<InputState>(L, "InputState", input_state);
 
-       if (lua_pcall(L, 4, 2, 0) != 0) {
+       if (lua_pcall(L, 5, 2, 0) != 0) {
                fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1));
                exit(1);
        }