From 6f7374c2fa58c8692b51018864b802e6b876d305 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 23 Nov 2024 21:52:24 +0100 Subject: A new start --- src/ui/button.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/ui/button.c (limited to 'src/ui/button.c') diff --git a/src/ui/button.c b/src/ui/button.c new file mode 100644 index 0000000..773fb7b --- /dev/null +++ b/src/ui/button.c @@ -0,0 +1,101 @@ +bool button_draw_background_percentage(float scale, s32 x, s32 y, s32 w, s32 h, color tint, color fill, float percentage, color bar_fill) +{ + s32 cornor_size = img_button_topleft->width*(scale/2); + s32 top_width = w - (cornor_size*2); + s32 size_height = h - (cornor_size*2); + + // top + renderer->render_image_tint(img_button_topleft, x, y, cornor_size, cornor_size, tint); + renderer->render_image_tint(img_button_top, x + cornor_size, y, top_width, cornor_size, tint); + renderer->render_image_tint(img_button_topright, x + cornor_size + top_width, y, cornor_size, cornor_size, tint); + + // left + renderer->render_image_tint(img_button_left, x, y + cornor_size-1, cornor_size, size_height+2, tint); + + // right + renderer->render_image_tint(img_button_right, x + cornor_size + top_width, y + cornor_size-1, cornor_size, size_height+2, tint); + + // bottom + renderer->render_image_tint(img_button_bottomleft, x, y + cornor_size + size_height, cornor_size, cornor_size, tint); + renderer->render_image_tint(img_button_bottom, x + cornor_size, y + cornor_size + size_height, top_width, cornor_size, tint); + renderer->render_image_tint(img_button_bottomright, x + cornor_size + top_width, y + cornor_size + size_height, cornor_size, cornor_size, tint); + + // fill + s32 pad = cornor_size-1; + renderer->render_rectangle(x+pad, y+pad, w-(pad*2), h-(pad*2), fill); + renderer->render_rectangle(x+pad, y+pad, (w-(pad*2))*percentage, h-(pad*2), bar_fill); + + return _global_mouse.x >= x && _global_mouse.x <= x + w && _global_mouse.y >= y && _global_mouse.y <= y + h; +} + +bool button_draw_background(float scale, s32 x, s32 y, s32 w, s32 h, color tint, color fill) +{ + s32 cornor_size = img_button_topleft->width*(scale/2); + s32 top_width = w - (cornor_size*2); + s32 size_height = h - (cornor_size*2); + + // top + renderer->render_image_tint(img_button_topleft, x, y, cornor_size, cornor_size, tint); + renderer->render_image_tint(img_button_top, x + cornor_size, y, top_width, cornor_size, tint); + renderer->render_image_tint(img_button_topright, x + cornor_size + top_width, y, cornor_size, cornor_size, tint); + + // left + renderer->render_image_tint(img_button_left, x, y + cornor_size-1, cornor_size, size_height+2, tint); + + // right + renderer->render_image_tint(img_button_right, x + cornor_size + top_width, y + cornor_size-1, cornor_size, size_height+2, tint); + + // bottom + renderer->render_image_tint(img_button_bottomleft, x, y + cornor_size + size_height, cornor_size, cornor_size, tint); + renderer->render_image_tint(img_button_bottom, x + cornor_size, y + cornor_size + size_height, top_width, cornor_size, tint); + renderer->render_image_tint(img_button_bottomright, x + cornor_size + top_width, y + cornor_size + size_height, cornor_size, cornor_size, tint); + + // fill + s32 pad = cornor_size-1; + renderer->render_rectangle(x+pad, y+pad, w-(pad*2), h-(pad*2), fill); + + return _global_mouse.x >= x && _global_mouse.x <= x + w && _global_mouse.y >= y && _global_mouse.y <= y + h; +} + +bool button_render(float scale, button_type enabled, char* text, s32 x, s32 y, s32 w, s32 h) +{ + bool result = false; + + color tint = COLOR_WHITE; + color fill = COLOR_BUTTON; + + if (enabled == BUTTON_ENABLED || enabled == BUTTON_HIGHLIGHTED) { + if (mouse_interacts(x,y,w,h)) { + platform_set_cursor(main_window, CURSOR_POINTER); + if (is_left_clicked()) { + result = true; + audio_play_sound(snd_click, AUDIO_CHANNEL_SFX_1); + } + tint = COLOR_BUTTON_ACTIVE_TINT; + fill = COLOR_BUTTON_ACTIVE; + } + + if (enabled == BUTTON_HIGHLIGHTED) { + tint = COLOR_BUTTON_HIGHLIGHTED_TINT; + fill = COLOR_BUTTON_HIGHLIGHTED_ACTIVE; + } + } + else if (enabled == BUTTON_DISABLED) { + tint = COLOR_BUTTON_DISABLED_TINT; + fill = COLOR_BUTTON_DISABLED; + } + + button_draw_background(scale,x,y,w,h,tint,fill); + + // text + if (text) { + font* font_sml = fnt_rd24; + s32 text_y = y + (h/2) - (font_sml->px_h/2); + s32 game_title_width = renderer->calculate_text_width(font_sml, text); + s32 text_x = x + (w/2) - (game_title_width/2); + + renderer->render_text(font_sml, text_x+1, text_y+1, text, COLOR_TEXT_SHADOW); + renderer->render_text(font_sml, text_x, text_y, text, COLOR_TEXT); + } + return result; +} \ No newline at end of file -- cgit v1.2.3-70-g09d2