From 75ef1916c622ed0b938b2a47b16294a748ff5aa1 Mon Sep 17 00:00:00 2001 From: hgn Date: Wed, 19 Mar 2025 03:18:27 +0000 Subject: [PATCH] colourpicker api --- vg_framebuffer.c | 6 ++---- vg_framebuffer.h | 3 +-- vg_ui/imgui.c | 25 ++++++++++++++----------- vg_ui/imgui.h | 37 +++++++++++++++++-------------------- 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/vg_framebuffer.c b/vg_framebuffer.c index 253c3a5..d227c7c 100644 --- a/vg_framebuffer.c +++ b/vg_framebuffer.c @@ -227,8 +227,7 @@ static void vg_framebuffer_allocate_texture( vg_framebuffer *fb, } } -vg_framebuffer *vg_framebuffer_allocate( void *alloc, - u32 attachment_count, bool track ) +vg_framebuffer *vg_framebuffer_allocate( void *alloc, u32 attachment_count, bool track ) { vg_framebuffer *fb = vg_linear_alloc( alloc, sizeof(vg_framebuffer) ); @@ -244,8 +243,7 @@ vg_framebuffer *vg_framebuffer_allocate( void *alloc, } } - fb->attachments = vg_linear_alloc( alloc, - sizeof(vg_framebuffer_attachment) * attachment_count ); + fb->attachments = vg_linear_alloc( alloc, sizeof(vg_framebuffer_attachment) * attachment_count ); fb->attachment_count = attachment_count; return fb; diff --git a/vg_framebuffer.h b/vg_framebuffer.h index 90500e0..8bf177d 100644 --- a/vg_framebuffer.h +++ b/vg_framebuffer.h @@ -87,8 +87,7 @@ void vg_framebuffer_bind_texture( vg_framebuffer *fb, int attachment, int slot ) * Allocation of a framebuffer memory. Optionally, track this framebuffer in * debugging systems. */ -vg_framebuffer *vg_framebuffer_allocate( void *alloc, - u32 attachment_count, bool track ); +vg_framebuffer *vg_framebuffer_allocate( void *alloc, u32 attachment_count, bool track ); /* * Allocate graphics memory and initialize diff --git a/vg_ui/imgui.c b/vg_ui/imgui.c index ab0915b..bddbbcd 100644 --- a/vg_ui/imgui.c +++ b/vg_ui/imgui.c @@ -1067,8 +1067,7 @@ bool ui_slider( ui_context *ctx, ui_rect inout_panel, const char *str_label, * ----------------------------------------------------------------------------- */ -void ui_colourpicker( ui_context *ctx, - ui_rect inout_panel, const char *str_label, v4f value ) +bool ui_colourpicker( ui_context *ctx, ui_rect inout_panel, const char *str_label, v4f value, enum ui_colour_type type ) { ui_rect widget, left, right; ui_standard_widget( ctx, inout_panel, widget, 8 ); @@ -1087,11 +1086,15 @@ void ui_colourpicker( ui_context *ctx, for( u32 i=0; i<4; i ++ ) { - modified |= ui_slider_standard( ctx, sliders[i], 0.0f, 1.0f, hsv+i, - (const char *[]){ "hue %.2f", - "sat %.2f", - "lum %.2f", - "alpha %.2f" }[i] ); + const char *labels[] = { "hue %.2f", "sat %.2f", "lum %.2f", "alpha %.2f" }; + + if( (i == 3) && (type == k_ui_colour_type_rgb) ) + { + ui_fill( ctx, sliders[i], ui_colour( ctx, k_ui_bg ) ); + ui_text( ctx, sliders[i], "alpha 1.0", 1, k_ui_align_middle_center, ui_colour( ctx, k_ui_bg+3 ) ); + } + else + modified |= ui_slider_standard( ctx, sliders[i], 0.0f, 1.0f, hsv+i, labels[i] ); } ui_rect preview, square; @@ -1117,15 +1120,13 @@ void ui_colourpicker( ui_context *ctx, hsv[2] = 1.0f-hsv[2]; } - if( modified & (k_ui_button_click|k_ui_button_holding_inside| - k_ui_button_holding_outside) ) + if( modified & (k_ui_button_click|k_ui_button_holding_inside|k_ui_button_holding_outside) ) { vg_hsv_rgb( hsv, value ); value[3] = hsv[3]; } - ui_outline( ctx, square, - 1, ui_colour(ctx, state? k_ui_fg+3: k_ui_bg+3 ), 0 ); + ui_outline( ctx, square, 1, ui_colour(ctx, state? k_ui_fg+3: k_ui_bg+3 ), 0 ); /* preview colour */ v4f colour; @@ -1153,6 +1154,8 @@ void ui_colourpicker( ui_context *ctx, ui_fill( ctx, marker, ui_colour(ctx, k_ui_fg ) ); ui_fill( ctx, lx, ui_colour(ctx, k_ui_fg ) ); ui_fill( ctx, ly, ui_colour(ctx, k_ui_fg ) ); + + return modified? 1: 0; } /* diff --git a/vg_ui/imgui.h b/vg_ui/imgui.h index 8279b9b..1f40553 100644 --- a/vg_ui/imgui.h +++ b/vg_ui/imgui.h @@ -300,8 +300,7 @@ u32 ui_opacity( u32 colour, f32 opacity ); /* standard widgets & controls */ ui_px ui_standard_widget_height( ui_context *ctx, ui_px count ); -void ui_standard_widget( ui_context *ctx, - ui_rect inout_panel, ui_rect out_rect, ui_px count ); +void ui_standard_widget( ui_context *ctx, ui_rect inout_panel, ui_rect out_rect, ui_px count ); void ui_panel( ui_context *ctx, ui_rect in_rect, ui_rect out_panel ); void ui_label( ui_context *ctx, ui_rect rect, const char *text, ui_px size, @@ -324,27 +323,25 @@ enum ui_button_state ui_colourbutton_text( enum ui_button_state ui_button_text( ui_context *ctx, ui_rect rect, const char *string, ui_px scale ); enum ui_button_state ui_button( ui_context *ctx, ui_rect inout_panel, const char *string ); void ui_postrender( ui_context *ctx, f32 delta_time ); -enum ui_button_state ui_checkbox_base( ui_context *ctx, - ui_rect box, i32 *data ); -int ui_checkbox( ui_context *ctx, - ui_rect inout_panel, const char *str_label, i32 *data ); +enum ui_button_state ui_checkbox_base( ui_context *ctx, ui_rect box, i32 *data ); +int ui_checkbox( ui_context *ctx, ui_rect inout_panel, const char *str_label, i32 *data ); void ui_enum( ui_context *ctx, ui_rect inout_panel, const char *str_label, struct ui_enum_opt *options, u32 len, i32 *value ); -enum ui_button_state ui_slider_base( - ui_context *ctx, - ui_rect box, enum ui_axis axis, f32 min, f32 max, f32 *value, f32 *out_t ); -void ui_slider_text( ui_context *ctx, - ui_rect box, const char *format, f32 value ); -bool ui_slider_standard( ui_context *ctx, - ui_rect box, f32 min, f32 max, f32 *value, - const char *format ); -bool ui_slider( ui_context *ctx, ui_rect inout_panel, const char *str_label, - f32 min, f32 max, f32 *value ); -void ui_colourpicker( ui_context *ctx, - ui_rect inout_panel, const char *str_label, v4f value ); -void _ui_textbox_move_cursor( ui_context *ctx, int *cursor0, int *cursor1, - int dir, int snap_together ); +enum ui_button_state ui_slider_base( ui_context *ctx, + ui_rect box, enum ui_axis axis, f32 min, f32 max, f32 *value, f32 *out_t ); +void ui_slider_text( ui_context *ctx, ui_rect box, const char *format, f32 value ); +bool ui_slider_standard( ui_context *ctx, ui_rect box, f32 min, f32 max, f32 *value, const char *format ); +bool ui_slider( ui_context *ctx, ui_rect inout_panel, const char *str_label, f32 min, f32 max, f32 *value ); + +enum ui_colour_type +{ + k_ui_colour_type_rgba, + k_ui_colour_type_rgb +}; + +bool ui_colourpicker( ui_context *ctx, ui_rect inout_panel, const char *str_label, v4f value, enum ui_colour_type type ); +void _ui_textbox_move_cursor( ui_context *ctx, int *cursor0, int *cursor1, int dir, int snap_together ); int _ui_textbox_delete_char( ui_context *ctx, int direction ); void ui_start_modal( ui_context *ctx, const char *message, u32 options ); -- 2.25.1