From 003787c974b32a0814658d98aae0b9292eb1f092 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Fri, 6 Sep 2019 09:34:59 +0200 Subject: [PATCH] add progress to export/import ppbr --- src/node_panel_brush.cpp | 162 +++++++++++++++++++++++---------------- src/node_panel_brush.h | 1 + 2 files changed, 96 insertions(+), 67 deletions(-) diff --git a/src/node_panel_brush.cpp b/src/node_panel_brush.cpp index 861ed2c..128b287 100644 --- a/src/node_panel_brush.cpp +++ b/src/node_panel_brush.cpp @@ -441,6 +441,8 @@ void NodePanelBrushPreset::init() */ m_btn_delete = find("btn-remove"); m_btn_delete->on_click = [this](Node*) { + auto pb = App::I->show_progress("Exporting ABR"); + return; if (!m_current) return; 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) { std::thread([this, path] { BT_SetTerminate(); - - 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); - }); + import_brush(path); + m_notification->SetVisibility(m_container->m_children.size() == 0); }).detach(); }); break; case 1: // export file App::I->pick_file_save({ "ppbr" }, [this] (std::string path) { - export_ppbr(path, {}); + std::thread([this, path] { + BT_SetTerminate(); + export_ppbr(path, {}); + App::I->message_box("Export PPBR", "Brushes exported to:\n" + path); + }).detach(); }); break; case 2: // download @@ -515,7 +502,7 @@ void NodePanelBrushPreset::init() App::I->pick_file({ "abr", "ppbr" }, [this](std::string path) { std::thread([this, path] { BT_SetTerminate(); - import_abr(path); + import_brush(path); m_notification->SetVisibility(m_container->m_children.size() == 0); }).detach(); }); @@ -639,21 +626,7 @@ bool NodePanelBrushPreset::restore() if (b->valid()) { - App::I->ui_task([this, 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); - }); + add_brush(b); } } m_notification->SetVisibility(m_container->m_children.size() == 0); @@ -693,8 +666,41 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea sw.wu16(0); sw.wu16(1); + // count list of images + std::set img_brushes; + std::set img_patterns; + for (auto& c : m_container->m_children) + { + auto bpi = std::static_pointer_cast(c); + if (!bpi->m_brush->m_brush_path.empty() && !Asset::is_asset(bpi->m_brush->m_brush_path)) + img_brushes.insert(bpi->m_brush->m_brush_path); + if (!bpi->m_brush->m_dual_path.empty() && !Asset::is_asset(bpi->m_brush->m_dual_path)) + img_brushes.insert(bpi->m_brush->m_dual_path); + if (!bpi->m_brush->m_pattern_path.empty() && !Asset::is_asset(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(has_header_image); + info.props["num_brush_tips"] = std::make_shared(img_brushes.size()); + info.props["num_brush_patt"] = std::make_shared(img_patterns.size()); + info.props["num_brushes"] = std::make_shared(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 - sw << header_image; + if (has_header_image) + sw << header_image; + + pb->increment(); // create previews sw.wu32((int)m_container->m_children.size()); @@ -708,20 +714,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea Image img = pr->render_to_image(); img.file_name = pr->m_brush->m_name; sw << img; - } - - // list of images - std::set img_brushes; - std::set img_patterns; - for (auto& c : m_container->m_children) - { - auto bpi = std::static_pointer_cast(c); - if (!bpi->m_brush->m_brush_path.empty() && !Asset::is_asset(bpi->m_brush->m_brush_path)) - img_brushes.insert(bpi->m_brush->m_brush_path); - if (!bpi->m_brush->m_dual_path.empty() && !Asset::is_asset(bpi->m_brush->m_dual_path)) - img_brushes.insert(bpi->m_brush->m_dual_path); - if (!bpi->m_brush->m_pattern_path.empty() && !Asset::is_asset(bpi->m_brush->m_pattern_path)) - img_patterns.insert(bpi->m_brush->m_pattern_path); + pb->increment(); } // write brushes @@ -732,6 +725,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea if (!img.load(image_path)) LOG("export_ppbr failed to load image: %s", image_path.c_str()); sw << img; + pb->increment(); } // write patterns @@ -742,6 +736,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea if (!img.load(image_path)) LOG("export_ppbr failed to load image: %s", image_path.c_str()); sw << img; + pb->increment(); } // write brush settings @@ -750,8 +745,12 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path, const Image& hea { auto bpi = std::static_pointer_cast(c); sw << *bpi->m_brush; + pb->increment(); } f.write((char*)sw.m_data.data(), sw.m_data.size()); + + pb->destroy(); + return true; } return false; @@ -782,9 +781,25 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path) return false; } + Serializer::Descriptor info; + sr >> info; + + int num_brush_tips = info.value("num_brush_tips"); + int num_brush_patt = info.value("num_brush_patt"); + int num_brushes = info.value("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 Image header_image; - sr >> header_image; + if (info.value("has_header_image")) + sr >> header_image; + + pb->increment(); // stroke previews auto previews_count = sr.ru32(); @@ -792,6 +807,7 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path) { Image img; sr >> img; + pb->increment(); } // list of images @@ -805,15 +821,19 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path) Image img; sr >> img; 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)) { img.save_png(path); + auto thumb = img.resize(64, 64); + thumb.save_png(path_thumb); } else { LOG("import_ppbr: brush image already exists in %s", path.c_str()); } img_brushes.insert(path); + pb->increment(); } // brush patterns @@ -823,14 +843,18 @@ bool NodePanelBrushPreset::import_ppbr(const std::string& path) Image img; sr >> img; 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)) { img.save_png(path); + auto thumb = img.resize(64, 64); + thumb.save_png(path_thumb); } else { LOG("import_ppbr: brush image already exists in %s", path.c_str()); } + pb->increment(); 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()); if (b->valid()) { - 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); + add_brush(b); } + pb->increment(); } + pb->destroy(); + return true; } return false; @@ -944,6 +959,19 @@ bool NodePanelBrushPreset::import_abr(const std::string& path) 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() { m_container->remove_all_children(); diff --git a/src/node_panel_brush.h b/src/node_panel_brush.h index 34263d8..23cee9a 100644 --- a/src/node_panel_brush.h +++ b/src/node_panel_brush.h @@ -101,6 +101,7 @@ public: bool export_ppbr(const std::string& path, const Image& header_image); bool import_ppbr(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); void clear_brushes(); };