Added horizontal scrolling

This commit is contained in:
vurtun 2017-03-21 21:21:58 +01:00
parent a979372909
commit 23eea231d8
14 changed files with 85 additions and 90 deletions

View File

@ -95,11 +95,9 @@ static float
nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text, int len)
{
NkAllegro5Font *font = (NkAllegro5Font*)handle.ptr;
if (!font || !text) {
return 0;
}
/* We must copy into a new buffer with exact length null-terminated
as nuklear uses variable size buffers and al_get_text_width doesn't
accept a length, it infers length from null-termination
@ -107,7 +105,6 @@ nk_allegro5_font_get_text_width(nk_handle handle, float height, const char *text
char strcpy[len+1];
strncpy((char*)&strcpy, text, len);
strcpy[len] = '\0';
return al_get_text_width(font->font, strcpy);
}
@ -289,7 +286,7 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
case ALLEGRO_EVENT_MOUSE_AXES: {
nk_input_motion(ctx, ev->mouse.x, ev->mouse.y);
if (ev->mouse.dz != 0) {
nk_input_scroll(ctx, (float)ev->mouse.dz / al_get_mouse_wheel_precision());
nk_input_scroll(ctx, nk_vec2(0,(float)ev->mouse.dz / al_get_mouse_wheel_precision()));
}
} break;
case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
@ -342,7 +339,7 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
case ALLEGRO_EVENT_KEY_UP: {
int kc = ev->keyboard.keycode;
int down = ev->type == ALLEGRO_EVENT_KEY_DOWN;
if (kc == ALLEGRO_KEY_LSHIFT || kc == ALLEGRO_KEY_RSHIFT) nk_input_key(ctx, NK_KEY_SHIFT, down);
else if (kc == ALLEGRO_KEY_DELETE) nk_input_key(ctx, NK_KEY_DEL, down);
else if (kc == ALLEGRO_KEY_ENTER) nk_input_key(ctx, NK_KEY_ENTER, down);
@ -367,7 +364,7 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
int kc = ev->keyboard.keycode;
int control_mask = (ev->keyboard.modifiers & ALLEGRO_KEYMOD_CTRL) ||
(ev->keyboard.modifiers & ALLEGRO_KEYMOD_COMMAND);
if (kc == ALLEGRO_KEY_C && control_mask) {
nk_input_key(ctx, NK_KEY_COPY, 1);
} else if (kc == ALLEGRO_KEY_V && control_mask) {
@ -380,9 +377,7 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
nk_input_key(ctx, NK_KEY_TEXT_REDO, 1);
} else if (kc == ALLEGRO_KEY_A && control_mask) {
nk_input_key(ctx, NK_KEY_TEXT_SELECT_ALL, 1);
}
else {
} else {
if (kc != ALLEGRO_KEY_BACKSPACE &&
kc != ALLEGRO_KEY_LEFT &&
kc != ALLEGRO_KEY_RIGHT &&
@ -398,7 +393,6 @@ nk_allegro5_handle_event(ALLEGRO_EVENT *ev)
nk_input_unicode(ctx, ev->keyboard.unichar);
}
}
} break;
default: break;
}
@ -462,3 +456,4 @@ void nk_allegro5_shutdown(void)
}
#endif /* NK_ALLEGRO5_IMPLEMENTATION */

View File

@ -341,7 +341,7 @@ nk_d3d11_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
return 1;
case WM_MOUSEWHEEL:
nk_input_scroll(&d3d11.ctx, (float)(short)HIWORD(wparam) / WHEEL_DELTA);
nk_input_scroll(&d3d11.ctx, nk_vec2(0,(float)(short)HIWORD(wparam) / WHEEL_DELTA));
return 1;
case WM_MOUSEMOVE:

View File

@ -652,7 +652,7 @@ nk_gdi_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
return 1;
case WM_MOUSEWHEEL:
nk_input_scroll(&gdi.ctx, (float)(short)HIWORD(wparam) / WHEEL_DELTA);
nk_input_scroll(&gdi.ctx, nk_vec2(0,(float)(short)HIWORD(wparam) / WHEEL_DELTA));
return 1;
case WM_MOUSEMOVE:

View File

@ -1024,7 +1024,7 @@ nk_gdip_handle_event(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
return 1;
case WM_MOUSEWHEEL:
nk_input_scroll(&gdip.ctx, (float)(short)HIWORD(wparam) / WHEEL_DELTA);
nk_input_scroll(&gdip.ctx, nk_vec2(0,(float)(short)HIWORD(wparam) / WHEEL_DELTA));
return 1;
case WM_MOUSEMOVE:

View File

@ -67,7 +67,7 @@ static struct nk_glfw {
struct nk_vec2 fb_scale;
unsigned int text[NK_GLFW_TEXT_MAX];
int text_len;
float scroll;
struct nk_vec2 scroll;
} glfw;
NK_INTERN void
@ -200,7 +200,8 @@ NK_API void
nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
{
(void)win; (void)xoff;
glfw.scroll += (float)yoff;
glfw.scroll.x += (float)xoff;
glfw.scroll.y += (float)yoff;
}
NK_INTERN void
@ -333,7 +334,7 @@ nk_glfw3_new_frame(void)
nk_input_scroll(ctx, glfw.scroll);
nk_input_end(&glfw.ctx);
glfw.text_len = 0;
glfw.scroll = 0;
glfw.scroll = nk_vec2(0,0);
}
NK_API

View File

@ -78,7 +78,7 @@ static struct nk_glfw {
struct nk_vec2 fb_scale;
unsigned int text[NK_GLFW_TEXT_MAX];
int text_len;
float scroll;
struct nk_vec2 scroll;
} glfw;
#ifdef __APPLE__
@ -308,7 +308,8 @@ NK_API void
nk_gflw3_scroll_callback(GLFWwindow *win, double xoff, double yoff)
{
(void)win; (void)xoff;
glfw.scroll += (float)yoff;
glfw.scroll.x += (float)xoff;
glfw.scroll.y += (float)yoff;
}
NK_INTERN void
@ -441,7 +442,7 @@ nk_glfw3_new_frame(void)
nk_input_scroll(ctx, glfw.scroll);
nk_input_end(&glfw.ctx);
glfw.text_len = 0;
glfw.scroll = 0;
glfw.scroll = nk_vec2(0,0);
}
NK_API

