started implementing dynamic widget allocation by xml tag

This commit is contained in:
2017-01-29 17:42:23 +00:00
parent 7436706b37
commit 16c1b6481e
10 changed files with 94 additions and 46 deletions

View File

@@ -109,8 +109,6 @@ void App::init()
void App::update(float dt)
{
glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f);
glClearColor(.1f, .1f, .1f, 1.f);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
@@ -129,23 +127,21 @@ void App::update(float dt)
glEnable(GL_SCISSOR_TEST);
for (auto& n : layout)
{
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
glm::mat4 scale = glm::scale(glm::vec3(n.m_size, 1.f));
glm::mat4 pos = glm::translate(glm::vec3(n.m_pos, 0));
auto mvp = proj * pos * scale * pivot;
auto box = n.m_clip;
glScissor(box.x, height - box.y - box.w, box.z, box.w);
shader_color.u_vec4("col", n.color);
shader_color.use();
shader_color.u_mat4("mvp", mvp);
plane.draw_fill();
shader_color.u_vec4("col", { 1, 1, 1, 1 });
shader_color.use();
shader_color.u_mat4("mvp", mvp);
plane.draw_stroke();
if (n.m_widget)
{
shader_color.u_vec4("col", n.color);
shader_color.use();
shader_color.u_mat4("mvp", n.m_widget->mvp);
plane.draw_fill();
shader_color.u_vec4("col", { 1, 1, 1, 1 });
shader_color.use();
shader_color.u_mat4("mvp", n.m_widget->mvp);
plane.draw_stroke();
}
}
glDisable(GL_SCISSOR_TEST);
tex.unbind();

View File

