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-03-09 10:28:25 +00:00
|
|
|
inline CMeasure* GetSelf(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-03-09 10:28:25 +00:00
|
|
|
return *(CMeasure**)lua_touserdata(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetName(lua_State* L)
|
|
|
|
{
|
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(L);
|
2011-07-27 10:42:35 +00:00
|
|
|
CMeterWindow* meterWindow = self->GetMeterWindow();
|
|
|
|
CConfigParser& 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());
|
|
|
|
|
|
|
|
LuaManager::PushWide(L, strTmp.c_str());
|
|
|
|
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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(L);
|
2012-03-08 15:42:46 +00:00
|
|
|
CMeterWindow* meterWindow = self->GetMeterWindow();
|
|
|
|
CConfigParser& parser = meterWindow->GetParser();
|
|
|
|
|
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
2012-03-09 10:28:25 +00:00
|
|
|
double value = parser.ReadFormula(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(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-03-09 10:28:25 +00:00
|
|
|
CMeasure* self = GetSelf(L);
|
|
|
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
AUTOSCALE autoScale = (top > 1) ? (AUTOSCALE)(int)lua_tonumber(L, 2) : AUTOSCALE_OFF;
|
|
|
|
double scale = (top > 2) ? lua_tonumber(L, 3) : 1.0;
|
|
|
|
int decimals = (int)lua_tonumber(L, 4);
|
|
|
|
bool percentual = lua_toboolean(L, 5);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
const WCHAR* val = self->GetStringValue(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 },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
luaL_register(L, "CMeasure", functions);
|
|
|
|
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
|
|
|
}
|