View File

@ -322,7 +322,7 @@ nk_sdl_handle_event(SDL_Event *evt)
nk_input_glyph(ctx, glyph);
return 1;
} else if (evt->type == SDL_MOUSEWHEEL) {
nk_input_scroll(ctx,(float)evt->wheel.y);
nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y));
return 1;
}
return 0;

View File

@ -419,7 +419,7 @@ nk_sdl_handle_event(SDL_Event *evt)
nk_input_glyph(ctx, glyph);
return 1;
} else if (evt->type == SDL_MOUSEWHEEL) {
nk_input_scroll(ctx,(float)evt->wheel.y);
nk_input_scroll(ctx,nk_vec2((float)evt->wheel.x,(float)evt->wheel.y));
return 1;
}
return 0;

View File

@ -332,7 +332,7 @@ nk_sfml_handle_event(sf::Event* evt)
nk_input_unicode(ctx, evt->text.unicode);
return 1;
} else if(evt->type == sf::Event::MouseWheelScrolled) {
nk_input_scroll(ctx, evt->mouseWheelScroll.delta);
nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta));
return 1;
}
return 0;

View File

@ -445,7 +445,7 @@ nk_sfml_handle_event(sf::Event* evt)
nk_input_unicode(ctx, evt->text.unicode);
return 1;
} else if(evt->type == sf::Event::MouseWheelScrolled) {
nk_input_scroll(ctx, evt->mouseWheelScroll.delta);
nk_input_scroll(ctx, nk_vec2(0,evt->mouseWheelScroll.delta));
return 1;
}
return 0;

View File

