diff --git a/imgui/api/imgui.script_api b/imgui/api/imgui.script_api index d4b42e5..1c4ec2c 100644 --- a/imgui/api/imgui.script_api +++ b/imgui/api/imgui.script_api @@ -1244,6 +1244,38 @@ - name: mousewheel type: number +#***************************************************************************************************** + + - name: set_mouse_button + type: function + + parameters: + - name: index + type: number + desc: one of imgui.MOUSEBUTTON_LEFT, imgui.MOUSEBUTTON_MIDDLE or imgui.MOUSEBUTTON_RIGHT + - name: state + type: number + +#***************************************************************************************************** + + - name: set_mouse_wheel + type: function + + parameters: + - name: mousewheel + type: number + +#***************************************************************************************************** + + - name: set_mouse_pos + type: function + + parameters: + - name: x + type: number + - name: y + type: number + #***************************************************************************************************** - name: set_key_down diff --git a/imgui/imgui.script b/imgui/imgui.script index 4639504..30748c6 100644 --- a/imgui/imgui.script +++ b/imgui/imgui.script @@ -15,13 +15,6 @@ function init(self) if self.acquire_input_focus then msg.post(".", "acquire_input_focus") end - self.actions = {} - self.mouse = { - x = 0, - y = 0, - wheel = 0, - buttons = {}, - } end function final(self) @@ -33,14 +26,6 @@ end function update(self, dt) local w, h = window.get_size() imgui.set_display_size(w, h) - - imgui.set_mouse_input( - self.mouse.x, h - self.mouse.y, - self.mouse.buttons[LEFT_MOUSE] or 0, - self.mouse.buttons[RIGHT_MOUSE] or 0, - self.mouse.buttons[MIDDLE_MOUSE] or 0, - self.mouse.wheel - ) end local IMGUI_KEYS = { @@ -70,16 +55,28 @@ local IMGUI_KEYS = { function on_input(self, action_id, action) - if action_id == LEFT_MOUSE or action_id == MIDDLE_MOUSE or action_id == RIGHT_MOUSE then + if action_id == LEFT_MOUSE then + if action.pressed then + imgui.set_mouse_button(imgui.MOUSEBUTTON_LEFT, 1) + elseif action.released then + imgui.set_mouse_button(imgui.MOUSEBUTTON_LEFT, 0) + end + elseif action_id == MIDDLE_MOUSE then + if action.pressed then + imgui.set_mouse_button(imgui.MOUSEBUTTON_MIDDLE, 1) + elseif action.released then + imgui.set_mouse_button(imgui.MOUSEBUTTON_MIDDLE, 0) + end + elseif action_id == RIGHT_MOUSE then if action.pressed then - self.mouse.buttons[action_id] = 1 + imgui.set_mouse_button(imgui.MOUSEBUTTON_RIGHT, 1) elseif action.released then - self.mouse.buttons[action_id] = 0 + imgui.set_mouse_button(imgui.MOUSEBUTTON_RIGHT, 0) end elseif action_id == WHEEL_UP then - self.mouse.wheel = action.value + imgui.set_mouse_wheel(action.value) elseif action_id == WHEEL_DOWN then - self.mouse.wheel = -action.value + imgui.set_mouse_wheel(-action.value) elseif action_id == TEXT then imgui.add_input_character(action.text) elseif action_id == KEY_SHIFT then @@ -108,7 +105,9 @@ function on_input(self, action_id, action) end if not action_id then - self.mouse.x = action.screen_x - self.mouse.y = action.screen_y + local w, h = window.get_size() + local x = action.screen_x + local y = h - action.screen_y + imgui.set_mouse_pos(x, y) end end diff --git a/imgui/src/extension_imgui.cpp b/imgui/src/extension_imgui.cpp index 0c4a39d..dd20860 100644 --- a/imgui/src/extension_imgui.cpp +++ b/imgui/src/extension_imgui.cpp @@ -374,6 +374,41 @@ static int imgui_SetMouseInput(lua_State* L) return 0; } +static int imgui_SetMousePos(lua_State* L) +{ + DM_LUA_STACK_CHECK(L, 0); + + ImGuiIO& io = ImGui::GetIO(); + + const ImVec2 mouse_pos_backup = io.MousePos; + io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); + + if (io.WantSetMousePos) + { + return luaL_error(L, "WantSetMousePos not supported yet."); + } + else + { + io.MousePos = ImVec2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)); + } + + return 0; +} +static int imgui_SetMouseButton(lua_State* L) +{ + DM_LUA_STACK_CHECK(L, 0); + ImGuiIO& io = ImGui::GetIO(); + int index = luaL_checknumber(L, 1); + io.MouseDown[index] = luaL_checknumber(L, 2); + return 0; +} +static int imgui_SetMouseWheel(lua_State* L) +{ + DM_LUA_STACK_CHECK(L, 0); + ImGuiIO& io = ImGui::GetIO(); + io.MouseWheel += luaL_checknumber(L, 1); + return 0; +} static int imgui_SetKeyDown(lua_State* L) { DM_LUA_STACK_CHECK(L, 0); @@ -2275,6 +2310,9 @@ static const luaL_reg Module_methods[] = {"demo", imgui_Demo}, {"set_mouse_input", imgui_SetMouseInput}, + {"set_mouse_pos", imgui_SetMousePos}, + {"set_mouse_button", imgui_SetMouseButton}, + {"set_mouse_wheel", imgui_SetMouseWheel}, {"set_key_down", imgui_SetKeyDown}, {"set_key_modifier_ctrl", imgui_SetKeyModifierCtrl}, {"set_key_modifier_shift", imgui_SetKeyModifierShift},