diff --git a/binding-mri/binding-mri.cpp b/binding-mri/binding-mri.cpp index 3c4128a..2dcd0b9 100644 --- a/binding-mri/binding-mri.cpp +++ b/binding-mri/binding-mri.cpp @@ -74,6 +74,7 @@ void graphicsBindingInit(); void fileIntBindingInit(); void screenBindingInit(); +void wallpaperBindingInit(); void oneshotBindingInit(); void steamBindingInit(); @@ -106,6 +107,7 @@ static void mriBindingInit() fileIntBindingInit(); screenBindingInit(); + wallpaperBindingInit(); oneshotBindingInit(); steamBindingInit(); diff --git a/binding-mri/wallpaper-binding.cpp b/binding-mri/wallpaper-binding.cpp new file mode 100644 index 0000000..1ce16bb --- /dev/null +++ b/binding-mri/wallpaper-binding.cpp @@ -0,0 +1,140 @@ +#include "etc.h" +#include "sharedstate.h" +#include "binding-util.h" +#include "binding-types.h" +#include "config.h" + +static bool isCached = false; + +#ifdef _WIN32 +#include +static WCHAR szStyle[8] = {0}; +static WCHAR szTile[8] = {0}; +static WCHAR szFile[MAX_PATH+1] = {0}; +static DWORD oldcolor = 0; +static DWORD szStyleSize = sizeof(szStyle) - 1; +static DWORD szTileSize = sizeof(szTile) - 1; +static bool setStyle = false; +static bool setTile = false; +#endif + +RB_METHOD(wallpaperSet) +{ + RB_UNUSED_PARAM; + const char *imageName; + rb_get_args(argc, argv, "z", &imageName RB_ARG_END); + std::string imgname = shState->config().gameFolder + "/Wallpaper/" + imageName + ".bmp"; +#ifdef _WIN32 + // Crapify the slashes + size_t index = 0; + for (;;) { + index = imgname.find("/", index); + if (index == std::string::npos) + break; + imgname.replace(index, 1, "\\"); + index += 1; + } + WCHAR imgnameW[MAX_PATH]; + WCHAR imgnameFull[MAX_PATH]; + MultiByteToWideChar(CP_UTF8, 0, imgname.c_str(), -1, imgnameW, MAX_PATH); + GetFullPathNameW(imgnameW, MAX_PATH, imgnameFull, NULL); + + + int colorId = COLOR_BACKGROUND; + int color = 0x1a041f; + WCHAR zero[2] = L"0"; + DWORD zeroSize = 4; + + HKEY hKey = NULL; + if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_READ, &hKey) != ERROR_SUCCESS) + goto end; + + if (!isCached) { + // QUERY + + // Style + setStyle = RegQueryValueExW(hKey, L"WallpaperStyle", 0, NULL, (LPBYTE)(szStyle), &szStyleSize) == ERROR_SUCCESS; + + // Tile + setTile = RegQueryValueExW(hKey, L"TileWallpaper", 0, NULL, (LPBYTE)(szTile), &szTileSize) == ERROR_SUCCESS; + + // File path + if (!SystemParametersInfoW(SPI_GETDESKWALLPAPER, MAX_PATH, (PVOID)szFile, 0)) + goto end; + + // Color + oldcolor = GetSysColor(COLOR_BACKGROUND); + + isCached = true; + } + + RegCloseKey(hKey); + hKey = NULL; + if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) + goto end; + + // SET + + // Set the style + if (RegSetValueExW(hKey, L"WallpaperStyle", 0, REG_SZ, (const BYTE*)zero, zeroSize) != ERROR_SUCCESS) + goto end; + + if (RegSetValueExW(hKey, L"TileWallpaper", 0, REG_SZ, (const BYTE*)zero, zeroSize) != ERROR_SUCCESS) + goto end; + + // Set the wallpaper + if (!SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (PVOID)imgnameFull, SPIF_UPDATEINIFILE)) + goto end; + + // Set the color + if (!SetSysColors(1, &colorId, (const COLORREF *)&color)) + goto end; +end: + if (hKey) + RegCloseKey(hKey); +#else +#endif + return Qnil; +} + +RB_METHOD(wallpaperReset) +{ + RB_UNUSED_PARAM; +#ifdef _WIN32 + if (isCached) { + int colorId = COLOR_BACKGROUND; + HKEY hKey = NULL; + if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) + goto end; + + // Set the style + if (setStyle) + RegSetValueExW(hKey, L"WallpaperStyle", 0, REG_SZ, (const BYTE*)szStyle, szStyleSize); + + if (setTile) + RegSetValueExW(hKey, L"TileWallpaper", 0, REG_SZ, (const BYTE*)szTile, szTileSize); + + // Set the wallpaper + if (!SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (PVOID)szFile, SPIF_UPDATEINIFILE)) + goto end; + + // Set the color + if (!SetSysColors(1, &colorId, (const COLORREF *)&oldcolor)) + goto end; + end: + if (hKey) + RegCloseKey(hKey); + } +#else +#endif + return Qnil; +} + +void wallpaperBindingInit() +{ + VALUE module = rb_define_module("Wallpaper"); + + //Functions + _rb_define_module_function(module, "set", wallpaperSet); + _rb_define_module_function(module, "reset", wallpaperReset); +} diff --git a/mkxp.pro b/mkxp.pro index 5186e77..2436627 100644 --- a/mkxp.pro +++ b/mkxp.pro @@ -197,7 +197,8 @@ SOURCES += \ binding-mri/steam-binding.cpp \ src/xdg-user-dir-lookup.c \ src/screen.cpp \ - binding-mri/screen-binding.cpp + binding-mri/screen-binding.cpp \ + binding-mri/wallpaper-binding.cpp STEAM { HEADERS += src/steam.h steamshim/steamshim_child.h diff --git a/scripts/Game_Oneshot.rb b/scripts/Game_Oneshot.rb index a2dd2fa..f007e91 100644 --- a/scripts/Game_Oneshot.rb +++ b/scripts/Game_Oneshot.rb @@ -4,6 +4,7 @@ class Game_Oneshot #-------------------------------------------------------------------------- attr_accessor :player_name # map music (for battle memory) attr_accessor :plight_timer # start of plight's plight + attr_accessor :wallpaper # the wallpaper that we should use def initialize if $GDC @@ -18,5 +19,18 @@ class Game_Oneshot end @lights = {} @plight_timer = nil + @wallpaper = nil + end +end + +module Wallpaper + def set_persistent(name) + $game_oneshot.wallpaper = name + Wallpaper.set(name) + end + + def reset_persistent + $game_oneshot.wallpaper = nil + Wallpaper.reset end end diff --git a/scripts/Main.rb b/scripts/Main.rb index afb8905..e256317 100644 --- a/scripts/Main.rb +++ b/scripts/Main.rb @@ -5,6 +5,7 @@ #============================================================================== at_exit do + Wallpaper.reset save unless ($game_system.map_interpreter.running? || !$scene.is_a?(Scene_Map)) end diff --git a/scripts/SaveLoad.rb b/scripts/SaveLoad.rb index 47a725a..630476e 100644 --- a/scripts/SaveLoad.rb +++ b/scripts/SaveLoad.rb @@ -79,6 +79,10 @@ def real_load # Switch to map screen $scene = Scene_Map.new $game_temp.common_event_id = 42 + # Set wallpaper if necessary + if $game_oneshot.wallpaper + Wallpaper.set $game_oneshot.wallpaper + end end def save_exists @@ -90,4 +94,4 @@ def quit_game_bed save Oneshot.allow_exit true exit -end \ No newline at end of file +end