diff --git a/src/imgui_impl.ts b/src/imgui_impl.ts index 9655604..b19b8d7 100644 --- a/src/imgui_impl.ts +++ b/src/imgui_impl.ts @@ -5,6 +5,8 @@ let clipboard_text: string = ""; +const contextIOs: ImGui.ImGuiIO[] = []; + class ImGuiImplInternalRenderer extends PIXI.ObjectRenderer { constructor(r: PIXI.Renderer) @@ -159,6 +161,7 @@ export class ImGuiWindow extends PIXI.Container { public readonly ctx: ImGui.ImGuiContext; + private readonly io: ImGui.ImGuiIO; private sizeX: number; private sizeY: number; public getSizeX(): number { return this.sizeX; } @@ -175,6 +178,9 @@ { super(); this.ctx = ImGui.CreateContext(); + ImGui.SetCurrentContext(this.ctx); + this.io = ImGui.GetIO(); + contextIOs.push(this.io); this.update = update; this.resize(sizeX, sizeY); this.imgui.pluginName = 'imgui_renderer'; @@ -185,13 +191,11 @@ } mouseDown(e: PIXI.interaction.InteractionEvent): void { - const io: ImGui.ImGuiIO = ImGui.GetIO(); - io.MouseDown[mouse_button_map[0]] = true; + this.io.MouseDown[mouse_button_map[0]] = true; } mouseUp(e: PIXI.interaction.InteractionEvent): void { - const io: ImGui.ImGuiIO = ImGui.GetIO(); - io.MouseDown[mouse_button_map[0]] = false; + this.io.MouseDown[mouse_button_map[0]] = false; } resize(sizeX: number, sizeY: number) { @@ -207,33 +211,31 @@ ImGui.SetCurrentContext(this.ctx); - const io: ImGui.ImGuiIO = ImGui.GetIO(); - - if (io.WantSaveIniSettings) { - io.WantSaveIniSettings = false; + if (this.io.WantSaveIniSettings) { + this.io.WantSaveIniSettings = false; if (typeof(window) !== "undefined") { window.localStorage.setItem("imgui.ini", ImGui.SaveIniSettingsToMemory()); } } - io.DisplaySize.x = this.sizeX; - io.DisplaySize.y = this.sizeY; - io.DisplayFramebufferScale.x = 1; - io.DisplayFramebufferScale.y = 1; + this.io.DisplaySize.x = this.sizeX; + this.io.DisplaySize.y = this.sizeY; + this.io.DisplayFramebufferScale.x = 1; + this.io.DisplayFramebufferScale.y = 1; - io.DeltaTime = dt; + this.io.DeltaTime = dt; // TODO global mouse pos to local const localPos: PIXI.Point = this.getGlobalPosition(new PIXI.Point()); - io.MousePos.x = localPos.x; - io.MousePos.y = localPos.y; + this.io.MousePos.x = localPos.x; + this.io.MousePos.y = localPos.y; - if (io.WantSetMousePos) { - console.log("TODO: MousePos", io.MousePos.x, io.MousePos.y); + if (this.io.WantSetMousePos) { + console.log("TODO: MousePos", this.io.MousePos.x, this.io.MousePos.y); } if (typeof(document) !== "undefined") { - if (io.MouseDrawCursor) { + if (this.io.MouseDrawCursor) { document.body.style.cursor = "none"; } else { switch (ImGui.GetMouseCursor()) { @@ -251,10 +253,10 @@ } // Gamepad navigation mapping [BETA] - for (let i = 0; i < io.NavInputs.length; ++i) { - io.NavInputs[i] = 0.0; + for (let i = 0; i < this.io.NavInputs.length; ++i) { + this.io.NavInputs[i] = 0.0; } - if (io.ConfigFlags & ImGui.ConfigFlags.NavEnableGamepad) { + if (this.io.ConfigFlags & ImGui.ConfigFlags.NavEnableGamepad) { // Update gamepad inputs const gamepads: (Gamepad | null)[] = (typeof(navigator) !== "undefined" && typeof(navigator.getGamepads) === "function") ? navigator.getGamepads() : []; for (let i = 0; i < gamepads.length; ++i) { @@ -265,14 +267,14 @@ const MAP_BUTTON = function(NAV_NO: number, BUTTON_NO: number): void { if (!gamepad) { return; } if (buttons_count > BUTTON_NO && gamepad.buttons[BUTTON_NO].pressed) - io.NavInputs[NAV_NO] = 1.0; + this.io.NavInputs[NAV_NO] = 1.0; } const MAP_ANALOG = function(NAV_NO: number, AXIS_NO: number, V0: number, V1: number): void { if (!gamepad) { return; } let v: number = (axes_count > AXIS_NO) ? gamepad.axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0) v = 1.0; - if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; + if (this.io.NavInputs[NAV_NO] < v) this.io.NavInputs[NAV_NO] = v; } // TODO: map input based on vendor and product id // https://developer.mozilla.org/en-US/docs/Web/API/Gamepad/id @@ -422,54 +424,62 @@ } function canvas_on_blur(event: FocusEvent): void { - const io = ImGui.GetIO(); - io.KeyCtrl = false; - io.KeyShift = false; - io.KeyAlt = false; - io.KeySuper = false; - for (let i = 0; i < io.KeysDown.length; ++i) { - io.KeysDown[i] = false; - } - for (let i = 0; i < io.MouseDown.length; ++i) { - io.MouseDown[i] = false; + for (var io of contextIOs) + { + io.KeyCtrl = false; + io.KeyShift = false; + io.KeyAlt = false; + io.KeySuper = false; + for (let i = 0; i < io.KeysDown.length; ++i) { + io.KeysDown[i] = false; + } + for (let i = 0; i < io.MouseDown.length; ++i) { + io.MouseDown[i] = false; + } } } function canvas_on_keydown(event: KeyboardEvent): void { // console.log(event.type, event.key, event.keyCode); - const io = ImGui.GetIO(); - io.KeyCtrl = event.ctrlKey; - io.KeyShift = event.shiftKey; - io.KeyAlt = event.altKey; - io.KeySuper = event.metaKey; - ImGui.IM_ASSERT(event.keyCode >= 0 && event.keyCode < ImGui.IM_ARRAYSIZE(io.KeysDown)); - io.KeysDown[event.keyCode] = true; - // forward to the keypress event - if (/*io.WantCaptureKeyboard ||*/ event.key === "Tab") { - event.preventDefault(); + for (var io of contextIOs) + { + io.KeyCtrl = event.ctrlKey; + io.KeyShift = event.shiftKey; + io.KeyAlt = event.altKey; + io.KeySuper = event.metaKey; + ImGui.IM_ASSERT(event.keyCode >= 0 && event.keyCode < ImGui.IM_ARRAYSIZE(io.KeysDown)); + io.KeysDown[event.keyCode] = true; + // forward to the keypress event + if (/*io.WantCaptureKeyboard ||*/ event.key === "Tab") { + event.preventDefault(); + } } } function canvas_on_keyup(event: KeyboardEvent): void { // console.log(event.type, event.key, event.keyCode); - const io = ImGui.GetIO(); - io.KeyCtrl = event.ctrlKey; - io.KeyShift = event.shiftKey; - io.KeyAlt = event.altKey; - io.KeySuper = event.metaKey; - ImGui.IM_ASSERT(event.keyCode >= 0 && event.keyCode < ImGui.IM_ARRAYSIZE(io.KeysDown)); - io.KeysDown[event.keyCode] = false; - if (io.WantCaptureKeyboard) { - event.preventDefault(); + for (var io of contextIOs) + { + io.KeyCtrl = event.ctrlKey; + io.KeyShift = event.shiftKey; + io.KeyAlt = event.altKey; + io.KeySuper = event.metaKey; + ImGui.IM_ASSERT(event.keyCode >= 0 && event.keyCode < ImGui.IM_ARRAYSIZE(io.KeysDown)); + io.KeysDown[event.keyCode] = false; + if (io.WantCaptureKeyboard) { + event.preventDefault(); + } } } function canvas_on_keypress(event: KeyboardEvent): void { // console.log(event.type, event.key, event.keyCode); - const io = ImGui.GetIO(); - io.AddInputCharacter(event.charCode); - if (io.WantCaptureKeyboard) { - event.preventDefault(); + for (var io of contextIOs) + { + io.AddInputCharacter(event.charCode); + if (io.WantCaptureKeyboard) { + event.preventDefault(); + } } } @@ -483,24 +493,26 @@ const mouse_button_map: number[] = [ 0, 2, 1, 3, 4 ]; function canvas_on_contextmenu(event: Event): void { - const io = ImGui.GetIO(); - if (io.WantCaptureMouse) { - event.preventDefault(); + for (var io of contextIOs) + { + if (io.WantCaptureMouse) { event.preventDefault(); } } } function canvas_on_wheel(event: WheelEvent): void { - const io = ImGui.GetIO(); - let scale: number = 1.0; - switch (event.deltaMode) { - case event.DOM_DELTA_PIXEL: scale = 0.01; break; - case event.DOM_DELTA_LINE: scale = 0.2; break; - case event.DOM_DELTA_PAGE: scale = 1.0; break; - } - io.MouseWheelH = event.deltaX * scale; - io.MouseWheel = -event.deltaY * scale; // Mouse wheel: 1 unit scrolls about 5 lines text. - if (io.WantCaptureMouse) { - event.preventDefault(); + for (var io of contextIOs) + { + let scale: number = 1.0; + switch (event.deltaMode) { + case event.DOM_DELTA_PIXEL: scale = 0.01; break; + case event.DOM_DELTA_LINE: scale = 0.2; break; + case event.DOM_DELTA_PAGE: scale = 1.0; break; + } + io.MouseWheelH = event.deltaX * scale; + io.MouseWheel = -event.deltaY * scale; // Mouse wheel: 1 unit scrolls about 5 lines text. + if (io.WantCaptureMouse) { + event.preventDefault(); + } } }