Enable file selector on emscripten.

This commit is contained in:
Bartosz Taudul 2022-10-06 17:42:57 +02:00
parent b369c784a6
commit 9f60b53729
No known key found for this signature in database
GPG Key ID: B7FE2008B7575DF3
2 changed files with 41 additions and 8 deletions

View File

@ -5,7 +5,7 @@ CFLAGS += -sUSE_FREETYPE=1 -pthread
CXXFLAGS := $(CFLAGS) -std=c++17
DEFINES += -DIMGUI_ENABLE_FREETYPE -DIMGUI_IMPL_OPENGL_ES2
INCLUDES := -I../../../imgui -I$(HOME)/.emscripten_cache/sysroot/include/capstone
LIBS += -lpthread -ldl $(HOME)/.emscripten_cache/sysroot/lib/libcapstone.a -sUSE_GLFW=3 -sTOTAL_MEMORY=512mb -sWASM_BIGINT=1 -sPTHREAD_POOL_SIZE=4 -sEXPORTED_FUNCTIONS=_main,_nativeResize -sEXPORTED_RUNTIME_METHODS=ccall --preload-file embed.tracy
LIBS += -lpthread -ldl $(HOME)/.emscripten_cache/sysroot/lib/libcapstone.a -sUSE_GLFW=3 -sTOTAL_MEMORY=512mb -sWASM_BIGINT=1 -sPTHREAD_POOL_SIZE=4 -sEXPORTED_FUNCTIONS=_main,_nativeResize,_nativeOpenFile -sEXPORTED_RUNTIME_METHODS=ccall --preload-file embed.tracy
PROJECT := Tracy
IMAGE := $(PROJECT)-$(BUILD).html
@ -13,7 +13,4 @@ NO_TBB := 1
FILTER := ../../../nfd/nfd_win.cpp
include ../../../common/src-from-vcxproj.mk
CXXFLAGS += -DTRACY_NO_FILESELECTOR
include ../../../common/unix.mk

View File

@ -1,7 +1,11 @@
#include "TracyFileselector.hpp"
#ifndef TRACY_NO_FILESELECTOR
# include "../nfd/nfd.h"
# ifdef __EMSCRIPTEN__
# include <emscripten.h>
# else
# include "../nfd/nfd.h"
# endif
#endif
namespace tracy::Fileselector
@ -9,21 +13,52 @@ namespace tracy::Fileselector
void Init()
{
#ifndef TRACY_NO_FILESELECTOR
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
NFD_Init();
#endif
}
void Shutdown()
{
#ifndef TRACY_NO_FILESELECTOR
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
NFD_Quit();
#endif
}
#ifdef __EMSCRIPTEN__
static std::function<void(const char*)> s_openFileCallback;
extern "C" int nativeOpenFile()
{
s_openFileCallback( "upload.tracy" );
return 0;
}
#endif
void OpenFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{
#ifndef TRACY_NO_FILESELECTOR
# ifdef __EMSCRIPTEN__
s_openFileCallback = callback;
EM_ASM( {
var input = document.createElement( 'input' );
input.type = 'file';
input.accept = UTF8ToString( $0 );
input.onchange = (e) => {
var file = e.target.files[0];
var reader = new FileReader();
reader.readAsArrayBuffer( file );
reader.onload = () => {
var buf = reader.result;
var view = new Uint8Array( buf );
FS.createDataFile( '/', 'upload.tracy', view, true, true );
Module.ccall( 'nativeOpenFile', 'number', [], [] );
FS.unlink( '/upload.tracy' );
};
};
input.click();
}, ext );
# else
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_OpenDialogU8( &fn, &filter, 1, nullptr ) == NFD_OKAY )
@ -31,12 +66,13 @@ void OpenFile( const char* ext, const char* desc, std::function<void(const char*
callback( (const char*)fn );
NFD_FreePathU8( fn );
}
# endif
#endif
}
void SaveFile( const char* ext, const char* desc, std::function<void(const char*)> callback )
{
#ifndef TRACY_NO_FILESELECTOR
#if !defined TRACY_NO_FILESELECTOR && !defined __EMSCRIPTEN__
nfdu8filteritem_t filter = { desc, ext };
nfdu8char_t* fn;
if( NFD_SaveDialogU8( &fn, &filter, 1, nullptr, nullptr ) == NFD_OKAY )