2012-01-23 06:36:15 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Matt King, Birunthan Mohanathas
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
#include "../../StdAfx.h"
|
|
|
|
#include "../LuaManager.h"
|
|
|
|
#include "../../Measure.h"
|
2011-07-27 10:42:35 +00:00
|
|
|
#include "../../MeterWindow.h"
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-09-02 07:26:09 +00:00
|
|
|
#define DECLARE_SELF(L) \
|
|
|
|
void* selfData = lua_touserdata(L, 1); \
|
|
|
|
if (!selfData) return 0; \
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure* self = *(Measure**)selfData;
|
2012-03-09 10:28:25 +00:00
|
|
|
|
|
|
|
static int GetName(lua_State* L)
|
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
LuaManager::PushWide(L, self->GetName());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetOption(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2013-05-31 14:18:52 +00:00
|
|
|
MeterWindow* meterWindow = self->GetMeterWindow();
|
|
|
|
ConfigParser& parser = meterWindow->GetParser();
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-03-08 15:42:46 +00:00
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
|
|
|
strTmp = parser.ReadString(self->GetName(), strTmp.c_str(), LuaManager::ToWide(L, 3).c_str());
|
|
|
|
|
2013-01-27 10:17:38 +00:00
|
|
|
LuaManager::PushWide(L, strTmp);
|
2012-03-08 15:42:46 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-11 09:59:48 +00:00
|
|
|
static int GetNumberOption(lua_State* L)
|
2012-03-08 15:42:46 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2013-05-31 14:18:52 +00:00
|
|
|
MeterWindow* meterWindow = self->GetMeterWindow();
|
|
|
|
ConfigParser& parser = meterWindow->GetParser();
|
2012-03-08 15:42:46 +00:00
|
|
|
|
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
2012-04-06 12:16:54 +00:00
|
|
|
double value = parser.ReadFloat(self->GetName(), strTmp.c_str(), lua_tonumber(L, 3));
|
2012-03-08 15:46:36 +00:00
|
|
|
|
2012-03-08 15:42:46 +00:00
|
|
|
lua_pushnumber(L, value);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int Disable(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2011-07-05 13:41:05 +00:00
|
|
|
self->Disable();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int Enable(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2011-07-05 13:41:05 +00:00
|
|
|
self->Enable();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetValue(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
lua_pushnumber(L, self->GetValue());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetRelativeValue(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
lua_pushnumber(L, self->GetRelativeValue());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetValueRange(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
lua_pushnumber(L, self->GetValueRange());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetMinValue(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
lua_pushnumber(L, self->GetMinValue());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetMaxValue(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:41:46 +00:00
|
|
|
lua_pushnumber(L, self->GetMaxValue());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetStringValue(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
|
2013-06-09 20:15:43 +00:00
|
|
|
AUTOSCALE autoScale = AUTOSCALE_OFF;
|
|
|
|
double scale = 1.0;
|
|
|
|
int decimals = 0;
|
|
|
|
bool percentual = false;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2013-04-10 15:59:41 +00:00
|
|
|
const WCHAR* val = self->GetStringOrFormattedValue(autoScale, scale, decimals, percentual);
|
2011-09-03 16:45:29 +00:00
|
|
|
LuaManager::PushWide(L, val);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LuaManager::RegisterMeasure(lua_State* L)
|
|
|
|
{
|
2012-03-09 13:19:49 +00:00
|
|
|
const luaL_Reg functions[] =
|
2012-03-09 10:28:25 +00:00
|
|
|
{
|
|
|
|
{ "GetName", GetName },
|
|
|
|
{ "GetOption", GetOption },
|
2012-03-11 09:59:48 +00:00
|
|
|
{ "GetNumberOption", GetNumberOption },
|
2012-03-09 10:28:25 +00:00
|
|
|
{ "Disable", Disable },
|
|
|
|
{ "Enable", Enable },
|
|
|
|
{ "GetValue", GetValue },
|
|
|
|
{ "GetRelativeValue", GetRelativeValue },
|
|
|
|
{ "GetValueRange", GetValueRange },
|
|
|
|
{ "GetMinValue", GetMinValue },
|
|
|
|
{ "GetMaxValue", GetMaxValue },
|
|
|
|
{ "GetStringValue", GetStringValue },
|
2013-05-31 14:28:39 +00:00
|
|
|
{ nullptr, nullptr }
|
2012-03-09 10:28:25 +00:00
|
|
|
};
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
luaL_register(L, "Measure", functions);
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
2012-03-10 07:03:19 +00:00
|
|
|
lua_pop(L, 1);
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|