diff --git a/src/nuklear-heli.c b/src/nuklear-heli.c index f5df474..512599e 100644 --- a/src/nuklear-heli.c +++ b/src/nuklear-heli.c @@ -74,17 +74,17 @@ struct nk_image nk_heli_image(Animation anim, int width, int height) { } -void nk_heli_input(nk_heli *n) { - struct { +void nk_heli_input_ex(nk_heli *n, unsigned int flags) { + static const struct { enum nk_buttons nk; const char *hl; } buttons[] = { { NK_BUTTON_LEFT , "left" }, { NK_BUTTON_MIDDLE , "middle" }, { NK_BUTTON_RIGHT , "right" } }; - const int buttonsCnt = sizeof(buttons)/sizeof(*buttons); + static const int buttonsCnt = sizeof(buttons)/sizeof(*buttons); - struct { + static const struct { enum nk_keys nk; const char *hl; int ctrl; @@ -115,66 +115,97 @@ void nk_heli_input(nk_heli *n) { { NK_KEY_SCROLL_END , "end", 1 }, { NK_KEY_SCROLL_DOWN , "page down" }, { NK_KEY_SCROLL_UP , "page up" } }; - const int keysCnt = sizeof(keys)/sizeof(*keys); + static const int keysCnt = sizeof(keys)/sizeof(*keys); struct nk_context *c = &n->context; - double mx = mouseTransformedX(); - double my = mouseTransformedY(); nk_input_begin(c); - nk_input_motion(c, mx, my); + double mx = 0, my = 0; + if (flags & (NKH_INPUT_MOUSE_MOTION | NKH_INPUT_MOUSE_BUTTON | NKH_INPUT_RELEASE_MOUSE)) { + mx = mouseTransformedX(); + my = mouseTransformedY(); + } - struct nk_vec2 scroll = {}; - scroll.x = mouseScrolledX(); - scroll.y = mouseScrolledY(); - if (scroll.x || scroll.y) - nk_input_scroll(c, scroll); + if (flags & NKH_INPUT_MOUSE_MOTION) + nk_input_motion(c, mx, my); - for(int i = 0; i < buttonsCnt; ++i) { - if (mouseWentDown(buttons[i].hl)) - nk_input_button(c, buttons[i].nk, mx, my, 1); - if (mouseWentUp(buttons[i].hl)) - nk_input_button(c, buttons[i].nk, mx, my, 0); + if (flags & NKH_INPUT_MOUSE_SCROLL) { + struct nk_vec2 scroll = {}; + scroll.x = mouseScrolledX(); + scroll.y = mouseScrolledY(); + if (scroll.x || scroll.y) + nk_input_scroll(c, scroll); } + if (flags & NKH_INPUT_MOUSE_BUTTON) { + for(int i = 0; i < buttonsCnt; ++i) { + int *b = &n->buttons[buttons[i].nk]; + if (!*b && mouseWentDown(buttons[i].hl)) { + nk_input_button(c, buttons[i].nk, mx, my, 1); + *b = 1; + } + if (*b && mouseWentUp(buttons[i].hl)) { + nk_input_button(c, buttons[i].nk, mx, my, 0); + *b = 0; + } + } + } + + if (flags & NKH_INPUT_RELEASE_MOUSE) + for(int i = 0; i < NK_BUTTON_MAX; ++i) + if (n->buttons[i]) nk_input_button(c, i, mx, my, 0); + - int modes[] = { KEYEVENT_KEY_WENTUP, KEYEVENT_KEY_WENTDOWN }; - int ctrl = keyDown("any ctrl") || keyWentDown("any ctrl"); - for(int down = 1; down >= 0; --down) - for(int i = 0; i < keyEventGetCount(modes[down]); ++i) { - const char *k = keyEventGet(modes[down], i); - for(int j = 0; j < keysCnt; ++j) { - int ct = keys[j].ctrl; - if ( (ct > 0 && !ctrl) - || (ct < 0 && ctrl) - || 0 != strcmp(k, keys[j].hl) ) - continue; - nk_input_key(c, keys[j].nk, down); + if (flags & NKH_INPUT_KEYBOARD_KEY) { + static const int modes[] = { KEYEVENT_KEY_WENTUP, KEYEVENT_KEY_WENTDOWN }; + int ctrl = keyDown("any ctrl") || keyWentDown("any ctrl"); + for(int down = 1; down >= 0; --down) + for(int i = 0; i < keyEventGetCount(modes[down]); ++i) { + const char *k = keyEventGet(modes[down], i); + for(int j = 0; j < keysCnt; ++j) { + int ct = keys[j].ctrl; + if ( (ct > 0 && !ctrl) + || (ct < 0 && ctrl) + || 0 != strcmp(k, keys[j].hl) ) + continue; + nk_input_key(c, keys[j].nk, down); + } } } - const char *t = textInputGet(); - while(*t) { - int len = (t[1] & 0xC0) == 0x80 ? 2 - : (t[0] & 0xF0) == 0xE0 - && (t[1] & 0xC0) == 0x80 - && (t[2] & 0xC0) == 0x80 ? 3 - : (t[0] & 0xF8) == 0xF0 - && (t[1] & 0xC0) == 0x80 - && (t[2] & 0xC0) == 0x80 - && (t[3] & 0xC0) == 0x80 ? 4 : 1; - nk_glyph g = {}; - memcpy(g, t, len); - nk_input_glyph(c, g); - t += len; + if (flags & NKH_INPUT_RELEASE_KEYBOARD) + for(int i = 0; i < NK_KEY_MAX; ++i) + if (n->keys[i]) nk_input_key(c, i, 0); + + if (flags & NKH_INPUT_KEYBOARD_TEXT) { + const char *t = textInputGet(); + while(*t) { + int len = (t[1] & 0xC0) == 0x80 ? 2 + : (t[0] & 0xF0) == 0xE0 + && (t[1] & 0xC0) == 0x80 + && (t[2] & 0xC0) == 0x80 ? 3 + : (t[0] & 0xF8) == 0xF0 + && (t[1] & 0xC0) == 0x80 + && (t[2] & 0xC0) == 0x80 + && (t[3] & 0xC0) == 0x80 ? 4 : 1; + nk_glyph g = {}; + memcpy(g, t, len); + nk_input_glyph(c, g); + t += len; + } } - textInputClear(); + if (flags & NKH_INPUT_KEYBOARD_TEXT_CAPTURE) + textInputClear(); nk_input_end(c); } +void nk_heli_input(nk_heli *n) + { nk_heli_input_ex(n, NKH_INPUT_DAFAULT); } + + void nk_heli_draw(nk_heli *n) { saveState(); diff --git a/src/nuklear-heli.h b/src/nuklear-heli.h index ce02432..e8ca017 100644 --- a/src/nuklear-heli.h +++ b/src/nuklear-heli.h @@ -19,9 +19,33 @@ #include "nuklear.h" +enum { + NKH_INPUT_MOUSE_MOTION = 1 << 0, + NKH_INPUT_MOUSE_BUTTON = 1 << 1, + NKH_INPUT_MOUSE_SCROLL = 1 << 2, + NKH_INPUT_MOUSE = NKH_INPUT_MOUSE_MOTION + | NKH_INPUT_MOUSE_BUTTON + | NKH_INPUT_MOUSE_SCROLL, + NKH_INPUT_KEYBOARD_KEY = 1 << 3, + NKH_INPUT_KEYBOARD_TEXT = 1 << 4, + NKH_INPUT_KEYBOARD_TEXT_CAPTURE = 1 << 5, + NKH_INPUT_KEYBOARD = NKH_INPUT_KEYBOARD_KEY + | NKH_INPUT_KEYBOARD_TEXT + | NKH_INPUT_KEYBOARD_TEXT_CAPTURE, + NKH_INPUT_DAFAULT = NKH_INPUT_MOUSE + | NKH_INPUT_KEYBOARD, + NKH_INPUT_RELEASE_MOUSE = 1 << 6, + NKH_INPUT_RELEASE_KEYBOARD = 1 << 7, + NKH_INPUT_RELEASE = NKH_INPUT_RELEASE_MOUSE + | NKH_INPUT_RELEASE_KEYBOARD, +}; + + typedef struct { struct nk_context context; struct nk_user_font font; + int keys[NK_KEY_MAX]; + int buttons[NK_BUTTON_MAX]; } nk_heli; @@ -32,6 +56,7 @@ struct nk_color nk_color_from_heli(unsigned int c); nk_bool nk_heli_init(nk_heli *n, double fontSize); void nk_heli_deinit(nk_heli *n); struct nk_image nk_heli_image(Animation anim, int width, int height); +void nk_heli_input_ex(nk_heli *n, unsigned int flags); void nk_heli_input(nk_heli *n); void nk_heli_draw(nk_heli *n); void nk_heli_process(nk_heli *n);