-
Notifications
You must be signed in to change notification settings - Fork 6
/
gui.cpp
288 lines (222 loc) · 7.41 KB
/
gui.cpp
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "includes.h"
GUI g_gui{};;
Input g_input{};;
void GUI::think( ) {
m_open = false;
// update keyboard input.
g_input.update( );
// run bound key callbacks.
if( !m_forms.empty( ) ) {
bool done = false;
for( const auto& f : m_forms ) {
if( !f || f->m_tabs.empty( ) )
continue;
for( const auto& t : f->m_tabs ) {
if( !t || t->m_elements.empty( ) )
continue;
for( const auto& e : t->m_elements ) {
if( !e || e->m_type != ElementTypes::KEYBIND )
continue;
// big cast.
Keybind* k = ( Keybind* )e;
if( k->m_toggle && g_input.GetKeyPress( k->get( ) ) ) {
k->m_toggle( );
done = true;
break;
}
}
if( done )
break;
}
if( done )
break;
}
}
// sort forms based on last touched tick.
std::sort( m_forms.begin( ), m_forms.end( ), []( Form* a, Form* b ) {
return a->m_tick > b->m_tick;
} );
// update form open states
// iterage forms.
for( const auto& f : m_forms ) {
if( !f )
continue;
if( f->m_key != -1 && g_input.GetKeyPress( f->m_key ) )
f->toggle( );
if( !m_open && f->m_open )
m_open = true;
// if closed, deactivate any active elements.
if( !f->m_open && f->m_active_element )
f->m_active_element = nullptr;
}
// update element visibility.
for( const auto& f : m_forms ) {
if( !f || !f->m_active_tab || f->m_active_tab->m_elements.empty( ) )
continue;
// reset.
f->m_active_tab->m_element_offset.fill( ELEMENT_BORDER );
for( auto& e : f->m_active_tab->m_elements ) {
// default to true.
e->m_show = true;
// this element has show callbacks.
if( e->m_show_callbacks.size( ) ) {
// iterate all callbacks.
for( auto& cb : e->m_show_callbacks ) {
// if one callback evaluates to false.
// set show to false.
if( !cb( ) )
e->m_show = false;
}
}
// this element will be shown, fix its y coordinate.
if( e->m_show ) {
// slight correction.
if( !e->m_use_label )
f->m_active_tab->m_element_offset[ e->m_col ] -= ( ELEMENT_DISTANCE - ELEMENT_COMBINED_DISTANCE );
e->m_pos.y = f->m_active_tab->m_element_offset[ e->m_col ];
// set offset for next element.
f->m_active_tab->m_element_offset[ e->m_col ] += ( ELEMENT_COMBINED_DISTANCE + e->m_base_h );
}
}
}
bool pressed = g_input.GetKeyPress( VK_LBUTTON );
bool down = g_input.GetKeyState( VK_LBUTTON );
// update mouse pos.
g_csgo.m_input_system->GetCursorPosition( &g_input.m_mouse.x, &g_input.m_mouse.y );
// mouse isnt clamped across multiple displays etc.
math::clamp( g_input.m_mouse.x, 0, g_cl.m_width );
math::clamp( g_input.m_mouse.y, 0, g_cl.m_height );
// we have a form that we are dragging, but mouse1 is not getting held, stop dragging.
if( m_drag_form && !down )
m_drag_form = nullptr;
// we still have a drag form ptr, this means mouse1 hasnt been released, as checked above, change the form coordinates.
if( m_drag_form && down && !pressed ) {
m_drag_form->m_x = g_input.m_mouse.x - m_drag_offset.x;
m_drag_form->m_y = g_input.m_mouse.y - m_drag_offset.y;
}
if( pressed || down ) {
// iterate forms.
for( const auto& f : m_forms ) {
if( !f )
continue;
if( g_input.IsCursorInRect( f->GetFormRect( ) ) ) {
f->m_tick = g_winapi.GetTickCount( );
break;
}
}
}
// re-sort forms based on last touched tick.
std::sort( m_forms.begin( ), m_forms.end( ), []( Form* a, Form* b ) {
return a->m_tick > b->m_tick;
} );
// iterate forms.
for( const auto& f : m_forms ) {
if( !f || !f->m_open )
continue;
bool element_input{};
Rect cl = f->GetClientRect( );
Rect el = f->GetElementsRect( );
// mouse1 was pressed.
if( pressed ) {
// we are in this form.
if( g_input.IsCursorInRect( f->GetFormRect( ) ) ) {
// we are in the client area, aka, not on the drag handle.
if( g_input.IsCursorInRect( cl ) ) {
Rect tabs_area = f->GetTabsRect( );
if( g_input.IsCursorInRect( tabs_area ) && !f->m_tabs.empty( ) ) {
if( f->m_active_element )
f->m_active_element = nullptr;
// iterate tabs.
for( size_t i{}; i < f->m_tabs.size( ); ++i ) {
const auto& t = f->m_tabs[ i ];
Rect tab{ tabs_area.x, tabs_area.y + ( i * 16 ), tabs_area.w, 16 };
if( g_input.IsCursorInRect( tab ) )
f->m_active_tab = t;
}
}
// to escape these mess of nested conditionals.
else if( g_input.IsCursorInRect( el ) )
element_input = true;
}
// we are within the form but not the client area.
// we must be trying to drag.
else {
// set dragged form.
m_drag_form = f;
// compute drag offset.
m_drag_offset.x = g_input.m_mouse.x - f->m_x;
m_drag_offset.y = g_input.m_mouse.y - f->m_y;
if( f->m_active_element )
f->m_active_element = nullptr;
}
}
// we clicked somewhere in outer space
// unfocus the active element.
else if( f->m_active_element )
f->m_active_element = nullptr;
}
// we have an active element already.
if( f->m_active_element && f->m_active_element->m_show ) {
// call think callback.
f->m_active_element->think( );
// element location.
Rect area{ el.x + f->m_active_element->m_pos.x, el.y + f->m_active_element->m_pos.y, f->m_active_element->m_w, f->m_active_element->m_h };
// nigga we accepin' input.
// so is dis element, and we on it
if( element_input && ( f->m_active_element->m_flags & ElementFlags::CLICK ) && g_input.IsCursorInRect( area ) ) {
// invoke clicky-callback.
f->m_active_element->click( );
if( f->m_active_element->m_flags & ElementFlags::DEACIVATE )
f->m_active_element = nullptr;
// we clicked something with success.
element_input = false;
}
}
// do we have any of deez fuckers?
if( f->m_active_tab && !f->m_active_tab->m_elements.empty( ) ) {
// iterate le elementz.
for( const auto& e : f->m_active_tab->m_elements ) {
// invalid element
// or already handled above.
if( !e || ( f->m_active_element && e == f->m_active_element ) )
continue;
if( !e->m_show )
continue;
e->think( );
Rect area{ el.x + e->m_pos.x, el.y + e->m_pos.y, e->m_w, e->m_h };
// we didnt click anything yet, we can click this, and we are in the correct area.
if( element_input && ( e->m_flags & ElementFlags::CLICK ) && g_input.IsCursorInRect( area ) ) {
// invoke the fuckers click-callback.
e->click( );
// we clicked something.
element_input = false;
// control has the ability to become active.
// i.e popped out
if( e->m_flags & ElementFlags::ACTIVE )
f->m_active_element = e;
// this control cant become active.
else if( f->m_active_element )
f->m_active_element = nullptr;
}
}
}
// we ended up clicking nothing
// nothing matched with a known element.
if( element_input && f->m_active_element )
f->m_active_element = nullptr;
}
// do the rendering.
draw( );
}
void GUI::draw( ) {
// sort forms based on last touched tick ( in reverse ).
std::sort( m_forms.begin( ), m_forms.end( ), []( Form* a, Form* b ) {
return a->m_tick < b->m_tick;
} );
// iterate forms.
for( const auto& f : m_forms ) {
if( !f )
continue;
f->draw( );
}
}