Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TracyMouse.cpp « server - github.com/wolfpld/tracy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3053c2ddbe11e5f8f93428f257b3e98a5a9e8323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <cmath>

#include "TracyMouse.hpp"

#include "imgui_internal.h"

namespace tracy
{

static constexpr int MouseButtons = IM_ARRAYSIZE( ImGuiContext::IO.MouseDown );
static constexpr float MouseDragThreshold = 2;

struct Mouse
{
    bool mouseDown[MouseButtons];
    bool mouseClicked[MouseButtons];
    bool mouseReleased[MouseButtons];
    bool mouseDragging[MouseButtons];
    ImVec2 mouseDragDelta[MouseButtons];
    bool mousePotentialClickRelease[MouseButtons];
};

static Mouse s_mouse = {};

void MouseFrame()
{
    for( int i=0; i<MouseButtons; i++ )
    {
        s_mouse.mouseDown[i] = ImGui::IsMouseDown( i );
        s_mouse.mouseClicked[i] = ImGui::IsMouseClicked( i );
        s_mouse.mouseReleased[i] = ImGui::IsMouseReleased( i );
        s_mouse.mouseDragging[i] = ImGui::IsMouseDragging( i, 0 );
        s_mouse.mouseDragDelta[i] = ImGui::GetMouseDragDelta( i, 0 );

        if( s_mouse.mouseDragging[i] )
        {
            if( s_mouse.mouseClicked[i] || s_mouse.mousePotentialClickRelease[i] )
            {
                if( std::abs( s_mouse.mouseDragDelta[i].x ) < MouseDragThreshold && std::abs( s_mouse.mouseDragDelta[i].y ) < MouseDragThreshold )
                {
                    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;
        }
    }
}

bool IsMouseDown( ImGuiMouseButton button )
{
    return s_mouse.mouseDown[button];
}

bool IsMouseClicked( ImGuiMouseButton button )
{
    return s_mouse.mouseClicked[button];
}

bool IsMouseDragging( ImGuiMouseButton button )
{
    return s_mouse.mouseDragging[button];
}

ImVec2 GetMouseDragDelta( ImGuiMouseButton button )
{
    return s_mouse.mouseDragDelta[button];
}

void ConsumeMouseEvents( ImGuiMouseButton button )
{
    s_mouse.mouseDown[button] = false;
    s_mouse.mouseClicked[button] = false;
    s_mouse.mouseDragging[button] = false;
}

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;
}

}