-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
272 lines (233 loc) · 6.45 KB
/
Source.cpp
File metadata and controls
272 lines (233 loc) · 6.45 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#include "olcConsoleGameEngine.h"
class olcWindow
{
public:
olcWindow()
{
}
~olcWindow()
{
}
short x = 3;
short y = 3;
short width = 30;
short height = 20;
short headerSize = 3;
};
class olcSynthGui : public olcConsoleGameEngine
{
public:
olcSynthGui()
{
m_sAppName = L"OLC Gui Demo";
}
private:
void DrawWindows(std::vector<olcWindow> ws)
{
for (auto s = ws.begin(); s != ws.end(); s++)
{
// Check if outside of screen
if (s->x + s->width + panOffsetX < 0) // to the left
DrawLine(0, max(s->y + panOffsetY, 0), 0, min(s->y + s->height + panOffsetY, scrHght_1), PIXEL_HALF, FG_GREEN);
else if (s->x + panOffsetX > scrWdth_1) // to the right
DrawLine(scrWdth_1, max(s->y + panOffsetY, 0), scrWdth_1, min(s->y + s->height + panOffsetY, scrHght_1), PIXEL_HALF, FG_GREEN);
else if (s->y + s->height + panOffsetY < 0) // above
DrawLine(s->x + panOffsetX, 0, s->x + s->width + panOffsetX, 0, PIXEL_HALF, FG_GREEN);
else if (s->y + panOffsetY > scrHght_1) // below
DrawLine(s->x + panOffsetX, scrHght_1, s->x + s->width + panOffsetX, scrHght_1, PIXEL_HALF, FG_GREEN);
// not outside, try to draw the whole window
else if (std::next(s) == ws.end())
DrawWindow(*s, PIXEL_SOLID, FG_WHITE);
else
DrawWindow(*s, PIXEL_HALF, FG_GREY);
}
}
void DrawWindow(olcWindow &w, short glyph, short colour)
{
// Adjust window position for panning
w.x += panOffsetX;
w.y += panOffsetY;
// Fill background of window
Fill(w.x + 1, w.y + 1, w.x + w.width, w.y + w.height, PIXEL_SOLID, FG_BLACK);
// Draw lines of window
DrawLine(w.x, w.y, w.x, w.y + w.height, glyph, colour); //left
DrawLine(w.x + w.width, w.y, w.x + w.width, w.y + w.height, glyph, colour); //right
DrawLine(w.x + 1, w.y, w.x + w.width - 1, w.y, glyph, colour); //top
DrawLine(w.x + 1, w.y + w.headerSize, w.x + w.width - 1, w.y + w.headerSize, glyph, colour); //header bottom
DrawLine(w.x + 1, w.y + w.height, w.x + w.width - 1, w.y + w.height, glyph, colour); //bottom
}
public:
std::vector<olcWindow> windows;
olcWindow myFirstWindow;
int scrWdth_1;
int scrHght_1;
short dragOffsetX = 0;
short dragOffsetY = 0;
short panOffsetX = 0;
short panOffsetY = 0;
const short maxPan = 1000;
bool resizing = false;
bool dragging = false;
bool panning = false;
bool OnUserCreate() override
{
Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, FG_BLACK);
scrWdth_1 = ScreenWidth() - 1;
scrHght_1 = ScreenHeight() - 1;
windows.push_back(myFirstWindow);
DrawWindows(windows);
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
if (CheckMouseEvent())
{
Fill(0, 0, ScreenWidth(), ScreenHeight(), PIXEL_SOLID, FG_BLACK);
DrawWindows(windows);
}
return true;
}
//Rotates a window to back of vector
void SetWindowActive(int i)
{
std::vector<olcWindow>::iterator iter = windows.begin();
for (int c = 0; c < i; c++)
iter++;
std::rotate(iter, iter + 1, windows.end());
}
bool IsWithinWindow(int i)
{
return m_mousePosX - panOffsetX >= windows[i].x &&
m_mousePosX - panOffsetX <= windows[i].x + windows[i].width &&
m_mousePosY - panOffsetY >= windows[i].y &&
m_mousePosY - panOffsetY <= windows[i].y + windows[i].height;
}
void CreateNewWindow()
{
olcWindow tempWnd;
tempWnd.x = m_mousePosX - panOffsetX;
tempWnd.y = m_mousePosY - panOffsetY;
windows.push_back(tempWnd);
}
void ResizeActiveWindow()
{
if (m_mousePosY - panOffsetY - windows.back().y > windows.back().headerSize)
windows.back().height = m_mousePosY - panOffsetY - windows.back().y;
if (m_mousePosX - panOffsetX - windows.back().x > 10)
windows.back().width = m_mousePosX - panOffsetX - windows.back().x;
}
void SetPositionActiveWindow()
{
windows.back().x = m_mousePosX - dragOffsetX;
windows.back().y = m_mousePosY - dragOffsetY;
}
bool CheckMouseEvent()
{
bool foundWindow = false;
bool deleteWindow = false;
// Check right mouse release
if (m_mouse[0].bReleased)
{
resizing = false;
dragging = false;
panning = false;
}
//// Dragging ////
if (dragging)
{
SetPositionActiveWindow();
return true;
}
//// Resizing ////
if (resizing)
{
ResizeActiveWindow();
return true;
}
//// Panning ////
if (panning)
{
panOffsetX = max(min(m_mousePosX - dragOffsetX, maxPan),-maxPan);
panOffsetY = max(min(m_mousePosY - dragOffsetY, maxPan),-maxPan);
return true;
}
//// Right Mouse Button ////
if (m_mouse[1].bPressed)
{
// Create new Window if there is none.
if (windows.size() == 0)
{
CreateNewWindow();
return true;
}
// Otherwise loop through the windows.
else
{
for (int i = windows.size() - 1; i >= 0; i--)
{
// Delete a window if one is clicked.
if (IsWithinWindow(i))
{
SetWindowActive(i);
windows.pop_back();
return true;
}
// Create a window if one isn't clicked.
else if (i == 0)
{
CreateNewWindow();
return true;
}
}
}
}
//// Left mouse Button ////
if (m_mouse[0].bPressed)
{
// Loop through the windows.
for (int i = windows.size() - 1; i >= 0; i--)
{
// Check if clicked bottom right pixel.
if (m_mousePosX - panOffsetX == windows[i].x + windows[i].width && m_mousePosY - panOffsetY == windows[i].y + windows[i].height)
{
// RESIZE
SetWindowActive(i);
resizing = true;
return true;
}
// Check if click in header.
if (m_mousePosX - panOffsetX >= windows[i].x &&
m_mousePosX - panOffsetX <= windows[i].x + windows[i].width &&
m_mousePosY - panOffsetY >= windows[i].y &&
m_mousePosY - panOffsetY <= windows[i].y + windows[i].headerSize)
{
// DRAG
dragOffsetX = m_mousePosX - windows[i].x;
dragOffsetY = m_mousePosY - windows[i].y;
SetWindowActive(i);
dragging = true;
return true;
}
// Check if clicked in any part of window.
if (IsWithinWindow(i))
{
SetWindowActive(i);
return true;
}
}
// Panning
dragOffsetX = m_mousePosX - panOffsetX;
dragOffsetY = m_mousePosY - panOffsetY;
panning = true;
return true;
}
return false;
}
};
int main()
{
olcSynthGui demo;
if (demo.ConstructConsole(200, 100, 6, 6))
demo.Start();
return 0;
}