fixed some deeper bugs & changed prefix

This commit is contained in:
vurtun 2015-09-17 23:09:49 +02:00
parent 514dfc2f6b
commit 2186694b04
8 changed files with 10061 additions and 10123 deletions

View File

@ -21,23 +21,22 @@ render backends it only focuses on the actual GUI.
- UTF-8 support
- Optional vertex buffer output and font handling
The library is self-contained with four different files that only have to be
copied and compiled into your application. Files gui.c and gui.h make up
The library is self-contained within four different files that only have to be
copied and compiled into your application. Files zahnrad.c and zahnrad.h make up
the core of the library, while stb_rect_pack.h and stb_truetype.h are
for a optional font handling implementation and can be removed if not needed.
- gui.c
- gui.h
- zahnrad.c
- zahnrad.h
- stb_rect_pack.h
- stb_truetype.h
There are no dependencies or particular building process required. You just have
to compile the .c file and and #include gui.h into your project. To actually
There are no dependencies or a particular building process required. You just have
to compile the .c file and #include zahnrad.h into your project. To actually
run you have to provide the input state, configuration style and memory
for draw commands to the library. After the GUI was executed all draw commands
have to be either executed or optionally converted into a vertex buffer to
draw the GUI.
## Gallery
![gui
demo](https://cloud.githubusercontent.com/assets/8057201/9937241/24f55e7e-5d60-11e5-9957-c010cf763f15.png)
@ -49,56 +48,56 @@ nodedit](https://cloud.githubusercontent.com/assets/8057201/9937243/24f6a5ea-5d6
## Example
```c
/* setup configuration */
struct gui_style style;
struct gui_user_font font = {...};
gui_style_default(&style, GUI_DEFAULT_ALL, &font);
struct zr_style style;
struct zr_user_font font = {...};
zr_style_default(&style, ZR_DEFAULT_ALL, &font);
/* allocate memory to hold draw commands */
struct gui_command_queue queue;
gui_command_queue_init_fixed(&queue, malloc(MEMORY_SIZE), MEMORY_SIZE);
struct zr_command_queue queue;
zr_command_queue_init_fixed(&queue, malloc(MEMORY_SIZE), MEMORY_SIZE);
/* initialize window */
struct gui_window window;
gui_window_init(&window, 50, 50, 220, 180,
GUI_PANEL_BORDER|GUI_PANEL_MOVEABLE|GUI_PANEL_SCALEABLE,
struct zr_window window;
zr_window_init(&window, 50, 50, 220, 180,
ZR_WINDOW_BORDER|ZR_WINDOW_MOVEABLE|ZR_WINDOW_SCALEABLE,
&queue, &style, &input);
/* setup widget data */
enum {EASY, HARD};
gui_size option = EASY;
gui_size item = 0;
gui_state active = 0;
zr_size option = EASY;
zr_size item = 0;
zr_state active = 0;
struct gui_input input = {0};
struct zr_input input = {0};
while (1) {
gui_input_begin(&input);
zr_input_begin(&input);
/* record input */
gui_input_end(&input);
zr_input_end(&input);
/* GUI */
struct gui_context context;
gui_begin(&context, &window);
struct zr_context context;
zr_begin(&context, &window);
{
const char *items[] = {"Fist", "Pistol", "Railgun", "BFG"};
gui_header(&context, "Show", GUI_CLOSEABLE, 0, GUI_HEADER_LEFT);
gui_layout_row_static(&context, 30, 80, 1);
if (gui_button_text(&context, "button", GUI_BUTTON_DEFAULT)) {
zr_header(&context, "Show", ZR_CLOSEABLE, 0, ZR_HEADER_LEFT);
zr_layout_row_static(&context, 30, 80, 1);
if (zr_button_text(&context, "button", ZR_BUTTON_DEFAULT)) {
/* event handling */
}
gui_layout_row_dynamic(&context, 30, 2);
if (gui_option(&context, "easy", option == EASY)) option = EASY;
if (gui_option(&context, "hard", option == HARD)) option = HARD;
gui_label(&context, "Weapon:", GUI_TEXT_LEFT);
gui_combo(&context, items, LEN(items), &item, 20, &active);
zr_layout_row_dynamic(&context, 30, 2);
if (zr_option(&context, "easy", option == EASY)) option = EASY;
if (zr_option(&context, "hard", option == HARD)) option = HARD;
zr_label(&context, "Weapon:", ZR_TEXT_LEFT);
zr_combo(&context, items, LEN(items), &item, 20, &active);
}
gui_end(&context, &window);
zr_end(&context, &window);
/* draw */
const struct gui_command *cmd;
gui_foreach_command(cmd, &queue) {
const struct zr_command *cmd;
zr_foreach_command(cmd, &queue) {
/* execute draw call command */
}
gui_command_queue_clear(&queue);
zr_command_queue_clear(&queue);
}
```
![gui
@ -146,7 +145,7 @@ This Project uses ANSI C which does not have the header file `<stdint.h>`
and therefore does not provide the fixed sized types that I need. Therefore
I defined my own types which need to be set to the correct size for each
platform. But if your development environment provides the header file you can define
`GUI_USE_FIXED_SIZE_TYPES` to directly use the correct types.
`ZR_USE_FIXED_SIZE_TYPES` to directly use the correct types.
## References
- [stb_rect_pack.h and stb_truetype.h by Sean Barret (public domain)](https:://github.com/nothings/stb/)

View File

@ -118,7 +118,7 @@ static const char *colors[] = {
* TREE WIDGET
* ----------------------------------------------------------------- */
struct tree_node {
gui_state state;
zr_state state;
const char *name;
struct tree_node *parent;
struct tree_node *children[8];
@ -136,7 +136,7 @@ static void
tree_init(struct test_tree *tree)
{
/* this is just test data */
tree->root.state = GUI_NODE_ACTIVE;
tree->root.state = ZR_NODE_ACTIVE;
tree->root.name = "Primitives";
tree->root.parent = NULL;
tree->root.count = 2;
@ -160,7 +160,7 @@ tree_init(struct test_tree *tree)
tree->nodes[2].parent = &tree->nodes[0];
tree->nodes[2].count = 0;
tree->nodes[4].state = GUI_NODE_ACTIVE;
tree->nodes[4].state = ZR_NODE_ACTIVE;
tree->nodes[4].name = "Cylinders";
tree->nodes[4].parent = &tree->root;
tree->nodes[4].count = 2;
@ -225,33 +225,33 @@ tree_pop_node(struct test_tree *tree)
}
static int
upload_tree(struct test_tree *base, struct gui_tree *tree, struct tree_node *node)
upload_tree(struct test_tree *base, struct zr_tree *tree, struct tree_node *node)
{
int i = 0, n = 0;
enum gui_tree_node_operation op;
enum zr_tree_node_operation op;
if (node->count) {
i = 0;
op = gui_tree_begin_node(tree, node->name, &node->state);
op = zr_tree_begin_node(tree, node->name, &node->state);
while (i < node->count)
i += upload_tree(base, tree, node->children[i]);
gui_tree_end_node(tree);
zr_tree_end_node(tree);
}
else op = gui_tree_leaf(tree, node->name, &node->state);
else op = zr_tree_leaf(tree, node->name, &node->state);
switch (op) {
case GUI_NODE_NOP: break;
case GUI_NODE_CUT:
case ZR_NODE_NOP: break;
case ZR_NODE_CUT:
tree_remove_node(node->parent, node);
tree_push_node(base, node);
return 0;
case GUI_NODE_DELETE:
case ZR_NODE_DELETE:
tree_remove_node(node->parent, node); break;
return 0;
case GUI_NODE_PASTE:
case ZR_NODE_PASTE:
i = 0; n = base->count;
while (i++ < n)
tree_add_node(node, tree_pop_node(base));
case GUI_NODE_CLONE:
case ZR_NODE_CLONE:
default:break;
}
return 1;
@ -261,54 +261,54 @@ upload_tree(struct test_tree *base, struct gui_tree *tree, struct tree_node *nod
* COLOR PICKER POPUP
* ----------------------------------------------------------------- */
struct color_picker {
gui_state active;
struct gui_color color;
gui_state r, g, b, a;
gui_size index;
zr_state active;
struct zr_color color;
zr_state r, g, b, a;
zr_size index;
};
static gui_bool
color_picker(struct gui_context *panel, struct color_picker* control,
const char *name, struct gui_color *color)
static zr_bool
color_picker(struct zr_context *panel, struct color_picker* control,
const char *name, struct zr_color *color)
{
int i;
gui_byte *iter;
gui_bool ret = gui_true;
struct gui_context popup;
gui_popup_begin(panel, &popup, GUI_POPUP_STATIC,0, gui_rect(10, 100, 280, 280), gui_vec2(0,0));
zr_byte *iter;
zr_bool ret = zr_true;
struct zr_context popup;
zr_popup_begin(panel, &popup, ZR_POPUP_STATIC,0, zr_rect(10, 100, 280, 280), zr_vec2(0,0));
{
if (gui_header(&popup, "Color", GUI_CLOSEABLE, GUI_CLOSEABLE, GUI_HEADER_LEFT)) {
gui_popup_close(&popup);
return gui_false;
if (zr_header(&popup, "Color", ZR_CLOSEABLE, ZR_CLOSEABLE, ZR_HEADER_LEFT)) {
zr_popup_close(&popup);
return zr_false;
}
gui_layout_row_dynamic(&popup, 30, 2);
gui_label(&popup, name, GUI_TEXT_LEFT);
gui_button_color(&popup, control->color, GUI_BUTTON_DEFAULT);
zr_layout_row_dynamic(&popup, 30, 2);
zr_label(&popup, name, ZR_TEXT_LEFT);
zr_button_color(&popup, control->color, ZR_BUTTON_DEFAULT);
iter = &control->color.r;
gui_layout_row_dynamic(&popup, 30, 2);
zr_layout_row_dynamic(&popup, 30, 2);
for (i = 0; i < 4; ++i, iter++) {
gui_float t;
*iter = (gui_byte)gui_spinner(&popup, 0, *iter, 255, 1, NULL);
zr_float t;
*iter = (zr_byte)zr_spinner(&popup, 0, *iter, 255, 1, NULL);
t = *iter;
t = gui_slider(&popup, 0, t, 255, 10);
*iter = (gui_byte)t;
t = zr_slider(&popup, 0, t, 255, 10);
*iter = (zr_byte)t;
}
gui_layout_row_dynamic(&popup, 30, 4);
gui_spacing(&popup, 1);
if (gui_button_text(&popup, "ok", GUI_BUTTON_DEFAULT)) {
gui_popup_close(&popup);
zr_layout_row_dynamic(&popup, 30, 4);
zr_spacing(&popup, 1);
if (zr_button_text(&popup, "ok", ZR_BUTTON_DEFAULT)) {
zr_popup_close(&popup);
*color = control->color;
ret = gui_false;
ret = zr_false;
}
if (gui_button_text(&popup, "cancel", GUI_BUTTON_DEFAULT)) {
gui_popup_close(&popup);
ret = gui_false;
if (zr_button_text(&popup, "cancel", ZR_BUTTON_DEFAULT)) {
zr_popup_close(&popup);
ret = zr_false;
}
}
gui_popup_end(panel, &popup);
control->active = (gui_state)ret;
zr_popup_end(panel, &popup);
control->active = (zr_state)ret;
return ret;
}
@ -316,14 +316,14 @@ color_picker(struct gui_context *panel, struct color_picker* control,
* LABEL
* ----------------------------------------------------------------- */
static void
gui_labelf(struct gui_context *panel, enum gui_text_align align, const gui_char *fmt, ...)
zr_labelf(struct zr_context *panel, enum zr_text_align align, const zr_char *fmt, ...)
{
gui_char buffer[1024];
zr_char buffer[1024];
va_list args;
va_start(args, fmt);
vsnprintf(buffer, sizeof(buffer), fmt, args);
buffer[1023] = 0;
gui_label(panel, buffer, align);
zr_label(panel, buffer, align);
va_end(args);
}
@ -331,100 +331,100 @@ gui_labelf(struct gui_context *panel, enum gui_text_align align, const gui_char
* COMBOBOXES
* ----------------------------------------------------------------- */
struct combobox {
gui_size selected;
gui_state active;
zr_size selected;
zr_state active;
};
struct check_combo_box {
gui_bool values[4];
gui_state active;
zr_bool values[4];
zr_state active;
};
struct prog_combo_box {
gui_state active;
zr_state active;
};
struct color_combo_box {
gui_state active;
struct gui_color color;
zr_state active;
struct zr_color color;
};
static void
combo_box(struct gui_context *panel, struct combobox *combo,
const char**values, gui_size count)
combo_box(struct zr_context *panel, struct combobox *combo,
const char**values, zr_size count)
{
gui_combo(panel, values, count, &combo->selected, 20, &combo->active);
zr_combo(panel, values, count, &combo->selected, 20, &combo->active);
}
static void
prog_combo_box(struct gui_context *panel, gui_size *values, gui_size count,
prog_combo_box(struct zr_context *panel, zr_size *values, zr_size count,
struct prog_combo_box *demo)
{
gui_size i = 0;
gui_int sum = 0;
gui_char buffer[64];
struct gui_context combo;
zr_size i = 0;
zr_int sum = 0;
zr_char buffer[64];
struct zr_context combo;
memset(&combo, 0, sizeof(combo));
for (i = 0; i < count; ++i)
sum += (gui_int)values[i];
sum += (zr_int)values[i];
sprintf(buffer, "%d", sum);
gui_combo_begin(panel, &combo, buffer, &demo->active);
zr_combo_begin(panel, &combo, buffer, &demo->active);
{
gui_layout_row_dynamic(&combo, 30, 1);
zr_layout_row_dynamic(&combo, 30, 1);
for (i = 0; i < count; ++i)
values[i] = gui_progress(&combo, values[i], 100, gui_true);
values[i] = zr_progress(&combo, values[i], 100, zr_true);
}
gui_combo_end(panel, &combo);
zr_combo_end(panel, &combo);
}
static void
color_combo_box(struct gui_context *panel, struct color_combo_box *demo)
color_combo_box(struct zr_context *panel, struct color_combo_box *demo)
{
/* color slider progressbar */
gui_char buffer[32];
struct gui_context combo;
zr_char buffer[32];
struct zr_context combo;
memset(&combo, 0, sizeof(combo));
sprintf(buffer, "#%02x%02x%02x%02x", demo->color.r, demo->color.g,
demo->color.b, demo->color.a);
gui_combo_begin(panel, &combo, buffer, &demo->active);
zr_combo_begin(panel, &combo, buffer, &demo->active);
{
int i;
const char *color_names[] = {"R:", "G:", "B:", "A:"};
gui_float ratios[] = {0.15f, 0.85f};
gui_byte *iter = &demo->color.r;
gui_layout_row(&combo, GUI_DYNAMIC, 30, 2, ratios);
zr_float ratios[] = {0.15f, 0.85f};
zr_byte *iter = &demo->color.r;
zr_layout_row(&combo, ZR_DYNAMIC, 30, 2, ratios);
for (i = 0; i < 4; ++i, iter++) {
gui_float t = *iter;
gui_label(&combo, color_names[i], GUI_TEXT_LEFT);
t = gui_slider(&combo, 0, t, 255, 5);
*iter = (gui_byte)t;
zr_float t = *iter;
zr_label(&combo, color_names[i], ZR_TEXT_LEFT);
t = zr_slider(&combo, 0, t, 255, 5);
*iter = (zr_byte)t;
}
}
gui_combo_end(panel, &combo);
zr_combo_end(panel, &combo);
}
static void
check_combo_box(struct gui_context *panel, gui_bool *values, gui_size count,
check_combo_box(struct zr_context *panel, zr_bool *values, zr_size count,
struct check_combo_box *demo)
{
/* checkbox combobox */
gui_int sum = 0;
gui_size i = 0;
gui_char buffer[64];
struct gui_context combo;
zr_int sum = 0;
zr_size i = 0;
zr_char buffer[64];
struct zr_context combo;
memset(&combo, 0, sizeof(combo));
for (i = 0; i < count; ++i)
sum += (gui_int)values[i];
sum += (zr_int)values[i];
sprintf(buffer, "%d", sum);
gui_combo_begin(panel, &combo, buffer, &demo->active);
zr_combo_begin(panel, &combo, buffer, &demo->active);
{
gui_layout_row_dynamic(&combo, 30, 1);
zr_layout_row_dynamic(&combo, 30, 1);
for (i = 0; i < count; ++i)
values[i] = gui_check(&combo, weapons[i], values[i]);
values[i] = zr_check(&combo, weapons[i], values[i]);
}
gui_combo_end(panel, &combo);
zr_combo_end(panel, &combo);
}
/* =================================================================
@ -434,8 +434,8 @@ check_combo_box(struct gui_context *panel, gui_bool *values, gui_size count,
* =================================================================
*/
struct state {
gui_char edit_buffer[MAX_BUFFER];
struct gui_edit_box edit;
zr_char edit_buffer[MAX_BUFFER];
struct zr_edit_box edit;
struct color_picker picker;
struct check_combo_box checkcom;
struct prog_combo_box progcom;
@ -444,140 +444,140 @@ struct state {
struct test_tree test;
/* widgets state */
gui_bool list[4];
gui_size prog_values[4];
gui_bool check_values[WEAPON_MAX];
gui_bool scaleable;
gui_bool checkbox;
gui_float slider;
gui_size progressbar;
gui_int spinner;
gui_state spinner_active;
gui_size item_current;
gui_size shelf_selection;
gui_bool toggle;
gui_int option;
gui_state popup;
gui_size cur;
gui_size op;
zr_bool list[4];
zr_size prog_values[4];
zr_bool check_values[WEAPON_MAX];
zr_bool scaleable;
zr_bool checkbox;
zr_float slider;
zr_size progressbar;
zr_int spinner;
zr_state spinner_active;
zr_size item_current;
zr_size shelf_selection;
zr_bool toggle;
zr_int option;
zr_state popup;
zr_size cur;
zr_size op;
/* subpanels */
struct gui_vec2 shelf;
struct gui_vec2 table;
struct gui_vec2 tree;
struct gui_vec2 menu;
struct zr_vec2 shelf;
struct zr_vec2 table;
struct zr_vec2 tree;
struct zr_vec2 menu;
/* open/close state */
gui_state file_open;
gui_state edit_open;
gui_state config_tab;
gui_state widget_tab;
gui_state combo_tab;
gui_state style_tab;
gui_state round_tab;
gui_state color_tab;
gui_state flag_tab;
zr_state file_open;
zr_state edit_open;
zr_state config_tab;
zr_state widget_tab;
zr_state combo_tab;
zr_state style_tab;
zr_state round_tab;
zr_state color_tab;
zr_state flag_tab;
};
struct demo_gui {
gui_bool running;
struct gui_buffer memory;
struct gui_input input;
struct gui_command_queue queue;
struct gui_style config;
struct gui_user_font font;
struct gui_window panel;
struct gui_window sub;
zr_bool running;
struct zr_buffer memory;
struct zr_input input;
struct zr_command_queue queue;
struct zr_style config;
struct zr_user_font font;
struct zr_window panel;
struct zr_window sub;
struct state state;
gui_size w, h;
zr_size w, h;
};
/* -----------------------------------------------------------------
* WIDGETS
* ----------------------------------------------------------------- */
static void
widget_panel(struct gui_context *panel, struct state *demo)
widget_panel(struct zr_context *panel, struct state *demo)
{
/* Labels */
gui_layout_row_dynamic(panel, 30, 1);
demo->scaleable = gui_check(panel, "Scaleable Layout", demo->scaleable);
zr_layout_row_dynamic(panel, 30, 1);
demo->scaleable = zr_check(panel, "Scaleable Layout", demo->scaleable);
if (!demo->scaleable)
gui_layout_row_static(panel, 30, 150, 1);
gui_label(panel, "text left", GUI_TEXT_LEFT);
gui_label(panel, "text center", GUI_TEXT_CENTERED);
gui_label(panel, "text right", GUI_TEXT_RIGHT);
zr_layout_row_static(panel, 30, 150, 1);
zr_label(panel, "text left", ZR_TEXT_LEFT);
zr_label(panel, "text center", ZR_TEXT_CENTERED);
zr_label(panel, "text right", ZR_TEXT_RIGHT);
/* Buttons */
if (gui_button_text(panel, "button", GUI_BUTTON_DEFAULT))
demo->popup = gui_true;
if (gui_button_text_symbol(panel, GUI_SYMBOL_TRIANGLE_RIGHT, "next", GUI_TEXT_LEFT, GUI_BUTTON_DEFAULT))
if (zr_button_text(panel, "button", ZR_BUTTON_DEFAULT))
demo->popup = zr_true;
if (zr_button_text_symbol(panel, ZR_SYMBOL_TRIANGLE_RIGHT, "next", ZR_TEXT_LEFT, ZR_BUTTON_DEFAULT))
fprintf(stdout, "right triangle button pressed!\n");
if (gui_button_text_symbol(panel,GUI_SYMBOL_TRIANGLE_LEFT,"previous",GUI_TEXT_RIGHT,GUI_BUTTON_DEFAULT))
if (zr_button_text_symbol(panel,ZR_SYMBOL_TRIANGLE_LEFT,"previous",ZR_TEXT_RIGHT,ZR_BUTTON_DEFAULT))
fprintf(stdout, "left triangle button pressed!\n");
/* Checkbox + Radio buttons */
demo->checkbox = gui_check(panel, "checkbox", demo->checkbox);
demo->checkbox = zr_check(panel, "checkbox", demo->checkbox);
if (!demo->scaleable)
gui_layout_row_static(panel, 30, 75, 2);
else gui_layout_row_dynamic(panel, 30, 2);
if (gui_option(panel, "option 0", demo->option == 0))
zr_layout_row_static(panel, 30, 75, 2);
else zr_layout_row_dynamic(panel, 30, 2);
if (zr_option(panel, "option 0", demo->option == 0))
demo->option = 0;
if (gui_option(panel, "option 1", demo->option == 1))
if (zr_option(panel, "option 1", demo->option == 1))
demo->option = 1;
{
/* custom row layout by array */
const gui_float ratio[] = {0.8f, 0.2f};
const gui_float pixel[] = {150.0f, 30.0f};
enum gui_layout_format fmt = (demo->scaleable) ? GUI_DYNAMIC : GUI_STATIC;
gui_layout_row(panel, fmt, 30, 2, (fmt == GUI_DYNAMIC) ? ratio: pixel);
demo->slider = gui_slider(panel, 0, demo->slider, 10, 1.0f);
gui_labelf(panel, GUI_TEXT_LEFT, "%.2f", demo->slider);
demo->progressbar = gui_progress(panel, demo->progressbar, 100, gui_true);
gui_labelf(panel, GUI_TEXT_LEFT, "%lu", demo->progressbar);
const zr_float ratio[] = {0.8f, 0.2f};
const zr_float pixel[] = {150.0f, 30.0f};
enum zr_layout_format fmt = (demo->scaleable) ? ZR_DYNAMIC : ZR_STATIC;
zr_layout_row(panel, fmt, 30, 2, (fmt == ZR_DYNAMIC) ? ratio: pixel);
demo->slider = zr_slider(panel, 0, demo->slider, 10, 1.0f);
zr_labelf(panel, ZR_TEXT_LEFT, "%.2f", demo->slider);
demo->progressbar = zr_progress(panel, demo->progressbar, 100, zr_true);
zr_labelf(panel, ZR_TEXT_LEFT, "%lu", demo->progressbar);
}
{
/* tiled widgets layout */
struct gui_tiled_layout tiled;
enum gui_layout_format fmt = (demo->scaleable) ? GUI_DYNAMIC : GUI_STATIC;
struct zr_tiled_layout tiled;
enum zr_layout_format fmt = (demo->scaleable) ? ZR_DYNAMIC : ZR_STATIC;
/* setup tiled layout */
gui_tiled_begin(&tiled, fmt, 250, 150);
zr_tiled_begin(&tiled, fmt, 250, 150);
if (!demo->scaleable) {
gui_tiled_slot(&tiled, GUI_SLOT_LEFT, 100, GUI_SLOT_VERTICAL, 4);
gui_tiled_slot(&tiled, GUI_SLOT_RIGHT, 150, GUI_SLOT_VERTICAL, 4);
zr_tiled_slot(&tiled, ZR_SLOT_LEFT, 100, ZR_SLOT_VERTICAL, 4);
zr_tiled_slot(&tiled, ZR_SLOT_RIGHT, 150, ZR_SLOT_VERTICAL, 4);
} else {
gui_tiled_slot(&tiled, GUI_SLOT_LEFT, 0.50, GUI_SLOT_VERTICAL, 4);
gui_tiled_slot(&tiled, GUI_SLOT_RIGHT, 0.50, GUI_SLOT_VERTICAL, 4);
zr_tiled_slot(&tiled, ZR_SLOT_LEFT, 0.50, ZR_SLOT_VERTICAL, 4);
zr_tiled_slot(&tiled, ZR_SLOT_RIGHT, 0.50, ZR_SLOT_VERTICAL, 4);
}
gui_tiled_end(&tiled);
zr_tiled_end(&tiled);
/* setup widgets with tiled layout */
gui_layout_row_tiled_begin(panel, &tiled);
zr_layout_row_tiled_begin(panel, &tiled);
{
gui_uint i = 0;
gui_layout_row_tiled_push(panel, GUI_SLOT_LEFT, 1);
gui_label(panel, "Test0", GUI_TEXT_CENTERED);
gui_layout_row_tiled_push(panel, GUI_SLOT_LEFT, 2);
gui_label(panel, "Test1", GUI_TEXT_CENTERED);
zr_uint i = 0;
zr_layout_row_tiled_push(panel, ZR_SLOT_LEFT, 1);
zr_label(panel, "Test0", ZR_TEXT_CENTERED);
zr_layout_row_tiled_push(panel, ZR_SLOT_LEFT, 2);
zr_label(panel, "Test1", ZR_TEXT_CENTERED);
for (i = 0; i < 4; ++i) {
const char *items[] = {"item0", "item1", "item2", "item3"};
gui_layout_row_tiled_push(panel, GUI_SLOT_RIGHT, i);
demo->list[i] = gui_button_toggle(panel, items[i], demo->list[i]);
zr_layout_row_tiled_push(panel, ZR_SLOT_RIGHT, i);
demo->list[i] = zr_button_toggle(panel, items[i], demo->list[i]);
}
}
gui_layout_row_tiled_end(panel);
zr_layout_row_tiled_end(panel);
}
/* item selection */
if (!demo->scaleable) gui_layout_row_static(panel, 30, 150, 1);
else gui_layout_row_dynamic(panel, 30, 1);
demo->spinner = gui_spinner(panel, 0, demo->spinner, 250, 10, &demo->spinner_active);
if (!demo->scaleable) zr_layout_row_static(panel, 30, 150, 1);
else zr_layout_row_dynamic(panel, 30, 1);
demo->spinner = zr_spinner(panel, 0, demo->spinner, 250, 10, &demo->spinner_active);
/* combo boxes */
if (!demo->scaleable) gui_layout_row_static(panel, 30, 150, 1);
else gui_layout_row_dynamic(panel, 30, 1);
if (!demo->scaleable) zr_layout_row_static(panel, 30, 150, 1);
else zr_layout_row_dynamic(panel, 30, 1);
combo_box(panel, &demo->combo, weapons, LEN(weapons));
prog_combo_box(panel, demo->prog_values, LEN(demo->prog_values), &demo->progcom);
color_combo_box(panel, &demo->colcom);
@ -585,18 +585,18 @@ widget_panel(struct gui_context *panel, struct state *demo)
{
/* custom row layout by im */
enum gui_layout_format fmt = (demo->scaleable) ? GUI_DYNAMIC : GUI_STATIC;
gui_layout_row_begin(panel, fmt, 30, 2);
enum zr_layout_format fmt = (demo->scaleable) ? ZR_DYNAMIC : ZR_STATIC;
zr_layout_row_begin(panel, fmt, 30, 2);
{
gui_layout_row_push(panel,(fmt == GUI_DYNAMIC) ? 0.7f : 100);
gui_editbox(panel, &demo->edit);
gui_layout_row_push(panel, (fmt == GUI_DYNAMIC) ? 0.3f : 80);
if (gui_button_text(panel, "submit", GUI_BUTTON_DEFAULT)) {
gui_edit_box_clear(&demo->edit);
zr_layout_row_push(panel,(fmt == ZR_DYNAMIC) ? 0.7f : 100);
zr_editbox(panel, &demo->edit);
zr_layout_row_push(panel, (fmt == ZR_DYNAMIC) ? 0.3f : 80);
if (zr_button_text(panel, "submit", ZR_BUTTON_DEFAULT)) {
zr_edit_box_clear(&demo->edit);
fprintf(stdout, "command executed!\n");
}
}
gui_layout_row_end(panel);
zr_layout_row_end(panel);
}
}
@ -604,65 +604,65 @@ widget_panel(struct gui_context *panel, struct state *demo)
* STYLE
* ----------------------------------------------------------------- */
static void
update_flags(struct gui_context *panel)
update_flags(struct zr_context *panel)
{
gui_size n = 0;
gui_flags res = 0;
gui_flags i = 0x01;
zr_size n = 0;
zr_flags res = 0;
zr_flags i = 0x01;
const char *options[]={"Hidden","Border","Header Border", "Moveable","Scaleable", "Minimized", "ROM"};
gui_layout_row_dynamic(panel, 30, 2);
zr_layout_row_dynamic(panel, 30, 2);
do {
if (gui_check(panel,options[n++],(panel->flags & i)?gui_true:gui_false))
if (zr_check(panel,options[n++],(panel->flags & i)?zr_true:zr_false))
res |= i;
i = i << 1;
} while (i <= GUI_WINDOW_ROM);
} while (i <= ZR_WINDOW_ROM);
panel->flags = res;
}
static void
properties_tab(struct gui_context *panel, struct gui_style *config)
properties_tab(struct zr_context *panel, struct zr_style *config)
{
int i = 0;
const char *properties[] = {"item spacing:", "item padding:", "panel padding:",
"scaler size:", "scrollbar:"};
gui_layout_row_dynamic(panel, 30, 3);
for (i = 0; i <= GUI_PROPERTY_SCROLLBAR_SIZE; ++i) {
gui_int tx, ty;
gui_label(panel, properties[i], GUI_TEXT_LEFT);
tx = gui_spinner(panel,0,(gui_int)config->properties[i].x, 20, 1, NULL);
ty = gui_spinner(panel,0,(gui_int)config->properties[i].y, 20, 1, NULL);
zr_layout_row_dynamic(panel, 30, 3);
for (i = 0; i <= ZR_PROPERTY_SCROLLBAR_SIZE; ++i) {
zr_int tx, ty;
zr_label(panel, properties[i], ZR_TEXT_LEFT);
tx = zr_spinner(panel,0,(zr_int)config->properties[i].x, 20, 1, NULL);
ty = zr_spinner(panel,0,(zr_int)config->properties[i].y, 20, 1, NULL);
config->properties[i].x = (float)tx;
config->properties[i].y = (float)ty;
}
}
static void
round_tab(struct gui_context *panel, struct gui_style *config)
round_tab(struct zr_context *panel, struct zr_style *config)
{
int i = 0;
const char *rounding[] = {"panel:", "button:", "checkbox:", "progress:", "input: ",
"graph:", "scrollbar:"};
gui_layout_row_dynamic(panel, 30, 2);
for (i = 0; i < GUI_ROUNDING_MAX; ++i) {
gui_int t;
gui_label(panel, rounding[i], GUI_TEXT_LEFT);
t = gui_spinner(panel,0,(gui_int)config->rounding[i], 20, 1, NULL);
zr_layout_row_dynamic(panel, 30, 2);
for (i = 0; i < ZR_ROUNDING_MAX; ++i) {
zr_int t;
zr_label(panel, rounding[i], ZR_TEXT_LEFT);
t = zr_spinner(panel,0,(zr_int)config->rounding[i], 20, 1, NULL);
config->rounding[i] = (float)t;
}
}
static void
color_tab(struct gui_context *panel, struct state *control, struct gui_style *config)
color_tab(struct zr_context *panel, struct state *control, struct zr_style *config)
{
gui_size i = 0;
gui_layout_row_dynamic(panel, 30, 2);
for (i = 0; i < GUI_COLOR_COUNT; ++i) {
struct gui_color c = config->colors[i];
gui_label(panel, colors[i], GUI_TEXT_LEFT);
if (gui_button_color(panel, c, GUI_BUTTON_DEFAULT)) {
control->picker.active = gui_true;
zr_size i = 0;
zr_layout_row_dynamic(panel, 30, 2);
for (i = 0; i < ZR_COLOR_COUNT; ++i) {
struct zr_color c = config->colors[i];
zr_label(panel, colors[i], ZR_TEXT_LEFT);
if (zr_button_color(panel, c, ZR_BUTTON_DEFAULT)) {
control->picker.active = zr_true;
control->picker.color = config->colors[i];
control->picker.index = i;
}
@ -677,9 +677,9 @@ color_tab(struct gui_context *panel, struct state *control, struct gui_style *co
* COPY & PASTE
* ----------------------------------------------------------------- */
static void
copy(gui_handle handle, const char *text, gui_size size)
copy(zr_handle handle, const char *text, zr_size size)
{
gui_char buffer[1024];
zr_char buffer[1024];
UNUSED(handle);
if (size >= 1023) return;
memcpy(buffer, text, size);
@ -688,15 +688,15 @@ copy(gui_handle handle, const char *text, gui_size size)
}
static void
paste(gui_handle handle, struct gui_edit_box *box)
paste(zr_handle handle, struct zr_edit_box *box)
{
gui_size len;
zr_size len;
const char *text;
UNUSED(handle);
if (!clipboard_is_filled()) return;
text = clipboard_get();
len = strlen(text);
gui_edit_box_add(box, text, len);
zr_edit_box_add(box, text, len);
}
/* -----------------------------------------------------------------
@ -706,21 +706,21 @@ static void
init_demo(struct demo_gui *gui)
{
void *memory;
struct gui_style *config = &gui->config;
struct zr_style *config = &gui->config;
struct state *win = &gui->state;
struct gui_clipboard clip;
gui->running = gui_true;
struct zr_clipboard clip;
gui->running = zr_true;
memory = gui_buffer_alloc(&gui->memory, GUI_BUFFER_FRONT, MAX_COMMAND_MEMORY, 0);
gui_command_queue_init_fixed(&gui->queue, memory, MAX_COMMAND_MEMORY);
gui_style_default(config, GUI_DEFAULT_ALL, &gui->font);
memory = zr_buffer_alloc(&gui->memory, ZR_BUFFER_FRONT, MAX_COMMAND_MEMORY, 0);
zr_command_queue_init_fixed(&gui->queue, memory, MAX_COMMAND_MEMORY);
zr_style_default(config, ZR_DEFAULT_ALL, &gui->font);
/* panel */
gui_window_init(&gui->panel, gui_rect(30, 30, 280, 530),
GUI_WINDOW_BORDER|GUI_WINDOW_MOVEABLE|GUI_WINDOW_SCALEABLE,
zr_window_init(&gui->panel, zr_rect(30, 30, 280, 530),
ZR_WINDOW_BORDER|ZR_WINDOW_MOVEABLE|ZR_WINDOW_SCALEABLE,
&gui->queue, config, &gui->input);
gui_window_init(&gui->sub, gui_rect(400, 50, 220, 180),
GUI_WINDOW_BORDER|GUI_WINDOW_MOVEABLE|GUI_WINDOW_SCALEABLE,
zr_window_init(&gui->sub, zr_rect(400, 50, 220, 180),
ZR_WINDOW_BORDER|ZR_WINDOW_MOVEABLE|ZR_WINDOW_SCALEABLE,
&gui->queue, config, &gui->input);
/* widget state */
@ -728,14 +728,14 @@ init_demo(struct demo_gui *gui)
clip.userdata.ptr = NULL,
clip.copy = copy;
clip.paste = paste;
gui_edit_box_init_fixed(&win->edit, win->edit_buffer, MAX_BUFFER, &clip, NULL);
zr_edit_box_init_fixed(&win->edit, win->edit_buffer, MAX_BUFFER, &clip, NULL);
win->prog_values[0] = 30;
win->prog_values[1] = 80;
win->prog_values[2] = 70;
win->prog_values[3] = 50;
win->scaleable = gui_true;
win->scaleable = zr_true;
win->slider = 2.0f;
win->progressbar = 50;
win->spinner = 100;
@ -748,26 +748,26 @@ init_demo(struct demo_gui *gui)
static void
run_demo(struct demo_gui *gui)
{
struct gui_context layout;
struct zr_context layout;
struct state *state = &gui->state;
struct gui_context tab;
struct gui_style *config = &gui->config;
struct zr_context tab;
struct zr_style *config = &gui->config;
/* first window */
gui_begin(&layout, &gui->panel);
zr_begin(&layout, &gui->panel);
{
/* header */
gui->running = !gui_header(&layout, "Demo",
GUI_CLOSEABLE|GUI_MINIMIZABLE, GUI_CLOSEABLE, GUI_HEADER_RIGHT);
gui->running = !zr_header(&layout, "Demo",
ZR_CLOSEABLE|ZR_MINIMIZABLE, ZR_CLOSEABLE, ZR_HEADER_RIGHT);
/* menubar */
gui_menubar_begin(&layout);
zr_menubar_begin(&layout);
{
gui_layout_row_begin(&layout, GUI_STATIC, 18, 2);
zr_layout_row_begin(&layout, ZR_STATIC, 18, 2);
{
gui_int sel;
gui_layout_row_push(&layout, config->font.width(config->font.userdata, "__FILE__", 8));
sel = gui_menu(&layout, "FILE", file_items, LEN(file_items), 25, 100,
zr_int sel;
zr_layout_row_push(&layout, config->font.width(config->font.userdata, "__FILE__", 8));
sel = zr_menu(&layout, "FILE", file_items, LEN(file_items), 25, 100,
&state->file_open);
switch (sel) {
case MENU_FILE_OPEN:
@ -776,12 +776,12 @@ run_demo(struct demo_gui *gui)
fprintf(stdout, "[Menu:File] close clicked!\n"); break;
case MENU_FILE_QUIT:
fprintf(stdout, "[Menu:File] quit clicked!\n"); break;
case GUI_NONE:
case ZR_NONE:
default: break;
}
gui_layout_row_push(&layout, config->font.width(config->font.userdata, "__EDIT__", 8));
sel = gui_menu(&layout, "EDIT", edit_items, LEN(edit_items), 25, 100,
zr_layout_row_push(&layout, config->font.width(config->font.userdata, "__EDIT__", 8));
sel = zr_menu(&layout, "EDIT", edit_items, LEN(edit_items), 25, 100,
&state->edit_open);
switch (sel) {
case MENU_EDIT_COPY:
@ -792,112 +792,112 @@ run_demo(struct demo_gui *gui)
fprintf(stdout, "[Menu:Edit] delete clicked!\n"); break;
case MENU_EDIT_PASTE:
fprintf(stdout, "[Menu:Edit] paste clicked!\n"); break;
case GUI_NONE:
case ZR_NONE:
default: break;
}
}
gui_layout_row_end(&layout);
zr_layout_row_end(&layout);
}
gui_menubar_end(&layout);
zr_menubar_end(&layout);
/* panel style configuration */
if (gui_layout_push(&layout, GUI_LAYOUT_TAB, "Style", &state->config_tab))
if (zr_layout_push(&layout, ZR_LAYOUT_TAB, "Style", &state->config_tab))
{
if (gui_layout_push(&layout, GUI_LAYOUT_NODE, "Options", &state->flag_tab)) {
if (zr_layout_push(&layout, ZR_LAYOUT_NODE, "Options", &state->flag_tab)) {
update_flags(&layout);
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
if (gui_layout_push(&layout, GUI_LAYOUT_NODE, "Properties", &state->style_tab)) {
if (zr_layout_push(&layout, ZR_LAYOUT_NODE, "Properties", &state->style_tab)) {
properties_tab(&layout, config);
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
if (gui_layout_push(&layout, GUI_LAYOUT_NODE, "Rounding", &state->round_tab)) {
if (zr_layout_push(&layout, ZR_LAYOUT_NODE, "Rounding", &state->round_tab)) {
round_tab(&layout, config);
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
if (gui_layout_push(&layout, GUI_LAYOUT_NODE, "Color", &state->color_tab)) {
if (zr_layout_push(&layout, ZR_LAYOUT_NODE, "Color", &state->color_tab)) {
color_tab(&layout, state, config);
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
/* widgets examples */
if (gui_layout_push(&layout, GUI_LAYOUT_TAB, "Widgets", &state->widget_tab)) {
if (zr_layout_push(&layout, ZR_LAYOUT_TAB, "Widgets", &state->widget_tab)) {
widget_panel(&layout, state);
gui_layout_pop(&layout);
zr_layout_pop(&layout);
}
/* popup panel */
if (state->popup)
{
gui_popup_begin(&layout, &tab, GUI_POPUP_STATIC, 0, gui_rect(20, 100, 220, 150), gui_vec2(0,0));
zr_popup_begin(&layout, &tab, ZR_POPUP_STATIC, 0, zr_rect(20, 100, 220, 150), zr_vec2(0,0));
{
if (gui_header(&tab, "Popup", GUI_CLOSEABLE, GUI_CLOSEABLE, GUI_HEADER_LEFT)) {
gui_popup_close(&tab);
state->popup = gui_false;
if (zr_header(&tab, "Popup", ZR_CLOSEABLE, ZR_CLOSEABLE, ZR_HEADER_LEFT)) {
zr_popup_close(&tab);
state->popup = zr_false;
}
gui_layout_row_dynamic(&tab, 30, 1);
gui_label(&tab, "Are you sure you want to exit?", GUI_TEXT_LEFT);
gui_layout_row_dynamic(&tab, 30, 4);
gui_spacing(&tab, 1);
if (gui_button_text(&tab, "Yes", GUI_BUTTON_DEFAULT)) {
gui_popup_close(&tab);
state->popup = gui_false;
zr_layout_row_dynamic(&tab, 30, 1);
zr_label(&tab, "Are you sure you want to exit?", ZR_TEXT_LEFT);
zr_layout_row_dynamic(&tab, 30, 4);
zr_spacing(&tab, 1);
if (zr_button_text(&tab, "Yes", ZR_BUTTON_DEFAULT)) {
zr_popup_close(&tab);
state->popup = zr_false;
}
if (gui_button_text(&tab, "No", GUI_BUTTON_DEFAULT)) {
gui_popup_close(&tab);
state->popup = gui_false;
if (zr_button_text(&tab, "No", ZR_BUTTON_DEFAULT)) {
zr_popup_close(&tab);
state->popup = zr_false;
}
}
gui_popup_end(&layout, &tab);
zr_popup_end(&layout, &tab);
}
{
/* shelf + graphes */
static const char *shelfs[] = {"Histogram", "Lines"};
gui_layout_row_dynamic(&layout, 190, 1);
state->shelf_selection = gui_shelf_begin(&layout, &tab, shelfs,
zr_layout_row_dynamic(&layout, 190, 1);
state->shelf_selection = zr_shelf_begin(&layout, &tab, shelfs,
LEN(shelfs), state->shelf_selection, state->shelf);
{
enum {COLUMNS, LINES};
static const gui_float values[]={8.0f,15.0f,20.0f,12.0f,30.0f,12.0f,35.0f,40.0f,20.0f};
gui_layout_row_dynamic(&tab, 100, 1);
static const zr_float values[]={8.0f,15.0f,20.0f,12.0f,30.0f,12.0f,35.0f,40.0f,20.0f};
zr_layout_row_dynamic(&tab, 100, 1);
switch (state->shelf_selection) {
case COLUMNS:
gui_graph(&tab, GUI_GRAPH_COLUMN, values, LEN(values), 0); break;
zr_graph(&tab, ZR_GRAPH_COLUMN, values, LEN(values), 0); break;
case LINES:
gui_graph(&tab, GUI_GRAPH_LINES, values, LEN(values), 0); break;
zr_graph(&tab, ZR_GRAPH_LINES, values, LEN(values), 0); break;
default: break;
}
}
state->shelf = gui_shelf_end(&layout, &tab);
state->shelf = zr_shelf_end(&layout, &tab);
}
{
/* tree */
struct gui_tree tree;
gui_layout_row_dynamic(&layout, 250, 1);
gui_tree_begin(&layout, &tree, "Tree", 20, state->tree);
struct zr_tree tree;
zr_layout_row_dynamic(&layout, 250, 1);
zr_tree_begin(&layout, &tree, "Tree", 20, state->tree);
upload_tree(&state->test, &tree, &state->test.root);
state->tree = gui_tree_end(&layout, &tree);
state->tree = zr_tree_end(&layout, &tree);
}
}
gui_end(&layout, &gui->panel);
zr_end(&layout, &gui->panel);
/* second panel */
gui_begin(&layout, &gui->sub);
zr_begin(&layout, &gui->sub);
{
enum {EASY, HARD};
gui_header(&layout, "Show", GUI_CLOSEABLE, 0, GUI_HEADER_LEFT);
gui_layout_row_static(&layout, 30, 80, 1);
if (gui_button_text(&layout, "button", GUI_BUTTON_DEFAULT)) {
zr_header(&layout, "Show", ZR_CLOSEABLE, 0, ZR_HEADER_LEFT);
zr_layout_row_static(&layout, 30, 80, 1);
if (zr_button_text(&layout, "button", ZR_BUTTON_DEFAULT)) {
/* event handling */
}
gui_layout_row_dynamic(&layout, 30, 2);
if (gui_option(&layout, "easy", state->op == EASY)) state->op = EASY;
if (gui_option(&layout, "hard", state->op == HARD)) state->op = HARD;
zr_layout_row_dynamic(&layout, 30, 2);
if (zr_option(&layout, "easy", state->op == EASY)) state->op = EASY;
if (zr_option(&layout, "hard", state->op == HARD)) state->op = HARD;
}
gui_end(&layout, &gui->sub);
zr_end(&layout, &gui->sub);
}

View File

@ -25,20 +25,20 @@
#include "nanovg/nanovg_gl_utils.h"
/* macros */
#define DTIME 16
#define DTIME 33
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) < (b) ? (b) : (a))
#define CLAMP(i,v,x) (MAX(MIN(v,x), i))
#define LEN(a) (sizeof(a)/sizeof(a)[0])
#define UNUSED(a) ((void)(a))
#include "../gui.h"
#include "../zahnrad.h"
static void
clipboard_set(const char *text)
{SDL_SetClipboardText(text);}
static gui_bool
static zr_bool
clipboard_is_filled(void)
{return SDL_HasClipboardText();}
@ -59,21 +59,21 @@ die(const char *fmt, ...)
exit(EXIT_FAILURE);
}
static gui_size
font_get_width(gui_handle handle, const gui_char *text, gui_size len)
static zr_size
font_get_width(zr_handle handle, const zr_char *text, zr_size len)
{
gui_size width;
zr_size width;
float bounds[4];
NVGcontext *ctx = (NVGcontext*)handle.ptr;
nvgTextBounds(ctx, 0, 0, text, &text[len], bounds);
width = (gui_size)(bounds[2] - bounds[0]);
width = (zr_size)(bounds[2] - bounds[0]);
return width;
}
static void
draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
draw(NVGcontext *nvg, struct zr_command_queue *queue, int width, int height)
{
const struct gui_command *cmd;
const struct zr_command *cmd;
glPushAttrib(GL_ENABLE_BIT|GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@ -83,45 +83,45 @@ draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
glEnable(GL_TEXTURE_2D);
nvgBeginFrame(nvg, width, height, ((float)width/(float)height));
gui_foreach_command(cmd, queue) {
zr_foreach_command(cmd, queue) {
switch (cmd->type) {
case GUI_COMMAND_NOP: break;
case GUI_COMMAND_SCISSOR: {
const struct gui_command_scissor *s = gui_command(scissor, cmd);
case ZR_COMMAND_NOP: break;
case ZR_COMMAND_SCISSOR: {
const struct zr_command_scissor *s = zr_command(scissor, cmd);
nvgScissor(nvg, s->x, s->y, s->w, s->h);
} break;
case GUI_COMMAND_LINE: {
const struct gui_command_line *l = gui_command(line, cmd);
case ZR_COMMAND_LINE: {
const struct zr_command_line *l = zr_command(line, cmd);
nvgBeginPath(nvg);
nvgMoveTo(nvg, l->begin.x, l->begin.y);
nvgLineTo(nvg, l->end.x, l->end.y);
nvgFillColor(nvg, nvgRGBA(l->color.r, l->color.g, l->color.b, l->color.a));
nvgFill(nvg);
} break;
case GUI_COMMAND_CURVE: {
const struct gui_command_curve *q = gui_command(curve, cmd);
case ZR_COMMAND_CURVE: {
const struct zr_command_curve *q = zr_command(curve, cmd);
nvgBeginPath(nvg);
nvgMoveTo(nvg, q->begin.x, q->begin.y);
nvgBezierTo(nvg, q->ctrl[0].x, q->ctrl[0].y, q->ctrl[1].x, q->ctrl[1].y, q->end.x, q->end.y);
nvgStrokeColor(nvg, nvgRGBA(q->color.r, q->color.g, q->color.b, q->color.a));
nvgStroke(nvg);
} break;
case GUI_COMMAND_RECT: {
const struct gui_command_rect *r = gui_command(rect, cmd);
case ZR_COMMAND_RECT: {
const struct zr_command_rect *r = zr_command(rect, cmd);
nvgBeginPath(nvg);
nvgRoundedRect(nvg, r->x, r->y, r->w, r->h, r->rounding);
nvgFillColor(nvg, nvgRGBA(r->color.r, r->color.g, r->color.b, r->color.a));
nvgFill(nvg);
} break;
case GUI_COMMAND_CIRCLE: {
const struct gui_command_circle *c = gui_command(circle, cmd);
case ZR_COMMAND_CIRCLE: {
const struct zr_command_circle *c = zr_command(circle, cmd);
nvgBeginPath(nvg);
nvgCircle(nvg, c->x + (c->w/2.0f), c->y + c->w/2.0f, c->w/2.0f);
nvgFillColor(nvg, nvgRGBA(c->color.r, c->color.g, c->color.b, c->color.a));
nvgFill(nvg);
} break;
case GUI_COMMAND_TRIANGLE: {
const struct gui_command_triangle *t = gui_command(triangle, cmd);
case ZR_COMMAND_TRIANGLE: {
const struct zr_command_triangle *t = zr_command(triangle, cmd);
nvgBeginPath(nvg);
nvgMoveTo(nvg, t->a.x, t->a.y);
nvgLineTo(nvg, t->b.x, t->b.y);
@ -130,8 +130,8 @@ draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
nvgFillColor(nvg, nvgRGBA(t->color.r, t->color.g, t->color.b, t->color.a));
nvgFill(nvg);
} break;
case GUI_COMMAND_TEXT: {
const struct gui_command_text *t = gui_command(text, cmd);
case ZR_COMMAND_TEXT: {
const struct zr_command_text *t = zr_command(text, cmd);
nvgBeginPath(nvg);
nvgRoundedRect(nvg, t->x, t->y, t->w, t->h, 0);
nvgFillColor(nvg, nvgRGBA(t->background.r, t->background.g,
@ -145,8 +145,8 @@ draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
nvgText(nvg, t->x, t->y + t->h * 0.5f, t->string, &t->string[t->length]);
nvgFill(nvg);
} break;
case GUI_COMMAND_IMAGE: {
const struct gui_command_image *i = gui_command(image, cmd);
case ZR_COMMAND_IMAGE: {
const struct zr_command_image *i = zr_command(image, cmd);
NVGpaint imgpaint;
imgpaint = nvgImagePattern(nvg, i->x, i->y, i->w, i->h, 0, i->img.handle.id, 1.0f);
nvgBeginPath(nvg);
@ -154,11 +154,11 @@ draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
nvgFillPaint(nvg, imgpaint);
nvgFill(nvg);
} break;
case GUI_COMMAND_MAX:
case ZR_COMMAND_MAX:
default: break;
}
}
gui_command_queue_clear(queue);
zr_command_queue_clear(queue);
nvgResetScissor(nvg);
nvgEndFrame(nvg);
@ -166,57 +166,57 @@ draw(NVGcontext *nvg, struct gui_command_queue *queue, int width, int height)
}
static void
key(struct gui_input *in, SDL_Event *evt, gui_bool down)
key(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Keycode sym = evt->key.keysym.sym;
if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT)
gui_input_key(in, GUI_KEY_SHIFT, down);
zr_input_key(in, ZR_KEY_SHIFT, down);
else if (sym == SDLK_DELETE)
gui_input_key(in, GUI_KEY_DEL, down);
zr_input_key(in, ZR_KEY_DEL, down);
else if (sym == SDLK_RETURN)
gui_input_key(in, GUI_KEY_ENTER, down);
zr_input_key(in, ZR_KEY_ENTER, down);
else if (sym == SDLK_SPACE)
gui_input_key(in, GUI_KEY_SPACE, down);
zr_input_key(in, ZR_KEY_SPACE, down);
else if (sym == SDLK_BACKSPACE)
gui_input_key(in, GUI_KEY_BACKSPACE, down);
zr_input_key(in, ZR_KEY_BACKSPACE, down);
else if (sym == SDLK_LEFT)
gui_input_key(in, GUI_KEY_LEFT, down);
zr_input_key(in, ZR_KEY_LEFT, down);
else if (sym == SDLK_RIGHT)
gui_input_key(in, GUI_KEY_RIGHT, down);
zr_input_key(in, ZR_KEY_RIGHT, down);
else if (sym == SDLK_c)
gui_input_key(in, GUI_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]);
else if (sym == SDLK_v)
gui_input_key(in, GUI_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]);
else if (sym == SDLK_x)
gui_input_key(in, GUI_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]);
}
static void
motion(struct gui_input *in, SDL_Event *evt)
motion(struct zr_input *in, SDL_Event *evt)
{
const gui_int x = evt->motion.x;
const gui_int y = evt->motion.y;
gui_input_motion(in, x, y);
const zr_int x = evt->motion.x;
const zr_int y = evt->motion.y;
zr_input_motion(in, x, y);
}
static void
btn(struct gui_input *in, SDL_Event *evt, gui_bool down)
btn(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const gui_int x = evt->button.x;
const gui_int y = evt->button.y;
const zr_int x = evt->button.x;
const zr_int y = evt->button.y;
if (evt->button.button == SDL_BUTTON_LEFT)
gui_input_button(in, GUI_BUTTON_LEFT, x, y, down);
zr_input_button(in, ZR_BUTTON_LEFT, x, y, down);
else if (evt->button.button == SDL_BUTTON_LEFT)
gui_input_button(in, GUI_BUTTON_RIGHT, x, y, down);
zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down);
}
static void
text(struct gui_input *in, SDL_Event *evt)
text(struct zr_input *in, SDL_Event *evt)
{
gui_glyph glyph;
memcpy(glyph, evt->text.text, GUI_UTF_SIZE);
gui_input_glyph(in, glyph);
zr_glyph glyph;
memcpy(glyph, evt->text.text, ZR_UTF_SIZE);
zr_input_glyph(in, glyph);
}
static void
@ -232,7 +232,7 @@ main(int argc, char *argv[])
/* Platform */
int width, height;
const char *font_path;
gui_size font_height;
zr_size font_height;
SDL_Window *win;
SDL_GLContext glContext;
NVGcontext *vg = NULL;
@ -274,8 +274,8 @@ main(int argc, char *argv[])
/* GUI */
memset(&gui, 0, sizeof gui);
gui_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
gui.font.userdata = gui_handle_ptr(vg);
zr_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
gui.font.userdata = zr_handle_ptr(vg);
gui.font.width = font_get_width;
nvgTextMetrics(vg, NULL, NULL, &gui.font.height);
init_demo(&gui);
@ -284,20 +284,20 @@ main(int argc, char *argv[])
/* Input */
SDL_Event evt;
started = SDL_GetTicks();
gui_input_begin(&gui.input);
zr_input_begin(&gui.input);
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_WINDOWEVENT) resize(&evt);
else if (evt.type == SDL_QUIT) goto cleanup;
else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, gui_false);
else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, gui_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, gui_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, gui_false);
else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, zr_false);
else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, zr_false);
else if (evt.type == SDL_MOUSEMOTION) motion(&gui.input, &evt);
else if (evt.type == SDL_TEXTINPUT) text(&gui.input, &evt);
else if (evt.type == SDL_MOUSEWHEEL)
gui_input_scroll(&gui.input,(float)evt.wheel.y);
zr_input_scroll(&gui.input,(float)evt.wheel.y);
}
gui_input_end(&gui.input);
zr_input_end(&gui.input);
/* GUI */
SDL_GetWindowSize(win, &width, &height);
@ -317,7 +317,7 @@ main(int argc, char *argv[])
cleanup:
/* Cleanup */
free(gui_buffer_memory(&gui.memory));
free(zr_buffer_memory(&gui.memory));
nvgDeleteGLES2(vg);
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(win);

View File

@ -18,7 +18,7 @@
#include <SDL2/SDL.h>
/* macros */
#define DTIME 17
#define DTIME 33
#define MAX_DRAW_COMMAND_MEMORY (4 * 1024)
#define MIN(a,b) ((a) < (b) ? (a) : (b))
@ -27,10 +27,10 @@
#define LEN(a) (sizeof(a)/sizeof(a)[0])
#define UNUSED(a) ((void)(a))
#include "../gui.h"
#include "../zahnrad.h"
static void clipboard_set(const char *text){SDL_SetClipboardText(text);}
static gui_bool clipboard_is_filled(void) {return SDL_HasClipboardText();}
static zr_bool clipboard_is_filled(void) {return SDL_HasClipboardText();}
static const char* clipboard_get(void){return SDL_GetClipboardText();}
#include "demo.c"
@ -77,8 +77,8 @@ struct device {
GLint uniform_tex;
GLint uniform_proj;
GLuint font_tex;
struct gui_draw_null_texture null;
struct gui_buffer cmds;
struct zr_draw_null_texture null;
struct zr_buffer cmds;
};
#define glerror() glerror_(__FILE__, __LINE__)
@ -152,13 +152,12 @@ device_init(struct device *dev)
dev->attrib_col = glGetAttribLocation(dev->prog, "Color");
glerror();
{
/* buffer setup */
size_t vs = sizeof(struct gui_draw_vertex);
size_t vp = offsetof(struct gui_draw_vertex, position);
size_t vt = offsetof(struct gui_draw_vertex, uv);
size_t vc = offsetof(struct gui_draw_vertex, col);
size_t vs = sizeof(struct zr_draw_vertex);
size_t vp = offsetof(struct zr_draw_vertex, position);
size_t vt = offsetof(struct zr_draw_vertex, uv);
size_t vc = offsetof(struct zr_draw_vertex, col);
glGenBuffers(1, &dev->vbo);
glGenBuffers(1, &dev->ebo);
@ -186,16 +185,16 @@ device_init(struct device *dev)
glerror();
}
static struct gui_user_font
font_bake_and_upload(struct device *dev, struct gui_font *font,
const char *path, unsigned int font_height, const gui_long *range)
static struct zr_user_font
font_bake_and_upload(struct device *dev, struct zr_font *font,
const char *path, unsigned int font_height, const zr_long *range)
{
gui_size glyph_count;
gui_size img_width, img_height;
struct gui_font_glyph *glyphes;
struct gui_baked_font baked_font;
struct gui_user_font user_font;
struct gui_recti custom;
zr_size glyph_count;
zr_size img_width, img_height;
struct zr_font_glyph *glyphes;
struct zr_baked_font baked_font;
struct zr_user_font user_font;
struct zr_recti custom;
memset(&baked_font, 0, sizeof(baked_font));
memset(&user_font, 0, sizeof(user_font));
@ -205,9 +204,9 @@ font_bake_and_upload(struct device *dev, struct gui_font *font,
/* bake and upload font texture */
void *img, *tmp;
size_t ttf_size;
gui_size tmp_size, img_size;
zr_size tmp_size, img_size;
const char *custom_data = "....";
struct gui_font_config config;
struct zr_font_config config;
char *ttf_blob = file_load(path, &ttf_size);
if (!ttf_blob)
die("[Font]: %s is not a file or cannot be found!\n", path);
@ -217,32 +216,32 @@ font_bake_and_upload(struct device *dev, struct gui_font *font,
config.ttf_blob = ttf_blob;
config.ttf_size = ttf_size;
config.font = &baked_font;
config.coord_type = GUI_COORD_UV;
config.coord_type = ZR_COORD_UV;
config.range = range;
config.pixel_snap = gui_false;
config.pixel_snap = zr_false;
config.size = font_height;
config.spacing = gui_vec2(0,0);
config.spacing = zr_vec2(0,0);
config.oversample_h = 1;
config.oversample_v = 1;
/* query needed amount of memory for the font baking process */
gui_font_bake_memory(&tmp_size, &glyph_count, &config, 1);
glyphes = (struct gui_font_glyph*)calloc(sizeof(struct gui_font_glyph), glyph_count);
zr_font_bake_memory(&tmp_size, &glyph_count, &config, 1);
glyphes = (struct zr_font_glyph*)calloc(sizeof(struct zr_font_glyph), glyph_count);
tmp = calloc(1, tmp_size);
/* pack all glyphes and return needed image width height and memory size*/
custom.w = 2; custom.h = 2;
if (!gui_font_bake_pack(&img_size, &img_width,&img_height,&custom,tmp,tmp_size,&config, 1))
if (!zr_font_bake_pack(&img_size, &img_width,&img_height,&custom,tmp,tmp_size,&config, 1))
die("[Font]: failed to load font!\n");
/* bake all glyphes and custom white pixel into image */
img = calloc(1, img_size);
gui_font_bake(img, img_width, img_height, tmp, tmp_size, glyphes, glyph_count, &config, 1);
gui_font_bake_custom_data(img, img_width, img_height, custom, custom_data, 2, 2, '.', 'X');
zr_font_bake(img, img_width, img_height, tmp, tmp_size, glyphes, glyph_count, &config, 1);
zr_font_bake_custom_data(img, img_width, img_height, custom, custom_data, 2, 2, '.', 'X');
{
/* convert alpha8 image into rgba8 image */
void *img_rgba = calloc(4, img_height * img_width);
gui_font_bake_convert(img_rgba, (gui_ushort)img_width, (gui_ushort)img_height, img);
zr_font_bake_convert(img_rgba, (zr_ushort)img_width, (zr_ushort)img_height, img);
free(img);
img = img_rgba;
}
@ -261,15 +260,15 @@ font_bake_and_upload(struct device *dev, struct gui_font *font,
}
/* default white pixel in a texture which is needed to draw primitives */
dev->null.texture.id = (gui_int)dev->font_tex;
dev->null.uv = gui_vec2((custom.x + 0.5f)/(gui_float)img_width,
(custom.y + 0.5f)/(gui_float)img_height);
dev->null.texture.id = (zr_int)dev->font_tex;
dev->null.uv = zr_vec2((custom.x + 0.5f)/(zr_float)img_width,
(custom.y + 0.5f)/(zr_float)img_height);
/* setup font with glyphes. IMPORTANT: the font only references the glyphes
this was done to have the possibility to have multible fonts with one
total glyph array. Not quite sure if it is a good thing since the
glyphes have to be freed as well. */
gui_font_init(font, font_height, '?', glyphes, &baked_font, dev->null.texture);
user_font = gui_font_ref(font);
zr_font_init(font, font_height, '?', glyphes, &baked_font, dev->null.texture);
user_font = zr_font_ref(font);
return user_font;
}
@ -286,12 +285,12 @@ device_shutdown(struct device *dev)
glDeleteBuffers(1, &dev->ebo);
}
/* this is stupid but needed for C89 since sinf and cosf do not exist in that library version */
static gui_float fsin(gui_float f) {return (gui_float)sin(f);}
static gui_float fcos(gui_float f) {return (gui_float)cos(f);}
/* this is stupid but needed for C89 since sinf and cosf do not exist */
static zr_float fsin(zr_float f) {return (zr_float)sin(f);}
static zr_float fcos(zr_float f) {return (zr_float)cos(f);}
static void
device_draw(struct device *dev, struct gui_command_queue *queue, int width, int height)
device_draw(struct device *dev, struct zr_command_queue *queue, int width, int height)
{
GLint last_prog, last_tex;
GLint last_ebo, last_vbo, last_vao;
@ -331,12 +330,12 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
{
/* convert from command queue into draw list and draw to screen */
struct gui_draw_list draw_list;
const struct gui_draw_command *cmd;
struct zr_draw_list draw_list;
const struct zr_draw_command *cmd;
static const GLsizeiptr max_vertex_memory = 128 * 1024;
static const GLsizeiptr max_element_memory = 32 * 1024;
void *vertexes, *elements;
const gui_draw_index *offset = NULL;
const zr_draw_index *offset = NULL;
/* allocate vertex and element buffer */
memset(&draw_list, 0, sizeof(draw_list));
@ -352,19 +351,19 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
vertexes = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
elements = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
{
struct gui_buffer vbuf, ebuf;
gui_buffer_init_fixed(&vbuf, vertexes, (gui_size)max_vertex_memory);
gui_buffer_init_fixed(&ebuf, elements, (gui_size)max_element_memory);
gui_draw_list_init(&draw_list, &dev->cmds, &vbuf, &ebuf,
fsin, fcos, dev->null, GUI_ANTI_ALIASING_ON);
gui_draw_list_load(&draw_list, queue, 1.0f, 22);
struct zr_buffer vbuf, ebuf;
zr_buffer_init_fixed(&vbuf, vertexes, (zr_size)max_vertex_memory);
zr_buffer_init_fixed(&ebuf, elements, (zr_size)max_element_memory);
zr_draw_list_init(&draw_list, &dev->cmds, &vbuf, &ebuf,
fsin, fcos, dev->null, ZR_ANTI_ALIASING_ON);
zr_draw_list_load(&draw_list, queue, 1.0f, 22);
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glerror();
/* iterate and execute each draw command */
gui_foreach_draw_command(cmd, &draw_list) {
zr_foreach_draw_command(cmd, &draw_list) {
glBindTexture(GL_TEXTURE_2D, (GLuint)cmd->texture.id);
glScissor((GLint)cmd->clip_rect.x,
height - (GLint)(cmd->clip_rect.y + cmd->clip_rect.h),
@ -374,8 +373,8 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
glerror();
}
gui_command_queue_clear(queue);
gui_draw_list_clear(&draw_list);
zr_command_queue_clear(queue);
zr_draw_list_clear(&draw_list);
}
/* restore old state */
@ -389,57 +388,57 @@ device_draw(struct device *dev, struct gui_command_queue *queue, int width, int
}
static void
key(struct gui_input *in, SDL_Event *evt, gui_bool down)
key(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const Uint8* state = SDL_GetKeyboardState(NULL);
SDL_Keycode sym = evt->key.keysym.sym;
if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT)
gui_input_key(in, GUI_KEY_SHIFT, down);
zr_input_key(in, ZR_KEY_SHIFT, down);
else if (sym == SDLK_DELETE)
gui_input_key(in, GUI_KEY_DEL, down);
zr_input_key(in, ZR_KEY_DEL, down);
else if (sym == SDLK_RETURN)
gui_input_key(in, GUI_KEY_ENTER, down);
zr_input_key(in, ZR_KEY_ENTER, down);
else if (sym == SDLK_SPACE)
gui_input_key(in, GUI_KEY_SPACE, down);
zr_input_key(in, ZR_KEY_SPACE, down);
else if (sym == SDLK_BACKSPACE)
gui_input_key(in, GUI_KEY_BACKSPACE, down);
zr_input_key(in, ZR_KEY_BACKSPACE, down);
else if (sym == SDLK_LEFT)
gui_input_key(in, GUI_KEY_LEFT, down);
zr_input_key(in, ZR_KEY_LEFT, down);
else if (sym == SDLK_RIGHT)
gui_input_key(in, GUI_KEY_RIGHT, down);
zr_input_key(in, ZR_KEY_RIGHT, down);
else if (sym == SDLK_c)
gui_input_key(in, GUI_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]);
else if (sym == SDLK_v)
gui_input_key(in, GUI_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]);
else if (sym == SDLK_x)
gui_input_key(in, GUI_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]);
zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]);
}
static void
motion(struct gui_input *in, SDL_Event *evt)
motion(struct zr_input *in, SDL_Event *evt)
{
const gui_int x = evt->motion.x;
const gui_int y = evt->motion.y;
gui_input_motion(in, x, y);
const zr_int x = evt->motion.x;
const zr_int y = evt->motion.y;
zr_input_motion(in, x, y);
}
static void
btn(struct gui_input *in, SDL_Event *evt, gui_bool down)
btn(struct zr_input *in, SDL_Event *evt, zr_bool down)
{
const gui_int x = evt->button.x;
const gui_int y = evt->button.y;
const zr_int x = evt->button.x;
const zr_int y = evt->button.y;
if (evt->button.button == SDL_BUTTON_LEFT)
gui_input_button(in, GUI_BUTTON_LEFT, x, y, down);
zr_input_button(in, ZR_BUTTON_LEFT, x, y, down);
if (evt->button.button == SDL_BUTTON_RIGHT)
gui_input_button(in, GUI_BUTTON_RIGHT, x, y, down);
zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down);
}
static void
text(struct gui_input *in, SDL_Event *evt)
text(struct zr_input *in, SDL_Event *evt)
{
gui_glyph glyph;
memcpy(glyph, evt->text.text, GUI_UTF_SIZE);
gui_input_glyph(in, glyph);
zr_glyph glyph;
memcpy(glyph, evt->text.text, ZR_UTF_SIZE);
zr_input_glyph(in, glyph);
}
static void
@ -465,7 +464,7 @@ main(int argc, char *argv[])
/* GUI */
struct device device;
struct demo_gui gui;
struct gui_font font;
struct zr_font font;
font_path = argv[1];
if (argc < 2)
@ -488,11 +487,11 @@ main(int argc, char *argv[])
/* GUI */
memset(&gui, 0, sizeof gui);
gui_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
mem = gui_buffer_alloc(&gui.memory, GUI_BUFFER_FRONT, MAX_DRAW_COMMAND_MEMORY, 0);
gui_buffer_init_fixed(&device.cmds, mem, MAX_DRAW_COMMAND_MEMORY);
zr_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
mem = zr_buffer_alloc(&gui.memory, ZR_BUFFER_FRONT, MAX_DRAW_COMMAND_MEMORY, 0);
zr_buffer_init_fixed(&device.cmds, mem, MAX_DRAW_COMMAND_MEMORY);
gui.font = font_bake_and_upload(&device, &font, font_path, 14,
gui_font_default_glyph_ranges());
zr_font_default_glyph_ranges());
init_demo(&gui);
device_init(&device);
@ -501,25 +500,25 @@ main(int argc, char *argv[])
/* Input */
SDL_Event evt;
started = SDL_GetTicks();
gui_input_begin(&gui.input);
zr_input_begin(&gui.input);
while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_WINDOWEVENT) resize(&evt);
else if (evt.type == SDL_QUIT) goto cleanup;
else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, gui_false);
else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, gui_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, gui_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, gui_false);
else if (evt.type == SDL_KEYUP) key(&gui.input, &evt, zr_false);
else if (evt.type == SDL_KEYDOWN) key(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.input, &evt, zr_true);
else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.input, &evt, zr_false);
else if (evt.type == SDL_MOUSEMOTION) motion(&gui.input, &evt);
else if (evt.type == SDL_TEXTINPUT) text(&gui.input, &evt);
else if (evt.type == SDL_MOUSEWHEEL)
gui_input_scroll(&gui.input,(float)evt.wheel.y);
zr_input_scroll(&gui.input,(float)evt.wheel.y);
}
gui_input_end(&gui.input);
zr_input_end(&gui.input);
/* GUI */
SDL_GetWindowSize(win, &width, &height);
gui.w = (gui_size)width;
gui.h = (gui_size)height;
gui.w = (zr_size)width;
gui.h = (zr_size)height;
run_demo(&gui);
/* Draw */
@ -537,7 +536,7 @@ main(int argc, char *argv[])
cleanup:
/* Cleanup */
free(font.glyphes);
free(gui_buffer_memory(&gui.memory));
free(zr_buffer_memory(&gui.memory));
device_shutdown(&device);
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(win);

View File

@ -22,10 +22,10 @@
#define LEN(a) (sizeof(a)/sizeof(a)[0])
#define UNUSED(a) ((void)(a))
#include "../gui.h"
#include "../zahnrad.h"
static void clipboard_set(const char *text){UNUSED(text);}
static gui_bool clipboard_is_filled(void){return gui_false;}
static zr_bool clipboard_is_filled(void){return zr_false;}
static const char* clipboard_get(void) {return NULL;}
#include "demo.c"
@ -138,12 +138,12 @@ font_create(Display *dpy, const char *name)
return font;
}
static gui_size
font_get_text_width(gui_handle handle, const gui_char *text, gui_size len)
static zr_size
font_get_text_width(zr_handle handle, const zr_char *text, zr_size len)
{
XFont *font = (XFont*)handle.ptr;
XRectangle r;
gui_size width;
zr_size width;
if(!font || !text)
return 0;
@ -152,7 +152,7 @@ font_get_text_width(gui_handle handle, const gui_char *text, gui_size len)
return r.width;
}
else {
return (gui_size)XTextWidth(font->xfont, (const char*)text, (int)len);
return (zr_size)XTextWidth(font->xfont, (const char*)text, (int)len);
}
return width;
}
@ -169,7 +169,7 @@ font_del(Display *dpy, XFont *font)
}
static unsigned long
color_from_byte(const gui_byte *c)
color_from_byte(const zr_byte *c)
{
/* NOTE(vurtun): this only works for little-endian */
unsigned long res = 0;
@ -218,8 +218,8 @@ surface_scissor(XSurface *surf, float x, float y, float w, float h)
}
static void
surface_draw_line(XSurface *surf, gui_short x0, gui_short y0, gui_short x1,
gui_short y1, struct gui_color col)
surface_draw_line(XSurface *surf, zr_short x0, zr_short y0, zr_short x1,
zr_short y1, struct zr_color col)
{
unsigned long c = color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
@ -227,8 +227,8 @@ surface_draw_line(XSurface *surf, gui_short x0, gui_short y0, gui_short x1,
}
static void
surface_draw_rect(XSurface* surf, gui_short x, gui_short y, gui_ushort w,
gui_ushort h, struct gui_color col)
surface_draw_rect(XSurface* surf, zr_short x, zr_short y, zr_ushort w,
zr_ushort h, struct zr_color col)
{
unsigned long c = color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
@ -236,8 +236,8 @@ surface_draw_rect(XSurface* surf, gui_short x, gui_short y, gui_ushort w,
}
static void
surface_draw_triangle(XSurface *surf, gui_short x0, gui_short y0, gui_short x1,
gui_short y1, gui_short x2, gui_short y2, struct gui_color col)
surface_draw_triangle(XSurface *surf, zr_short x0, zr_short y0, zr_short x1,
zr_short y1, zr_short x2, zr_short y2, struct zr_color col)
{
XPoint pnts[3];
unsigned long c = color_from_byte(&col.r);
@ -252,8 +252,8 @@ surface_draw_triangle(XSurface *surf, gui_short x0, gui_short y0, gui_short x1,
}
static void
surface_draw_circle(XSurface *surf, gui_short x, gui_short y, gui_ushort w,
gui_ushort h, struct gui_color col)
surface_draw_circle(XSurface *surf, zr_short x, zr_short y, zr_ushort w,
zr_ushort h, struct zr_color col)
{
unsigned long c = color_from_byte(&col.r);
XSetForeground(surf->dpy, surf->gc, c);
@ -262,8 +262,8 @@ surface_draw_circle(XSurface *surf, gui_short x, gui_short y, gui_ushort w,
}
static void
surface_draw_text(XSurface *surf, gui_short x, gui_short y, gui_ushort w, gui_ushort h,
const char *text, size_t len, XFont *font, struct gui_color cbg, struct gui_color cfg)
surface_draw_text(XSurface *surf, zr_short x, zr_short y, zr_ushort w, zr_ushort h,
const char *text, size_t len, XFont *font, struct zr_color cbg, struct zr_color cfg)
{
int tx, ty, th;
unsigned long bg = color_from_byte(&cbg.r);
@ -305,100 +305,100 @@ surface_del(XSurface *surf)
}
static void
draw(XSurface *surf, struct gui_command_queue *queue)
draw(XSurface *surf, struct zr_command_queue *queue)
{
const struct gui_command *cmd;
gui_foreach_command(cmd, queue) {
const struct zr_command *cmd;
zr_foreach_command(cmd, queue) {
switch (cmd->type) {
case GUI_COMMAND_NOP: break;
case GUI_COMMAND_SCISSOR: {
const struct gui_command_scissor *s = gui_command(scissor, cmd);
case ZR_COMMAND_NOP: break;
case ZR_COMMAND_SCISSOR: {
const struct zr_command_scissor *s = zr_command(scissor, cmd);
surface_scissor(surf, s->x, s->y, s->w, s->h);
} break;
case GUI_COMMAND_LINE: {
const struct gui_command_line *l = gui_command(line, cmd);
case ZR_COMMAND_LINE: {
const struct zr_command_line *l = zr_command(line, cmd);
surface_draw_line(surf, l->begin.x, l->begin.y, l->end.x,
l->end.y, l->color);
} break;
case GUI_COMMAND_RECT: {
const struct gui_command_rect *r = gui_command(rect, cmd);
case ZR_COMMAND_RECT: {
const struct zr_command_rect *r = zr_command(rect, cmd);
surface_draw_rect(surf, r->x, r->y, r->w, r->h, r->color);
} break;
case GUI_COMMAND_CIRCLE: {
const struct gui_command_circle *c = gui_command(circle, cmd);
case ZR_COMMAND_CIRCLE: {
const struct zr_command_circle *c = zr_command(circle, cmd);
surface_draw_circle(surf, c->x, c->y, c->w, c->h, c->color);
} break;
case GUI_COMMAND_TRIANGLE: {
const struct gui_command_triangle *t = gui_command(triangle, cmd);
case ZR_COMMAND_TRIANGLE: {
const struct zr_command_triangle *t = zr_command(triangle, cmd);
surface_draw_triangle(surf, t->a.x, t->a.y, t->b.x, t->b.y,
t->c.x, t->c.y, t->color);
} break;
case GUI_COMMAND_TEXT: {
const struct gui_command_text *t = gui_command(text, cmd);
case ZR_COMMAND_TEXT: {
const struct zr_command_text *t = zr_command(text, cmd);
surface_draw_text(surf, t->x, t->y, t->w, t->h, (const char*)t->string,
t->length, (XFont*)t->font->userdata.ptr, t->background, t->foreground);
} break;
case GUI_COMMAND_CURVE:
case GUI_COMMAND_IMAGE:
case GUI_COMMAND_MAX:
case ZR_COMMAND_CURVE:
case ZR_COMMAND_IMAGE:
case ZR_COMMAND_MAX:
default: break;
}
}
gui_command_queue_clear(queue);
zr_command_queue_clear(queue);
}
static void
key(struct XWindow *xw, struct gui_input *in, XEvent *evt, gui_bool down)
key(struct XWindow *xw, struct zr_input *in, XEvent *evt, zr_bool down)
{
int ret;
KeySym *code = XGetKeyboardMapping(xw->dpy, (KeyCode)evt->xkey.keycode, 1, &ret);
if (*code == XK_Shift_L || *code == XK_Shift_R)
gui_input_key(in, GUI_KEY_SHIFT, down);
zr_input_key(in, ZR_KEY_SHIFT, down);
else if (*code == XK_Delete)
gui_input_key(in, GUI_KEY_DEL, down);
zr_input_key(in, ZR_KEY_DEL, down);
else if (*code == XK_Return)
gui_input_key(in, GUI_KEY_ENTER, down);
zr_input_key(in, ZR_KEY_ENTER, down);
else if (*code == XK_space)
gui_input_key(in, GUI_KEY_SPACE, down);
zr_input_key(in, ZR_KEY_SPACE, down);
else if (*code == XK_Left)
gui_input_key(in, GUI_KEY_LEFT, down);
zr_input_key(in, ZR_KEY_LEFT, down);
else if (*code == XK_Right)
gui_input_key(in, GUI_KEY_RIGHT, down);
zr_input_key(in, ZR_KEY_RIGHT, down);
else if (*code == XK_BackSpace)
gui_input_key(in, GUI_KEY_BACKSPACE, down);
zr_input_key(in, ZR_KEY_BACKSPACE, down);
else if (*code > 32 && *code < 128) {
if (*code == 'c')
gui_input_key(in, GUI_KEY_COPY, down && (evt->xkey.state & ControlMask));
zr_input_key(in, ZR_KEY_COPY, down && (evt->xkey.state & ControlMask));
else if (*code == 'v')
gui_input_key(in, GUI_KEY_PASTE, down && (evt->xkey.state & ControlMask));
zr_input_key(in, ZR_KEY_PASTE, down && (evt->xkey.state & ControlMask));
else if (*code == 'x')
gui_input_key(in, GUI_KEY_CUT, down && (evt->xkey.state & ControlMask));
if (!down) gui_input_char(in, (char)*code);
zr_input_key(in, ZR_KEY_CUT, down && (evt->xkey.state & ControlMask));
if (!down) zr_input_char(in, (char)*code);
}
XFree(code);
}
static void
motion(struct gui_input *in, XEvent *evt)
motion(struct zr_input *in, XEvent *evt)
{
const gui_int x = evt->xmotion.x;
const gui_int y = evt->xmotion.y;
gui_input_motion(in, x, y);
const zr_int x = evt->xmotion.x;
const zr_int y = evt->xmotion.y;
zr_input_motion(in, x, y);
}
static void
btn(struct gui_input *in, XEvent *evt, gui_bool down)
btn(struct zr_input *in, XEvent *evt, zr_bool down)
{
const gui_int x = evt->xbutton.x;
const gui_int y = evt->xbutton.y;
const zr_int x = evt->xbutton.x;
const zr_int y = evt->xbutton.y;
if (evt->xbutton.button == Button1)
gui_input_button(in, GUI_BUTTON_LEFT, x, y, down);
zr_input_button(in, ZR_BUTTON_LEFT, x, y, down);
else if (evt->xbutton.button == Button3)
gui_input_button(in, GUI_BUTTON_RIGHT, x, y, down);
zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down);
else if (evt->xbutton.button == Button4)
gui_input_scroll(in, 1.0f);
zr_input_scroll(in, 1.0f);
else if (evt->xbutton.button == Button5)
gui_input_scroll(in, -1.0f);
zr_input_scroll(in, -1.0f);
}
static void
@ -446,9 +446,9 @@ main(int argc, char *argv[])
/* GUI */
memset(&gui, 0, sizeof gui);
gui_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
gui.font.userdata = gui_handle_ptr(xw.font);
gui.font.height = (gui_float)xw.font->height;
zr_buffer_init_fixed(&gui.memory, calloc(MAX_MEMORY, 1), MAX_MEMORY);
gui.font.userdata = zr_handle_ptr(xw.font);
gui.font.height = (zr_float)xw.font->height;
gui.font.width = font_get_text_width;
init_demo(&gui);
@ -456,18 +456,18 @@ main(int argc, char *argv[])
/* Input */
XEvent evt;
started = timestamp();
gui_input_begin(&gui.input);
zr_input_begin(&gui.input);
while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) {
if (evt.type == KeyPress)
key(&xw, &gui.input, &evt, gui_true);
else if (evt.type == KeyRelease) key(&xw, &gui.input, &evt, gui_false);
else if (evt.type == ButtonPress) btn(&gui.input, &evt, gui_true);
else if (evt.type == ButtonRelease) btn(&gui.input, &evt, gui_false);
key(&xw, &gui.input, &evt, zr_true);
else if (evt.type == KeyRelease) key(&xw, &gui.input, &evt, zr_false);
else if (evt.type == ButtonPress) btn(&gui.input, &evt, zr_true);
else if (evt.type == ButtonRelease) btn(&gui.input, &evt, zr_false);
else if (evt.type == MotionNotify) motion(&gui.input, &evt);
else if (evt.type == Expose || evt.type == ConfigureNotify)
resize(&xw, xw.surf);
}
gui_input_end(&gui.input);
zr_input_end(&gui.input);
/* GUI */
run_demo(&gui);
@ -485,7 +485,7 @@ main(int argc, char *argv[])
sleep_for(DTIME - dt);
}
free(gui_buffer_memory(&gui.memory));
free(zr_buffer_memory(&gui.memory));
font_del(xw.dpy, xw.font);
surface_del(xw.surf);
XUnmapWindow(xw.dpy, xw.win);

8093
gui.c

File diff suppressed because it is too large Load Diff

8052
zahnrad.c Normal file

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff