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

View File

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

View File

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