Nuklear
This is a minimal-state, immediate-mode graphical user interface toolkit written in ANSI C and licensed under public domain. It was designed as a simple embeddable user interface for application and does not have any dependencies, a default render backend or OS window/input handling but instead provides a highly modular, library-based approach, with simple input state for input and draw commands describing primitive shapes as output. So instead of providing a layered library that tries to abstract over a number of platform and render backends, it focuses only on the actual UI.
 
Loading...
Searching...
No Matches
nuklear_scrollbar.c
1#include "nuklear.h"
2#include "nuklear_internal.h"
3
4/* ===============================================================
5 *
6 * SCROLLBAR
7 *
8 * ===============================================================*/
9NK_LIB float
10nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
11 int has_scrolling, const struct nk_rect *scroll,
12 const struct nk_rect *cursor, const struct nk_rect *empty0,
13 const struct nk_rect *empty1, float scroll_offset,
14 float target, float scroll_step, enum nk_orientation o)
15{
16 nk_flags ws = 0;
17 int left_mouse_down;
18 unsigned int left_mouse_clicked;
19 int left_mouse_click_in_cursor;
20 float scroll_delta;
21
22 nk_widget_state_reset(state);
23 if (!in) return scroll_offset;
24
25 left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
26 left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
27 left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
28 NK_BUTTON_LEFT, *cursor, nk_true);
29 if (nk_input_is_mouse_hovering_rect(in, *scroll))
31
32 scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
33 if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
34 /* update cursor by mouse dragging */
35 float pixel, delta;
37 if (o == NK_VERTICAL) {
38 float cursor_y;
39 pixel = in->mouse.delta.y;
40 delta = (pixel / scroll->h) * target;
41 scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
42 cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
43 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
44 } else {
45 float cursor_x;
46 pixel = in->mouse.delta.x;
47 delta = (pixel / scroll->w) * target;
48 scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
49 cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
50 in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
51 }
52 } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
53 nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
54 /* scroll page up by click on empty space or shortcut */
55 if (o == NK_VERTICAL)
56 scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
57 else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
58 } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
59 nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
60 /* scroll page down by click on empty space or shortcut */
61 if (o == NK_VERTICAL)
62 scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
63 else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
64 } else if (has_scrolling) {
65 if ((scroll_delta < 0 || (scroll_delta > 0))) {
66 /* update cursor by mouse scrolling */
67 scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
68 if (o == NK_VERTICAL)
69 scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
70 else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
71 } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
72 /* update cursor to the beginning */
73 if (o == NK_VERTICAL) scroll_offset = 0;
74 } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
75 /* update cursor to the end */
76 if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
77 }
78 }
79 if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
81 else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
82 *state |= NK_WIDGET_STATE_LEFT;
83 return scroll_offset;
84}
85NK_LIB void
86nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
87 const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
88 const struct nk_rect *scroll)
89{
90 const struct nk_style_item *background;
91 const struct nk_style_item *cursor;
92
93 /* select correct colors/images to draw */
94 if (state & NK_WIDGET_STATE_ACTIVED) {
95 background = &style->active;
96 cursor = &style->cursor_active;
97 } else if (state & NK_WIDGET_STATE_HOVER) {
98 background = &style->hover;
99 cursor = &style->cursor_hover;
100 } else {
101 background = &style->normal;
102 cursor = &style->cursor_normal;
103 }
104
105 /* draw background */
106 switch (background->type) {
107 case NK_STYLE_ITEM_IMAGE:
108 nk_draw_image(out, *bounds, &background->data.image, nk_white);
109 break;
110 case NK_STYLE_ITEM_NINE_SLICE:
111 nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
112 break;
113 case NK_STYLE_ITEM_COLOR:
114 nk_fill_rect(out, *bounds, style->rounding, background->data.color);
115 nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
116 break;
117 }
118
119 /* draw cursor */
120 switch (cursor->type) {
121 case NK_STYLE_ITEM_IMAGE:
122 nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
123 break;
124 case NK_STYLE_ITEM_NINE_SLICE:
125 nk_draw_nine_slice(out, *scroll, &cursor->data.slice, nk_white);
126 break;
127 case NK_STYLE_ITEM_COLOR:
128 nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
129 nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
130 break;
131 }
132}
133NK_LIB float
134nk_do_scrollbarv(nk_flags *state,
135 struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
136 float offset, float target, float step, float button_pixel_inc,
137 const struct nk_style_scrollbar *style, struct nk_input *in,
138 const struct nk_user_font *font)
139{
140 struct nk_rect empty_north;
141 struct nk_rect empty_south;
142 struct nk_rect cursor;
143
144 float scroll_step;
145 float scroll_offset;
146 float scroll_off;
147 float scroll_ratio;
148
149 NK_ASSERT(out);
150 NK_ASSERT(style);
151 NK_ASSERT(state);
152 if (!out || !style) return 0;
153
154 scroll.w = NK_MAX(scroll.w, 1);
155 scroll.h = NK_MAX(scroll.h, 0);
156 if (target <= scroll.h) return 0;
157
158 /* optional scrollbar buttons */
159 if (style->show_buttons) {
160 nk_flags ws;
161 float scroll_h;
162 struct nk_rect button;
163
164 button.x = scroll.x;
165 button.w = scroll.w;
166 button.h = scroll.w;
167
168 scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
169 scroll_step = NK_MIN(step, button_pixel_inc);
170
171 /* decrement button */
172 button.y = scroll.y;
173 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
174 NK_BUTTON_REPEATER, &style->dec_button, in, font))
175 offset = offset - scroll_step;
176
177 /* increment button */
178 button.y = scroll.y + scroll.h - button.h;
179 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
180 NK_BUTTON_REPEATER, &style->inc_button, in, font))
181 offset = offset + scroll_step;
182
183 scroll.y = scroll.y + button.h;
184 scroll.h = scroll_h;
185 }
186
187 /* calculate scrollbar constants */
188 scroll_step = NK_MIN(step, scroll.h);
189 scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
190 scroll_ratio = scroll.h / target;
191 scroll_off = scroll_offset / target;
192
193 /* calculate scrollbar cursor bounds */
194 cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
195 cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
196 cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
197 cursor.x = scroll.x + style->border + style->padding.x;
198
199 /* calculate empty space around cursor */
200 empty_north.x = scroll.x;
201 empty_north.y = scroll.y;
202 empty_north.w = scroll.w;
203 empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
204
205 empty_south.x = scroll.x;
206 empty_south.y = cursor.y + cursor.h;
207 empty_south.w = scroll.w;
208 empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
209
210 /* update scrollbar */
211 scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
212 &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
213 scroll_off = scroll_offset / target;
214 cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
215
216 /* draw scrollbar */
217 if (style->draw_begin) style->draw_begin(out, style->userdata);
218 nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
219 if (style->draw_end) style->draw_end(out, style->userdata);
220 return scroll_offset;
221}
222NK_LIB float
223nk_do_scrollbarh(nk_flags *state,
224 struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
225 float offset, float target, float step, float button_pixel_inc,
226 const struct nk_style_scrollbar *style, struct nk_input *in,
227 const struct nk_user_font *font)
228{
229 struct nk_rect cursor;
230 struct nk_rect empty_west;
231 struct nk_rect empty_east;
232
233 float scroll_step;
234 float scroll_offset;
235 float scroll_off;
236 float scroll_ratio;
237
238 NK_ASSERT(out);
239 NK_ASSERT(style);
240 if (!out || !style) return 0;
241
242 /* scrollbar background */
243 scroll.h = NK_MAX(scroll.h, 1);
244 scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
245 if (target <= scroll.w) return 0;
246
247 /* optional scrollbar buttons */
248 if (style->show_buttons) {
249 nk_flags ws;
250 float scroll_w;
251 struct nk_rect button;
252 button.y = scroll.y;
253 button.w = scroll.h;
254 button.h = scroll.h;
255
256 scroll_w = scroll.w - 2 * button.w;
257 scroll_step = NK_MIN(step, button_pixel_inc);
258
259 /* decrement button */
260 button.x = scroll.x;
261 if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
262 NK_BUTTON_REPEATER, &style->dec_button, in, font))
263 offset = offset - scroll_step;
264
265 /* increment button */
266 button.x = scroll.x + scroll.w - button.w;
267 if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
268 NK_BUTTON_REPEATER, &style->inc_button, in, font))
269 offset = offset + scroll_step;
270
271 scroll.x = scroll.x + button.w;
272 scroll.w = scroll_w;
273 }
274
275 /* calculate scrollbar constants */
276 scroll_step = NK_MIN(step, scroll.w);
277 scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
278 scroll_ratio = scroll.w / target;
279 scroll_off = scroll_offset / target;
280
281 /* calculate cursor bounds */
282 cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
283 cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
284 cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
285 cursor.y = scroll.y + style->border + style->padding.y;
286
287 /* calculate empty space around cursor */
288 empty_west.x = scroll.x;
289 empty_west.y = scroll.y;
290 empty_west.w = cursor.x - scroll.x;
291 empty_west.h = scroll.h;
292
293 empty_east.x = cursor.x + cursor.w;
294 empty_east.y = scroll.y;
295 empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
296 empty_east.h = scroll.h;
297
298 /* update scrollbar */
299 scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
300 &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
301 scroll_off = scroll_offset / target;
302 cursor.x = scroll.x + (scroll_off * scroll.w);
303
304 /* draw scrollbar */
305 if (style->draw_begin) style->draw_begin(out, style->userdata);
306 nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
307 if (style->draw_end) style->draw_end(out, style->userdata);
308 return scroll_offset;
309}
310
main API and documentation file
NK_API void nk_draw_image(struct nk_command_buffer *, struct nk_rect, const struct nk_image *, struct nk_color)
misc
NK_API void nk_fill_rect(struct nk_command_buffer *, struct nk_rect, float rounding, struct nk_color)
filled shades
@ NK_WIDGET_STATE_LEFT
!< widget is currently activated
Definition nuklear.h:3093
@ NK_WIDGET_STATE_ACTIVED
!< widget is being hovered
Definition nuklear.h:3092
@ NK_WIDGET_STATE_ENTERED
!< widget is neither active nor hovered
Definition nuklear.h:3090
@ NK_WIDGET_STATE_HOVER
!< widget has been hovered on the current frame
Definition nuklear.h:3091
@ NK_WIDGET_STATE_ACTIVE
!< widget is being hovered
Definition nuklear.h:3095
@ NK_WIDGET_STATE_HOVERED
!< widget is from this frame on not hovered anymore
Definition nuklear.h:3094