|
|
@ -32,39 +32,31 @@ static void gui_on_mousemotion(const struct Event* event); |
|
|
|
static void gui_on_mousebutton(const struct Event* event); |
|
|
|
static void gui_on_mousebutton(const struct Event* event); |
|
|
|
static void gui_on_key(const struct Event* event); |
|
|
|
static void gui_on_key(const struct Event* event); |
|
|
|
|
|
|
|
|
|
|
|
static void gui_upload_atlas(const void *image, int width, int height); |
|
|
|
static void gui_upload_atlas(struct Gui* gui, const void *image, int width, int height); |
|
|
|
static void gui_font_set_default(void); |
|
|
|
static void gui_font_set_default(struct Gui* gui); |
|
|
|
|
|
|
|
|
|
|
|
static struct Gui_State* gui_state = NULL; |
|
|
|
bool gui_init(struct Gui* gui) |
|
|
|
|
|
|
|
|
|
|
|
bool gui_init(void) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
bool success = false; |
|
|
|
bool success = false; |
|
|
|
gui_state = malloc(sizeof(*gui_state)); |
|
|
|
|
|
|
|
if(!gui_state) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
log_error("gui:init", "Malloc failed, out of memory"); |
|
|
|
|
|
|
|
return success; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
nk_init_default(&gui_state->context, 0); |
|
|
|
nk_init_default(&gui->context, 0); |
|
|
|
nk_buffer_init_default(&gui_state->cmds); |
|
|
|
nk_buffer_init_default(&gui->cmds); |
|
|
|
gui_state->context.clip.copy = gui_on_clipbard_copy; |
|
|
|
gui->context.clip.copy = gui_on_clipbard_copy; |
|
|
|
gui_state->context.clip.paste = gui_on_clipbard_paste; |
|
|
|
gui->context.clip.paste = gui_on_clipbard_paste; |
|
|
|
gui_state->context.clip.userdata = nk_handle_ptr(0); |
|
|
|
gui->context.clip.userdata = nk_handle_ptr(0); |
|
|
|
gui_state->current_font = NULL; |
|
|
|
gui->current_font = NULL; |
|
|
|
gui_state->shader = shader_create("gui.vert", "gui.frag"); |
|
|
|
gui->shader = shader_create("gui.vert", "gui.frag"); |
|
|
|
if(gui_state->shader < 0) |
|
|
|
if(gui->shader < 0) |
|
|
|
{ |
|
|
|
{ |
|
|
|
log_error("gui:init", "Failed to create shader for gui"); |
|
|
|
log_error("gui:init", "Failed to create shader for gui"); |
|
|
|
free(gui_state); |
|
|
|
free(gui); |
|
|
|
return success; |
|
|
|
return success; |
|
|
|
} |
|
|
|
} |
|
|
|
gui_state->uniform_tex = shader_get_uniform_location(gui_state->shader, "sampler"); |
|
|
|
gui->uniform_tex = shader_get_uniform_location(gui->shader, "sampler"); |
|
|
|
gui_state->uniform_proj = shader_get_uniform_location(gui_state->shader, "proj_mat"); |
|
|
|
gui->uniform_proj = shader_get_uniform_location(gui->shader, "proj_mat"); |
|
|
|
gui_state->attrib_pos = shader_get_attribute_location(gui_state->shader, "vPosition"); |
|
|
|
gui->attrib_pos = shader_get_attribute_location(gui->shader, "vPosition"); |
|
|
|
gui_state->attrib_uv = shader_get_attribute_location(gui_state->shader, "vUV"); |
|
|
|
gui->attrib_uv = shader_get_attribute_location(gui->shader, "vUV"); |
|
|
|
gui_state->attrib_col = shader_get_attribute_location(gui_state->shader, "vColor"); |
|
|
|
gui->attrib_col = shader_get_attribute_location(gui->shader, "vColor"); |
|
|
|
|
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
/* buffer setup */ |
|
|
|
/* buffer setup */ |
|
|
@ -73,21 +65,21 @@ bool gui_init(void) |
|
|
|
size_t vt = offsetof(struct Gui_Vertex, uv); |
|
|
|
size_t vt = offsetof(struct Gui_Vertex, uv); |
|
|
|
size_t vc = offsetof(struct Gui_Vertex, col); |
|
|
|
size_t vc = offsetof(struct Gui_Vertex, col); |
|
|
|
|
|
|
|
|
|
|
|
glGenBuffers(1, &gui_state->vbo); |
|
|
|
glGenBuffers(1, &gui->vbo); |
|
|
|
glGenBuffers(1, &gui_state->ebo); |
|
|
|
glGenBuffers(1, &gui->ebo); |
|
|
|
glGenVertexArrays(1, &gui_state->vao); |
|
|
|
glGenVertexArrays(1, &gui->vao); |
|
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(gui_state->vao); |
|
|
|
glBindVertexArray(gui->vao); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gui_state->vbo); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gui->vbo); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gui_state->ebo); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gui->ebo); |
|
|
|
|
|
|
|
|
|
|
|
glEnableVertexAttribArray((GLuint)gui_state->attrib_pos); |
|
|
|
glEnableVertexAttribArray((GLuint)gui->attrib_pos); |
|
|
|
glEnableVertexAttribArray((GLuint)gui_state->attrib_uv); |
|
|
|
glEnableVertexAttribArray((GLuint)gui->attrib_uv); |
|
|
|
glEnableVertexAttribArray((GLuint)gui_state->attrib_col); |
|
|
|
glEnableVertexAttribArray((GLuint)gui->attrib_col); |
|
|
|
|
|
|
|
|
|
|
|
glVertexAttribPointer((GLuint)gui_state->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp); |
|
|
|
glVertexAttribPointer((GLuint)gui->attrib_pos, 2, GL_FLOAT, GL_FALSE, vs, (void*)vp); |
|
|
|
glVertexAttribPointer((GLuint)gui_state->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt); |
|
|
|
glVertexAttribPointer((GLuint)gui->attrib_uv, 2, GL_FLOAT, GL_FALSE, vs, (void*)vt); |
|
|
|
glVertexAttribPointer((GLuint)gui_state->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc); |
|
|
|
glVertexAttribPointer((GLuint)gui->attrib_col, 4, GL_UNSIGNED_BYTE, GL_TRUE, vs, (void*)vc); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
|
glBindTexture(GL_TEXTURE_2D, 0); |
|
|
@ -97,9 +89,9 @@ bool gui_init(void) |
|
|
|
|
|
|
|
|
|
|
|
//gui_font_set("Ubuntu-R.ttf", 14);
|
|
|
|
//gui_font_set("Ubuntu-R.ttf", 14);
|
|
|
|
//gui_font_set("FiraSans-Regular.ttf", 14);
|
|
|
|
//gui_font_set("FiraSans-Regular.ttf", 14);
|
|
|
|
gui_font_set("roboto_condensed.ttf", 18); |
|
|
|
gui_font_set(gui, "roboto_condensed.ttf", 18); |
|
|
|
// gui_theme_set(GT_RED);
|
|
|
|
// gui_theme_set(GT_RED);
|
|
|
|
gui_theme_set(GT_DEFAULT); |
|
|
|
gui_theme_set(gui, GT_DEFAULT); |
|
|
|
|
|
|
|
|
|
|
|
struct Event_Manager* event_manager = game_state_get()->event_manager; |
|
|
|
struct Event_Manager* event_manager = game_state_get()->event_manager; |
|
|
|
event_manager_subscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key); |
|
|
|
event_manager_subscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key); |
|
|
@ -114,20 +106,20 @@ bool gui_init(void) |
|
|
|
return success; |
|
|
|
return success; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_upload_atlas(const void *image, int width, int height) |
|
|
|
void gui_upload_atlas(struct Gui* gui, const void *image, int width, int height) |
|
|
|
{ |
|
|
|
{ |
|
|
|
gui_state->font_tex = texture_create("Gui_Font_Tex", |
|
|
|
gui->font_tex = texture_create("Gui_Font_Tex", |
|
|
|
TU_DIFFUSE, |
|
|
|
TU_DIFFUSE, |
|
|
|
width, height, |
|
|
|
width, height, |
|
|
|
GL_RGBA, |
|
|
|
GL_RGBA, |
|
|
|
GL_RGBA, |
|
|
|
GL_RGBA, |
|
|
|
GL_UNSIGNED_BYTE, |
|
|
|
GL_UNSIGNED_BYTE, |
|
|
|
image); |
|
|
|
image); |
|
|
|
texture_set_param(gui_state->font_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
texture_set_param(gui->font_tex, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
|
|
|
texture_set_param(gui_state->font_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
texture_set_param(gui->font_tex, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_cleanup(void) |
|
|
|
void gui_cleanup(struct Gui* gui) |
|
|
|
{ |
|
|
|
{ |
|
|
|
struct Event_Manager* event_manager = game_state_get()->event_manager; |
|
|
|
struct Event_Manager* event_manager = game_state_get()->event_manager; |
|
|
|
event_manager_unsubscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key); |
|
|
|
event_manager_unsubscribe(event_manager, EVT_KEY_PRESSED, &gui_on_key); |
|
|
@ -137,17 +129,16 @@ void gui_cleanup(void) |
|
|
|
event_manager_unsubscribe(event_manager, EVT_MOUSEMOTION, &gui_on_mousemotion); |
|
|
|
event_manager_unsubscribe(event_manager, EVT_MOUSEMOTION, &gui_on_mousemotion); |
|
|
|
event_manager_unsubscribe(event_manager, EVT_MOUSEWHEEL, &gui_on_mousewheel); |
|
|
|
event_manager_unsubscribe(event_manager, EVT_MOUSEWHEEL, &gui_on_mousewheel); |
|
|
|
|
|
|
|
|
|
|
|
nk_font_atlas_clear(&gui_state->atlas); |
|
|
|
nk_font_atlas_clear(&gui->atlas); |
|
|
|
nk_free(&gui_state->context); |
|
|
|
nk_free(&gui->context); |
|
|
|
shader_remove(gui_state->shader); |
|
|
|
shader_remove(gui->shader); |
|
|
|
texture_remove(gui_state->font_tex); |
|
|
|
texture_remove(gui->font_tex); |
|
|
|
glDeleteBuffers(1, &gui_state->vbo); |
|
|
|
glDeleteBuffers(1, &gui->vbo); |
|
|
|
glDeleteBuffers(1, &gui_state->ebo); |
|
|
|
glDeleteBuffers(1, &gui->ebo); |
|
|
|
nk_buffer_free(&gui_state->cmds); |
|
|
|
nk_buffer_free(&gui->cmds); |
|
|
|
free(gui_state); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_render(enum nk_anti_aliasing AA) |
|
|
|
void gui_render(struct Gui* gui, enum nk_anti_aliasing AA) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int width, height; |
|
|
|
int width, height; |
|
|
|
int display_width, display_height; |
|
|
|
int display_width, display_height; |
|
|
@ -173,9 +164,9 @@ void gui_render(enum nk_anti_aliasing AA) |
|
|
|
glEnable(GL_SCISSOR_TEST); |
|
|
|
glEnable(GL_SCISSOR_TEST); |
|
|
|
|
|
|
|
|
|
|
|
/* setup program */ |
|
|
|
/* setup program */ |
|
|
|
shader_bind(gui_state->shader); |
|
|
|
shader_bind(gui->shader); |
|
|
|
glUniform1i(gui_state->uniform_tex, 0); |
|
|
|
glUniform1i(gui->uniform_tex, 0); |
|
|
|
shader_set_uniform(UT_MAT4, gui_state->uniform_proj, &gui_mat); |
|
|
|
shader_set_uniform(UT_MAT4, gui->uniform_proj, &gui_mat); |
|
|
|
{ |
|
|
|
{ |
|
|
|
/* convert from command queue into draw list and draw to screen */ |
|
|
|
/* convert from command queue into draw list and draw to screen */ |
|
|
|
const struct nk_draw_command *cmd; |
|
|
|
const struct nk_draw_command *cmd; |
|
|
@ -183,9 +174,9 @@ void gui_render(enum nk_anti_aliasing AA) |
|
|
|
const nk_draw_index *offset = NULL; |
|
|
|
const nk_draw_index *offset = NULL; |
|
|
|
|
|
|
|
|
|
|
|
/* allocate vertex and element buffer */ |
|
|
|
/* allocate vertex and element buffer */ |
|
|
|
glBindVertexArray(gui_state->vao); |
|
|
|
glBindVertexArray(gui->vao); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gui_state->vbo); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, gui->vbo); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gui_state->ebo); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gui->ebo); |
|
|
|
|
|
|
|
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, MAX_GUI_VERTEX_MEMORY, NULL, GL_STREAM_DRAW); |
|
|
|
glBufferData(GL_ARRAY_BUFFER, MAX_GUI_VERTEX_MEMORY, NULL, GL_STREAM_DRAW); |
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_GUI_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW); |
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_GUI_ELEMENT_MEMORY, NULL, GL_STREAM_DRAW); |
|
|
@ -207,7 +198,7 @@ void gui_render(enum nk_anti_aliasing AA) |
|
|
|
config.vertex_layout = vertex_layout; |
|
|
|
config.vertex_layout = vertex_layout; |
|
|
|
config.vertex_size = sizeof(struct Gui_Vertex); |
|
|
|
config.vertex_size = sizeof(struct Gui_Vertex); |
|
|
|
config.vertex_alignment = NK_ALIGNOF(struct Gui_Vertex); |
|
|
|
config.vertex_alignment = NK_ALIGNOF(struct Gui_Vertex); |
|
|
|
config.null = gui_state->null; |
|
|
|
config.null = gui->null; |
|
|
|
config.circle_segment_count = 22; |
|
|
|
config.circle_segment_count = 22; |
|
|
|
config.curve_segment_count = 22; |
|
|
|
config.curve_segment_count = 22; |
|
|
|
config.arc_segment_count = 22; |
|
|
|
config.arc_segment_count = 22; |
|
|
@ -219,13 +210,13 @@ void gui_render(enum nk_anti_aliasing AA) |
|
|
|
struct nk_buffer vbuf, ebuf; |
|
|
|
struct nk_buffer vbuf, ebuf; |
|
|
|
nk_buffer_init_fixed(&vbuf, vertices, (nk_size)MAX_GUI_VERTEX_MEMORY); |
|
|
|
nk_buffer_init_fixed(&vbuf, vertices, (nk_size)MAX_GUI_VERTEX_MEMORY); |
|
|
|
nk_buffer_init_fixed(&ebuf, elements, (nk_size)MAX_GUI_ELEMENT_MEMORY); |
|
|
|
nk_buffer_init_fixed(&ebuf, elements, (nk_size)MAX_GUI_ELEMENT_MEMORY); |
|
|
|
nk_convert(&gui_state->context, &gui_state->cmds, &vbuf, &ebuf, &config); |
|
|
|
nk_convert(&gui->context, &gui->cmds, &vbuf, &ebuf, &config); |
|
|
|
} |
|
|
|
} |
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER); |
|
|
|
glUnmapBuffer(GL_ARRAY_BUFFER); |
|
|
|
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); |
|
|
|
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER); |
|
|
|
|
|
|
|
|
|
|
|
/* iterate over and execute each draw command */ |
|
|
|
/* iterate over and execute each draw command */ |
|
|
|
nk_draw_foreach(cmd, &gui_state->context, &gui_state->cmds) |
|
|
|
nk_draw_foreach(cmd, &gui->context, &gui->cmds) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (!cmd->elem_count) continue; |
|
|
|
if (!cmd->elem_count) continue; |
|
|
|
texture_bind(cmd->texture.id); |
|
|
|
texture_bind(cmd->texture.id); |
|
|
@ -236,11 +227,11 @@ void gui_render(enum nk_anti_aliasing AA) |
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); |
|
|
|
glDrawElements(GL_TRIANGLES, (GLsizei)cmd->elem_count, GL_UNSIGNED_SHORT, offset); |
|
|
|
offset += cmd->elem_count; |
|
|
|
offset += cmd->elem_count; |
|
|
|
} |
|
|
|
} |
|
|
|
nk_clear(&gui_state->context); |
|
|
|
nk_clear(&gui->context); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
shader_unbind(); |
|
|
|
shader_unbind(); |
|
|
|
texture_unbind(gui_state->font_tex); |
|
|
|
texture_unbind(gui->font_tex); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
|
|
|
glBindVertexArray(0); |
|
|
|
glBindVertexArray(0); |
|
|
@ -278,7 +269,7 @@ void gui_on_key(const struct Event* event) |
|
|
|
|
|
|
|
|
|
|
|
int key = event->key.key; |
|
|
|
int key = event->key.key; |
|
|
|
bool mod_ctrl = event->key.mod_ctrl; |
|
|
|
bool mod_ctrl = event->key.mod_ctrl; |
|
|
|
struct nk_context* ctx = &gui_state->context; |
|
|
|
struct nk_context* ctx = &game_state_get()->gui->context; |
|
|
|
|
|
|
|
|
|
|
|
int down = event->type == EVT_KEY_PRESSED ? 1 : 0; |
|
|
|
int down = event->type == EVT_KEY_PRESSED ? 1 : 0; |
|
|
|
|
|
|
|
|
|
|
@ -351,7 +342,7 @@ void gui_on_mousebutton(const struct Event* event) |
|
|
|
int x = event->mousebutton.x; |
|
|
|
int x = event->mousebutton.x; |
|
|
|
int y = event->mousebutton.y; |
|
|
|
int y = event->mousebutton.y; |
|
|
|
int down = event->type == EVT_MOUSEBUTTON_PRESSED ? 1 : 0; |
|
|
|
int down = event->type == EVT_MOUSEBUTTON_PRESSED ? 1 : 0; |
|
|
|
struct nk_context* ctx = &gui_state->context; |
|
|
|
struct nk_context* ctx = &game_state_get()->gui->context; |
|
|
|
|
|
|
|
|
|
|
|
if(button == MSB_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); |
|
|
|
if(button == MSB_LEFT) nk_input_button(ctx, NK_BUTTON_LEFT, x, y, down); |
|
|
|
if(button == MSB_MIDDLE) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); |
|
|
|
if(button == MSB_MIDDLE) nk_input_button(ctx, NK_BUTTON_MIDDLE, x, y, down); |
|
|
@ -364,7 +355,7 @@ void gui_on_mousemotion(const struct Event* event) |
|
|
|
int y = event->mousemotion.y; |
|
|
|
int y = event->mousemotion.y; |
|
|
|
int xrel = event->mousemotion.xrel; |
|
|
|
int xrel = event->mousemotion.xrel; |
|
|
|
int yrel = event->mousemotion.yrel; |
|
|
|
int yrel = event->mousemotion.yrel; |
|
|
|
struct nk_context* ctx = &gui_state->context; |
|
|
|
struct nk_context* ctx = &game_state_get()->gui->context; |
|
|
|
|
|
|
|
|
|
|
|
if(ctx->input.mouse.grabbed) |
|
|
|
if(ctx->input.mouse.grabbed) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -379,7 +370,7 @@ void gui_on_mousemotion(const struct Event* event) |
|
|
|
|
|
|
|
|
|
|
|
void gui_on_textinput(const struct Event* event) |
|
|
|
void gui_on_textinput(const struct Event* event) |
|
|
|
{ |
|
|
|
{ |
|
|
|
struct nk_context *ctx = &gui_state->context; |
|
|
|
struct nk_context *ctx = &game_state_get()->gui->context; |
|
|
|
nk_glyph glyph; |
|
|
|
nk_glyph glyph; |
|
|
|
memcpy(glyph, event->text_input.text, NK_UTF_SIZE); |
|
|
|
memcpy(glyph, event->text_input.text, NK_UTF_SIZE); |
|
|
|
nk_input_glyph(ctx, glyph); |
|
|
|
nk_input_glyph(ctx, glyph); |
|
|
@ -389,29 +380,24 @@ void gui_on_mousewheel(const struct Event* event) |
|
|
|
{ |
|
|
|
{ |
|
|
|
int x = event->mousewheel.x; |
|
|
|
int x = event->mousewheel.x; |
|
|
|
int y = event->mousewheel.y; |
|
|
|
int y = event->mousewheel.y; |
|
|
|
struct nk_context* ctx = &gui_state->context; |
|
|
|
struct nk_context* ctx = &game_state_get()->gui->context; |
|
|
|
nk_input_scroll(ctx, nk_vec2(x, y)); |
|
|
|
nk_input_scroll(ctx, nk_vec2(x, y)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct Gui_State* gui_state_get(void) |
|
|
|
void gui_input_begin(struct Gui* gui) |
|
|
|
{ |
|
|
|
|
|
|
|
return gui_state; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void gui_input_begin(void) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_input_begin(&gui_state->context); |
|
|
|
nk_input_begin(&gui->context); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_input_end(void) |
|
|
|
void gui_input_end(struct Gui* gui) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_input_end(&gui_state->context); |
|
|
|
nk_input_end(&gui->context); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_font_set(const char* font_name, float font_size) |
|
|
|
void gui_font_set(struct Gui* gui, const char* font_name, float font_size) |
|
|
|
{ |
|
|
|
{ |
|
|
|
assert(font_name && font_size > 1.f); |
|
|
|
assert(font_name && font_size > 1.f); |
|
|
|
struct nk_font_atlas* atlas = &gui_state->atlas; |
|
|
|
struct nk_font_atlas* atlas = &gui->atlas; |
|
|
|
long size = 0; |
|
|
|
long size = 0; |
|
|
|
char* font_file_name = str_new("fonts/%s", font_name); |
|
|
|
char* font_file_name = str_new("fonts/%s", font_name); |
|
|
|
char* font_data = io_file_read(DIRT_INSTALL, font_file_name, "rb", &size); |
|
|
|
char* font_data = io_file_read(DIRT_INSTALL, font_file_name, "rb", &size); |
|
|
@ -419,16 +405,16 @@ void gui_font_set(const char* font_name, float font_size) |
|
|
|
if(!font_data) |
|
|
|
if(!font_data) |
|
|
|
{ |
|
|
|
{ |
|
|
|
log_error("gui:init", "Could not load font %s, reverting to default", font_name); |
|
|
|
log_error("gui:init", "Could not load font %s, reverting to default", font_name); |
|
|
|
if(!gui_state->current_font) gui_font_set_default(); |
|
|
|
if(!gui->current_font) gui_font_set_default(gui); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(gui_state->current_font) |
|
|
|
if(gui->current_font) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_font_atlas_clear(&gui_state->atlas); |
|
|
|
nk_font_atlas_clear(&gui->atlas); |
|
|
|
texture_remove(gui_state->font_tex); |
|
|
|
texture_remove(gui->font_tex); |
|
|
|
gui_state->font_tex = -1; |
|
|
|
gui->font_tex = -1; |
|
|
|
gui_state->current_font = NULL; |
|
|
|
gui->current_font = NULL; |
|
|
|
} |
|
|
|
} |
|
|
|
const void *image = NULL; |
|
|
|
const void *image = NULL; |
|
|
|
int w = 0, h = 0; |
|
|
|
int w = 0, h = 0; |
|
|
@ -436,43 +422,43 @@ void gui_font_set(const char* font_name, float font_size) |
|
|
|
nk_font_atlas_begin(atlas);
|
|
|
|
nk_font_atlas_begin(atlas);
|
|
|
|
struct nk_font *new_font = nk_font_atlas_add_from_memory(atlas, font_data, size, font_size, NULL); |
|
|
|
struct nk_font *new_font = nk_font_atlas_add_from_memory(atlas, font_data, size, font_size, NULL); |
|
|
|
image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
|
|
|
image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
|
|
|
gui_upload_atlas(image, w, h); |
|
|
|
gui_upload_atlas(gui, image, w, h); |
|
|
|
nk_font_atlas_end(atlas, nk_handle_id((int)gui_state->font_tex), &gui_state->null); |
|
|
|
nk_font_atlas_end(atlas, nk_handle_id((int)gui->font_tex), &gui->null); |
|
|
|
if(new_font) |
|
|
|
if(new_font) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_style_set_font(&gui_state->context, &new_font->handle); |
|
|
|
nk_style_set_font(&gui->context, &new_font->handle); |
|
|
|
log_message("Set %s as current font", font_name); |
|
|
|
log_message("Set %s as current font", font_name); |
|
|
|
gui_state->current_font = new_font; |
|
|
|
gui->current_font = new_font; |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
log_error("gui:init", "Could not add font %s, reverting to default", font_name); |
|
|
|
log_error("gui:init", "Could not add font %s, reverting to default", font_name); |
|
|
|
gui_font_set_default(); |
|
|
|
gui_font_set_default(gui); |
|
|
|
} |
|
|
|
} |
|
|
|
free(font_data); |
|
|
|
free(font_data); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_font_set_default(void) |
|
|
|
void gui_font_set_default(struct Gui* gui) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if(gui_state->current_font) |
|
|
|
if(gui->current_font) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_font_atlas_clear(&gui_state->atlas); |
|
|
|
nk_font_atlas_clear(&gui->atlas); |
|
|
|
texture_remove(gui_state->font_tex); |
|
|
|
texture_remove(gui->font_tex); |
|
|
|
} |
|
|
|
} |
|
|
|
struct nk_font_atlas* atlas = &gui_state->atlas; |
|
|
|
struct nk_font_atlas* atlas = &gui->atlas; |
|
|
|
nk_font_atlas_init_default(atlas); |
|
|
|
nk_font_atlas_init_default(atlas); |
|
|
|
const void *image = NULL; |
|
|
|
const void *image = NULL; |
|
|
|
int w = 0, h = 0; |
|
|
|
int w = 0, h = 0; |
|
|
|
image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
|
|
|
image = nk_font_atlas_bake(atlas, &w, &h, NK_FONT_ATLAS_RGBA32); |
|
|
|
gui_upload_atlas(image, w, h); |
|
|
|
gui_upload_atlas(gui, image, w, h); |
|
|
|
nk_style_set_font(&gui_state->context, &atlas->default_font->handle); |
|
|
|
nk_style_set_font(&gui->context, &atlas->default_font->handle); |
|
|
|
gui_state->current_font = atlas->default_font; |
|
|
|
gui->current_font = atlas->default_font; |
|
|
|
nk_font_atlas_end(atlas, nk_handle_id((int)gui_state->font_tex), &gui_state->null); |
|
|
|
nk_font_atlas_end(atlas, nk_handle_id((int)gui->font_tex), &gui->null); |
|
|
|
log_message("Set default font"); |
|
|
|
log_message("Set default font"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void gui_theme_set(enum Gui_Theme theme) |
|
|
|
void gui_theme_set(struct Gui* gui, enum Gui_Theme theme) |
|
|
|
{ |
|
|
|
{ |
|
|
|
struct nk_color table[NK_COLOR_COUNT]; |
|
|
|
struct nk_color table[NK_COLOR_COUNT]; |
|
|
|
if(theme == GT_WHITE) |
|
|
|
if(theme == GT_WHITE) |
|
|
@ -505,7 +491,7 @@ void gui_theme_set(enum Gui_Theme theme) |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(150, 150, 150, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(150, 150, 150, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(160, 160, 160, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(160, 160, 160, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(180, 180, 180, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(180, 180, 180, 255); |
|
|
|
nk_style_from_table(&gui_state->context, table); |
|
|
|
nk_style_from_table(&gui->context, table); |
|
|
|
} |
|
|
|
} |
|
|
|
else if(theme == GT_RED) |
|
|
|
else if(theme == GT_RED) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -537,7 +523,7 @@ void gui_theme_set(enum Gui_Theme theme) |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(70, 90, 100, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(70, 90, 100, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(75, 95, 105, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(75, 95, 105, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(181, 45, 69, 220); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(181, 45, 69, 220); |
|
|
|
nk_style_from_table(&gui_state->context, table); |
|
|
|
nk_style_from_table(&gui->context, table); |
|
|
|
} |
|
|
|
} |
|
|
|
else if(theme == GT_BLUE) |
|
|
|
else if(theme == GT_BLUE) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -569,7 +555,7 @@ void gui_theme_set(enum Gui_Theme theme) |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(70, 90, 100, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(70, 90, 100, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(75, 95, 105, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(75, 95, 105, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(156, 193, 220, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(156, 193, 220, 255); |
|
|
|
nk_style_from_table(&gui_state->context, table); |
|
|
|
nk_style_from_table(&gui->context, table); |
|
|
|
} |
|
|
|
} |
|
|
|
else if(theme == GT_DARK) |
|
|
|
else if(theme == GT_DARK) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -601,15 +587,15 @@ void gui_theme_set(enum Gui_Theme theme) |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(53, 88, 116, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_HOVER] = nk_rgba(53, 88, 116, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(58, 93, 121, 255); |
|
|
|
table[NK_COLOR_SCROLLBAR_CURSOR_ACTIVE] = nk_rgba(58, 93, 121, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(48, 83, 111, 255); |
|
|
|
table[NK_COLOR_TAB_HEADER] = nk_rgba(48, 83, 111, 255); |
|
|
|
nk_style_from_table(&gui_state->context, table); |
|
|
|
nk_style_from_table(&gui->context, table); |
|
|
|
} |
|
|
|
} |
|
|
|
else if(theme == GT_DEFAULT) |
|
|
|
else if(theme == GT_DEFAULT) |
|
|
|
{ |
|
|
|
{ |
|
|
|
nk_style_default(&gui_state->context); |
|
|
|
nk_style_default(&gui->context); |
|
|
|
} |
|
|
|
} |
|
|
|
else |
|
|
|
else |
|
|
|
{ |
|
|
|
{ |
|
|
|
log_error("gui:theme_set", "Unrecognized theme, reverting to default"); |
|
|
|
log_error("gui:theme_set", "Unrecognized theme, reverting to default"); |
|
|
|
nk_style_default(&gui_state->context); |
|
|
|
nk_style_default(&gui->context); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|