summaryrefslogtreecommitdiff
path: root/imgui_helper_widgets.cpp
blob: 17a9193fd579b062de0fa4d3bd132cd24aa09d46 (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
// Widgets not directly related to drawing UI.

// Returns a normalized UV position of the composition
static v2
ImGui_ScreenPointToCompUV(ImVec2 ViewportMin, ImVec2 CompPos, ImVec2 CompZoom, ImVec2 MousePos)
{
    ImVec2 LocalMousePos = MousePos - ViewportMin;
    ImVec2 LocalCompPos = CompPos - ViewportMin;
    ImVec2 MouseScreenUV = LocalMousePos - LocalCompPos;
    ImVec2 Result = MouseScreenUV / CompZoom;
    return V2(Result);
}

// NOTE(fox): We have to do a bit of hackery here to tell how many times the
// mouse has been warped during a drag, since it doesn't seem like we can rely
// on SDL_WarpMouseGlobal to update on the first frame of a WantSetPos request.

static void
ImGui_WarpMouse(ui *UI, ImVec2 MousePos, ImVec2 Min, ImVec2 Max, int Direction = 3)
{
    if (Direction & 1) {
        if (MousePos.x < Min.x) {
            UI->Warp_WantSetPos = true;
            UI->Warp_PositionToSet = ImVec2(Max.x - 5, MousePos.y);
            UI->Warp_PositionInitial = MousePos.x;
            UI->Warp_Direction = 0;
        }
        if (MousePos.x > Max.x) {
            UI->Warp_WantSetPos = true;
            UI->Warp_PositionToSet = ImVec2(Min.x + 5, MousePos.y);
            UI->Warp_PositionInitial = MousePos.x;
            UI->Warp_Direction = 1;
        }
    }
    if (Direction & 2) {
        if (MousePos.y < Min.y) {
            UI->Warp_WantSetPos = true;
            UI->Warp_PositionToSet = ImVec2(MousePos.x, Max.y - 5);
            UI->Warp_PositionInitial = MousePos.y;
            UI->Warp_Direction = 2;
        }
        if (MousePos.y > Max.y) {
            UI->Warp_WantSetPos = true;
            UI->Warp_PositionToSet = ImVec2(MousePos.x, Min.y + 5);
            UI->Warp_PositionInitial = MousePos.y;
            UI->Warp_Direction = 3;
        }
    }
}

// We record the initial position and the direction of the wrap, and only
// increment the wrap amount when MousePos actually is measured to be what we expect.

static void
ImGui_WarpMouseFinish(ui *UI, ImVec2 MousePos)
{
    if (UI->Warp_Direction == 0) {
        if (MousePos.x < UI->Warp_PositionInitial) UI->Warp_X--;
    } else if (UI->Warp_Direction == 1) {
        if (MousePos.x > UI->Warp_PositionInitial) UI->Warp_X++;
    } else if (UI->Warp_Direction == 2) {
        if (MousePos.y < UI->Warp_PositionInitial) UI->Warp_Y--;
    } else if (UI->Warp_Direction == 3) {
        if (MousePos.y > UI->Warp_PositionInitial) UI->Warp_Y++;
    } else {
        Assert(0);
    }
}

static ImVec2
ImGui_Brush_CalcMousePos(project_state *State, ImGuiIO &io, ImVec2 MouseDelta, int32 i, real32 DeltaDistance, real32 DeltaSlope)
{
    ImVec2 MousePos;
    if (State->Brush.Type == brush_normal) {
        MousePos = io.MousePos - (MouseDelta * (i / DeltaDistance));
    } else if (State->Brush.Type == brush_wacky1) {
        MousePos = io.MousePos + (io.MousePos * (i / MouseDelta));
    } else if (State->Brush.Type == brush_wacky2) {
        MousePos = io.MousePos - (MouseDelta / (i / DeltaDistance));
    } else if (State->Brush.Type == brush_wacky3) {
        MousePos = io.MousePos - (MouseDelta * (i / ImVec2(MouseDelta.y, MouseDelta.x)));
    } else {
        Assert(0);
    }
    return MousePos;
}