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

@@ -136,7 +136,7 @@
<layout id="tpl-panel-stroke">
<node width="250" margin="0 0 10 0" rtl="ltr">
<border height="30" color=".5" align="center" justify="center">
<text text="Stroke" color="1 1 1 1"/>
<text text="Brush Settings" color="1 1 1 1"/>
</border>
<border color=".3" pad="5" dir="col" width="100%">
@@ -503,7 +503,7 @@
<border color=".2" height="20" dir="row" justify="center" align="center" margin="5 0 5 0">
<node align="center" width="1" grow="1">
<text text="Color Variations"/>
<text text="Pressure Variations"/>
</node>
<button id="button-unfold-colorvar" width="30" height="18" text="+"/>
</border>

View File

@@ -28,12 +28,12 @@ mediump float blend_stroke_color_dodge(mediump float base, mediump float stroke)
mediump float blend_stroke_color_burn(mediump float base, mediump float stroke)
{
if (base == 0.0)
return 0.0;
else if (stroke == 1.0)
if (base == 1.0)
return 1.0;
else if (stroke == 0.0)
return 0.0;
else
return 1.0 - (1.0 - base) / stroke;
return 1.0 - min(1.0, (1.0 - base) / stroke);
}
mediump float blend_stroke_linear_height(mediump float base, mediump float stroke, mediump float depth)

View File

@@ -148,8 +148,9 @@ std::vector<std::shared_ptr<Brush>> ABR::compute_brushes(const std::string& path
{
auto patt_uid = wstr2str(patt->value<String>("Idnt"));
b->m_pattern_path = path + "/patterns/" + patt_uid + ".png";
//b->m_brush_thumb_path = path + "/patterns/thumbs/" + patt_uid + ".png";
b->m_pattern_thumb_path = path + "/patterns/thumbs/" + patt_uid + ".png";
b->m_pattern_depth = p->value<UnitFloat>("textureDepth") * 0.01f;
b->m_pattern_enabled = true;
}
ret.push_back(b);
}
@@ -305,8 +306,8 @@ std::shared_ptr<ABR::Double> ABR::parse_doub()
std::shared_ptr<ABR::Enum> ABR::parse_enum()
{
auto ret = std::make_shared<Enum>();
auto t = rkey_or_string();
auto e = rkey_or_string();
ret->type = rkey_or_string();
ret->value = rkey_or_string();
//printf("enum: %s %s\n", t.c_str(), e.c_str());
return ret;
}

View File

@@ -450,9 +450,9 @@ class ABR : private BinaryStream
std::shared_ptr<RawData> parse_tdta();
std::map<std::string /*key*/, std::function<Type::Ref()>> m_parser_table;
std::vector<std::shared_ptr<Descriptor>> m_presets;
public:
std::vector<std::shared_ptr<Descriptor>> m_presets;
std::map<std::string /*uid*/, std::shared_ptr<Image>> m_patterns;
std::map<std::string /*uid*/, std::shared_ptr<Image>> m_samples;

View File

