Created
July 11, 2019 21:02
-
-
Save carloscm/03af9d41a24de64bc6b53275e11d832b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct UIDeclare { | |
virtual bool button(const char* txt, int flags = 0) = 0; | |
virtual void label(const char* txt, int flags = 0) = 0; | |
virtual void space() = 0; | |
}; | |
struct UILayoutEmit : public UIDeclare { | |
lay_context ctx; | |
lay_id root = 0; | |
eastl::vector<lay_id> ids; | |
void init() { | |
lay_init_context(&ctx); | |
lay_reserve_items_capacity(&ctx, 1024); | |
root = lay_item(&ctx); | |
lay_set_size_xy(&ctx, root, 250, 0); // sizes? | |
lay_set_contain(&ctx, root, LAY_COLUMN); | |
} | |
bool button(const char* txt, int flags = 0) override { | |
lay_id id = lay_item(&ctx); | |
lay_insert(&ctx, root, id); | |
lay_set_size_xy(&ctx, id, 0, 24); // H? | |
lay_set_behave(&ctx, id, LAY_HFILL); | |
ids.push_back(id); | |
return false; | |
} | |
void label(const char* txt, int flags = 0) override { | |
lay_id id = lay_item(&ctx); | |
lay_insert(&ctx, root, id); | |
lay_set_size_xy(&ctx, id, 0, 24); // H? | |
lay_set_behave(&ctx, id, LAY_HFILL); | |
ids.push_back(id); | |
} | |
void space() override { | |
lay_id id = lay_item(&ctx); | |
lay_insert(&ctx, root, id); | |
lay_set_size_xy(&ctx, id, 0, 24); // H? | |
lay_set_behave(&ctx, id, LAY_HFILL); | |
ids.push_back(id); | |
} | |
void run() { | |
lay_run_context(&ctx); | |
} | |
virtual ~UILayoutEmit() { | |
lay_destroy_context(&ctx); | |
} | |
}; | |
struct UINuklearEmit : public UIDeclare { | |
nk_context* ctx = nullptr; | |
UILayoutEmit layout; | |
size_t idx = 0; | |
void emit_rect() { | |
assert(idx < layout.ids.size()); | |
lay_vec4 rect = lay_get_rect(&layout.ctx, layout.ids[idx]); | |
nk_layout_space_push(ctx, nk_rect(rect[0], rect[1], rect[2], rect[3])); | |
idx++; | |
} | |
bool button(const char* txt, int flags = 0) override { | |
emit_rect(); | |
return nk_button_label(ctx, txt); | |
} | |
void label(const char* txt, int flags = 0) override { | |
emit_rect(); | |
nk_label(ctx, txt, flags); | |
} | |
void space() override { | |
emit_rect(); | |
nk_spacing(ctx, 1); | |
} | |
}; | |
template <typename Callable> | |
void emit_ui(Callable c) { | |
nk_context* ctx = get_nk_context(); | |
UINuklearEmit nk_emit; | |
nk_emit.ctx = ctx; | |
nk_emit.layout.init(); | |
c(nk_emit.layout); | |
nk_emit.layout.run(); | |
lay_vec4 root_rect = lay_get_rect(&nk_emit.layout.ctx, nk_emit.layout.root); | |
nk_begin(ctx, "##main_menu", | |
nk_rect(20.0f, 20.0f, root_rect[2], root_rect[3]), 0 | |
//| NK_WINDOW_BORDER | |
| NK_WINDOW_NO_SCROLLBAR | |
); | |
nk_layout_space_begin(ctx, NK_STATIC, root_rect[3], INT_MAX); | |
c(nk_emit); | |
nk_end(ctx); | |
} | |
void update_main_menu_top(UIState* ui_state, const platform::UpdateState& us) | |
{ | |
const bool is_playing = bool(ui_state->client); | |
const bool is_client = is_playing && ui_state->client->client; | |
const bool is_server = bool(ui_state->server); | |
const bool is_offline = !is_client && !is_server; | |
const bool is_online = !is_offline; | |
const bool has_files = true; | |
emit_ui([&](UIDeclare& ui) { | |
if (!is_online) { | |
ui.label(lloc("single_player", "Single player"), NK_TEXT_CENTERED); | |
if (ui.button(lloc("new_game", "New game"))) { | |
mode = Mode::single_pick_map; | |
refresh_files(); | |
} | |
if (has_files) { | |
if (ui.button(lloc("load_game", "Load game"))) { | |
mode = Mode::load_game; | |
refresh_files(); | |
} | |
} | |
if (is_playing) { | |
if (ui.button(lloc("save_game", "Save game"))) { | |
mode = Mode::save_game; | |
refresh_files(); | |
} | |
} | |
ui.space(); | |
} | |
if (!is_playing) { | |
ui.label(lloc("multiplayer", "Multiplayer"), NK_TEXT_CENTERED); | |
if (ui.button(lloc("join_game", "Join game"))) { | |
mode = Mode::join_dialog; | |
} | |
if (ui.button(lloc("host_game", "Host game"))) { | |
mode = Mode::server_pick_map; | |
refresh_files(); | |
} | |
ui.space(); | |
} | |
if (is_playing) { | |
if (ui.button(lloc("quit_game", "Quit game"))) { | |
ui_state->destroy_game(); | |
ui_state->initial_screen(); | |
} | |
} | |
if (ui.button(lloc("quit_desktop", "Quit to desktop"))) { | |
request_quit_now(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment