Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating demo with ImGuiWindowFlags_ResizeFromAnySide (was: Adding mouse cursor support in backends) #1495

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion examples/directx10_example/imgui_impl_dx10.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,43 @@ void ImGui_ImplDX10_NewFrame()
}

// Hide OS mouse cursor if ImGui is drawing it
if (io.MouseDrawCursor)
if(io.MouseDrawCursor)
{
SetCursor(NULL);
}
else
{
HCURSOR cursor = NULL;

switch(ImGui::GetMouseCursor())
{
case ImGuiMouseCursor_None:
break;
case ImGuiMouseCursor_Arrow:
cursor = LoadCursor(NULL, IDC_ARROW);
break;
case ImGuiMouseCursor_TextInput:
cursor = LoadCursor(NULL, IDC_IBEAM);
break;
case ImGuiMouseCursor_Move:
cursor = LoadCursor(NULL, IDC_SIZEALL);
break;
case ImGuiMouseCursor_ResizeNS:
cursor = LoadCursor(NULL, IDC_SIZENS);
break;
case ImGuiMouseCursor_ResizeEW:
cursor = LoadCursor(NULL, IDC_SIZEWE);
break;
case ImGuiMouseCursor_ResizeNESW:
cursor = LoadCursor(NULL, IDC_SIZENESW);
break;
case ImGuiMouseCursor_ResizeNWSE:
cursor = LoadCursor(NULL, IDC_SIZENWSE);
break;
}

SetCursor(cursor);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
ImGui::NewFrame();
Expand Down
94 changes: 91 additions & 3 deletions examples/directx11_example/imgui_impl_dx11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ static ID3D11BlendState* g_pBlendState = NULL;
static ID3D11DepthStencilState* g_pDepthStencilState = NULL;
static int g_VertexBufferSize = 5000, g_IndexBufferSize = 10000;

static HCURSOR g_NativeCursors[ImGuiMouseCursor_Count_] = {NULL};
static bool g_WindowsSetCursorThisFrame = false;
ImGuiMouseCursor g_LastCursorSet = ImGuiMouseCursor_None;

struct VERTEX_CONSTANT_BUFFER
{
float mvp[4][4];
Expand Down Expand Up @@ -249,9 +253,23 @@ static bool IsAnyMouseButtonDown()
// PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
char buffer[1024];

ImGuiIO& io = ImGui::GetIO();
switch (msg)
{
case WM_CREATE:
{
g_NativeCursors[ImGuiMouseCursor_Arrow] = LoadCursor(NULL, IDC_ARROW);
g_NativeCursors[ImGuiMouseCursor_TextInput] = LoadCursor(NULL, IDC_IBEAM);
g_NativeCursors[ImGuiMouseCursor_Move] = LoadCursor(NULL, IDC_SIZEALL);
g_NativeCursors[ImGuiMouseCursor_ResizeNS] = LoadCursor(NULL, IDC_SIZENS);
g_NativeCursors[ImGuiMouseCursor_ResizeEW] = LoadCursor(NULL, IDC_SIZEWE);
g_NativeCursors[ImGuiMouseCursor_ResizeNESW] = LoadCursor(NULL, IDC_SIZENESW);
g_NativeCursors[ImGuiMouseCursor_ResizeNWSE] = LoadCursor(NULL, IDC_SIZENWSE);

return 0;
}
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
Expand Down Expand Up @@ -300,6 +318,65 @@ IMGUI_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wPa
if (wParam > 0 && wParam < 0x10000)
io.AddInputCharacter((unsigned short)wParam);
return 0;
case WM_SETCURSOR:
if(g_hWnd == (HWND)wParam)
{
if(!io.MouseDrawCursor)
{
switch(LOWORD(lParam))
{
case HTCLIENT: //window area
SetCursor(g_NativeCursors[ImGui::GetMouseCursor()]);
g_LastCursorSet = ImGui::GetMouseCursor();
break;
case HTNOWHERE:
case HTCAPTION: //title bar
case HTMENU:
case HTSYSMENU: //window icon title bar
case HTMINBUTTON: //minimize button
case HTMAXBUTTON: //maximize button
case HTBORDER:
case HTCLOSE: //close window button
SetCursor(g_NativeCursors[ImGuiMouseCursor_Arrow]);
ImGui::SetMouseCursor(ImGuiMouseCursor_Arrow);
break;
case HTLEFT: //left border
case HTRIGHT: //right border
SetCursor(g_NativeCursors[ImGuiMouseCursor_ResizeEW]);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeEW);
break;
case HTTOP: //top border
case HTBOTTOM: //bottom border
SetCursor(g_NativeCursors[ImGuiMouseCursor_ResizeNS]);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
break;
case HTTOPRIGHT: //top right
case HTBOTTOMLEFT: //bottom left
SetCursor(g_NativeCursors[ImGuiMouseCursor_ResizeNESW]);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNS);
break;
case HTTOPLEFT: //top left
case HTBOTTOMRIGHT: //bottom right
SetCursor(g_NativeCursors[ImGuiMouseCursor_ResizeNWSE]);
ImGui::SetMouseCursor(ImGuiMouseCursor_ResizeNWSE);
break;
}
}
else
{
SetCursor(NULL);
}

g_WindowsSetCursorThisFrame = true;

return TRUE;
}
else
{
return FALSE;
}

return 0;
}
return 0;
}
Expand Down Expand Up @@ -608,9 +685,20 @@ void ImGui_ImplDX11_NewFrame()
SetCursorPos(pos.x, pos.y);
}

