added opengl text input

This commit is contained in:
vurtun 2015-05-01 13:18:19 +02:00
parent 95ce312b18
commit d8364f2bb6
3 changed files with 48 additions and 31 deletions

View File

@ -249,7 +249,6 @@ font_get_text_width(void *handle, const gui_char *t, gui_size l)
size_t text_len = 0;
size_t glyph_len;
struct font *font = handle;
assert(font);
if (!t || !l) return 0;
@ -330,7 +329,6 @@ font_new(const char *path, unsigned int font_height, unsigned int bake_height,
atlas.size = atlas.dim * atlas.dim * FONT_ATLAS_DEPTH;
atlas.memory = calloc((gui_size)atlas.size, 1);
memset(font, 0, sizeof(*font));
font->glyph_count = (unsigned int)atlas.range;
font->glyphes = calloc(atlas.range, sizeof(struct font_glyph));
font->fallback = &font->glyphes['?'];
@ -449,30 +447,11 @@ demo_panel(struct gui_panel_layout *panel, struct demo *demo)
demo->prog = gui_panel_progress(&tab, demo->prog, 100, gui_true);
demo->item_cur = gui_panel_selector(&tab, items, LEN(items), demo->item_cur);
demo->spinner = gui_panel_spinner(&tab, 0, demo->spinner, 250, 10, &demo->spin_act);
demo->in_len = gui_panel_input(&tab,demo->in_buf,demo->in_len,
MAX_BUFFER,&demo->in_act,GUI_INPUT_DEFAULT);
demo->in_len = gui_panel_input(&tab, demo->in_buf, demo->in_len,
MAX_BUFFER, &demo->in_act, GUI_INPUT_DEFAULT);
demo->group_off = gui_panel_group_end(panel, &tab);
}
#define glerror() glerror_(__FILE__, __LINE__)
static void
glerror_(const char *file, int line)
{
GLenum code = glGetError();
if (code == GL_INVALID_ENUM)
fprintf(stdout, "[GL] Error: (%s:%d) invalid value!\n", file, line);
else if (code == GL_INVALID_OPERATION)
fprintf(stdout, "[GL] Error: (%s:%d) invalid operation!\n", file, line);
else if (code == GL_INVALID_FRAMEBUFFER_OPERATION)
fprintf(stdout, "[GL] Error: (%s:%d) invalid frame op!\n", file, line);
else if (code == GL_OUT_OF_MEMORY)
fprintf(stdout, "[GL] Error: (%s:%d) out of memory!\n", file, line);
else if (code == GL_STACK_UNDERFLOW)
fprintf(stdout, "[GL] Error: (%s:%d) stack underflow!\n", file, line);
else if (code == GL_STACK_OVERFLOW)
fprintf(stdout, "[GL] Error: (%s:%d) stack overflow!\n", file, line);
}
static void
draw(struct gui_command_list *list, int width, int height)
{
@ -495,7 +474,6 @@ draw(struct gui_command_list *list, int width, int height)
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glerror();
cmd = list->begin;
while (cmd != list->end) {
@ -529,7 +507,9 @@ draw(struct gui_command_list *list, int width, int height)
} break;
case GUI_COMMAND_TEXT: {
struct gui_command_text *t = (void*)cmd;
font_draw_text(t->font, t->x, t->y, t->fg, t->string, t->length);
draw_rect(t->x, t->y, t->w, t->h, t->fg);
/*font_draw_text(t->font, t->x, t->y, t->fg, t->string,
* t->length);*/
} break;
default: break;
}
@ -579,6 +559,14 @@ btn(struct gui_input *in, SDL_Event *evt, gui_bool down)
gui_input_button(in, x, y, down);
}
static void
text(struct gui_input *in, SDL_Event *evt)
{
gui_glyph glyph;
memcpy(glyph, evt->text.text, GUI_UTF_SIZE);
gui_input_char(in, glyph);
}
static void
resize(SDL_Event *evt)
{
@ -639,7 +627,6 @@ main(int argc, char *argv[])
font.userdata = glfont;
font.height = glfont->height;
font.width = font_get_text_width;
gui_default_config(&config);
gui_panel_init(&panel, 50, 50, 420, 300,
GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE|
@ -666,6 +653,7 @@ main(int argc, char *argv[])
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&in, &evt, gui_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&in, &evt, gui_false);
else if (evt.type == SDL_MOUSEMOTION) motion(&in, &evt);
else if (evt.type == SDL_TEXTINPUT) text(&in, &evt);
}
gui_input_end(&in);

35
gui.c
View File

