Added functionality to allow multiple IDs per Widget

This commit is contained in:
OmniaX-Dev 2026-04-05 06:48:58 +02:00
parent 234aa60ddd
commit fcb90c0916
7 changed files with 81 additions and 41 deletions

View file

@ -25,13 +25,25 @@ window.backgroundColor = Color(rgba(40, 0, 0, 255))
fontSize = 20
}
(@testLabel2 label) {
showBackground = false
}
(@testLabel2 label:hover) {
borderColor = Color(#208080FF)
}
(@testLabel2 label:pressed) {
showBackground = true
}
(label:hover) {
textColor = Color(rgb(0, 255, 0))
borderColor = Color(rgb(0, 255, 0))
}
(label:pressed) {
textColor = Color(#FF0000FF)
textColor = Color(#FF00FFFF)
}
(@testLabel label:hover) {
@ -45,8 +57,8 @@ window.backgroundColor = Color(rgba(40, 0, 0, 255))
(label:disabled) {
backgroundColor = Color(#EEEEEEFF)
textColor = Color(#505050FF)
borderColor = Color(#000000FF)
%textColor = Color(#505050FF)
%borderColor = Color(#000000FF)
}
% ===================================================

View file

@ -20,6 +20,7 @@
#include "Widgets.hpp"
#include "gui/Events.hpp"
#include "io/Memory.hpp"
#include "utils/Keycodes.hpp"
#include <ogfx/render/BasicRenderer.hpp>
#include <ogfx/gui/Window.hpp>
@ -356,6 +357,22 @@ namespace ogfx
return false;
}
bool Widget::addThemeID(const ostd::String& id)
{
if (STDVEC_CONTAINS(m_themeIDList, id))
return false;
m_themeIDList.push_back(id);
return true;
}
bool Widget::removeThemeID(const ostd::String& id)
{
if (!STDVEC_CONTAINS(m_themeIDList, id))
return false;
STDVEC_REMOVE(m_themeIDList, id);
return true;
}
void Widget::__draw(ogfx::BasicRenderer2D& gfx)
{
if (isDrawBoxEnabled())
@ -551,7 +568,7 @@ namespace ogfx
void RootWidget::applyTheme(const ostd::Stylesheet& theme)
{
m_color = theme.get<ostd::Color>("window.backgroundColor", getWindow().getClearColor(), getThemeID(), getThemeQualifierList());
m_color = getThemeValue<ostd::Color>(theme, "window.backgroundColor", getWindow().getClearColor());
}
void RootWidget::onDraw(ogfx::BasicRenderer2D& gfx)
@ -575,15 +592,15 @@ namespace ogfx
void Label::applyTheme(const ostd::Stylesheet& theme)
{
setColor(theme.get<ostd::Color>("label.textColor", ostd::Colors::White, getThemeID(), getThemeQualifierList()));
setBackGroundColor(theme.get<ostd::Color>("label.backgroundColor", ostd::Colors::Transparent, getThemeID(), getThemeQualifierList()));
setFontSize(theme.get<int32_t>("label.fontSize", 20, getThemeID(), getThemeQualifierList()));
m_borderRadius = theme.get<int32_t>("label.borderRadius", 10, getThemeID(), getThemeQualifierList());
m_borderWidth = theme.get<int32_t>("label.borderWidth", 2, getThemeID(), getThemeQualifierList());
m_showBorder = theme.get<bool>("label.showBorder", false, getThemeID(), getThemeQualifierList());
m_borderColor = theme.get<ostd::Color>("label.borderColor", ostd::Colors::White, getThemeID(), getThemeQualifierList());
enableBackground(theme.get<bool>("label.showBackground", false, getThemeID(), getThemeQualifierList()));
setPadding(theme.get<ostd::Rectangle>("label.padding", { 5, 5, 5, 5 }, getThemeID(), getThemeQualifierList()));
setColor(getThemeValue<ostd::Color>(theme, "label.textColor", ostd::Colors::White));
setBackGroundColor(getThemeValue<ostd::Color>(theme, "label.backgroundColor", ostd::Colors::Transparent));
setFontSize(getThemeValue<int32_t>(theme, "label.fontSize", 20));
m_borderRadius = getThemeValue<int32_t>(theme, "label.borderRadius", 10);
m_borderWidth = getThemeValue<int32_t>(theme, "label.borderWidth", 2);
m_showBorder = getThemeValue<bool>(theme, "label.showBorder", false);
m_borderColor = getThemeValue<ostd::Color>(theme, "label.borderColor", ostd::Colors::White);
enableBackground(getThemeValue<bool>(theme, "label.showBackground", false));
setPadding(getThemeValue<ostd::Rectangle>(theme, "label.padding", { 5, 5, 5, 5 }));
}
void Label::onDraw(ogfx::BasicRenderer2D& gfx)

View file

@ -25,6 +25,7 @@
#include <ostd/math/Geometry.hpp>
#include <ostd/data/Color.hpp>
#include <ostd/io/Stylesheet.hpp>
#include <ostd/utils/Time.hpp>
#include <functional>
namespace ogfx
@ -90,6 +91,8 @@ namespace ogfx
void reloadTheme(void);
void setThemeQualifier(const ostd::String& qualifier, bool value = true);
bool getThemeQualifier(const ostd::String& qualifier);
bool addThemeID(const ostd::String& id);
bool removeThemeID(const ostd::String& id);
inline const ostd::Stylesheet::QualifierList& getThemeQualifierList(void) const { return m_qualifierList; }
inline virtual void onDraw(ogfx::BasicRenderer2D& gfx) { }
@ -155,8 +158,13 @@ namespace ogfx
inline Rectangle getPadding(void) { return m_padding; }
inline bool isMouseInside(void) const { return m_mouseInside; }
inline ogfx::MouseEventData::eButton getPressedMouseButton(void) const { return m_pressedButton; }
inline ostd::String getThemeID(void) const { return m_themeID; }
inline void setThemeID(const ostd::String& id) { m_themeID = id; }
inline const std::vector<ostd::String>& getThemeIDList(void) const { return m_themeIDList; }
template<typename T>
inline T getThemeValue(const ostd::Stylesheet &theme, const ostd::String& key, const T& fallback)
{
return theme.get<T>(key, fallback, getThemeIDList(), getThemeQualifierList());
}
protected:
inline void disableChildren(void) { m_allowChildren = false; }
@ -194,7 +202,7 @@ namespace ogfx
bool m_mouseInside { false };
MouseEventData::eButton m_pressedButton { MouseEventData::eButton::None };
ostd::String m_themeID { "" };
std::vector<ostd::String> m_themeIDList;
ostd::Stylesheet::QualifierList m_qualifierList {
{ "disabled", false },
{ "pressed", false },

View file

@ -25,6 +25,7 @@
#include <ostd/io/IOHandlers.hpp>
#define STDVEC_CONTAINS(vec, elem) (std::find(vec.begin(), vec.end(), elem) != vec.end())
#define STDVEC_REMOVE(vec, elem) (vec.erase(std::remove(vec.begin(), vec.end(), elem), vec.end()))
namespace ostd
{

View file

@ -151,35 +151,35 @@ namespace ostd
m_values[fullKey] = std::move(value);
}
const Stylesheet::TypeVariant* Stylesheet::getVariant(const ostd::String& key, const ostd::String& themeID, const QualifierList& qualifierList) const
const Stylesheet::TypeVariant* Stylesheet::getVariant(const ostd::String& key, const std::vector<ostd::String>& themeIDList, const QualifierList& qualifierList) const
{
std::vector<ostd::String> emptyThemeIDList { "" };
const std::vector<ostd::String>& __themeIDList = (themeIDList.size() == 0 ? emptyThemeIDList : themeIDList);
for (const auto&[qualifier, state] : qualifierList)
{
const TypeVariant* v = (state ? getFull("@" + themeID + ":" + qualifier + "." + key) : nullptr);
// std::cout << "0: " << "@" + themeID + ":" + qualifier + "." + key << "\n";
if (v)
const TypeVariant* v = nullptr;
for (int32_t i = __themeIDList.size() - 1; i >= 0; i--)
{
std::cout << "1: " << "@" + themeID + ":" + qualifier + "." + key << "\n";
return v;
}
v = (state ? getFull("@:" + qualifier + "." + key) : nullptr);
if (v)
{
std::cout << "2: " << "@:" + qualifier + "." + key << "\n";
return v;
const ostd::String& themeID = __themeIDList[i];
v = (state ? getFull("@" + themeID + ":" + qualifier + "." + key) : nullptr);
if (v)
return v;
}
}
if (auto v = getFull("@" + themeID + "." + key))
for (const auto&[qualifier, state] : qualifierList)
{
std::cout << "3: " << "@" + themeID + "." + key << "\n";
return v;
const TypeVariant* v = (state ? getFull("@:" + qualifier + "." + key) : nullptr);
if (v)
return v;
}
else if (auto v = getFull("@." + key))
for (int32_t i = __themeIDList.size() - 1; i >= 0; i--)
{
std::cout << "4: " << "@." + key << "\n";
return v;
const ostd::String& themeID = __themeIDList[i];
if (auto v = getFull("@" + themeID + "." + key))
return v;
}
std::cout << "\n\n";
if (auto v = getFull("@." + key))
return v;
return nullptr;
}

View file

@ -38,13 +38,13 @@ namespace ostd
Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true);
void set(const std::string& key, TypeVariant value, const ostd::String& themeID);
void setFull(const ostd::String& fullKey, TypeVariant value);
const TypeVariant* getVariant(const ostd::String& key, const ostd::String& themeID, const QualifierList& qualifierList) const;
const TypeVariant* getVariant(const ostd::String& key, const std::vector<ostd::String>& themeIDList, const QualifierList& qualifierList) const;
const TypeVariant* getFull(const ostd::String& fullKey) const;
template<typename T>
inline T get(const ostd::String& key, const T& fallback, const ostd::String& themeID, const QualifierList& qualifierList) const
inline T get(const ostd::String& key, const T& fallback, const std::vector<ostd::String>& themeIDList, const QualifierList& qualifierList) const
{
if (auto v = getVariant(key, themeID, qualifierList))
if (auto v = getVariant(key, themeIDList, qualifierList))
{
if (auto p = std::get_if<T>(v))
return *p;

View file

@ -62,16 +62,18 @@ class Window : public ogfx::gui::Window
// m_label1.setMouseDraggedCallback([&](const ogfx::gui::Event& event) -> void {
// m_label1.applyThemeValue(m_theme, "label.backgroundColor", ostd::Colors::DarkGreen, false);
// });
addWidget(m_label1);
m_label1.setThemeQualifier("disabled");
// addWidget(m_label1);
// m_label1.setThemeQualifier("disabled");
m_label2.setPosition(100, 400);
m_label2.setText("Ciccia Bella!");
m_label2.setThemeID("testLabel");
m_label2.addThemeID("testLabel");
m_label2.addThemeID("testLabel2");
addWidget(m_label2);
m_theme.loadFromFile("./testTheme.txt");
setTheme(m_theme);
m_theme.debugPrint();
}
inline void onSignal(ostd::Signal& signal) override