added panel pool + introduced output buffer to make multible buffered panels easier

This commit is contained in:
vurtun 2015-05-07 13:41:55 +02:00
parent 027ae073cb
commit 902bdc8e11
4 changed files with 574 additions and 206 deletions

View File

@ -287,12 +287,9 @@ font_draw_text(const struct font *font, float x, float y, float h,
g = (g->code == 0) ? font->fallback : g;
/* th = font->ascent + font->descent;*/
/*ty = (int)y + ((int)h / 2) - (th / 2) + font->ascent;*/
gw = (float)g->width * font->scale;
gh = (float)g->height * font->scale;
gx = x + g->xoff * font->scale;
/*gy = y + (font->height - g->yoff * font->scale);*/
gy = y + (h / 2) - (g->yoff * font->scale);
char_width = g->xadvance * font->scale;

View File

@ -320,7 +320,7 @@ surface_del(XSurface *surf)
}
static void
draw(XSurface *surf, struct gui_command_list *list)
execute(XSurface *surf, const struct gui_command_list *list)
{
const struct gui_command *cmd;
if (!list->count) return;
@ -361,6 +361,18 @@ draw(XSurface *surf, struct gui_command_list *list)
}
}
static void
draw(XSurface *surf, struct gui_output *out)
{
const struct gui_command_list *iter;
if (!out->size) return;
iter = out->begin;
while (iter) {
execute(surf, iter);
iter = iter->next;
}
}
static void
key(struct XWindow *xw, struct gui_input *in, XEvent *evt, gui_bool down)
{
@ -475,13 +487,12 @@ main(int argc, char *argv[])
struct gui_input in;
struct gui_font font;
struct gui_memory memory;
struct gui_memory_status status;
struct gui_config config;
struct gui_canvas canvas;
struct gui_command_buffer buffer;
struct gui_command_list list;
struct gui_output_buffer buffer;
struct gui_output output;
struct gui_panel_layout layout;
struct gui_panel panel;
struct gui_window win;
struct gui_window msg;
/* Window */
UNUSED(argc); UNUSED(argv);
@ -506,20 +517,25 @@ main(int argc, char *argv[])
xw.surf = surface_create(xw.dpy, xw.screen, xw.win, xw.width, xw.height);
xw.font = font_create(xw.dpy, "fixed");
/* GUI */
memset(&in, 0, sizeof in);
memory.memory = calloc(MAX_MEMORY, 1);
memory.size = MAX_MEMORY;
gui_buffer_init_fixed(&buffer, &memory, GUI_BUFFER_CLIPPING);
gui_output_init_fixed(&buffer, &memory);
font.userdata = xw.font;
font.height = (gui_float)xw.font->height;
font.width = font_get_text_width;
gui_default_config(&config);
gui_panel_init(&panel, 50, 50, 420, 300,
gui_panel_init(&win.panel, 50, 50, 420, 300,
GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE|
GUI_PANEL_CLOSEABLE|GUI_PANEL_SCALEABLE|
GUI_PANEL_MINIMIZABLE, &config, &font);
gui_panel_init(&msg.panel, 150, 150, 200, 80,
GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE, &config, &font);
/* Demo */
memset(&demo, 0, sizeof(demo));
@ -546,16 +562,22 @@ main(int argc, char *argv[])
gui_input_end(&in);
/* GUI */
gui_buffer_begin(&canvas, &buffer, xw.width, xw.height);
running = gui_panel_begin(&layout, &panel, "Demo", &canvas, &in);
gui_output_begin(&buffer, xw.width, xw.height);
running = gui_window_begin(&layout, &win, &buffer, "Demo", &in);
demo_panel(&layout, &demo);
gui_panel_end(&layout, &panel);
gui_buffer_end(&list, &buffer, &canvas, &status);
gui_window_end(&layout, &win, &buffer, NULL);
gui_window_begin(&layout, &msg, &buffer, "Error", &in);
gui_panel_row(&layout, 30, 2);
if (gui_panel_button_text(&layout, "ok", GUI_BUTTON_DEFAULT)) break;
if (gui_panel_button_text(&layout, "cancel", GUI_BUTTON_DEFAULT)) break;
gui_window_end(&layout, &msg, &buffer, NULL);
gui_output_end(&output, &buffer, NULL);
/* Draw */
XClearWindow(xw.dpy, xw.win);
surface_clear(xw.surf, 0x00646464);
draw(xw.surf, &list);
draw(xw.surf, &output);
surface_blit(xw.win, xw.surf, xw.width, xw.height);
XFlush(xw.dpy);
@ -575,3 +597,4 @@ main(int argc, char *argv[])
return 0;
}

647
gui.c

File diff suppressed because it is too large Load Diff

81
gui.h
View File

@ -315,6 +315,7 @@ struct gui_command_buffer {
};
struct gui_command_list {
struct gui_command_list *next;
struct gui_command *begin;
struct gui_command *end;
gui_size count;
@ -323,8 +324,8 @@ struct gui_command_list {
enum gui_panel_colors {
GUI_COLOR_TEXT,
GUI_COLOR_PANEL,
GUI_COLOR_HEADER,
GUI_COLOR_BORDER,
GUI_COLOR_TITLEBAR,
GUI_COLOR_BUTTON,
GUI_COLOR_BUTTON_BORDER,
GUI_COLOR_BUTTON_HOVER,
@ -429,6 +430,38 @@ struct gui_panel_stack {
struct gui_panel *end;
};
struct gui_pool_page {
struct gui_pool_page *next;
void *memory;
gui_size capacity;
gui_size count;
};
struct gui_pool {
struct gui_allocator allocator;
struct gui_pool_page base;
struct gui_pool_page *pages;
gui_size page_count;
gui_size page_size;
gui_size panel_size;
gui_size panel_offset;
struct gui_panel *free_list;
};
struct gui_output {
struct gui_command_list *begin;
struct gui_command_list *end;
gui_size size;
};
struct gui_output_buffer {
gui_size width, height;
struct gui_canvas canvas;
struct gui_command_buffer global;
struct gui_command_buffer current;
struct gui_output out;
};
/* Input */
gui_size gui_utf_decode(const gui_char*, gui_long*, gui_size);
gui_size gui_utf_encode(gui_long, gui_char*, gui_size);
@ -447,9 +480,12 @@ void gui_buffer_init_fixed(struct gui_command_buffer*, const struct gui_memory*,
gui_flag clipping);
void gui_buffer_begin(struct gui_canvas *canvas, struct gui_command_buffer *buffer,
gui_size width, gui_size height);
void gui_buffer_lock(struct gui_command_buffer *buffer, struct gui_command_buffer *sub,
gui_flag clipping);
void gui_buffer_unlock(struct gui_command_buffer *buf, struct gui_command_buffer *sub);
void gui_buffer_lock(struct gui_canvas*, struct gui_command_buffer *buffer,
struct gui_command_buffer *sub, gui_flag clipping,
gui_size width, gui_size height);
void gui_buffer_unlock(struct gui_command_list*, struct gui_command_buffer *buf,
struct gui_command_buffer *sub, struct gui_canvas*,
struct gui_memory_status*);
void *gui_buffer_push(struct gui_command_buffer*,
enum gui_command_type, gui_size size);
void gui_buffer_push_scissor(struct gui_command_buffer*, gui_float, gui_float,
@ -518,6 +554,7 @@ gui_bool gui_panel_begin_stacked(struct gui_panel_layout *layout, struct gui_pan
struct gui_panel_stack*, const char *title, const struct gui_canvas*,
const struct gui_input*);
void gui_panel_row(struct gui_panel_layout*, gui_float height, gui_size cols);
void gui_panel_alloc_space(struct gui_rect*, struct gui_panel_layout*);
void gui_panel_seperator(struct gui_panel_layout*, gui_size cols);
void gui_panel_text(struct gui_panel_layout*, const char*, gui_size, enum gui_text_align);
void gui_panel_text_colored(struct gui_panel_layout*, const char*, gui_size, enum gui_text_align,
@ -557,7 +594,6 @@ gui_int gui_panel_graph_ex(struct gui_panel_layout*, enum gui_graph_type, gui_si
gui_float(*get_value)(void*, gui_size), void *userdata);
void gui_panel_table_begin(struct gui_panel_layout*, gui_flags flags,
gui_size row_height, gui_size cols);
void gui_panel_table_label(struct gui_panel_layout*, const char*);
void gui_panel_table_row(struct gui_panel_layout*);
void gui_panel_table_end(struct gui_panel_layout*);
gui_bool gui_panel_tab_begin(struct gui_panel_layout*, struct gui_panel_layout *tab,
@ -572,11 +608,44 @@ gui_float gui_panel_shelf_end(struct gui_panel_layout*, struct gui_panel_layout
void gui_panel_end(struct gui_panel_layout*, struct gui_panel*);
/* Stack */
/* Stack */
void gui_stack_clear(struct gui_panel_stack*);
void gui_stack_push(struct gui_panel_stack*, struct gui_panel*);
void gui_stack_pop(struct gui_panel_stack*, struct gui_panel*);
/* Pool */
void gui_pool_init(struct gui_pool*, const struct gui_allocator*,
gui_size panel_size, gui_size offset, gui_size panels_per_page);
void gui_pool_init_fixed(struct gui_pool*, void *memory, gui_size panel_count,
gui_size panel_size, gui_size offset);
void *gui_pool_alloc(struct gui_pool*);
void gui_pool_free(struct gui_pool*, void*);
void gui_pool_clear(struct gui_pool*);
/* Output */
void gui_output_init(struct gui_output_buffer*, const struct gui_allocator*,
gui_size initial, gui_float grow_factor);
void gui_output_init_fixed(struct gui_output_buffer*, struct gui_memory *memory);
void gui_output_begin(struct gui_output_buffer*, gui_size width, gui_size height);
void gui_output_end(struct gui_output*, struct gui_output_buffer*,
struct gui_memory_status*);
void gui_output_end_ordered(struct gui_output*, struct gui_output_buffer*,
struct gui_panel_stack*, struct gui_memory_status*);
void gui_output_clear(struct gui_output_buffer*);
/* Window */
gui_bool gui_window_begin(struct gui_panel_layout *layout, struct gui_window *win,
struct gui_output_buffer *buffer, const char*, const struct gui_input *in);
gui_bool gui_window_begin_stacked(struct gui_panel_layout*, struct gui_window*,
struct gui_output_buffer*, struct gui_panel_stack*, const char*,
const struct gui_input*);
void gui_window_end(struct gui_panel_layout*, struct gui_window*,
struct gui_output_buffer*, struct gui_memory_status*);
#ifdef __cplusplus
}
#endif