@@ -404,23 +404,39 @@ void App::init_menu_file()
};
if (auto b = popup->find<NodeButtonCustom>("file-import"))
b->on_click = [this, popup](Node*) {
App::I.pick_image([this](std::string path){
Image img;
async_start();
img.load_file(path);
if (img.width == img.height / 6 || img.width == img.height * 2)
App::I.pick_file({ "JPG", "PNG", "ABR" }, [this](std::string path){
std::string name, base, ext;
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return;
base = m[1].str();
name = m[2].str();
ext = m[3].str();
if (str_iequals(ext, "abr"))
{
Canvas::I->import_equirectangular(path);
std::thread([this,path] { stroke->import_abr(path); }).detach();
}
else
{
auto m = static_cast<CanvasModeTransform*>(canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
m->m_action = CanvasModeTransform::ActionType::Import;
m->m_source_image = std::move(img);
Canvas::set_mode(kCanvasMode::Import);
Image img;
async_start();
img.load_file(path);
if (img.width == img.height / 6 || img.width == img.height * 2)
{
Canvas::I->import_equirectangular(path);
}
else
{
auto m = static_cast<CanvasModeTransform*>(canvas->m_canvas->modes[(int)kCanvasMode::Import][0]);
m->m_action = CanvasModeTransform::ActionType::Import;
m->m_source_image = std::move(img);
Canvas::set_mode(kCanvasMode::Import);
}
async_redraw();
async_end();
}
async_redraw();
async_end();
});
popup->mouse_release();
popup->destroy();

View File

@@ -323,7 +323,7 @@ bool Brush::load_pattern(const std::string& path, const std::string& thumb)
m_pattern_texture->create_mipmaps();
m_pattern_texture->auto_destroy = true;
m_pattern_path = path;
m_texture_thumb_path = thumb;
m_pattern_thumb_path = thumb;
return true;
}

View File

@@ -20,7 +20,7 @@ public:
std::shared_ptr<Texture2D> m_pattern_texture;
std::string m_pattern_path;
std::string m_texture_thumb_path;
std::string m_pattern_thumb_path;
glm::vec4 m_tip_color{0, 0, 0, 1};
float m_tip_size = .25f;

View File

@@ -51,7 +51,7 @@ void NodePanelBrush::init()
m_btn_add = find<NodeButtonCustom>("btn-add");
m_btn_add->on_click = [this](Node*) {
App::I.pick_file({ "JPG", "PNG", "ABR" }, [this](std::string path) {
App::I.pick_file({ "JPG", "PNG" }, [this](std::string path) {
std::string name, base, ext;
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::smatch m;
@@ -61,72 +61,7 @@ void NodePanelBrush::init()
name = m[2].str();
ext = m[3].str();
Image img;
if (str_iequals(ext, "abr"))
{
ABR abr;
LOG("ABR detected");
if (!abr.open(path))
{
LOG("ABR read failed");
return;
}
parallel_for(abr.m_samples.size(), [&](size_t i)
{
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_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, this, std::placeholders::_1);
//app_redraw();
//async_end();
});
parallel_for(abr.m_patterns.size(), [&](size_t i)
{
auto ii = abr.m_patterns.begin();
std::advance(ii, i);
const auto& patt = *ii;
std::string path_high = App::I.data_path + "/textures/" + patt.first + ".png";
std::string path_thumb = App::I.data_path + "/textures/thumbs/" + patt.first + ".png";
patt.second->save(path_high);
auto thumb = patt.second->resize(64, 64);
thumb.save(path_thumb);
});
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);
}
async_end();
}
//save();
}
else if (!m_dir_name.empty() && img.load_file(path))
if (!m_dir_name.empty() && img.load_file(path))
{
std::string path_high = App::I.data_path + "/" + m_dir_name + "/" + name + ".png";
std::string path_thumb = App::I.data_path + "/" + m_dir_name + "/thumbs/" + name + ".png";
@@ -547,7 +482,7 @@ bool NodePanelBrushPreset::save()
i.m_dual_path_len = b->m_brush_path.size();
i.m_dual_thumb_path_len = b->m_brush_thumb_path.size();
i.m_stencil_path_len = b->m_pattern_path.size();
i.m_stencil_thumb_path_len = b->m_texture_thumb_path.size();
i.m_stencil_thumb_path_len = b->m_pattern_thumb_path.size();
i.m_tip_color = b->m_tip_color;
i.m_tip_size = b->m_tip_size;
i.m_tip_spacing = b->m_tip_spacing;
@@ -611,7 +546,7 @@ bool NodePanelBrushPreset::save()
fwrite(b->m_dual_path.c_str(), 1, b->m_brush_path.size(), fp);
fwrite(b->m_dual_thumb_path.c_str(), 1, b->m_brush_thumb_path.size(), fp);
fwrite(b->m_pattern_path.c_str(), 1, b->m_pattern_path.size(), fp);
fwrite(b->m_texture_thumb_path.c_str(), 1, b->m_texture_thumb_path.size(), fp);
fwrite(b->m_pattern_thumb_path.c_str(), 1, b->m_pattern_thumb_path.size(), fp);
}
fclose(fp);
return true;
@@ -704,19 +639,19 @@ bool NodePanelBrushPreset::restore()
b->m_dual_path.resize(i.m_brush_path_len);
b->m_dual_thumb_path.resize(i.m_brush_thumb_path_len);
b->m_pattern_path.resize(i.m_stencil_path_len);
b->m_texture_thumb_path.resize(i.m_stencil_thumb_path_len);
b->m_pattern_thumb_path.resize(i.m_stencil_thumb_path_len);
fread((char*)b->m_name.c_str(), 1, b->m_name.size(), fp);
fread((char*)b->m_brush_path.c_str(), 1, b->m_brush_path.size(), fp);
fread((char*)b->m_brush_thumb_path.c_str(), 1, b->m_brush_thumb_path.size(), fp);
fread((char*)b->m_dual_path.c_str(), 1, b->m_brush_path.size(), fp);
fread((char*)b->m_dual_thumb_path.c_str(), 1, b->m_brush_thumb_path.size(), fp);
fread((char*)b->m_pattern_path.c_str(), 1, b->m_pattern_path.size(), fp);
fread((char*)b->m_texture_thumb_path.c_str(), 1, b->m_texture_thumb_path.size(), fp);
fread((char*)b->m_pattern_thumb_path.c_str(), 1, b->m_pattern_thumb_path.size(), fp);
if (b->load_tip(b->m_brush_path, b->m_brush_thumb_path))
{
if (!b->m_pattern_path.empty())
b->load_pattern(b->m_pattern_path, b->m_texture_thumb_path);
b->load_pattern(b->m_pattern_path, b->m_pattern_thumb_path);
NodeBrushPresetItem* brush = new NodeBrushPresetItem;
m_container->add_child(brush);

View File

@@ -28,7 +28,6 @@ class NodePanelBrush : public Node
// brushes that are marked as deleted but file still exists
std::vector<std::string> m_deleted;
NodeButtonBrush* m_current = nullptr;
NodeScroll* m_container;
NodeButtonCustom* m_btn_add;
NodeButtonCustom* m_btn_up;
NodeButtonCustom* m_btn_down;
@@ -47,6 +46,7 @@ class NodePanelBrush : public Node
bool m_user_brush = false;
};
public:
NodeScroll* m_container;
std::string m_dir_name;
std::function<void(Node* target, int index)> on_brush_changed;
std::function<void(Node* target)> on_popup_close;

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();
};
};

View File

@@ -92,6 +92,7 @@ public:
virtual Node* clone_instantiate() const override;
virtual void clone_finalize(Node* dest) const override;
virtual void init() override;
bool import_abr(const std::string& path);
void init_controls();
void update_controls();