parent
02ee41c788
commit
f8b4d09554
@ -0,0 +1,81 @@ |
||||
#include "console.h" |
||||
#include "gui.h" |
||||
#include "game.h" |
||||
#include "../common/log.h" |
||||
#include "../common/common.h" |
||||
|
||||
#include <assert.h> |
||||
#include <string.h> |
||||
#include <nuklear.h> |
||||
|
||||
static struct nk_color color_normal; |
||||
|
||||
static int console_filter(const struct nk_text_edit *box, nk_rune unicode); |
||||
|
||||
void console_init(struct Console* console) |
||||
{ |
||||
assert(console); |
||||
|
||||
console->visible = false; |
||||
console->text_region_height = 22.f; |
||||
console->line_height = 20.f; |
||||
|
||||
color_normal = nk_rgb(255, 255, 255); |
||||
memset(console->console_command_text, '\0', MAX_CONSOLE_COMMAND_LEN); |
||||
} |
||||
|
||||
void console_toggle(struct Console* console) |
||||
{ |
||||
console->visible = !console->visible; |
||||
} |
||||
|
||||
void console_update(struct Console* console, struct Gui_State* gui_state, float dt) |
||||
{ |
||||
if(!console->visible) return; |
||||
|
||||
struct nk_context* context = &gui_state->context; |
||||
struct Game_State* game_state = game_state_get(); |
||||
|
||||
int win_width = 0, win_height = 0; |
||||
platform->window.get_drawable_size(game_state->window, &win_width, &win_height); |
||||
int half_height = win_height / 2; |
||||
|
||||
if(nk_begin_titled(context, "Console", "Console", nk_recti(0, 0, win_width, half_height), NK_WINDOW_SCROLL_AUTO_HIDE)) |
||||
{ |
||||
nk_layout_row_dynamic(context, nk_window_get_height(context) - console->text_region_height * 2, 1); |
||||
if(nk_group_begin(context, "Log", NK_WINDOW_SCROLL_AUTO_HIDE)) |
||||
{ |
||||
for(int i = 0; i < MAX_CONSOLE_LINES / 2; i++) |
||||
{ |
||||
nk_layout_row_dynamic(context, console->line_height, 1); |
||||
nk_labelf_colored(context, NK_TEXT_ALIGN_LEFT | NK_TEXT_ALIGN_MIDDLE, color_normal, "This is a test log line. Do not panic!"); |
||||
} |
||||
nk_group_end(context); |
||||
} |
||||
|
||||
nk_layout_row_dynamic(context, console->text_region_height, 1); |
||||
int edit_flags = NK_EDIT_GOTO_END_ON_ACTIVATE | NK_EDIT_FIELD | NK_EDIT_SIG_ENTER; |
||||
nk_edit_focus(context, edit_flags); |
||||
int edit_state = nk_edit_string_zero_terminated(context, edit_flags, console->console_command_text, MAX_CONSOLE_COMMAND_LEN, console_filter); |
||||
if(edit_state & NK_EDIT_COMMITED) |
||||
{ |
||||
log_message("New message entered : %s", console->console_command_text); |
||||
memset(console->console_command_text, '\0', MAX_CONSOLE_COMMAND_LEN); |
||||
} |
||||
} |
||||
nk_end(context); |
||||
} |
||||
|
||||
void console_destroy(struct Console* console) |
||||
{ |
||||
|
||||
} |
||||
|
||||
int console_filter(const struct nk_text_edit *box, nk_rune unicode) |
||||
{ |
||||
NK_UNUSED(box); |
||||
if(unicode > 128 || unicode == 96) // Ignore tilde or anything other than ascii
|
||||
return nk_false; |
||||
else
|
||||
return nk_true; |
||||
} |
@ -0,0 +1,24 @@ |
||||
#ifndef CONSOLE_H |
||||
#define CONSOLE_H |
||||
|
||||
#include <stdbool.h> |
||||
|
||||
#define MAX_CONSOLE_COMMAND_LEN 128 |
||||
#define MAX_CONSOLE_LINES 1024 |
||||
#define MAX_CONSOLE_LINE_LEN 256 |
||||
|
||||
struct Console |
||||
{ |
||||
bool visible; |
||||
float text_region_height; |
||||
float line_height; |
||||
char console_command_text[MAX_CONSOLE_COMMAND_LEN]; |
||||
}; |
||||
|
||||
void console_init(struct Console* console); |
||||
void console_toggle(struct Console* console); |
||||
void console_update(struct Console* console, struct Gui_State* gui_state, float dt); |
||||
void console_destroy(struct Console* console); |
||||
|
||||
|
||||
#endif |
Loading…
Reference in new issue