Added functionality to allow multiple IDs per Widget
This commit is contained in:
parent
234aa60ddd
commit
fcb90c0916
7 changed files with 81 additions and 41 deletions
|
|
@ -25,13 +25,25 @@ window.backgroundColor = Color(rgba(40, 0, 0, 255))
|
||||||
fontSize = 20
|
fontSize = 20
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(@testLabel2 label) {
|
||||||
|
showBackground = false
|
||||||
|
}
|
||||||
|
|
||||||
|
(@testLabel2 label:hover) {
|
||||||
|
borderColor = Color(#208080FF)
|
||||||
|
}
|
||||||
|
|
||||||
|
(@testLabel2 label:pressed) {
|
||||||
|
showBackground = true
|
||||||
|
}
|
||||||
|
|
||||||
(label:hover) {
|
(label:hover) {
|
||||||
textColor = Color(rgb(0, 255, 0))
|
textColor = Color(rgb(0, 255, 0))
|
||||||
borderColor = Color(rgb(0, 255, 0))
|
borderColor = Color(rgb(0, 255, 0))
|
||||||
}
|
}
|
||||||
|
|
||||||
(label:pressed) {
|
(label:pressed) {
|
||||||
textColor = Color(#FF0000FF)
|
textColor = Color(#FF00FFFF)
|
||||||
}
|
}
|
||||||
|
|
||||||
(@testLabel label:hover) {
|
(@testLabel label:hover) {
|
||||||
|
|
@ -45,8 +57,8 @@ window.backgroundColor = Color(rgba(40, 0, 0, 255))
|
||||||
|
|
||||||
(label:disabled) {
|
(label:disabled) {
|
||||||
backgroundColor = Color(#EEEEEEFF)
|
backgroundColor = Color(#EEEEEEFF)
|
||||||
textColor = Color(#505050FF)
|
%textColor = Color(#505050FF)
|
||||||
borderColor = Color(#000000FF)
|
%borderColor = Color(#000000FF)
|
||||||
}
|
}
|
||||||
|
|
||||||
% ===================================================
|
% ===================================================
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@
|
||||||
|
|
||||||
#include "Widgets.hpp"
|
#include "Widgets.hpp"
|
||||||
#include "gui/Events.hpp"
|
#include "gui/Events.hpp"
|
||||||
|
#include "io/Memory.hpp"
|
||||||
#include "utils/Keycodes.hpp"
|
#include "utils/Keycodes.hpp"
|
||||||
#include <ogfx/render/BasicRenderer.hpp>
|
#include <ogfx/render/BasicRenderer.hpp>
|
||||||
#include <ogfx/gui/Window.hpp>
|
#include <ogfx/gui/Window.hpp>
|
||||||
|
|
@ -356,6 +357,22 @@ namespace ogfx
|
||||||
return false;
|
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)
|
void Widget::__draw(ogfx::BasicRenderer2D& gfx)
|
||||||
{
|
{
|
||||||
if (isDrawBoxEnabled())
|
if (isDrawBoxEnabled())
|
||||||
|
|
@ -551,7 +568,7 @@ namespace ogfx
|
||||||
|
|
||||||
void RootWidget::applyTheme(const ostd::Stylesheet& theme)
|
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)
|
void RootWidget::onDraw(ogfx::BasicRenderer2D& gfx)
|
||||||
|
|
@ -575,15 +592,15 @@ namespace ogfx
|
||||||
|
|
||||||
void Label::applyTheme(const ostd::Stylesheet& theme)
|
void Label::applyTheme(const ostd::Stylesheet& theme)
|
||||||
{
|
{
|
||||||
setColor(theme.get<ostd::Color>("label.textColor", ostd::Colors::White, getThemeID(), getThemeQualifierList()));
|
setColor(getThemeValue<ostd::Color>(theme, "label.textColor", ostd::Colors::White));
|
||||||
setBackGroundColor(theme.get<ostd::Color>("label.backgroundColor", ostd::Colors::Transparent, getThemeID(), getThemeQualifierList()));
|
setBackGroundColor(getThemeValue<ostd::Color>(theme, "label.backgroundColor", ostd::Colors::Transparent));
|
||||||
setFontSize(theme.get<int32_t>("label.fontSize", 20, getThemeID(), getThemeQualifierList()));
|
setFontSize(getThemeValue<int32_t>(theme, "label.fontSize", 20));
|
||||||
m_borderRadius = theme.get<int32_t>("label.borderRadius", 10, getThemeID(), getThemeQualifierList());
|
m_borderRadius = getThemeValue<int32_t>(theme, "label.borderRadius", 10);
|
||||||
m_borderWidth = theme.get<int32_t>("label.borderWidth", 2, getThemeID(), getThemeQualifierList());
|
m_borderWidth = getThemeValue<int32_t>(theme, "label.borderWidth", 2);
|
||||||
m_showBorder = theme.get<bool>("label.showBorder", false, getThemeID(), getThemeQualifierList());
|
m_showBorder = getThemeValue<bool>(theme, "label.showBorder", false);
|
||||||
m_borderColor = theme.get<ostd::Color>("label.borderColor", ostd::Colors::White, getThemeID(), getThemeQualifierList());
|
m_borderColor = getThemeValue<ostd::Color>(theme, "label.borderColor", ostd::Colors::White);
|
||||||
enableBackground(theme.get<bool>("label.showBackground", false, getThemeID(), getThemeQualifierList()));
|
enableBackground(getThemeValue<bool>(theme, "label.showBackground", false));
|
||||||
setPadding(theme.get<ostd::Rectangle>("label.padding", { 5, 5, 5, 5 }, getThemeID(), getThemeQualifierList()));
|
setPadding(getThemeValue<ostd::Rectangle>(theme, "label.padding", { 5, 5, 5, 5 }));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Label::onDraw(ogfx::BasicRenderer2D& gfx)
|
void Label::onDraw(ogfx::BasicRenderer2D& gfx)
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
#include <ostd/math/Geometry.hpp>
|
#include <ostd/math/Geometry.hpp>
|
||||||
#include <ostd/data/Color.hpp>
|
#include <ostd/data/Color.hpp>
|
||||||
#include <ostd/io/Stylesheet.hpp>
|
#include <ostd/io/Stylesheet.hpp>
|
||||||
|
#include <ostd/utils/Time.hpp>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace ogfx
|
namespace ogfx
|
||||||
|
|
@ -90,6 +91,8 @@ namespace ogfx
|
||||||
void reloadTheme(void);
|
void reloadTheme(void);
|
||||||
void setThemeQualifier(const ostd::String& qualifier, bool value = true);
|
void setThemeQualifier(const ostd::String& qualifier, bool value = true);
|
||||||
bool getThemeQualifier(const ostd::String& qualifier);
|
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 const ostd::Stylesheet::QualifierList& getThemeQualifierList(void) const { return m_qualifierList; }
|
||||||
|
|
||||||
inline virtual void onDraw(ogfx::BasicRenderer2D& gfx) { }
|
inline virtual void onDraw(ogfx::BasicRenderer2D& gfx) { }
|
||||||
|
|
@ -155,8 +158,13 @@ namespace ogfx
|
||||||
inline Rectangle getPadding(void) { return m_padding; }
|
inline Rectangle getPadding(void) { return m_padding; }
|
||||||
inline bool isMouseInside(void) const { return m_mouseInside; }
|
inline bool isMouseInside(void) const { return m_mouseInside; }
|
||||||
inline ogfx::MouseEventData::eButton getPressedMouseButton(void) const { return m_pressedButton; }
|
inline ogfx::MouseEventData::eButton getPressedMouseButton(void) const { return m_pressedButton; }
|
||||||
inline ostd::String getThemeID(void) const { return m_themeID; }
|
inline const std::vector<ostd::String>& getThemeIDList(void) const { return m_themeIDList; }
|
||||||
inline void setThemeID(const ostd::String& id) { m_themeID = id; }
|
|
||||||
|
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:
|
protected:
|
||||||
inline void disableChildren(void) { m_allowChildren = false; }
|
inline void disableChildren(void) { m_allowChildren = false; }
|
||||||
|
|
@ -194,7 +202,7 @@ namespace ogfx
|
||||||
bool m_mouseInside { false };
|
bool m_mouseInside { false };
|
||||||
MouseEventData::eButton m_pressedButton { MouseEventData::eButton::None };
|
MouseEventData::eButton m_pressedButton { MouseEventData::eButton::None };
|
||||||
|
|
||||||
ostd::String m_themeID { "" };
|
std::vector<ostd::String> m_themeIDList;
|
||||||
ostd::Stylesheet::QualifierList m_qualifierList {
|
ostd::Stylesheet::QualifierList m_qualifierList {
|
||||||
{ "disabled", false },
|
{ "disabled", false },
|
||||||
{ "pressed", false },
|
{ "pressed", false },
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
||||||
#include <ostd/io/IOHandlers.hpp>
|
#include <ostd/io/IOHandlers.hpp>
|
||||||
|
|
||||||
#define STDVEC_CONTAINS(vec, elem) (std::find(vec.begin(), vec.end(), elem) != vec.end())
|
#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
|
namespace ostd
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -151,35 +151,35 @@ namespace ostd
|
||||||
m_values[fullKey] = std::move(value);
|
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)
|
for (const auto&[qualifier, state] : qualifierList)
|
||||||
{
|
{
|
||||||
const TypeVariant* v = (state ? getFull("@" + themeID + ":" + qualifier + "." + key) : nullptr);
|
const TypeVariant* v = nullptr;
|
||||||
// std::cout << "0: " << "@" + themeID + ":" + qualifier + "." + key << "\n";
|
for (int32_t i = __themeIDList.size() - 1; i >= 0; i--)
|
||||||
if (v)
|
|
||||||
{
|
{
|
||||||
std::cout << "1: " << "@" + themeID + ":" + qualifier + "." + key << "\n";
|
const ostd::String& themeID = __themeIDList[i];
|
||||||
return v;
|
v = (state ? getFull("@" + themeID + ":" + qualifier + "." + key) : nullptr);
|
||||||
}
|
if (v)
|
||||||
v = (state ? getFull("@:" + qualifier + "." + key) : nullptr);
|
return v;
|
||||||
if (v)
|
|
||||||
{
|
|
||||||
std::cout << "2: " << "@:" + qualifier + "." + key << "\n";
|
|
||||||
return v;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (auto v = getFull("@" + themeID + "." + key))
|
for (const auto&[qualifier, state] : qualifierList)
|
||||||
{
|
{
|
||||||
std::cout << "3: " << "@" + themeID + "." + key << "\n";
|
const TypeVariant* v = (state ? getFull("@:" + qualifier + "." + key) : nullptr);
|
||||||
return v;
|
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";
|
const ostd::String& themeID = __themeIDList[i];
|
||||||
return v;
|
if (auto v = getFull("@" + themeID + "." + key))
|
||||||
|
return v;
|
||||||
}
|
}
|
||||||
std::cout << "\n\n";
|
if (auto v = getFull("@." + key))
|
||||||
|
return v;
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,13 @@ namespace ostd
|
||||||
Stylesheet& loadFromString(const ostd::String& content, const ostd::String& filePath = "memory://", bool clearCurrentRules = true);
|
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 set(const std::string& key, TypeVariant value, const ostd::String& themeID);
|
||||||
void setFull(const ostd::String& fullKey, TypeVariant value);
|
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;
|
const TypeVariant* getFull(const ostd::String& fullKey) const;
|
||||||
|
|
||||||
template<typename T>
|
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))
|
if (auto p = std::get_if<T>(v))
|
||||||
return *p;
|
return *p;
|
||||||
|
|
|
||||||
|
|
@ -62,16 +62,18 @@ class Window : public ogfx::gui::Window
|
||||||
// m_label1.setMouseDraggedCallback([&](const ogfx::gui::Event& event) -> void {
|
// m_label1.setMouseDraggedCallback([&](const ogfx::gui::Event& event) -> void {
|
||||||
// m_label1.applyThemeValue(m_theme, "label.backgroundColor", ostd::Colors::DarkGreen, false);
|
// m_label1.applyThemeValue(m_theme, "label.backgroundColor", ostd::Colors::DarkGreen, false);
|
||||||
// });
|
// });
|
||||||
addWidget(m_label1);
|
// addWidget(m_label1);
|
||||||
m_label1.setThemeQualifier("disabled");
|
// m_label1.setThemeQualifier("disabled");
|
||||||
|
|
||||||
m_label2.setPosition(100, 400);
|
m_label2.setPosition(100, 400);
|
||||||
m_label2.setText("Ciccia Bella!");
|
m_label2.setText("Ciccia Bella!");
|
||||||
m_label2.setThemeID("testLabel");
|
m_label2.addThemeID("testLabel");
|
||||||
|
m_label2.addThemeID("testLabel2");
|
||||||
addWidget(m_label2);
|
addWidget(m_label2);
|
||||||
|
|
||||||
m_theme.loadFromFile("./testTheme.txt");
|
m_theme.loadFromFile("./testTheme.txt");
|
||||||
setTheme(m_theme);
|
setTheme(m_theme);
|
||||||
|
m_theme.debugPrint();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void onSignal(ostd::Signal& signal) override
|
inline void onSignal(ostd::Signal& signal) override
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue