Simplify tiny file dialog implementation

This commit is contained in:
stujones11 2018-10-03 18:39:59 +01:00
parent fafe848e4f
commit 57c8d9123b
3 changed files with 51 additions and 126 deletions

View File

@ -1,4 +1,3 @@
#include <thread>
#include <stdlib.h>
#include <iostream>
#include <irrlicht.h>
@ -19,51 +18,19 @@
namespace dialog
{
SEvent event;
bool has_event = false;
const char *filename = nullptr;
static inline void fileOpen(IGUIEnvironment *env, s32 id,
const char *caption, const char **filters, const int filter_count)
{
IGUIWindow *window = env->addWindow(rect<s32>(0,0,0,0), true);
window->setVisible(false);
io::IFileSystem *fs = env->getFileSystem();
io::path path = fs->getWorkingDirectory() + "/";
const char *fn = tinyfd_openFileDialog(caption, path.c_str(),
filter_count, filters, 0);
try
{
event.EventType = EET_GUI_EVENT;
event.UserEvent.UserData1 = id;
if (fn)
{
filename = fn;
io::path cwd = fs->getFileDir(fn);
fs->changeWorkingDirectoryTo(cwd);
event.GUIEvent.EventType = EGET_FILE_SELECTED;
}
else
{
filename = nullptr;
event.GUIEvent.EventType = EGET_FILE_CHOOSE_DIALOG_CANCELLED;
}
window->remove();
has_event = true;
}
catch (const std::exception &except)
{
std::cout << except.what() << std::endl;
}
}
void showFileOpen(IGUIEnvironment *env, s32 id, const char *caption,
const char *fileOpenDialog(IGUIEnvironment *env, const char *caption,
const char **filters, const int filter_count)
{
std::thread thread(fileOpen, env, id, caption, filters,
filter_count);
thread.detach();
io::IFileSystem *fs = env->getFileSystem();
io::path path = fs->getWorkingDirectory() + "/";
const char *fn = tinyfd_openFileDialog(caption, path.c_str(),
filter_count, filters, 0);
if (fn)
{
io::path cwd = fs->getFileDir(fn);
fs->changeWorkingDirectoryTo(cwd);
}
return fn;
}
}
@ -331,16 +298,6 @@ ITexture *TexturesDialog::getTexture(const io::path &filename)
return texture;
}
void TexturesDialog::draw()
{
if (dialog::has_event)
{
OnEvent(dialog::event);
dialog::has_event = false;
}
IGUIElement::draw();
}
bool TexturesDialog::OnEvent(const SEvent &event)
{
if (event.EventType == EET_GUI_EVENT)
@ -425,39 +382,32 @@ bool TexturesDialog::OnEvent(const SEvent &event)
}
else
{
s32 texture_id = 0;
IGUIEditBox *edit = 0;
s32 edit_id = 0;
for (s32 i = 0; i < 6; ++i)
{
if (id == E_BUTTON_ID_MODEL + i)
edit_id = E_TEXTURE_ID_MODEL + i;
else if (id == E_BUTTON_ID_MODEL + i)
edit_id = E_TEXTURE_ID_MODEL + 1;
if (edit_id)
{
texture_id = E_TEXTURE_ID_MODEL;
break;
}
else if (id == E_BUTTON_ID_WIELD + i)
{
texture_id = E_TEXTURE_ID_WIELD;
edit = (IGUIEditBox*)getElementFromId(edit_id, true);
break;
}
}
if (texture_id)
{
dialog::showFileOpen(Environment, texture_id,
"Open Image File", dialog::texture_filters,
dialog::texture_filter_count);
}
}
}
else if (event.GUIEvent.EventType == EGET_FILE_SELECTED)
{
stringw fn = (dialog::filename) ? dialog::filename : "";
if (!fn.empty())
{
s32 id = event.UserEvent.UserData1;
IGUIEditBox *edit = (IGUIEditBox*)getElementFromId(id, true);
if (edit)
{
edit->setText(fn.c_str());
edit->enableOverrideColor(!(getTexture(fn)));
const char *fn = dialog::fileOpenDialog(Environment,
"Open Image File", dialog::texture_filters,
dialog::texture_filter_count);
if (fn)
{
edit->setText(stringw(fn).c_str());
edit->enableOverrideColor(!(getTexture(fn)));
}
}
}
}

