move abr import to main menu, add progress bar

This commit is contained in:
2019-02-23 00:45:14 +01:00
parent edc2bacc90
commit e1f82373c6
11 changed files with 163 additions and 98 deletions

View File

@@ -4,6 +4,7 @@
#include "canvas.h"
#include "node_button.h"
#include "app.h"
#include "abr.h"
Node* NodePanelStroke::clone_instantiate() const
{
@@ -22,6 +23,116 @@ void NodePanelStroke::init()
init_controls();
}
bool NodePanelStroke::import_abr(const std::string& path)
{
ABR abr;
LOG("ABR detected");
std::string name, base, ext;
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return false;
base = m[1].str();
name = m[2].str();
ext = m[3].str();
if (!str_iequals(ext, "abr"))
return false;
if (!abr.open(path))
{
LOG("ABR read failed");
return false;
}
int tot = abr.m_samples.size() + abr.m_patterns.size() + abr.m_presets.size();
std::atomic_int count(0);
async_start();
auto pb = App::I.show_progress("Importing ABR");
app_redraw();
async_update();
async_end();
parallel_for(abr.m_samples.size(), [&](size_t i)
//for (const auto& samp : abr.m_samples)
{
auto ii = abr.m_samples.begin();
std::advance(ii, i);
const auto& samp = *ii;
std::string path_high = App::I.data_path + "/brushes/" + samp.first + ".png";
std::string path_thumb = App::I.data_path + "/brushes/thumbs/" + samp.first + ".png";
auto padded = samp.second->resize_squared(glm::u8vec4(255));
auto high = padded.resize_power2();
high.save(path_high);
auto thumb = padded.resize(64, 64);
thumb.save(path_thumb);
async_start();
NodeButtonBrush* brush = new NodeButtonBrush;
m_brush_popup->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 = name;
brush->m_user_brush = true;
brush->on_click = std::bind(&NodePanelBrush::handle_click, m_brush_popup, std::placeholders::_1);
count++;
float prog = (float)count / (float)tot;
pb->m_progress->SetWidthP(prog * 100.f);
app_redraw();
async_update();
async_end();
});
parallel_for(abr.m_patterns.size(), [&](size_t i)
//for (const auto& patt : abr.m_patterns)
{
auto ii = abr.m_patterns.begin();
std::advance(ii, i);
const auto& patt = *ii;
std::string path_high = App::I.data_path + "/patterns/" + patt.first + ".png";
std::string path_thumb = App::I.data_path + "/patterns/thumbs/" + patt.first + ".png";
patt.second->save(path_high);
auto thumb = patt.second->resize(64, 64);
thumb.save(path_thumb);
async_start();
count++;
float prog = (float)count / (float)tot;
pb->m_progress->SetWidthP(prog * 100.f);
app_redraw();
async_update();
async_end();
});
auto brushes = abr.compute_brushes(App::I.data_path);
for (const auto& pr : brushes)
{
auto presets = App::I.stroke->m_presets_popup;
async_start();
if (pr->load())
{
LOG("add preset %s", pr->m_name.c_str());
presets->add_brush(pr);
}
count++;
float prog = (float)count / (float)tot;
pb->m_progress->SetWidthP(prog * 100.f);
app_redraw();
async_update();
async_end();
}
async_start();
pb->destroy();
app_redraw();
async_update();
async_end();
//save();
}
void NodePanelStroke::update_controls()
{
const auto& b = Canvas::I->m_current_brush;
@@ -124,7 +235,7 @@ void NodePanelStroke::init_controls()
m_presets_popup->create();
m_presets_popup->loaded();
m_presets_popup->SetPositioning(YGPositionTypeAbsolute);
m_presets_popup->SetSize(200, 400);
m_presets_popup->SetSize(300, 400);
m_presets_popup->m_mouse_ignore = false;
m_presets_popup->m_flood_events = true;
m_presets_popup->m_capture_children = false;
@@ -182,6 +293,7 @@ void NodePanelStroke::init_controls()
m_brush_thumb->set_image(b->m_brush_thumb_path);
m_dual_brush_thumb->set_image(b->m_dual_thumb_path);
m_preset_thumb->set_image(b->m_brush_thumb_path);
m_pattern_thumb->set_image(b->m_pattern_thumb_path);
update_controls();
};
};