@@ -1,15 +1,19 @@
#include "pch.h"
#include "layout.h"
#include "util.hpp"
Plane* WidgetBorder::m_plane;
void Node::update(float width, float height)
{
YGNodeStyleSetWidth(y_node, width);
YGNodeStyleSetHeight(y_node, height);
YGNodeCalculateLayout(y_node, YGUndefined, YGUndefined, YGDirectionLTR);
update_internal({ 0, 0 });
glm::mat4 proj = glm::ortho(0.f, m_size.x, m_size.y, 0.f, -1.f, 1.f);
update_internal({ 0, 0 }, proj);
}
void Node::update_internal(glm::vec2 origin)
void Node::update_internal(const glm::vec2& origin, const glm::mat4& proj)
{
float x = YGNodeLayoutGetLeft(y_node);
float y = YGNodeLayoutGetTop(y_node);
@@ -17,12 +21,17 @@ void Node::update_internal(glm::vec2 origin)
float h = YGNodeLayoutGetHeight(y_node);
m_pos = origin + glm::vec2(x, y);
m_size = glm::vec2(w, h);
if (!parent)
m_clip = glm::vec4(m_pos, m_size);
else
m_clip = rect_intersection(glm::vec4(m_pos, m_size), parent->m_clip);
m_clip = !parent ? glm::vec4(m_pos, m_size) : rect_intersection(glm::vec4(m_pos, m_size), parent->m_clip);
if (m_widget)
{
glm::mat4 pivot = glm::translate(glm::vec3(.5f, .5f, 0.f));
glm::mat4 scale = glm::scale(glm::vec3(m_size, 1.f));
glm::mat4 pos = glm::translate(glm::vec3(m_pos, 0));
m_widget->mvp = proj * pos * scale * pivot;
m_widget->clip = m_clip;
}
for (auto& c : m_children)
c.update_internal(m_pos);
c.update_internal(m_pos, proj);
}
void Node::parse_attributes(att::kAttribute ka, const tinyxml2::XMLAttribute* attr)
@@ -179,9 +188,18 @@ void Node::load_internal(const tinyxml2::XMLElement* x_node)
{
m_name = x_node->Name();
auto attr = x_node->FirstAttribute();
att::kWidget widget_id = (att::kWidget)const_hash(x_node->Name());
switch (widget_id)
{
case att::kWidget::Border:
m_widget = std::make_unique<WidgetBorder>();
break;
}
while (attr)
{
parse_attributes((att::kAttribute)att::const_hash(attr->Name()), attr);
parse_attributes((att::kAttribute)const_hash(attr->Name()), attr);
attr = attr->Next();
}

View File

@@ -1,14 +1,9 @@
#pragma once
#include "shape.hpp"
#include "util.hpp"
namespace att
{
uint16_t constexpr const_hash(char const *input)
{
return *input ?
static_cast<uint16_t>(*input) + 33 * const_hash(input + 1) :
5381;
}
enum class kAttribute : uint16_t
{
Width = const_hash("width"),
@@ -107,13 +102,20 @@ namespace att
class Widget
{
public:
glm::mat4 mvp;
glm::vec4 clip;
virtual void draw() { };
};
class WidgetBorder : public Widget
{
public:
static class Plane* m_plane;
static Plane* m_plane;
virtual void draw() override
{
m_plane->draw_fill();
}
};
class Node
@@ -186,11 +188,12 @@ public:
}
void update(float width, float height);
void update_internal(glm::vec2 origin);
void update_internal(const glm::vec2& origin, const glm::mat4& proj);
void parse_attributes(att::kAttribute ka, const tinyxml2::XMLAttribute* attr);
void load(const char* path);
void load_internal(const tinyxml2::XMLElement* x_node);
void reload();
void draw();
class iterator
{

View File

@@ -80,18 +80,18 @@ void Shader::use()
{
glUseProgram(prog);
}
void Shader::u_vec4(std::string name, const glm::vec4& v)
void Shader::u_vec4(const char* name, const glm::vec4& v)
{
auto loc = glGetUniformLocation(prog, name.c_str());
auto loc = glGetUniformLocation(prog, name);
glUniform4fv(loc, 1, glm::value_ptr(v));
}
void Shader::u_mat4(std::string name, const glm::mat4& m)
void Shader::u_mat4(const char* name, const glm::mat4& m)
{
auto loc = glGetUniformLocation(prog, name.c_str());
auto loc = glGetUniformLocation(prog, name);
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(m));
}
void Shader::u_int(std::string name, int i)
void Shader::u_int(const char* name, int i)
{
auto loc = glGetUniformLocation(prog, name.c_str());
auto loc = glGetUniformLocation(prog, name);
glUniform1i(loc, i);
}

View File

@@ -1,4 +1,5 @@
#pragma once
#include "util.hpp"
class Shader
{
@@ -6,7 +7,19 @@ class Shader
public:
bool create(std::string vertex, std::string fragment);
void use();
void u_vec4(std::string name, const glm::vec4& v);
void u_mat4(std::string name, const glm::mat4& m);
void u_int(std::string name, int i);
void u_vec4(const char* name, const glm::vec4& v);
void u_mat4(const char* name, const glm::mat4& m);
void u_int(const char* name, int i);
};
enum class kShader : uint16_t
{
Color = const_hash("color"),
};
class ShaderManager
{
public:
static void use(kShader id);
static void use(const char* name);
};

View File

@@ -1,5 +1,4 @@
#pragma once
#include "layout.h"
class Shape
{
@@ -10,7 +9,7 @@ protected:
GLvoid* ioff[2]{ 0 };
struct vertex_t { glm::vec4 pos; glm::vec2 uvs; };
public:
att::AttrubutesMap attribs;
// att::AttrubutesMap attribs;
bool create_buffers(GLvoid* idx, GLvoid* vertices, int isize, int vsize);
void draw_fill() const;
void draw_stroke() const;
@@ -57,6 +56,7 @@ public:
create_impl(w, h, div, idx, vertices);
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
}
/*
bool create(att::Divisions divisions, att::Width w, att::Height h)
{
const int div = divisions.value;
@@ -80,6 +80,7 @@ public:
const auto d = get_attribute(att::Divisions(1));
return create(d, w, h);
}
*/
};
class RectShape : public Shape

3
engine/util.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include "pch.h"
#include "util.hpp"

8
engine/util.hpp Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
uint16_t constexpr const_hash(char const *input)
{
return *input ?
static_cast<uint16_t>(*input) + 33 * const_hash(input + 1) :
5381;
}