2010-12-12 19:27:56 +00:00
|
|
|
#include "StdAfx.h"
|
2010-12-12 17:08:36 +00:00
|
|
|
#include "MeasureScript.h"
|
|
|
|
#include "lua/LuaManager.h"
|
|
|
|
#include "Litestep.h"
|
|
|
|
#include "Rainmeter.h"
|
|
|
|
|
|
|
|
const char* g_strInitFunction = "Initialize";
|
|
|
|
const char* g_strUpdateFunction = "Update";
|
|
|
|
const char* g_strGetValueFunction = "GetValue";
|
|
|
|
const char* g_strGetStringValueFunction = "GetStringValue";
|
|
|
|
|
2011-01-29 00:11:01 +00:00
|
|
|
CMeasureScript::CMeasureScript(CMeterWindow* meterWindow) : CMeasure(meterWindow),
|
|
|
|
m_pLuaScript(),
|
|
|
|
m_bUpdateDefined(false),
|
|
|
|
m_bGetValueDefined(false),
|
|
|
|
m_bGetStringValueDefined(false),
|
|
|
|
m_bInitializeDefined(false)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
LuaManager::Init();
|
|
|
|
}
|
|
|
|
|
|
|
|
CMeasureScript::~CMeasureScript()
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
|
|
|
LuaManager::CleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureScript::DeleteLuaScript()
|
|
|
|
{
|
|
|
|
if (m_pLuaScript)
|
|
|
|
{
|
|
|
|
delete m_pLuaScript;
|
|
|
|
m_pLuaScript = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_bUpdateDefined = false;
|
|
|
|
m_bGetValueDefined = false;
|
|
|
|
m_bGetStringValueDefined = false;
|
|
|
|
m_bInitializeDefined = false;
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureScript::Initialize()
|
|
|
|
{
|
|
|
|
CMeasure::Initialize();
|
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_bInitializeDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
m_pLuaScript->RunFunction(g_strInitFunction);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMeasureScript::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_bUpdateDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
m_pLuaScript->RunFunction(g_strUpdateFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PostUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
double CMeasureScript::GetValue()
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_bGetValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
|
|
|
return m_pLuaScript->RunFunctionDouble(g_strGetValueFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMeasure::GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureScript::SetValue(double d)
|
|
|
|
{
|
|
|
|
m_Value = d;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GetStringValue
|
|
|
|
**
|
|
|
|
** This method returns the value as text string. The actual value is
|
|
|
|
** get with GetValue() so we don't have to worry about m_Invert.
|
|
|
|
**
|
|
|
|
** autoScale If true, scale the value automatically to some sensible range.
|
|
|
|
** scale The scale to use if autoScale is false.
|
|
|
|
** decimals Number of decimals used in the value.
|
|
|
|
** percentual Return the value as % from the maximum value.
|
|
|
|
*/
|
2011-01-19 15:31:45 +00:00
|
|
|
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_bGetStringValueDefined && m_pLuaScript && m_pLuaScript->IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
m_strValue = m_pLuaScript->RunFunctionString(g_strGetStringValueFunction);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
|
|
|
return CheckSubstitute(m_strValue.c_str());
|
|
|
|
}
|
2010-12-13 11:47:03 +00:00
|
|
|
|
|
|
|
return CMeasure::GetStringValue(autoScale, scale, decimals, percentual);
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
static void stackDump(lua_State *L)
|
|
|
|
{
|
|
|
|
int i = lua_gettop(L);
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, " ---------------- Stack Dump ----------------" );
|
|
|
|
while(i)
|
|
|
|
{
|
|
|
|
int t = lua_type(L, i);
|
|
|
|
switch (t)
|
|
|
|
{
|
|
|
|
case LUA_TSTRING:
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d:`%s'", i, lua_tostring(L, i));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s",i,lua_toboolean(L, i) ? "true" : "false");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LUA_TNUMBER:
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %g", i, lua_tonumber(L, i));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2011-01-30 15:39:14 +00:00
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "%d: %s", i, lua_typename(L, t));
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
}
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
LuaManager::LuaLog(LOG_DEBUG, "--------------- Stack Dump Finished ---------------" );
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the measure specific configs.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasureScript::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
// Store the current values
|
|
|
|
std::string oldScriptFile = m_ScriptFile;
|
|
|
|
std::string oldTableName = m_TableName;
|
|
|
|
|
|
|
|
// Read common configs
|
2010-12-12 17:08:36 +00:00
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
m_ScriptFile = ConvertToAscii(parser.ReadString(section, L"ScriptFile", L"").c_str());
|
|
|
|
m_TableName = ConvertToAscii(parser.ReadString(section, L"TableName", L"").c_str());
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
if (!m_ScriptFile.empty())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
if (!m_TableName.empty())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
if (!m_Initialized ||
|
|
|
|
oldScriptFile != m_ScriptFile ||
|
|
|
|
oldTableName != m_TableName)
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
lua_State* L = LuaManager::GetState();
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
|
|
|
m_pLuaScript = new LuaScript(LuaManager::GetState(), m_ScriptFile.c_str(), m_TableName.c_str());
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_pLuaScript->IsInitialized())
|
|
|
|
{
|
|
|
|
m_bUpdateDefined = m_pLuaScript->FunctionExists(g_strUpdateFunction);
|
|
|
|
m_bInitializeDefined = m_pLuaScript->FunctionExists(g_strInitFunction);
|
|
|
|
m_bGetValueDefined = m_pLuaScript->FunctionExists(g_strGetValueFunction);
|
|
|
|
m_bGetStringValueDefined = m_pLuaScript->FunctionExists(g_strGetStringValueFunction);
|
|
|
|
|
|
|
|
m_pLuaScript->PushTable();
|
|
|
|
|
|
|
|
// Push the variable name we want to put a value in.
|
|
|
|
lua_pushstring(L, "SELF");
|
|
|
|
// Push the value
|
|
|
|
tolua_pushusertype(L, this, "CMeasure");
|
|
|
|
// Bind the variable
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
// Push the variable name we want to put a value in.
|
|
|
|
lua_pushstring(L, "SKIN");
|
|
|
|
// Push the value
|
|
|
|
tolua_pushusertype(L, m_MeterWindow, "CMeterWindow");
|
|
|
|
// Bind the variable
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
// Push the variable name we want to put a value in.
|
|
|
|
lua_pushstring(L, "RAINMETER");
|
|
|
|
// Push the value
|
|
|
|
tolua_pushusertype(L, m_MeterWindow->GetMainObject(), "CRainmeter");
|
|
|
|
// Bind the variable
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
// Look i nthe properties table for values to read from the section.
|
|
|
|
lua_getfield(L, -1, "PROPERTIES");
|
|
|
|
if (lua_isnil(L, -1) == 0)
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
while(lua_next(L, -2))
|
2011-01-30 10:39:10 +00:00
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
lua_pop(L, 1);
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
const char* strKey = lua_tostring(L, -1);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
std::wstring wstrKey = ConvertToWide(strKey);
|
|
|
|
std::wstring wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");
|
|
|
|
|
|
|
|
if (!wstrValue.empty())
|
|
|
|
{
|
|
|
|
std::string strStrVal = ConvertToAscii(wstrValue.c_str());
|
|
|
|
const char* strValue = strStrVal.c_str();
|
|
|
|
|
|
|
|
lua_pushstring(L, strValue);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
|
|
|
|
lua_pushstring(L, strKey);
|
|
|
|
}
|
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
// Pop PROPERTIES table
|
|
|
|
lua_pop(L, 1);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 15:39:14 +00:00
|
|
|
// Pop the table
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DeleteLuaScript();
|
|
|
|
}
|
2011-01-30 10:39:10 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-30 10:39:10 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: TableName missing in %s.", m_ANSIName.c_str());
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-30 10:39:10 +00:00
|
|
|
LuaManager::LuaLog(LOG_ERROR, "Script: ScriptFile missing in %s.", m_ANSIName.c_str());
|
2011-01-30 15:39:14 +00:00
|
|
|
DeleteLuaScript();
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-30 15:39:14 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
void CMeasureScript::RunFunctionWithMeter(const char* p_strFunction, CMeter* p_pMeter)
|
|
|
|
{
|
|
|
|
// Get the Lua State
|
|
|
|
lua_State* L = m_pLuaScript->GetState();
|
|
|
|
|
|
|
|
// Push the script table
|
|
|
|
m_pLuaScript->PushTable();
|
|
|
|
|
|
|
|
// Push the function onto the stack
|
2011-01-30 10:39:10 +00:00
|
|
|
lua_getfield(L, -1, p_strFunction);
|
2010-12-12 17:08:36 +00:00
|
|
|
|
2011-01-30 10:39:10 +00:00
|
|
|
// Check if the function exists
|
|
|
|
if (!lua_isnil(L, -1))
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 10:39:10 +00:00
|
|
|
// Push the Meter
|
|
|
|
tolua_pushusertype(L, (void*) p_pMeter, "CMeter");
|
|
|
|
|
|
|
|
if (lua_pcall(L, 1, 0, 0))
|
|
|
|
{
|
|
|
|
LuaManager::ReportErrors(L);
|
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CMeasureScript::MeterMouseEvent(CMeter* p_pMeter, MOUSE p_eMouse)
|
|
|
|
{
|
2011-01-30 15:39:14 +00:00
|
|
|
if (m_pLuaScript && m_pLuaScript->IsInitialized())
|
2010-12-12 17:08:36 +00:00
|
|
|
{
|
2011-01-30 10:39:10 +00:00
|
|
|
switch (p_eMouse)
|
|
|
|
{
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_LMB_DOWN:
|
|
|
|
RunFunctionWithMeter("LeftMouseDown", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_LMB_UP:
|
|
|
|
RunFunctionWithMeter("LeftMouseUp", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_LMB_DBLCLK:
|
|
|
|
RunFunctionWithMeter("LeftMouseDoubleClick", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_RMB_DOWN:
|
|
|
|
RunFunctionWithMeter("RightMouseDown", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_RMB_UP:
|
|
|
|
RunFunctionWithMeter("RightMouseUp", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_RMB_DBLCLK:
|
|
|
|
RunFunctionWithMeter("RightMouseDoubleClick", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_MMB_DOWN:
|
|
|
|
RunFunctionWithMeter("MiddleMouseDown", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_MMB_UP:
|
|
|
|
RunFunctionWithMeter("MiddleMouseUp", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
|
2010-12-12 17:08:36 +00:00
|
|
|
case MOUSE_MMB_DBLCLK:
|
|
|
|
RunFunctionWithMeter("MiddleMouseDoubleClick", p_pMeter);
|
|
|
|
break;
|
2011-01-30 10:39:10 +00:00
|
|
|
}
|
2010-12-12 17:08:36 +00:00
|
|
|
}
|
|
|
|
}
|