added button click event handler

This commit is contained in:
2017-02-12 01:25:20 +00:00
parent fb25ffb347
commit 0d3c48fd98
6 changed files with 54 additions and 67 deletions

View File

@@ -24,14 +24,14 @@
</layout>
<layout id="main">
<node dir="col" wrap="0" width="100%" height="100%" pad="5">
<border margin="0 0 5 0" pad="0 0 0 5" color=".3" width="100%" height="25" dir="row" align="center">
<text text="File" font-face="arial" font-size="11" margin="0 15 0 0"/>
<text text="Edit" font-face="arial" font-size="11" margin="0 15 0 0"/>
<text text="View" font-face="arial" font-size="11" margin="0 15 0 0"/>
<border margin="0 0 5 0" pad="3 0 3 3" color=".3" width="100%" height="25" dir="row" align="center">
<button height="100%" margin="0 5 0 0" text="File"/>
<button height="100%" margin="0 5 0 0" text="Edit"/>
<button height="100%" margin="0 5 0 0" text="View"/>
</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"/>
<button id="btn-close" 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"/>
@@ -48,7 +48,7 @@
<text text="atlas" font-face="arial" font-size="11"/>
</image>
</border>
<border width="70" margin="0 5 0 0" color=".2" thickness="2" border-color=".7">
<border width="60" pad="2" margin="0 5 0 0" color=".2" thickness="2" border-color=".7">
<image path="data/social.png" region="0 0 230 230" width="100%" height="100%" pad="5" align="center" justify="flex-end">
<text text="Facebook" font-face="arial" font-size="11"/>
</image>

View File

