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
|
#pragma once
#include "ui_base_item.h"
namespace ui
{
class c_key_picker : public base_item {
public:
c_key_picker( int x, int y, int w, const char* name, int* setting ) :
base_item( x, y, w, 16, name ), m_setting( setting ) { }
virtual bool is_hovered( ) override {
if ( m_disabled ) return false;
int x = get_relative_x( );
int y = get_relative_y( ) + 12;
int h = m_height;
int mouse_x, mouse_y;
ui_get_cursor_pos( mouse_x, mouse_y );
return mouse_x >= x && mouse_x <= x + m_width
&& mouse_y >= y && mouse_y <= y + h;
}
virtual int get_total_height( ) const override {
return m_height + 12;
}
void input( ) {
bool active_backup = m_active;
bool active_changed = false;
if ( is_hovered( ) && g_input.is_key_pressed( KEYS_MOUSE1 ) ) {
if ( !m_mouse_held ) {
m_active = true;
}
m_mouse_held = true;
}
if ( g_input.is_key_pressed( KEYS_ESCAPE ) ) {
m_active = false;
*m_setting = KEYS_NONE;
}
if ( m_active && !m_mouse_held ) {
int key = g_input.is_any_key_pressed( );
if ( key != KEYS_NONE ) {
*m_setting = key;
m_active = false;
}
}
active_changed = active_backup != m_active;
if ( active_changed ) {
set_disabled_callbacks( m_active );
}
}
virtual void render( ) override {
int x = get_relative_x( );
int y = get_relative_y( );
input( );
ui_draw_string( x + 2, y, false, ui_get_text_col( ), m_text );
ui_draw_rect( x, y + 13, m_width, m_height, ui_get_disabled_col( ) );
ui_draw_outlined_rect( x - 1, y + 12, m_width + 1, m_height + 1,
is_hovered( ) || m_active ? ui_get_text_col( ) : ui_get_accent_col( ) );
ui_draw_string( x + m_width / 2, y + 14, true, ui_get_text_col( ),
g_input.get_key_name( ( VirtualKeys_t )*m_setting ) );
}
protected:
int* m_setting{ };
bool m_active{ };
bool m_mouse_held{ };
};
//skEeT PiCkErS
class c_key_picker_small : public base_item {
public:
c_key_picker_small( int x, int y, int* setting ) :
base_item( x, y, 0, 0, xors( "KEY_PICKER" ) ), m_setting( setting ) { }
virtual int get_total_height( ) const override {
const char* name = g_input.get_short_name( ( VirtualKeys_t )*m_setting );
int w, h;
ui_get_text_size( w, h, name );
return h;
}
virtual bool is_hovered( ) override {
if ( m_disabled ) return false;
const char* name = g_input.get_short_name( ( VirtualKeys_t )*m_setting );
int x = get_relative_x( );
int y = get_relative_y( );
int mouse_x, mouse_y;
int w, h;
ui_get_text_size( w, h, "[%s]", name );
ui_get_cursor_pos( mouse_x, mouse_y );
return mouse_x >= x - w && mouse_x <= x
&& mouse_y >= y && mouse_y <= y + h;
}
void input( ) {
bool active_backup = m_active;
bool active_changed = false;
if ( g_input.is_key_pressed( KEYS_ESCAPE ) ) {
m_active = false;
*m_setting = KEYS_NONE;
}
if ( m_active && !m_mouse_held ) {
int key = g_input.is_any_key_pressed( );
if ( key != KEYS_NONE ) {
*m_setting = key;
m_active = false;
}
}
if ( is_hovered( ) && g_input.is_key_pressed( KEYS_MOUSE1 ) ) {
if ( !m_mouse_held ) {
m_active = true;
}
m_mouse_held = true;
}
else {
m_mouse_held = false;
}
active_changed = active_backup != m_active;
if ( active_changed ) {
set_disabled_callbacks( m_active );
}
}
virtual void render( ) override {
const char* name = g_input.get_short_name( ( VirtualKeys_t )*m_setting );
int x = get_relative_x( );
int y = get_relative_y( );
int w, h;
ui_get_text_size( w, h, "[%s]", name );
input( );
ui_draw_string( x - w, y, false, is_hovered( ) || m_active ?
ui_get_accent_col( ) : ui_get_text_col( ), "[%s]", name );
}
protected:
int* m_setting;
bool m_active{ };
bool m_mouse_held{ };
};
}
|