1 /* Copyright (C) 2021-2024 Harry Godden (hgn) - All Rights Reserved */
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
11 * 5. a maximum of ONE control per memory location can be drawn at any given
17 #include "vg_engine.h"
19 #include "vg_shader.h"
22 typedef ui_px ui_rect
[4];
23 typedef ui_px ui_point
[2];
24 typedef struct ui_vert ui_vert
;
31 /* Relative to cursor p0 */
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,
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,
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,
59 enum ui_scheme_colour
{
80 extern ui_px k_ui_widget_height
,
84 typedef u32 ui_scheme
[8*4];
97 typedef struct ui_font ui_font
;
99 extern ui_font vg_ui_font_small
, vg_ui_font_big
;
101 #define UI_RGB( STDHEX ) 0xff000000 |\
102 ((STDHEX&0x000000ff)<<16) |\
103 ((STDHEX&0x0000ff00) ) |\
104 ((STDHEX&0x00ff0000)>>16)
106 #define UI_TEXTBOX_MULTILINE 0x1
107 #define UI_TEXTBOX_WRAP 0x2
108 #define UI_TEXTBOX_AUTOFOCUS 0x4
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
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))
122 #define UI_BOTTOM 0x4
127 struct ui_vert
*vertex_buffer
;
129 u32 max_verts
, max_indices
,
130 cur_vert
, cur_indice
,
131 vert_start
, indice_start
;
134 void *focused_control_id
; /* uses the memory location of various locking
140 u32 focused_control_hit
;
141 enum ui_control_type
{
143 k_ui_control_textbox
,
147 focused_control_type
;
149 union{ /* controls that can be focused */
151 int cursor_user
, cursor_pos
;
155 struct ui_textbox_callbacks
{
156 void (*enter
)( char *, u32
),
157 (*up
)( char *, u32
),
158 (*down
)( char *, u32
),
159 (*change
)( char *, u32
),
182 struct ui_modal_callbacks
{
189 GLuint tex_glyphs
, vao
, vbo
, ebo
, tex_bg
;
190 v2f bg_inverse_ratio
;
192 ui_px mouse
[2], mouse_delta
[2], mouse_click
[2];
194 u32 ignore_input_frames
;
197 ui_rect click_fader
, click_fader_end
;
198 float click_fade_opacity
;
212 SDL_Cursor
*cursor_map
[ k_ui_cursor_max
];
215 f32 hue
; /* lol this sucks */
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
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
);
254 /* get an appropriately contrasting colour given the base */
255 u32
ui_colourcont( enum ui_scheme_colour id
);
257 void ui_hex_to_norm( u32 hex
, v4f norm
);
258 u32
v4f_u32_colour( v4f colour
);
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
,
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);
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);
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);