lean & mean demo

This commit is contained in:
vurtun 2015-04-02 20:09:03 +02:00
parent 260b7da2cc
commit 226dbee7cd
3 changed files with 30 additions and 149 deletions

View File

@ -37,29 +37,6 @@ static void brelease(struct gui_input*, SDL_Event*);
static void bmotion(struct gui_input*, SDL_Event*);
static GLuint ldbmp(gui_byte*, uint32_t*, uint32_t*);
/* types */
struct widget {
gui_char cmd_buf[MAX_BUFFER];
gui_size cmd_len;
gui_bool cmd_act;
gui_bool check;
gui_int option;
gui_float slider;
gui_size prog;
gui_int spinner;
gui_bool spin_act;
gui_size item_cur;
gui_float list_off;
gui_bool list_sel[5];
};
struct layout {
gui_size current_tab;
gui_float offset;
gui_float group_offset;
gui_bool minimized;
};
/* gobals */
static void
die(const char *fmt, ...)
@ -362,92 +339,6 @@ draw(int width, int height, struct gui_draw_call_list **list, gui_size count)
glPopAttrib();
}
static void
widget_panel(struct gui_context *ctx, struct gui_panel *panel, struct widget *demo)
{
const gui_float values[] = {8.0f, 15.0f, 20.0f, 12.0f, 30.0f};
const char *items[] = {"Fist", "Pistol", "Shotgun", "Railgun", "BFG"};
gui_begin_panel(ctx, panel, "Widgets",
GUI_PANEL_HEADER|GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|GUI_PANEL_BORDER|
GUI_PANEL_MOVEABLE|GUI_PANEL_SCROLLBAR|GUI_PANEL_SCALEABLE);
gui_panel_layout(panel, 30, 1);
if (gui_panel_button_text(panel, "button", GUI_BUTTON_SWITCH))
fprintf(stdout, "button pressed!\n");
demo->check = gui_panel_check(panel, "advanced", demo->check);
gui_panel_layout(panel, 30, 2);
if (gui_panel_option(panel, "easy", demo->option == 0)) demo->option = 0;
if (gui_panel_option(panel, "hard", demo->option == 1)) demo->option = 1;
gui_panel_layout(panel, 30, 1);
demo->slider = gui_panel_slider(panel, 0, demo->slider, 10, 1.0f, GUI_HORIZONTAL);
demo->prog = gui_panel_progress(panel, demo->prog, 100, gui_true, GUI_HORIZONTAL);
gui_panel_shell(panel, demo->cmd_buf, &demo->cmd_len, MAX_BUFFER, &demo->cmd_act);
demo->spin_act = gui_panel_spinner(panel, 0, &demo->spinner, 1024, 10, demo->spin_act);
demo->item_cur = gui_panel_selector(panel, items, LEN(items), demo->item_cur);
gui_panel_layout(panel, 100, 1);
gui_panel_plot(panel, values, LEN(values));
gui_panel_histo(panel, values, LEN(values));
gui_panel_layout(panel, 150, 1);
demo->list_off = gui_panel_list(panel, demo->list_sel, items, LEN(items), demo->list_off, 30);
gui_end_panel(ctx, panel, NULL);
}
static void
layout_panel(struct gui_context *ctx, struct gui_panel *panel, struct layout *l)
{
struct gui_panel tab;
enum {MOUSE, KEYBOARD, GAMEPAD};
const char *shelfs[] = {"Mouse", "Keyboard", "Gamepad"};
gui_begin_panel(ctx, panel, "Layouts",
GUI_PANEL_HEADER|GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|
GUI_PANEL_MOVEABLE|GUI_PANEL_BORDER);
/* Tabs */
gui_panel_layout(panel, 100, 1);
l->minimized = gui_panel_tab_begin(panel, &tab, "Options", l->minimized);
gui_panel_layout(&tab, 30, 1);
if (gui_panel_button_text(&tab, "button", GUI_BUTTON_SWITCH))
fprintf(stdout, "tab button pressed!\n");
gui_panel_tab_end(panel, &tab);
/* Shelf */
gui_panel_layout(panel, 200, 2);
l->current_tab = gui_panel_shelf_begin(panel, &tab, shelfs, LEN(shelfs), l->current_tab);
gui_panel_layout(&tab, 30, 1);
if (l->current_tab == MOUSE) {
if (gui_panel_button_text(&tab, "button0", GUI_BUTTON_SWITCH))
fprintf(stdout, "shelf button0 pressed!\n");
} else if (l->current_tab == KEYBOARD) {
if (gui_panel_button_text(&tab, "button1", GUI_BUTTON_SWITCH))
fprintf(stdout, "shelf button1 pressed!\n");
} else {
if (gui_panel_button_text(&tab, "button2", GUI_BUTTON_SWITCH))
fprintf(stdout, "shelf button2 pressed!\n");
}
gui_panel_shelf_end(panel, &tab);
/* Group */
gui_panel_group_begin(panel, &tab, "Options", l->group_offset);
gui_panel_layout(&tab, 30, 1);
if (gui_panel_button_text(&tab, "button", GUI_BUTTON_SWITCH))
fprintf(stdout, "group button pressed!\n");
l->group_offset = gui_panel_group_end(panel, &tab);
gui_end_panel(ctx, panel, NULL);
}
static gui_bool
msgbox_panel(struct gui_context *ctx, struct gui_panel *panel)
{
gui_begin_panel(ctx, panel, "Error",
GUI_PANEL_HEADER|GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE);
gui_panel_layout(panel, 30, 2);
if (gui_panel_button_text(panel, "ok", GUI_BUTTON_SWITCH)) return gui_false;
if (gui_panel_button_text(panel, "cancel", GUI_BUTTON_SWITCH)) return gui_false;
gui_end_panel(ctx, panel, NULL);
return gui_true;
}
int
main(int argc, char *argv[])
{
@ -464,11 +355,6 @@ main(int argc, char *argv[])
struct gui_font *font;
struct gui_context *ctx;
struct gui_panel *panel;
struct gui_panel *subpanel;
struct gui_panel *groups;
struct widget widgets;
struct layout layout;
/* Window */
UNUSED(argc); UNUSED(argv);
@ -489,7 +375,6 @@ main(int argc, char *argv[])
memory.vertex_percentage = 0.80f;
memory.command_percentage = 0.19f;
memory.clip_percentage = 0.01f;
ctx = gui_new(&memory, &input);
font = ldfont("mono.sdf", 16);
gui_default_config(&config);
@ -497,18 +382,7 @@ main(int argc, char *argv[])
config.colors[GUI_COLOR_TEXT].g = 255;
config.colors[GUI_COLOR_TEXT].b = 255;
config.colors[GUI_COLOR_TEXT].a = 255;
panel = gui_panel_new(ctx, 20, 20, 200, 400, &config, font);
subpanel = gui_panel_new(ctx, 250, 20, 200, 200, &config, font);
groups = gui_panel_new(ctx, 230, 150, 550, 400, &config, font);
/* State */
memset(&layout, 0, sizeof layout);
memset(&widgets, 0, sizeof widgets);
widgets.spinner = 100;
widgets.slider = 2.0f;
widgets.prog = 60;
layout.minimized = gui_true;
running = gui_true;
while (running) {
@ -530,9 +404,13 @@ main(int argc, char *argv[])
/* ------------------------- GUI --------------------------*/
gui_begin(ctx, (gui_float)width, (gui_float)height);
widget_panel(ctx, panel, &widgets);
layout_panel(ctx, groups, &layout);
if (!msgbox_panel(ctx, subpanel)) break;
running = gui_begin_panel(ctx, panel, "GUI",
GUI_PANEL_HEADER|GUI_PANEL_CLOSEABLE|GUI_PANEL_MINIMIZABLE|GUI_PANEL_BORDER|
GUI_PANEL_MOVEABLE|GUI_PANEL_SCROLLBAR|GUI_PANEL_SCALEABLE);
gui_panel_layout(panel, 30, 1);
if (gui_panel_button_text(panel, "button", GUI_BUTTON_SWITCH))
fprintf(stdout, "button pressed!\n");
gui_end_panel(ctx, panel, NULL);
gui_end(ctx, &output, NULL);
/* ---------------------------------------------------------*/

20
gui.c
View File

@ -44,7 +44,7 @@ struct gui_context_panel {
struct gui_context {
gui_float width, height;
struct gui_draw_buffer global_buffer;
struct gui_draw_buffer buffer;
struct gui_draw_buffer current_buffer;
const struct gui_input *input;
struct gui_context_panel *active;
struct gui_context_panel *panel_pool;
@ -2516,7 +2516,7 @@ gui_panel_group_end(struct gui_panel *panel, struct gui_panel* group)
gui_size
gui_panel_shelf_begin(struct gui_panel *panel, struct gui_panel *shelf,
const char *tabs[], gui_size tab_count, gui_size current)
const char *tabs[], gui_size tab_count, gui_size current, gui_float offset)
{
gui_size i;
gui_flags flags;
@ -2573,19 +2573,21 @@ gui_panel_shelf_begin(struct gui_panel *panel, struct gui_panel *shelf,
bounds.h -= header_h;
gui_panel_init(shelf, panel->config, panel->font);
flags = GUI_PANEL_BORDER|GUI_PANEL_SCROLLBAR|GUI_PANEL_TAB;
panel->offset = offset;
gui_panel_begin(shelf, panel->out, panel->in, NULL,
bounds.x, bounds.y, bounds.w, bounds.h, flags);
return current;
}
void
gui_float
gui_panel_shelf_end(struct gui_panel *panel, struct gui_panel *tab)
{
assert(panel);
assert(tab);
if (!panel || !tab) return;
if (panel->minimized || (panel->flags & GUI_PANEL_HIDDEN)) return;
if (!panel || !tab) return 0;
if (panel->minimized || (panel->flags & GUI_PANEL_HIDDEN)) return 0;
gui_panel_end(tab);
return tab->offset;
}
void
@ -2852,7 +2854,7 @@ gui_begin_panel(struct gui_context *ctx, struct gui_panel *panel,
}
global = &ctx->global_buffer;
out = &ctx->buffer;
out = &ctx->current_buffer;
out->vertex_size = 0;
out->vertex_needed = 0;
out->command_size = 0;
@ -2883,9 +2885,9 @@ gui_end_panel(struct gui_context *ctx, struct gui_panel *panel,
cpanel = (struct gui_context_panel*)panel;
gui_panel_end(panel);
global = &ctx->global_buffer;
global->vertex_size += ctx->buffer.vertex_size;
global->command_size += ctx->buffer.command_size;
gui_output_end(&ctx->buffer, &cpanel->list, status);
global->vertex_size += ctx->current_buffer.vertex_size;
global->command_size += ctx->current_buffer.command_size;
gui_output_end(&ctx->current_buffer, &cpanel->list, status);
}
void

23
gui.h
View File

@ -348,17 +348,17 @@ struct gui_panel {
};
/* Input */
void gui_input_begin(struct gui_input *in);
void gui_input_motion(struct gui_input *in, gui_int x, gui_int y);
void gui_input_key(struct gui_input *in, enum gui_keys key, gui_int down);
void gui_input_button(struct gui_input *in, gui_int x, gui_int y, gui_bool down);
void gui_input_char(struct gui_input *in, const gui_glyph glyph);
void gui_input_end(struct gui_input *in);
void gui_input_begin(struct gui_input*);
void gui_input_motion(struct gui_input*, gui_int x, gui_int y);
void gui_input_key(struct gui_input*, enum gui_keys, gui_int down);
void gui_input_button(struct gui_input*, gui_int x, gui_int y, gui_bool down);
void gui_input_char(struct gui_input*, const gui_glyph);
void gui_input_end(struct gui_input*);
/* Output */
void gui_output_begin(struct gui_draw_buffer *buf, const struct gui_memory *memory);
void gui_output_end(struct gui_draw_buffer *buf, struct gui_draw_call_list *calls,
struct gui_memory_status* status);
void gui_output_begin(struct gui_draw_buffer*, const struct gui_memory*);
void gui_output_end(struct gui_draw_buffer*, struct gui_draw_call_list*,
struct gui_memory_status*);
/* Widgets */
void gui_widget_text(struct gui_draw_buffer*, const struct gui_text*,
@ -438,8 +438,9 @@ void gui_panel_group_begin(struct gui_panel*, struct gui_panel* tab,
const char *title, gui_float offset);
gui_float gui_panel_group_end(struct gui_panel*, struct gui_panel* tab);
gui_size gui_panel_shelf_begin(struct gui_panel*, struct gui_panel *shelf,
const char *tabs[], gui_size tab_count, gui_size current);
void gui_panel_shelf_end(struct gui_panel *panel, struct gui_panel *tab);
const char *tabs[], gui_size tab_count, gui_size current,
gui_float offset);
gui_float gui_panel_shelf_end(struct gui_panel *panel, struct gui_panel *tab);
void gui_panel_end(struct gui_panel*);
/* Context */