implement add and remove custom brushes
This commit is contained in:
@@ -47,10 +47,72 @@ Node* NodePanelBrush::clone_instantiate() const
|
||||
void NodePanelBrush::init()
|
||||
{
|
||||
init_template("tpl-panel-brushes");
|
||||
//m_layers_container = find<NodeBorder>("layers-container");
|
||||
static auto icons = Asset::list_files("data/thumbs", true, ".*\\.png$");
|
||||
|
||||
if ((m_container = find<NodeBorder>("brushes")))
|
||||
m_btn_add = find<NodeButtonCustom>("btn-add");
|
||||
m_btn_add->on_click = [this](Node*) {
|
||||
App::I.pick_image([this](std::string path) {
|
||||
Image img;
|
||||
if (img.load_file(path))
|
||||
{
|
||||
std::string name, base, ext;
|
||||
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
|
||||
std::smatch m;
|
||||
if (std::regex_search(path, m, r))
|
||||
{
|
||||
base = m[1].str();
|
||||
name = m[2].str();
|
||||
ext = m[3].str();
|
||||
|
||||
std::string path_high = App::I.data_path + "/brushes/" + name + ".png";
|
||||
std::string path_thumb = App::I.data_path + "/brushes/thumbs/" + name + ".png";
|
||||
auto thumb = img.resize(64, 64);
|
||||
thumb.save(path_thumb);
|
||||
img.save(path_high);
|
||||
|
||||
async_start();
|
||||
NodeButtonBrush* brush = new NodeButtonBrush;
|
||||
m_container->add_child(brush);
|
||||
brush->init();
|
||||
brush->create();
|
||||
brush->loaded();
|
||||
brush->set_icon(path.c_str());
|
||||
brush->thumb_path = path_thumb;
|
||||
brush->high_path = path_high;
|
||||
brush->brush_name = name;
|
||||
brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1);
|
||||
|
||||
app_redraw();
|
||||
async_end();
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
m_btn_remove = find<NodeButtonCustom>("btn-remove");
|
||||
m_btn_remove->on_click = [this](Node*) {
|
||||
if (m_current)
|
||||
{
|
||||
int idx = m_container->get_child_index(m_current);
|
||||
Asset::delete_file(m_current->thumb_path);
|
||||
Asset::delete_file(m_current->high_path);
|
||||
m_container->remove_child(m_current);
|
||||
if (m_container->m_children.size() > 0)
|
||||
{
|
||||
idx = std::max(0, std::min(idx, (int)m_container->m_children.size() - 1));
|
||||
m_current = (NodeButtonBrush*)m_container->m_children[idx].get();
|
||||
m_current->m_selected = true;
|
||||
if (on_brush_changed)
|
||||
on_brush_changed(this, idx);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_current = nullptr;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if ((m_container = find<NodeScroll>("brushes")))
|
||||
{
|
||||
int count = 0;
|
||||
for (auto& i : icons)
|
||||
@@ -63,12 +125,26 @@ void NodePanelBrush::init()
|
||||
brush->create();
|
||||
brush->loaded();
|
||||
brush->set_icon(path.c_str());
|
||||
brush->m_brushID = count++;
|
||||
brush->thumb_path = path;
|
||||
brush->high_path = path_hi;
|
||||
brush->brush_name = i;
|
||||
brush->high_id = const_hash(path_hi.c_str());
|
||||
m_brushes.push_back(brush);
|
||||
brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1);
|
||||
}
|
||||
|
||||
auto custom_icons = Asset::list_files(App::I.data_path + "/brushes", true, ".*\\.png$");
|
||||
for (auto& i : custom_icons)
|
||||
{
|
||||
std::string path_thumb = App::I.data_path + "/brushes/thumbs/" + i;
|
||||
std::string path_high = App::I.data_path + "/brushes/" + i;
|
||||
NodeButtonBrush* brush = new NodeButtonBrush;
|
||||
m_container->add_child(brush);
|
||||
brush->init();
|
||||
brush->create();
|
||||
brush->loaded();
|
||||
brush->set_icon(path_thumb.c_str());
|
||||
brush->thumb_path = path_thumb;
|
||||
brush->high_path = path_high;
|
||||
brush->brush_name = i;
|
||||
brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1);
|
||||
}
|
||||
}
|
||||
@@ -103,46 +179,45 @@ void NodePanelBrush::handle_click(Node* target)
|
||||
m_current = (NodeButtonBrush*)target;
|
||||
m_current->m_selected = true;
|
||||
if (on_brush_changed)
|
||||
on_brush_changed(this, m_current->m_brushID);
|
||||
on_brush_changed(this, m_container->get_child_index(target));
|
||||
}
|
||||
|
||||
int NodePanelBrush::find_brush(const std::string & name) const
|
||||
{
|
||||
for (int i = 0; i < m_brushes.size(); i++)
|
||||
for (int i = 0; i < m_container->m_children.size(); i++)
|
||||
{
|
||||
if (m_brushes[i]->brush_name.find(name) != std::string::npos)
|
||||
{
|
||||
NodeButtonBrush* b = (NodeButtonBrush*)m_container->m_children[i].get();
|
||||
if (b->brush_name.find(name) != std::string::npos)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string NodePanelBrush::get_texture_path(int index) const
|
||||
{
|
||||
return m_brushes[index]->high_path;
|
||||
return ((NodeButtonBrush*)m_container->m_children[index].get())->high_path;
|
||||
}
|
||||
|
||||
std::string NodePanelBrush::get_thumb_path(int index) const
|
||||
{
|
||||
return m_brushes[index]->thumb_path;
|
||||
return ((NodeButtonBrush*)m_container->m_children[index].get())->thumb_path;
|
||||
}
|
||||
|
||||
// select the current brush based on the texture id
|
||||
void NodePanelBrush::select_brush(int brush_id)
|
||||
{
|
||||
if (m_current)
|
||||
m_current->m_selected = false;
|
||||
for (auto b : m_brushes)
|
||||
{
|
||||
if (b->m_brushID == brush_id)
|
||||
{
|
||||
b->m_selected = true;
|
||||
m_current = b;
|
||||
TextureManager::load(b->high_path.c_str(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
//void NodePanelBrush::select_brush(int brush_id)
|
||||
//{
|
||||
// if (m_current)
|
||||
// m_current->m_selected = false;
|
||||
// for (auto b : m_brushes)
|
||||
// {
|
||||
// if (b->m_brushID == brush_id)
|
||||
// {
|
||||
// b->m_selected = true;
|
||||
// m_current = b;
|
||||
// TextureManager::load(b->high_path.c_str(), true);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user