colourpicker

Simple Colour Picker written in C
git clone anongit@rnpnr.xyz:colourpicker.git
Log | Files | Refs | Feed | Submodules | README | LICENSE

Commit: 40e131ee120d724d93637c86bb59fb5853d86b23
Parent: 602db0d780a50dfe529c8722061c8b60b8deb4d7
Author: Randy Palamar
Date:   Mon,  5 Aug 2024 15:21:23 -0600

support window resizing/scaling

Diffstat:
Mcolourpicker.c | 33++++++++++++++++++++++++++-------
Mgen_incs.c | 13++++++++++---
Mmain.c | 5++++-
Mutil.c | 5++++-
4 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/colourpicker.c b/colourpicker.c @@ -328,6 +328,12 @@ do_text_input(ColourPickerCtx *ctx, Rect r, Color colour) i32 buf_delta = ctx->is.buf_len - max_chars[ctx->is.idx]; if (buf_delta < 0) buf_delta = 0; char *buf = ctx->is.buf + buf_delta; + { + /* NOTE: drop a char if the subtext still doesn't fit */ + v2 nts = {.rv = MeasureTextEx(ctx->font, buf, ctx->font_size, 0)}; + if (nts.w > 0.96 * r.size.w) + buf++; + } DrawTextEx(ctx->font, buf, pos.rv, ctx->font_size, 0, colour); ctx->is.cursor_t = move_towards_f32(ctx->is.cursor_t, ctx->is.cursor_t_target, @@ -364,7 +370,7 @@ do_text_input(ColourPickerCtx *ctx, Rect r, Color colour) v2 sts = {.rv = MeasureTextEx(ctx->font, buf, ctx->font_size, 0)}; f32 cursor_x = r.pos.x + sts.x; - f32 cursor_width = ctx->is.cursor == ctx->is.buf_len ? 20 : 6; + f32 cursor_width = ctx->is.cursor == ctx->is.buf_len ? ctx->window_size.w * 0.03 : ctx->window_size.w * 0.01; buf[ctx->is.cursor - buf_delta] = saved_c; @@ -467,10 +473,6 @@ do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx, v2 relative_origin) Rect lr, sr, vr; get_slider_subrects(r, &lr, &sr, &vr); - const char *label = mode_labels[ctx->colour_mode][label_idx]; - v2 fpos = center_align_text_in_rect(lr, label, ctx->font, ctx->font_size); - DrawTextEx(ctx->font, label, fpos.rv, ctx->font_size, 0, ctx->fg); - v2 test_pos = ctx->mouse_pos; test_pos.x -= relative_origin.x; test_pos.y -= relative_origin.y; @@ -547,6 +549,7 @@ do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx, v2 relative_origin) draw_cardinal_triangle(tri_mid, SLIDER_TRI_SIZE, tri_scale, NORTH, ctx->fg); } + v2 fpos; { SliderState *s = &ctx->ss; b32 collides = CheckCollisionPointRec(test_pos.rv, vr.rr); @@ -578,6 +581,10 @@ do_slider(ColourPickerCtx *ctx, Rect r, i32 label_idx, v2 relative_origin) do_text_input(ctx, vr, colour_rl); } } + + const char *label = mode_labels[ctx->colour_mode][label_idx]; + fpos = center_align_text_in_rect(lr, label, ctx->font, ctx->font_size); + DrawTextEx(ctx->font, label, fpos.rv, ctx->font_size, 0, ctx->fg); } static void @@ -677,7 +684,7 @@ do_colour_stack(ColourPickerCtx *ctx, Rect sa) r.size.w *= 0.75; r.pos.x += (sa.size.w - r.size.w) * 0.5; - f32 y_pos_delta = r.size.h + 10; + f32 y_pos_delta = r.size.h * 1.2; r.pos.y -= y_pos_delta * css->y_off_t; /* NOTE: Stack is moving up; draw last top item as it moves up and fades out */ @@ -875,7 +882,7 @@ do_vertical_slider(ColourPickerCtx *ctx, v2 test_pos, Rect r, i32 idx, EndShaderMode(); - DrawRectangleRoundedLinesEx(r.rr, SLIDER_ROUNDNESS, 0, 4 * SLIDER_BORDER_WIDTH, ctx->bg); + DrawRectangleRoundedLinesEx(r.rr, SLIDER_ROUNDNESS, 0, 4.85 * SLIDER_BORDER_WIDTH, ctx->bg); DrawRectangleRoundedLinesEx(r.rr, SLIDER_ROUNDNESS, 0, SLIDER_BORDER_WIDTH, SLIDER_BORDER_COLOUR); @@ -1050,6 +1057,18 @@ do_picker_mode(ColourPickerCtx *ctx, v2 relative_origin) DEBUG_EXPORT void do_colour_picker(ColourPickerCtx *ctx, f32 dt, Vector2 window_pos, Vector2 mouse_pos) { + if (IsWindowResized()) { + ctx->window_size.h = GetScreenHeight(); + ctx->window_size.w = ctx->window_size.h / WINDOW_ASPECT_RATIO; + ctx->flags |= CPF_REFILL_TEXTURE; + SetWindowSize(ctx->window_size.w, ctx->window_size.h); + + UnloadTexture(ctx->font.texture); + if (ctx->window_size.w < 480) ctx->font = LoadFont_lora_sb_1_inc(); + else ctx->font = LoadFont_lora_sb_0_inc(); + ctx->font_size = ctx->font.baseSize; + } + ctx->dt = dt; ctx->mouse_pos.rv = mouse_pos; ctx->window_pos.rv = window_pos; diff --git a/gen_incs.c b/gen_incs.c @@ -59,9 +59,16 @@ main(void) s8 smem = {.data = mem, .len = sizeof(mem)}; SetTraceLogLevel(LOG_NONE); - char *font_inc_name = "lora_sb_inc.h"; - if (!ExportFontAsCodeEx("assets/Lora-SemiBold.ttf", font_inc_name, FONT_SIZE, 0, 0)) - printf("Failed to export font: %s\n", font_inc_name); + { + s8 tmem = smem; + int font_sizes[] = { FONT_SIZE, FONT_SIZE/2 }; + for (int i = 0; i < sizeof(font_sizes)/sizeof(*font_sizes); i++) { + snprintf((char *)tmem.data, tmem.len, "lora_sb_%d_inc.h", i); + if (!ExportFontAsCodeEx("assets/Lora-SemiBold.ttf", (char *)tmem.data, + font_sizes[i], 0, 0)) + printf("Failed to export font: %s\n", (char *)tmem.data); + } + } FILE *out_file = fopen("external/include/shader_inc.h", "w"); if (!out_file) { diff --git a/main.c b/main.c @@ -151,8 +151,11 @@ main(i32 argc, char *argv[]) SetConfigFlags(FLAG_VSYNC_HINT); InitWindow(ctx.window_size.w, ctx.window_size.h, "Colour Picker"); + /* NOTE: do this after initing so that the window starts out floating in tiling wm */ + SetWindowMinSize(320, 320 * WINDOW_ASPECT_RATIO); + SetWindowState(FLAG_WINDOW_RESIZABLE); - ctx.font = LoadFont_lora_sb_inc(); + ctx.font = LoadFont_lora_sb_0_inc(); ctx.font_size = ctx.font.baseSize; while(!WindowShouldClose()) { diff --git a/util.c b/util.c @@ -8,7 +8,8 @@ #include <stdint.h> #include "shader_inc.h" -#include "lora_sb_inc.h" +#include "lora_sb_0_inc.h" +#include "lora_sb_1_inc.h" #include "config.h" #ifndef asm @@ -82,6 +83,8 @@ enum input_indices { enum cardinal_direction { NORTH, EAST, SOUTH, WEST }; +#define WINDOW_ASPECT_RATIO (4.3f/3.2f) + #define BUTTON_HOVER_SPEED 8.0f #define SLIDER_BORDER_COLOUR (Color){.r = 0x00, .g = 0x00, .b = 0x00, .a = 0xCC}