nuklear_scrollbar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include "nuklear.h"
  2. #include "nuklear_internal.h"
  3. /* ===============================================================
  4. *
  5. * SCROLLBAR
  6. *
  7. * ===============================================================*/
  8. NK_LIB float
  9. nk_scrollbar_behavior(nk_flags *state, struct nk_input *in,
  10. int has_scrolling, const struct nk_rect *scroll,
  11. const struct nk_rect *cursor, const struct nk_rect *empty0,
  12. const struct nk_rect *empty1, float scroll_offset,
  13. float target, float scroll_step, enum nk_orientation o)
  14. {
  15. nk_flags ws = 0;
  16. int left_mouse_down;
  17. unsigned int left_mouse_clicked;
  18. int left_mouse_click_in_cursor;
  19. float scroll_delta;
  20. nk_widget_state_reset(state);
  21. if (!in) return scroll_offset;
  22. left_mouse_down = in->mouse.buttons[NK_BUTTON_LEFT].down;
  23. left_mouse_clicked = in->mouse.buttons[NK_BUTTON_LEFT].clicked;
  24. left_mouse_click_in_cursor = nk_input_has_mouse_click_down_in_rect(in,
  25. NK_BUTTON_LEFT, *cursor, nk_true);
  26. if (nk_input_is_mouse_hovering_rect(in, *scroll))
  27. *state = NK_WIDGET_STATE_HOVERED;
  28. scroll_delta = (o == NK_VERTICAL) ? in->mouse.scroll_delta.y: in->mouse.scroll_delta.x;
  29. if (left_mouse_down && left_mouse_click_in_cursor && !left_mouse_clicked) {
  30. /* update cursor by mouse dragging */
  31. float pixel, delta;
  32. *state = NK_WIDGET_STATE_ACTIVE;
  33. if (o == NK_VERTICAL) {
  34. float cursor_y;
  35. pixel = in->mouse.delta.y;
  36. delta = (pixel / scroll->h) * target;
  37. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->h);
  38. cursor_y = scroll->y + ((scroll_offset/target) * scroll->h);
  39. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.y = cursor_y + cursor->h/2.0f;
  40. } else {
  41. float cursor_x;
  42. pixel = in->mouse.delta.x;
  43. delta = (pixel / scroll->w) * target;
  44. scroll_offset = NK_CLAMP(0, scroll_offset + delta, target - scroll->w);
  45. cursor_x = scroll->x + ((scroll_offset/target) * scroll->w);
  46. in->mouse.buttons[NK_BUTTON_LEFT].clicked_pos.x = cursor_x + cursor->w/2.0f;
  47. }
  48. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_UP) && o == NK_VERTICAL && has_scrolling)||
  49. nk_button_behavior(&ws, *empty0, in, NK_BUTTON_DEFAULT)) {
  50. /* scroll page up by click on empty space or shortcut */
  51. if (o == NK_VERTICAL)
  52. scroll_offset = NK_MAX(0, scroll_offset - scroll->h);
  53. else scroll_offset = NK_MAX(0, scroll_offset - scroll->w);
  54. } else if ((nk_input_is_key_pressed(in, NK_KEY_SCROLL_DOWN) && o == NK_VERTICAL && has_scrolling) ||
  55. nk_button_behavior(&ws, *empty1, in, NK_BUTTON_DEFAULT)) {
  56. /* scroll page down by click on empty space or shortcut */
  57. if (o == NK_VERTICAL)
  58. scroll_offset = NK_MIN(scroll_offset + scroll->h, target - scroll->h);
  59. else scroll_offset = NK_MIN(scroll_offset + scroll->w, target - scroll->w);
  60. } else if (has_scrolling) {
  61. if ((scroll_delta < 0 || (scroll_delta > 0))) {
  62. /* update cursor by mouse scrolling */
  63. scroll_offset = scroll_offset + scroll_step * (-scroll_delta);
  64. if (o == NK_VERTICAL)
  65. scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->h);
  66. else scroll_offset = NK_CLAMP(0, scroll_offset, target - scroll->w);
  67. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_START)) {
  68. /* update cursor to the beginning */
  69. if (o == NK_VERTICAL) scroll_offset = 0;
  70. } else if (nk_input_is_key_pressed(in, NK_KEY_SCROLL_END)) {
  71. /* update cursor to the end */
  72. if (o == NK_VERTICAL) scroll_offset = target - scroll->h;
  73. }
  74. }
  75. if (*state & NK_WIDGET_STATE_HOVER && !nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  76. *state |= NK_WIDGET_STATE_ENTERED;
  77. else if (nk_input_is_mouse_prev_hovering_rect(in, *scroll))
  78. *state |= NK_WIDGET_STATE_LEFT;
  79. return scroll_offset;
  80. }
  81. NK_LIB void
  82. nk_draw_scrollbar(struct nk_command_buffer *out, nk_flags state,
  83. const struct nk_style_scrollbar *style, const struct nk_rect *bounds,
  84. const struct nk_rect *scroll)
  85. {
  86. const struct nk_style_item *background;
  87. const struct nk_style_item *cursor;
  88. /* select correct colors/images to draw */
  89. if (state & NK_WIDGET_STATE_ACTIVED) {
  90. background = &style->active;
  91. cursor = &style->cursor_active;
  92. } else if (state & NK_WIDGET_STATE_HOVER) {
  93. background = &style->hover;
  94. cursor = &style->cursor_hover;
  95. } else {
  96. background = &style->normal;
  97. cursor = &style->cursor_normal;
  98. }
  99. /* draw background */
  100. switch (background->type) {
  101. case NK_STYLE_ITEM_IMAGE:
  102. nk_draw_image(out, *bounds, &background->data.image, nk_white);
  103. break;
  104. case NK_STYLE_ITEM_NINE_SLICE:
  105. nk_draw_nine_slice(out, *bounds, &background->data.slice, nk_white);
  106. break;
  107. case NK_STYLE_ITEM_COLOR:
  108. nk_fill_rect(out, *bounds, style->rounding, background->data.color);
  109. nk_stroke_rect(out, *bounds, style->rounding, style->border, style->border_color);
  110. break;
  111. }
  112. /* draw cursor */
  113. switch (cursor->type) {
  114. case NK_STYLE_ITEM_IMAGE:
  115. nk_draw_image(out, *scroll, &cursor->data.image, nk_white);
  116. break;
  117. case NK_STYLE_ITEM_NINE_SLICE:
  118. nk_draw_nine_slice(out, *scroll, &cursor->data.slice, nk_white);
  119. break;
  120. case NK_STYLE_ITEM_COLOR:
  121. nk_fill_rect(out, *scroll, style->rounding_cursor, cursor->data.color);
  122. nk_stroke_rect(out, *scroll, style->rounding_cursor, style->border_cursor, style->cursor_border_color);
  123. break;
  124. }
  125. }
  126. NK_LIB float
  127. nk_do_scrollbarv(nk_flags *state,
  128. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  129. float offset, float target, float step, float button_pixel_inc,
  130. const struct nk_style_scrollbar *style, struct nk_input *in,
  131. const struct nk_user_font *font)
  132. {
  133. struct nk_rect empty_north;
  134. struct nk_rect empty_south;
  135. struct nk_rect cursor;
  136. float scroll_step;
  137. float scroll_offset;
  138. float scroll_off;
  139. float scroll_ratio;
  140. NK_ASSERT(out);
  141. NK_ASSERT(style);
  142. NK_ASSERT(state);
  143. if (!out || !style) return 0;
  144. scroll.w = NK_MAX(scroll.w, 1);
  145. scroll.h = NK_MAX(scroll.h, 0);
  146. if (target <= scroll.h) return 0;
  147. /* optional scrollbar buttons */
  148. if (style->show_buttons) {
  149. nk_flags ws;
  150. float scroll_h;
  151. struct nk_rect button;
  152. button.x = scroll.x;
  153. button.w = scroll.w;
  154. button.h = scroll.w;
  155. scroll_h = NK_MAX(scroll.h - 2 * button.h,0);
  156. scroll_step = NK_MIN(step, button_pixel_inc);
  157. /* decrement button */
  158. button.y = scroll.y;
  159. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  160. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  161. offset = offset - scroll_step;
  162. /* increment button */
  163. button.y = scroll.y + scroll.h - button.h;
  164. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  165. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  166. offset = offset + scroll_step;
  167. scroll.y = scroll.y + button.h;
  168. scroll.h = scroll_h;
  169. }
  170. /* calculate scrollbar constants */
  171. scroll_step = NK_MIN(step, scroll.h);
  172. scroll_offset = NK_CLAMP(0, offset, target - scroll.h);
  173. scroll_ratio = scroll.h / target;
  174. scroll_off = scroll_offset / target;
  175. /* calculate scrollbar cursor bounds */
  176. cursor.h = NK_MAX((scroll_ratio * scroll.h) - (2*style->border + 2*style->padding.y), 0);
  177. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border + style->padding.y;
  178. cursor.w = scroll.w - (2 * style->border + 2 * style->padding.x);
  179. cursor.x = scroll.x + style->border + style->padding.x;
  180. /* calculate empty space around cursor */
  181. empty_north.x = scroll.x;
  182. empty_north.y = scroll.y;
  183. empty_north.w = scroll.w;
  184. empty_north.h = NK_MAX(cursor.y - scroll.y, 0);
  185. empty_south.x = scroll.x;
  186. empty_south.y = cursor.y + cursor.h;
  187. empty_south.w = scroll.w;
  188. empty_south.h = NK_MAX((scroll.y + scroll.h) - (cursor.y + cursor.h), 0);
  189. /* update scrollbar */
  190. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  191. &empty_north, &empty_south, scroll_offset, target, scroll_step, NK_VERTICAL);
  192. scroll_off = scroll_offset / target;
  193. cursor.y = scroll.y + (scroll_off * scroll.h) + style->border_cursor + style->padding.y;
  194. /* draw scrollbar */
  195. if (style->draw_begin) style->draw_begin(out, style->userdata);
  196. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  197. if (style->draw_end) style->draw_end(out, style->userdata);
  198. return scroll_offset;
  199. }
  200. NK_LIB float
  201. nk_do_scrollbarh(nk_flags *state,
  202. struct nk_command_buffer *out, struct nk_rect scroll, int has_scrolling,
  203. float offset, float target, float step, float button_pixel_inc,
  204. const struct nk_style_scrollbar *style, struct nk_input *in,
  205. const struct nk_user_font *font)
  206. {
  207. struct nk_rect cursor;
  208. struct nk_rect empty_west;
  209. struct nk_rect empty_east;
  210. float scroll_step;
  211. float scroll_offset;
  212. float scroll_off;
  213. float scroll_ratio;
  214. NK_ASSERT(out);
  215. NK_ASSERT(style);
  216. if (!out || !style) return 0;
  217. /* scrollbar background */
  218. scroll.h = NK_MAX(scroll.h, 1);
  219. scroll.w = NK_MAX(scroll.w, 2 * scroll.h);
  220. if (target <= scroll.w) return 0;
  221. /* optional scrollbar buttons */
  222. if (style->show_buttons) {
  223. nk_flags ws;
  224. float scroll_w;
  225. struct nk_rect button;
  226. button.y = scroll.y;
  227. button.w = scroll.h;
  228. button.h = scroll.h;
  229. scroll_w = scroll.w - 2 * button.w;
  230. scroll_step = NK_MIN(step, button_pixel_inc);
  231. /* decrement button */
  232. button.x = scroll.x;
  233. if (nk_do_button_symbol(&ws, out, button, style->dec_symbol,
  234. NK_BUTTON_REPEATER, &style->dec_button, in, font))
  235. offset = offset - scroll_step;
  236. /* increment button */
  237. button.x = scroll.x + scroll.w - button.w;
  238. if (nk_do_button_symbol(&ws, out, button, style->inc_symbol,
  239. NK_BUTTON_REPEATER, &style->inc_button, in, font))
  240. offset = offset + scroll_step;
  241. scroll.x = scroll.x + button.w;
  242. scroll.w = scroll_w;
  243. }
  244. /* calculate scrollbar constants */
  245. scroll_step = NK_MIN(step, scroll.w);
  246. scroll_offset = NK_CLAMP(0, offset, target - scroll.w);
  247. scroll_ratio = scroll.w / target;
  248. scroll_off = scroll_offset / target;
  249. /* calculate cursor bounds */
  250. cursor.w = (scroll_ratio * scroll.w) - (2*style->border + 2*style->padding.x);
  251. cursor.x = scroll.x + (scroll_off * scroll.w) + style->border + style->padding.x;
  252. cursor.h = scroll.h - (2 * style->border + 2 * style->padding.y);
  253. cursor.y = scroll.y + style->border + style->padding.y;
  254. /* calculate empty space around cursor */
  255. empty_west.x = scroll.x;
  256. empty_west.y = scroll.y;
  257. empty_west.w = cursor.x - scroll.x;
  258. empty_west.h = scroll.h;
  259. empty_east.x = cursor.x + cursor.w;
  260. empty_east.y = scroll.y;
  261. empty_east.w = (scroll.x + scroll.w) - (cursor.x + cursor.w);
  262. empty_east.h = scroll.h;
  263. /* update scrollbar */
  264. scroll_offset = nk_scrollbar_behavior(state, in, has_scrolling, &scroll, &cursor,
  265. &empty_west, &empty_east, scroll_offset, target, scroll_step, NK_HORIZONTAL);
  266. scroll_off = scroll_offset / target;
  267. cursor.x = scroll.x + (scroll_off * scroll.w);
  268. /* draw scrollbar */
  269. if (style->draw_begin) style->draw_begin(out, style->userdata);
  270. nk_draw_scrollbar(out, *state, style, &scroll, &cursor);
  271. if (style->draw_end) style->draw_end(out, style->userdata);
  272. return scroll_offset;
  273. }