@ -985,6 +985,7 @@ gui_buffer_push_scissor(struct gui_command_buffer *buffer, gui_float x, gui_floa
gui_float w, gui_float h)
{
struct gui_command_scissor *cmd;
assert(buffer);
if (!buffer) return;
cmd = gui_buffer_push(buffer, GUI_COMMAND_SCISSOR, sizeof(*cmd));
if (!cmd) return;
@ -1005,6 +1006,7 @@ gui_buffer_push_line(struct gui_command_buffer *buffer, gui_float x0, gui_float
gui_float x1, gui_float y1, struct gui_color c)
{
struct gui_command_line *cmd;
assert(buffer);
if (!buffer) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
@ -1028,6 +1030,7 @@ gui_buffer_push_rect(struct gui_command_buffer *buffer, gui_float x, gui_float y
gui_float w, gui_float h, struct gui_color c)
{
struct gui_command_rect *cmd;
assert(buffer);
if (!buffer) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
@ -1051,6 +1054,7 @@ gui_buffer_push_circle(struct gui_command_buffer *buffer, gui_float x, gui_float
gui_float w, gui_float h, struct gui_color c)
{
struct gui_command_circle *cmd;
assert(buffer);
if (!buffer) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
@ -1075,6 +1079,7 @@ gui_buffer_push_triangle(struct gui_command_buffer *buffer, gui_float x0, gui_fl
gui_float x1, gui_float y1, gui_float x2, gui_float y2, struct gui_color c)
{
struct gui_command_triangle *cmd;
assert(buffer);
if (!buffer) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
@ -1102,6 +1107,7 @@ gui_buffer_push_image(struct gui_command_buffer *buffer, gui_float x, gui_float
gui_float w, gui_float h, gui_image img)
{
struct gui_command_image *cmd;
assert(buffer);
if (!buffer) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
@ -1126,8 +1132,9 @@ gui_buffer_push_text(struct gui_command_buffer *buffer, gui_float x, gui_float y
const struct gui_font *font, struct gui_color bg, struct gui_color fg)
{
struct gui_command_text *cmd;
if (!buffer) return;
if (!string || !length) return;
assert(buffer);
assert(font);
if (!buffer || !string || !length) return;
if (buffer->clipping == GUI_CLIP) {
const struct gui_rect *r = &buffer->clip;
if (!INTERSECT(r->x, r->y, r->w, r->h, x, y, w, h)) {
@ -1154,6 +1161,10 @@ void
gui_buffer_init_fixed(struct gui_command_buffer *buffer, const struct gui_memory *memory,
enum gui_clipping clipping)
{
assert(buffer);
assert(memory);
if (!buffer || !memory) return;
zero(buffer, sizeof(*buffer));
buffer->memory = memory->memory;
buffer->capacity = memory->size;
@ -1168,6 +1179,11 @@ void
gui_buffer_init(struct gui_command_buffer *buffer, const struct gui_allocator *memory,
gui_size initial_size, gui_float grow_factor, enum gui_clipping clipping)
{
assert(buffer);
assert(memory);
assert(initial_size);
if (!buffer || !memory || !initial_size) return;
zero(buffer, sizeof(*buffer));
buffer->memory = memory->alloc(memory->userdata, initial_size);
buffer->allocator = *memory;
@ -1183,6 +1199,12 @@ void
gui_buffer_begin(struct gui_canvas *canvas, struct gui_command_buffer *buffer,
gui_size width, gui_size height)
{
assert(canvas);
assert(buffer);
assert(buffer->memory);
assert(buffer->begin == buffer->end);
if (!canvas || !buffer) return;
canvas->userdata = buffer;
canvas->width = width;
canvas->height = height;
@ -1199,7 +1221,8 @@ gui_buffer_end(struct gui_command_list *list, struct gui_command_buffer *buffer,
struct gui_canvas *canvas, struct gui_memory_status *status)
{
assert(buffer);
if (!buffer) return;
assert(canvas);
if (!buffer || !canvas) return;
if (status) {
status->size = buffer->capacity;
status->allocated = buffer->allocated;
@ -1234,6 +1257,7 @@ gui_buffer_clear(struct gui_command_buffer *buffer)
void
gui_default_config(struct gui_config *config)
{
assert(config);
if (!config) return;
config->scrollbar_width = 16;
vec2_load(config->panel_padding, 15.0f, 10.0f);
@ -1494,6 +1518,11 @@ gui_panel_begin_stacked(struct gui_panel_layout *layout, struct gui_panel *panel
const struct gui_input *in)
{
gui_bool inpanel;
assert(layout);
assert(panel);
assert(stack);
assert(canvas);
inpanel = INBOX(in->mouse_prev.x, in->mouse_prev.y, panel->x, panel->y, panel->w, panel->h);
if (in->mouse_down && in->mouse_clicked && inpanel && panel != stack->end) {
struct gui_panel *iter = panel->next;

4
gui.h
View File

@ -521,8 +521,8 @@ gui_size gui_panel_selector(struct gui_panel_layout*, const char *items[],
gui_size item_count, gui_size item_current);
gui_int gui_panel_plot(struct gui_panel_layout*, const gui_float *values, gui_size value_count);
gui_int gui_panel_histo(struct gui_panel_layout*, const gui_float *values, gui_size value_count);
gui_bool gui_panel_tab_begin(struct gui_panel_layout *parent, struct gui_panel_layout*,
const char*, gui_bool minimized);
gui_bool gui_panel_tab_begin(struct gui_panel_layout*, struct gui_panel_layout*,
const char*, gui_bool);
void gui_panel_tab_end(struct gui_panel_layout*, struct gui_panel_layout *tab);
void gui_panel_group_begin(struct gui_panel_layout *panel, struct gui_panel_layout*,
const char*,gui_float offset);