#include "pch.h" #include "log.h" #include "node_panel_brush.h" #include "asset.h" #include "texture.h" #ifdef __APPLE__ #include #endif #include "canvas.h" #include "app.h" #include "abr.h" 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(); } bool NodeButtonBrush::read(BinaryStreamReader& r) { Serializer::Descriptor d; r >> d; d.value("brush_name", brush_name); d.value("high_path", high_path); d.value("thumb_path", thumb_path); d.value("m_user_brush", m_user_brush); high_path = str_replace(high_path, "{data_path}", App::I->data_path); thumb_path = str_replace(thumb_path, "{data_path}", App::I->data_path); return true; } void NodeButtonBrush::write(BinaryStreamWriter& w) const { Serializer::Descriptor d; d.class_id = "brush"; d.name = L"Brush class"; d.props["brush_name"] = std::make_shared(brush_name); d.props["high_path"] = std::make_shared( str_replace(high_path, App::I->data_path, "{data_path}")); d.props["thumb_path"] = std::make_shared( str_replace(thumb_path, App::I->data_path, "{data_path}")); d.props["m_user_brush"] = std::make_shared(m_user_brush); w << d; } Node* NodePanelBrush::clone_instantiate() const { return new NodePanelBrush(); } void NodePanelBrush::init() { init_template("tpl-panel-brushes"); m_btn_add = find("btn-add"); m_btn_add->on_click = [this](Node*) { App::I->pick_file({ "JPG", "PNG" }, [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(); Image img; 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"; //img = img.resize_squared(glm::u8vec4(255)); if (m_dir_name == "brushes") img.gayscale_alpha(); auto thumb = img.resize(64, 64).resize_squared(glm::u8vec4(255)); thumb.save(path_thumb); //auto po2 = img.resize_power2(); img.save(path_high); 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); save(); } }); }; m_btn_remove = find("btn-remove"); m_btn_remove->on_click = [this](Node*) { if (m_current) { int idx = m_container->get_child_index(m_current); if (m_current->m_user_brush) { // only delete user brushes Asset::delete_file(m_current->thumb_path); Asset::delete_file(m_current->high_path); } m_container->remove_child(m_current); if (m_container->m_children.size() > 0) { idx = std::max(0, std::min(idx, (int)m_container->m_children.size() - 1)); m_current = (NodeButtonBrush*)m_container->m_children[idx].get(); m_current->m_selected = true; if (on_brush_changed) on_brush_changed(this, idx); } else { m_current = nullptr; } save(); } }; m_btn_up = find("btn-up"); m_btn_up->on_click = [this](Node*) { if (m_current) { int idx = m_container->get_child_index(m_current); idx = std::max(0, std::min(idx - 1, (int)m_container->m_children.size() - 1)); m_container->move_child(m_current, idx); save(); } }; m_btn_down = find("btn-down"); m_btn_down->on_click = [this](Node*) { if (m_current) { int idx = m_container->get_child_index(m_current); idx = std::max(0, std::min(idx + 1, (int)m_container->m_children.size() - 1)); m_container->move_child(m_current, idx); save(); } }; m_container = find("brushes"); if (Asset::exist(App::I->data_path + "/settings/" + m_dir_name + ".bin") && !restore()) { auto mb = App::I->message_box("Brushes", "Could not read brush textures file, it will be deleted.", true); mb->btn_ok->on_click = [this, mb](Node*) { Asset::delete_file(App::I->data_path + "/settings/" + m_dir_name + ".bin"); mb->destroy(); }; mb->btn_cancel->on_click = [mb](Node*) { mb->destroy(); }; } if (m_container->m_children.empty() && !m_dir_name.empty()) { auto icons = Asset::list_files("data/" + m_dir_name, ".*\\.png$"); for (auto& i : icons) { std::string path = "data/" + m_dir_name + "/thumbs/" + i; std::string path_hi = "data/" + m_dir_name + "/" + i; NodeButtonBrush* brush = new NodeButtonBrush; m_container->add_child(brush); brush->init(); brush->create(); brush->loaded(); brush->set_icon(path.c_str()); brush->thumb_path = path; brush->high_path = path_hi; brush->brush_name = i; brush->m_user_brush = false; // system brush, cannot be deleted from file brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1); } auto custom_icons = Asset::list_files(App::I->data_path + "/" + m_dir_name, ".*\\.png$"); for (auto& i : custom_icons) { std::string path_thumb = App::I->data_path + "/" + m_dir_name + "/thumbs/" + i; std::string path_high = App::I->data_path + "/" + m_dir_name + "/" + i; 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 = i; brush->m_user_brush = true; brush->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1); } } save(); } kEventResult NodePanelBrush::handle_event(Event* e) { switch (e->m_type) { case kEventType::MouseLeave: if (!m_interacted) break; // else fall through case kEventType::MouseUpL: if (!m_mouse_inside) { mouse_release(); m_parent->remove_child(this); if (on_popup_close) on_popup_close(this); } break; default: return kEventResult::Available; break; } return kEventResult::Consumed; } 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_container->get_child_index(target)); m_interacted = true; } int NodePanelBrush::find_brush(const std::string & name) const { for (int i = 0; i < m_container->m_children.size(); i++) { NodeButtonBrush* b = (NodeButtonBrush*)m_container->m_children[i].get(); if (b->brush_name.find(name) != std::string::npos) return i; } return -1; } std::string NodePanelBrush::get_texture_path(int index) const { if (index < 0 || index >= m_container->m_children.size()) return ""; return ((NodeButtonBrush*)m_container->m_children[index].get())->high_path; } std::string NodePanelBrush::get_thumb_path(int index) const { if (index < 0 || index >= m_container->m_children.size()) return ""; return ((NodeButtonBrush*)m_container->m_children[index].get())->thumb_path; } bool NodePanelBrush::save() { std::ofstream f(App::I->data_path + "/settings/" + m_dir_name + ".bin", std::ios::binary); if (f.good()) { BinaryStreamWriter sw; sw.init(); sw.wstring_raw("PPVR"); // magic code sw.wu16(0); // version major sw.wu16(1); // minor sw.wu32((int)m_container->m_children.size()); // number of items for (const auto& child : m_container->m_children) { auto b = std::static_pointer_cast(child); sw << *b; } f.write((char*)sw.m_data.data(), sw.m_data.size()); return true; } return false; } bool NodePanelBrush::restore() { Asset f; auto path = App::I->data_path + "/settings/" + m_dir_name + ".bin"; if (f.open(path.c_str())) { f.read_all(); BinaryStreamReader sr; sr.init(f.m_data, f.m_len); // sanity checks if (sr.rstring(4) != "PPVR") { LOG("PPVR tag not found") return false; } auto vmaj = sr.ru16(); auto vmin = sr.ru16(); if (vmaj != 0 && vmin != 1) { LOG("unrecognised version %d.%d", vmaj, vmin); return false; } auto count = sr.ru32(); for (int k = 0; k < count; k++) { auto b = std::make_shared(); if (!b->read(sr)) { LOG("error deserializing the button brush"); return false; } if (Asset::exist(b->high_path)) { m_container->add_child(b); b->init(); b->create(); b->loaded(); b->set_icon(b->thumb_path.c_str()); b->on_click = std::bind(&NodePanelBrush::handle_click, this, std::placeholders::_1); } } return true; } return false; } void NodePanelBrush::clear() { m_container->remove_all_children(); } void NodePanelBrush::added(Node* parent) { m_interacted = false; } // ----------------------------------------------------------------------- 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 = find("thumb"); m_caption_size = find("caption-size"); m_preview = find("canvas"); m_preview->m_min_flow = 1.f; } 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"); m_container = find("brushes"); m_btn_add = find("btn-add"); m_btn_add->on_click = [this] (Node*) { add_brush(std::make_shared(*Canvas::I->m_current_brush)); save(); }; m_btn_up = find("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)); save(); } }; m_btn_down = find("btn-down"); 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)); save(); } }; m_btn_save = find("btn-save"); m_btn_save->on_click = [this](Node*) { if (m_current) { *m_current->m_brush = *Canvas::I->m_current_brush; m_current->m_preview->draw_stroke(); m_current->m_thumb->set_image(m_current->m_brush->m_brush_thumb_path); save(); } }; m_btn_delete = find("btn-remove"); m_btn_delete->on_click = [this](Node*) { if (!m_current) return; int index = m_container->get_child_index(m_current); m_current->destroy_immediate(); m_container->remove_child(m_current); if (m_container->m_children.empty()) { m_current = nullptr; } else { int next = std::min((int)m_container->m_children.size() - 1, index); m_current = (NodeBrushPresetItem*)m_container->m_children[next].get(); m_current->m_selected = true; } save(); }; if (Asset::exist(App::I->data_path + "/settings/presets.bin") && !restore()) { auto mb = App::I->message_box("Presets", "Could not read brush presets file, it will be deleted.", true); mb->btn_ok->on_click = [mb](Node*) { Asset::delete_file(App::I->data_path + "/settings/presets.bin"); mb->destroy(); }; mb->btn_cancel->on_click = [mb](Node*) { mb->destroy(); }; } } kEventResult NodePanelBrushPreset::handle_event(Event* e) { switch (e->m_type) { case kEventType::MouseLeave: if (!m_interacted) break; // else fall through case kEventType::MouseUpL: if (!m_mouse_inside) { mouse_release(); m_parent->remove_child(this); if (on_popup_close) on_popup_close(this); } break; default: return kEventResult::Available; break; } return kEventResult::Consumed; } 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; if (on_brush_changed) on_brush_changed(this, m_current->m_brush); m_interacted = true; } bool NodePanelBrushPreset::save() { std::ofstream f(App::I->data_path + "/settings/presets.bin", std::ios::binary); if (f.good()) { BinaryStreamWriter sw; sw.init(); sw.wstring_raw("PPVR"); sw.wu16(0); sw.wu16(1); sw.wu32((int)m_container->m_children.size()); for (int ci = 0; ci < m_container->m_children.size(); ci++) { auto bpi = static_cast(m_container->get_child_at(ci)); sw << *bpi->m_brush; } f.write((char*)sw.m_data.data(), sw.m_data.size()); return true; } return false; } bool NodePanelBrushPreset::restore() { Asset f; auto path = App::I->data_path + "/settings/presets.bin"; if (f.open(path.c_str())) { f.read_all(); BinaryStreamReader sr; sr.init(f.m_data, f.m_len); // sanity checks if (sr.rstring(4) != "PPVR") { LOG("PPVR tag not found") return false; } auto vmaj = sr.ru16(); auto vmin = sr.ru16(); if (vmaj != 0 && vmin != 1) { LOG("unrecognised version %d.%d", vmaj, vmin); return false; } auto count = sr.ru32(); for (int k = 0; k < count; k++) { auto b = std::make_shared(); if (!b->read(sr)) { LOG("error deserializing the brush"); return false; } if (b->valid()) { NodeBrushPresetItem* brush = new NodeBrushPresetItem; m_container->add_child(brush); brush->init(); brush->create(); brush->loaded(); brush->thumb_path = b->m_brush_thumb_path; brush->high_path = b->m_brush_path; brush->m_brush = b; brush->m_preview->m_brush = b; brush->m_preview->draw_stroke(); brush->m_caption_size->set_text_format("%d", (int)b->m_tip_size); brush->m_thumb->set_image(brush->m_brush->m_brush_thumb_path); brush->on_click = std::bind(&NodePanelBrushPreset::handle_click, this, std::placeholders::_1); } } return true; } return false; } void NodePanelBrushPreset::add_brush(std::shared_ptr brush) { NodeBrushPresetItem* b = new NodeBrushPresetItem; m_container->add_child(b); b->init(); b->create(); b->loaded(); 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; b->m_thumb->set_image(brush->m_brush_thumb_path); b->m_caption_size->set_text_format("%d", (int)brush->m_tip_size); b->on_click = std::bind(&NodePanelBrushPreset::handle_click, this, std::placeholders::_1); } void NodePanelBrushPreset::clear_brushes() { m_container->remove_all_children(); } void NodePanelBrushPreset::added(Node* parent) { m_interacted = false; }