add progress to export/import ppbr

This commit is contained in:
2019-09-06 09:34:59 +02:00
parent 20ac4cdbe1
commit 003787c974
2 changed files with 96 additions and 67 deletions

View File

@@ -441,6 +441,8 @@ void NodePanelBrushPreset::init()
*/ */
m_btn_delete = find<NodeButtonCustom>("btn-remove"); m_btn_delete = find<NodeButtonCustom>("btn-remove");
m_btn_delete->on_click = [this](Node*) { m_btn_delete->on_click = [this](Node*) {
auto pb = App::I->show_progress("Exporting ABR");
return;
if (!m_current) if (!m_current)
return; return;
int index = m_container->get_child_index(m_current); int index = m_container->get_child_index(m_current);
@@ -473,33 +475,18 @@ void NodePanelBrushPreset::init()
App::I->pick_file({"abr", "ppbr"}, [this] (std::string path) { App::I->pick_file({"abr", "ppbr"}, [this] (std::string path) {
std::thread([this, path] { std::thread([this, path] {
BT_SetTerminate(); BT_SetTerminate();
import_brush(path);
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return;
std::string base = m[1].str();
std::string name = m[2].str();
std::string ext = m[3].str();
if (ext == "ppbr")
{
import_ppbr(path);
}
else
{
import_abr(path);
}
App::I->ui_task([this] {
m_notification->SetVisibility(m_container->m_children.size() == 0); m_notification->SetVisibility(m_container->m_children.size() == 0);
});
}).detach(); }).detach();
}); });
break; break;
case 1: // export file case 1: // export file
App::I->pick_file_save({ "ppbr" }, [this] (std::string path) { App::I->pick_file_save({ "ppbr" }, [this] (std::string path) {
std::thread([this, path] {
BT_SetTerminate();
export_ppbr(path, {}); export_ppbr(path, {});
App::I->message_box("Export PPBR", "Brushes exported to:\n" + path);
}).detach();
}); });
break; break;
case 2: // download case 2: // download
@@ -515,7 +502,7 @@ void NodePanelBrushPreset::init()
App::I->pick_file({ "abr", "ppbr" }, [this](std::string path) { App::I->pick_file({ "abr", "ppbr" }, [this](std::string path) {
std::thread([this, path] { std::thread([this, path] {
BT_SetTerminate(); BT_SetTerminate();
import_abr(path); import_brush(path);
m_notification->SetVisibility(m_container->m_children.size() == 0); m_notification->SetVisibility(m_container->m_children.size() == 0);
}).detach(); }).detach();
}); });
@@ -639,21 +626,7 @@ bool NodePanelBrushPreset::restore()
if (b->valid()) if (b->valid())
{ {
App::I->ui_task([this, b] { add_brush(b);
NodeBrushPresetItem* brush = new NodeBrushPresetItem;
m_container->add_child(brush);
brush->init();
brush->create();
brush->loaded();
brush->thumb_path = b->m_brush_thumb_path;
brush->high_path = b->m_brush_path;
brush->m_brush = b;
brush->m_preview->m_brush = b;
brush->m_preview->draw_stroke();
brush->m_caption_size->set_text_format("%d", (int)b->m_tip_size);
brush->m_thumb->set_image(brush->m_brush->m_brush_thumb_path);
brush->on_click = std::bind(&NodePanelBrushPreset::handle_click, this, std::placeholders::_1);
});
} }
} }
m_notification->SetVisibility(m_container->m_children.size() == 0); m_notification->SetVisibility(m_container->m_children.size() == 0);
@@ -693,24 +666,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea
sw.wu16(0); sw.wu16(0);
sw.wu16(1); sw.wu16(1);
// header image // count list of images
sw << header_image;
// create previews
sw.wu32((int)m_container->m_children.size());
auto pr = std::make_unique<NodeStrokePreview>();
pr->m_preview_size = pr->m_size = { 256, 128 };
for (auto& c : m_container->m_children)
{
auto bpi = std::static_pointer_cast<NodeBrushPresetItem>(c);
pr->m_brush = std::make_shared<Brush>(*bpi->m_brush); // create copy
pr->m_brush->load();
Image img = pr->render_to_image();
img.file_name = pr->m_brush->m_name;
sw << img;
}
// list of images
std::set<std::string> img_brushes; std::set<std::string> img_brushes;
std::set<std::string> img_patterns; std::set<std::string> img_patterns;
for (auto& c : m_container->m_children) for (auto& c : m_container->m_children)
@@ -724,6 +680,43 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea
img_patterns.insert(bpi->m_brush->m_pattern_path); img_patterns.insert(bpi->m_brush->m_pattern_path);
} }
Serializer::Descriptor info;
info.class_id = "ppbr_info";
info.name = L"info header";
bool has_header_image = header_image.m_data != nullptr;
info.props["has_header_image"] = std::make_shared<Serializer::Boolean>(has_header_image);
info.props["num_brush_tips"] = std::make_shared<Serializer::Integer>(img_brushes.size());
info.props["num_brush_patt"] = std::make_shared<Serializer::Integer>(img_patterns.size());
info.props["num_brushes"] = std::make_shared<Serializer::Integer>(m_container->m_children.size());
auto pb = App::I->show_progress("Exporting ABR", 1 + img_brushes.size() +
img_patterns.size() + m_container->m_children.size() * 2);
sw << info;
// header image
if (has_header_image)
sw << header_image;
pb->increment();
// create previews
sw.wu32((int)m_container->m_children.size());
auto pr = std::make_unique<NodeStrokePreview>();
pr->m_preview_size = pr->m_size = { 256, 128 };
for (auto& c : m_container->m_children)
{
auto bpi = std::static_pointer_cast<NodeBrushPresetItem>(c);
pr->m_brush = std::make_shared<Brush>(*bpi->m_brush); // create copy
pr->m_brush->load();
Image img = pr->render_to_image();
img.file_name = pr->m_brush->m_name;
sw << img;
pb->increment();
}
// write brushes // write brushes
sw.wu32((int)img_brushes.size()); sw.wu32((int)img_brushes.size());
for (std::string image_path : img_brushes) for (std::string image_path : img_brushes)
@@ -732,6 +725,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea
if (!img.load(image_path)) if (!img.load(image_path))
LOG("export_ppbr failed to load image: %s", image_path.c_str()); LOG("export_ppbr failed to load image: %s", image_path.c_str());
sw << img; sw << img;
pb->increment();
} }
// write patterns // write patterns
@@ -742,6 +736,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea
if (!img.load(image_path)) if (!img.load(image_path))
LOG("export_ppbr failed to load image: %s", image_path.c_str()); LOG("export_ppbr failed to load image: %s", image_path.c_str());
sw << img; sw << img;
pb->increment();
} }
// write brush settings // write brush settings
@@ -750,8 +745,12 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea
{ {
auto bpi = std::static_pointer_cast<NodeBrushPresetItem>(c); auto bpi = std::static_pointer_cast<NodeBrushPresetItem>(c);
sw << *bpi->m_brush; sw << *bpi->m_brush;
pb->increment();
} }
f.write((char*)sw.m_data.data(), sw.m_data.size()); f.write((char*)sw.m_data.data(), sw.m_data.size());
pb->destroy();
return true; return true;
} }
return false; return false;
@@ -782,16 +781,33 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path)
return false; return false;
} }
Serializer::Descriptor info;
sr >> info;
int num_brush_tips = info.value<Serializer::Integer>("num_brush_tips");
int num_brush_patt = info.value<Serializer::Integer>("num_brush_patt");
int num_brushes = info.value<Serializer::Integer>("num_brushes");
std::string info_dump = info.str(0, "Info");
LOG("%s", info_dump.c_str());
auto pb = App::I->show_progress("Importing ABR", 1 + num_brush_patt + num_brush_tips + num_brushes * 2);
// header image // header image
Image header_image; Image header_image;
if (info.value<Serializer::Boolean>("has_header_image"))
sr >> header_image; sr >> header_image;
pb->increment();
// stroke previews // stroke previews
auto previews_count = sr.ru32(); auto previews_count = sr.ru32();
for (int i = 0; i < previews_count; i++) for (int i = 0; i < previews_count; i++)
{ {
Image img; Image img;
sr >> img; sr >> img;
pb->increment();
} }
// list of images // list of images
@@ -805,15 +821,19 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path)
Image img; Image img;
sr >> img; sr >> img;
std::string path = App::I->data_path + "/brushes/" + img.file_name + "." + img.file_ext; std::string path = App::I->data_path + "/brushes/" + img.file_name + "." + img.file_ext;
std::string path_thumb = App::I->data_path + "/brushes/thumbs/" + img.file_name + "." + img.file_ext;
if (!Asset::exist(path)) if (!Asset::exist(path))
{ {
img.save_png(path); img.save_png(path);
auto thumb = img.resize(64, 64);
thumb.save_png(path_thumb);
} }
else else
{ {
LOG("import_ppbr: brush image already exists in %s", path.c_str()); LOG("import_ppbr: brush image already exists in %s", path.c_str());
} }
img_brushes.insert(path); img_brushes.insert(path);
pb->increment();
} }
// brush patterns // brush patterns
@@ -823,14 +843,18 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path)
Image img; Image img;
sr >> img; sr >> img;
std::string path = App::I->data_path + "/patterns/" + img.file_name + "." + img.file_ext; std::string path = App::I->data_path + "/patterns/" + img.file_name + "." + img.file_ext;
std::string path_thumb = App::I->data_path + "/patterns/thumbs/" + img.file_name + "." + img.file_ext;
if (!Asset::exist(path)) if (!Asset::exist(path))
{ {
img.save_png(path); img.save_png(path);
auto thumb = img.resize(64, 64);
thumb.save_png(path_thumb);
} }
else else
{ {
LOG("import_ppbr: brush image already exists in %s", path.c_str()); LOG("import_ppbr: brush image already exists in %s", path.c_str());
} }
pb->increment();
img_patterns.insert(path); img_patterns.insert(path);
} }
@@ -844,22 +868,13 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path)
LOG("import_ppbr brush name %s", b->m_name.c_str()); LOG("import_ppbr brush name %s", b->m_name.c_str());
if (b->valid()) if (b->valid())
{ {
NodeBrushPresetItem* brush = new NodeBrushPresetItem; add_brush(b);
m_container->add_child(brush);
brush->init();
brush->create();
brush->loaded();
brush->thumb_path = b->m_brush_thumb_path;
brush->high_path = b->m_brush_path;
brush->m_brush = b;
brush->m_preview->m_brush = b;
brush->m_preview->draw_stroke();
brush->m_caption_size->set_text_format("%d", (int)b->m_tip_size);
brush->m_thumb->set_image(brush->m_brush->m_brush_thumb_path);
brush->on_click = std::bind(&NodePanelBrushPreset::handle_click, this, std::placeholders::_1);
} }
pb->increment();
} }
pb->destroy();
return true; return true;
} }
return false; return false;
@@ -944,6 +959,19 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
return true; return true;
} }
bool NodePanelBrushPreset::import_brush(const std::string& path)
{
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return false;
std::string base = m[1].str();
std::string name = m[2].str();
std::string ext = m[3].str();
return ext == "ppbr" ? import_ppbr(path) : import_abr(path);
}
void NodePanelBrushPreset::clear_brushes() void NodePanelBrushPreset::clear_brushes()
{ {
m_container->remove_all_children(); m_container->remove_all_children();

View File

@@ -101,6 +101,7 @@ public:
bool export_ppbr(const std::string& path, const Image& header_image); bool export_ppbr(const std::string& path, const Image& header_image);
bool import_ppbr(const std::string& path); bool import_ppbr(const std::string& path);
bool import_abr(const std::string& path); bool import_abr(const std::string& path);
bool import_brush(const std::string& path);
std::string replace_path(std::string path, std::string new_base); std::string replace_path(std::string path, std::string new_base);
void clear_brushes(); void clear_brushes();
}; };