Files
panopainter/engine/node_combobox.cpp

93 lines
2.9 KiB
C++

#include "pch.h"
#include "log.h"
#include "node_combobox.h"
#include "node_popup_menu.h"
Node* NodeComboBox::clone_instantiate() const
{
return new NodeComboBox;
}
void NodeComboBox::clone_copy(Node* dest) const
{
NodeButton::clone_copy(dest);
NodeComboBox* n = static_cast<NodeComboBox*>(dest);
n->m_data = m_data;
n->m_current_index = m_current_index;
}
void NodeComboBox::loaded()
{
NodeButton::loaded();
on_click = [this](Node* target) {
NodePopupMenu* popup = new NodePopupMenu;
popup->init();
popup->create();
popup->loaded();
root()->add_child(popup);
m_items.clear();
for (int i = 0; i < m_data.size(); i++)
{
if (m_data[i] == "-")
{
auto n = popup->add_child<NodeBorder>();
n->SetHeight(5.f);
n->SetWidthP(100.f);
n->m_color = {0, 0, 0, 1};
}
else
{
auto btn = popup->add_child<NodeButton>();
btn->m_text->set_text(m_data[i].c_str());
btn->m_border->SetWidthP(100.f);
btn->m_border->SetHeight(30.f);
int index = (int)m_items.size();
m_items.push_back(m_data[i]);
btn->on_click = [this,popup,btn,index](Node* target) {
m_current_index = index;
m_selected_child_index = popup->get_child_index(target);
m_text->set_text(m_items[index].c_str());
popup->mouse_release();
popup->destroy();
if (on_select)
on_select(btn, index);
};
}
}
float offset = 0;
for (int i = 0; i <= m_selected_child_index; i++)
offset += (m_data[i] == "-") ? 5.f : 30.f;
float height = m_items.size() * 30.f + (m_data.size() - m_items.size()) * 5.f; // add items and separators
glm::vec2 pos = m_pos + glm::vec2(0, m_size.y - offset);
auto screen = root()->m_size;
if ((pos.y + height) > screen.y) pos.y = screen.y - height;
if (pos.y < 0) pos.y = 0;
popup->SetPositioning(YGPositionTypeAbsolute);
popup->SetPosition(pos.x, pos.y);
popup->SetSize(m_size.x, YGUndefined);
popup->SetFlexGrow(1.f);
popup->update();
root()->update();
popup->mouse_capture();
popup->m_mouse_ignore = false;
popup->m_flood_events = true;
popup->m_capture_children = false;
};
}
void NodeComboBox::parse_attributes(kAttribute ka, const tinyxml2::XMLAttribute* attr)
{
NodeButton::parse_attributes(ka, attr);
switch (ka)
{
case kAttribute::ComboList:
{
m_data = split(attr->Value(), ',');
break;
}
case kAttribute::Default:
m_current_index = attr->IntValue();
break;
}
}