From 3c8e26369b74233908f6a5a76c1cac74c697cdce Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 9 Jan 2005 01:31:06 +0000 Subject: [PATCH] Lua should not depend on ThemeManager --- stepmania/src/LuaHelpers.cpp | 31 ++++++--------------------- stepmania/src/LuaHelpers.h | 4 +++- stepmania/src/ScreenOptionsMaster.cpp | 2 +- stepmania/src/ThemeManager.cpp | 29 ++++++++++++++++++++++++- stepmania/src/ThemeManager.h | 1 + 5 files changed, 39 insertions(+), 28 deletions(-) diff --git a/stepmania/src/LuaHelpers.cpp b/stepmania/src/LuaHelpers.cpp index 1f154fafaa..280a78b3bd 100644 --- a/stepmania/src/LuaHelpers.cpp +++ b/stepmania/src/LuaHelpers.cpp @@ -3,7 +3,6 @@ #include "LuaFunctions.h" #include "RageUtil.h" #include "RageLog.h" -#include "ScreenDimensions.h" #include "arch/Dialog/Dialog.h" #include @@ -98,6 +97,10 @@ bool Lua::GetStack( lua_State *L, int pos, int &out ) return true; } +void Lua::SetGlobal( lua_State *L, const CString &sName ) +{ + lua_setglobal( L, sName ); +} @@ -145,33 +148,11 @@ void CloseLua() L = NULL; } -void Lua::UpdateGlobals() +lua_State *Lua::GetGlobalState() { if( L == NULL ) OpenLua(); - - ASSERT( L ); - - /* Important: explicitly refresh cached metrics that we use. */ - THEME_SCREEN_WIDTH.Read(); - THEME_SCREEN_HEIGHT.Read(); - - lua_pushnumber( L, SCREEN_WIDTH ); - lua_setglobal( L, "SCREEN_WIDTH" ); - lua_pushnumber( L, SCREEN_HEIGHT ); - lua_setglobal( L, "SCREEN_HEIGHT" ); - lua_pushnumber( L, SCREEN_LEFT ); - lua_setglobal( L, "SCREEN_LEFT" ); - lua_pushnumber( L, SCREEN_RIGHT ); - lua_setglobal( L, "SCREEN_RIGHT" ); - lua_pushnumber( L, SCREEN_TOP ); - lua_setglobal( L, "SCREEN_TOP" ); - lua_pushnumber( L, SCREEN_BOTTOM ); - lua_setglobal( L, "SCREEN_BOTTOM" ); - lua_pushnumber( L, SCREEN_CENTER_X ); - lua_setglobal( L, "SCREEN_CENTER_X" ); - lua_pushnumber( L, SCREEN_CENTER_Y ); - lua_setglobal( L, "SCREEN_CENTER_Y" ); + return L; } void Lua::PrepareExpression( CString &sInOut ) diff --git a/stepmania/src/LuaHelpers.h b/stepmania/src/LuaHelpers.h index bafc053c42..3714b452f0 100644 --- a/stepmania/src/LuaHelpers.h +++ b/stepmania/src/LuaHelpers.h @@ -19,7 +19,9 @@ namespace Lua void PushStack( lua_State *L, const CString &out ); void PopStack( lua_State *L, CString &out ); bool GetStack( lua_State *L, int pos, int &out ); - void UpdateGlobals(); // call this when the theme changes + void SetGlobal( lua_State *L, const CString &sName ); + + lua_State *GetGlobalState(); }; #endif diff --git a/stepmania/src/ScreenOptionsMaster.cpp b/stepmania/src/ScreenOptionsMaster.cpp index 7468b09863..e0c4d1b1eb 100644 --- a/stepmania/src/ScreenOptionsMaster.cpp +++ b/stepmania/src/ScreenOptionsMaster.cpp @@ -538,7 +538,7 @@ void ScreenOptionsMaster::ExportOptions() if( ChangeMask & OPT_APPLY_ASPECT_RATIO ) { - Lua::UpdateGlobals(); // This needs to be done before resetting the projection matrix below + THEME->UpdateLuaGlobals(); // This needs to be done before resetting the projection matrix below SCREENMAN->ThemeChanged(); // recreate ScreenSystemLayer and SharedBGA } diff --git a/stepmania/src/ThemeManager.cpp b/stepmania/src/ThemeManager.cpp index 425e5ba5a3..cbb8846618 100644 --- a/stepmania/src/ThemeManager.cpp +++ b/stepmania/src/ThemeManager.cpp @@ -18,6 +18,7 @@ #include "Foreach.h" #include "ThemeMetric.h" #include "LuaHelpers.h" +#include "ScreenDimensions.h" ThemeManager* THEME = NULL; // global object accessable from anywhere in the program @@ -267,12 +268,38 @@ void ThemeManager::SwitchThemeAndLanguage( const CString &sThemeName, const CStr /* Lua globals can use metrics which are cached, and vice versa. Update Lua * globals first; it's Lua's job to explicitly update cached metrics that it * uses. */ - Lua::UpdateGlobals(); + UpdateLuaGlobals(); // reload subscribers FOREACH( IThemeMetric*, *g_pvpSubscribers, p ) (*p)->Read(); } +void ThemeManager::UpdateLuaGlobals() +{ + /* Important: explicitly refresh cached metrics that we use. */ + THEME_SCREEN_WIDTH.Read(); + THEME_SCREEN_HEIGHT.Read(); + + lua_State *L = Lua::GetGlobalState(); + + Lua::PushStack( L, (int) SCREEN_WIDTH ); + Lua::SetGlobal( L, "SCREEN_WIDTH" ); + Lua::PushStack( L, (int) SCREEN_HEIGHT ); + Lua::SetGlobal( L, "SCREEN_HEIGHT" ); + Lua::PushStack( L, (int) SCREEN_LEFT ); + Lua::SetGlobal( L, "SCREEN_LEFT" ); + Lua::PushStack( L, (int) SCREEN_RIGHT ); + Lua::SetGlobal( L, "SCREEN_RIGHT" ); + Lua::PushStack( L, (int) SCREEN_TOP ); + Lua::SetGlobal( L, "SCREEN_TOP" ); + Lua::PushStack( L, (int) SCREEN_BOTTOM ); + Lua::SetGlobal( L, "SCREEN_BOTTOM" ); + Lua::PushStack( L, (int) SCREEN_CENTER_X ); + Lua::SetGlobal( L, "SCREEN_CENTER_X" ); + Lua::PushStack( L, (int) SCREEN_CENTER_Y ); + Lua::SetGlobal( L, "SCREEN_CENTER_Y" ); +} + CString ThemeManager::GetThemeDirFromName( const CString &sThemeName ) { return THEMES_DIR + sThemeName + "/"; diff --git a/stepmania/src/ThemeManager.h b/stepmania/src/ThemeManager.h index f567541cd8..a7fe499f05 100644 --- a/stepmania/src/ThemeManager.h +++ b/stepmania/src/ThemeManager.h @@ -27,6 +27,7 @@ public: void GetLanguages( CStringArray& AddTo ); bool DoesLanguageExist( const CString &sLanguage ); void SwitchThemeAndLanguage( const CString &sThemeName, const CString &sLanguage ); + void UpdateLuaGlobals(); CString GetCurThemeName() { return m_sCurThemeName; }; CString GetCurLanguage() { return m_sCurLanguage; }; CString GetCurThemeDir() { return GetThemeDirFromName(m_sCurThemeName); };