tracy/server/TracyMouse.cpp

91 lines
2.3 KiB
C++
Raw Permalink Normal View History

2020-08-01 10:35:15 +00:00
#include <cmath>
2020-08-01 09:49:43 +00:00
#include "TracyMouse.hpp"
2021-05-17 10:07:06 +00:00
#include "imgui_internal.h"
2020-08-01 10:02:11 +00:00
2020-08-01 09:49:43 +00:00
namespace tracy
{
2020-08-01 10:02:11 +00:00
static constexpr int MouseButtons = IM_ARRAYSIZE( ImGuiContext::IO.MouseDown );
2020-08-01 10:55:49 +00:00
static constexpr float MouseDragThreshold = 2;
2020-08-01 10:02:11 +00:00
struct Mouse
{
bool mouseDown[MouseButtons];
bool mouseClicked[MouseButtons];
2020-08-01 10:35:15 +00:00
bool mouseReleased[MouseButtons];
2020-08-01 10:02:11 +00:00
bool mouseDragging[MouseButtons];
ImVec2 mouseDragDelta[MouseButtons];
2020-08-01 10:35:15 +00:00
bool mousePotentialClickRelease[MouseButtons];
2020-08-01 10:02:11 +00:00
};
2020-08-01 10:35:15 +00:00
static Mouse s_mouse = {};
2020-08-01 10:02:11 +00:00
void MouseFrame()
{
for( int i=0; i<MouseButtons; i++ )
{
s_mouse.mouseDown[i] = ImGui::IsMouseDown( i );
s_mouse.mouseClicked[i] = ImGui::IsMouseClicked( i );
2020-08-01 10:35:15 +00:00
s_mouse.mouseReleased[i] = ImGui::IsMouseReleased( i );
2020-08-01 10:02:11 +00:00
s_mouse.mouseDragging[i] = ImGui::IsMouseDragging( i, 0 );
s_mouse.mouseDragDelta[i] = ImGui::GetMouseDragDelta( i, 0 );
2020-08-01 10:35:15 +00:00
if( s_mouse.mouseDragging[i] )
{
if( s_mouse.mouseClicked[i] || s_mouse.mousePotentialClickRelease[i] )
{
2020-08-01 10:55:49 +00:00
if( std::abs( s_mouse.mouseDragDelta[i].x ) < MouseDragThreshold && std::abs( s_mouse.mouseDragDelta[i].y ) < MouseDragThreshold )
2020-08-01 10:35:15 +00:00
{
s_mouse.mouseDragging[i] = false;
}
else
{
s_mouse.mousePotentialClickRelease[i] = false;
}
}
}
else if( !s_mouse.mouseDown[i] && !s_mouse.mouseReleased[i] )
{
s_mouse.mousePotentialClickRelease[i] = false;
}
2020-08-01 10:02:11 +00:00
}
}
2020-08-01 09:49:43 +00:00
bool IsMouseDown( ImGuiMouseButton button )
{
2020-08-01 10:02:11 +00:00
return s_mouse.mouseDown[button];
2020-08-01 09:49:43 +00:00
}
bool IsMouseClicked( ImGuiMouseButton button )
{
2020-08-01 10:02:11 +00:00
return s_mouse.mouseClicked[button];
2020-08-01 09:49:43 +00:00
}
2020-08-01 10:03:11 +00:00
bool IsMouseDragging( ImGuiMouseButton button )
2020-08-01 09:49:43 +00:00
{
2020-08-01 10:02:11 +00:00
return s_mouse.mouseDragging[button];
2020-08-01 09:49:43 +00:00
}
2020-08-01 10:03:11 +00:00
ImVec2 GetMouseDragDelta( ImGuiMouseButton button )
2020-08-01 09:49:43 +00:00
{
2020-08-01 10:02:11 +00:00
return s_mouse.mouseDragDelta[button];
2020-08-01 09:49:43 +00:00
}
2020-08-01 10:10:59 +00:00
void ConsumeMouseEvents( ImGuiMouseButton button )
{
s_mouse.mouseDown[button] = false;
s_mouse.mouseClicked[button] = false;
s_mouse.mouseDragging[button] = false;
}
2020-08-01 10:35:15 +00:00
bool IsMouseClickReleased( ImGuiMouseButton button )
{
if( s_mouse.mouseReleased[button] && s_mouse.mousePotentialClickRelease[button] ) return true;
if( s_mouse.mouseClicked[button] ) s_mouse.mousePotentialClickRelease[button] = true;
return false;
}
2020-08-01 09:49:43 +00:00
}