From ba78e5360e353e2452793d8ab875e1b27b7c6cde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sun, 15 Jan 2023 20:21:56 +0100 Subject: [PATCH] add crafting tooltip --- code/foundation/src/core/game.c | 7 +++-- code/foundation/src/core/game.h | 3 ++ code/foundation/src/gui/inventory.c | 18 ++++++++++++ code/foundation/src/models/crafting.c | 40 +++++++++++++++++---------- code/foundation/src/models/crafting.h | 18 ++++++++++++ code/games/minimal/src/platform.c | 5 ++++ code/games/sandbox/src/platform.c | 5 ++++ 7 files changed, 80 insertions(+), 16 deletions(-) diff --git a/code/foundation/src/core/game.c b/code/foundation/src/core/game.c index 3800b3b..9ff85a5 100644 --- a/code/foundation/src/core/game.c +++ b/code/foundation/src/core/game.c @@ -33,7 +33,7 @@ static uint8_t game_should_close; static world_view *world_viewers; static world_view *active_viewer; -static struct nk_context *game_ui = 0; +struct nk_context *game_ui = 0; static WORLD_PKT_READER(pkt_reader) { pkt_header header = {0}; @@ -259,10 +259,13 @@ void game_update() { void game_render() { if (game_mode != GAMEKIND_HEADLESS) { platform_render(); - DrawNuklear(game_ui); } } +void game_draw_ui() { + DrawNuklear(game_ui); +} + void game_action_send_keystate(game_keystate_data *data) { pkt_send_keystate_send(active_viewer->view_id, data); } diff --git a/code/foundation/src/core/game.h b/code/foundation/src/core/game.h index 509585e..6d9e554 100644 --- a/code/foundation/src/core/game.h +++ b/code/foundation/src/core/game.h @@ -23,6 +23,9 @@ void game_input(); void game_update(); void game_render(); +//~ Called from platform.c +void game_draw_ui(); + //~ NOTE(zaklaus): world view management world_view *game_world_view_get_active(void); world_view *game_world_view_get(uint16_t idx); diff --git a/code/foundation/src/gui/inventory.c b/code/foundation/src/gui/inventory.c index 8f82fd2..0067168 100644 --- a/code/foundation/src/gui/inventory.c +++ b/code/foundation/src/gui/inventory.c @@ -89,6 +89,23 @@ inv_draw_result inventory_draw_crafting_btn(float xpos, float ypos, const char * inv_is_inside = true; player_inv.craft_item = id; } + + if (check_mouse_area(xpos, ypos, name_width, 22) != DAREA_OUTSIDE) { + Vector2 mpos = GetMousePosition(); + recipe rp = craft_get_recipe_data(craft_get_recipe_id_from_product(id)); + int num_reagents = 0; + for (int i = 0; rp.reagents[i].id; i++) num_reagents++; + if (nk_begin(game_ui , name, nk_rect(mpos.x+15, mpos.y+15, name_width+5, 80+25*(float)num_reagents), + NK_WINDOW_BORDER | NK_WINDOW_TITLE | NK_WINDOW_NO_INPUT)) { + if (nk_tree_push_id(game_ui, NK_TREE_NODE, "Reagents", NK_MAXIMIZED, id)) { + for (asset_id i = 0; rp.reagents[i].id; i++) { + nk_label(game_ui, asset_names[rp.reagents[i].id], NK_TEXT_LEFT); + } + nk_tree_pop(game_ui); + } + nk_end(game_ui); + } + } Color _c_compare_lol = BLACK; if (!zpl_memcompare(&color, &_c_compare_lol, sizeof(Color))) { @@ -97,6 +114,7 @@ inv_draw_result inventory_draw_crafting_btn(float xpos, float ypos, const char * inv_draw_result res = DrawColoredText(xpos, ypos, text, new_color); ypos = res.y; + return res; } diff --git a/code/foundation/src/models/crafting.c b/code/foundation/src/models/crafting.c index 8f2a36d..7ded89f 100644 --- a/code/foundation/src/models/crafting.c +++ b/code/foundation/src/models/crafting.c @@ -1,19 +1,6 @@ #include "crafting.h" #include "models/items.h" -typedef struct { - asset_id id; - uint32_t qty; -} reagent; - -typedef struct { - asset_id product; - uint32_t product_qty; - int32_t process_ticks; - asset_id producer; - reagent *reagents; -} recipe; - #include "lists/crafting_list.c" uint32_t craft__find_num_recipes_by_reagent(asset_id producer, asset_id id) { @@ -46,6 +33,20 @@ recipe *craft__find_recipe_by_reagent(asset_id producer, asset_id id, uint32_t s } } return NULL; +} + +uint16_t craft_get_recipe_id_from_product(asset_id id) { + for (int i = 0; i < (int)MAX_RECIPES; ++i) { + if (id == recipes[i].product) { + return i; + } + } + return ASSET_INVALID; +} + +recipe craft_get_recipe_data(uint16_t i) { + ZPL_ASSERT(i >= 0 && i < MAX_RECIPES); + return recipes[i]; } bool craft_is_reagent_used_in_producer(asset_id reagent, asset_id producer) { @@ -53,7 +54,6 @@ bool craft_is_reagent_used_in_producer(asset_id reagent, asset_id producer) { } bool craft_is_item_produced_by_producer(asset_id item, asset_id producer) { - uint32_t num_recipes=0; for (int i = 0; i < (int)MAX_RECIPES; ++i) { if (recipes[i].producer == producer && item == recipes[i].product) { return true; @@ -62,6 +62,18 @@ bool craft_is_item_produced_by_producer(asset_id item, asset_id producer) { return false; } +bool craft_is_item_produced_by_reagent(asset_id item, asset_id reagent) { + for (int i = 0; i < (int)MAX_RECIPES; ++i) { + if (item == recipes[i].product) { + for (int j = 0; recipes[i].reagents[j].id; ++j) { + if (recipes[i].reagents[j].id == reagent) + return true; + } + } + } + return false; +} + uint16_t craft_get_num_recipes(void) { return MAX_RECIPES; } diff --git a/code/foundation/src/models/crafting.h b/code/foundation/src/models/crafting.h index 63f8c75..6e92240 100644 --- a/code/foundation/src/models/crafting.h +++ b/code/foundation/src/models/crafting.h @@ -4,6 +4,19 @@ #include "models/components.h" +typedef struct { + asset_id id; + uint32_t qty; +} reagent; + +typedef struct { + asset_id product; + uint32_t product_qty; + int32_t process_ticks; + asset_id producer; + reagent *reagents; +} recipe; + // NOTE(zaklaus): resolves recipe dependencies and consumes reagents // to enqueue a production of a new item. // TODO(zaklaus): "items" is assumed to come from ItemContainer component. @@ -15,9 +28,14 @@ bool craft_is_reagent_used_in_producer(asset_id reagent, asset_id producer); // used to filter out craftables bool craft_is_item_produced_by_producer(asset_id item, asset_id producer); +// used to filter out reagents +bool craft_is_item_produced_by_reagent(asset_id item, asset_id reagent); + // NOTE(zaklaus): utilities uint16_t craft_get_num_recipes(void); asset_id craft_get_recipe_asset(uint16_t id); +uint16_t craft_get_recipe_id_from_product(asset_id id); +recipe craft_get_recipe_data(uint16_t i); //~TODO(zaklaus): not implemented and might get removed diff --git a/code/games/minimal/src/platform.c b/code/games/minimal/src/platform.c index a49b0cb..be02823 100644 --- a/code/games/minimal/src/platform.c +++ b/code/games/minimal/src/platform.c @@ -14,6 +14,10 @@ #include "utils/raylib_helpers.h" #include "platform/renderer.h" +ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0) +#include "raylib-nuklear.h" +ZPL_DIAGNOSTIC_POP + #define ARCH_IMPL #include "platform/arch.h" @@ -95,6 +99,7 @@ void platform_render() { } debug_draw(); + game_draw_ui(); } EndDrawing(); diff --git a/code/games/sandbox/src/platform.c b/code/games/sandbox/src/platform.c index 652622d..6c57966 100644 --- a/code/games/sandbox/src/platform.c +++ b/code/games/sandbox/src/platform.c @@ -16,6 +16,10 @@ #include "utils/raylib_helpers.h" #include "platform/renderer.h" +ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0) +#include "raylib-nuklear.h" +ZPL_DIAGNOSTIC_POP + #define ARCH_IMPL #include "platform/arch.h" @@ -186,6 +190,7 @@ void platform_render() { } display_conn_status(); debug_draw(); + game_draw_ui(); } EndDrawing();