@ -616,9 +616,9 @@ nk_xlib_handle_event(Display *dpy, int screen, Window win, XEvent *evt)
else if (evt->xbutton.button == Button3)
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
else if (evt->xbutton.button == Button4)
nk_input_scroll(ctx, 1.0f);
nk_input_scroll(ctx, nk_vec2(0, 1.0f));
else if (evt->xbutton.button == Button5)
nk_input_scroll(ctx, -1.0f);
nk_input_scroll(ctx, nk_vec2(0, -1.0f));
else return 0;
return 1;
} else if (evt->type == MotionNotify) {

View File

@ -298,9 +298,9 @@ nk_x11_handle_event(XEvent *evt)
else if (evt->xbutton.button == Button3)
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
else if (evt->xbutton.button == Button4)
nk_input_scroll(ctx, 1.0f);
nk_input_scroll(ctx, nk_vec2(0,1.0f));
else if (evt->xbutton.button == Button5)
nk_input_scroll(ctx, -1.0f);
nk_input_scroll(ctx, nk_vec2(0,-1.0f));
else return 0;
return 1;
} else if (evt->type == MotionNotify) {

View File

@ -668,9 +668,9 @@ nk_x11_handle_event(XEvent *evt)
else if (evt->xbutton.button == Button3)
nk_input_button(ctx, NK_BUTTON_RIGHT, x, y, down);
else if (evt->xbutton.button == Button4)
nk_input_scroll(ctx, 1.0f);
nk_input_scroll(ctx, nk_vec2(0,1.0f));
else if (evt->xbutton.button == Button5)
nk_input_scroll(ctx, -1.0f);
nk_input_scroll(ctx, nk_vec2(0,-1.0f));
else return 0;
return 1;
} else if (evt->type == MotionNotify) {

120
nuklear.h
View File

@ -1003,7 +1003,7 @@ NK_API void nk_input_begin(struct nk_context*);
NK_API void nk_input_motion(struct nk_context*, int x, int y);
NK_API void nk_input_key(struct nk_context*, enum nk_keys, int down);
NK_API void nk_input_button(struct nk_context*, enum nk_buttons, int x, int y, int down);
NK_API void nk_input_scroll(struct nk_context*, float y);
NK_API void nk_input_scroll(struct nk_context*, struct nk_vec2 val);
NK_API void nk_input_char(struct nk_context*, char);
NK_API void nk_input_glyph(struct nk_context*, const nk_glyph);
NK_API void nk_input_unicode(struct nk_context*, nk_rune);
@ -2010,7 +2010,7 @@ struct nk_mouse {
struct nk_vec2 pos;
struct nk_vec2 prev;
struct nk_vec2 delta;
float scroll_delta;
struct nk_vec2 scroll_delta;
unsigned char grab;
unsigned char grabbed;
unsigned char ungrab;
@ -11319,7 +11319,7 @@ nk_input_begin(struct nk_context *ctx)
in->mouse.buttons[i].clicked = 0;
in->keyboard.text_len = 0;
in->mouse.scroll_delta = 0;
in->mouse.scroll_delta = nk_vec2(0,0);
in->mouse.prev.x = in->mouse.pos.x;
in->mouse.prev.y = in->mouse.pos.y;
in->mouse.delta.x = 0;
@ -11386,11 +11386,12 @@ nk_input_button(struct nk_context *ctx, enum nk_buttons id, int x, int y, int do
}
NK_API void
nk_input_scroll(struct nk_context *ctx, float y)
nk_input_scroll(struct nk_context *ctx, struct nk_vec2 val)
{
NK_ASSERT(ctx);
if (!ctx) return;
ctx->input.mouse.scroll_delta += y;
ctx->input.mouse.scroll_delta.x += val.x;
ctx->input.mouse.scroll_delta.y += val.y;
}
NK_API void
@ -13762,6 +13763,7 @@ nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
nk_flags ws;
int left_mouse_down;
int left_mouse_click_in_cursor;
float scroll_delta;
nk_widget_state_reset(state);
if (!in) return scroll_offset;
@ -13772,6 +13774,7 @@ nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
if (nk_input_is_mouse_hovering_rect(in, *scroll))
*state = NK_WIDGET_STATE_HOVERED;
scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
if (left_mouse_down && left_mouse_click_in_cursor) {
/* update cursor by mouse dragging */
float pixel, delta;
@ -13804,9 +13807,9 @@ nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
} else if (has_scrolling) {
if ((in->mouse.scroll_delta<0 || (in->mouse.scroll_delta>0))) {
if ((scroll_delta < 0 || (scroll_delta > 0))) {
/* update cursor by mouse scrolling */
scroll_offset = scroll_offset + scroll_step * (-in->mouse.scroll_delta);
scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
if (o == NK_VERTICAL)
scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
@ -16744,6 +16747,44 @@ nk_panel_end(struct nk_context *ctx)
float scroll_offset;
float scroll_step;
float scroll_inc;
/* mouse wheel scrolling */
if (nk_panel_is_sub(layout->type))
{
/* sub-window mouse wheel scrolling */
struct nk_window *root_window = window;
struct nk_panel *root_panel = window->layout;
while (root_panel->parent)
root_panel = root_panel->parent;
while (root_window->parent)
root_window = root_window->parent;
/* only allow scrolling if parent window is active */
scroll_has_scrolling = 0;
if ((root_window == ctx->active) && layout->has_scrolling) {
/* and panel is being hovered and inside clip rect*/
if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
{
/* deactivate all parent scrolling */
root_panel = window->layout;
while (root_panel->parent) {
root_panel->has_scrolling = nk_false;
root_panel = root_panel->parent;
}
root_panel->has_scrolling = nk_false;
scroll_has_scrolling = nk_true;
}
}
} else if (!nk_panel_is_sub(layout->type)) {
/* window mouse wheel scrolling */
scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
if (in && (in->mouse.scroll_delta.y > 0 || in->mouse.scroll_delta.x > 0) && scroll_has_scrolling)
window->scrolled = nk_true;
else window->scrolled = nk_false;
} else scroll_has_scrolling = nk_false;
{
/* vertical scrollbar */
nk_flags state = 0;
@ -16756,51 +16797,12 @@ nk_panel_end(struct nk_context *ctx)
scroll_step = scroll.h * 0.10f;
scroll_inc = scroll.h * 0.01f;
scroll_target = (float)(int)(layout->at_y - scroll.y);
/* scrolling by mouse wheel */
if (nk_panel_is_sub(layout->type))
{
/* sub-window scrollbar wheel scrolling */
struct nk_window *root_window = window;
struct nk_panel *root_panel = window->layout;
while (root_panel->parent)
root_panel = root_panel->parent;
while (root_window->parent)
root_window = root_window->parent;
/* only allow scrolling if parent window is active */
scroll_has_scrolling = 0;
if ((root_window == ctx->active) && layout->has_scrolling) {
/* and panel is being hovered and inside clip rect*/
if (nk_input_is_mouse_hovering_rect(in, layout->bounds) &&
NK_INTERSECT(layout->bounds.x, layout->bounds.y, layout->bounds.w, layout->bounds.h,
root_panel->clip.x, root_panel->clip.y, root_panel->clip.w, root_panel->clip.h))
{
/* deactivate all parent scrolling */
root_panel = window->layout;
while (root_panel->parent) {
root_panel->has_scrolling = nk_false;
root_panel = root_panel->parent;
}
root_panel->has_scrolling = nk_false;
scroll_has_scrolling = nk_true;
}
}
} else if (!nk_panel_is_sub(layout->type)) {
/* window scrollbar wheel scrolling */
scroll_has_scrolling = (window == ctx->active) && layout->has_scrolling;
if (in && in->mouse.scroll_delta > 0 && scroll_has_scrolling)
window->scrolled = nk_true;
else window->scrolled = nk_false;
} else scroll_has_scrolling = nk_false;
/* execute scrollbar */
scroll_offset = nk_do_scrollbarv(&state, out, scroll, scroll_has_scrolling,
scroll_offset, scroll_target, scroll_step, scroll_inc,
&ctx->style.scrollv, in, style->font);
scroll_offset, scroll_target, scroll_step, scroll_inc,
&ctx->style.scrollv, in, style->font);
*layout->offset_y = (nk_uint)scroll_offset;
if (in && scroll_has_scrolling)
in->mouse.scroll_delta = 0;
in->mouse.scroll_delta.y = 0;
}
{
/* horizontal scrollbar */
@ -16814,17 +16816,17 @@ nk_panel_end(struct nk_context *ctx)
scroll_target = (float)(int)(layout->max_x - scroll.x);
scroll_step = layout->max_x * 0.05f;
scroll_inc = layout->max_x * 0.005f;
scroll_has_scrolling = nk_false;
scroll_has_scrolling = scroll_has_scrolling;
scroll_offset = nk_do_scrollbarh(&state, out, scroll, scroll_has_scrolling,
scroll_offset, scroll_target, scroll_step, scroll_inc,
&ctx->style.scrollh, in, style->font);
scroll_offset, scroll_target, scroll_step, scroll_inc,
&ctx->style.scrollh, in, style->font);
*layout->offset_x = (nk_uint)scroll_offset;
}
}
/* hide scroll if no user input */
if (window->flags & NK_WINDOW_SCROLL_AUTO_HIDE) {
int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta != 0;
int has_input = ctx->input.mouse.delta.x != 0 || ctx->input.mouse.delta.y != 0 || ctx->input.mouse.scroll_delta.y != 0;
int is_window_hovered = nk_window_is_hovered(ctx);
int any_item_active = (ctx->last_widget_state & NK_WIDGET_STATE_MODIFIED);
if ((!has_input && is_window_hovered) || (!is_window_hovered && !any_item_active))
@ -16915,7 +16917,6 @@ nk_panel_end(struct nk_context *ctx)
}
}
}
if (!nk_panel_is_sub(layout->type)) {
/* window is hidden so clear command buffer */
if (layout->flags & NK_WINDOW_HIDDEN)
@ -16940,7 +16941,6 @@ nk_panel_end(struct nk_context *ctx)
window->property.prev = window->property.active;
window->property.seq = 0;
}
/* edit garbage collector */
if (window->edit.active && window->edit.old != window->edit.seq &&
window->edit.active == window->edit.prev) {
@ -16950,7 +16950,6 @@ nk_panel_end(struct nk_context *ctx)
window->edit.prev = window->edit.active;
window->edit.seq = 0;
}
/* contextual garbage collector */
if (window->popup.active_con && window->popup.con_old != window->popup.con_count) {
window->popup.con_count = 0;
@ -16962,7 +16961,7 @@ nk_panel_end(struct nk_context *ctx)
}
window->popup.combo_count = 0;
/* helper to make sure you have a 'nk_tree_push' * for every 'nk_tree_pop' */
/* helper to make sure you have a 'nk_tree_push' for every 'nk_tree_pop' */
NK_ASSERT(!layout->row.tree_depth);
}
@ -16985,7 +16984,7 @@ nk_create_page_element(struct nk_context *ctx)
NK_ASSERT(elem);
if (!elem) return 0;
} else {
/* allocate new page element from the back of the fixed size memory buffer */
/* allocate new page element from back of fixed size memory buffer */
NK_STORAGE const nk_size size = sizeof(struct nk_page_element);
NK_STORAGE const nk_size align = NK_ALIGNOF(struct nk_page_element);
elem = (struct nk_page_element*)nk_buffer_alloc(&ctx->memory, NK_BUFFER_BACK, size, align);
@ -17019,7 +17018,6 @@ nk_free_page_element(struct nk_context *ctx, struct nk_page_element *elem)
nk_link_page_element_into_freelist(ctx, elem);
return;
}
/* if possible remove last element from back of fixed memory buffer */
{void *elem_end = (void*)(elem + 1);
void *buffer_end = (nk_byte*)ctx->memory.memory.ptr + ctx->memory.size;
@ -17163,7 +17161,7 @@ NK_INTERN void
nk_free_window(struct nk_context *ctx, struct nk_window *win)
{
/* unlink windows from list */
struct nk_table *n, *it = win->tables;
struct nk_table *it = win->tables;
if (win->popup.win) {
nk_free_window(ctx, win->popup.win);
win->popup.win = 0;
@ -17173,7 +17171,7 @@ nk_free_window(struct nk_context *ctx, struct nk_window *win)
while (it) {
/*free window state tables */
n = it->next;
struct nk_table *n = it->next;
nk_remove_table(win, it);
nk_free_table(ctx, it);
if (it == win->tables)