-
Notifications
You must be signed in to change notification settings - Fork 68
/
gui.h
96 lines (76 loc) · 1.84 KB
/
gui.h
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
#pragma once
class GUI {
private:
std::vector< Form* > m_forms;
public:
bool m_open;
Form* m_drag_form;
Point m_drag_offset;
Color m_color{ colors::white };
public:
void think( );
void draw( );
// registers a new form.
__forceinline void RegisterForm( Form* form, int key = -1 ) {
// set key to toggle form.
form->SetToggle( key );
// add form to our container.
m_forms.push_back( form );
}
};
class Input {
public:
struct key_t {
bool down;
bool pressed;
int tick;
int oldtick;
};
std::array< key_t, 256 > m_keys;
Point m_mouse;
std::string m_buffer;
public:
__forceinline void update( ) {
// iterate all keys.
for( int i{}; i <= 254; ++i ) {
key_t* key = &m_keys[ i ];
key->pressed = false;
if( key->down && key->tick > key->oldtick ) {
key->oldtick = key->tick;
key->pressed = true;
}
}
}
// mouse within coords.
__forceinline bool IsCursorInBounds( int x, int y, int x2, int y2 ) const {
return m_mouse.x > x && m_mouse.y > y && m_mouse.x < x2 && m_mouse.y < y2;
}
// mouse within rectangle.
__forceinline bool IsCursorInRect( Rect area ) const {
return IsCursorInBounds( area.x, area.y, area.x + area.w, area.y + area.h );
}
__forceinline void SetDown( int vk ) {
key_t* key = &m_keys[ vk ];
key->down = true;
key->tick = g_winapi.GetTickCount( );
}
__forceinline void SetUp( int vk ) {
key_t* key = &m_keys[ vk ];
key->down = false;
}
// key is being held.
__forceinline bool GetKeyState( int vk ) {
if( vk == -1 )
return false;
return m_keys[ vk ].down;
}
// key was pressed.
__forceinline bool GetKeyPress( int vk ) {
if( vk == -1 )
return false;
key_t* key = &m_keys[ vk ];
return key->pressed;
}
};
extern GUI g_gui;
extern Input g_input;