Script: Extend Measure:GetStringValue to support a parameter table

Example usage: GetStringValue({AutoScale = 0, Scale = 1.0, NumOfDecimals = 0, Percentual = false})
All keys are optional.
This commit is contained in:
Birunthan Mohanathas 2013-06-09 23:15:43 +03:00
parent 72bf8dc317
commit b0d101ed71

View File

@ -120,11 +120,59 @@ static int GetStringValue(lua_State* L)
{ {
DECLARE_SELF(L) DECLARE_SELF(L)
int top = lua_gettop(L); AUTOSCALE autoScale = AUTOSCALE_OFF;
AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF; double scale = 1.0;
double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0; int decimals = 0;
int decimals = (int)lua_tonumber(L, 4); bool percentual = false;
bool percentual = lua_toboolean(L, 5);
const int top = lua_gettop(L);
if (top == 2 && lua_istable(L, 2))
{
lua_pushvalue(L, 2);
lua_pushnil(L);
// Stack: nil, table, ...
while (lua_next(L, -2))
{
// Stack: value, key, table, ...
// Push copy of key to avoid changing orignal key.
lua_pushvalue(L, -2);
// Stack: key, value, key, table, ...
const char* key = lua_tostring(L, -1);
if (strcmp(key, "AutoScale") == 0)
{
// TODO: Implement something more reliable than casting.
autoScale = (AUTOSCALE)(int)lua_tonumber(L, -2);
}
else if (strcmp(key, "Scale") == 0)
{
scale = lua_tonumber(L, -2);
}
else if (strcmp(key, "NumOfDecimals") == 0)
{
decimals = (int)lua_tonumber(L, -2);
}
else if (strcmp(key, "Percentual") == 0)
{
percentual = lua_toboolean(L, -2);
}
lua_pop(L, 2);
// Stack: key, table, ...
}
// Stack: table, ...
lua_pop(L, 1);
}
else
{
// For backwards compatibility.
if (top > 1) autoScale = (AUTOSCALE)(int)lua_tonumber(L, 2);
if (top > 2) scale = lua_tonumber(L, 3);
if (top > 3) decimals = (int)lua_tonumber(L, 4);
if (top > 4) percentual = lua_toboolean(L, 5);
}
const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual); const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual);
LuaManager::PushWide(L, val); LuaManager::PushWide(L, val);