#include "pch.h" #include "log.h" #include "node_panel_brush.h" #include "asset.h" #include "texture.h" #ifdef __APPLE__ #include #endif Node* NodeButtonBrush::clone_instantiate() const { return new NodeButtonBrush(); } void NodeButtonBrush::init() { init_template("tpl-brush-icon"); color_hover = glm::vec4(.7, .7, .7, 1); color_normal = glm::vec4(.3, .3, .3, 1); m_color = color_normal; img = (NodeImage*)m_children[0].get(); } void NodeButtonBrush::set_icon(const char* path) { img->m_path = path; img->m_tex_id = const_hash(img->m_path.c_str()); img->m_use_mipmaps = true; img->create(); } void NodeButtonBrush::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* NodePanelBrush::clone_instantiate() const { return new NodePanelBrush(); } void NodePanelBrush::init() { init_template("tpl-panel-brushes"); //m_layers_container = find("layers-container"); static auto icons = Asset::list_files("data/thumbs", true, ".*\\.png$"); if ((m_container = find("brushes"))) { int count = 0; for (auto& i : icons) { std::string path = "data/thumbs/" + i; std::string path_hi = "data/brushes/" + i; NodeButtonBrush* brush = new NodeButtonBrush; 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()); m_brushes.push_back(brush); brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1); } } } void NodePanelBrush::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); } uint16_t NodePanelBrush::get_texture_id(int index) const { TextureManager::load(m_brushes[index]->high_path.c_str(), true); return m_brushes[index]->high_id; } int NodePanelBrush::get_brush_id(int index) const { return m_brushes[index]->m_brushID; } // select the current brush based on the texture id void NodePanelBrush::select_brush(int brush_id) { if (m_current) m_current->m_selected = false; for (auto b : m_brushes) { if (b->m_brushID == brush_id) { b->m_selected = true; m_current = b; } } } // ----------------------------------------------------------------------- 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("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; }