added singleton shader manager, custom widget attribute forward, parse border thickness and color for Border widget, parse and cache uniform locations, remove unused attrubutes code
This commit is contained in:
169
engine/layout.h
169
engine/layout.h
@@ -1,104 +1,34 @@
|
||||
#pragma once
|
||||
#include "shape.hpp"
|
||||
#include "util.hpp"
|
||||
#include "shader.hpp"
|
||||
|
||||
namespace att
|
||||
enum class kAttribute : uint16_t
|
||||
{
|
||||
enum class kAttribute : uint16_t
|
||||
{
|
||||
Width = const_hash("width"),
|
||||
MinWidth = const_hash("max-width"),
|
||||
MaxWidth = const_hash("min-width"),
|
||||
Height = const_hash("height"),
|
||||
MinHeight = const_hash("min-height"),
|
||||
MaxHeight = const_hash("max-height"),
|
||||
Divisions = const_hash("divisions"),
|
||||
InnerRadius = const_hash("inner-radius"),
|
||||
OuterRadius = const_hash("outer-radius"),
|
||||
Grow = const_hash("grow"),
|
||||
Shrink = const_hash("shrink"),
|
||||
FlexDir = const_hash("dir"),
|
||||
FlexWrap = const_hash("wrap"),
|
||||
Padding = const_hash("pad"),
|
||||
Margin = const_hash("margin"),
|
||||
Color = const_hash("color"),
|
||||
};
|
||||
Width = const_hash("width"),
|
||||
MinWidth = const_hash("max-width"),
|
||||
MaxWidth = const_hash("min-width"),
|
||||
Height = const_hash("height"),
|
||||
MinHeight = const_hash("min-height"),
|
||||
MaxHeight = const_hash("max-height"),
|
||||
Divisions = const_hash("divisions"),
|
||||
InnerRadius = const_hash("inner-radius"),
|
||||
OuterRadius = const_hash("outer-radius"),
|
||||
Grow = const_hash("grow"),
|
||||
Shrink = const_hash("shrink"),
|
||||
FlexDir = const_hash("dir"),
|
||||
FlexWrap = const_hash("wrap"),
|
||||
Padding = const_hash("pad"),
|
||||
Margin = const_hash("margin"),
|
||||
Color = const_hash("color"),
|
||||
Thickness = const_hash("thickness"),
|
||||
BorderColor = const_hash("border-color"),
|
||||
};
|
||||
|
||||
enum class kWidget : uint16_t
|
||||
{
|
||||
Border = const_hash("border"),
|
||||
};
|
||||
|
||||
struct AttributeBase
|
||||
{
|
||||
kAttribute id;
|
||||
};
|
||||
|
||||
typedef std::map<att::kAttribute, std::unique_ptr<att::AttributeBase>> AttrubutesMap;
|
||||
|
||||
template<kAttribute T, typename V>
|
||||
struct Attribute : public AttributeBase
|
||||
{
|
||||
static const kAttribute static_id = T;
|
||||
V value;
|
||||
Attribute() : value{ 0 } { id = static_id; }
|
||||
Attribute(V v) : value(v) { id = static_id; }
|
||||
};
|
||||
|
||||
struct kAttributeMap
|
||||
{
|
||||
kAttribute value;
|
||||
const char* name;
|
||||
};
|
||||
static constexpr kAttributeMap map[] =
|
||||
{
|
||||
{ kAttribute::Width, "width" },
|
||||
{ kAttribute::MinWidth, "min-width" },
|
||||
{ kAttribute::MaxWidth, "max-width" },
|
||||
{ kAttribute::Height, "height" },
|
||||
{ kAttribute::MinHeight, "min-height" },
|
||||
{ kAttribute::MaxHeight, "max-height" },
|
||||
{ kAttribute::Divisions, "divisions" },
|
||||
{ kAttribute::InnerRadius, "inner-radius" },
|
||||
{ kAttribute::OuterRadius, "outer-radius" },
|
||||
{ kAttribute::Grow, "grow" },
|
||||
{ kAttribute::Shrink, "shrink" },
|
||||
{ kAttribute::FlexDir, "dir" },
|
||||
{ kAttribute::FlexWrap, "wrap" },
|
||||
{ kAttribute::Padding, "pad" },
|
||||
{ kAttribute::Margin, "margin" },
|
||||
{ kAttribute::Color, "color" },
|
||||
};
|
||||
|
||||
constexpr const char* string(const kAttribute value, int i = 0)
|
||||
{
|
||||
// no check on size, value MUST be found
|
||||
return (map[i].value == value) ? map[i].name : string(value, i + 1);
|
||||
}
|
||||
|
||||
#define DECLARE_ATTRIBUTE(N,T) \
|
||||
struct N : public Attribute<kAttribute::N,T> \
|
||||
{ using Attribute<kAttribute::N,T>::Attribute; };
|
||||
|
||||
DECLARE_ATTRIBUTE(Width, float);
|
||||
DECLARE_ATTRIBUTE(MinWidth, float);
|
||||
DECLARE_ATTRIBUTE(MaxWidth, float);
|
||||
DECLARE_ATTRIBUTE(Height, float);
|
||||
DECLARE_ATTRIBUTE(MinHeight, float);
|
||||
DECLARE_ATTRIBUTE(MaxHeight, float);
|
||||
DECLARE_ATTRIBUTE(Divisions, int);
|
||||
DECLARE_ATTRIBUTE(InnerRadius, float);
|
||||
DECLARE_ATTRIBUTE(OuterRadius, float);
|
||||
DECLARE_ATTRIBUTE(Grow, float);
|
||||
DECLARE_ATTRIBUTE(Shrink, float);
|
||||
DECLARE_ATTRIBUTE(FlexDir, int);
|
||||
DECLARE_ATTRIBUTE(FlexWrap, int);
|
||||
DECLARE_ATTRIBUTE(Padding, glm::vec4);
|
||||
DECLARE_ATTRIBUTE(Margin, glm::vec4);
|
||||
DECLARE_ATTRIBUTE(Color, glm::vec4);
|
||||
|
||||
#undef DECLARE_ATTRIBUTE
|
||||
}
|
||||
enum class kWidget : uint16_t
|
||||
{
|
||||
Border = const_hash("border"),
|
||||
};
|
||||
|
||||
class Widget
|
||||
{
|
||||
@@ -106,15 +36,56 @@ public:
|
||||
glm::mat4 mvp;
|
||||
glm::vec4 clip;
|
||||
virtual void draw() { };
|
||||
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) { };
|
||||
};
|
||||
|
||||
class WidgetBorder : public Widget
|
||||
{
|
||||
public:
|
||||
static Plane* m_plane;
|
||||
static Plane m_plane;
|
||||
glm::vec4 m_color;
|
||||
glm::vec4 m_border_color;
|
||||
float m_thinkness{ 1 };
|
||||
virtual void draw() override
|
||||
{
|
||||
m_plane->draw_fill();
|
||||
ShaderManager::use(kShader::Color);
|
||||
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
|
||||
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
|
||||
m_plane.draw_fill();
|
||||
|
||||
glLineWidth(m_thinkness);
|
||||
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
|
||||
m_plane.draw_stroke();
|
||||
}
|
||||
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr)
|
||||
{
|
||||
switch (id)
|
||||
{
|
||||
case kAttribute::Color:
|
||||
{
|
||||
glm::vec4 pad;
|
||||
int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
|
||||
if (n == 1)
|
||||
m_color = glm::vec4(pad.x);
|
||||
else
|
||||
m_color = pad;
|
||||
break;
|
||||
}
|
||||
case kAttribute::BorderColor:
|
||||
{
|
||||
glm::vec4 pad;
|
||||
int n = sscanf(attr->Value(), "%f %f %f %f", &pad.x, &pad.y, &pad.z, &pad.w);
|
||||
if (n == 1)
|
||||
m_border_color = glm::vec4(pad.x);
|
||||
else
|
||||
m_border_color = pad;
|
||||
break;
|
||||
}
|
||||
case kAttribute::Thickness:
|
||||
m_thinkness = attr->FloatValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -131,7 +102,6 @@ public:
|
||||
glm::vec2 m_pos;
|
||||
glm::vec2 m_size;
|
||||
glm::vec4 m_clip;
|
||||
glm::vec4 color;
|
||||
std::string m_name;
|
||||
Node(const Node&) = delete;
|
||||
Node& operator=(const Node&) = delete;
|
||||
@@ -143,7 +113,6 @@ public:
|
||||
c.parent = this;
|
||||
parent = o.parent;
|
||||
y_node = o.y_node;
|
||||
color = o.color;
|
||||
m_pos = o.m_pos;
|
||||
m_size = o.m_size;
|
||||
m_clip = o.m_clip;
|
||||
@@ -189,7 +158,7 @@ public:
|
||||
|
||||
void update(float width, float height);
|
||||
void update_internal(const glm::vec2& origin, const glm::mat4& proj);
|
||||
void parse_attributes(att::kAttribute ka, const tinyxml2::XMLAttribute* attr);
|
||||
void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr);
|
||||
void load(const char* path);
|
||||
void load_internal(const tinyxml2::XMLElement* x_node);
|
||||
void reload();
|
||||
|
||||
Reference in New Issue
Block a user