add/remove presets on all panels

This commit is contained in:
2019-09-06 23:59:49 +02:00
parent 17603bf468
commit 14a037e2d3
4 changed files with 72 additions and 49 deletions

View File

@@ -136,7 +136,7 @@
<text text="Import from Photoshop ABR files."/> <text text="Import from Photoshop ABR files."/>
<text text="Or download free ones."/> <text text="Or download free ones."/>
<border color=".3" dir="row" height="60" margin="15 0 15 0"> <border color=".3" dir="row" height="60" margin="15 0 15 0">
<button id="import" text="Import ABR" pad="10" height="100%"/> <button id="import" text="Import Brush" pad="10" height="100%"/>
<button id="download" text="Download" pad="10" height="100%"/> <button id="download" text="Download" pad="10" height="100%"/>
</border> </border>
<text text="Otherwise you can create new brushes,"/> <text text="Otherwise you can create new brushes,"/>

View File

@@ -370,6 +370,8 @@ void NodePanelBrush::added(Node* parent)
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
std::vector<NodePanelBrushPreset*> NodePanelBrushPreset::s_panels;
Node* NodeBrushPresetItem::clone_instantiate() const Node* NodeBrushPresetItem::clone_instantiate() const
{ {
return new NodeBrushPresetItem(); return new NodeBrushPresetItem();
@@ -394,6 +396,16 @@ void NodeBrushPresetItem::draw()
NodeButtonCustom::draw(); NodeButtonCustom::draw();
} }
NodePanelBrushPreset::NodePanelBrushPreset()
{
s_panels.push_back(this);
}
NodePanelBrushPreset::~NodePanelBrushPreset()
{
s_panels.erase(std::remove(s_panels.begin(), s_panels.end(), this));
}
//--- //---
Node* NodePanelBrushPreset::clone_instantiate() const Node* NodePanelBrushPreset::clone_instantiate() const
@@ -407,14 +419,18 @@ void NodePanelBrushPreset::init()
m_container = find<Node>("brushes"); m_container = find<Node>("brushes");
m_btn_add = find<NodeButtonCustom>("btn-add"); m_btn_add = find<NodeButtonCustom>("btn-add");
m_btn_add->on_click = [this] (Node*) { m_btn_add->on_click = [this] (Node*) {
add_brush(std::make_shared<Brush>(*Canvas::I->m_current_brush)); for (auto p : s_panels)
p->add_brush(std::make_shared<Brush>(*Canvas::I->m_current_brush));
save(); save();
}; };
m_btn_up = find<NodeButtonCustom>("btn-up"); m_btn_up = find<NodeButtonCustom>("btn-up");
m_btn_up->on_click = [this](Node*) { m_btn_up->on_click = [this](Node*) {
if (m_current) if (m_current)
{ {
m_container->move_child(m_current, std::max(m_container->get_child_index(m_current) - 1, 0)); int idx = m_container->get_child_index(m_current);
int to = std::max(idx - 1, 0);
for (auto p : s_panels)
p->m_container->move_child(p->m_container->m_children[idx].get(), to);
save(); save();
} }
}; };
@@ -422,8 +438,10 @@ void NodePanelBrushPreset::init()
m_btn_down->on_click = [this](Node*) { m_btn_down->on_click = [this](Node*) {
if (m_current) if (m_current)
{ {
m_container->move_child(m_current, int idx = m_container->get_child_index(m_current);
std::min(m_container->get_child_index(m_current) + 1, (int)m_container->m_children.size() - 1)); int to = std::min(idx + 1, (int)m_container->m_children.size() - 1);
for (auto p : s_panels)
p->m_container->move_child(p->m_container->m_children[idx].get(), to);
save(); save();
} }
}; };
@@ -441,24 +459,26 @@ 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);
m_current->destroy(); for (auto p : s_panels)
if (m_container->m_children.empty())
{ {
m_current = nullptr; bool new_current = !p->m_current || p->m_container->get_child_index(p->m_current) == index;
} p->m_container->m_children[index]->destroy();
else if (p->m_container->m_children.empty())
{ {
int next = std::min<int>((int)m_container->m_children.size() - 1, index); p->m_current = nullptr;
m_current = (NodeBrushPresetItem*)m_container->m_children[next].get(); }
m_current->m_selected = true; else if (new_current)
{
int next = std::min<int>((int)p->m_container->m_children.size() - 1, index);
p->m_current = (NodeBrushPresetItem*)p->m_container->m_children[next].get();
p->m_current->m_selected = true;
}
p->m_notification->SetVisibility(p->m_container->m_children.size() == 0);
} }
save(); save();
m_notification->SetVisibility(m_container->m_children.size() == 0);
}; };
m_btn_menu = find<NodeButtonCustom>("btn-menu"); m_btn_menu = find<NodeButtonCustom>("btn-menu");
m_btn_menu->on_click = [this](Node* b) { m_btn_menu->on_click = [this](Node* b) {
@@ -476,7 +496,8 @@ void NodePanelBrushPreset::init()
std::thread([this, path] { std::thread([this, path] {
BT_SetTerminate(); BT_SetTerminate();
import_brush(path); import_brush(path);
m_notification->SetVisibility(m_container->m_children.size() == 0); for (auto p : s_panels)
p->m_notification->SetVisibility(p->m_container->m_children.size() == 0);
}).detach(); }).detach();
}); });
break; break;
@@ -503,7 +524,8 @@ void NodePanelBrushPreset::init()
std::thread([this, path] { std::thread([this, path] {
BT_SetTerminate(); BT_SetTerminate();
import_brush(path); import_brush(path);
m_notification->SetVisibility(m_container->m_children.size() == 0); for (auto p : s_panels)
p->m_notification->SetVisibility(p->m_container->m_children.size() == 0);
}).detach(); }).detach();
}); });
}; };
@@ -553,15 +575,17 @@ kEventResult NodePanelBrushPreset::handle_event(Event* e)
void NodePanelBrushPreset::handle_click(Node* target) void NodePanelBrushPreset::handle_click(Node* target)
{ {
//if (target == m_current) int idx = m_container->get_child_index(target);
// return; for (auto p : s_panels)
if (m_current) {
m_current->m_selected = false; if (p->m_current)
m_current = (NodeBrushPresetItem*)target; p->m_current->m_selected = false;
m_current->m_selected = true; p->m_current = (NodeBrushPresetItem*)p->m_container->get_child_at(idx);
p->m_current->m_selected = true;
p->m_interacted = true;
}
if (on_brush_changed) if (on_brush_changed)
on_brush_changed(this, m_current->m_brush); on_brush_changed(this, m_current->m_brush);
m_interacted = true;
} }
bool NodePanelBrushPreset::save() bool NodePanelBrushPreset::save()
@@ -645,7 +669,6 @@ void NodePanelBrushPreset::add_brush(std::shared_ptr<Brush> brush)
b->thumb_path = brush->m_brush_thumb_path; b->thumb_path = brush->m_brush_thumb_path;
b->high_path = brush->m_brush_path; b->high_path = brush->m_brush_path;
b->m_brush = brush; b->m_brush = brush;
//brush->m_brush->m_tip_size = .05f;
b->m_preview->m_brush = brush; b->m_preview->m_brush = brush;
b->m_preview->draw_stroke(); b->m_preview->draw_stroke();
b->m_thumb->m_use_mipmaps = true; b->m_thumb->m_use_mipmaps = true;
@@ -868,7 +891,8 @@ 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())
{ {
add_brush(b); for (auto p : s_panels)
p->add_brush(b);
} }
pb->increment(); pb->increment();
} }
@@ -899,12 +923,10 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
if (!str_iequals(ext, "abr") || !Asset::exist(path)) if (!str_iequals(ext, "abr") || !Asset::exist(path))
return false; return false;
auto pb = App::I->show_progress("Importing ABR");
abr.open(path); abr.open(path);
int tot = (int)(abr.m_samples.size() + abr.m_patterns.size() + abr.m_presets.size()); auto pb = App::I->show_progress("Importing ABR",
std::atomic_int count(0); abr.m_samples.size() + abr.m_patterns.size() + abr.m_presets.size());
parallel_for(abr.m_samples.size(), [&](size_t i) parallel_for(abr.m_samples.size(), [&](size_t i)
//for (const auto& samp : abr.m_samples) //for (const auto& samp : abr.m_samples)
@@ -920,9 +942,7 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
samp.second->save_png(path_high); samp.second->save_png(path_high);
auto thumb = padded.resize(64, 64); auto thumb = padded.resize(64, 64);
thumb.save_png(path_thumb); thumb.save_png(path_thumb);
pb->increment();
count++;
pb->set_progress((float)count / (float)tot);
}); });
parallel_for(abr.m_patterns.size(), [&](size_t i) parallel_for(abr.m_patterns.size(), [&](size_t i)
@@ -936,24 +956,22 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
patt.second->save_png(path_high); patt.second->save_png(path_high);
auto thumb = patt.second->resize(64, 64); auto thumb = patt.second->resize(64, 64);
thumb.save_png(path_thumb); thumb.save_png(path_thumb);
pb->increment();
count++;
pb->set_progress((float)count / (float)tot);
}); });
auto brushes = abr.compute_brushes(App::I->data_path); auto brushes = abr.compute_brushes(App::I->data_path);
for (const auto& pr : brushes) for (const auto& b : brushes)
{ {
if (pr->valid()) if (b->valid())
{ {
LOG("add preset %s", pr->m_name.c_str()); LOG("add preset %s", b->m_name.c_str());
App::I->presets->add_brush(pr); for (auto p : s_panels)
p->add_brush(b);
} }
count++; pb->increment();
pb->set_progress((float)count / (float)tot);
} }
App::I->presets->save(); save();
pb->destroy(); pb->destroy();
return true; return true;
@@ -974,8 +992,11 @@ bool NodePanelBrushPreset::import_brush(const std::string& path)
void NodePanelBrushPreset::clear_brushes() void NodePanelBrushPreset::clear_brushes()
{ {
m_container->remove_all_children(); for (auto p : s_panels)
m_notification->SetVisibility(m_container->m_children.size() == 0); {
p->m_container->remove_all_children();
p->m_notification->SetVisibility(p->m_container->m_children.size() == 0);
}
} }
void NodePanelBrushPreset::added(Node* parent) void NodePanelBrushPreset::added(Node* parent)

