diff options
| author | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-29 21:04:07 (GMT) |
|---|---|---|
| committer | Simon Robertshaw <simon@hardwired.org.uk> | 2012-08-29 21:04:07 (GMT) |
| commit | 58dc2559aad5abb3ae23f89155a4cdf37e0de885 (patch) | |
| tree | a7a452483b8875f3a95145578d7d45c36d53b52d /src/cat/LuaScriptInterface.cpp | |
| parent | 3e78f64da8265816e973c6047fc9985193311e31 (diff) | |
| download | powder-58dc2559aad5abb3ae23f89155a4cdf37e0de885.zip powder-58dc2559aad5abb3ae23f89155a4cdf37e0de885.tar.gz | |
Implement retrieval of element properties
Diffstat (limited to 'src/cat/LuaScriptInterface.cpp')
| -rw-r--r-- | src/cat/LuaScriptInterface.cpp | 78 |
1 files changed, 74 insertions, 4 deletions
diff --git a/src/cat/LuaScriptInterface.cpp b/src/cat/LuaScriptInterface.cpp index 50e2735..8a01fc0 100644 --- a/src/cat/LuaScriptInterface.cpp +++ b/src/cat/LuaScriptInterface.cpp @@ -409,6 +409,7 @@ void LuaScriptInterface::initElementsAPI() //Methods struct luaL_reg elementsAPIMethods [] = { {"allocate", elements_allocate}, + {"element", elements_element}, {"free", elements_free}, {NULL, NULL} }; @@ -501,6 +502,66 @@ int LuaScriptInterface::elements_allocate(lua_State * l) return 1; } +int LuaScriptInterface::elements_element(lua_State * l) +{ + int args = lua_gettop(l); + int id; + luaL_checktype(l, 1, LUA_TNUMBER); + id = lua_tointeger(l, 1); + + if(id < 0 || id >= PT_NUM || !luacon_sim->elements[id].Enabled) + return luaL_error(l, "Invalid element"); + + if(args > 1) + { + //Write values from fields in the table + return 0; + } + else + { + std::vector<StructProperty> properties = Element::GetProperties(); + //Write values from native data to a table + lua_newtable(l); + for(std::vector<StructProperty>::iterator iter = properties.begin(), end = properties.end(); iter != end; ++iter) + { + intptr_t offset = (*iter).Offset; + switch((*iter).Type) + { + case StructProperty::ParticleType: + case StructProperty::Integer: + lua_pushinteger(l, *((int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::UInteger: + lua_pushinteger(l, *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Float: + lua_pushnumber(l, *((float*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Char: + lua_pushinteger(l, *((char*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::UChar: + lua_pushinteger(l, *((unsigned char*)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::String: + lua_pushstring(l, *((char**)(((unsigned char*)&luacon_sim->elements[id])+offset))); + break; + case StructProperty::Colour: +#if PIXELSIZE == 4 + lua_pushinteger(l, *((unsigned int*)(((unsigned char*)&luacon_sim->elements[id])+offset))); +#else + lua_pushinteger(l, *((unsigned short*)(((unsigned char*)&luacon_sim->elements[id])+offset))); +#endif + break; + default: + lua_pushnil(l); + } + lua_setfield(l, -2, (*iter).Name.c_str()); + } + return 1; + } +} + int LuaScriptInterface::elements_free(lua_State * l) { int id; @@ -1417,10 +1478,19 @@ int luatpt_setconsole(lua_State* l) int luatpt_log(lua_State* l) { - if((*luacon_currentCommand) && !(*luacon_lastError).length()) - (*luacon_lastError) = luaL_optstring(l, 1, ""); - else - luacon_ci->Log(CommandInterface::LogNotice, luaL_optstring(l, 1, "")); + int args = lua_gettop(l); + for(int i = 1; i <= args; i++) + { + if((*luacon_currentCommand)) + { + if(!(*luacon_lastError).length()) + (*luacon_lastError) = luaL_optstring(l, i, ""); + else + (*luacon_lastError) += ", " + std::string(luaL_optstring(l, i, "")); + } + else + luacon_ci->Log(CommandInterface::LogNotice, luaL_optstring(l, i, "")); + } return 0; } |
