prep game ui nuklear ctx

efd/v1
Dominik Madarász 2023-01-15 19:12:28 +01:00
parent d51d88002d
commit f24ab7dc04
5 changed files with 51 additions and 41 deletions

View File

@ -22,12 +22,19 @@
#include "packets/pkt_01_welcome.h" #include "packets/pkt_01_welcome.h"
#include "packets/pkt_switch_viewer.h" #include "packets/pkt_switch_viewer.h"
#define RAYLIB_NUKLEAR_IMPLEMENTATION
ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
#include "raylib-nuklear.h"
ZPL_DIAGNOSTIC_POP
static uint8_t game_mode; static uint8_t game_mode;
static uint8_t game_should_close; static uint8_t game_should_close;
static world_view *world_viewers; static world_view *world_viewers;
static world_view *active_viewer; static world_view *active_viewer;
static struct nk_context *game_ui = 0;
static WORLD_PKT_READER(pkt_reader) { static WORLD_PKT_READER(pkt_reader) {
pkt_header header = {0}; pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen); uint32_t ok = pkt_header_decode(&header, data, datalen);
@ -148,6 +155,8 @@ void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_
world_viewers_init(num_viewers); world_viewers_init(num_viewers);
active_viewer = &world_viewers[0]; active_viewer = &world_viewers[0];
camera_reset(); camera_reset();
game_ui = InitNuklear(12);
} }
if (game_mode != GAMEKIND_SINGLE) { if (game_mode != GAMEKIND_SINGLE) {
@ -201,7 +210,9 @@ void game_shutdown() {
// TODO(zaklaus): crashes on exit // TODO(zaklaus): crashes on exit
//platform_shutdown(); //platform_shutdown();
} UnloadNuklear(game_ui);
}
} }
uint8_t game_is_running() { uint8_t game_is_running() {
@ -219,6 +230,7 @@ game_kind game_get_kind(void) {
void game_input() { void game_input() {
if (game_mode != GAMEKIND_HEADLESS) { if (game_mode != GAMEKIND_HEADLESS) {
platform_input(); platform_input();
UpdateNuklear(game_ui);
} }
} }
@ -247,6 +259,7 @@ void game_update() {
void game_render() { void game_render() {
if (game_mode != GAMEKIND_HEADLESS) { if (game_mode != GAMEKIND_HEADLESS) {
platform_render(); platform_render();
DrawNuklear(game_ui);
} }
} }

View File

@ -9,8 +9,6 @@
#include "models/components.h" #include "models/components.h"
#define RAYLIB_NUKLEAR_IMPLEMENTATION
#define NK_INCLUDE_STANDARD_VARARGS
ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0) ZPL_DIAGNOSTIC_PUSH_WARNLEVEL(0)
#include "raylib-nuklear.h" #include "raylib-nuklear.h"
ZPL_DIAGNOSTIC_POP ZPL_DIAGNOSTIC_POP
@ -50,7 +48,7 @@ static uint8_t is_handle_ctrl_held;
static float debug_xpos = DBG_START_XPOS; static float debug_xpos = DBG_START_XPOS;
static float debug_ypos = DBG_START_YPOS; static float debug_ypos = DBG_START_YPOS;
static zpl_u16 sel_item_id = 0; static zpl_u16 sel_item_id = 0;
static struct nk_context *nk_ctx = 0; static struct nk_context *dev_ui = 0;
typedef enum { typedef enum {
L_NONE = 0, L_NONE = 0,
@ -414,10 +412,10 @@ void debug_draw(void) {
ActSpawnItemNext(); ActSpawnItemNext();
// Initialize Nuklear ctx // Initialize Nuklear ctx
nk_ctx = InitNuklear(10); dev_ui = InitNuklear(10);
} }
UpdateNuklear(nk_ctx); UpdateNuklear(dev_ui);
float xpos = debug_xpos; float xpos = debug_xpos;
float ypos = debug_ypos; float ypos = debug_ypos;
@ -464,7 +462,7 @@ void debug_draw(void) {
debug_draw_list(items, xpos, ypos, 0); debug_draw_list(items, xpos, ypos, 0);
} }
DrawNuklear(nk_ctx); DrawNuklear(dev_ui);
} }
debug_area_status check_mouse_area(float xpos, float ypos, float w, float h) { debug_area_status check_mouse_area(float xpos, float ypos, float w, float h) {

View File

@ -4,43 +4,43 @@
#include "models/items.h" #include "models/items.h"
void ToolAssetInspector(void) { void ToolAssetInspector(void) {
if (nk_begin(nk_ctx, "Asset Inspector", nk_rect(400, 100, 240, 800), if (nk_begin(dev_ui, "Asset Inspector", nk_rect(400, 100, 240, 800),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_TITLE)) NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_TITLE))
{ {
for (int i = 0; i < MAX_ASSETS; i++) { for (int i = 0; i < MAX_ASSETS; i++) {
uint16_t idx = assets_find(i); uint16_t idx = assets_find(i);
if (idx != ASSET_INVALID && nk_tree_push_id(nk_ctx, NK_TREE_TAB, asset_names[i], NK_MINIMIZED, i)) { if (idx != ASSET_INVALID && nk_tree_push_id(dev_ui, NK_TREE_TAB, asset_names[i], NK_MINIMIZED, i)) {
{ {
// draw kind // draw kind
const char *asset_kind_name = assets_get_kind_name(idx); const char *asset_kind_name = assets_get_kind_name(idx);
nk_labelf(nk_ctx, NK_TEXT_LEFT, "kind: %s", asset_kind_name); nk_labelf(dev_ui, NK_TEXT_LEFT, "kind: %s", asset_kind_name);
nk_labelf(nk_ctx, NK_TEXT_LEFT, "spawnable entity: %s", entity_spawn_provided(i) ? "true" : "false"); nk_labelf(dev_ui, NK_TEXT_LEFT, "spawnable entity: %s", entity_spawn_provided(i) ? "true" : "false");
// draw block // draw block
block_id blk_id = blocks_find(i); block_id blk_id = blocks_find(i);
if (blk_id != 0xF) { if (blk_id != 0xF) {
if (nk_tree_push_id(nk_ctx, NK_TREE_NODE, "block", NK_MINIMIZED, i)) { if (nk_tree_push_id(dev_ui, NK_TREE_NODE, "block", NK_MINIMIZED, i)) {
{ {
nk_labelf(nk_ctx, NK_TEXT_LEFT, "symbol: %s", zpl_bprintf("%c", blocks_get_symbol(blk_id))); nk_labelf(dev_ui, NK_TEXT_LEFT, "symbol: %s", zpl_bprintf("%c", blocks_get_symbol(blk_id)));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "flags: %u", blocks_get_flags(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "flags: %u", blocks_get_flags(blk_id));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "drag: %f", blocks_get_drag(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "drag: %f", blocks_get_drag(blk_id));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "friction: %f", blocks_get_friction(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "friction: %f", blocks_get_friction(blk_id));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "bounce: %f", blocks_get_bounce(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "bounce: %f", blocks_get_bounce(blk_id));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "velx: %f", blocks_get_velx(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "velx: %f", blocks_get_velx(blk_id));
nk_labelf(nk_ctx, NK_TEXT_LEFT, "vely: %f", blocks_get_vely(blk_id)); nk_labelf(dev_ui, NK_TEXT_LEFT, "vely: %f", blocks_get_vely(blk_id));
} }
nk_tree_pop(nk_ctx); nk_tree_pop(dev_ui);
} }
} }
// draw item // draw item
item_id it_id = item_find_no_proxy(i); item_id it_id = item_find_no_proxy(i);
if (it_id != ASSET_INVALID) { if (it_id != ASSET_INVALID) {
if (nk_tree_push_id(nk_ctx, NK_TREE_NODE, "item", NK_MINIMIZED, i)) { if (nk_tree_push_id(dev_ui, NK_TREE_NODE, "item", NK_MINIMIZED, i)) {
{ {
item_desc it = item_get_desc(it_id); item_desc it = item_get_desc(it_id);
if (nk_button_label(nk_ctx, "spawn")) { if (nk_button_label(dev_ui, "spawn")) {
ecs_entity_t e = item_spawn(i, it.max_quantity); ecs_entity_t e = item_spawn(i, it.max_quantity);
ecs_entity_t plr = camera_get().ent_id; ecs_entity_t plr = camera_get().ent_id;
@ -48,27 +48,27 @@ void ToolAssetInspector(void) {
entity_set_position(e, origin->x, origin->y); entity_set_position(e, origin->x, origin->y);
} }
nk_labelf(nk_ctx, NK_TEXT_LEFT, "usage: %d", it.usage); nk_labelf(dev_ui, NK_TEXT_LEFT, "usage: %d", it.usage);
nk_labelf(nk_ctx, NK_TEXT_LEFT, "attachment: %d", it.attachment); nk_labelf(dev_ui, NK_TEXT_LEFT, "attachment: %d", it.attachment);
nk_labelf(nk_ctx, NK_TEXT_LEFT, "max quantity: %d", it.max_quantity); nk_labelf(dev_ui, NK_TEXT_LEFT, "max quantity: %d", it.max_quantity);
nk_labelf(nk_ctx, NK_TEXT_LEFT, "has storage: %s", it.has_storage ? "true" : "false"); nk_labelf(dev_ui, NK_TEXT_LEFT, "has storage: %s", it.has_storage ? "true" : "false");
// todo: draw item-specific data // todo: draw item-specific data
} }
nk_tree_pop(nk_ctx); nk_tree_pop(dev_ui);
} }
} }
} }
nk_tree_pop(nk_ctx); nk_tree_pop(dev_ui);
} }
} }
nk_end(nk_ctx); nk_end(dev_ui);
} }
} }
void ToolEntityInspector(void) { void ToolEntityInspector(void) {
if (nk_begin(nk_ctx, "Entity Inspector", nk_rect(660, 100, 240, 800), if (nk_begin(dev_ui, "Entity Inspector", nk_rect(660, 100, 240, 800),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_TITLE)) NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| NK_WINDOW_TITLE))
{ {
static ecs_query_t *q = 0; static ecs_query_t *q = 0;
@ -87,31 +87,31 @@ void ToolEntityInspector(void) {
const Classify *c = ecs_field(&it, Classify, 2); const Classify *c = ecs_field(&it, Classify, 2);
for (int i = 0; i < it.count; i++) { for (int i = 0; i < it.count; i++) {
if (nk_tree_push_id(nk_ctx, NK_TREE_NODE, zpl_bprintf("%lld [%s]", it.entities[i], class_names[c[i].id]), NK_MINIMIZED, (int)it.entities[i])) { if (nk_tree_push_id(dev_ui, NK_TREE_NODE, zpl_bprintf("%lld [%s]", it.entities[i], class_names[c[i].id]), NK_MINIMIZED, (int)it.entities[i])) {
{ {
nk_label(nk_ctx, "position:", NK_TEXT_LEFT); nk_label(dev_ui, "position:", NK_TEXT_LEFT);
nk_property_float(nk_ctx, "#x:", ZPL_F32_MIN, &p[i].x, ZPL_F32_MAX, 0.1f, 0.2f); nk_property_float(dev_ui, "#x:", ZPL_F32_MIN, &p[i].x, ZPL_F32_MAX, 0.1f, 0.2f);
nk_property_float(nk_ctx, "#y:", ZPL_F32_MIN, &p[i].y, ZPL_F32_MAX, 0.1f, 0.2f); nk_property_float(dev_ui, "#y:", ZPL_F32_MIN, &p[i].y, ZPL_F32_MAX, 0.1f, 0.2f);
if (nk_button_label(nk_ctx, "teleport to")) { if (nk_button_label(dev_ui, "teleport to")) {
ecs_entity_t plr = camera_get().ent_id; ecs_entity_t plr = camera_get().ent_id;
Position const* origin = ecs_get(world_ecs(), it.entities[i], Position); Position const* origin = ecs_get(world_ecs(), it.entities[i], Position);
entity_set_position(plr, origin->x, origin->y); entity_set_position(plr, origin->x, origin->y);
} }
if (nk_button_label(nk_ctx, "teleport here")) { if (nk_button_label(dev_ui, "teleport here")) {
ecs_entity_t plr = camera_get().ent_id; ecs_entity_t plr = camera_get().ent_id;
Position const* origin = ecs_get(world_ecs(), plr, Position); Position const* origin = ecs_get(world_ecs(), plr, Position);
entity_set_position(it.entities[i], origin->x, origin->y); entity_set_position(it.entities[i], origin->x, origin->y);
} }
} }
nk_tree_pop(nk_ctx); nk_tree_pop(dev_ui);
} }
} }
} }
nk_end(nk_ctx); nk_end(dev_ui);
} }
} }

View File

@ -105,9 +105,6 @@ bool inventory_draw_crafting_list(entity_view *e, float xpos, float ypos) {
float start_xpos = xpos; float start_xpos = xpos;
float start_ypos = ypos; float start_ypos = ypos;
if (!e->sel_ent)
return DAREA_OUTSIDE;
for (uint16_t i = 0; e->craftables[i]; ++i) { for (uint16_t i = 0; e->craftables[i]; ++i) {
asset_id id = e->craftables[i]; asset_id id = e->craftables[i];
inventory_draw_crafting_btn(start_xpos+1, ypos+1, asset_names[id], id, BLACK); inventory_draw_crafting_btn(start_xpos+1, ypos+1, asset_names[id], id, BLACK);

View File

@ -35,6 +35,8 @@ static bool request_shutdown;
static float temp_time = 0.0f; static float temp_time = 0.0f;
extern struct nk_context *game_ui;
float get_cached_time(void) { float get_cached_time(void) {
return temp_time; return temp_time;
} }