add viewport node, return ptr instead of ref for layout[id] to check wether is valid or not

This commit is contained in:
2017-03-02 21:40:36 +00:00
parent 75f1b96b4f
commit 8242db8815
5 changed files with 148 additions and 38 deletions

View File

@@ -49,6 +49,7 @@ enum class kWidget : uint16_t
Button = const_hash("button"),
ButtonCustom = const_hash("button-custom"),
PopupMenu = const_hash("popup-menu"),
Viewport = const_hash("viewport"),
Ref = const_hash("ref"),
};
@@ -104,7 +105,7 @@ public:
std::function<void()> on_loaded;
bool load(const char* path);
bool reload();
class Node& operator[](uint16_t id) { return *m_layouts[id]; }
class Node* operator[](uint16_t id) { return m_layouts[id].get(); }
//Node& operator[](const char* ids) { return m_layouts[const_hash(ids)]; }
};
@@ -617,7 +618,7 @@ public:
SetWidthP(100);
SetHeightP(100);
SetPositioning(YGPositionTypeAbsolute);
m_template = (*m_manager)[const_hash("message-box")].m_children[0]->clone();
m_template = (*m_manager)[const_hash("message-box")]->m_children[0]->clone();
add_child(m_template);
btnOk = m_template->find<NodeButton>("btn-ok");
btnOk->on_click = [&] { destroy(); };
@@ -703,7 +704,7 @@ public:
SetWidthP(100);
SetHeightP(100);
SetPositioning(YGPositionTypeAbsolute);
m_template = (*m_manager)[const_hash("settings")].m_children[0]->clone();
m_template = (*m_manager)[const_hash("settings")]->m_children[0]->clone();
add_child(m_template);
btnOk = m_template->find<NodeButton>("btn-ok");
btnOk->on_click = [&] { destroy(); };
@@ -764,3 +765,55 @@ public:
}
}
};
class NodeViewport : public Node
{
public:
std::unique_ptr<Plane> m_faces;
std::unique_ptr<Sampler> m_sampler;
uint16_t m_tex_id;
virtual void draw() override
{
static float angle = 0;
angle += 0.05;
glm::mat4 cam = glm::lookAt(glm::vec3(sinf(angle)*10, 0, -10), glm::vec3(0, 0, 0), glm::vec3(0, -1, 0));
glm::mat4 proj = glm::perspective<float>(glm::radians(45.f), m_clip.z / m_clip.w, .1f, 100);
GLint vp[4];
GLfloat cc[4];
glGetIntegerv(GL_VIEWPORT, vp);
glGetFloatv(GL_COLOR_CLEAR_VALUE, cc);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
auto box = m_clip;
glViewport(box.x, (int)(vp[3] - box.y - box.w), box.z, box.w);
TextureManager::get(m_tex_id).bind();
m_sampler->bind(0);
glEnable(GL_BLEND);
ShaderManager::use(kShader::Texture);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, proj * cam);
m_faces->draw_fill();
m_sampler->unbind();
TextureManager::get(m_tex_id).unbind();
glDisable(GL_BLEND);
glViewport(vp[0], vp[1], vp[2], vp[3]);
glClearColor(cc[0], cc[1], cc[2], cc[3]);
}
virtual Node* clone_instantiate() const override
{
return new NodeViewport;
}
virtual void create() override
{
m_faces = std::make_unique<Plane>();
m_faces->create<1>(10, 10);
m_sampler = std::make_unique<Sampler>();
m_sampler->create();
TextureManager::load("data/uvs.jpg");
m_tex_id = const_hash("data/uvs.jpg");
}
};