Browse Source

Add functions to pushing indices to Lua and getting indices from Lua (same as getting/pushing integers but handles changing between 0 and 1 based indexing).

Erik Crevel 4 years ago
parent
commit
b8cd4d3937
2 changed files with 19 additions and 0 deletions
  1. 8 0
      Include/RmlUi/Lua/Utilities.h
  2. 11 0
      Source/Lua/Utilities.cpp

+ 8 - 0
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 */
 /** Populate the variant based on the Lua value at the given index */
 void RMLUILUA_API GetVariant(lua_State* L, int index, Variant* variant);
 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
 //Helper function, so that the types don't have to define individual functions themselves
 // to fill the Elements.As table
 // to fill the Elements.As table
 template<typename ToType>
 template<typename ToType>

+ 11 - 0
Source/Lua/Utilities.cpp

@@ -113,5 +113,16 @@ void GetVariant(lua_State* L, int index, Variant* variant)
 		break;
 		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 Lua
 } // namespace Rml
 } // namespace Rml