Files
panopainter/engine/node_panel_brush.cpp

123 lines
3.2 KiB
C++

#include "pch.h"
#include "log.h"
#include "node_panel_brush.h"
#include "asset.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->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<NodeBorder>("layers-container");
static auto icons = FindAllBrushes("data/Icons/");
if ((m_container = find<NodeBorder>("brushes")))
{
int count = 0;
for (auto& i : icons)
{
std::string path = "data/Icons/" + 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++;
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);
}
std::vector<std::string> NodePanelBrush::FindAllBrushes(std::string folder)
{
std::vector<std::string> names;
std::string search_path = folder + "*.png";
#ifdef _WIN32
WIN32_FIND_DATAA fd;
HANDLE hFind = ::FindFirstFileA(search_path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// read all (real) files in current folder
// , delete '!' read other 2 default folder . and ..
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
names.push_back(fd.cFileName);
}
} while (::FindNextFileA(hFind, &fd));
::FindClose(hFind);
}
#elif __ANDROID__
//LOG("listing brushes");
AAssetDir* dir = AAssetManager_openDir(Asset::m_am, "data/Icons");
while (const char* name = AAssetDir_getNextFileName(dir))
{
//LOG("asset: %s", name);
names.push_back(name);
}
AAssetDir_close(dir);
#else
DIR *dp;
struct dirent *ep;
dp = opendir(folder.c_str());
if (dp != NULL)
{
while ((ep = readdir(dp)))
if (ep->d_type != DT_DIR)
names.push_back(ep->d_name);
closedir(dp);
}
else
LOG("Couldn't open the directory: %s", folder.c_str());
#endif
return names;
}
uint16_t NodePanelBrush::get_texture_id(int index) const
{
return m_brushes[index]->img->m_tex_id;
}