Files
panopainter/src/node_dialog_open.cpp

286 lines
8.3 KiB
C++

#include "pch.h"
#include "log.h"
#include "node_dialog_open.h"
#include "canvas.h"
#include "node_image_texture.h"
#include "asset.h"
#include "node_message_box.h"
#include "app.h"
Node* NodeDialogOpen::clone_instantiate() const
{
return new NodeDialogOpen();
}
void NodeDialogOpen::clone_finalize(Node* dest) const
{
NodeDialogOpen* n = static_cast<NodeDialogOpen*>(dest);
n->init_controls();
}
void NodeDialogOpen::init()
{
auto tpl = static_cast<const NodeBorder*>(init_template("dialog-open"));
m_color = tpl->m_color;
m_border_color = tpl->m_border_color;;
m_thinkness = tpl->m_thinkness;;
init_controls();
}
void NodeDialogOpen::init_controls()
{
btn_ok = find<NodeButton>("btn-ok");
btn_cancel = find<NodeButton>("btn-cancel");
btn_cancel->on_click = [this](Node*) {
destroy();
};
btn_delete = find<NodeButton>("btn-delete");
btn_delete->on_click = [this](Node*) {
if (!current)
return;
auto msgbox = new NodeMessageBox();
msgbox->m_manager = m_manager;
msgbox->init();
msgbox->m_title->set_text("Delete Project");
msgbox->m_message->set_text(("Are you sure you want to delete " + current->m_file_name + "?").c_str());
msgbox->btn_ok->on_click = [this,msgbox](Node*){
auto path = current->m_path;
int idx = container->get_child_index(current);
container->remove_child(current);
if (!container->m_children.empty())
{
int newidx = std::min<int>(idx, (int)container->m_children.size() - 1);
auto next = (NodeDialogOpenItem*)container->get_child_at(newidx);
current = nullptr;
next->on_selected(next);
next->m_selected = true;
}
else
{
current = nullptr;
auto image_tex = find<NodeImageTexture>("thumb-tex");
image_tex->tex.destroy();
}
Asset::delete_file(path);
msgbox->destroy();
};
root()->add_child(msgbox);
root()->update();
};
container = find<Node>("files-list");
auto names = Asset::list_files(App::I->work_path, ".*\\.ppi");
for (const auto& n : names)
{
auto node = new NodeDialogOpenItem;
node->m_manager = m_manager;
node->init();
node->m_text->set_text(n.c_str());
node->m_path = App::I->work_path + "/" + n;
node->m_file_name = n;
node->on_selected = [&](NodeDialogOpenItem* target) {
if (target == current)
return;
Image thumb = Canvas::I->thumbnail_read(target->m_path);
auto image_tex = find<NodeImageTexture>("thumb-tex");
image_tex->tex.destroy();
image_tex->tex.create(thumb);
selected_path = target->m_path;
selected_file = target->m_file_name;
selected_name = selected_file.substr(0, selected_file.length() - strlen(".ppi"));
if (current)
current->m_selected = false;
current = target;
};
container->add_child(node);
}
container->update();
if (auto* first = (NodeDialogOpenItem*)container->get_child_at(0))
{
first->on_selected(first);
first->m_selected = true;
}
}
void NodeDialogOpen::loaded()
{
}
//////////////////////////////////////////////////////////////////
Node* NodeDialogOpenItem::clone_instantiate() const
{
return new NodeDialogOpenItem;
}
void NodeDialogOpenItem::clone_finalize(Node* dest) const
{
NodeDialogOpenItem* n = static_cast<NodeDialogOpenItem*>(dest);
n->init_controls();
}
void NodeDialogOpenItem::init()
{
auto tpl = static_cast<const NodeBorder*>(init_template("dialog-open-item"));
m_color = tpl->m_color;
m_border_color = tpl->m_border_color;
m_thinkness = tpl->m_thinkness;
init_controls();
}
void NodeDialogOpenItem::init_controls()
{
m_text = find<NodeText>("title");
}
void NodeDialogOpenItem::loaded()
{
}
void NodeDialogOpenItem::draw()
{
auto c = m_selected ? m_color_selected : m_color_normal;
m_thinkness = m_selected ? 1.f : 0.f;
m_color = m_mouse_inside ? m_color_hover : c;
NodeBorder::draw();
}
kEventResult NodeDialogOpenItem::handle_event(Event* e)
{
NodeBorder::handle_event(e);
switch (e->m_type)
{
case kEventType::MouseEnter:
break;
case kEventType::MouseLeave:
break;
case kEventType::MouseDownL:
m_selected = true;
if (on_selected)
on_selected(this);
break;
case kEventType::MouseUpL:
break;
default:
return kEventResult::Available;
break;
}
return kEventResult::Consumed;
}
//////////////////////////////////////////////////////////////////
Node* NodeDialogSave::clone_instantiate() const
{
return new NodeDialogSave;
}
void NodeDialogSave::clone_finalize(Node* dest) const
{
NodeDialogSave* n = static_cast<NodeDialogSave*>(dest);
n->init_controls();
}
void NodeDialogSave::init()
{
auto tpl = static_cast<const NodeBorder*>(init_template("dialog-save"));
m_color = tpl->m_color;
m_border_color = tpl->m_border_color;
m_thinkness = tpl->m_thinkness;
init_controls();
}
void NodeDialogSave::init_controls()
{
btn_ok = find<NodeButton>("btn-ok");
btn_cancel = find<NodeButton>("btn-cancel");
btn_cancel->on_click = [this](Node*) {
destroy();
};
input = find<NodeTextInput>("txt-input");
input->on_return = [&](NodeTextInput* target){
if (btn_ok->on_click)
btn_ok->on_click(btn_ok);
};
#if defined(_WIN32) || defined(__OSX__)
static char path_buffer[256];
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*){
App::I->pick_dir([this](std::string path){
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
#ifdef _WIN32
GetFullPathNameA(path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
});
};
working_path = find<NodeText>("path");
#ifdef _WIN32
GetFullPathNameA(App::I->work_path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(App::I->work_path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
#endif
}
void NodeDialogSave::loaded()
{
}
//////////////////////////////////////////////////////////////////
Node* NodeDialogNewDoc::clone_instantiate() const
{
return new NodeDialogNewDoc;
}
void NodeDialogNewDoc::clone_finalize(Node* dest) const
{
NodeDialogNewDoc* n = static_cast<NodeDialogNewDoc*>(dest);
n->init_controls();
}
void NodeDialogNewDoc::init()
{
auto tpl = static_cast<const NodeBorder*>(init_template("dialog-newdoc"));
m_color = tpl->m_color;
m_border_color = tpl->m_border_color;
m_thinkness = tpl->m_thinkness;
init_controls();
}
void NodeDialogNewDoc::init_controls()
{
btn_ok = find<NodeButton>("btn-ok");
m_resolution = find<NodeComboBox>("resolution");
btn_cancel = find<NodeButton>("btn-cancel");
btn_cancel->on_click = [this](Node*) {
destroy();
};
input = find<NodeTextInput>("txt-input");
input->on_return = [&](NodeTextInput* target){
if (btn_ok->on_click)
btn_ok->on_click(btn_ok);
};
#if defined(_WIN32) || defined(__OSX__)
static char path_buffer[256];
btn_path = find<NodeButton>("btn-path");
btn_path->on_click = [this](Node*){
App::I->pick_dir([this](std::string path){
LOG("change working path to %s", path.c_str());
App::I->work_path = path;
#ifdef _WIN32
GetFullPathNameA(path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
});
};
working_path = find<NodeText>("path");
#ifdef _WIN32
GetFullPathNameA(App::I->work_path.c_str(), sizeof(path_buffer), path_buffer, nullptr);
#else
realpath(App::I->work_path.c_str(), path_buffer);
#endif
working_path->set_text_format("Working dir: %s", path_buffer);
#endif
}
void NodeDialogNewDoc::loaded()
{
}