// Hide OS mouse cursor if ImGui is drawing it
if (io.MouseDrawCursor)
SetCursor(NULL);
if(!g_WindowsSetCursorThisFrame && ImGui::GetMouseCursor() != g_LastCursorSet)
{
if(io.MouseDrawCursor)
{
SetCursor(NULL);
}
else
{
assert(ImGui::GetMouseCursor() != ImGuiMouseCursor_None);
SetCursor(g_NativeCursors[ImGui::GetMouseCursor()]);
}
}

g_WindowsSetCursorThisFrame = false;

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
ImGui::NewFrame();
Expand Down
37 changes: 36 additions & 1 deletion examples/directx9_example/imgui_impl_dx9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,43 @@ void ImGui_ImplDX9_NewFrame()
}

// Hide OS mouse cursor if ImGui is drawing it
if (io.MouseDrawCursor)
if(io.MouseDrawCursor)
{
SetCursor(NULL);
}
else
{
HCURSOR cursor = NULL;

switch(ImGui::GetMouseCursor())
{
case ImGuiMouseCursor_None:
break;
case ImGuiMouseCursor_Arrow:
cursor = LoadCursor(NULL, IDC_ARROW);
break;
case ImGuiMouseCursor_TextInput:
cursor = LoadCursor(NULL, IDC_IBEAM);
break;
case ImGuiMouseCursor_Move:
cursor = LoadCursor(NULL, IDC_SIZEALL);
break;
case ImGuiMouseCursor_ResizeNS:
cursor = LoadCursor(NULL, IDC_SIZENS);
break;
case ImGuiMouseCursor_ResizeEW:
cursor = LoadCursor(NULL, IDC_SIZEWE);
break;
case ImGuiMouseCursor_ResizeNESW:
cursor = LoadCursor(NULL, IDC_SIZENESW);
break;
case ImGuiMouseCursor_ResizeNWSE:
cursor = LoadCursor(NULL, IDC_SIZENWSE);
break;
}

SetCursor(cursor);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
ImGui::NewFrame();
Expand Down
52 changes: 51 additions & 1 deletion examples/opengl2_example/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@

// Data
static GLFWwindow* g_Window = NULL;
static GLFWcursor* g_ArrowCursor = NULL;
static GLFWcursor* g_VResizeCursor = NULL;
static GLFWcursor* g_HResizeCursor = NULL;
static GLFWcursor* g_IBeamCursor = NULL;
static GLFWcursor* g_HandCursor = NULL;
static GLFWcursor* g_CrosshairCursor = NULL;
static double g_Time = 0.0f;
static bool g_MouseJustPressed[3] = { false, false, false };
static float g_MouseWheel = 0.0f;
Expand Down Expand Up @@ -202,6 +208,12 @@ void ImGui_ImplGlfwGL2_InvalidateDeviceObjects()
bool ImGui_ImplGlfwGL2_Init(GLFWwindow* window, bool install_callbacks)
{
g_Window = window;
g_ArrowCursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_IBeamCursor = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
g_CrosshairCursor = glfwCreateStandardCursor(GLFW_CROSSHAIR_CURSOR);
g_HandCursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
g_HResizeCursor = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
g_VResizeCursor = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);

ImGuiIO& io = ImGui::GetIO();
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
Expand Down Expand Up @@ -300,7 +312,45 @@ void ImGui_ImplGlfwGL2_NewFrame()
g_MouseWheel = 0.0f;

// Hide OS mouse cursor if ImGui is drawing it
glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
if(io.MouseDrawCursor)
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

GLFWcursor *cursor = NULL;

switch(ImGui::GetMouseCursor())
{
case ImGuiMouseCursor_None:
break;
case ImGuiMouseCursor_Arrow:
cursor = g_ArrowCursor;
break;
case ImGuiMouseCursor_TextInput:
cursor = g_IBeamCursor;
break;
case ImGuiMouseCursor_Move:
cursor = g_HandCursor;
break;
case ImGuiMouseCursor_ResizeNS:
cursor = g_VResizeCursor;
break;
case ImGuiMouseCursor_ResizeEW:
cursor = g_HResizeCursor;
break;
case ImGuiMouseCursor_ResizeNESW:
cursor = g_ArrowCursor;
break;
case ImGuiMouseCursor_ResizeNWSE:
cursor = g_ArrowCursor;
break;
}

glfwSetCursor(g_Window, cursor);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
ImGui::NewFrame();
Expand Down
52 changes: 51 additions & 1 deletion examples/opengl3_example/imgui_impl_glfw_gl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

// Data
static GLFWwindow* g_Window = NULL;
static GLFWcursor* g_ArrowCursor = NULL;
static GLFWcursor* g_VResizeCursor = NULL;
static GLFWcursor* g_HResizeCursor = NULL;
static GLFWcursor* g_IBeamCursor = NULL;
static GLFWcursor* g_HandCursor = NULL;
static GLFWcursor* g_CrosshairCursor = NULL;
static double g_Time = 0.0f;
static bool g_MouseJustPressed[3] = { false, false, false };
static float g_MouseWheel = 0.0f;
Expand Down Expand Up @@ -314,6 +320,12 @@ void ImGui_ImplGlfwGL3_InvalidateDeviceObjects()
bool ImGui_ImplGlfwGL3_Init(GLFWwindow* window, bool install_callbacks)
{
g_Window = window;
g_ArrowCursor = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
g_IBeamCursor = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
g_CrosshairCursor = glfwCreateStandardCursor(GLFW_CROSSHAIR_CURSOR);
g_HandCursor = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
g_HResizeCursor = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
g_VResizeCursor = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);

ImGuiIO& io = ImGui::GetIO();
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB; // Keyboard mapping. ImGui will use those indices to peek into the io.KeyDown[] array.
Expand Down Expand Up @@ -412,7 +424,45 @@ void ImGui_ImplGlfwGL3_NewFrame()
g_MouseWheel = 0.0f;

// Hide OS mouse cursor if ImGui is drawing it
glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
if(io.MouseDrawCursor)
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
else
{
glfwSetInputMode(g_Window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

GLFWcursor *cursor = NULL;

switch(ImGui::GetMouseCursor())
{
case ImGuiMouseCursor_None:
break;
case ImGuiMouseCursor_Arrow:
cursor = g_ArrowCursor;
break;
case ImGuiMouseCursor_TextInput:
cursor = g_IBeamCursor;
break;
case ImGuiMouseCursor_Move:
cursor = g_HandCursor;
break;
case ImGuiMouseCursor_ResizeNS:
cursor = g_VResizeCursor;
break;
case ImGuiMouseCursor_ResizeEW:
cursor = g_HResizeCursor;
break;
case ImGuiMouseCursor_ResizeNESW:
cursor = g_ArrowCursor;
break;
case ImGuiMouseCursor_ResizeNWSE:
cursor = g_ArrowCursor;
break;
}

glfwSetCursor(g_Window, cursor);
}

// Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
ImGui::NewFrame();
Expand Down
15 changes: 9 additions & 6 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,17 @@ void ImGui::ShowTestWindow(bool* p_open)
static bool no_resize = false;
static bool no_collapse = false;
static bool no_close = false;
static bool resize_any_side = false;

// Demonstrate the various window flags. Typically you would just use the default.
ImGuiWindowFlags window_flags = 0;
if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar;
if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar;
if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar;
if (no_move) window_flags |= ImGuiWindowFlags_NoMove;
if (no_resize) window_flags |= ImGuiWindowFlags_NoResize;
if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse;
if (no_titlebar) window_flags |= ImGuiWindowFlags_NoTitleBar;
if (no_scrollbar) window_flags |= ImGuiWindowFlags_NoScrollbar;
if (!no_menu) window_flags |= ImGuiWindowFlags_MenuBar;
if (no_move) window_flags |= ImGuiWindowFlags_NoMove;
if (no_resize) window_flags |= ImGuiWindowFlags_NoResize;
if (no_collapse) window_flags |= ImGuiWindowFlags_NoCollapse;
if (resize_any_side) window_flags |= ImGuiWindowFlags_ResizeFromAnySide;
if (no_close) p_open = NULL; // Don't pass our bool* to Begin

ImGui::SetNextWindowSize(ImVec2(550,680), ImGuiCond_FirstUseEver);
Expand Down Expand Up @@ -245,6 +247,7 @@ void ImGui::ShowTestWindow(bool* p_open)
ImGui::Checkbox("No resize", &no_resize); ImGui::SameLine(300);
ImGui::Checkbox("No collapse", &no_collapse);
ImGui::Checkbox("No close", &no_close);
ImGui::Checkbox("Resize from any side", &resize_any_side);

if (ImGui::TreeNode("Style"))
{
Expand Down