View File

@@ -75,6 +75,7 @@ public:
class NodePanelBrushPreset : public Node class NodePanelBrushPreset : public Node
{ {
static std::vector<NodePanelBrushPreset*> s_panels;
bool m_interacted = false; bool m_interacted = false;
NodeBrushPresetItem* m_current = nullptr; NodeBrushPresetItem* m_current = nullptr;
NodeButtonCustom* m_btn_add; NodeButtonCustom* m_btn_add;
@@ -90,6 +91,8 @@ public:
Node* m_container; Node* m_container;
std::function<void(Node* target, std::shared_ptr<Brush>& brush)> on_brush_changed; std::function<void(Node* target, std::shared_ptr<Brush>& brush)> on_brush_changed;
std::function<void(Node* target)> on_popup_close; std::function<void(Node* target)> on_popup_close;
NodePanelBrushPreset();
~NodePanelBrushPreset();
virtual Node* clone_instantiate() const override; virtual Node* clone_instantiate() const override;
virtual void init() override; virtual void init() override;
virtual kEventResult handle_event(Event* e) override; virtual kEventResult handle_event(Event* e) override;
@@ -102,7 +105,6 @@ public:
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); bool import_brush(const std::string& path);
std::string replace_path(std::string path, std::string new_base);
void clear_brushes(); void clear_brushes();
}; };

View File

@@ -13,7 +13,7 @@ public:
NodeBorder* m_progress; NodeBorder* m_progress;
NodeBorder* m_body; NodeBorder* m_body;
int m_total = 0; int m_total = 0;
int m_count = 0; std::atomic_int m_count = 0;
virtual Node* clone_instantiate() const override; virtual Node* clone_instantiate() const override;
virtual void init() override; virtual void init() override;
void increment() noexcept; void increment() noexcept;