diff --git a/gui.c b/gui.c index c238928..b6a6510 100644 --- a/gui.c +++ b/gui.c @@ -181,15 +181,18 @@ gui_font_text_width(const struct gui_font *font, const gui_char *t, gui_size l) gui_long unicode; gui_size len = 0; const struct gui_font_glyph *g; - gui_size text_len = utf_decode(t, &unicode, l); + gui_size text_len; + + if (!t || !l) return 0; + text_len = utf_decode(t, &unicode, l); while (text_len < l) { if (unicode == UTF_INVALID) return 0; g = (unicode < font->glyph_count) ? &font->glyphes[unicode] : font->fallback; g = (g->code == 0) ? font->fallback : g; - len += g->width + g->xadvance; + len += g->xadvance * font->scale; text_len += utf_decode(t + text_len, &unicode, l - text_len); } - return (gui_size)((gui_float)len * font->scale); + return len; } static struct gui_draw_command* @@ -250,10 +253,8 @@ gui_vertex_line(struct gui_draw_command* cmd, gui_float x0, gui_float y0, vec2_load(b, x1, y1); vec2_sub(d, b, a); - if (d.x == 0.0f && d.y == 0.0f) - return; - - len = 0.5f / vec2_len(d); + len = vec2_len(d); + if (len) len = 0.5f / vec2_len(d); vec2_muls(hn, d, len); vec2_load(hp0, +hn.y, -hn.x); vec2_load(hp1, -hn.y, +hn.x); @@ -353,7 +354,7 @@ gui_text(struct gui_draw_queue *que, const struct gui_font *font, gui_float x, font->fallback; g = (g->code == 0) ? font->fallback : g; - y1 = y;/*(gui_float)(y + (g->yoff * font->scale));*/ + y1 = (gui_float)(y + (g->yoff * font->scale)); y2 = (gui_float)(y1 + (gui_float)g->height * font->scale); x1 = (gui_float)(x + g->xoff * font->scale); x2 = (gui_float)(x1 + (gui_float)g->width * font->scale); @@ -405,6 +406,7 @@ gui_end(struct gui_draw_queue *que) if (!que) return 0; needed = que->needed; que->needed = 0; + que->vertex_count = 0; return needed; } @@ -414,8 +416,12 @@ gui_button(struct gui_draw_queue *que, const struct gui_input *in, gui_int x, gui_int y, gui_int w, gui_int h, gui_int pad, const char *str, gui_int l) { + gui_int text_width; + gui_int label_x, label_y; + gui_int label_w, label_h; gui_int ret = gui_false; const gui_char *t = (const gui_char*)str; + if (!que || !in) return gui_false; if (!in->mouse_down && in->mouse_clicked) { const gui_int clicked_x = in->mouse_clicked_pos.x; @@ -423,9 +429,24 @@ gui_button(struct gui_draw_queue *que, const struct gui_input *in, if (INBOX(clicked_x, clicked_y, x, y, x+w, y+h)) ret = gui_true; } + gui_rectf(que, x, y, w, h, bg); - gui_rect(que, x+1, y, w-1, h, fg); - gui_text(que, font, x + pad, y + pad, w - 2 * pad, h - 2 * pad, fg, t, l); + gui_rect(que, x+1, y+1, w-1, h-1, fg); + if (!font || !str || !l) return ret; + + text_width = gui_font_text_width(font, t, l); + if (text_width > (w - 2 * pad)) { + label_x = x + pad; + label_y = y + pad; + label_w = w - 2 * pad; + label_h = h - 2 * pad; + } else { + label_w = w - 2 * pad; + label_h = h - 2 * pad; + label_x = x + (label_w - text_width) / 2; + label_y = y + (label_h - font->height) / 2; + } + gui_text(que, font, label_x, label_y, label_w, label_h, fg, t, l); return ret; } @@ -435,8 +456,8 @@ gui_toggle(struct gui_draw_queue *que, const struct gui_input *in, gui_int x, gui_int y, gui_int w, gui_int h, gui_int pad, const char *str, gui_int len, gui_int active) { - gui_int select_x, select_y; gui_int select_size; + gui_int select_x, select_y; gui_int cursor_x, cursor_y; gui_int cursor_pad, cursor_size; gui_int label_x, label_w; @@ -468,12 +489,12 @@ gui_toggle(struct gui_draw_queue *que, const struct gui_input *in, } gui_rectf(que, select_x, select_y, select_size, select_size, bg); - if (!active) gui_rectf(que, cursor_x, cursor_y, cursor_size, cursor_size, fg); + if (active) gui_rectf(que, cursor_x, cursor_y, cursor_size, cursor_size, fg); gui_text(que, font, label_x, y + pad, label_w, h - 2 * pad, white, t, len); return active; } -gui_int +gui_float gui_slider(struct gui_draw_queue *que, const struct gui_input *in, struct gui_color bg, struct gui_color fg, gui_int x, gui_int y, gui_int w, gui_int h, gui_int pad, @@ -618,8 +639,8 @@ gui_scroll(struct gui_draw_queue *que, const struct gui_input *in, yoff = y + (button_size - pad); boff = button_y + (button_size - pad); - up = gui_button(que, in, NULL, fg, bg, x, y, button_size, button_size, 0, "", 0); - down = gui_button(que,in,NULL, fg,bg,x,button_y,button_size,button_size,0,"", 0); + up = gui_button(que, in, NULL, fg, bg, x, y, button_size, button_size, 0, NULL, 0); + down = gui_button(que, in, NULL, fg, bg, x, button_y, button_size, button_size, 0,NULL, 0); gui_trianglef(que, xmid, y + pad, xoff, yoff, xpad, yoff, bg); gui_trianglef(que, xpad, ypad, xoff, ypad, xmid, boff, bg); @@ -645,3 +666,11 @@ gui_scroll(struct gui_draw_queue *que, const struct gui_input *in, return offset; } +gui_int gui_input(struct gui_draw_queue *que, const struct gui_input *in, + const struct gui_font *font, struct gui_color bg, struct gui_color fg, + gui_int x, gui_int y, gui_int w, gui_int h, + gui_char *buffer, gui_int *len, gui_bool active) +{ + return active; +} + diff --git a/gui.h b/gui.h index e2e743a..27d2747 100644 --- a/gui.h +++ b/gui.h @@ -93,16 +93,50 @@ struct gui_font { }; enum gui_panel_flags { - GUI_PANEL_HEADER = 0x01, - GUI_PANEL_MINIMIZABLE = 0x02, - GUI_PANEL_CLOSEABLE = 0x04, - GUI_PANEL_SCALEABLE = 0x08, - GUI_PANEL_SCROLLBAR = 0x10 + GUI_PANEL_BORDER = 0x01, + GUI_PANEL_TITLEBAR = 0x02, + GUI_PANEL_MINIMIZABLE = 0x04, + GUI_PANEL_CLOSEABLE = 0x08, + GUI_PANEL_SCALEABLE = 0x10, + GUI_PANEL_SCROLLBAR = 0x20 +}; + +enum gui_colors { + GUI_COLOR_TEXT, + GUI_COLOR_PANEL, + GUI_COLOR_BORDER, + GUI_COLOR_FRAME, + GUI_COLOR_TITLEBAR, + GUI_COLOR_BUTTON, + GUI_COLOR_BUTTON_HOVER, + GUI_COLOR_BUTTON_BORDER, + GUI_COLOR_TOGGLE, + GUI_COLOR_TOGGLE_ACTIVE, + GUI_COLOR_SCROLL, + GUI_COLOR_SCROLL_CURSOR, + GUI_COLOR_SLIDER, + GUI_COLOR_SLIDER_CURSOR, + GUI_COLOR_PROGRESS, + GUI_COLOR_PROGRESS_CURSOR, + GUI_COLOR_COUNT +}; + +struct gui_config { + gui_float global_alpha; + struct gui_vec2 window_padding; + struct gui_vec2 window_min_size; + struct gui_vec2 frame_padding; + struct gui_vec2 item_spacing; + struct gui_vec2 item_padding; + gui_int scroll_width; + struct gui_color colors[GUI_COLOR_COUNT]; }; struct gui_panel { gui_flags flags; + gui_int x, y; gui_int at_x, at_y; + gui_int width; }; void gui_input_begin(struct gui_input *in); @@ -115,7 +149,7 @@ void gui_input_end(struct gui_input *in); void gui_begin(struct gui_draw_queue *que, gui_byte *memory, gui_size size); gui_size gui_end(struct gui_draw_queue *que); const struct gui_draw_command *gui_next(const struct gui_draw_queue *que, - const struct gui_draw_command*); + const struct gui_draw_command *cmd); gui_int gui_button(struct gui_draw_queue *que, const struct gui_input *in, const struct gui_font *font, @@ -127,7 +161,7 @@ gui_int gui_toggle(struct gui_draw_queue *que, const struct gui_input *in, struct gui_color bg, struct gui_color fg, gui_int x, gui_int y, gui_int w, gui_int h, gui_int pad, const char *str, gui_int len, gui_int active); -gui_int gui_slider(struct gui_draw_queue *que, const struct gui_input *in, +gui_float gui_slider(struct gui_draw_queue *que, const struct gui_input *in, struct gui_color bg, struct gui_color fg, gui_int x, gui_int y, gui_int w, gui_int h, gui_int pad, gui_float min, gui_float v, gui_float max, gui_float step); @@ -145,4 +179,17 @@ gui_int gui_input(struct gui_draw_queue *que, const struct gui_input *in, gui_int x, gui_int y, gui_int w, gui_int h, gui_char *buffer, gui_int *len, gui_bool active); +gui_int gui_panel_begin(struct gui_panel *panel, const char *title, gui_flags f, + const struct gui_config *c, const struct gui_input *i); +gui_int gui_panel_button(struct gui_panel *panel, const char *str, gui_int len); +gui_int gui_panel_toggle(struct gui_panel *panel, const char *str, gui_int len, + gui_int active); +gui_float gui_panel_slider(struct gui_panel *panel, gui_float min, gui_float v, + gui_float max, gui_float step); +gui_float gui_panel_progress(struct gui_panel *panel, gui_size cur, gui_size max, + gui_bool modifyable); +gui_int gui_panel_input(struct gui_panel *panel, gui_char *buffer, gui_int *len, + gui_bool active); +void gui_panel_end(struct gui_panel *panel); + #endif diff --git a/opengl.c b/opengl.c index ba2eb45..8efaf74 100644 --- a/opengl.c +++ b/opengl.c @@ -72,9 +72,10 @@ static void brelease(struct GUI*, XEvent*); static void bmotion(struct GUI*, XEvent*); static void resize(struct GUI*, XEvent*); +static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*); static struct gui_font *ldfont(const char*, unsigned char); static void delfont(struct gui_font *font); -static void draw(struct GUI*, int, int , const struct gui_draw_queue*); +static void draw(int, int, const struct gui_draw_queue*); /* gobals */ static void @@ -302,7 +303,7 @@ ldfont(const char *name, unsigned char height) glyphes[id].yoff = *(float*)&iter[14]; glyphes[id].xadvance = *(float*)&iter[18]; glyphes[id].uv[0].u = (float)x/(float)tex_width; - glyphes[id].uv[0].v = (float)(y)/(float)tex_height; + glyphes[id].uv[0].v = (float)y/(float)tex_height; glyphes[id].uv[1].u = (float)(x+w)/(float)tex_width; glyphes[id].uv[1].v = (float)(y+h)/(float)tex_height; if (glyphes[id].height > max_height) max_height = glyphes[id].height; @@ -337,7 +338,7 @@ delfont(struct gui_font *font) } static void -draw(struct GUI *con, int width, int height, const struct gui_draw_queue *que) +draw(int width, int height, const struct gui_draw_queue *que) { const struct gui_draw_command *cmd; static const size_t v = sizeof(struct gui_vertex); @@ -394,6 +395,7 @@ draw(struct GUI *con, int width, int height, const struct gui_draw_queue *que) int main(int argc, char *argv[]) { + GLenum err; struct XWindow xw; struct GUI gui; long dt, started; @@ -401,9 +403,7 @@ main(int argc, char *argv[]) gui_size buffer_size = MAX_VERTEX_BUFFER; const struct gui_color colorA = {100, 100, 100, 255}; const struct gui_color colorB = {45, 45, 45, 255}; - const struct gui_color colorC = {0, 0, 0, 255}; static GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE,24, GLX_DOUBLEBUFFER, None}; - GLenum err; gui_float slider = 5.0f; gui_float prog = 60.0f; @@ -462,7 +462,7 @@ main(int argc, char *argv[]) /* ------------------------- GUI --------------------------*/ gui_begin(&gui.out, buffer, MAX_VERTEX_BUFFER); - if (gui_button(&gui.out, &gui.in, gui.font, colorA, colorC, 50,50,150,30,5,"button",6)) + if (gui_button(&gui.out, &gui.in, gui.font, colorA, colorB, 50,50,150,30,5,"button",6)) fprintf(stdout, "Button pressed!\n"); slider = gui_slider(&gui.out, &gui.in, colorA, colorB, 50, 100, 150, 30, 2, 0.0f, slider, 10.0f, 1.0f); @@ -479,7 +479,7 @@ main(int argc, char *argv[]) /* Draw */ glClearColor(45.0f/255.0f,45.0f/255.0f,45.0f/255.0f,1); glClear(GL_COLOR_BUFFER_BIT); - draw(&gui, xw.gwa.width, xw.gwa.height, &gui.out); + draw(xw.gwa.width, xw.gwa.height, &gui.out); glXSwapBuffers(xw.dpy, xw.win); /* Timing */