Index: ps/trunk/source/gui/CGUI.h =================================================================== --- ps/trunk/source/gui/CGUI.h +++ ps/trunk/source/gui/CGUI.h @@ -138,6 +138,12 @@ void UnsetObjectHotkey(IGUIObject* pObject, const CStr& hotkeyTag); /** + * Allows the JS side to modify the style setting assigned to a GUI object. + */ + void SetObjectStyle(IGUIObject* pObject, const CStr& styleName); + void UnsetObjectStyle(IGUIObject* pObject); + + /** * Allows the JS side to add or remove global hotkeys. */ void SetGlobalHotkey(const CStr& hotkeyTag, const CStr& eventName, JS::HandleValue function); Index: ps/trunk/source/gui/CGUI.cpp =================================================================== --- ps/trunk/source/gui/CGUI.cpp +++ ps/trunk/source/gui/CGUI.cpp @@ -389,6 +389,19 @@ } } +void CGUI::SetObjectStyle(IGUIObject* pObject, const CStr& styleName) +{ + // If the style is not recognised (or an empty string) then ApplyStyle will + // emit an error message. Thus we don't need to handle it here. + if (pObject->ApplyStyle(styleName)) + pObject->m_Style = styleName; +} + +void CGUI::UnsetObjectStyle(IGUIObject* pObject) +{ + SetObjectStyle(pObject, "default"); +} + void CGUI::SetObjectHotkey(IGUIObject* pObject, const CStr& hotkeyTag) { if (!hotkeyTag.empty()) @@ -599,18 +612,11 @@ // // Always load default (if it's available) first! // - CStr argStyle(attributes.GetNamedItem(attr_style)); - - if (m_Styles.find("default") != m_Styles.end()) - object->LoadStyle("default"); + SetObjectStyle(object, "default"); + CStr argStyle(attributes.GetNamedItem(attr_style)); if (!argStyle.empty()) - { - if (m_Styles.find(argStyle) == m_Styles.end()) - LOGERROR("GUI: Trying to use style '%s' that doesn't exist.", argStyle.c_str()); - else - object->LoadStyle(argStyle); - } + SetObjectStyle(object, argStyle); bool NameSet = false; bool ManuallySetZ = false; Index: ps/trunk/source/gui/ObjectBases/IGUIObject.h =================================================================== --- ps/trunk/source/gui/ObjectBases/IGUIObject.h +++ ps/trunk/source/gui/ObjectBases/IGUIObject.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2020 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -322,9 +322,12 @@ virtual InReaction ManuallyHandleKeys(const SDL_Event_* UNUSED(ev)) { return IN_PASS; } /** - * Loads a style. + * Applies the given style to the object. + * + * Returns false if the style is not recognised (and thus has + * not been applied). */ - void LoadStyle(const CStr& StyleName); + bool ApplyStyle(const CStr& StyleName); /** * Returns not the Z value, but the actual buffered Z value, i.e. if it's Index: ps/trunk/source/gui/ObjectBases/IGUIObject.cpp =================================================================== --- ps/trunk/source/gui/ObjectBases/IGUIObject.cpp +++ ps/trunk/source/gui/ObjectBases/IGUIObject.cpp @@ -162,6 +162,8 @@ } else if (Setting == "hotkey") m_pGUI.SetObjectHotkey(this, GetSetting(Setting)); + else if (Setting == "style") + m_pGUI.SetObjectStyle(this, GetSetting(Setting)); if (SendMessage) { @@ -267,10 +269,13 @@ } } -void IGUIObject::LoadStyle(const CStr& StyleName) +bool IGUIObject::ApplyStyle(const CStr& StyleName) { if (!m_pGUI.HasStyle(StyleName)) - debug_warn(L"IGUIObject::LoadStyle failed"); + { + LOGERROR("IGUIObject: Trying to use style '%s' that doesn't exist.", StyleName.c_str()); + return false; + } // The default style may specify settings for any GUI object. // Other styles are reported if they specify a Setting that does not exist, @@ -283,6 +288,7 @@ else if (StyleName != "default") LOGWARNING("GUI object has no setting \"%s\", but the style \"%s\" defines it", p.first, StyleName.c_str()); } + return true; } float IGUIObject::GetBufferedZ() const