diff --git a/bind-imgui.cpp b/bind-imgui.cpp index 7017aa2..f49bbfc 100644 --- a/bind-imgui.cpp +++ b/bind-imgui.cpp @@ -153,7 +153,7 @@ emscripten::val _ImGui_SetAllocatorFunctions_user_data = emscripten::val::undefined(); public: - WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext()) { + WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext(shared_font_atlas)) { ImGuiContext* prev_ctx = ImGui::GetCurrentContext(); ImGui::SetCurrentContext(ctx); ImGuiIO& io = ImGui::GetIO(); @@ -1600,8 +1600,8 @@ // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); - emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (), { - return WrapImGuiContext::CreateContext(); // TODO: shared font atlas + emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (ImFontAtlas* atlas), { + return WrapImGuiContext::CreateContext(atlas); // TODO: shared font atlas }), emscripten::allow_raw_pointers()); // IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = Destroy current context emscripten::function("DestroyContext", FUNCTION(void, (WrapImGuiContext* wrap), { diff --git a/bind-imgui.cpp b/bind-imgui.cpp index 7017aa2..f49bbfc 100644 --- a/bind-imgui.cpp +++ b/bind-imgui.cpp @@ -153,7 +153,7 @@ emscripten::val _ImGui_SetAllocatorFunctions_user_data = emscripten::val::undefined(); public: - WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext()) { + WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext(shared_font_atlas)) { ImGuiContext* prev_ctx = ImGui::GetCurrentContext(); ImGui::SetCurrentContext(ctx); ImGuiIO& io = ImGui::GetIO(); @@ -1600,8 +1600,8 @@ // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); - emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (), { - return WrapImGuiContext::CreateContext(); // TODO: shared font atlas + emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (ImFontAtlas* atlas), { + return WrapImGuiContext::CreateContext(atlas); // TODO: shared font atlas }), emscripten::allow_raw_pointers()); // IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = Destroy current context emscripten::function("DestroyContext", FUNCTION(void, (WrapImGuiContext* wrap), { diff --git a/bind-imgui.d.ts b/bind-imgui.d.ts index 41e8cce..8d7533c 100644 --- a/bind-imgui.d.ts +++ b/bind-imgui.d.ts @@ -958,7 +958,7 @@ // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); -CreateContext(): WrapImGuiContext; +CreateContext(shared_font_atlas: reference_ImFontAtlas | null): WrapImGuiContext; // IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = Destroy current context DestroyContext(ctx: WrapImGuiContext | null): void; // IMGUI_API ImGuiContext* GetCurrentContext(); diff --git a/bind-imgui.cpp b/bind-imgui.cpp index 7017aa2..f49bbfc 100644 --- a/bind-imgui.cpp +++ b/bind-imgui.cpp @@ -153,7 +153,7 @@ emscripten::val _ImGui_SetAllocatorFunctions_user_data = emscripten::val::undefined(); public: - WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext()) { + WrapImGuiContext(ImFontAtlas* shared_font_atlas = NULL): ctx(ImGui::CreateContext(shared_font_atlas)) { ImGuiContext* prev_ctx = ImGui::GetCurrentContext(); ImGui::SetCurrentContext(ctx); ImGuiIO& io = ImGui::GetIO(); @@ -1600,8 +1600,8 @@ // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); - emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (), { - return WrapImGuiContext::CreateContext(); // TODO: shared font atlas + emscripten::function("CreateContext", FUNCTION(WrapImGuiContext*, (ImFontAtlas* atlas), { + return WrapImGuiContext::CreateContext(atlas); // TODO: shared font atlas }), emscripten::allow_raw_pointers()); // IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = Destroy current context emscripten::function("DestroyContext", FUNCTION(void, (WrapImGuiContext* wrap), { diff --git a/bind-imgui.d.ts b/bind-imgui.d.ts index 41e8cce..8d7533c 100644 --- a/bind-imgui.d.ts +++ b/bind-imgui.d.ts @@ -958,7 +958,7 @@ // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); -CreateContext(): WrapImGuiContext; +CreateContext(shared_font_atlas: reference_ImFontAtlas | null): WrapImGuiContext; // IMGUI_API void DestroyContext(ImGuiContext* ctx = NULL); // NULL = Destroy current context DestroyContext(ctx: WrapImGuiContext | null): void; // IMGUI_API ImGuiContext* GetCurrentContext(); diff --git a/imgui.ts b/imgui.ts index dae074b..e8fbc15 100644 --- a/imgui.ts +++ b/imgui.ts @@ -2320,43 +2320,36 @@ // IMGUI_API ImGuiIO(); } +const _texturesById: Array = []; + // Context creation and access, if you want to use multiple context, share context between modules (e.g. DLL). // All contexts share a same ImFontAtlas by default. If you want different font atlas, you can new() them and overwrite the GetIO().Fonts variable of an ImGui context. // All those functions are not reliant on the current context. export class ImGuiContext { public static current_ctx: ImGuiContext | null = null; public static getTexture(index: number): ImTextureID | null { - if (ImGuiContext.current_ctx === null) { throw new Error(); } - return ImGuiContext.current_ctx._getTexture(index); + return _texturesById[index] || null; } public static setTexture(texture: ImTextureID | null): number { - if (ImGuiContext.current_ctx === null) { throw new Error(); } - return ImGuiContext.current_ctx._setTexture(texture); - } - - private textures: Array = []; - constructor(public readonly native: Bind.WrapImGuiContext) {} - private _getTexture(index: number): ImTextureID | null { - return this.textures[index] || null; - } - private _setTexture(texture: ImTextureID | null): number { - let index = this.textures.indexOf(texture); + let index = _texturesById.indexOf(texture); if (index === -1) { - for (let i = 0; i < this.textures.length; ++i) { - if (this.textures[i] === null) { - this.textures[i] = texture; + for (let i = 0; i < _texturesById.length; ++i) { + if (_texturesById[i] === null) { + _texturesById[i] = texture; return i; } } - index = this.textures.length; - this.textures.push(texture); + index = _texturesById.length; + _texturesById.push(texture); } return index; } + + constructor(public readonly native: Bind.WrapImGuiContext) {} } // IMGUI_API ImGuiContext* CreateContext(ImFontAtlas* shared_font_atlas = NULL); export function CreateContext(shared_font_atlas: ImFontAtlas | null = null): ImGuiContext | null { - const ctx: ImGuiContext = new ImGuiContext(bind.CreateContext()); + const ctx: ImGuiContext = new ImGuiContext(bind.CreateContext(shared_font_atlas ? shared_font_atlas.native : null)); if (ImGuiContext.current_ctx === null) { ImGuiContext.current_ctx = ctx; }