View File

@ -68,11 +68,8 @@ namespace dialog
"*.png", "*.jpg", "*.tga", "*.bmp",
"*.psd", "*.pcx", "*.ppm", "*.wal"
};
extern const char *filename;
extern bool has_event;
extern SEvent event;
void showFileOpen(IGUIEnvironment *env, s32 id, const char *caption,
const char *fileOpenDialog(IGUIEnvironment *env, const char *caption,
const char **filters, const int filter_count);
}
@ -107,7 +104,6 @@ public:
TexturesDialog(IGUIEnvironment *env, IGUIElement *parent, s32 id,
const rect<s32> &rectangle, Config *conf, ISceneManager *smgr);
virtual ~TexturesDialog() {}
virtual void draw();
virtual bool OnEvent(const SEvent &event);
private:

View File

@ -81,8 +81,6 @@ bool Viewer::run(IrrlichtDevice *irr_device)
while (device->run())
{
resize();
if (dialog::has_event)
dialog::has_event = !OnEvent(dialog::event);
driver->beginScene(true, true, bg_color);
smgr->drawAll();
env->drawAll();
@ -161,15 +159,35 @@ bool Viewer::OnEvent(const SEvent &event)
switch (id)
{
case E_GUI_ID_LOAD_MODEL_MESH:
dialog::showFileOpen(env, E_GUI_ID_LOAD_MODEL_MESH,
{
const char *fn = dialog::fileOpenDialog(env,
"Open main model file", dialog::model_filters,
dialog::model_filter_count);
if (fn && scene->loadModelMesh(fn))
{
ISceneNode *model = scene->getNode(E_SCENE_ID_MODEL);
if (model)
{
animation->load(model);
setCaptionFileName(fn);
gui->reloadToolBox(E_GUI_ID_TOOLBOX_MODEL);
conf->set("model_mesh", fn);
}
}
break;
}
case E_GUI_ID_LOAD_WIELD_MESH:
dialog::showFileOpen(env, E_GUI_ID_LOAD_WIELD_MESH,
{
const char *fn = dialog::fileOpenDialog(env,
"Open wield model file", dialog::model_filters,
dialog::model_filter_count);
if (fn && scene->loadWieldMesh(fn))
{
gui->reloadToolBox(E_GUI_ID_TOOLBOX_WIELD);
conf->set("wield_mesh", fn);
}
break;
}
case E_GUI_ID_ENABLE_WIELD:
{
ISceneNode *wield = scene->getNode(E_SCENE_ID_WIELD);
@ -314,45 +332,6 @@ bool Viewer::OnEvent(const SEvent &event)
break;
}
}
else if (event.GUIEvent.EventType == EGET_FILE_SELECTED)
{
stringc fn = (dialog::filename) ? dialog::filename : "";
s32 id = event.UserEvent.UserData1;
gui->setFocused(false);
switch (id)
{
case E_GUI_ID_LOAD_MODEL_MESH:
{
if (!fn.empty() && scene->loadModelMesh(fn))
{
ISceneNode *model = scene->getNode(E_SCENE_ID_MODEL);
if (model)
{
animation->load(model);
setCaptionFileName(fn);
gui->reloadToolBox(E_GUI_ID_TOOLBOX_MODEL);
conf->set("model_mesh", fn.c_str());
}
}
return true;
}
case E_GUI_ID_LOAD_WIELD_MESH:
{
if (!fn.empty() && scene->loadWieldMesh(fn))
{
gui->reloadToolBox(E_GUI_ID_TOOLBOX_WIELD);
conf->set("wield_mesh", fn.c_str());
}
return true;
}
default:
break;
}
}
else if (event.GUIEvent.EventType == EGET_FILE_CHOOSE_DIALOG_CANCELLED)
{
gui->setFocused(false);
}
else if (event.GUIEvent.EventType == EGET_ELEMENT_CLOSED)
{
IGUIContextMenu *menu =