4e725a824c37c62cc9e6448f1c0e349f4796534d
[vg.git] / vg_imgui.h
1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
2
3 /*
4 * Principles:
5 *
6 * 1. layout is defined by subdividing
7 * 2. a parent node should never be resized by the content after creation
8 * 3. when the ui is in an interactive state, no controls should ever move
9 * 4. controls directly reference a memory location and use that as their
10 * unique id
11 * 5. a maximum of ONE control per memory location can be drawn at any given
12 * point.
13 */
14
15 #pragma once
16
17 #include "vg_engine.h"
18 #include "vg_tex.h"
19 #include "vg_shader.h"
20
21 typedef i16 ui_px;
22 typedef ui_px ui_rect[4];
23 typedef ui_px ui_point[2];
24 typedef struct ui_vert ui_vert;
25
26 enum ui_axis {
27 k_ui_axis_h = 0x0u,
28 k_ui_axis_v = 0x1u,
29 };
30
31 /* Relative to cursor p0 */
32 enum ui_align
33 { /* DC BA */
34 k_ui_align_lwr = 0xff,
35 k_ui_align_left = 0x0000| 0x00,
36 k_ui_align_right = 0x0000| 0x01,
37 k_ui_align_center = 0x0000| 0x02,
38
39 k_ui_align_middle = 0x0100,
40 k_ui_align_middle_left = 0x0100| 0x00,
41 k_ui_align_middle_right = 0x0100| 0x01,
42 k_ui_align_middle_center = 0x0100| 0x02,
43
44 k_ui_align_bottom = 0x0200,
45 k_ui_align_bottom_left = 0x0200| 0x00,
46 k_ui_align_bottom_right = 0x0200| 0x01,
47 k_ui_align_bottom_center = 0x0200| 0x02,
48 };
49
50 #pragma pack(push,1)
51 struct ui_vert
52 {
53 ui_px co[2];
54 u16 uv[2];
55 u32 colour;
56 };
57 #pragma pack(pop)
58
59 enum ui_scheme_colour{
60 k_ui_bg = 0,
61 k_ui_fg = 8,
62 k_ui_hue = 16,
63 k_ui_red = 16,
64 k_ui_orange,
65 k_ui_yellow,
66 k_ui_green,
67 k_ui_aqua,
68 k_ui_blue,
69 k_ui_purple,
70 k_ui_gray,
71 k_ui_brighter = 8
72 };
73
74 enum ui_shader {
75 k_ui_shader_colour,
76 k_ui_shader_image,
77 k_ui_shader_hsv,
78 };
79
80 extern ui_px k_ui_widget_height,
81 k_ui_scale,
82 k_ui_padding;
83
84 typedef u32 ui_scheme[8*4];
85
86 struct ui_font {
87 ui_px glyph_width,
88 glyph_height,
89 glyph_baseline,
90 line_height,
91 sheet_size,
92 spacing,
93 offset_y;
94
95 u8 ascii_start;
96 };
97 typedef struct ui_font ui_font;
98
99 extern ui_font vg_ui_font_small, vg_ui_font_big;
100
101 #define UI_RGB( STDHEX ) 0xff000000 |\
102 ((STDHEX&0x000000ff)<<16) |\
103 ((STDHEX&0x0000ff00) ) |\
104 ((STDHEX&0x00ff0000)>>16)
105
106 #define UI_TEXTBOX_MULTILINE 0x1
107 #define UI_TEXTBOX_WRAP 0x2
108 #define UI_TEXTBOX_AUTOFOCUS 0x4
109
110 #define UI_MODAL_OK 0x0
111 #define UI_MODAL_GOOD 0x1
112 #define UI_MODAL_BAD 0x2
113 #define UI_MODAL_WARN 0x3
114 #define UI_MODAL_TYPE_BITS 0x3
115
116 #define UI_MOUSE_LEFT (SDL_BUTTON(SDL_BUTTON_LEFT))
117 #define UI_MOUSE_RIGHT (SDL_BUTTON(SDL_BUTTON_RIGHT))
118 #define UI_MOUSE_MIDDLE (SDL_BUTTON(SDL_BUTTON_MIDDLE))
119
120 #define UI_TOP 0x1
121 #define UI_LEFT 0x2
122 #define UI_BOTTOM 0x4
123 #define UI_RIGHT 0x8
124
125 struct vg_imgui
126 {
127 struct ui_vert *vertex_buffer;
128 u16 *indice_buffer;
129 u32 max_verts, max_indices,
130 cur_vert, cur_indice,
131 vert_start, indice_start;
132
133 union {
134 void *focused_control_id; /* uses the memory location of various locking
135 controls as an id */
136 char *textbuf;
137 i32 *ptr_enum;
138 };
139
140 u32 focused_control_hit;
141 enum ui_control_type{
142 k_ui_control_none,
143 k_ui_control_textbox,
144 k_ui_control_enum,
145 k_ui_control_modal
146 }
147 focused_control_type;
148
149 union{ /* controls that can be focused */
150 struct ui_textbuf{
151 int cursor_user, cursor_pos;
152 u32 len;
153 u32 flags;
154
155 struct ui_textbox_callbacks{
156 void (*enter)( char *, u32 ),
157 (*up)( char *, u32 ),
158 (*down)( char *, u32 ),
159 (*change)( char *, u32 ),
160 (*escape)( void );
161 }
162 callbacks;
163 }
164 textbox;
165
166 struct ui_enum{
167 struct ui_enum_opt{
168 i32 value;
169 const char *alias;
170 }
171 *options;
172 u32 option_count;
173 ui_rect rect;
174 }
175 _enum;
176 };
177
178 struct ui_modal{
179 const char *message;
180 u32 options;
181
182 struct ui_modal_callbacks{
183 void (*close)(u32);
184 }
185 callbacks;
186 }
187 modal;
188
189 GLuint tex_glyphs, vao, vbo, ebo, tex_bg;
190 v2f bg_inverse_ratio;
191
192 ui_px mouse[2], mouse_delta[2], mouse_click[2];
193 u32 mouse_state[2];
194 u32 ignore_input_frames;
195 int wants_mouse;
196
197 ui_rect click_fader, click_fader_end;
198 float click_fade_opacity;
199 f32 frosting;
200
201 ui_scheme scheme;
202 const ui_font *font;
203
204 enum ui_cursor{
205 k_ui_cursor_default,
206 k_ui_cursor_ibeam,
207 k_ui_cursor_hand,
208 k_ui_cursor_max
209 }
210 cursor;
211
212 SDL_Cursor *cursor_map[ k_ui_cursor_max ];
213 v4f colour;
214
215 f32 hue; /* lol this sucks */
216 }
217 extern vg_ui;
218
219 enum ui_button_state {
220 k_ui_button_none = 0x0,
221 k_ui_button_click = 0x1,
222 k_ui_button_holding_inside = 0x2,
223 k_ui_button_holding_outside = 0x4,
224 k_ui_button_hover = 0x8
225 };
226
227 /* TODO: docu.. */
228
229 void vg_ui_init(void);
230 void rect_copy( ui_rect a, ui_rect b );
231 void ui_flush( enum ui_shader shader, f32 w, f32 h );
232 void ui_fill_rect( ui_rect rect, u32 colour, ui_px uv[4] );
233 void ui_fill( ui_rect rect, u32 colour );
234 void ui_outline( ui_rect rect, ui_px thickness, u32 colour, u32 mask );
235 void ui_split( ui_rect rect, enum ui_axis other, ui_px width, ui_px gap,
236 ui_rect l, ui_rect r );
237 void ui_rect_center( ui_rect parent, ui_rect rect );
238 void ui_fit_item( ui_rect rect, ui_px size[2], ui_rect d );
239 void ui_split_ratio( ui_rect rect, enum ui_axis dir, float ratio,
240 ui_px gap, ui_rect l, ui_rect r );
241 void ui_rect_pad( ui_rect rect, ui_px pad[2] );
242 ui_px ui_text_line_width( const char *str );
243 ui_px ui_text_string_height( const char *str );
244 ui_px ui_text_aligned_x( const char *str, ui_rect rect, ui_px scale,
245 enum ui_align align );
246 int ui_clip( ui_rect parent, ui_rect child, ui_rect clipped );
247 int ui_inside_rect( ui_rect rect, ui_px co[2] );
248 int ui_click_down( u32 mask );
249 int ui_clicking( u32 mask );
250 int ui_click_up( u32 mask );
251 void ui_prerender(void);
252 u32 ui_colour( enum ui_scheme_colour id );
253
254 /* get an appropriately contrasting colour given the base */
255 u32 ui_colourcont( enum ui_scheme_colour id );
256
257 void ui_hex_to_norm( u32 hex, v4f norm );
258 u32 v4f_u32_colour( v4f colour );
259
260 u32 ui_opacity( u32 colour, f32 opacity );
261 u32 ui_ntext( ui_rect rect, const char *str, u32 len, ui_px scale,
262 enum ui_align align, u32 colour );
263 void ui_text( ui_rect rect, const char *str, ui_px scale,
264 enum ui_align align, u32 colour );
265 void ui_panel( ui_rect in_rect, ui_rect out_panel );
266 void ui_label( ui_rect rect, const char *text, ui_px size,
267 ui_px gap, ui_rect r );
268 void ui_standard_widget( ui_rect inout_panel, ui_rect out_rect, ui_px count );
269 void ui_info( ui_rect inout_panel, const char *text );
270 void ui_image( ui_rect rect, GLuint image );
271 void ui_defocus_all(void);
272 enum ui_button_state ui_colourbutton( ui_rect rect,
273 enum ui_scheme_colour colour,
274 enum ui_scheme_colour hover_colour,
275 enum ui_scheme_colour hi_colour,
276 bool const fill );
277 enum ui_button_state ui_colourbutton_text(
278 ui_rect rect, const char *string, ui_px scale,
279 enum ui_scheme_colour colour );
280 enum ui_button_state ui_button_text( ui_rect rect,
281 const char *string, ui_px scale );
282 enum ui_button_state ui_button( ui_rect inout_panel, const char *string );
283 void ui_postrender(void);
284
285 int ui_checkbox( ui_rect inout_panel, const char *str_label, i32 *data );
286 void ui_enum( ui_rect inout_panel, const char *str_label,
287 struct ui_enum_opt *options, u32 len, i32 *value );
288 void ui_slider( ui_rect inout_panel, const char *str_label,
289 f32 min, f32 max, f32 *value, const char *format );
290 void ui_colourpicker( ui_rect inout_panel, const char *str_label, v4f value );
291 int ui_textbox( ui_rect inout_panel, const char *label,
292 char *buf, u32 len, u32 lines, u32 flags,
293 struct ui_textbox_callbacks *callbacks );
294 void ui_tabs( ui_rect inout_panel, ui_rect out_content_panel,
295 const char **titles, u32 count, i32 *page );
296 void ui_start_modal( const char *message, u32 options );
297 void ui_proc_key( SDL_Keysym ev );
298 void ui_proc_utf8( const char *text );
299 void ui_dev_colourview(void);
300
301 void _ui_textbox_move_cursor( int *cursor0, int *cursor1,
302 int dir, int snap_together );
303 int _ui_textbox_delete_char( int direction );
304 void _ui_textbox_put_char( char c );
305 void _ui_textbox_up(void);
306 void _ui_textbox_left(void);
307 void _ui_textbox_left_select(void);
308 void _ui_textbox_down(void);
309 void _ui_textbox_right_select(void);
310 void _ui_textbox_right(void);
311 void _ui_textbox_backspace(void);
312 void _ui_textbox_delete(void);
313 void _ui_textbox_home_select(void);
314 void _ui_textbox_home(void);
315 void _ui_textbox_end_select(void);
316 void _ui_textbox_end(void);
317 void _ui_textbox_select_all(void);
318 void _ui_textbox_cut(void);
319 void _ui_textbox_enter(void);