From 6d3f5cf70cb3360327be903350784e7dbfc8f4cb Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Sat, 30 Aug 2025 20:59:13 +0400 Subject: [PATCH] Dehardcode script settings window font and colors --- CMakeLists.txt | 2 +- apps/components_tests/lua/testutilpackage.cpp | 7 ++++++ apps/openmw/mwlua/uibindings.cpp | 1 + components/lua/utilpackage.cpp | 25 +++++++++++++++++++ files/data/scripts/omw/mwui/constants.lua | 9 ++++--- files/lua_api/openmw/core.lua | 5 +++- files/lua_api/openmw/util.lua | 10 ++++++++ 7 files changed, 53 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0bc9bfd106..1673c80a0c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...") set(OPENMW_VERSION_MAJOR 0) set(OPENMW_VERSION_MINOR 51) set(OPENMW_VERSION_RELEASE 0) -set(OPENMW_LUA_API_REVISION 100) +set(OPENMW_LUA_API_REVISION 101) set(OPENMW_POSTPROCESSING_API_REVISION 3) set(OPENMW_VERSION_COMMITHASH "") diff --git a/apps/components_tests/lua/testutilpackage.cpp b/apps/components_tests/lua/testutilpackage.cpp index 47041985f6..11874a2cea 100644 --- a/apps/components_tests/lua/testutilpackage.cpp +++ b/apps/components_tests/lua/testutilpackage.cpp @@ -158,6 +158,13 @@ namespace EXPECT_EQ(get(lua, "darkRed:asHex()"), "a01112"); EXPECT_TRUE(get(lua, "green:asRgba() == util.vector4(0, 1, 0, 1)")); EXPECT_TRUE(get(lua, "red:asRgb() == util.vector3(1, 0, 0)")); + lua.safe_script("green = util.color.commaString('0,255,0')"); + EXPECT_EQ(get(lua, "tostring(green)"), "(0, 1, 0, 1)"); + lua.safe_script("red = util.color.commaString('255, 0, 0, 255')"); + EXPECT_EQ(get(lua, "tostring(red)"), "(1, 0, 0, 1)"); + lua.safe_script("blue = util.color.commaString('0, 0, 1000, 255')"); + EXPECT_EQ(get(lua, "tostring(blue)"), "(0, 0, 1, 1)"); + EXPECT_ERROR(lua.safe_script("white = util.color.commaString('aaa')"), "Invalid comma-separated color"); } TEST_F(LuaUtilPackageTest, Transform) diff --git a/apps/openmw/mwlua/uibindings.cpp b/apps/openmw/mwlua/uibindings.cpp index 18f7d91001..05d3910f42 100644 --- a/apps/openmw/mwlua/uibindings.cpp +++ b/apps/openmw/mwlua/uibindings.cpp @@ -77,6 +77,7 @@ namespace MWLua luaManager->addAction([state] { MWBase::Environment::get().getWindowManager()->setHudVisibility(state); }); }; api["_isHudVisible"] = []() -> bool { return MWBase::Environment::get().getWindowManager()->isHudVisible(); }; + api["_getDefaultFontSize"] = []() -> int { return Settings::gui().mFontSize; }; api["showMessage"] = [luaManager = context.mLuaManager](std::string_view message, const sol::optional& options) { MWGui::ShowInDialogueMode mode = MWGui::ShowInDialogueMode_IfPossible; diff --git a/components/lua/utilpackage.cpp b/components/lua/utilpackage.cpp index 0117934def..c4a8230def 100644 --- a/components/lua/utilpackage.cpp +++ b/components/lua/utilpackage.cpp @@ -8,6 +8,9 @@ #include #include +#include + +#include #include "luastate.hpp" #include "util.hpp" @@ -243,6 +246,28 @@ namespace LuaUtil color["rgba"] = [](float r, float g, float b, float a) { return Misc::Color(r, g, b, a); }; color["rgb"] = [](float r, float g, float b) { return Misc::Color(r, g, b, 1); }; color["hex"] = [](std::string_view hex) { return Misc::Color::fromHex(hex); }; + color["commaString"] = [](std::string_view str) { + int wrongChars = std::count_if( + str.begin(), str.end(), [](unsigned char c) { return !std::isdigit(c) && c != ' ' && c != ','; }); + + if (wrongChars != 0) + { + throw std::runtime_error("Invalid comma-separated color: " + std::string(str)); + } + + std::vector rgba; + Misc::StringUtils::split(str, rgba, ","); + if (rgba.size() != 3 && rgba.size() != 4) + { + throw std::runtime_error("Invalid comma-separated color: " + std::string(str)); + } + + if (rgba.size() == 3) + rgba.push_back("255"); + + return Misc::Color(MyGUI::utility::parseInt(rgba[0]) / 255.f, MyGUI::utility::parseInt(rgba[1]) / 255.f, + MyGUI::utility::parseInt(rgba[2]) / 255.f, MyGUI::utility::parseInt(rgba[3]) / 255.f); + }; util["color"] = LuaUtil::makeReadOnly(color); // Lua bindings for Transform diff --git a/files/data/scripts/omw/mwui/constants.lua b/files/data/scripts/omw/mwui/constants.lua index 8a02268f77..918a09376b 100644 --- a/files/data/scripts/omw/mwui/constants.lua +++ b/files/data/scripts/omw/mwui/constants.lua @@ -1,11 +1,12 @@ +local core = require('openmw.core') local ui = require('openmw.ui') local util = require('openmw.util') return { - textNormalSize = 16, - textHeaderSize = 16, - headerColor = util.color.rgb(223 / 255, 201 / 255, 159 / 255), - normalColor = util.color.rgb(202 / 255, 165 / 255, 96 / 255), + textNormalSize = ui._getDefaultFontSize(), + textHeaderSize = ui._getDefaultFontSize(), + headerColor = util.color.commaString(core.getGMST("FontColor_color_header")), + normalColor = util.color.commaString(core.getGMST("FontColor_color_normal")), border = 2, thickBorder = 4, padding = 2, diff --git a/files/lua_api/openmw/core.lua b/files/lua_api/openmw/core.lua index 545cda634f..e5d5a3ae7f 100644 --- a/files/lua_api/openmw/core.lua +++ b/files/lua_api/openmw/core.lua @@ -57,10 +57,13 @@ -- @return #number --- --- Get a GMST setting from content files. +-- Get a game setting with given name (from GMST ESM records or from openmw.cfg). -- @function [parent=#core] getGMST -- @param #string setting Setting name -- @return #any +-- @usage local skillBonus = core.getGMST('fMinorSkillBonus') -- get a numeric GMST from ESM data +-- @usage local jailFormatString = core.getGMST('sNotifyMessage42') -- get a string GMST from ESM data +-- @usage local bloodTextureName = core.getGMST('Blood_Texture_1') -- get a "fallback" parameter value from openmw.cfg (always a string) --- -- The game's difficulty setting. diff --git a/files/lua_api/openmw/util.lua b/files/lua_api/openmw/util.lua index 0ba1fe2a8e..f04b24843d 100644 --- a/files/lua_api/openmw/util.lua +++ b/files/lua_api/openmw/util.lua @@ -477,6 +477,16 @@ -- @param #number a -- @return #Color +--- +-- Creates a Color from comma-separated string (in RGB or RGBA order, spaces are ignored) +-- @function [parent=#COLOR] commaString +-- @param #string str +-- @return #Color +-- @usage local color = util.color.commaString('255,0,0') -- red color +-- @usage local color = util.color.commaString('10000,0,0') -- red color (values are still capped at 255) +-- @usage local color = util.color.commaString('0, 0, 255, 255') -- blue color, with explicit alpha +-- @usage local color = util.color.commaString('0,255,0,128') -- green color, semi-transparent + --- -- Creates a Color from RGB format. Equivalent to calling util.rgba with a = 1. -- @function [parent=#COLOR] rgb