@@ -124,7 +124,10 @@ void App::init()
FontManager::load(kFont::Arial_30, ttf, 30);
layout.load("data/layout.xml");
//layout["main"].update(width, height);
if (auto* button = layout[main_id].find<NodeButton>("btn-close"))
{
button->on_click = [] { exit(0); };
}
sampler.create(GL_NEAREST);
ShaderManager::create(kShader::Texture, shader_v, shader_f);
@@ -164,8 +167,6 @@ void App::init()
void App::update(float dt)
{
constexpr auto main_id = const_hash("main");
glClearColor(.1f, .1f, .1f, 1.f);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glClear(GL_COLOR_BUFFER_BIT);
@@ -173,13 +174,10 @@ void App::update(float dt)
if (layout.reload())
{
layout[main_id].update(width, height);
//Node* check = layout[main_id].find("check");
//if (check)
// check->m_display = false;
}
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//glEnable(GL_SCISSOR_TEST);
glEnable(GL_SCISSOR_TEST);
for (auto& n : layout[main_id])
{
if (n.m_display)
@@ -190,32 +188,6 @@ void App::update(float dt)
}
}
glDisable(GL_SCISSOR_TEST);
glm::mat4 proj = glm::ortho(0.f, width, height, 0.f, -1.f, 1.f);
glm::mat4 tran = glm::translate(glm::vec3(200, 200, 0));
/*
static int i = 0;
i = (i + 1) % 32;
auto c = chars["hgfrr56789opk0876ryuewighfcuisdbn"[i] - 32];
glm::vec2 a(c.x0, c.y0);
glm::vec2 b(c.x1, c.y1);
glm::vec2 s(512, 512);
glm::vec2 tof = a / s;
glm::vec2 tsz = (b - a) / s;
glActiveTexture(GL_TEXTURE0);
font_tex.bind();
sampler.bind(0);
ShaderManager::use(kShader::Font);
ShaderManager::u_int(kShaderUniform::Tex, 0);
ShaderManager::u_mat4(kShaderUniform::MVP, proj * tran);
ShaderManager::u_vec2(kShaderUniform::Tof, tof);
ShaderManager::u_vec2(kShaderUniform::Tsz, tsz);
//plane.draw_fill();
font_tex.unbind();
sampler.unbind();
*/
}
void App::resize(float w, float h)
@@ -228,7 +200,6 @@ void App::resize(float w, float h)
void App::mouse_down(int button, float x, float y)
{
constexpr auto main_id = const_hash("main");
MouseEvent e;
e.m_type = kEventType::MouseDownL;
e.m_pos = { x, y };
@@ -237,7 +208,6 @@ void App::mouse_down(int button, float x, float y)
}
void App::mouse_move(float x, float y)
{
constexpr auto main_id = const_hash("main");
MouseEvent e;
e.m_type = kEventType::MouseMove;
e.m_pos = { x, y };
@@ -245,7 +215,6 @@ void App::mouse_move(float x, float y)
}
void App::mouse_up(int button, float x, float y)
{
constexpr auto main_id = const_hash("main");
MouseEvent e;
e.m_type = kEventType::MouseUpL;
e.m_pos = { x, y };

View File

@@ -11,6 +11,7 @@ class App
Sampler sampler;
Texture2D tex;
LayoutManager layout;
const uint16_t main_id = const_hash("main");
public:
static App I;
float width;

View File

@@ -6,17 +6,6 @@ Plane NodeBorder::m_plane;
Plane NodeImage::m_plane;
Sampler NodeImage::m_sampler;
Node* Node::find(const char* ids)
{
uint16_t id = const_hash(ids);
if (id == m_nodeID)
return this;
for (auto& c : *this)
if (c.m_nodeID == id)
return &c;
return nullptr;
}
kEventResult Node::on_event(Event* e)
{
for (auto& c : m_children)

View File

@@ -194,7 +194,18 @@ public:
Node* clone();
virtual Node* clone_instantiate() const;
virtual void clone_copy(Node* dest) const;
Node* find(const char* ids);
template<class T> T* find(const char* ids)
{
uint16_t id = const_hash(ids);
if (id == m_nodeID)
return static_cast<T*>(this);
for (auto& c : *this)
if (c.m_nodeID == id)
return static_cast<T*>(&c);
return nullptr;
}
kEventResult on_event(Event* e);
virtual kEventResult handle_event(Event* e) { return kEventResult::Available; }
virtual void create() { }
@@ -237,11 +248,11 @@ public:
class NodeBorder : public Node
{
public:
static Plane m_plane;
glm::vec4 m_color;
glm::vec4 m_border_color;
float m_thinkness{ 1 };
public:
static void static_init()
{
m_plane.create<1>(1, 1);
@@ -299,18 +310,6 @@ public:
ShaderManager::u_vec4(kShaderUniform::Col, m_border_color);
m_plane.draw_stroke();
}
virtual kEventResult handle_event(Event* e) override
{
switch (e->m_type)
{
case kEventType::MouseEnter:
m_color = rand_color();
break;
default:
break;
}
return kEventResult::Consumed;
}
};
class NodeText : public Node
@@ -462,6 +461,10 @@ class NodeButton : public Node
public:
NodeBorder* m_border;
NodeText* m_text;
glm::vec4 color_normal{ .1, .1, .1, 1 };
glm::vec4 color_hover{ .2, .2, .2, 1 };
glm::vec4 color_down{ .3, .3, .3, 1 };
std::function<void()> on_click;
virtual Node* clone_instantiate() const override { return new NodeImage(); }
virtual void clone_copy(Node* dest) const override
{
@@ -514,6 +517,29 @@ public:
m_border->draw();
m_text->draw();
}
virtual kEventResult handle_event(Event* e) override
{
switch (e->m_type)
{
case kEventType::MouseEnter:
m_border->m_color = color_hover;
break;
case kEventType::MouseLeave:
m_border->m_color = color_normal;
break;
case kEventType::MouseDownL:
m_border->m_color = color_down;
break;
case kEventType::MouseUpL:
m_border->m_color = color_normal;
if (m_mouse_inside && on_click != nullptr)
on_click();
break;
default:
break;
}
return kEventResult::Consumed;
}
};
class LayoutManager

View File

@@ -20,6 +20,8 @@
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_SWIZZLE