From fd1af144506ee35a0fa32aa96e1f2afefb9a0068 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Fri, 28 Feb 2025 08:55:09 +0100 Subject: [PATCH] =?UTF-8?q?Avanc=C3=A9e=20d'UI=20matinales?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui.c | 77 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/ui.c b/src/ui.c index fc4010b..2cd3fa1 100644 --- a/src/ui.c +++ b/src/ui.c @@ -41,25 +41,28 @@ void draw_buttons(Widget *widget){ } } +void buttons_do_event(Widget *widget, Game *game, MTEvent event){ + if(event.type != EV_Mouse || event.click != MOUSE_BUTTON_LEFT) + return; + for(int i = 0; i < widget->button_n; i++){ + Button *butt = &widget->buttons[i]; + if(CheckCollisionPointRec(event.pos, butt->box) && butt->handler) + butt->handler(game); + } +} + void main_menu_draw(Widget *widget, Game *game){ ClearBackground(color_main_brown); draw_text_centered("Mineur Tycoon", 50, 100, BLACK); draw_text_centered(VERSION_STR, 160, 30, BLACK); - DrawRectangleRounded((Rectangle){500, 200, 280, 260}, 0.1, 3, color_main_brown1); + DrawRectangleRounded((Rectangle){500, 200, 280, 260}, 0.1, 3, + color_main_brown1); draw_buttons(widget); } -void main_menu_do_event(Widget *widget, Game *game, MTEvent event){ - if(event.type == EV_Mouse && event.click == MOUSE_BUTTON_LEFT){ - gui_info.widget_active[0] = true; - gui_info.widget_active[1] = false; - game->paused = false; - } -} - void widget_draw_game(Widget *widget, Game *game){ draw_game(game); } @@ -68,10 +71,16 @@ void main_menu_exit(Game *game){ game->exit = true; } +void main_menu_new_game(Game *game){ + gui_info.widget_active[0] = true; + gui_info.widget_active[1] = false; + game->paused = false; +} + Widget main_menu = { .box = {.x = 0, .y = 0, .width = -1, .height = -1}, - .draw = &main_menu_draw, - .do_event = &main_menu_do_event, + .draw = main_menu_draw, + .do_event = buttons_do_event, .capt_flags = CF_Mouse | CF_Keyb, .buttons = { {.box = {520,220,240,40}, @@ -79,7 +88,7 @@ Widget main_menu = { .color_1 = PINK, .color_txt = BLACK, .txt = "New game", - .handler = NULL + .handler = main_menu_new_game }, {.box = {520,270,240,40}, .color_0 = WHITE, @@ -98,7 +107,7 @@ Widget main_menu = { {.box = {520,400,240,40}, .color_0 = WHITE, .color_1 = PINK, - .color_txt = GRAY, + .color_txt = BLACK, .txt = "Exit", .handler = main_menu_exit }, @@ -106,9 +115,33 @@ Widget main_menu = { .button_n = 4 }; +void worker_widget_draw(Widget *widget, Game *game){ + DrawRectangleRounded(widget->box, 0.1, 3, color_main_brown); +} + +void worker_widget_do_event(Widget *widget, Game *game, MTEvent event){ + if(event.click == MOUSE_BUTTON_LEFT) + gui_info.widget_active[2] = false; +} + +Widget worker_widget = { + .box = {.x = 40, .y = 40, .width = 300, .height = 600}, + .draw = worker_widget_draw, + .do_event = NULL, + .capt_flags = CF_Mouse, + .buttons = { + + }, + .button_n = 0 +}; + +Widget machine_widget = { + +}; + Widget game_widget = { .box = {0,0,0,0}, - .draw = &widget_draw_game, + .draw = widget_draw_game, .do_event = NULL, .capt_flags = 0, .buttons = {}, @@ -119,7 +152,12 @@ void init_ui(){ snprintf(_ver_str, 256, "v%d.%d-%d", VERSION_MAJ, VERSION_MIN, VERSION_PATCH); - gui_info = (GUIInfo){{&game_widget, &main_menu}, {false, true}, NULL}; + // Android exclusive key -> no exit key + SetExitKey(KEY_MENU); + + gui_info = (GUIInfo){{&game_widget, &main_menu, &worker_widget}, + {false, true, false}, + NULL}; } void draw_widgets(Game *game){ @@ -196,6 +234,7 @@ void try_interface(Game *game, V2d pos){ return; printf("found worker\n"); + gui_info.widget_active[2] = true; return; } @@ -204,6 +243,12 @@ bool keydown(int key){ return IsKeyDown(key); } +void goto_pause_menu(Game *game){ + memset(gui_info.widget_active, false, sizeof(bool)*WIDGET_N); + gui_info.widget_active[1] = true; + game->paused = true; +} + int get_mouse(){ for(int i = 0; i < MOUSE_BUTTON_NULL; i++){ if(IsMouseButtonPressed(i)) @@ -234,4 +279,6 @@ void get_keys(Game *game){ game->camera.zoom *= 1/1.1; if(IsKeyPressed(KEY_P)) game->paused = !game->paused; + if(IsKeyPressed(KEY_ESCAPE)) + goto_pause_menu(game); }