added right click, popup menu, mark for destroy

This commit is contained in:
2017-02-20 02:25:59 +00:00
parent 4e3f898d4b
commit ee5e4c2d98
8 changed files with 210 additions and 74 deletions

View File

@@ -39,12 +39,14 @@ enum class kAttribute : uint16_t
enum class kWidget : uint16_t
{
Border = const_hash("border"),
Shape = const_hash("shape"),
Text = const_hash("text"),
Image = const_hash("image"),
Button = const_hash("button"),
Ref = const_hash("ref"),
Border = const_hash("border"),
Shape = const_hash("shape"),
Text = const_hash("text"),
Image = const_hash("image"),
Button = const_hash("button"),
ButtonCustom = const_hash("button-custom"),
PopupMenu = const_hash("popup-menu"),
Ref = const_hash("ref"),
};
enum class kShapeType : uint16_t
@@ -90,23 +92,6 @@ public:
glm::vec2 m_pos;
};
class Widget
{
public:
glm::mat4 mvp;
glm::mat4 proj;
glm::mat4 scale;
glm::mat4 pos;
glm::vec4 clip;
bool m_mouse_inside = false;
virtual Widget* clone() = 0;
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 LayoutManager
{
std::map<uint16_t, std::unique_ptr<class Node>> m_layouts;
@@ -134,6 +119,8 @@ public:
glm::mat4 m_proj;
glm::mat4 m_mvp;
bool m_mouse_inside = false;
bool m_flood_events = false;
bool m_destroyed = false;
glm::vec2 m_pos;
glm::vec2 m_size;
@@ -213,6 +200,7 @@ public:
}
void update(float width, float height);
void update();
void update_internal(const glm::vec2& origin, const glm::mat4& proj);
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr);
void load_internal(const tinyxml2::XMLElement* x_node);
@@ -227,6 +215,7 @@ public:
for (auto& c : m_children)
c->watch(observer);
}
void destroy() { m_destroyed = true; }
template<class T = Node> T* find(const char* ids)
{
@@ -243,6 +232,7 @@ public:
virtual kEventResult handle_event(Event* e) { return kEventResult::Available; }
virtual void create() { }
virtual void init() { }
virtual void loaded() { }
void add_child(Node* n);
void remove_child(Node* n);
@@ -557,6 +547,12 @@ public:
m_border->create();
m_text->create();
}
virtual void loaded() override
{
m_border->m_thinkness = 1;
m_border->m_border_color = glm::vec4(0, 0, 0, 1);
m_border->m_color = color_normal;
}
virtual void parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr) override
{
switch (ka)
@@ -612,11 +608,6 @@ public:
Node* m_template;
NodeButton* btnOk;
virtual Node* clone_instantiate() const override { return new NodeMessageBox(); }
virtual void clone_copy(Node* dest) const override
{
Node::clone_copy(dest);
NodeMessageBox* n = static_cast<NodeMessageBox*>(dest);
}
virtual void init() override
{
SetPosition(0, 0);
@@ -626,23 +617,72 @@ public:
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 = [&] { parent->remove_child(this); };
// btnOk = (NodeButton*)n;
// btnOk->m_display = false;
}
virtual void create() override
{
}
virtual void draw() override
{
//m_template->draw();
}
// virtual kEventResult handle_event(Event* e) override
// {
// return kEventResult::Available;
// }
virtual kEventResult on_event(Event* e)
{
return Node::on_event(e);
btnOk->on_click = [&] { destroy(); };
}
};
class NodePopupMenu : public Node
{
public:
virtual Node* clone_instantiate() const override { return new NodePopupMenu(); }
virtual void init() override
{
m_flood_events = true;
SetPadding(10, 10, 10, 10);
SetPosition(0, 0);
SetWidth(100);
SetHeight(400);
SetPositioning(YGPositionTypeAbsolute);
}
virtual kEventResult handle_event(Event* e) override
{
return kEventResult::Consumed;
}
};
class NodeButtonCustom : public NodeBorder
{
public:
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 NodeButtonCustom(); }
virtual void clone_copy(Node* dest) const override
{
Node::clone_copy(dest);
NodeButtonCustom* n = static_cast<NodeButtonCustom*>(dest);
n->color_normal = color_normal;
n->color_hover = color_hover;
n->color_down = color_down;
}
virtual void loaded() override
{
m_thinkness = 1;
m_border_color = glm::vec4(0, 0, 0, 1);
m_color = color_normal;
}
virtual kEventResult handle_event(Event* e) override
{
switch (e->m_type)
{
case kEventType::MouseEnter:
m_color = color_hover;
break;
case kEventType::MouseLeave:
m_color = color_normal;
break;
case kEventType::MouseDownL:
m_color = color_down;
break;
case kEventType::MouseUpL:
m_color = color_normal;
if (m_mouse_inside && on_click != nullptr)
on_click();
break;
default:
break;
}
return kEventResult::Consumed;
}
};