Files
panopainter/src/node_checkbox.cpp

136 lines
3.4 KiB
C++

#include "pch.h"
#include "log.h"
#include "node_checkbox.h"
#include "texture.h"
Node* NodeCheckBox::clone_instantiate() const
{
return new NodeCheckBox();
}
void NodeCheckBox::clone_children(Node* dest) const
{
Node::clone_children(dest);
NodeCheckBox* n = static_cast<NodeCheckBox*>(dest);
n->m_outer = (NodeBorder*)n->m_children[0].get();
n->m_inner = (NodeBorder*)n->m_outer->m_children[0].get();
n->m_icon = n->m_inner->m_children.empty() ? nullptr : (NodeImage*)n->m_inner->m_children[0].get();
n->m_mouse_ignore = false;
n->m_icon_path = m_icon_path;
}
void NodeCheckBox::init()
{
Node::init();
m_outer = new NodeBorder();
m_inner = new NodeBorder();
add_child(m_outer);
m_outer->add_child(m_inner);
m_outer->init();
m_outer->m_color = { .4, .4, .4, 1 };
m_outer->SetAlign(YGAlignCenter);
m_outer->SetJustify(YGJustifyCenter);
m_outer->SetPadding(5, 5, 5, 5);
m_outer->SetWidthP(100);
m_outer->SetHeightP(100);
m_outer->m_mouse_ignore = false;
m_inner->init();
m_inner->SetWidthP(100);
m_inner->SetHeightP(100);
m_inner->m_border_color = glm::vec4(.8, .8, .8, 1);
m_inner->m_thinkness = 1;
m_inner->m_color = glm::vec4(.8, .8, .8, 1);
m_mouse_ignore = false;
}
void NodeCheckBox::create()
{
Node::create();
m_outer->create();
m_inner->create();
if (!m_icon_path.empty())
set_icon(m_icon_path);
}
kEventResult NodeCheckBox::handle_event(Event* e)
{
Node::handle_event(e);
switch (e->m_type)
{
case kEventType::MouseEnter:
break;
case kEventType::MouseLeave:
break;
case kEventType::MouseDownL:
break;
case kEventType::MouseUpL:
checked = !checked;
update_icon();
if (on_value_changed)
on_value_changed(this, checked);
break;
default:
return kEventResult::Available;
break;
}
return kEventResult::Consumed;
}
void NodeCheckBox::draw()
{
m_inner->m_color = checked ? glm::vec4(.4, .4, .4, 1) : glm::vec4(.8, .8, .8, 1);
Node::draw();
}
void NodeCheckBox::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
switch (ka)
{
case kAttribute::Padding:
m_outer->parse_attributes(ka, attr);
break;
case kAttribute::Icon:
m_icon_path = attr->Value();
break;
default:
Node::parse_attributes(ka, attr);
break;
}
}
void NodeCheckBox::set_icon(const std::string& icon_path)
{
if (icon_path.empty() || !TextureManager::load(icon_path.c_str()))
{
if (m_icon)
m_icon->destroy();
m_icon = nullptr;
return;
}
m_icon_path = icon_path;
update_icon();
}
void NodeCheckBox::set_value(bool checked, bool trigger_event)
{
this->checked = checked;
update_icon();
if (trigger_event && on_value_changed)
on_value_changed(this, checked);
}
void NodeCheckBox::update_icon()
{
if (m_icon_path.empty())
return;
auto& t = TextureManager::get(const_hash(m_icon_path.c_str()));
if (!m_icon)
m_icon = m_inner->add_child<NodeImage>();
auto region = checked ? glm::vec4(.5f, 0, 1, 1) : glm::vec4(0, 0, .5f, 1);
m_icon->m_region = region * glm::vec4(t.size(), t.size());
m_icon->m_use_atlas = true;
m_icon->set_image(m_icon_path);
m_icon->SetWidthP(100);
m_icon->SetHeightP(100);
}