add javascript binding and file picker for web

This commit is contained in:
2019-10-08 23:16:04 +02:00
parent f2a73a905d
commit a669d1313b
7 changed files with 228 additions and 135 deletions

View File

@@ -26,6 +26,8 @@ void destroy_window();
#elif __LINUX__
std::string linux_home_path();
int mkpath(const std::string& dir, mode_t mode = DEFFILEMODE);
#elif __WEB__
#include <unistd.h>
#endif
App* App::I = nullptr; // singleton
@@ -52,7 +54,7 @@ void App::create()
void App::open_document(std::string path)
{
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return;
@@ -236,6 +238,14 @@ void App::initLog()
mkpath(data_path + "/patterns/thumbs");
mkpath(data_path + "/settings");
mkpath(data_path + "/frames");
#elif __WEB__
data_path = "/";
mkdir("/brushes", 0777);
mkdir("/brushes/thumbs", 0777);
mkdir("/patterns", 0777);
mkdir("/patterns/thumbs", 0777);
mkdir("/settings", 0777);
mkdir("/frames", 0777);
#endif
// TODO: save this path somewhere in the settings, don't overwrite every start

View File

@@ -15,8 +15,10 @@ void win32_show_cursor(bool visible);
bool win32_clipboard_set_text(const std::string & s);
std::string win32_clipboard_get_text();
#elif __APPLE__
#else
#elif __LINUX__
#include <tinyfiledialogs.h>
#elif __WEB__
void webgl_pick_file(std::function<void(std::string)> callback);
#endif
@@ -149,6 +151,11 @@ void App::pick_image(std::function<void(std::string path)> callback)
std::string path = win32_open_file("Image Files (*.jpg, *.png)\0*.jpg;*.png");
if (!path.empty())
callback(path);
#elif __LINUX__
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
callback(p);
#elif __WEB__
webgl_pick_file(callback);
#endif
}
@@ -196,6 +203,8 @@ void App::pick_file(std::vector<std::string> types, std::function<void (std::str
#elif __LINUX__
if (auto p = tinyfd_openFileDialog("Open File", "", 0, nullptr, nullptr, false))
callback(p);
#elif __WEB__
webgl_pick_file(callback);
#endif
}

View File

@@ -80,7 +80,7 @@ void NodePanelBrush::init()
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::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return;
@@ -546,7 +546,7 @@ void NodePanelBrushPreset::init()
});
};
m_btn_download = find<NodeButton>("download");
m_btn_download->on_click = [] (Node*) {
m_btn_download->on_click = [this] (Node*) {
App::I->dialog_preset_download();
};
m_notification = find("notification");
@@ -701,7 +701,7 @@ bool NodePanelBrushPreset::export_ppbr(const std::string& path_in, const PPBRInf
path += ".ppbr";
LOG("export ppbr to: %s", path.c_str());
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)?$)");
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)?$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return false;
@@ -965,7 +965,7 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
LOG("ABR detected");
std::string name, base, ext;
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return false;
@@ -1035,7 +1035,7 @@ bool NodePanelBrushPreset::import_abr(const std::string& path)
bool NodePanelBrushPreset::import_brush(const std::string& path)
{
std::regex r(R"((.*)[\\/]([^\\/]+)\.(\w+)$)");
std::regex r(R"((.*)[\\/]?([^\\/]+)\.(\w+)$)");
std::smatch m;
if (!std::regex_search(path, m, r))
return false;