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.
|
|
|
|
*/
|
|
|
|
|
2014-01-05 11:00:50 +00:00
|
|
|
#include "StdAfx.h"
|
2011-07-05 13:41:05 +00:00
|
|
|
#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)
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::PushWide(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
|
|
|
|
2013-09-19 14:07:47 +00:00
|
|
|
const std::wstring section = LuaManager::ToWide(2);
|
|
|
|
const std::wstring defValue = LuaManager::ToWide(3);
|
|
|
|
const std::wstring& value =
|
|
|
|
parser.ReadString(self->GetName(), section.c_str(), defValue.c_str());
|
|
|
|
LuaManager::PushWide(value);
|
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
|
|
|
|
2013-08-06 17:43:57 +00:00
|
|
|
std::wstring strTmp = LuaManager::ToWide(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-10 15:19:33 +00:00
|
|
|
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);
|
2013-12-23 19:42:31 +00:00
|
|
|
bool percentual = lua_toboolean(L, 5) != 0;
|
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);
|
2013-08-06 17:43:57 +00:00
|
|
|
LuaManager::PushWide(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
|
|
|
}
|