Skip to content

Commit

Permalink
use screen mouse position instead of input events
Browse files Browse the repository at this point in the history
  • Loading branch information
pkdawson committed Dec 2, 2022
1 parent 489d916 commit b7137fe
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
13 changes: 11 additions & 2 deletions addons/imgui-godot/ImGuiGodot/ImGuiLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,22 @@ private void OnChangeVisibility()
if (Visible)
{
ProcessMode = ProcessModeEnum.Always;
// TODO: show all windows
// TODO: fix position with multiple monitors
//foreach (Node child in GetChildren())
//{
// if (child is Window w)
// w.Show();
//}
}
else
{
ProcessMode = ProcessModeEnum.Disabled;
Internal.Renderer.OnHide();
// TODO: hide all windows
//foreach (Node child in GetChildren())
//{
// if (child is Window w)
// w.Hide();
//}
}
}

Expand Down
34 changes: 15 additions & 19 deletions addons/imgui-godot/ImGuiGodot/Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,22 @@ public static void Update(double delta, Viewport vp)

if (io.WantSetMousePos)
{
Input.WarpMouse(new(io.MousePos.X, io.MousePos.Y));
// TODO: get current focused window
// Input.WarpMouse(new(io.MousePos.X, io.MousePos.Y));
}
else
{
var mousePos = DisplayServer.MouseGetPosition();
io.AddMousePosEvent(mousePos.x, mousePos.y);
}

MouseButton mbs = DisplayServer.MouseGetButtonState();
io.AddMouseButtonEvent((int)ImGuiMouseButton.Left, mbs.HasFlag(MouseButton.Left));
io.AddMouseButtonEvent((int)ImGuiMouseButton.Right, mbs.HasFlag(MouseButton.Right));
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle, mbs.HasFlag(MouseButton.Middle));
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle + 1, mbs.HasFlag(MouseButton.Xbutton1));
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle + 2, mbs.HasFlag(MouseButton.Xbutton2));

// scrolling works better if we allow no more than one event per frame
if (_mouseWheel != Vector2.Zero)
{
Expand Down Expand Up @@ -326,31 +339,14 @@ public static bool ProcessInput(InputEvent evt, Window window)

bool consumed = false;

if (evt is InputEventMouseMotion mm)
if (evt is InputEventMouseMotion)
{
GD.Print($"{windowPos.x + mm.GlobalPosition.x}, {windowPos.y + mm.GlobalPosition.y}");
io.AddMousePosEvent(windowPos.x + mm.GlobalPosition.x, windowPos.y + mm.GlobalPosition.y);
consumed = io.WantCaptureMouse;
}
else if (evt is InputEventMouseButton mb)
{
switch (mb.ButtonIndex)
{
case MouseButton.Left:
io.AddMouseButtonEvent((int)ImGuiMouseButton.Left, mb.Pressed);
break;
case MouseButton.Right:
io.AddMouseButtonEvent((int)ImGuiMouseButton.Right, mb.Pressed);
break;
case MouseButton.Middle:
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle, mb.Pressed);
break;
case MouseButton.Xbutton1:
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle + 1, mb.Pressed);
break;
case MouseButton.Xbutton2:
io.AddMouseButtonEvent((int)ImGuiMouseButton.Middle + 2, mb.Pressed);
break;
case MouseButton.WheelUp:
_mouseWheel.y = mb.Factor;
break;
Expand Down

0 comments on commit b7137fe

Please sign in to comment.