Added __i_stringeable interface to streamline the string conversion
protocol
This commit is contained in:
parent
fcb90c0916
commit
25e06eba0a
12 changed files with 66 additions and 65 deletions
1
.clangd
1
.clangd
|
|
@ -1,3 +1,2 @@
|
|||
CompileFlags:
|
||||
CompilationDatabase: bin
|
||||
Add: ["--target=x86_64-w64-mingw32"]
|
||||
|
|
@ -61,7 +61,6 @@ list(APPEND OSTD_SOURCE_FILES
|
|||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/io/Stylesheet.cpp
|
||||
|
||||
# math
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/math/Geometry.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/math/ShuntingYard.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/math/Random.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/src/ostd/math/SineWave.cpp
|
||||
|
|
|
|||
|
|
@ -305,12 +305,8 @@ namespace ogfx
|
|||
return ostd::Rectangle(getGlobalPosition(), getSize()).contains(p, includeBounds);
|
||||
}
|
||||
|
||||
void Widget::addThemeOverride(const ostd::String& key, ostd::Stylesheet::TypeVariant value, const ostd::String& themeID, const ostd::String& qualifier, bool propagate)
|
||||
void Widget::addThemeOverride(const ostd::String& fullKey, ostd::Stylesheet::TypeVariant value, bool propagate)
|
||||
{
|
||||
ostd::String fullKey = "@" + themeID;
|
||||
if (qualifier.new_trim() != "")
|
||||
fullKey += ":" + qualifier;
|
||||
fullKey += "." + key;
|
||||
m_themeOverrides.push_back({ fullKey, value, propagate });
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ namespace ogfx
|
|||
using ostd::Rectangle::contains;
|
||||
bool contains(ostd::Vec2 p, bool includeBounds = false) const override;
|
||||
virtual void applyTheme(const ostd::Stylesheet& theme) = 0;
|
||||
void addThemeOverride(const ostd::String& key, ostd::Stylesheet::TypeVariant value, const ostd::String& themeID = "", const ostd::String& qualifier = "", bool propagate = true);
|
||||
void addThemeOverride(const ostd::String& fullKey, ostd::Stylesheet::TypeVariant value, bool propagate = true);
|
||||
void reloadTheme(void);
|
||||
void setThemeQualifier(const ostd::String& qualifier, bool value = true);
|
||||
bool getThemeQualifier(const ostd::String& qualifier);
|
||||
|
|
|
|||
|
|
@ -25,12 +25,6 @@ namespace ostd
|
|||
return *this;
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const BaseObject& val)
|
||||
{
|
||||
out << val.toString();
|
||||
return out;
|
||||
}
|
||||
|
||||
void BaseObject::print(bool newLine, OutputHandlerBase* __destination) const
|
||||
{
|
||||
if (__destination == nullptr)
|
||||
|
|
|
|||
|
|
@ -21,14 +21,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <ostd/string/String.hpp>
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
class OutputHandlerBase;
|
||||
struct Signal;
|
||||
class BaseObject
|
||||
class BaseObject : public __i_stringeable
|
||||
{
|
||||
public:
|
||||
BaseObject(const BaseObject& copy);
|
||||
|
|
@ -56,7 +55,7 @@ namespace ostd
|
|||
inline bool signalsEnabled(void) { return m_signalsEnabled; }
|
||||
inline void enableSignals(bool e = true) { m_signalsEnabled = e; }
|
||||
|
||||
virtual inline String toString(void) const { return getObjectHeaderString(); };
|
||||
virtual inline String toString(void) const override { return getObjectHeaderString(); };
|
||||
virtual void print(bool newLine = true, OutputHandlerBase* __destination = nullptr) const;
|
||||
|
||||
virtual inline void handleSignal(Signal& signal) { }
|
||||
|
|
@ -64,8 +63,6 @@ namespace ostd
|
|||
|
||||
void __handle_signal(Signal& signal);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& os, const BaseObject& obj);
|
||||
|
||||
protected:
|
||||
inline BaseObject(void) { m_uid = -1; m_valid = false; m_oid = BaseObject::s_next_oid++; }
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -303,7 +303,27 @@ namespace ostd
|
|||
{
|
||||
for (const auto&[key, value] : m_values)
|
||||
{
|
||||
std::cout << key << "\n";
|
||||
std::cout << key << " = " << typeVariantToString(value) << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
ostd::String Stylesheet::typeVariantToString(const TypeVariant& v)
|
||||
{
|
||||
ostd::String str = "";
|
||||
// if (auto p = std::get_if<int32_t>(&v))
|
||||
// str.add(*p);
|
||||
// else if (auto p = std::get_if<float>(&v))
|
||||
// str.add(*p);
|
||||
// else if (auto p = std::get_if<bool>(&v))
|
||||
// str.add(STR_BOOL(*p));
|
||||
// else if (auto p = std::get_if<ostd::String>(&v))
|
||||
// str.add(*p);
|
||||
// else if (auto p = std::get_if<ostd::Color>(&v))
|
||||
// str.add(*p);
|
||||
// else if (auto p = std::get_if<ostd::Rectangle>(&v))
|
||||
// std::cout << std::format("{{ {}, {}, {}, {} }}", (*p).x, (*p).y, (*p).w, (*p).h);
|
||||
// else if (auto p = std::get_if<ostd::Vec2>(&v))
|
||||
// std::cout << *p;
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ namespace ostd
|
|||
}
|
||||
|
||||
void debugPrint(void);
|
||||
ostd::String typeVariantToString(const TypeVariant& v);
|
||||
|
||||
private:
|
||||
bool parseThemeFileLine(const ostd::String& line);
|
||||
|
|
|
|||
|
|
@ -1,14 +0,0 @@
|
|||
#include "Geometry.hpp"
|
||||
#include "../string/String.hpp"
|
||||
|
||||
namespace ostd
|
||||
{
|
||||
String Vec2::toString(void) const
|
||||
{
|
||||
return String("{ ").add(x).add(", ").add(y).add(" }");
|
||||
}
|
||||
String Vec3::toString(void) const
|
||||
{
|
||||
return String("{ ").add(x).add(", ").add(y).add(", ").add(z).add(" }");
|
||||
}
|
||||
}
|
||||
|
|
@ -45,9 +45,8 @@
|
|||
|
||||
namespace ostd
|
||||
{
|
||||
class OutputHandlerBase;
|
||||
template<class T>
|
||||
class Point
|
||||
class Point : public __i_stringeable
|
||||
{
|
||||
public:
|
||||
T x;
|
||||
|
|
@ -65,6 +64,8 @@ namespace ostd
|
|||
x = (T2)(copy.x);
|
||||
y = (T2)(copy.y);
|
||||
}
|
||||
|
||||
inline String toString(void) const override { return String("{ ").add(x).add(", ").add(y).add(" }"); }
|
||||
};
|
||||
|
||||
typedef Point<float> FPoint;
|
||||
|
|
@ -78,7 +79,7 @@ namespace ostd
|
|||
typedef Point<int16_t> I16Point;
|
||||
typedef Point<int8_t> I8Point;
|
||||
|
||||
struct Vec2
|
||||
struct Vec2 : public __i_stringeable
|
||||
{
|
||||
//======================== Data ========================
|
||||
float x;
|
||||
|
|
@ -103,7 +104,7 @@ namespace ostd
|
|||
//===================== Conversion =====================
|
||||
inline Vec2 toIsometric(void) const { return Vec2(x - y, (x + y) / 2.0f); }
|
||||
inline Vec2 toCartesian(void) const { return Vec2((2 * y + x) / 2.0f, (2 * y - x) / 2.0f); }
|
||||
String toString(void) const;
|
||||
inline String toString(void) const override { return String("{ ").add(x).add(", ").add(y).add(" }"); }
|
||||
//======================================================
|
||||
|
||||
|
||||
|
|
@ -159,8 +160,6 @@ namespace ostd
|
|||
inline Vec2& operator-=(const float& op2) { return subm(op2, op2); }
|
||||
inline Vec2& operator*=(const float& op2) { return mulm(op2); }
|
||||
inline Vec2& operator/=(const float& op2) { return divm(op2); }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const Vec2& val);
|
||||
//======================================================
|
||||
|
||||
private:
|
||||
|
|
@ -168,13 +167,7 @@ namespace ostd
|
|||
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const Vec2& val)
|
||||
{
|
||||
out << val.toString();
|
||||
return out;
|
||||
}
|
||||
|
||||
struct Vec3
|
||||
struct Vec3 : public __i_stringeable
|
||||
{
|
||||
inline Vec3(float xx = 0, float yy = 0, float zz = 0)
|
||||
{
|
||||
|
|
@ -186,25 +179,17 @@ namespace ostd
|
|||
inline Vec2 xy(void) const { return Vec2(x, y); }
|
||||
inline Vec2 yz(void) const { return Vec2(y, z); }
|
||||
inline Vec2 zx(void) const { return Vec2(z, x); }
|
||||
String toString(void) const;
|
||||
inline String toString(void) const override { return String("{ ").add(x).add(", ").add(y).add(", ").add(z).add(" }"); }
|
||||
inline bool operator==(const Vec3& op2 ) const { return (x == op2.x && y == op2.y && op2.z == z); }
|
||||
inline bool operator!=(const Vec3& op2 ) const { return (x != op2.x || y != op2.y || op2.z != z); }
|
||||
inline Vec3& operator+=(const Vec3& op2 ) { x += op2.x; y += op2.y; z += op2.z; return *this; }
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const Vec2& val);
|
||||
|
||||
float x;
|
||||
float y;
|
||||
float z;
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const Vec3& val)
|
||||
{
|
||||
out << val.toString();
|
||||
return out;
|
||||
}
|
||||
|
||||
struct Vec4
|
||||
struct Vec4 : public __i_stringeable
|
||||
{
|
||||
inline Vec4(float xx = 0, float yy = 0, float zz = 0, float ww = 0)
|
||||
{
|
||||
|
|
@ -220,7 +205,7 @@ namespace ostd
|
|||
inline Vec2 yz(void) const { return Vec2(y, z); }
|
||||
inline Vec2 zw(void) const { return Vec2(z, w); }
|
||||
inline Vec2 wx(void) const { return Vec2(w, x); }
|
||||
|
||||
inline String toString(void) const override { return String("{ ").add(x).add(", ").add(y).add(", ").add(z).add(", ").add(w).add(" }"); }
|
||||
|
||||
float x;
|
||||
float y;
|
||||
|
|
@ -228,7 +213,7 @@ namespace ostd
|
|||
float w;
|
||||
};
|
||||
|
||||
struct Triangle
|
||||
struct Triangle : public __i_stringeable
|
||||
{
|
||||
Vec2 vA;
|
||||
Vec2 vB;
|
||||
|
|
@ -249,7 +234,7 @@ namespace ostd
|
|||
vC.x = cx;
|
||||
vC.y = cy;
|
||||
}
|
||||
bool contains(Vec2 p)
|
||||
inline bool contains(Vec2 p)
|
||||
{
|
||||
float d1, d2, d3;
|
||||
bool has_neg, has_pos;
|
||||
|
|
@ -263,6 +248,7 @@ namespace ostd
|
|||
|
||||
return !(has_neg && has_pos);
|
||||
}
|
||||
inline String toString(void) const override { return String("{ A: ").add(vA.toString()).add(", B: ").add(vB.toString()).add(", C: ").add(vC.toString()).add(" }"); }
|
||||
|
||||
private:
|
||||
inline float __sign(Vec2 p1, Vec2 p2, Vec2 p3)
|
||||
|
|
@ -271,7 +257,7 @@ namespace ostd
|
|||
}
|
||||
};
|
||||
|
||||
class Rectangle
|
||||
class Rectangle : public __i_stringeable
|
||||
{
|
||||
public:
|
||||
inline Rectangle(void) : x(0), y(0), w(0), h(0) {}
|
||||
|
|
@ -350,6 +336,8 @@ namespace ostd
|
|||
inline virtual float top(void) const { return gety(); }
|
||||
inline virtual float bottom(void) const { return geth(); }
|
||||
|
||||
inline String toString(void) const override { return String("{ ").add(x).add(", ").add(y).add(", ").add(w).add(", ").add(h).add(" }"); }
|
||||
|
||||
inline virtual bool intersects(Rectangle rect, bool includeBounds = true) const
|
||||
{
|
||||
if (includeBounds)
|
||||
|
|
@ -397,13 +385,14 @@ namespace ostd
|
|||
};
|
||||
|
||||
template<class T>
|
||||
class Rect
|
||||
class Rect : public __i_stringeable
|
||||
{
|
||||
public:
|
||||
Rect(void) = default;
|
||||
inline Rect(T pos_x, T pos_y, T size_x, T size_y) : position(pos_x, pos_y), size(size_x, size_y) { }
|
||||
inline Rect(Point<T> pos, Point<T> _size) : position(pos), size(_size) { }
|
||||
template <class T2> inline Rect(Rect<T2> copy) { position = { (T2)(copy.position.x), (T2)(copy.position.y) }; size = { (T2)(copy.size.x), (T2)(copy.size.y) }; }
|
||||
inline String toString(void) const override { return String("{ ").add(position.x).add(", ").add(position.y).add(", ").add(size.x).add(", ").add(size.y).add(" }"); }
|
||||
|
||||
public:
|
||||
Point<T> position;
|
||||
|
|
@ -422,13 +411,14 @@ namespace ostd
|
|||
typedef Rect<int8_t> I8Rect;
|
||||
|
||||
template<class T>
|
||||
class Line
|
||||
class Line : public __i_stringeable
|
||||
{
|
||||
public:
|
||||
Line(void) = default;
|
||||
inline Line(T x1, T y1, T x2, T y2) : p1(x1, y1), p2(x2, y2) { }
|
||||
inline Line(Point<T> _p1, Point<T> _p2) : p1(_p1), p2(_p2) { }
|
||||
template <class T2> inline Line(Line<T2> copy) { p1 = { (T2)(copy.p1.x), (T2)(copy.p1.y) }; p2 = { (T2)(copy.p2.x), (T2)(copy.p2.y) }; }
|
||||
inline String toString(void) const override { return String("{ P1: ").add(p1.toString()).add(", P2").add(p2.toString()).add(" }"); }
|
||||
|
||||
public:
|
||||
Point<T> p1;
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@
|
|||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <iostream>
|
||||
#include <ostd/data/Types.hpp>
|
||||
|
||||
#define STR_BOOL(b) (b ? "true" : "false")
|
||||
|
|
@ -179,6 +180,20 @@ namespace ostd
|
|||
private:
|
||||
ostd::cpp_string m_data;
|
||||
};
|
||||
|
||||
struct __i_stringeable
|
||||
{
|
||||
virtual String toString(void) const = 0;
|
||||
friend std::ostream& operator<<(std::ostream& out, const __i_stringeable& val);
|
||||
inline operator String() const { return toString(); }
|
||||
inline operator std::string() const { return toString(); }
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const __i_stringeable& val)
|
||||
{
|
||||
out << val.toString();
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ 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);
|
||||
addWidget(m_label1);
|
||||
// m_label1.setThemeQualifier("disabled");
|
||||
|
||||
m_label2.setPosition(100, 400);
|
||||
|
|
@ -73,6 +73,10 @@ class Window : public ogfx::gui::Window
|
|||
|
||||
m_theme.loadFromFile("./testTheme.txt");
|
||||
setTheme(m_theme);
|
||||
|
||||
m_label2.addThemeOverride("@:hover.label.fontSize", 10);
|
||||
m_label2.reloadTheme();
|
||||
|
||||
m_theme.debugPrint();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue