add brush preset panel

This commit is contained in:
2017-10-03 14:35:30 +01:00
parent db55a055fd
commit 41e52b7757
7 changed files with 191 additions and 7 deletions

View File

@@ -41,6 +41,7 @@ public:
std::shared_ptr<NodePanelLayer> layers;
std::shared_ptr<NodePanelColor> color;
std::shared_ptr<NodePanelStroke> stroke;
std::shared_ptr<NodePanelBrushPreset> presets;
NodeCanvas* canvas;
Node* current_panel = nullptr;
NodeScroll* panels;

View File

@@ -121,6 +121,12 @@ void App::init_sidebar()
stroke->create();
stroke->loaded();
presets = std::make_shared<NodePanelBrushPreset>();
presets->m_manager = &layout;
presets->init();
presets->create();
presets->loaded();
if (canvas)
{
stroke->m_canvas->m_brush.m_tip_color = color->m_color;
@@ -136,6 +142,16 @@ void App::init_sidebar()
if (on_brush_select)
on_brush_select(index);
};
presets->on_brush_changed = [this](Node* target, int index) {
auto b = presets->get_brush(index);
// don't change some params
b.m_tip_size = stroke->m_canvas->m_brush.m_tip_size;
b.m_tip_color = stroke->m_canvas->m_brush.m_tip_color;
stroke->set_params(b);
canvas->m_brush = stroke->m_canvas->m_brush;
if (on_brush_select)
on_brush_select(index);
};
color->on_color_changed = [this](Node* target, glm::vec4 color) {
stroke->m_canvas->m_brush.m_tip_color = color;
@@ -196,6 +212,14 @@ void App::init_sidebar()
button->set_color(panels->get_child_index(brushes.get()) == -1 ? color_button_normal : color_button_hlight);
};
}
if (auto* button = layout[main_id]->find<NodeButton>("btn-brush-preset"))
{
button->on_click = [this, button](Node*) {
panels->get_child_index(presets.get()) == -1 ? panels->add_child(presets) : panels->remove_child(presets.get());
panels->fix_scroll();
button->set_color(panels->get_child_index(presets.get()) == -1 ? color_button_normal : color_button_hlight);
};
}
if (auto* button = layout[main_id]->find<NodeButtonCustom>("btn-color"))
{
button->on_click = [this, button](Node*) {

View File

@@ -106,3 +106,105 @@ void NodePanelBrush::select_brush(int brush_id)
}
}
}
// -----------------------------------------------------------------------
Node* NodeBrushPresetItem::clone_instantiate() const
{
return new NodeBrushPresetItem();
}
void NodeBrushPresetItem::init()
{
init_template("tpl-brush-preset");
color_hover = glm::vec4(.7, .7, .7, 1);
color_normal = glm::vec4(.3, .3, .3, 1);
m_color = color_normal;
m_thumb = (NodeImage*)m_children[0].get();
m_preview = (NodeStrokePreview*)m_children[1].get();
}
void NodeBrushPresetItem::draw()
{
m_color = m_mouse_inside ? color_hover : color_normal;
m_color = m_selected ? glm::vec4(.9, 0, 0, 1) : m_color;
NodeButtonCustom::draw();
}
//---
Node* NodePanelBrushPreset::clone_instantiate() const
{
return new NodePanelBrushPreset();
}
void NodePanelBrushPreset::init()
{
init_template("tpl-panel-brush-preset");
static auto icons = Asset::list_files("data/thumbs", true, ".*\\.png$");
if ((m_container = find<NodeBorder>("brushes")))
{
int count = 0;
for (auto& i : icons)
{
std::string path = "data/thumbs/" + i;
std::string path_hi = "data/brushes/" + i;
NodeBrushPresetItem* brush = new NodeBrushPresetItem;
m_container->add_child(brush);
brush->init();
brush->create();
brush->loaded();
// brush->set_icon(path.c_str());
brush->m_brushID = count++;
brush->high_path = path_hi;
brush->high_id = const_hash(path_hi.c_str());
brush->m_brush.m_tex_id = const_hash(path.c_str());
brush->m_brush.m_tip_size = .05;
brush->m_brush.m_tip_flow = .2;
brush->m_brush.m_tip_opacity = 1;
brush->m_brush.m_tip_spacing = 0.03;
brush->m_brush.m_jitter_spread = (rand() % 1000) * 0.0001;
brush->m_preview->m_brush = brush->m_brush;
brush->m_preview->draw_stroke();
brush->m_thumb->m_path = path;
brush->m_thumb->m_tex_id = const_hash(path.c_str());
brush->m_thumb->create();
m_brushes.push_back(brush);
brush->on_click = std::bind(&NodePanelBrushPreset::handle_click, this, std::placeholders::_1);
}
}
}
void NodePanelBrushPreset::handle_click(Node* target)
{
if (target == m_current)
return;
if (m_current)
m_current->m_selected = false;
m_current = (NodeButtonBrush*)target;
m_current->m_selected = true;
if (on_brush_changed)
on_brush_changed(this, m_current->m_brushID);
}
ui::Brush NodePanelBrushPreset::get_brush(int index) const
{
auto b = m_brushes[index]->m_brush;
TextureManager::load(m_brushes[index]->high_path.c_str(), true);
b.m_tex_id = m_brushes[index]->high_id;
return b;
}
uint16_t NodePanelBrushPreset::get_texture_id(int index) const
{
TextureManager::load(m_brushes[index]->high_path.c_str(), true);
return m_brushes[index]->high_id;
}
int NodePanelBrushPreset::get_brush_id(int index) const
{
return m_brushes[index]->m_brushID;
}

View File

@@ -2,6 +2,8 @@
#include "node.h"
#include "node_button_custom.h"
#include "node_image.h"
#include "node_stroke_preview.h"
#include "brush.h"
class NodeButtonBrush : public NodeButtonCustom
{
@@ -32,3 +34,38 @@ public:
int get_brush_id(int index) const;
void select_brush(int brush_id);
};
// -----------------------------------------------------------------------
class NodeBrushPresetItem : public NodeButtonCustom
{
public:
int m_brushID;
ui::Brush m_brush;
std::string high_path;
uint16_t high_id;
bool m_selected = false;
NodeStrokePreview* m_preview;
NodeImage* m_thumb;
virtual Node* clone_instantiate() const override;
virtual void init() override;
virtual void draw() override;
};
class NodePanelBrushPreset : public Node
{
std::vector<NodeBrushPresetItem*> m_brushes;
NodeButtonBrush* m_current = nullptr;
Node* m_container;
public:
std::function<void(Node* target, int id)> on_brush_changed;
virtual Node* clone_instantiate() const override;
virtual void init() override;
void handle_click(Node* target);
std::vector<std::string> FindAllBrushes(const std::string& folder);
uint16_t get_texture_id(int index) const;
ui::Brush get_brush(int index) const;
int get_brush_id(int index) const;
void select_brush(int brush_id);
};

View File

@@ -31,9 +31,9 @@ void NodeStrokePreview::init_controls()
m_mesh.create();
m_sampler.create();
m_sampler_brush.create();
m_sampler_brush.set_filter(GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR);
TextureManager::load("data/Icons/Round-Hard.png");
m_brush.m_tex_id = const_hash("data/Icons/Round-Hard.png");
m_sampler_brush.set_filter(GL_LINEAR, GL_LINEAR);
TextureManager::load("data/thumbs/Round-Hard.png");
m_brush.m_tex_id = const_hash("data/thumbs/Round-Hard.png");
}
void NodeStrokePreview::restore_context()