From 138f6b3af5fe636b8e1290f1260ecabcfcbcc34f Mon Sep 17 00:00:00 2001 From: vurtun Date: Sun, 3 Jan 2016 20:23:12 +0100 Subject: [PATCH] some cleaning up --- demo/allegro5_complex/allegro.c | 44 +- demo/allegro5_simple/allegro.c | 44 +- demo/demo.c | 19 +- demo/glfw/glfw.c | 35 +- demo/linuxgl/linuxgl.c | 54 +-- demo/nanovg/nanovg.c | 56 +-- demo/sdl/sdl.c | 54 +-- demo/x11/xlib.c | 54 +-- example/demo/demo.c | 74 ++-- example/filex/filex.c | 102 +++-- example/nodedit/nodedit.c | 58 +-- zahnrad.c | 724 +++++++++++++++++--------------- zahnrad.h | 398 +++++++++--------- 13 files changed, 894 insertions(+), 822 deletions(-) diff --git a/demo/allegro5_complex/allegro.c b/demo/allegro5_complex/allegro.c index 42486c7..44a81a2 100644 --- a/demo/allegro5_complex/allegro.c +++ b/demo/allegro5_complex/allegro.c @@ -273,40 +273,40 @@ device_draw(struct device *dev, struct zr_context *ctx, enum zr_anti_aliasing AA } static void -input_key(struct zr_input *in, ALLEGRO_EVENT *evt, int down) +input_key(struct zr_context *ctx, ALLEGRO_EVENT *evt, int down) { int sym = evt->keyboard.keycode; if (sym == ALLEGRO_KEY_RSHIFT || sym == ALLEGRO_KEY_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == ALLEGRO_KEY_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == ALLEGRO_KEY_ENTER) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == ALLEGRO_KEY_TAB) - zr_input_key(in, ZR_KEY_TAB, down); + zr_input_key(ctx, ZR_KEY_TAB, down); else if (sym == ALLEGRO_KEY_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == ALLEGRO_KEY_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == ALLEGRO_KEY_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == ALLEGRO_KEY_C) - zr_input_key(in, ZR_KEY_COPY, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_COPY, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); else if (sym == ALLEGRO_KEY_V) - zr_input_key(in, ZR_KEY_PASTE, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_PASTE, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); else if (sym == ALLEGRO_KEY_X) - zr_input_key(in, ZR_KEY_CUT, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_CUT, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); } static void -input_button(struct zr_input *in, ALLEGRO_EVENT *evt, int down) +input_button(struct zr_context *ctx, ALLEGRO_EVENT *evt, int down) { const int x = evt->mouse.x; const int y = evt->mouse.y; if (evt->mouse.button == 1) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); if (evt->mouse.button == 2) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); } static void* mem_alloc(zr_handle unused, size_t size) @@ -360,26 +360,26 @@ main(int argc, char *argv[]) while (running) { /* Input */ ALLEGRO_EVENT evt; - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); while (al_get_next_event(dev.queue, &evt)) { if (evt.type == ALLEGRO_EVENT_DISPLAY_CLOSE) goto cleanup; else if (evt.type == ALLEGRO_EVENT_KEY_UP && evt.keyboard.display == dev.display) - input_key(&gui.ctx.input, &evt, zr_false); + input_key(&gui.ctx, &evt, zr_false); else if (evt.type == ALLEGRO_EVENT_KEY_DOWN && evt.keyboard.display == dev.display) - input_key(&gui.ctx.input, &evt, zr_true); + input_key(&gui.ctx, &evt, zr_true); else if (evt.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) - input_button(&gui.ctx.input, &evt, zr_true); + input_button(&gui.ctx, &evt, zr_true); else if (evt.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) - input_button(&gui.ctx.input, &evt, zr_false); + input_button(&gui.ctx, &evt, zr_false); else if (evt.type == ALLEGRO_EVENT_MOUSE_AXES) { - zr_input_motion(&gui.ctx.input, evt.mouse.x, evt.mouse.y); + zr_input_motion(&gui.ctx, evt.mouse.x, evt.mouse.y); } else if (evt.type == ALLEGRO_EVENT_KEY_CHAR) { if (evt.keyboard.display == dev.display) if (evt.keyboard.unichar > 0 && evt.keyboard.unichar < 0x10000) - zr_input_unicode(&gui.ctx.input, (zr_rune)evt.keyboard.unichar); + zr_input_unicode(&gui.ctx, (zr_rune)evt.keyboard.unichar); } } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ running = run_demo(&gui); diff --git a/demo/allegro5_simple/allegro.c b/demo/allegro5_simple/allegro.c index fecb6bf..86b2a9d 100644 --- a/demo/allegro5_simple/allegro.c +++ b/demo/allegro5_simple/allegro.c @@ -119,40 +119,40 @@ font_get_width(zr_handle handle, float height, const char *text, size_t len) } static void -input_key(struct zr_input *in, ALLEGRO_EVENT *evt, int down) +input_key(struct zr_context *ctx, ALLEGRO_EVENT *evt, int down) { int sym = evt->keyboard.keycode; if (sym == ALLEGRO_KEY_RSHIFT || sym == ALLEGRO_KEY_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == ALLEGRO_KEY_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == ALLEGRO_KEY_ENTER) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == ALLEGRO_KEY_TAB) - zr_input_key(in, ZR_KEY_TAB, down); + zr_input_key(ctx, ZR_KEY_TAB, down); else if (sym == ALLEGRO_KEY_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == ALLEGRO_KEY_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == ALLEGRO_KEY_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == ALLEGRO_KEY_C) - zr_input_key(in, ZR_KEY_COPY, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_COPY, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); else if (sym == ALLEGRO_KEY_V) - zr_input_key(in, ZR_KEY_PASTE, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_PASTE, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); else if (sym == ALLEGRO_KEY_X) - zr_input_key(in, ZR_KEY_CUT, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); + zr_input_key(ctx, ZR_KEY_CUT, down && evt->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL); } static void -input_button(struct zr_input *in, ALLEGRO_EVENT *evt, int down) +input_button(struct zr_context *ctx, ALLEGRO_EVENT *evt, int down) { const int x = evt->mouse.x; const int y = evt->mouse.y; if (evt->mouse.button == 1) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); if (evt->mouse.button == 2) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); } static void* mem_alloc(zr_handle unused, size_t size) @@ -211,26 +211,26 @@ main(int argc, char *argv[]) while (running) { /* Input */ ALLEGRO_EVENT evt; - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); while (al_get_next_event(queue, &evt)) { if (evt.type == ALLEGRO_EVENT_DISPLAY_CLOSE) goto cleanup; else if (evt.type == ALLEGRO_EVENT_KEY_UP && evt.keyboard.display == display) - input_key(&gui.ctx.input, &evt, zr_false); + input_key(&gui.ctx, &evt, zr_false); else if (evt.type == ALLEGRO_EVENT_KEY_DOWN && evt.keyboard.display == display) - input_key(&gui.ctx.input, &evt, zr_true); + input_key(&gui.ctx, &evt, zr_true); else if (evt.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) - input_button(&gui.ctx.input, &evt, zr_true); + input_button(&gui.ctx, &evt, zr_true); else if (evt.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP) - input_button(&gui.ctx.input, &evt, zr_false); + input_button(&gui.ctx, &evt, zr_false); else if (evt.type == ALLEGRO_EVENT_MOUSE_AXES) { - zr_input_motion(&gui.ctx.input, evt.mouse.x, evt.mouse.y); + zr_input_motion(&gui.ctx, evt.mouse.x, evt.mouse.y); } else if (evt.type == ALLEGRO_EVENT_KEY_CHAR) { if (evt.keyboard.display == display) if (evt.keyboard.unichar > 0 && evt.keyboard.unichar < 0x10000) - zr_input_unicode(&gui.ctx.input, (zr_rune)evt.keyboard.unichar); + zr_input_unicode(&gui.ctx, (zr_rune)evt.keyboard.unichar); } } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ width = al_get_display_width(display); diff --git a/demo/demo.c b/demo/demo.c index e88bd4f..f80142c 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -79,8 +79,7 @@ set_style(struct zr_context *ctx, enum theme theme) ctx->style.colors[ZR_COLOR_SCALER] = zr_rgba(100, 100, 100, 255); } else { - struct zr_user_font fnt = ctx->style.font; - zr_style_default(&ctx->style, ZR_DEFAULT_ALL, &fnt); + zr_load_default_style(ctx, ZR_DEFAULT_ALL); } } @@ -204,7 +203,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) ZR_WINDOW_CLOSEABLE, zr_rect(10, 100, 360, 280))) { zr_layout_row_dynamic(ctx, 30, 2); - zr_label(ctx, zr_style_color_name((enum zr_style_colors)color_picker_index), ZR_TEXT_LEFT); + zr_label(ctx, zr_get_color_name((enum zr_style_colors)color_picker_index), ZR_TEXT_LEFT); zr_button_color(ctx, color_picker_color, ZR_BUTTON_DEFAULT); /* color selection */ @@ -281,7 +280,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) size_t i = 0; zr_layout_row_dynamic(ctx, 30, 3); for (i = 0; i <= ZR_PROPERTY_SCROLLBAR_SIZE; ++i) { - zr_label(ctx, zr_style_property_name((enum zr_style_properties)i), ZR_TEXT_LEFT); + zr_label(ctx, zr_get_property_name((enum zr_style_properties)i), ZR_TEXT_LEFT); zr_property_float(ctx, "#X:", 0, &ctx->style.properties[i].x, 20, 1, 1); zr_property_float(ctx, "#Y:", 0, &ctx->style.properties[i].y, 20, 1, 1); } @@ -292,7 +291,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) size_t i = 0; zr_layout_row_dynamic(ctx, 30, 2); for (i = 0; i < ZR_ROUNDING_MAX; ++i) { - zr_label(ctx, zr_style_rounding_name((enum zr_style_rounding)i), ZR_TEXT_LEFT); + zr_label(ctx, zr_get_rounding_name((enum zr_style_rounding)i), ZR_TEXT_LEFT); zr_property_float(ctx, "#R:", 0, &ctx->style.rounding[i], 20, 1, 1); } zr_layout_pop(ctx); @@ -307,7 +306,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) if (zr_group_begin(ctx, &tab, "Color_Picker", 0)) { for (i = 0; i < ZR_COLOR_COUNT; ++i) { zr_layout_row_dynamic(ctx, 30, 2); - zr_label(ctx, zr_style_color_name((enum zr_style_colors)i), ZR_TEXT_LEFT); + zr_label(ctx, zr_get_color_name((enum zr_style_colors)i), ZR_TEXT_LEFT); if (zr_button_color(ctx, ctx->style.colors[i], ZR_BUTTON_DEFAULT)) { show_color_picker_popup = zr_true; color_picker_index = (int)i; @@ -728,7 +727,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) /* tiles */ zr_layout_row(ctx, ZR_STATIC, 200, 5, row_layout); - zr_style_push_property(&ctx->style, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); /* left space */ if (zr_group_begin(ctx, &sub, "left", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER)) { @@ -770,7 +769,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) zr_group_end(ctx); } - zr_style_pop_property(&ctx->style); + zr_pop_property(ctx); zr_layout_pop(ctx); } @@ -791,7 +790,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) zr_label(ctx, "bottom:", ZR_TEXT_LEFT); zr_slider_float(ctx, 10.0f, &c, 200.0f, 10.0f); - zr_style_push_property(&ctx->style, ZR_PROPERTY_ITEM_SPACING, zr_vec2(4, 0)); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(4, 0)); /* top space */ zr_layout_row_dynamic(ctx, a, 1); @@ -838,7 +837,7 @@ demo_window(struct zr_layout *layout, struct zr_context *ctx, enum theme *theme) zr_group_end(ctx); } - zr_style_pop_property(&ctx->style); + zr_pop_property(ctx); zr_layout_pop(ctx); } zr_layout_pop(ctx); diff --git a/demo/glfw/glfw.c b/demo/glfw/glfw.c index 7caf8e5..8ea1fca 100644 --- a/demo/glfw/glfw.c +++ b/demo/glfw/glfw.c @@ -380,25 +380,25 @@ input_key(GLFWwindow *window, int key, int scancode, int action, int mods) UNUSED(window); UNUSED(scancode); if (key == GLFW_KEY_RIGHT_SHIFT || key == GLFW_KEY_LEFT_SHIFT) - zr_input_key(&gui.ctx.input, ZR_KEY_SHIFT, down); + zr_input_key(&gui.ctx, ZR_KEY_SHIFT, down); else if (key == GLFW_KEY_DELETE) - zr_input_key(&gui.ctx.input, ZR_KEY_DEL, down); + zr_input_key(&gui.ctx, ZR_KEY_DEL, down); else if (key == GLFW_KEY_ENTER) - zr_input_key(&gui.ctx.input, ZR_KEY_ENTER, down); + zr_input_key(&gui.ctx, ZR_KEY_ENTER, down); else if (key == GLFW_KEY_TAB) - zr_input_key(&gui.ctx.input, ZR_KEY_TAB, down); + zr_input_key(&gui.ctx, ZR_KEY_TAB, down); else if (key == GLFW_KEY_BACKSPACE) - zr_input_key(&gui.ctx.input, ZR_KEY_BACKSPACE, down); + zr_input_key(&gui.ctx, ZR_KEY_BACKSPACE, down); else if (key == GLFW_KEY_LEFT) - zr_input_key(&gui.ctx.input, ZR_KEY_LEFT, down); + zr_input_key(&gui.ctx, ZR_KEY_LEFT, down); else if (key == GLFW_KEY_RIGHT) - zr_input_key(&gui.ctx.input, ZR_KEY_RIGHT, down); + zr_input_key(&gui.ctx, ZR_KEY_RIGHT, down); else if (key == GLFW_KEY_C) - zr_input_key(&gui.ctx.input, ZR_KEY_COPY, down && (mods & GLFW_MOD_CONTROL)); + zr_input_key(&gui.ctx, ZR_KEY_COPY, down && (mods & GLFW_MOD_CONTROL)); else if (key == GLFW_KEY_V) - zr_input_key(&gui.ctx.input, ZR_KEY_PASTE, down && (mods & GLFW_MOD_CONTROL)); + zr_input_key(&gui.ctx, ZR_KEY_PASTE, down && (mods & GLFW_MOD_CONTROL)); else if (key == GLFW_KEY_X) - zr_input_key(&gui.ctx.input, ZR_KEY_CUT, down && (mods & GLFW_MOD_CONTROL)); + zr_input_key(&gui.ctx, ZR_KEY_CUT, down && (mods & GLFW_MOD_CONTROL)); } static void @@ -409,28 +409,27 @@ input_motion(GLFWwindow *window, double xpos, double ypos) UNUSED(window); mouse_pos_x = x; mouse_pos_y = y; - zr_input_motion(&gui.ctx.input, x, y); + zr_input_motion(&gui.ctx, x, y); } static void input_button(GLFWwindow *window, int button, int action, int mods) { - struct zr_input *in = &gui.ctx.input; int x = mouse_pos_x; int y = mouse_pos_y; UNUSED(window); UNUSED(mods); if (button == 0) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, action == GLFW_PRESS); + zr_input_button(&gui.ctx, ZR_BUTTON_LEFT, x, y, action == GLFW_PRESS); if (button == 1) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, action == GLFW_PRESS); + zr_input_button(&gui.ctx, ZR_BUTTON_RIGHT, x, y, action == GLFW_PRESS); } static void input_text(GLFWwindow *window, unsigned int codepoint) { UNUSED(window); - zr_input_unicode(&gui.ctx.input, codepoint); + zr_input_unicode(&gui.ctx, codepoint); } static void @@ -438,7 +437,7 @@ input_scroll(GLFWwindow *window, double xoffset, double yoffset) { UNUSED(window); UNUSED(xoffset); - zr_input_scroll(&gui.ctx.input, (float)yoffset); + zr_input_scroll(&gui.ctx, (float)yoffset); } static void* mem_alloc(zr_handle unused, size_t size) @@ -505,9 +504,9 @@ main(int argc, char *argv[]) device_init(&device); while (!glfwWindowShouldClose(win) && running) { /* Input */ - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); glfwPollEvents(); - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ glfwGetWindowSize(win, &width, &height); diff --git a/demo/linuxgl/linuxgl.c b/demo/linuxgl/linuxgl.c index 6ea8f1e..f28e0ca 100644 --- a/demo/linuxgl/linuxgl.c +++ b/demo/linuxgl/linuxgl.c @@ -544,60 +544,60 @@ device_draw(struct device *dev, struct zr_context *ctx, int width, int height, static void -input_key(struct XWindow *xw, struct zr_input *in, XEvent *evt, int down) +input_key(struct XWindow *xw, struct zr_context *ctx, XEvent *evt, int down) { int ret; KeySym *code = XGetKeyboardMapping(xw->dpy, (KeyCode)evt->xkey.keycode, 1, &ret); if (*code == XK_Shift_L || *code == XK_Shift_R) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (*code == XK_Delete) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (*code == XK_Return) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (*code == XK_Tab) - zr_input_key(in, ZR_KEY_TAB, down); + zr_input_key(ctx, ZR_KEY_TAB, down); else if (*code == XK_space && !down) - zr_input_char(in, ' '); + zr_input_char(ctx, ' '); else if (*code == XK_Left) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (*code == XK_Right) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (*code == XK_BackSpace) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (*code > 32 && *code < 128) { if (*code == 'c') - zr_input_key(in, ZR_KEY_COPY, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_COPY, down && (evt->xkey.state & ControlMask)); else if (*code == 'v') - zr_input_key(in, ZR_KEY_PASTE, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_PASTE, down && (evt->xkey.state & ControlMask)); else if (*code == 'x') - zr_input_key(in, ZR_KEY_CUT, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_CUT, down && (evt->xkey.state & ControlMask)); if (!down) - zr_input_unicode(in, (zr_rune)*code); + zr_input_unicode(ctx, (zr_rune)*code); } XFree(code); } static void -input_motion(struct zr_input *in, XEvent *evt) +input_motion(struct zr_context *ctx, XEvent *evt) { const int x = evt->xmotion.x; const int y = evt->xmotion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -input_button(struct zr_input *in, XEvent *evt, int down) +input_button(struct zr_context *ctx, XEvent *evt, int down) { const int x = evt->xbutton.x; const int y = evt->xbutton.y; if (evt->xbutton.button == Button1) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); else if (evt->xbutton.button == Button3) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); else if (evt->xbutton.button == Button4) - zr_input_scroll(in, 1.0f); + zr_input_scroll(ctx, 1.0f); else if (evt->xbutton.button == Button5) - zr_input_scroll(in, -1.0f); + zr_input_scroll(ctx, -1.0f); } static void* mem_alloc(zr_handle unused, size_t size) @@ -862,25 +862,25 @@ int main(int argc, char **argv) while (running) { /* input */ XEvent evt; - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); while (XCheckWindowEvent(win.dpy, win.win, win.swa.event_mask, &evt)) { if (evt.type == KeyPress) - input_key(&win, &gui.ctx.input, &evt, zr_true); + input_key(&win, &gui.ctx, &evt, zr_true); else if (evt.type == KeyRelease) - input_key(&win, &gui.ctx.input, &evt, zr_false); + input_key(&win, &gui.ctx, &evt, zr_false); else if (evt.type == ButtonPress) - input_button(&gui.ctx.input, &evt, zr_true); + input_button(&gui.ctx, &evt, zr_true); else if (evt.type == ButtonRelease) - input_button(&gui.ctx.input, &evt, zr_false); + input_button(&gui.ctx, &evt, zr_false); else if (evt.type == MotionNotify) - input_motion(&gui.ctx.input, &evt); + input_motion(&gui.ctx, &evt); else if (evt.type == Expose || evt.type == ConfigureNotify) { XGetWindowAttributes(win.dpy, win.win, &win.attr); win.width = win.attr.width; win.height = win.attr.height; } } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ XGetWindowAttributes(win.dpy, win.win, &win.attr); diff --git a/demo/nanovg/nanovg.c b/demo/nanovg/nanovg.c index d5a984f..8c9d12c 100644 --- a/demo/nanovg/nanovg.c +++ b/demo/nanovg/nanovg.c @@ -177,55 +177,57 @@ draw(NVGcontext *nvg, struct zr_context *ctx, int width, int height) } static void -key(struct zr_input *in, SDL_Event *evt, int down) +input_key(struct zr_context *ctx, SDL_Event *evt, int down) { const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == SDLK_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == SDLK_RETURN) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); + else if (sym == SDLK_TAB) + zr_input_key(ctx, ZR_KEY_TAB, down); else if (sym == SDLK_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == SDLK_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == SDLK_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == SDLK_c) - zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_v) - zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_x) - zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); } static void -motion(struct zr_input *in, SDL_Event *evt) +input_motion(struct zr_context *ctx, SDL_Event *evt) { const int x = evt->motion.x; const int y = evt->motion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -btn(struct zr_input *in, SDL_Event *evt, int down) +input_button(struct zr_context *ctx, SDL_Event *evt, int down) { const int x = evt->button.x; const int y = evt->button.y; if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); - else if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); + if (evt->button.button == SDL_BUTTON_RIGHT) + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); } static void -text(struct zr_input *in, SDL_Event *evt) +input_text(struct zr_context *ctx, SDL_Event *evt) { zr_glyph glyph; memcpy(glyph, evt->text.text, ZR_UTF_SIZE); - zr_input_glyph(in, glyph); + zr_input_glyph(ctx, glyph); } static void @@ -299,7 +301,7 @@ main(int argc, char *argv[]) uint64_t dt, started = SDL_GetTicks(); - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); if (!poll) { ret = SDL_WaitEvent(&evt); poll = 1; @@ -307,17 +309,17 @@ main(int argc, char *argv[]) while (ret) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; - else if (evt.type == SDL_KEYUP) key(&gui.ctx.input, &evt, zr_false); - else if (evt.type == SDL_KEYDOWN) key(&gui.ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&gui.ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONUP) btn(&gui.ctx.input, &evt, zr_false); - else if (evt.type == SDL_MOUSEMOTION) motion(&gui.ctx.input, &evt); - else if (evt.type == SDL_TEXTINPUT) text(&gui.ctx.input, &evt); + else if (evt.type == SDL_KEYUP) input_key(&gui.ctx, &evt, zr_false); + else if (evt.type == SDL_KEYDOWN) input_key(&gui.ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONDOWN) input_button(&gui.ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONUP) input_button(&gui.ctx, &evt, zr_false); + else if (evt.type == SDL_MOUSEMOTION) input_motion(&gui.ctx, &evt); + else if (evt.type == SDL_TEXTINPUT) input_text(&gui.ctx, &evt); else if (evt.type == SDL_MOUSEWHEEL) - zr_input_scroll(&gui.ctx.input,(float)evt.wheel.y); + zr_input_scroll(&gui.ctx,(float)evt.wheel.y); ret = SDL_PollEvent(&evt); } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ SDL_GetWindowSize(win, &width, &height); diff --git a/demo/sdl/sdl.c b/demo/sdl/sdl.c index f58a6fc..2638469 100644 --- a/demo/sdl/sdl.c +++ b/demo/sdl/sdl.c @@ -359,57 +359,57 @@ device_draw(struct device *dev, struct zr_context *ctx, int width, int height, } static void -input_key(struct zr_input *in, SDL_Event *evt, int down) +input_key(struct zr_context *ctx, SDL_Event *evt, int down) { const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == SDLK_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == SDLK_RETURN) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == SDLK_TAB) - zr_input_key(in, ZR_KEY_TAB, down); + zr_input_key(ctx, ZR_KEY_TAB, down); else if (sym == SDLK_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == SDLK_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == SDLK_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == SDLK_c) - zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_v) - zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_x) - zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); } static void -input_motion(struct zr_input *in, SDL_Event *evt) +input_motion(struct zr_context *ctx, SDL_Event *evt) { const int x = evt->motion.x; const int y = evt->motion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -input_button(struct zr_input *in, SDL_Event *evt, int down) +input_button(struct zr_context *ctx, SDL_Event *evt, int down) { const int x = evt->button.x; const int y = evt->button.y; if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); if (evt->button.button == SDL_BUTTON_RIGHT) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); } static void -input_text(struct zr_input *in, SDL_Event *evt) +input_text(struct zr_context *ctx, SDL_Event *evt) { zr_glyph glyph; memcpy(glyph, evt->text.text, ZR_UTF_SIZE); - zr_input_glyph(in, glyph); + zr_input_glyph(ctx, glyph); } static void @@ -476,26 +476,26 @@ main(int argc, char *argv[]) while (running) { /* Input */ SDL_Event evt; - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); 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) - input_key(&gui.ctx.input, &evt, zr_false); + input_key(&gui.ctx, &evt, zr_false); else if (evt.type == SDL_KEYDOWN) - input_key(&gui.ctx.input, &evt, zr_true); + input_key(&gui.ctx, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONDOWN) - input_button(&gui.ctx.input, &evt, zr_true); + input_button(&gui.ctx, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONUP) - input_button(&gui.ctx.input, &evt, zr_false); + input_button(&gui.ctx, &evt, zr_false); else if (evt.type == SDL_MOUSEMOTION) - input_motion(&gui.ctx.input, &evt); + input_motion(&gui.ctx, &evt); else if (evt.type == SDL_TEXTINPUT) - input_text(&gui.ctx.input, &evt); + input_text(&gui.ctx, &evt); else if (evt.type == SDL_MOUSEWHEEL) - zr_input_scroll(&gui.ctx.input,(float)evt.wheel.y); + zr_input_scroll(&gui.ctx,(float)evt.wheel.y); } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ SDL_GetWindowSize(win, &width, &height); diff --git a/demo/x11/xlib.c b/demo/x11/xlib.c index 60f8287..49a14e3 100644 --- a/demo/x11/xlib.c +++ b/demo/x11/xlib.c @@ -314,60 +314,60 @@ surface_del(XSurface *surf) } static void -input_key(struct XWindow *xw, struct zr_input *in, XEvent *evt, int down) +input_key(struct XWindow *xw, struct zr_context *ctx, XEvent *evt, int down) { int ret; KeySym *code = XGetKeyboardMapping(xw->dpy, (KeyCode)evt->xkey.keycode, 1, &ret); if (*code == XK_Shift_L || *code == XK_Shift_R) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (*code == XK_Delete) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (*code == XK_Return) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (*code == XK_Tab) - zr_input_key(in, ZR_KEY_TAB, down); + zr_input_key(ctx, ZR_KEY_TAB, down); else if (*code == XK_space && !down) - zr_input_char(in, ' '); + zr_input_char(ctx, ' '); else if (*code == XK_Left) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (*code == XK_Right) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (*code == XK_BackSpace) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (*code > 32 && *code < 128) { if (*code == 'c') - zr_input_key(in, ZR_KEY_COPY, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_COPY, down && (evt->xkey.state & ControlMask)); else if (*code == 'v') - zr_input_key(in, ZR_KEY_PASTE, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_PASTE, down && (evt->xkey.state & ControlMask)); else if (*code == 'x') - zr_input_key(in, ZR_KEY_CUT, down && (evt->xkey.state & ControlMask)); + zr_input_key(ctx, ZR_KEY_CUT, down && (evt->xkey.state & ControlMask)); if (!down) - zr_input_unicode(in, (zr_rune)*code); + zr_input_unicode(ctx, (zr_rune)*code); } XFree(code); } static void -input_motion(struct zr_input *in, XEvent *evt) +input_motion(struct zr_context *ctx, XEvent *evt) { const int x = evt->xmotion.x; const int y = evt->xmotion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -input_button(struct zr_input *in, XEvent *evt, int down) +input_button(struct zr_context *ctx, XEvent *evt, int down) { const int x = evt->xbutton.x; const int y = evt->xbutton.y; if (evt->xbutton.button == Button1) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); else if (evt->xbutton.button == Button3) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); else if (evt->xbutton.button == Button4) - zr_input_scroll(in, 1.0f); + zr_input_scroll(ctx, 1.0f); else if (evt->xbutton.button == Button5) - zr_input_scroll(in, -1.0f); + zr_input_scroll(ctx, -1.0f); } static void @@ -426,22 +426,22 @@ main(int argc, char *argv[]) /* Input */ XEvent evt; started = timestamp(); - zr_input_begin(&gui.ctx.input); + zr_input_begin(&gui.ctx); while (XCheckWindowEvent(xw.dpy, xw.win, xw.swa.event_mask, &evt)) { if (evt.type == KeyPress) - input_key(&xw, &gui.ctx.input, &evt, zr_true); + input_key(&xw, &gui.ctx, &evt, zr_true); else if (evt.type == KeyRelease) - input_key(&xw, &gui.ctx.input, &evt, zr_false); + input_key(&xw, &gui.ctx, &evt, zr_false); else if (evt.type == ButtonPress) - input_button(&gui.ctx.input, &evt, zr_true); + input_button(&gui.ctx, &evt, zr_true); else if (evt.type == ButtonRelease) - input_button(&gui.ctx.input, &evt, zr_false); + input_button(&gui.ctx, &evt, zr_false); else if (evt.type == MotionNotify) - input_motion(&gui.ctx.input, &evt); + input_motion(&gui.ctx, &evt); else if (evt.type == Expose || evt.type == ConfigureNotify) resize(&xw, xw.surf); } - zr_input_end(&gui.ctx.input); + zr_input_end(&gui.ctx); /* GUI */ running = run_demo(&gui); diff --git a/example/demo/demo.c b/example/demo/demo.c index d4db210..62f305b 100644 --- a/example/demo/demo.c +++ b/example/demo/demo.c @@ -90,8 +90,8 @@ die(const char *fmt, ...) static void ui_header(struct zr_context *ctx, const char *title) { - zr_style_reset_font_height(&ctx->style); - zr_style_push_font_height(&ctx->style, 18); + zr_reset_font_height(ctx); + zr_push_font_height(ctx, 18); zr_layout_row_dynamic(ctx, 20, 1); zr_label(ctx, title, ZR_TEXT_LEFT); } @@ -100,8 +100,8 @@ static void ui_widget(struct zr_context *ctx, float height, float font_height) { static const float ratio[] = {0.15f, 0.85f}; - zr_style_reset_font_height(&ctx->style); - zr_style_push_font_height(&ctx->style, font_height); + zr_reset_font_height(ctx); + zr_push_font_height(ctx, font_height); zr_layout_row(ctx, ZR_DYNAMIC, height, 2, ratio); zr_spacing(ctx, 1); } @@ -110,8 +110,8 @@ static void ui_widget_centered(struct zr_context *ctx, float height, float font_height) { static const float ratio[] = {0.15f, 0.50f, 0.35f}; - zr_style_reset_font_height(&ctx->style); - zr_style_push_font_height(&ctx->style, font_height); + zr_reset_font_height(ctx); + zr_push_font_height(ctx, font_height); zr_layout_row(ctx, ZR_DYNAMIC, height, 3, ratio); zr_spacing(ctx, 1); } @@ -128,9 +128,9 @@ ui_piemenu(struct zr_context *ctx, /* hide popup background */ struct zr_color border; - zr_style_push_color(&ctx->style, ZR_COLOR_WINDOW, zr_rgba(0,0,0,0)); + zr_push_color(ctx, ZR_COLOR_WINDOW, zr_rgba(0,0,0,0)); border = ctx->style.colors[ZR_COLOR_BORDER]; - zr_style_push_color(&ctx->style, ZR_COLOR_BORDER, zr_rgba(0,0,0,0)); + zr_push_color(ctx, ZR_COLOR_BORDER, zr_rgba(0,0,0,0)); if (ctx->current != ctx->active) return 0; /* pie menu popup */ @@ -213,8 +213,8 @@ ui_piemenu(struct zr_context *ctx, zr_layout_space_end(ctx); zr_popup_end(ctx); - zr_style_reset_colors(&ctx->style); - zr_style_reset_properties(&ctx->style); + zr_reset_colors(ctx); + zr_reset_properties(ctx); if (!zr_input_is_mouse_down(&ctx->input, ZR_BUTTON_RIGHT)) return active_item; @@ -611,57 +611,57 @@ draw(NVGcontext *nvg, struct zr_context *ctx, int width, int height) static void -key(struct zr_input *in, SDL_Event *evt, int down) +key(struct zr_context *ctx, SDL_Event *evt, int down) { const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == SDLK_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == SDLK_RETURN) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == SDLK_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == SDLK_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == SDLK_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == SDLK_c) - zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_v) - zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_x) - zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); } static void -motion(struct zr_input *in, SDL_Event *evt) +motion(struct zr_context *ctx, SDL_Event *evt) { const int x = evt->motion.x; const int y = evt->motion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -btn(struct zr_input *in, SDL_Event *evt, int down) +btn(struct zr_context *ctx, SDL_Event *evt, int down) { const int x = evt->button.x; const int y = evt->button.y; if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); else if (evt->button.button == SDL_BUTTON_RIGHT) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); else if (evt->button.button == SDL_BUTTON_MIDDLE) - zr_input_button(in, ZR_BUTTON_MIDDLE, x, y, down); + zr_input_button(ctx, ZR_BUTTON_MIDDLE, x, y, down); } static void -text(struct zr_input *in, SDL_Event *evt) +text(struct zr_context *ctx, SDL_Event *evt) { zr_glyph glyph; memcpy(glyph, evt->text.text, ZR_UTF_SIZE); - zr_input_glyph(in, glyph); + zr_input_glyph(ctx, glyph); } int @@ -765,7 +765,7 @@ main(int argc, char *argv[]) int ret; SDL_Event evt; started = SDL_GetTicks(); - zr_input_begin(&ctx.input); + zr_input_begin(&ctx); if (!poll) { ret = SDL_WaitEvent(&evt); @@ -778,22 +778,22 @@ main(int argc, char *argv[]) glViewport(0, 0, evt.window.data1, evt.window.data2); else if (evt.type == SDL_QUIT) goto cleanup; else if (evt.type == SDL_KEYUP) - key(&ctx.input, &evt, zr_false); + key(&ctx, &evt, zr_false); else if (evt.type == SDL_KEYDOWN) - key(&ctx.input, &evt, zr_true); + key(&ctx, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONDOWN) - btn(&ctx.input, &evt, zr_true); + btn(&ctx, &evt, zr_true); else if (evt.type == SDL_MOUSEBUTTONUP) - btn(&ctx.input, &evt, zr_false); + btn(&ctx, &evt, zr_false); else if (evt.type == SDL_MOUSEMOTION) - motion(&ctx.input, &evt); + motion(&ctx, &evt); else if (evt.type == SDL_TEXTINPUT) - text(&ctx.input, &evt); + text(&ctx, &evt); else if (evt.type == SDL_MOUSEWHEEL) - zr_input_scroll(&ctx.input, evt.wheel.y); + zr_input_scroll(&ctx, evt.wheel.y); ret = SDL_PollEvent(&evt); } - zr_input_end(&ctx.input); + zr_input_end(&ctx); /* GUI */ button_demo(&ctx, &icons); diff --git a/example/filex/filex.c b/example/filex/filex.c index 569149c..2454f22 100644 --- a/example/filex/filex.c +++ b/example/filex/filex.c @@ -457,13 +457,14 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width { struct zr_layout sub; float row_layout[3]; + /* output path directory selector in the menubar */ zr_menubar_begin(ctx); { char *d = browser->directory; char *begin = d + 1; zr_layout_row_dynamic(ctx, 25, 6); - zr_style_push_property(&ctx->style, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); while (*d++) { if (*d == '/') { *d = '\0'; @@ -476,7 +477,7 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width begin = d + 1; } } - zr_style_pop_property(&ctx->style); + zr_pop_property(ctx); } zr_menubar_end(ctx); @@ -486,9 +487,10 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width row_layout[1] = 8; row_layout[2] = (total_space.w - 8) * browser->ratio_dir; zr_layout_row(ctx, ZR_STATIC, total_space.h, 3, row_layout); - zr_style_push_property(&ctx->style, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 4)); /* output special important directory list in own window */ + /* TODO: maybe allow to add current directory into list? */ zr_group_begin(ctx, &sub, "Special", ZR_WINDOW_NO_SCROLLBAR|ZR_WINDOW_BORDER); { struct zr_image home = icons->home.img; @@ -496,14 +498,14 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width struct zr_image computer = icons->computer.img; zr_layout_row_dynamic(ctx, 40, 1); - zr_style_push_property(&ctx->style, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 0)); + zr_push_property(ctx, ZR_PROPERTY_ITEM_SPACING, zr_vec2(0, 0)); if (zr_button_text_image(ctx, home, "home", ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) file_browser_reload_directory_content(browser, browser->home); if (zr_button_text_image(ctx,desktop,"desktop",ZR_TEXT_CENTERED, ZR_BUTTON_DEFAULT)) file_browser_reload_directory_content(browser, browser->desktop); if (zr_button_text_image(ctx,computer,"computer",ZR_TEXT_CENTERED,ZR_BUTTON_DEFAULT)) file_browser_reload_directory_content(browser, "/"); - zr_style_pop_property(&ctx->style); + zr_pop_property(ctx); zr_group_end(ctx); } @@ -539,8 +541,8 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width /* draw one row of icons */ size_t n = j + cols; zr_layout_row_dynamic(ctx, 135, cols); - zr_style_push_color(&ctx->style, ZR_COLOR_BUTTON, zr_rgb(45, 45, 45)); - zr_style_push_color(&ctx->style, ZR_COLOR_BORDER, zr_rgb(45, 45, 45)); + zr_push_color(ctx, ZR_COLOR_BUTTON, zr_rgb(45, 45, 45)); + zr_push_color(ctx, ZR_COLOR_BORDER, zr_rgb(45, 45, 45)); for (; j < count && j < n; ++j) { if (j < browser->dir_count) { /* draw and execute directory buttons */ @@ -559,8 +561,8 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width } } } - zr_style_pop_color(&ctx->style); - zr_style_pop_color(&ctx->style); + zr_pop_color(ctx); + zr_pop_color(ctx); } { /* draw one row of labels */ @@ -589,14 +591,14 @@ file_browser_run(struct file_browser *browser, struct zr_context *ctx, int width } zr_group_end(ctx); } - zr_style_pop_property(&ctx->style); + zr_pop_property(ctx); } zr_end(ctx); return 1; } /* ================================================================= * - * APP + * BACKEND * * ================================================================= */ static size_t @@ -713,64 +715,49 @@ draw(NVGcontext *nvg, struct zr_context *ctx, int width, int height) } static void -key(struct zr_input *in, SDL_Event *evt, int down) +key(struct zr_context *ctx, SDL_Event *evt, int down) { const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == SDLK_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == SDLK_RETURN) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == SDLK_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == SDLK_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == SDLK_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == SDLK_c) - zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_v) - zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_x) - zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); } static void -motion(struct zr_input *in, SDL_Event *evt) -{ - const int x = evt->motion.x; - const int y = evt->motion.y; - zr_input_motion(in, x, y); -} - -static void -btn(struct zr_input *in, SDL_Event *evt, int down) +btn(struct zr_context *ctx, SDL_Event *evt, int down) { const int x = evt->button.x; const int y = evt->button.y; if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); else if (evt->button.button == SDL_BUTTON_RIGHT) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); else if (evt->button.button == SDL_BUTTON_MIDDLE) - zr_input_button(in, ZR_BUTTON_MIDDLE, x, y, down); + zr_input_button(ctx, ZR_BUTTON_MIDDLE, x, y, down); } static void -text(struct zr_input *in, SDL_Event *evt) +text(struct zr_context *ctx, SDL_Event *evt) { zr_glyph glyph; memcpy(glyph, evt->text.text, ZR_UTF_SIZE); - zr_input_glyph(in, glyph); -} - -static void -resize(SDL_Event *evt) -{ - if (evt->window.event != SDL_WINDOWEVENT_RESIZED) return; - glViewport(0, 0, evt->window.data1, evt->window.data2); + zr_input_glyph(ctx, glyph); } int @@ -831,25 +818,34 @@ main(int argc, char *argv[]) /* Input */ int ret; SDL_Event evt; - zr_input_begin(&ctx.input); + zr_input_begin(&ctx); if (!poll) { ret = SDL_WaitEvent(&evt); poll = 1; } else ret = SDL_PollEvent(&evt); while (ret) { - if (evt.type == SDL_WINDOWEVENT) resize(&evt); - else if (evt.type == SDL_QUIT) goto cleanup; - else if (evt.type == SDL_KEYUP) key(&ctx.input, &evt, zr_false); - else if (evt.type == SDL_KEYDOWN) key(&ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONUP) btn(&ctx.input, &evt, zr_false); - else if (evt.type == SDL_MOUSEMOTION) motion(&ctx.input, &evt); - else if (evt.type == SDL_TEXTINPUT) text(&ctx.input, &evt); - else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx.input, evt.wheel.y); + if (evt.type == SDL_QUIT) goto cleanup; + else if (evt.type == SDL_WINDOWEVENT) { + if (evt.window.event == SDL_WINDOWEVENT_RESIZED) + glViewport(0, 0, evt.window.data1, evt.window.data2); + } else if (evt.type == SDL_KEYUP) + key(&ctx, &evt, zr_false); + else if (evt.type == SDL_KEYDOWN) + key(&ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONDOWN) + btn(&ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONUP) + btn(&ctx, &evt, zr_false); + else if (evt.type == SDL_MOUSEMOTION) { + zr_input_motion(&ctx, evt.motion.x, evt.motion.y); + } else if (evt.type == SDL_TEXTINPUT) + text(&ctx, &evt); + else if (evt.type == SDL_MOUSEWHEEL) + zr_input_scroll(&ctx, evt.wheel.y); ret = SDL_PollEvent(&evt); } - zr_input_end(&ctx.input); + zr_input_end(&ctx); SDL_GetWindowSize(win, &width, &height); running = file_browser_run(&browser, &ctx, width, height); diff --git a/example/nodedit/nodedit.c b/example/nodedit/nodedit.c index 62eb4ca..9649505 100644 --- a/example/nodedit/nodedit.c +++ b/example/nodedit/nodedit.c @@ -208,7 +208,7 @@ node_editor_draw(struct zr_context *ctx, struct node_editor *nodedit) } /* execute each node as a moveable group */ - zr_style_push_color(&ctx->style, ZR_COLOR_WINDOW, zr_rgb(48, 48, 48)); + zr_push_color(ctx, ZR_COLOR_WINDOW, zr_rgb(48, 48, 48)); while (it) { /* calculate scrolled node window position and size */ @@ -325,7 +325,7 @@ node_editor_draw(struct zr_context *ctx, struct node_editor *nodedit) zr_draw_curve(canvas, l0.x, l0.y, l0.x + 50.0f, l0.y, l1.x - 50.0f, l1.y, l1.x, l1.y, zr_rgb(100, 100, 100)); } - zr_style_pop_color(&ctx->style); + zr_pop_color(ctx); if (updated) { /* reshuffle nodes to have last recently selected node on top */ @@ -510,57 +510,57 @@ draw(NVGcontext *nvg, struct zr_context *ctx, int width, int height) } static void -key(struct zr_input *in, SDL_Event *evt, int down) +key(struct zr_context *ctx, SDL_Event *evt, int down) { const Uint8* state = SDL_GetKeyboardState(NULL); SDL_Keycode sym = evt->key.keysym.sym; if (sym == SDLK_RSHIFT || sym == SDLK_LSHIFT) - zr_input_key(in, ZR_KEY_SHIFT, down); + zr_input_key(ctx, ZR_KEY_SHIFT, down); else if (sym == SDLK_DELETE) - zr_input_key(in, ZR_KEY_DEL, down); + zr_input_key(ctx, ZR_KEY_DEL, down); else if (sym == SDLK_RETURN) - zr_input_key(in, ZR_KEY_ENTER, down); + zr_input_key(ctx, ZR_KEY_ENTER, down); else if (sym == SDLK_BACKSPACE) - zr_input_key(in, ZR_KEY_BACKSPACE, down); + zr_input_key(ctx, ZR_KEY_BACKSPACE, down); else if (sym == SDLK_LEFT) - zr_input_key(in, ZR_KEY_LEFT, down); + zr_input_key(ctx, ZR_KEY_LEFT, down); else if (sym == SDLK_RIGHT) - zr_input_key(in, ZR_KEY_RIGHT, down); + zr_input_key(ctx, ZR_KEY_RIGHT, down); else if (sym == SDLK_c) - zr_input_key(in, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_COPY, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_v) - zr_input_key(in, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_PASTE, down && state[SDL_SCANCODE_LCTRL]); else if (sym == SDLK_x) - zr_input_key(in, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); + zr_input_key(ctx, ZR_KEY_CUT, down && state[SDL_SCANCODE_LCTRL]); } static void -motion(struct zr_input *in, SDL_Event *evt) +motion(struct zr_context *ctx, SDL_Event *evt) { const int x = evt->motion.x; const int y = evt->motion.y; - zr_input_motion(in, x, y); + zr_input_motion(ctx, x, y); } static void -btn(struct zr_input *in, SDL_Event *evt, int down) +btn(struct zr_context *ctx, SDL_Event *evt, int down) { const int x = evt->button.x; const int y = evt->button.y; if (evt->button.button == SDL_BUTTON_LEFT) - zr_input_button(in, ZR_BUTTON_LEFT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_LEFT, x, y, down); else if (evt->button.button == SDL_BUTTON_RIGHT) - zr_input_button(in, ZR_BUTTON_RIGHT, x, y, down); + zr_input_button(ctx, ZR_BUTTON_RIGHT, x, y, down); else if (evt->button.button == SDL_BUTTON_MIDDLE) - zr_input_button(in, ZR_BUTTON_MIDDLE, x, y, down); + zr_input_button(ctx, ZR_BUTTON_MIDDLE, x, y, down); } static void -text(struct zr_input *in, SDL_Event *evt) +text(struct zr_context *ctx, SDL_Event *evt) { zr_glyph glyph; memcpy(glyph, evt->text.text, ZR_UTF_SIZE); - zr_input_glyph(in, glyph); + zr_input_glyph(ctx, glyph); } static void @@ -646,7 +646,7 @@ main(int argc, char *argv[]) SDL_Event evt; started = SDL_GetTicks(); - zr_input_begin(&ctx.input); + zr_input_begin(&ctx); if (!poll) { ret = SDL_WaitEvent(&evt); poll = 1; @@ -654,16 +654,16 @@ main(int argc, char *argv[]) while (ret) { if (evt.type == SDL_WINDOWEVENT) resize(&evt); else if (evt.type == SDL_QUIT) goto cleanup; - else if (evt.type == SDL_KEYUP) key(&ctx.input, &evt, zr_false); - else if (evt.type == SDL_KEYDOWN) key(&ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&ctx.input, &evt, zr_true); - else if (evt.type == SDL_MOUSEBUTTONUP) btn(&ctx.input, &evt, zr_false); - else if (evt.type == SDL_MOUSEMOTION) motion(&ctx.input, &evt); - else if (evt.type == SDL_TEXTINPUT) text(&ctx.input, &evt); - else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx.input, evt.wheel.y); + else if (evt.type == SDL_KEYUP) key(&ctx, &evt, zr_false); + else if (evt.type == SDL_KEYDOWN) key(&ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONDOWN) btn(&ctx, &evt, zr_true); + else if (evt.type == SDL_MOUSEBUTTONUP) btn(&ctx, &evt, zr_false); + else if (evt.type == SDL_MOUSEMOTION) motion(&ctx, &evt); + else if (evt.type == SDL_TEXTINPUT) text(&ctx, &evt); + else if (evt.type == SDL_MOUSEWHEEL) zr_input_scroll(&ctx, evt.wheel.y); ret = SDL_PollEvent(&evt); } - zr_input_end(&ctx.input); + zr_input_end(&ctx); { int incursor; diff --git a/zahnrad.c b/zahnrad.c index b2dbd4e..957b0ab 100644 --- a/zahnrad.c +++ b/zahnrad.c @@ -1321,231 +1321,6 @@ zr_user_font_glyphs_fitting_in_space(const struct zr_user_font *font, const char *row_len = l; return offset; } -/* ============================================================== - * - * Input - * - * ===============================================================*/ -void -zr_input_begin(struct zr_input *in) -{ - zr_size i; - ZR_ASSERT(in); - if (!in) return; - - for (i = 0; i < ZR_BUTTON_MAX; ++i) - in->mouse.buttons[i].clicked = 0; - in->keyboard.text_len = 0; - in->mouse.scroll_delta = 0; - zr_vec2_mov(in->mouse.prev, in->mouse.pos); - for (i = 0; i < ZR_KEY_MAX; i++) - in->keyboard.keys[i].clicked = 0; -} - -void -zr_input_motion(struct zr_input *in, int x, int y) -{ - ZR_ASSERT(in); - if (!in) return; - in->mouse.pos.x = (float)x; - in->mouse.pos.y = (float)y; -} - -void -zr_input_key(struct zr_input *in, enum zr_keys key, int down) -{ - ZR_ASSERT(in); - if (!in) return; - if (in->keyboard.keys[key].down == down) return; - in->keyboard.keys[key].down = down; - in->keyboard.keys[key].clicked++; -} - -void -zr_input_button(struct zr_input *in, enum zr_buttons id, int x, int y, int down) -{ - struct zr_mouse_button *btn; - ZR_ASSERT(in); - if (!in) return; - if (in->mouse.buttons[id].down == down) return; - btn = &in->mouse.buttons[id]; - btn->clicked_pos.x = (float)x; - btn->clicked_pos.y = (float)y; - btn->down = down; - btn->clicked++; -} - -void -zr_input_scroll(struct zr_input *in, float y) -{ - ZR_ASSERT(in); - if (!in) return; - in->mouse.scroll_delta += y; -} - -void -zr_input_glyph(struct zr_input *in, const zr_glyph glyph) -{ - zr_size len = 0; - zr_rune unicode; - ZR_ASSERT(in); - if (!in) return; - - len = zr_utf_decode(glyph, &unicode, ZR_UTF_SIZE); - if (len && ((in->keyboard.text_len + len) < ZR_INPUT_MAX)) { - zr_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len], - ZR_INPUT_MAX - in->keyboard.text_len); - in->keyboard.text_len += len; - } -} - -void -zr_input_char(struct zr_input *in, char c) -{ - zr_glyph glyph; - ZR_ASSERT(in); - glyph[0] = c; - zr_input_glyph(in, glyph); -} - -void -zr_input_unicode(struct zr_input *in, zr_rune unicode) -{ - zr_glyph rune; - ZR_ASSERT(in); - zr_utf_encode(unicode, rune, ZR_UTF_SIZE); - zr_input_glyph(in, rune); -} - -void -zr_input_end(struct zr_input *in) -{ - ZR_ASSERT(in); - if (!in) return; - in->mouse.delta = zr_vec2_sub(in->mouse.pos, in->mouse.prev); -} - -int -zr_input_has_mouse_click_in_rect(const struct zr_input *i, enum zr_buttons id, - struct zr_rect b) -{ - const struct zr_mouse_button *btn; - if (!i) return zr_false; - btn = &i->mouse.buttons[id]; - if (!ZR_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h)) - return zr_false; - return zr_true; -} - -int -zr_input_has_mouse_click_down_in_rect(const struct zr_input *i, enum zr_buttons id, - struct zr_rect b, int down) -{ - const struct zr_mouse_button *btn; - if (!i) return zr_false; - btn = &i->mouse.buttons[id]; - return zr_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down); -} - -int -zr_input_is_mouse_click_in_rect(const struct zr_input *i, enum zr_buttons id, - struct zr_rect b) -{ - const struct zr_mouse_button *btn; - if (!i) return zr_false; - btn = &i->mouse.buttons[id]; - return (zr_input_has_mouse_click_down_in_rect(i, id, b, zr_true) && - btn->clicked) ? zr_true : zr_false; -} - -int -zr_input_any_mouse_click_in_rect(const struct zr_input *in, struct zr_rect b) -{ - int i, down = 0; - for (i = 0; i < ZR_BUTTON_MAX; ++i) - down = down || zr_input_is_mouse_click_in_rect(in, (enum zr_buttons)i, b); - return down; -} - -int -zr_input_is_mouse_hovering_rect(const struct zr_input *i, struct zr_rect rect) -{ - if (!i) return zr_false; - return ZR_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h); -} - -int -zr_input_is_mouse_prev_hovering_rect(const struct zr_input *i, struct zr_rect rect) -{ - if (!i) return zr_false; - return ZR_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h); -} - -int -zr_input_mouse_clicked(const struct zr_input *i, enum zr_buttons id, struct zr_rect rect) -{ - if (!i) return zr_false; - if (!zr_input_is_mouse_hovering_rect(i, rect)) return zr_false; - return zr_input_is_mouse_click_in_rect(i, id, rect); -} - -int -zr_input_is_mouse_down(const struct zr_input *i, enum zr_buttons id) -{ - if (!i) return zr_false; - return i->mouse.buttons[id].down; -} - -int -zr_input_is_mouse_pressed(const struct zr_input *i, enum zr_buttons id) -{ - const struct zr_mouse_button *b; - if (!i) return zr_false; - b = &i->mouse.buttons[id]; - if (b->down && b->clicked) - return zr_true; - return zr_false; -} - -int -zr_input_is_mouse_released(const struct zr_input *i, enum zr_buttons id) -{ - if (!i) return zr_false; - return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked) ? zr_true : zr_false; -} - -int -zr_input_is_key_pressed(const struct zr_input *i, enum zr_keys key) -{ - const struct zr_key *k; - if (!i) return zr_false; - k = &i->keyboard.keys[key]; - if (k->down && k->clicked) - return zr_true; - return zr_false; -} - -int -zr_input_is_key_released(const struct zr_input *i, enum zr_keys key) -{ - const struct zr_key *k; - if (!i) return zr_false; - k = &i->keyboard.keys[key]; - if (!k->down && k->clicked) - return zr_true; - return zr_false; -} - -int -zr_input_is_key_down(const struct zr_input *i, enum zr_keys key) -{ - const struct zr_key *k; - if (!i) return zr_false; - k = &i->keyboard.keys[key]; - if (k->down) return zr_true; - return zr_false; -} - /* ============================================================== * * BUFFER @@ -5735,6 +5510,249 @@ zr_do_property(enum zr_widget_status *ws, } return property_value; } +/* ============================================================== + * + * INPUT + * + * ===============================================================*/ +void +zr_input_begin(struct zr_context *ctx) +{ + zr_size i; + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + + in = &ctx->input; + for (i = 0; i < ZR_BUTTON_MAX; ++i) + in->mouse.buttons[i].clicked = 0; + in->keyboard.text_len = 0; + in->mouse.scroll_delta = 0; + zr_vec2_mov(in->mouse.prev, in->mouse.pos); + for (i = 0; i < ZR_KEY_MAX; i++) + in->keyboard.keys[i].clicked = 0; +} + +void +zr_input_motion(struct zr_context *ctx, int x, int y) +{ + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + + in = &ctx->input; + in->mouse.pos.x = (float)x; + in->mouse.pos.y = (float)y; +} + +void +zr_input_key(struct zr_context *ctx, enum zr_keys key, int down) +{ + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + in = &ctx->input; + if (in->keyboard.keys[key].down == down) return; + + in->keyboard.keys[key].down = down; + in->keyboard.keys[key].clicked++; +} + +void +zr_input_button(struct zr_context *ctx, enum zr_buttons id, int x, int y, int down) +{ + struct zr_mouse_button *btn; + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + in = &ctx->input; + if (in->mouse.buttons[id].down == down) return; + + btn = &in->mouse.buttons[id]; + btn->clicked_pos.x = (float)x; + btn->clicked_pos.y = (float)y; + btn->down = down; + btn->clicked++; +} + +void +zr_input_scroll(struct zr_context *ctx, float y) +{ + ZR_ASSERT(ctx); + if (!ctx) return; + ctx->input.mouse.scroll_delta += y; +} + +void +zr_input_glyph(struct zr_context *ctx, const zr_glyph glyph) +{ + zr_size len = 0; + zr_rune unicode; + + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + in = &ctx->input; + + len = zr_utf_decode(glyph, &unicode, ZR_UTF_SIZE); + if (len && ((in->keyboard.text_len + len) < ZR_INPUT_MAX)) { + zr_utf_encode(unicode, &in->keyboard.text[in->keyboard.text_len], + ZR_INPUT_MAX - in->keyboard.text_len); + in->keyboard.text_len += len; + } +} + +void +zr_input_char(struct zr_context *ctx, char c) +{ + zr_glyph glyph; + ZR_ASSERT(ctx); + if (!ctx) return; + glyph[0] = c; + zr_input_glyph(ctx, glyph); +} + +void +zr_input_unicode(struct zr_context *ctx, zr_rune unicode) +{ + zr_glyph rune; + ZR_ASSERT(ctx); + if (!ctx) return; + zr_utf_encode(unicode, rune, ZR_UTF_SIZE); + zr_input_glyph(ctx, rune); +} + +void +zr_input_end(struct zr_context *ctx) +{ + struct zr_input *in; + ZR_ASSERT(ctx); + if (!ctx) return; + in = &ctx->input; + in->mouse.delta = zr_vec2_sub(in->mouse.pos, in->mouse.prev); +} + +int +zr_input_has_mouse_click_in_rect(const struct zr_input *i, enum zr_buttons id, + struct zr_rect b) +{ + const struct zr_mouse_button *btn; + if (!i) return zr_false; + btn = &i->mouse.buttons[id]; + if (!ZR_INBOX(btn->clicked_pos.x,btn->clicked_pos.y,b.x,b.y,b.w,b.h)) + return zr_false; + return zr_true; +} + +int +zr_input_has_mouse_click_down_in_rect(const struct zr_input *i, enum zr_buttons id, + struct zr_rect b, int down) +{ + const struct zr_mouse_button *btn; + if (!i) return zr_false; + btn = &i->mouse.buttons[id]; + return zr_input_has_mouse_click_in_rect(i, id, b) && (btn->down == down); +} + +int +zr_input_is_mouse_click_in_rect(const struct zr_input *i, enum zr_buttons id, + struct zr_rect b) +{ + const struct zr_mouse_button *btn; + if (!i) return zr_false; + btn = &i->mouse.buttons[id]; + return (zr_input_has_mouse_click_down_in_rect(i, id, b, zr_true) && + btn->clicked) ? zr_true : zr_false; +} + +int +zr_input_any_mouse_click_in_rect(const struct zr_input *in, struct zr_rect b) +{ + int i, down = 0; + for (i = 0; i < ZR_BUTTON_MAX; ++i) + down = down || zr_input_is_mouse_click_in_rect(in, (enum zr_buttons)i, b); + return down; +} + +int +zr_input_is_mouse_hovering_rect(const struct zr_input *i, struct zr_rect rect) +{ + if (!i) return zr_false; + return ZR_INBOX(i->mouse.pos.x, i->mouse.pos.y, rect.x, rect.y, rect.w, rect.h); +} + +int +zr_input_is_mouse_prev_hovering_rect(const struct zr_input *i, struct zr_rect rect) +{ + if (!i) return zr_false; + return ZR_INBOX(i->mouse.prev.x, i->mouse.prev.y, rect.x, rect.y, rect.w, rect.h); +} + +int +zr_input_mouse_clicked(const struct zr_input *i, enum zr_buttons id, struct zr_rect rect) +{ + if (!i) return zr_false; + if (!zr_input_is_mouse_hovering_rect(i, rect)) return zr_false; + return zr_input_is_mouse_click_in_rect(i, id, rect); +} + +int +zr_input_is_mouse_down(const struct zr_input *i, enum zr_buttons id) +{ + if (!i) return zr_false; + return i->mouse.buttons[id].down; +} + +int +zr_input_is_mouse_pressed(const struct zr_input *i, enum zr_buttons id) +{ + const struct zr_mouse_button *b; + if (!i) return zr_false; + b = &i->mouse.buttons[id]; + if (b->down && b->clicked) + return zr_true; + return zr_false; +} + +int +zr_input_is_mouse_released(const struct zr_input *i, enum zr_buttons id) +{ + if (!i) return zr_false; + return (!i->mouse.buttons[id].down && i->mouse.buttons[id].clicked) ? zr_true : zr_false; +} + +int +zr_input_is_key_pressed(const struct zr_input *i, enum zr_keys key) +{ + const struct zr_key *k; + if (!i) return zr_false; + k = &i->keyboard.keys[key]; + if (k->down && k->clicked) + return zr_true; + return zr_false; +} + +int +zr_input_is_key_released(const struct zr_input *i, enum zr_keys key) +{ + const struct zr_key *k; + if (!i) return zr_false; + k = &i->keyboard.keys[key]; + if (!k->down && k->clicked) + return zr_true; + return zr_false; +} + +int +zr_input_is_key_down(const struct zr_input *i, enum zr_keys key) +{ + const struct zr_key *k; + if (!i) return zr_false; + k = &i->keyboard.keys[key]; + if (k->down) return zr_true; + return zr_false; +} + /* ============================================================== * @@ -5821,15 +5839,15 @@ static const char *zr_style_property_names[] = { }; const char* -zr_style_color_name(enum zr_style_colors color) +zr_get_color_name(enum zr_style_colors color) {return zr_style_color_names[color];} const char* -zr_style_rounding_name(enum zr_style_rounding rounding) +zr_get_rounding_name(enum zr_style_rounding rounding) {return zr_style_rounding_names[rounding];} const char* -zr_style_property_name(enum zr_style_properties property) +zr_get_property_name(enum zr_style_properties property) {return zr_style_property_names[property];} static void @@ -5857,13 +5875,13 @@ zr_style_default_color(struct zr_style *style) } void -zr_style_default(struct zr_style *style, zr_flags flags, - const struct zr_user_font *font) +zr_load_default_style(struct zr_context *ctx, zr_flags flags) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; zr_zero(style, sizeof(*style)); - style->font = *font; if (flags & ZR_DEFAULT_COLOR) zr_style_default_color(style); @@ -5879,39 +5897,46 @@ zr_style_default(struct zr_style *style, zr_flags flags, } void -zr_style_set_font(struct zr_style *style, const struct zr_user_font *font) +zr_set_font(struct zr_context *ctx, const struct zr_user_font *font) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; style->font = *font; } struct zr_vec2 -zr_style_property(const struct zr_style *style, enum zr_style_properties index) +zr_get_property(const struct zr_context *ctx, enum zr_style_properties index) { + const struct zr_style *style; static const struct zr_vec2 zero; - ZR_ASSERT(style); - if (!style) return zero; + ZR_ASSERT(ctx); + if (!ctx) return zero; + style = &ctx->style; return style->properties[index]; - /* draw open/close symbol */ } struct zr_color -zr_style_color(const struct zr_style *style, enum zr_style_colors index) +zr_get_color(const struct zr_context *ctx, enum zr_style_colors index) { + const struct zr_style *style; static const struct zr_color zero; - ZR_ASSERT(style); - if (!style) return zero; + ZR_ASSERT(ctx); + if (!ctx) return zero; + style = &ctx->style; return style->colors[index]; } void -zr_style_push_color(struct zr_style *style, enum zr_style_colors index, +zr_push_color(struct zr_context *ctx, enum zr_style_colors index, struct zr_color col) { + struct zr_style *style; struct zr_saved_color *c; - ZR_ASSERT(style); - if (!style) return; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (style->stack.color >= ZR_MAX_COLOR_STACK) return; c = &style->stack.colors[style->stack.color++]; @@ -5921,12 +5946,14 @@ zr_style_push_color(struct zr_style *style, enum zr_style_colors index, } void -zr_style_push_property(struct zr_style *style, enum zr_style_properties index, +zr_push_property(struct zr_context *ctx, enum zr_style_properties index, struct zr_vec2 v) { + struct zr_style *style; struct zr_saved_property *p; - ZR_ASSERT(style); - if (!style) return; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (style->stack.property >= ZR_MAX_ATTRIB_STACK) return; p = &style->stack.properties[style->stack.property++]; @@ -5936,11 +5963,13 @@ zr_style_push_property(struct zr_style *style, enum zr_style_properties index, } void -zr_style_push_font(struct zr_style *style, struct zr_user_font font) +zr_push_font(struct zr_context *ctx, struct zr_user_font font) { + struct zr_style *style; struct zr_saved_font *f; - ZR_ASSERT(style); - if (!style) return; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (style->stack.font >= ZR_MAX_FONT_STACK) return; f = &style->stack.fonts[style->stack.font++]; @@ -5951,10 +5980,12 @@ zr_style_push_font(struct zr_style *style, struct zr_user_font font) } void -zr_style_push_font_height(struct zr_style *style, float font_height) +zr_push_font_height(struct zr_context *ctx, float font_height) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (style->stack.font >= ZR_MAX_FONT_HEIGHT_STACK) return; style->stack.font_heights[style->stack.font_height++] = style->font.height; @@ -5964,33 +5995,44 @@ zr_style_push_font_height(struct zr_style *style, float font_height) } void -zr_style_pop_color(struct zr_style *style) +zr_pop_color(struct zr_context *ctx) { + struct zr_style *style; struct zr_saved_color *c; - ZR_ASSERT(style); - if (!style) return; + ZR_ASSERT(ctx); + + if (!ctx) return; + style = &ctx->style; if (!style->stack.color) return; + c = &style->stack.colors[--style->stack.color]; style->colors[c->type] = c->value; } void -zr_style_pop_property(struct zr_style *style) +zr_pop_property(struct zr_context *ctx) { + struct zr_style *style; struct zr_saved_property *p; - ZR_ASSERT(style); - if (!style) return; + + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (!style->stack.property) return; + p = &style->stack.properties[--style->stack.property]; style->properties[p->type] = p->value; } void -zr_style_pop_font(struct zr_style *style) +zr_pop_font(struct zr_context *ctx) { + struct zr_style *style; struct zr_saved_font *f; - ZR_ASSERT(style); - if (!style) return; + + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (!style->stack.font) return; f = &style->stack.fonts[--style->stack.font]; @@ -6001,12 +6043,16 @@ zr_style_pop_font(struct zr_style *style) } void -zr_style_pop_font_height(struct zr_style *style) +zr_pop_font_height(struct zr_context *ctx) { + struct zr_style *style; float font_height; - ZR_ASSERT(style); - if (!style) return; + + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; if (!style->stack.font_height) return; + font_height = style->stack.font_heights[--style->stack.font_height]; style->font.height = font_height; if (style->stack.font) { @@ -6016,50 +6062,61 @@ zr_style_pop_font_height(struct zr_style *style) } void -zr_style_reset_colors(struct zr_style *style) +zr_reset_colors(struct zr_context *ctx) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; while (style->stack.color) - zr_style_pop_color(style); + zr_pop_color(ctx); } void -zr_style_reset_properties(struct zr_style *style) +zr_reset_properties(struct zr_context *ctx) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; while (style->stack.property) - zr_style_pop_property(style); + zr_pop_property(ctx); } void -zr_style_reset_font(struct zr_style *style) +zr_reset_font(struct zr_context *ctx) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; while (style->stack.font) - zr_style_pop_font(style); + zr_pop_font(ctx); } void -zr_style_reset_font_height(struct zr_style *style) +zr_reset_font_height(struct zr_context *ctx) { - ZR_ASSERT(style); - if (!style) return; + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; while (style->stack.font_height) - zr_style_pop_font_height(style); + zr_pop_font_height(ctx); } void -zr_style_reset(struct zr_style *style) +zr_reset(struct zr_context *ctx) { - ZR_ASSERT(style); - if (!style) return; - zr_style_reset_colors(style); - zr_style_reset_properties(style); - zr_style_reset_font(style); - zr_style_reset_font_height(style); + struct zr_style *style; + ZR_ASSERT(ctx); + if (!ctx) return; + style = &ctx->style; + + zr_reset_colors(ctx); + zr_reset_properties(ctx); + zr_reset_font(ctx); + zr_reset_font_height(ctx); } /* =============================================================== @@ -6527,8 +6584,12 @@ zr_clear(struct zr_context *ctx) static void zr_setup(struct zr_context *ctx, const struct zr_user_font *font) { + ZR_ASSERT(ctx); + ZR_ASSERT(font); + if (!ctx || !font) return; zr_zero(ctx, sizeof(*ctx)); - zr_style_default(&ctx->style, ZR_DEFAULT_ALL, font); + zr_load_default_style(ctx, ZR_DEFAULT_ALL); + ctx->style.font = *font; #if ZR_COMPILE_WITH_VERTEX_BUFFER zr_canvas_init(&ctx->canvas); #endif @@ -6977,7 +7038,7 @@ zr_header_button(struct zr_context *ctx, struct zr_window_header *header, layout = win->layout; c = &ctx->style; out = &win->buffer; - item_padding = zr_style_property(c, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); sym.x = header->front; sym.y = header->y; @@ -7070,11 +7131,11 @@ zr_layout_begin(struct zr_context *ctx, const char *title) layout = win->layout; /* cache style data */ - scrollbar_size = zr_style_property(c, ZR_PROPERTY_SCROLLBAR_SIZE).x; - window_padding = zr_style_property(c, ZR_PROPERTY_PADDING); - item_padding = zr_style_property(c, ZR_PROPERTY_ITEM_PADDING); - item_spacing = zr_style_property(c, ZR_PROPERTY_ITEM_SPACING); - scaler_size = zr_style_property(c, ZR_PROPERTY_SCALER_SIZE); + scrollbar_size = zr_get_property(ctx, ZR_PROPERTY_SCROLLBAR_SIZE).x; + window_padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); + item_spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); + scaler_size = zr_get_property(ctx, ZR_PROPERTY_SCALER_SIZE); /* check arguments */ zr_zero(layout, sizeof(*layout)); @@ -7288,11 +7349,11 @@ zr_layout_end(struct zr_context *ctx) zr_draw_scissor(out, zr_null_rect); /* cache configuration data */ - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); - item_spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING); - window_padding = zr_style_property(config, ZR_PROPERTY_PADDING); - scrollbar_size = zr_style_property(config, ZR_PROPERTY_SCROLLBAR_SIZE).x; - scaler_size = zr_style_property(config, ZR_PROPERTY_SCALER_SIZE); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); + item_spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); + window_padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); + scrollbar_size = zr_get_property(ctx, ZR_PROPERTY_SCROLLBAR_SIZE).x; + scaler_size = zr_get_property(ctx, ZR_PROPERTY_SCALER_SIZE); /* update the current cursor Y-position to point over the last added widget */ layout->at_y += layout->row.height; @@ -7402,7 +7463,7 @@ zr_layout_end(struct zr_context *ctx) if (!(window->flags & ZR_WINDOW_ROM)) { float prev_x = in->mouse.prev.x; float prev_y = in->mouse.prev.y; - struct zr_vec2 window_size = zr_style_property(config, ZR_PROPERTY_SIZE); + struct zr_vec2 window_size = zr_get_property(ctx, ZR_PROPERTY_SIZE); int incursor = ZR_INBOX(prev_x,prev_y,scaler_x,scaler_y,scaler_w,scaler_h); if (zr_input_is_mouse_down(in, ZR_BUTTON_LEFT) && incursor) { @@ -7544,8 +7605,8 @@ zr_panel_layout(const struct zr_context *ctx, struct zr_window *win, config = &ctx->style; out = &win->buffer; color = &config->colors[ZR_COLOR_WINDOW]; - item_spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING); - panel_padding = zr_style_property(config, ZR_PROPERTY_PADDING); + item_spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); + panel_padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); /* update the current row and set the current row layout */ layout->row.index = 0; @@ -7865,7 +7926,7 @@ zr_panel_alloc_row(const struct zr_context *ctx, struct zr_window *win) { struct zr_layout *layout = win->layout; const struct zr_style *c = &ctx->style; - struct zr_vec2 spacing = zr_style_property(c, ZR_PROPERTY_ITEM_SPACING); + struct zr_vec2 spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); const float row_height = layout->row.height - spacing.y; zr_panel_layout(ctx, win, row_height, layout->row.columns); } @@ -7891,8 +7952,8 @@ zr_layout_widget_space(struct zr_rect *bounds, const struct zr_context *ctx, /* cache some configuration data */ config = &ctx->style; - spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING); - padding = zr_style_property(config, ZR_PROPERTY_PADDING); + spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); + padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); /* calculate the useable panel space */ panel_padding = 2 * padding.x; @@ -8071,9 +8132,9 @@ zr_layout_push(struct zr_context *ctx, enum zr_layout_node_type type, const char out = &win->buffer; config = &ctx->style; - item_spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING); - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); - panel_padding = zr_style_property(config, ZR_PROPERTY_PADDING); + item_spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); + panel_padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); /* calculate header bounds and draw background */ zr_layout_row_dynamic(ctx, config->font.height + 2 * item_padding.y, 1); @@ -8166,7 +8227,7 @@ zr_layout_pop(struct zr_context *ctx) win = ctx->current; layout = win->layout; config = &ctx->style; - panel_padding = zr_style_property(config, ZR_PROPERTY_PADDING); + panel_padding = zr_get_property(ctx, ZR_PROPERTY_PADDING); layout->at_x -= panel_padding.x; layout->width += 2 * panel_padding.x; } @@ -8229,8 +8290,8 @@ zr_seperator(struct zr_context *ctx) layout = win->layout; out = &win->buffer; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); - item_spacing = zr_style_property(config, ZR_PROPERTY_ITEM_SPACING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); + item_spacing = zr_get_property(ctx, ZR_PROPERTY_ITEM_SPACING); bounds.h = 1; bounds.w = MAX(layout->width, 2 * item_spacing.x + 2 * item_padding.x); @@ -8312,7 +8373,7 @@ zr_text_colored(struct zr_context *ctx, const char *str, zr_size len, win = ctx->current; zr_panel_alloc_space(&bounds, ctx); config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); text.padding.x = item_padding.x; text.padding.y = item_padding.y; @@ -8340,7 +8401,7 @@ zr_text_wrap_colored(struct zr_context *ctx, const char *str, win = ctx->current; zr_panel_alloc_space(&bounds, ctx); config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); text.padding.x = item_padding.x; text.padding.y = item_padding.y; @@ -8394,7 +8455,7 @@ zr_image(struct zr_context *ctx, struct zr_image img) return; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); bounds.x += item_padding.x; bounds.y += item_padding.y; bounds.w -= 2 * item_padding.x; @@ -8403,11 +8464,12 @@ zr_image(struct zr_context *ctx, struct zr_image img) } static void -zr_fill_button(const struct zr_style *config, struct zr_button *button) +zr_fill_button(const struct zr_context *ctx, struct zr_button *button) { struct zr_vec2 item_padding; + const struct zr_style *config = &ctx->style; zr_zero(button, sizeof(*button)); - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); button->rounding = config->rounding[ZR_ROUNDING_BUTTON]; button->normal = config->colors[ZR_COLOR_BUTTON]; button->hover = config->colors[ZR_COLOR_BUTTON_HOVER]; @@ -8432,7 +8494,7 @@ zr_button(struct zr_button *button, struct zr_rect *bounds, if (!state) return state; zr_zero(button, sizeof(*button)); config = &ctx->style; - zr_fill_button(config, button); + zr_fill_button(ctx, button); return state; } @@ -8657,7 +8719,7 @@ zr_select(struct zr_context *ctx, const char *str, win = ctx->current; zr_panel_alloc_space(&bounds, ctx); config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); background = (!value) ? config->colors[ZR_COLOR_WINDOW]: config->colors[ZR_COLOR_SELECTABLE]; @@ -8702,7 +8764,7 @@ zr_toggle_base(struct zr_toggle *toggle, struct zr_rect *bounds, if (!state) return state; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); toggle->rounding = 0; toggle->padding.x = item_padding.x; toggle->padding.y = item_padding.y; @@ -8828,7 +8890,7 @@ zr_slider_float(struct zr_context *ctx, float min_value, float *value, i = (state == ZR_WIDGET_ROM || layout->flags & ZR_WINDOW_ROM) ? 0 : &ctx->input; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); slider.padding.x = item_padding.x; slider.padding.y = item_padding.y; slider.bg = config->colors[ZR_COLOR_SLIDER]; @@ -8877,7 +8939,7 @@ zr_progress(struct zr_context *ctx, zr_size *cur_value, zr_size max_value, i = (state == ZR_WIDGET_ROM || layout->flags & ZR_WINDOW_ROM) ? 0 : &ctx->input; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); prog.rounding = config->rounding[ZR_ROUNDING_PROGRESS]; prog.padding.x = item_padding.x; prog.padding.y = item_padding.y; @@ -8901,7 +8963,7 @@ zr_edit_base(struct zr_rect *bounds, struct zr_edit *field, if (!state) return state; config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); field->border_size = 1; field->scrollbar_width = config->properties[ZR_PROPERTY_SCROLLBAR_SIZE].x; field->rounding = config->rounding[ZR_ROUNDING_INPUT]; @@ -9116,7 +9178,7 @@ zr_property(struct zr_context *ctx, const char *name, /* execute property widget */ config = &ctx->style; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); prop.border_size = 1; prop.rounding = config->rounding[ZR_ROUNDING_PROPERTY]; prop.padding = item_padding; @@ -9205,7 +9267,7 @@ zr_graph_begin(struct zr_context *ctx, enum zr_graph_type type, /* draw graph background */ config = &ctx->style; out = &win->buffer; - item_padding = zr_style_property(config, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); color = (type == ZR_GRAPH_LINES) ? config->colors[ZR_COLOR_PLOT]: config->colors[ZR_COLOR_HISTO]; zr_draw_rect(out, bounds, config->rounding[ZR_ROUNDING_GRAPH], color); @@ -10013,7 +10075,7 @@ zr_combo_begin_text(struct zr_context *ctx, struct zr_layout *layout, if (!zr_widget(&header, ctx)) return 0; - item_padding = zr_style_property(&ctx->style, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); if (zr_button_behavior(&state, header, &ctx->input, ZR_BUTTON_DEFAULT)) is_active = zr_true; @@ -10066,7 +10128,7 @@ zr_combo_begin_color(struct zr_context *ctx, struct zr_layout *layout, if (!zr_widget(&header, ctx)) return 0; - item_padding = zr_style_property(&ctx->style, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); if (zr_button_behavior(&state, header, &ctx->input, ZR_BUTTON_DEFAULT)) is_active = !is_active; @@ -10117,7 +10179,7 @@ zr_combo_begin_image(struct zr_context *ctx, struct zr_layout *layout, if (!zr_widget(&header, ctx)) return 0; - item_padding = zr_style_property(&ctx->style, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); if (zr_button_behavior(&state, header, &ctx->input, ZR_BUTTON_DEFAULT)) is_active = !is_active; @@ -10168,7 +10230,7 @@ zr_combo_begin_icon(struct zr_context *ctx, struct zr_layout *layout, const char if (!zr_widget(&header, ctx)) return 0; - item_padding = zr_style_property(&ctx->style, ZR_PROPERTY_ITEM_PADDING); + item_padding = zr_get_property(ctx, ZR_PROPERTY_ITEM_PADDING); if (zr_button_behavior(&state, header, &ctx->input, ZR_BUTTON_DEFAULT)) is_active = !is_active; diff --git a/zahnrad.h b/zahnrad.h index 21e03de..9195cb6 100644 --- a/zahnrad.h +++ b/zahnrad.h @@ -168,112 +168,10 @@ struct zr_image zr_subimage_id(int, unsigned short w, unsigned short h, struct z int zr_image_is_subimage(const struct zr_image* img); /* ============================================================== - * INPUT - * ===============================================================*/ -/* The input API is responsible for holding the input state by keeping track of - mouse, key and text. The core of the API is a persistent - zr_input struct which holds the input state while running. - It is important to note that no direct os or window handling is done by the input - API, instead all the input state has to be provided by the user. This in one hand - expects more work from the user and complicates the usage but on the other hand - provides simple abstraction over a big number of platforms, libraries and other - already provided functionality. -*/ -enum zr_keys { - ZR_KEY_SHIFT, - ZR_KEY_DEL, - ZR_KEY_ENTER, - ZR_KEY_TAB, - ZR_KEY_BACKSPACE, - ZR_KEY_COPY, - ZR_KEY_CUT, - ZR_KEY_PASTE, - ZR_KEY_LEFT, - ZR_KEY_RIGHT, - ZR_KEY_MAX -}; - -/* every used mouse button */ -enum zr_buttons { - ZR_BUTTON_LEFT, - ZR_BUTTON_MIDDLE, - ZR_BUTTON_RIGHT, - ZR_BUTTON_MAX -}; - -struct zr_mouse_button { - int down; - /* current button state */ - unsigned int clicked; - /* button state change */ - struct zr_vec2 clicked_pos; - /* mouse position of last state change */ -}; - -struct zr_mouse { - struct zr_mouse_button buttons[ZR_BUTTON_MAX]; - /* mouse button states */ - struct zr_vec2 pos; - /* current mouse position */ - struct zr_vec2 prev; - /* mouse position in the last frame */ - struct zr_vec2 delta; - /* mouse travelling distance from last to current frame */ - float scroll_delta; - /* number of steps in the up or down scroll direction */ -}; - -struct zr_key { - int down; - unsigned int clicked; -}; - -struct zr_keyboard { - struct zr_key keys[ZR_KEY_MAX]; - /* state of every used key */ - char text[ZR_INPUT_MAX]; - /* utf8 text input frame buffer */ - zr_size text_len; - /* text input frame buffer length in bytes */ -}; - -struct zr_input { - struct zr_keyboard keyboard; - /* current keyboard key + text input state */ - struct zr_mouse mouse; - /* current mouse button and position state */ -}; - -/* gathering input state */ -void zr_input_begin(struct zr_input*); -void zr_input_motion(struct zr_input*, int x, int y); -void zr_input_key(struct zr_input*, enum zr_keys, int down); -void zr_input_button(struct zr_input*, enum zr_buttons, int x, int y, int down); -void zr_input_scroll(struct zr_input*, float y); -void zr_input_glyph(struct zr_input*, const zr_glyph); -void zr_input_char(struct zr_input*, char); -void zr_input_unicode(struct zr_input *in, zr_rune unicode); -void zr_input_end(struct zr_input*); - -/* query input state */ -int zr_input_has_mouse_click_in_rect(const struct zr_input*,enum zr_buttons, struct zr_rect); -int zr_input_has_mouse_click_down_in_rect(const struct zr_input*, enum zr_buttons, - struct zr_rect, int down); -int zr_input_is_mouse_click_in_rect(const struct zr_input*, enum zr_buttons, struct zr_rect); -int zr_input_any_mouse_click_in_rect(const struct zr_input*, struct zr_rect); -int zr_input_is_mouse_prev_hovering_rect(const struct zr_input*, struct zr_rect); -int zr_input_is_mouse_hovering_rect(const struct zr_input*, struct zr_rect); -int zr_input_mouse_clicked(const struct zr_input*, enum zr_buttons, struct zr_rect); -int zr_input_is_mouse_down(const struct zr_input*, enum zr_buttons); -int zr_input_is_mouse_pressed(const struct zr_input*, enum zr_buttons); -int zr_input_is_mouse_released(const struct zr_input*, enum zr_buttons); -int zr_input_is_key_pressed(const struct zr_input*, enum zr_keys); -int zr_input_is_key_released(const struct zr_input*, enum zr_keys); -int zr_input_is_key_down(const struct zr_input*, enum zr_keys); - -/* ============================================================== + * * MEMORY BUFFER - * =============================================================== */ + * + * ===============================================================*/ /* A basic (double)-buffer API with linear allocation and resetting as only freeing policy. The buffers main purpose is to control all memory management inside the GUI toolkit and still leave memory control as much as possible in the hand @@ -600,11 +498,25 @@ struct zr_user_font zr_font_ref(struct zr_font*); const struct zr_font_glyph* zr_font_find_glyph(struct zr_font*, zr_rune unicode); #endif + /* =============================================================== * - * CANVAS + * RENDERING * * ===============================================================*/ +/* This library was designed to be render backend agnostic so it does + not draw anything to the screen. Instead all drawn primitives, widgets + are made of, are buffered into memory and make up a command queue. + Each frame therefore fills the command buffer with draw commands + that than need to be executed by the user and his own render backend. + After that the command buffer needs to be cleared and a new frame can be started. + + The reason for buffering simple primitives as draw commands instead of + directly buffering a hardware accessible format with vertex and element + buffer was to support native render backends like X11 and Win32. + That being said it is possible to convert the command buffer into a + hardware accessible format to support hardware based rendering as well. +*/ enum zr_command_type { ZR_COMMAND_NOP, ZR_COMMAND_SCISSOR, @@ -745,39 +657,7 @@ struct zr_draw_null_texture { /* coordinates to the white pixel in the texture */ }; -struct zr_canvas { - enum zr_anti_aliasing AA; - /* flag indicating if anti-aliasing should be used to render primtives */ - struct zr_draw_null_texture null; - /* texture with white pixel for easy primitive drawing */ - struct zr_rect clip_rect; - /* current clipping rectangle */ - /* cosine/sine calculation callback since this library does not use libc */ - struct zr_buffer *buffer; - /* buffer to store draw commands and temporarily store path */ - struct zr_buffer *vertexes; - /* buffer to store each draw vertex */ - struct zr_buffer *elements; - /* buffer to store each draw element index */ - unsigned int element_count; - /* total number of elements inside the elements buffer */ - unsigned int vertex_count; - /* total number of vertexes inside the vertex buffer */ - zr_size cmd_offset; - /* offset to the first command in the buffer */ - unsigned int cmd_count; - /* number of commands inside the buffer */ - unsigned int path_count; - /* current number of points inside the path */ - unsigned int path_offset; - /* offset to the first point in the buffer */ - struct zr_vec2 circle_vtx[12]; - /* small lookup table for fast circle drawing */ -}; -#endif - -/* drawing routines */ -#define zr_command(t, c) ((const struct zr_command_##t*)c) +/* drawing routines for custom widgets */ void zr_draw_scissor(struct zr_command_buffer*, struct zr_rect); void zr_draw_line(struct zr_command_buffer*, float, float, float, float, struct zr_color); void zr_draw_curve(struct zr_command_buffer*, float, float, float, float, float, float, @@ -791,6 +671,94 @@ void zr_draw_image(struct zr_command_buffer*, struct zr_rect, struct zr_image*); void zr_draw_text(struct zr_command_buffer*, struct zr_rect, const char*, zr_size, const struct zr_user_font*, struct zr_color, struct zr_color); +#endif +/* =============================================================== + * + * GUI + * + * ===============================================================*/ +enum zr_keys { + ZR_KEY_SHIFT, + ZR_KEY_DEL, + ZR_KEY_ENTER, + ZR_KEY_TAB, + ZR_KEY_BACKSPACE, + ZR_KEY_COPY, + ZR_KEY_CUT, + ZR_KEY_PASTE, + ZR_KEY_LEFT, + ZR_KEY_RIGHT, + ZR_KEY_MAX +}; + +/* every used mouse button */ +enum zr_buttons { + ZR_BUTTON_LEFT, + ZR_BUTTON_MIDDLE, + ZR_BUTTON_RIGHT, + ZR_BUTTON_MAX +}; + +struct zr_mouse_button { + int down; + /* current button state */ + unsigned int clicked; + /* button state change */ + struct zr_vec2 clicked_pos; + /* mouse position of last state change */ +}; + +struct zr_mouse { + struct zr_mouse_button buttons[ZR_BUTTON_MAX]; + /* mouse button states */ + struct zr_vec2 pos; + /* current mouse position */ + struct zr_vec2 prev; + /* mouse position in the last frame */ + struct zr_vec2 delta; + /* mouse travelling distance from last to current frame */ + float scroll_delta; + /* number of steps in the up or down scroll direction */ +}; + +struct zr_key { + int down; + unsigned int clicked; +}; + +struct zr_keyboard { + struct zr_key keys[ZR_KEY_MAX]; + /* state of every used key */ + char text[ZR_INPUT_MAX]; + /* utf8 text input frame buffer */ + zr_size text_len; + /* text input frame buffer length in bytes */ +}; + +struct zr_input { + struct zr_keyboard keyboard; + /* current keyboard key + text input state */ + struct zr_mouse mouse; + /* current mouse button and position state */ +}; + +/* query input state */ +int zr_input_has_mouse_click_in_rect(const struct zr_input*,enum zr_buttons, struct zr_rect); +int zr_input_has_mouse_click_down_in_rect(const struct zr_input*, enum zr_buttons, + struct zr_rect, int down); +int zr_input_is_mouse_click_in_rect(const struct zr_input*, enum zr_buttons, struct zr_rect); +int zr_input_any_mouse_click_in_rect(const struct zr_input*, struct zr_rect); +int zr_input_is_mouse_prev_hovering_rect(const struct zr_input*, struct zr_rect); +int zr_input_is_mouse_hovering_rect(const struct zr_input*, struct zr_rect); +int zr_input_mouse_clicked(const struct zr_input*, enum zr_buttons, struct zr_rect); +int zr_input_is_mouse_down(const struct zr_input*, enum zr_buttons); +int zr_input_is_mouse_pressed(const struct zr_input*, enum zr_buttons); +int zr_input_is_mouse_released(const struct zr_input*, enum zr_buttons); +int zr_input_is_key_pressed(const struct zr_input*, enum zr_keys); +int zr_input_is_key_released(const struct zr_input*, enum zr_keys); +int zr_input_is_key_down(const struct zr_input*, enum zr_keys); + + /* ============================================================== * STYLE * ===============================================================*/ @@ -947,37 +915,6 @@ struct zr_style { /* modification stack */ }; -/* style setup and access */ -struct zr_style; -void zr_style_default(struct zr_style*, zr_flags, const struct zr_user_font*); -void zr_style_set_font(struct zr_style*, const struct zr_user_font*); -struct zr_vec2 zr_style_property(const struct zr_style*, enum zr_style_properties); -struct zr_color zr_style_color(const struct zr_style*, enum zr_style_colors); - -/* temporarily modify a style value and save the old value in a stack*/ -void zr_style_push_property(struct zr_style*, enum zr_style_properties, struct zr_vec2); -void zr_style_push_color(struct zr_style*, enum zr_style_colors, struct zr_color); -void zr_style_push_font(struct zr_style*, struct zr_user_font font); -void zr_style_push_font_height(struct zr_style*, float font_height); - -/* restores a previously saved style value */ -void zr_style_pop_color(struct zr_style*); -void zr_style_pop_property(struct zr_style*); -void zr_style_pop_font(struct zr_style*); -void zr_style_pop_font_height(struct zr_style*); - -/* resets the style back into the beginning state */ -void zr_style_reset_colors(struct zr_style*); -void zr_style_reset_properties(struct zr_style*); -void zr_style_reset_font(struct zr_style*); -void zr_style_reset_font_height(struct zr_style*); -void zr_style_reset(struct zr_style*); - -/* return string representation of different styles values */ -const char *zr_style_color_name(enum zr_style_colors); -const char *zr_style_rounding_name(enum zr_style_rounding); -const char *zr_style_property_name(enum zr_style_properties); - /*=============================================================== * EDIT BOX * ===============================================================*/ @@ -1010,9 +947,7 @@ zr_size zr_edit_box_len_char(struct zr_edit_box*); zr_size zr_edit_box_len(struct zr_edit_box*); /*============================================================== - * - * GUI - * + * WINDOW * =============================================================*/ #define ZR_UNDEFINED (-1.0f) #define ZR_FLAG(x) (1 << (x)) @@ -1038,15 +973,6 @@ enum zr_symbol { ZR_SYMBOL_MAX }; -struct zr_clipboard { - zr_handle userdata; - /* user memory for callback */ - zr_paste_f paste; - /* paste callback for the edit box */ - zr_copy_f copy; - /* copy callback for the edit box */ -}; - enum zr_widget_status { ZR_INACTIVE, ZR_HOVERED, @@ -1265,6 +1191,48 @@ struct zr_layout { struct zr_layout *parent; }; +/*============================================================== + * CONTEXT + * =============================================================*/ +struct zr_clipboard { + zr_handle userdata; + /* user memory for callback */ + zr_paste_f paste; + /* paste callback for the edit box */ + zr_copy_f copy; + /* copy callback for the edit box */ +}; + +struct zr_canvas { + enum zr_anti_aliasing AA; + /* flag indicating if anti-aliasing should be used to render primtives */ + struct zr_draw_null_texture null; + /* texture with white pixel for easy primitive drawing */ + struct zr_rect clip_rect; + /* current clipping rectangle */ + /* cosine/sine calculation callback since this library does not use libc */ + struct zr_buffer *buffer; + /* buffer to store draw commands and temporarily store path */ + struct zr_buffer *vertexes; + /* buffer to store each draw vertex */ + struct zr_buffer *elements; + /* buffer to store each draw element index */ + unsigned int element_count; + /* total number of elements inside the elements buffer */ + unsigned int vertex_count; + /* total number of vertexes inside the vertex buffer */ + zr_size cmd_offset; + /* offset to the first command in the buffer */ + unsigned int cmd_count; + /* number of commands inside the buffer */ + unsigned int path_count; + /* current number of points inside the path */ + unsigned int path_offset; + /* offset to the first point in the buffer */ + struct zr_vec2 circle_vtx[12]; + /* small lookup table for fast circle drawing */ +}; + struct zr_context { unsigned int seq; struct zr_input input; @@ -1293,10 +1261,6 @@ int zr_init_fixed(struct zr_context*, void *memory, zr_size size, const struct z int zr_init_custom(struct zr_context*, struct zr_buffer *cmds, struct zr_buffer *pool, const struct zr_user_font*); int zr_init(struct zr_context*, struct zr_allocator*, const struct zr_user_font*); -void zr_convert(struct zr_context*, struct zr_buffer *cmds, - struct zr_buffer *vertexes, struct zr_buffer *elements, - struct zr_draw_null_texture , enum zr_anti_aliasing, - float line_thickness, unsigned int circle_segment_count); void zr_clear(struct zr_context*); void zr_free(struct zr_context*); @@ -1327,20 +1291,71 @@ void zr_window_collapse(struct zr_context *ctx, const char *name, enum zr_collap void zr_window_collapse_if(struct zr_context *ctx, const char *name, enum zr_collapse_states, int cond); void zr_window_set_focus(struct zr_context *ctx, const char *name); -/* drawing */ +/*-------------------------------------------------------------- + * DRAWING + * -------------------------------------------------------------*/ +/* command drawing */ +#define zr_command(t, c) ((const struct zr_command_##t*)c) #define zr_foreach(c, ctx) for((c)=zr__begin(ctx); (c)!=0; (c)=zr__next(ctx, c)) -#define zr_draw_foreach(cmd,ctx, b) for((cmd)=zr__draw_begin(ctx, b); (cmd)!=0; (cmd)=zr__draw_next(cmd, b, ctx)) const struct zr_command* zr__next(struct zr_context*, const struct zr_command*); const struct zr_command* zr__begin(struct zr_context*); + +/* vertex command drawing */ +#define zr_draw_foreach(cmd,ctx, b) for((cmd)=zr__draw_begin(ctx, b); (cmd)!=0; (cmd)=zr__draw_next(cmd, b, ctx)) +void zr_convert(struct zr_context*, struct zr_buffer *cmds, + struct zr_buffer *vertexes, struct zr_buffer *elements, + struct zr_draw_null_texture , enum zr_anti_aliasing, + float line_thickness, unsigned int circle_segment_count); const struct zr_draw_command* zr__draw_begin(const struct zr_context*, const struct zr_buffer*); const struct zr_draw_command* zr__draw_next(const struct zr_draw_command*, - const struct zr_buffer*, - const struct zr_context*); + const struct zr_buffer*, const struct zr_context*); + +/*-------------------------------------------------------------- + * INPUT + * -------------------------------------------------------------*/ +void zr_input_begin(struct zr_context*); +void zr_input_motion(struct zr_context*, int x, int y); +void zr_input_key(struct zr_context*, enum zr_keys, int down); +void zr_input_button(struct zr_context*, enum zr_buttons, int x, int y, int down); +void zr_input_scroll(struct zr_context*, float y); +void zr_input_glyph(struct zr_context*, const zr_glyph); +void zr_input_char(struct zr_context*, char); +void zr_input_unicode(struct zr_context *in, zr_rune unicode); +void zr_input_end(struct zr_context*); + +/*-------------------------------------------------------------- + * STYLE + * -------------------------------------------------------------*/ +void zr_load_default_style(struct zr_context*, zr_flags); +void zr_set_font(struct zr_context*, const struct zr_user_font*); +struct zr_vec2 zr_get_property(const struct zr_context*, enum zr_style_properties); +struct zr_color zr_get_color(const struct zr_context*, enum zr_style_colors); +const char *zr_get_color_name(enum zr_style_colors); +const char *zr_get_rounding_name(enum zr_style_rounding); +const char *zr_get_property_name(enum zr_style_properties); + +/* temporarily modify a style value and save the old value in a stack*/ +void zr_push_property(struct zr_context*, enum zr_style_properties, struct zr_vec2); +void zr_push_color(struct zr_context*, enum zr_style_colors, struct zr_color); +void zr_push_font(struct zr_context*, struct zr_user_font font); +void zr_push_font_height(struct zr_context*, float font_height); + +/* restores a previously saved style value */ +void zr_pop_color(struct zr_context*); +void zr_pop_property(struct zr_context*); +void zr_pop_font(struct zr_context*); +void zr_pop_font_height(struct zr_context*); + +/* resets the style back into the beginning state */ +void zr_reset_colors(struct zr_context*); +void zr_reset_properties(struct zr_context*); +void zr_reset_font(struct zr_context*); +void zr_reset_font_height(struct zr_context*); +void zr_reset(struct zr_context*); /*-------------------------------------------------------------- * Layout * -------------------------------------------------------------*/ -/* layout query functions */ void zr_layout_peek(struct zr_rect *bounds, struct zr_context*); /* columns based layouting with generated position and width and fixed height*/ @@ -1378,7 +1393,6 @@ void zr_layout_pop(struct zr_context*); /*-------------------------------------------------------------- * Widgets * -------------------------------------------------------------*/ -/* base function called by all widgets (needs to be called for custom widgets) */ enum zr_widget_state zr_widget(struct zr_rect*, const struct zr_context*); enum zr_widget_state zr_widget_fitting(struct zr_rect*, struct zr_context*); void zr_spacing(struct zr_context*, zr_size cols);