diff --git a/blendish.h b/blendish.h index 8fcdf98..cc66bf6 100644 --- a/blendish.h +++ b/blendish.h @@ -35,7 +35,7 @@ extern "C" { /* -Revision 3 (2014-07-08) +Revision 4 (2014-07-09) Summary ------- @@ -545,6 +545,9 @@ void bndUpDownArrow(NVGcontext *ctx, float x, float y, float s, NVGcolor color); // max glyphs for position testing #define BND_MAX_GLYPHS 1024 +// text distance from bottom +#define BND_TEXT_PAD_DOWN 7 + //////////////////////////////////////////////////////////////////////////////// BND_INLINE float bnd_clamp(float v, float mn, float mx) { @@ -1168,7 +1171,7 @@ void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h, + nvgTextBounds(ctx, 1, 1, value, NULL, NULL); x += ((w-BND_PAD_RIGHT-pleft)-width)*0.5f; } - y += h-6; + y += h-BND_TEXT_PAD_DOWN; nvgText(ctx, x, y, label, NULL); x += label_width; nvgText(ctx, x, y, BND_LABEL_SEPARATOR, NULL); @@ -1178,7 +1181,8 @@ void bndIconLabelValue(NVGcontext *ctx, float x, float y, float w, float h, nvgTextAlign(ctx, (align==BND_LEFT)?(NVG_ALIGN_LEFT|NVG_ALIGN_BASELINE): (NVG_ALIGN_CENTER|NVG_ALIGN_BASELINE)); - nvgTextBox(ctx,x+pleft,y+h-6,w-BND_PAD_RIGHT-pleft,label, NULL); + nvgTextBox(ctx,x+pleft,y+h-BND_TEXT_PAD_DOWN, + w-BND_PAD_RIGHT-pleft,label, NULL); } } else if (iconid >= 0) { bndIcon(ctx,x+2,y+2,iconid); @@ -1199,7 +1203,7 @@ void bndIconLabelCaret(NVGcontext *ctx, float x, float y, float w, float h, if (bnd_font < 0) return; x+=pleft; - y+=h-6; + y+=h-BND_TEXT_PAD_DOWN; nvgFontFaceId(ctx, bnd_font); nvgFontSize(ctx, fontsize); diff --git a/example.cpp b/example.cpp index 6d180e9..c1026c5 100644 --- a/example.cpp +++ b/example.cpp @@ -16,6 +16,77 @@ #define BLENDISH_IMPLEMENTATION #include "blendish.h" +#include + +//////////////////////////////////////////////////////////////////////////////// + +#define UI_IMPLEMENTATION +#ifndef _UI_H_ +#define _UI_H_ + +typedef struct UIcontext UIcontext; + +UIcontext *uiCreateContext(); +void uiMakeCurrent(UIcontext *ctx); +void uiDestroyContext(UIcontext *ctx); + +void uiSetButton(int button, int enabled); +int uiGetButton(int button); + +void uiSetCursor(int x, int y); +void uiGetCursor(int *x, int *y); + +#endif // _UI_H_ + +#ifdef UI_IMPLEMENTATION + +struct UIcontext { + int cx, cy; + unsigned long long buttons; +}; + +static UIcontext *ui_context = NULL; + +UIcontext *uiCreateContext() { + UIcontext *ctx = (UIcontext *)malloc(sizeof(UIcontext)); + return ctx; +} + +void uiMakeCurrent(UIcontext *ctx) { + ui_context = ctx; +} + +void uiDestroyContext(UIcontext *ctx) { + free(ctx); +} + +void uiSetButton(int button, int enabled) { + assert(ui_context); + unsigned long long mask = 1ull<buttons = (enabled)? + (ui_context->buttons | mask): + (ui_context->buttons & ~mask); +} + +int uiGetButton(int button) { + assert(ui_context); + return (ui_context->buttons & (1ull<cx = x; + ui_context->cy = y; +} + +void uiGetCursor(int *x, int *y) { + assert(ui_context); + *x = ui_context->cx; + *y = ui_context->cy; +} + +#endif // UI_IMPLEMENTATION + //////////////////////////////////////////////////////////////////////////////// void init(NVGcontext *vg) { @@ -209,6 +280,9 @@ void draw(NVGcontext *vg, float w, float h) { x += BND_TOOL_WIDTH-1; bndRadioButton(vg,x,y,BND_TOOL_WIDTH,BND_WIDGET_HEIGHT,BND_CORNER_LEFT, BND_DEFAULT,BND_ICONID(5,11),NULL); + + + } //////////////////////////////////////////////////////////////////////////////// @@ -218,6 +292,17 @@ void errorcb(int error, const char* desc) printf("GLFW error %d: %s\n", error, desc); } +static void mousebutton(GLFWwindow *window, int button, int action, int mods) { + NVG_NOTUSED(window); + NVG_NOTUSED(mods); + uiSetButton(button, (action==GLFW_PRESS)?1:0); +} + +static void cursorpos(GLFWwindow *window, double x, double y) { + NVG_NOTUSED(window); + uiSetCursor((int)x,(int)y); +} + static void key(GLFWwindow* window, int key, int scancode, int action, int mods) { NVG_NOTUSED(scancode); @@ -230,6 +315,10 @@ int main() { GLFWwindow* window; struct NVGcontext* vg = NULL; + UIcontext *uictx; + + uictx = uiCreateContext(); + uiMakeCurrent(uictx); if (!glfwInit()) { printf("Failed to init GLFW."); @@ -252,6 +341,8 @@ int main() } glfwSetKeyCallback(window, key); + glfwSetCursorPosCallback(window, cursorpos); + glfwSetMouseButtonCallback(window, mousebutton); glfwMakeContextCurrent(window); #ifdef NANOVG_GLEW @@ -304,6 +395,8 @@ int main() glfwPollEvents(); } + uiDestroyContext(uictx); + nvgDeleteGL3(vg); glfwTerminate();