added NodeButton as composition and cleaned up old Widget code

This commit is contained in:
2017-02-11 15:51:22 +00:00
parent b1abb42dd5
commit fb25ffb347
4 changed files with 113 additions and 378 deletions

View File

@@ -31,6 +31,7 @@
</border>
<!-- toolbar -->
<border height="50" width="100%" pad="5" dir="row" color=".2">
<button width="50" height="100%" margin="0 5 0 0" text="Button" color=".1" thickness="2" border-color=".5"/>
<ref id="multi-button"/>
<ref id="multi-button"/>
<ref id="multi-button"/>

View File

@@ -132,8 +132,8 @@ void App::init()
ShaderManager::create(kShader::UVs, shader_v, shader_uv_f);
ShaderManager::create(kShader::Font, shader_font_v, shader_font_f);
ShaderManager::create(kShader::Atlas, shader_atlas_v, shader_atlas_f);
NodeBorder::init();
NodeImage::init();
NodeBorder::static_init();
NodeImage::static_init();
if (!tex.load("data/uvs.jpg"))
printf("error loading image\n");

View File

@@ -65,6 +65,14 @@ kEventResult Node::on_event(Event* e)
return kEventResult::Available;
}
void Node::add_child(Node* n)
{
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
}
void Node::update(float width, float height)
{
YGNodeStyleSetWidth(y_node, width);
@@ -263,7 +271,9 @@ void Node::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
void Node::load_internal(const tinyxml2::XMLElement* x_node)
{
m_name = x_node->Name();
init();
auto attr = x_node->FirstAttribute();
while (attr)
{
@@ -282,54 +292,46 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node)
case kWidget::Border:
{
auto n = new NodeBorder();
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
add_child(n);
n->load_internal(x_child);
break;
}
// case kWidget::Shape:
// break;
case kWidget::Image:
{
auto n = new NodeImage();
add_child(n);
n->load_internal(x_child);
break;
}
case kWidget::Text:
{
auto n = new NodeText();
add_child(n);
n->load_internal(x_child);
break;
}
case kWidget::Button:
{
auto n = new NodeButton();
add_child(n);
n->load_internal(x_child);
break;
}
// case kWidget::Shape:
// break;
case kWidget::Image:
{
auto n = new NodeImage();
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->load_internal(x_child);
break;
}
case kWidget::Text:
{
auto n = new NodeText();
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
n->load_internal(x_child);
break;
}
case kWidget::Ref:
{
auto ids = x_child->Attribute("id");
auto id = const_hash(ids);
auto& ref = (*m_manager)[id];
auto n = ref.clone();
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
auto& ref = (*m_manager)[id].m_children[0];
auto n = ref->clone();
add_child(n);
break;
}
default:
{
auto n = new Node();
m_children.emplace_back(n);
n->parent = this;
n->m_manager = m_manager;
YGNodeInsertChild(y_node, n->y_node, YGNodeGetChildCount(y_node));
add_child(n);
n->load_internal(x_child);
break;
}

View File

@@ -41,6 +41,7 @@ enum class kWidget : uint16_t
Shape = const_hash("shape"),
Text = const_hash("text"),
Image = const_hash("image"),
Button = const_hash("button"),
Ref = const_hash("ref"),
};
@@ -103,338 +104,6 @@ public:
virtual void update() { }
virtual kEventResult on_event(Event* e) { return kEventResult::Available; }
};
/*
class WidgetBorder : public Widget
{
public:
static Plane m_plane;
glm::vec4 m_color;
glm::vec4 m_border_color;
float m_thinkness{ 1 };
static void init()
{
m_plane.create<1>(1, 1);
}
virtual Widget* clone() override
{
WidgetBorder* ret = new WidgetBorder();
*ret = *this;
return ret;
}
virtual void draw() override
{
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) override
{
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;
default:
break;
}
}
virtual kEventResult on_event(Event* e)
{
switch (e->m_type)
{
case kEventType::MouseEnter:
m_color = rand_color();
break;
default:
break;
}
return kEventResult::Consumed;
}
};
class WidgetShape : public Widget
{
public:
std::unique_ptr<Shape> m_shape;
kShapeType m_type;
glm::vec4 m_color;
glm::vec4 m_border_color;
float m_thinkness{ 1 };
float m_radius{ .5f };
virtual Widget* clone() override
{
WidgetShape* ret = new WidgetShape();
//*ret = *this;
return ret;
}
virtual void create() override
{
switch (m_type)
{
case kShapeType::Quad:
m_shape = std::make_unique<Plane>();
((Plane*)m_shape.get())->create<1>(1, 1);
break;
case kShapeType::Poly:
m_shape = std::make_unique<Circle>();
((Circle*)m_shape.get())->create<5>(m_radius);
break;
case kShapeType::RoundRect:
m_shape = std::make_unique<Rounded>();
((Rounded*)m_shape.get())->create<3>(1, 1, .1f);
break;
case kShapeType::Slice9:
m_shape = std::make_unique<Slice9>();
((Slice9*)m_shape.get())->create(1, 1, .1f, .1f);
break;
default:
break;
}
}
virtual void draw() override
{
ShaderManager::use(kShader::Color);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
m_shape->draw_fill();
glLineWidth(m_thinkness);
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
m_shape->draw_stroke();
}
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override
{
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;
case kAttribute::Type:
m_type = (kShapeType)const_hash(attr->Value());
break;
default:
break;
}
}
};
class WidgetText : public Widget
{
public:
TextMesh m_text_mesh;
std::string m_text;
std::string m_font;
glm::vec4 m_color{ 1, 1, 1, 1 };
int m_size;
virtual Widget* clone() override
{
WidgetText* ret = new WidgetText();
*ret = *this;
return ret;
}
virtual void create() override
{
char font[64];
sprintf(font, "%s-%d", m_font.c_str(), m_size);
kFont font_id = (kFont)const_hash(font);
m_text_mesh.create();
m_text_mesh.update(font_id, m_text.c_str());
}
virtual void draw() override
{
ShaderManager::use(kShader::Font);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
ShaderManager::u_vec4(kShaderUniform::Col, m_color);
glEnable(GL_BLEND);
m_text_mesh.draw();
glDisable(GL_BLEND);
}
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override
{
switch (ka)
{
case kAttribute::Text:
m_text = attr->Value();
break;
case kAttribute::FontFace:
m_font = attr->Value();
break;
case kAttribute::FontSize:
m_size = attr->IntValue();
break;
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;
}
default:
break;
}
}
virtual void update() override
{
mvp = proj * pos;
}
virtual kEventResult on_event(Event* e)
{
//m_color = rand_color();
return kEventResult::Consumed;
}
};
class WidgetImage : public Widget
{
public:
static Plane m_plane;
static Sampler m_sampler;
bool m_use_atlas;
glm::vec4 m_region;
glm::vec2 m_off;
glm::vec2 m_sz;
std::string m_path;
uint16_t m_id;
static void init()
{
m_plane.create<1>(1, 1);
m_sampler.create();
}
virtual Widget* clone() override
{
WidgetImage* ret = new WidgetImage();
*ret = *this;
return ret;
}
virtual void create() override
{
if (TextureManager::load(m_path.c_str()))
{
auto tex_sz = TextureManager::get(m_id).size();
m_off = m_region.xy / tex_sz;
m_sz = (m_region.zw - m_region.xy) / tex_sz;
}
}
virtual void draw() override
{
TextureManager::get(m_id).bind();
m_sampler.bind(0);
glEnable(GL_BLEND);
if (m_use_atlas)
{
ShaderManager::use(kShader::Atlas);
ShaderManager::u_vec2(kShaderUniform::Tof, m_off);
ShaderManager::u_vec2(kShaderUniform::Tsz, m_sz);
}
else
{
ShaderManager::use(kShader::Texture);
}
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, mvp);
m_plane.draw_fill();
m_sampler.unbind();
TextureManager::get(m_id).unbind();
glDisable(GL_BLEND);
}
virtual void parse_attributes(kAttribute id, const tinyxml2::XMLAttribute* attr) override
{
switch (id)
{
case kAttribute::Path:
m_path = attr->Value();
m_id = const_hash(attr->Value());
break;
case kAttribute::Region:
{
glm::vec4 v;
int n = sscanf(attr->Value(), "%f %f %f %f", &v.x, &v.y, &v.z, &v.w);
if (n == 4)
{
m_region = v;
m_use_atlas = true;
}
break;
}
default:
break;
}
}
virtual kEventResult on_event(Event* e)
{
MouseEvent* me = static_cast<MouseEvent*>(e);
return kEventResult::Available;
}
};
class WidgetButton : public Widget
{
public:
std::unique_ptr<WidgetBorder> m_border;
std::unique_ptr<WidgetText> m_text;
virtual Widget* clone()
{
WidgetButton* ret = new WidgetButton();
ret->m_border.reset((WidgetBorder*)m_border->clone());
return std::move(ret);
}
virtual void create() { }
virtual void draw() { }
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) { }
virtual void update() { }
virtual kEventResult on_event(Event* e) { return kEventResult::Available; }
};
*/
class Node
{
@@ -504,6 +173,8 @@ public:
void SetFlexShrink(float value) { YGNodeStyleSetFlexShrink(y_node, value); }
void SetFlexDir(YGFlexDirection value) { YGNodeStyleSetFlexDirection(y_node, value); }
void SetFlexWrap(YGWrap value) { YGNodeStyleSetFlexWrap(y_node, value); }
void SetJustify(YGJustify value) { YGNodeStyleSetJustifyContent(y_node, value); }
void SetAlign(YGAlign value) { YGNodeStyleSetAlignItems(y_node, value); }
glm::vec4 rect_intersection(glm::vec4 a, glm::vec4 b)
{
@@ -526,7 +197,9 @@ public:
Node* find(const char* ids);
kEventResult on_event(Event* e);
virtual kEventResult handle_event(Event* e) { return kEventResult::Available; }
virtual void create() { };
virtual void create() { }
virtual void init() { }
void add_child(Node* n);
class iterator
{
@@ -569,7 +242,7 @@ class NodeBorder : public Node
glm::vec4 m_border_color;
float m_thinkness{ 1 };
public:
static void init()
static void static_init()
{
m_plane.create<1>(1, 1);
}
@@ -642,12 +315,12 @@ public:
class NodeText : public Node
{
public:
TextMesh m_text_mesh;
std::string m_text;
std::string m_font;
glm::vec4 m_color{ 1, 1, 1, 1 };
int m_size;
public:
int m_font_size;
virtual Node* clone_instantiate() const override { return new NodeText(); }
virtual void clone_copy(Node* dest) const override
{
@@ -657,7 +330,7 @@ public:
virtual void create() override
{
char font[64];
sprintf(font, "%s-%d", m_font.c_str(), m_size);
sprintf(font, "%s-%d", m_font.c_str(), m_font_size);
kFont font_id = (kFont)const_hash(font);
m_text_mesh.create();
m_text_mesh.update(font_id, m_text.c_str());
@@ -675,7 +348,7 @@ public:
m_font = attr->Value();
break;
case kAttribute::FontSize:
m_size = attr->IntValue();
m_font_size = attr->IntValue();
break;
case kAttribute::Color:
{
@@ -716,7 +389,7 @@ public:
glm::vec2 m_sz;
std::string m_path;
uint16_t m_id;
static void init()
static void static_init()
{
m_plane.create<1>(1, 1);
m_sampler.create();
@@ -784,6 +457,65 @@ public:
}
};
class NodeButton : public Node
{
public:
NodeBorder* m_border;
NodeText* m_text;
virtual Node* clone_instantiate() const override { return new NodeImage(); }
virtual void clone_copy(Node* dest) const override
{
Node::clone_copy(dest);
NodeImage* n = static_cast<NodeImage*>(dest);
}
virtual void init() override
{
m_border = new NodeBorder();
m_text = new NodeText();
add_child(m_border);
m_border->add_child(m_text);
m_border->init();
m_text->init();
m_text->m_font = "arial";
m_text->m_font_size = 11;
m_border->SetAlign(YGAlignCenter);
m_border->SetJustify(YGJustifyCenter);
}
virtual void create() override
{
m_border->create();
m_text->create();
}
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override
{
Node::parse_attributes(ka, attr);
switch (ka)
{
case kAttribute::Width:
case kAttribute::Height:
case kAttribute::Color:
case kAttribute::Thickness:
case kAttribute::BorderColor:
m_border->parse_attributes(ka, attr);
break;
case kAttribute::Text:
case kAttribute::FontFace:
case kAttribute::FontSize:
m_text->parse_attributes(ka, attr);
break;
default:
break;
}
// m_border->parse_attributes(ka, attr);
// m_text->parse_attributes(ka, attr);
}
virtual void draw() override
{
m_border->draw();
m_text->draw();
}
};
class LayoutManager
{
std::map<uint16_t, std::unique_ptr<Node>> m_layouts;