Skip to content

Commit

Permalink
Set mouse states independently
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
britzl committed Jan 28, 2024
1 parent 6ca062c commit fca0e4b
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 22 deletions.
32 changes: 32 additions & 0 deletions imgui/api/imgui.script_api
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 21 additions & 22 deletions imgui/imgui.script
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
38 changes: 38 additions & 0 deletions imgui/src/extension_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit fca0e4b

Please sign in to comment.