diff --git a/Include/RmlUi/Lua/Utilities.h b/Include/RmlUi/Lua/Utilities.h index 5487cc3d6..cbecc920f 100644 --- a/Include/RmlUi/Lua/Utilities.h +++ b/Include/RmlUi/Lua/Utilities.h @@ -46,6 +46,14 @@ void RMLUILUA_API PushVariant(lua_State* L, const Variant* var); /** Populate the variant based on the Lua value at the given index */ void RMLUILUA_API GetVariant(lua_State* L, int index, Variant* variant); +// Converts index from 0-based to 1-based before pushing it to the stack +void RMLUILUA_API PushIndex(lua_State* L, int index); + +// Returns 0-based index after retrieving from stack and converting from 1-based +// The index parameter refers to the position on the stack and is not affected +// by the conversion +int RMLUILUA_API GetIndex(lua_State* L, int index); + //Helper function, so that the types don't have to define individual functions themselves // to fill the Elements.As table template diff --git a/Source/Lua/Element.cpp b/Source/Lua/Element.cpp index e3829a374..1fc92bee4 100644 --- a/Source/Lua/Element.cpp +++ b/Source/Lua/Element.cpp @@ -182,7 +182,7 @@ int ElementGetElementsByTagName(lua_State* L, Element* obj) lua_newtable(L); for(unsigned int i = 0; i < list.size(); i++) { - lua_pushinteger(L,i); + PushIndex(L,i); LuaType::push(L,list[i],false); lua_settable(L,-3); //-3 is the table } diff --git a/Source/Lua/Elements/ElementFormControlSelect.cpp b/Source/Lua/Elements/ElementFormControlSelect.cpp index 90a8a0b5a..87dc3b1d6 100644 --- a/Source/Lua/Elements/ElementFormControlSelect.cpp +++ b/Source/Lua/Elements/ElementFormControlSelect.cpp @@ -45,7 +45,7 @@ int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj) const char* value = luaL_checkstring(L,2); int before = -1; //default if(lua_gettop(L) >= 3) - before = (int)luaL_checkinteger(L,3); + before = GetIndex(L,3); int index = obj->Add(rml,value,before); lua_pushinteger(L,index); @@ -54,7 +54,7 @@ int ElementFormControlSelectAdd(lua_State* L, ElementFormControlSelect* obj) int ElementFormControlSelectRemove(lua_State* L, ElementFormControlSelect* obj) { - int index = (int)luaL_checkinteger(L,1); + int index = GetIndex(L,1); obj->Remove(index); return 0; } @@ -81,7 +81,7 @@ int ElementFormControlSelectGetAttrselection(lua_State* L) ElementFormControlSelect* obj = LuaType::check(L,1); RMLUI_CHECK_OBJ(obj); int selection = obj->GetSelection(); - lua_pushinteger(L,selection); + PushIndex(L,selection); return 1; } @@ -91,7 +91,7 @@ int ElementFormControlSelectSetAttrselection(lua_State* L) { ElementFormControlSelect* obj = LuaType::check(L,1); RMLUI_CHECK_OBJ(obj); - int selection = (int)luaL_checkinteger(L,2); + int selection = GetIndex(L,2); obj->SetSelection(selection); return 0; } diff --git a/Source/Lua/Elements/ElementTabSet.cpp b/Source/Lua/Elements/ElementTabSet.cpp index b4f5a903f..60b8f5aaa 100644 --- a/Source/Lua/Elements/ElementTabSet.cpp +++ b/Source/Lua/Elements/ElementTabSet.cpp @@ -38,7 +38,7 @@ namespace Lua { int ElementTabSetSetPanel(lua_State* L, ElementTabSet* obj) { RMLUI_CHECK_OBJ(obj); - int index = (int)luaL_checkinteger(L,1); + int index = GetIndex(L,1); const char* rml = luaL_checkstring(L,2); obj->SetPanel(index,rml); @@ -48,7 +48,7 @@ int ElementTabSetSetPanel(lua_State* L, ElementTabSet* obj) int ElementTabSetSetTab(lua_State* L, ElementTabSet* obj) { RMLUI_CHECK_OBJ(obj); - int index = (int)luaL_checkinteger(L,1); + int index = GetIndex(L,1); const char* rml = luaL_checkstring(L,2); obj->SetTab(index,rml); @@ -62,7 +62,7 @@ int ElementTabSetGetAttractive_tab(lua_State* L) ElementTabSet* obj = LuaType::check(L,1); RMLUI_CHECK_OBJ(obj); int tab = obj->GetActiveTab(); - lua_pushinteger(L,tab); + PushIndex(L,tab); return 1; } @@ -81,7 +81,7 @@ int ElementTabSetSetAttractive_tab(lua_State* L) { ElementTabSet* obj = LuaType::check(L,1); RMLUI_CHECK_OBJ(obj); - int tab = (int)luaL_checkinteger(L,2); + int tab = GetIndex(L,2); obj->SetActiveTab(tab); return 0; } diff --git a/Source/Lua/EventParametersProxy.cpp b/Source/Lua/EventParametersProxy.cpp index 0ee27e6fd..1c1daeaec 100644 --- a/Source/Lua/EventParametersProxy.cpp +++ b/Source/Lua/EventParametersProxy.cpp @@ -31,6 +31,7 @@ #include #include #include "Pairs.h" +#include namespace Rml { @@ -55,7 +56,15 @@ int EventParametersProxy__index(lua_State* L) const char* key = lua_tostring(L,2); auto it = obj->owner->GetParameters().find(key); const Variant* param = (it == obj->owner->GetParameters().end() ? nullptr : &it->second); - PushVariant(L,param); + if (obj->owner->GetId() == EventId::Tabchange && + std::strcmp(key, "tab_index") == 0 && + param && + param->GetType() == Variant::Type::INT) + { + PushIndex(L,param->Get()); + } + else + PushVariant(L,param); return 1; } else diff --git a/Source/Lua/Utilities.cpp b/Source/Lua/Utilities.cpp index 969cdfcfd..d3f8d9073 100644 --- a/Source/Lua/Utilities.cpp +++ b/Source/Lua/Utilities.cpp @@ -113,5 +113,16 @@ void GetVariant(lua_State* L, int index, Variant* variant) break; } } + +void PushIndex(lua_State* L, int index) +{ + lua_pushinteger(L, index + 1); +} + +int GetIndex(lua_State* L, int index) +{ + return (int)luaL_checkinteger(L, index) - 1; +} + } // namespace Lua } // namespace Rml