Script: Fixed that returning "" in Update() displayed 0 due to r885.

This commit is contained in:
Birunthan Mohanathas 2011-08-01 09:08:11 +00:00
parent 6dfb307636
commit cb4aa0ade3
5 changed files with 25 additions and 18 deletions

View File

@ -830,7 +830,7 @@ CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, cons
{
return new CMeasureCalc(meterWindow, name);
}
else if (_wcsicmp(L"script", measure) == 0)
else if (_wcsicmp(L"Script", measure) == 0)
{
return new CMeasureScript(meterWindow, name);
}

View File

@ -34,7 +34,8 @@ CMeasureScript::CMeasureScript(CMeterWindow* meterWindow, const WCHAR* name) : C
m_LuaScript(),
m_HasInitializeFunction(false),
m_HasUpdateFunction(false),
m_HasGetStringFunction(false)
m_HasGetStringFunction(false),
m_ValueType(LUA_TNIL)
{
LuaManager::Init();
}
@ -95,11 +96,19 @@ bool CMeasureScript::Update()
return false;
}
if (!(m_HasUpdateFunction && m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue)) &&
!(m_HasGetStringFunction && m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue)))
if (m_HasUpdateFunction)
{
m_Value = 0;
m_StringValue.clear();
m_ValueType = m_LuaScript->RunFunctionWithReturn(g_UpdateFunctionName, m_Value, m_StringValue);
if (m_ValueType == LUA_TSTRING)
{
m_Value = 0;
}
else if (m_ValueType == LUA_TNIL && m_HasGetStringFunction)
{
// For backwards compatbility
m_ValueType = m_LuaScript->RunFunctionWithReturn(g_GetStringFunctionName, m_Value, m_StringValue);
}
}
return PostUpdate();
@ -113,7 +122,7 @@ bool CMeasureScript::Update()
*/
const WCHAR* CMeasureScript::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
{
if (!m_StringValue.empty())
if (m_ValueType == LUA_TSTRING)
{
return CheckSubstitute(m_StringValue.c_str());
}

View File

@ -37,11 +37,13 @@ public:
protected:
LuaScript* m_LuaScript;
bool m_HasInitializeFunction;
bool m_HasUpdateFunction;
bool m_HasGetStringFunction;
int m_ValueType;
std::wstring m_StringValue;
std::string m_ScriptFile;

View File

@ -147,12 +147,12 @@ void LuaScript::RunFunction(const char* funcName)
** RunFunctionWithReturn
**
** Runs given function in script file and stores the retruned number or string.
** Returns true if the executed function returns a valid value.
** Returns LUA_TNIL when no return.
**
*/
bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue)
{
bool ret = false;
int ret = LUA_TNIL;
if (m_Initialized)
{
@ -172,17 +172,13 @@ bool LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, st
if (lua_isnumber(m_State, -1))
{
numValue = lua_tonumber(m_State, -1);
strValue.clear();
ret = true;
ret = LUA_TNUMBER;
}
else if (lua_isstring(m_State, -1))
{
const char* str = lua_tostring(m_State, -1);
strValue = ConvertToWide(str);
numValue = 0;
ret = true;
ret = LUA_TSTRING;
}
lua_pop(m_State, 2);

View File

@ -32,7 +32,7 @@ public:
bool IsFunction(const char* funcName);
void RunFunction(const char* funcName);
bool RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue);
int RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue);
void RunString(const char* str);
protected: