aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui.c
blob: 72956a5df1a2be8dc5d83c135f133240917604f2 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "ui.h"

FloatingWindow createFloatingWindow()
{
  return (FloatingWindow){
    .rect = (Rectangle){0.0, 0.0, 100.0, 100.0},
    .minimized = false,
    .moving = false,
    .resizing = false,
    .callback = NULL,
    .contentSize = Vector2Zero(),
    .scroll = Vector2Zero()
  };
}

void updateFloatingWindow(FloatingWindow* window, const char* title)
{
#ifndef RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT
#define RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT 24
#endif

#ifndef RAYGUI_WINDOW_CLOSEBUTTON_SIZE
#define RAYGUI_WINDOW_CLOSEBUTTON_SIZE 18
#endif

  int closeTitleSizeDeltaHalf = (RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT -
                                 RAYGUI_WINDOW_CLOSEBUTTON_SIZE) / 2;

  // Window movement and resize input and collision check.
  if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && !window->moving
      && !window->resizing)
  {
    Vector2 mousePosition = GetMousePosition();

    Rectangle titleCollisionRect = (Rectangle){
      window->rect.x,
      window->rect.y,
      window->rect.width - (RAYGUI_WINDOW_CLOSEBUTTON_SIZE
                            + closeTitleSizeDeltaHalf),
      RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT
    };

    Rectangle resizeCollisionRect = (Rectangle){
      window->rect.x + window->rect.width - 20,
      window->rect.y + window->rect.height - 20,
      20,
      20
    };

    if (CheckCollisionPointRec(mousePosition, titleCollisionRect))
    {
      window->moving = true;
    }
    else if (!window->minimized
             && CheckCollisionPointRec(mousePosition, resizeCollisionRect))
    {
      window->resizing = true;
    }
  }

  // Window movement and resize update.
  if (window->moving)
  {
    Vector2 mouseDelta = GetMouseDelta();
    window->rect.x += mouseDelta.x;
    window->rect.y += mouseDelta.y;

    if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
    {
      window->moving = false;

      // Clamp window position keep it inside the application area.
      if (window->rect.x < 0)
      {
        window->rect.x = 0;
      }
      else if (window->rect.x > GetScreenWidth() - window->rect.width)
      {
        window->rect.x = GetScreenWidth() - window->rect.width;
      }
      
      if (window->rect.y < 0)
      {
        window->rect.y = 0;
      }
      else if (window->rect.y > GetScreenHeight())
      {
        window->rect.y = GetScreenHeight() - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT;
      }
    }
  }
  else if (window->resizing)
  {
    Vector2 mousePosition = GetMousePosition();
    
    if (mousePosition.x > window->rect.x)
    {
      window->rect.width = mousePosition.x - window->rect.x;
    }
    if (mousePosition.y > window->rect.y)
    {
      window->rect.height = mousePosition.y - window->rect.y;
    }

    // Clamp window size to an arbitrary minimum value and the window size as
    // the maximum.
    if (window->rect.width < 100)
    {
      window->rect.width = 100;
    }
    else if (window->rect.width > GetScreenWidth())
    {
      window->rect.width = GetScreenWidth();
    }
    
    if (window->rect.height < 100)
    {
      window->rect.height = 100;
    }
    else if (window->rect.height > GetScreenHeight())
    {
      window->rect.height = GetScreenHeight();
    }

    if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
    {
      window->resizing = false;
    }
  }

  // window and content drawing with scissor and scroll area
  if(window->minimized)
  {
    GuiStatusBar(
      (Rectangle){window->rect.x, window->rect.y,
                  window->rect.width, RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT},
      title);

    if (GuiButton(
          (Rectangle){window->rect.x + window->rect.width
                      - RAYGUI_WINDOW_CLOSEBUTTON_SIZE
                      - closeTitleSizeDeltaHalf,
                      window->rect.y + closeTitleSizeDeltaHalf,
                      RAYGUI_WINDOW_CLOSEBUTTON_SIZE,
                      RAYGUI_WINDOW_CLOSEBUTTON_SIZE},
          "#120#"))
    {
      window->minimized = false;
    }

  } else {
    window->minimized = GuiWindowBox(
      (Rectangle){window->rect.x, window->rect.y, window->rect.width,
                  window->rect.height}, title);

    // Scissor and draw content within a scroll panel.
    if (window->callback != NULL)
    {
      Rectangle scissor;
      
      GuiScrollPanel(
        (Rectangle){
          window->rect.x, window->rect.y + RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT,
          window->rect.width,
          window->rect.height - RAYGUI_WINDOWBOX_STATUSBAR_HEIGHT},
        NULL,
        (Rectangle){
          window->rect.x, window->rect.y,
          window->contentSize.x, window->contentSize.y},
        &window->scroll,
        &scissor);

      bool requireScissor = window->rect.width < window->contentSize.x
        || window->rect.height < window->contentSize.x;

      if (requireScissor)
      {
        BeginScissorMode(scissor.x, scissor.y, scissor.width, scissor.height);
      }

      window->callback((Vector2){window->rect.x, window->rect.y},
                       window->scroll);

      if (requireScissor)
      {
        EndScissorMode();
      }
    }

    // Draw the resize button/icon.
    GuiDrawIcon(71, window->rect.x + window->rect.width - 20,
                window->rect.y + window->rect.height - 20, 1, GREEN);
  }
}