Blender V5.3
view2d.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2008 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <cfloat>
11#include <climits>
12#include <cmath>
13#include <cstring>
14
15#include "MEM_guardedalloc.h"
16
17#include "DNA_userdef_types.h"
18
19#include "BLI_link_utils.hh"
20#include "BLI_listbase.hh"
21#include "BLI_math_matrix_c.hh"
22#include "BLI_memarena.hh"
23#include "BLI_rect.hh"
24#include "BLI_utildefines.hh"
25
26#include "BKE_context.hh"
27#include "BKE_global.hh"
28#include "BKE_screen.hh"
29
30#include "GPU_immediate.hh"
31#include "GPU_matrix.hh"
32#include "GPU_state.hh"
33
34#include "WM_api.hh"
35
36#include "BLF_api.hh"
37
38#include "ED_screen.hh"
39
40#include "UI_interface.hh"
41#include "UI_view2d.hh"
42
43#include "view2d_intern.hh"
44
45namespace blender::ui {
46
47static void view2d_curRect_validate_resize(View2D *v2d, bool resize);
48
49/* -------------------------------------------------------------------- */
52
54{
55 const float min = float(INT_MIN);
56 const float max = float(INT_MAX);
57
58 if (f < min) [[unlikely]] {
59 return min;
60 }
61 if (f > max) [[unlikely]] {
62 return int(max);
63 }
64 return int(f);
65}
66
71BLI_INLINE void clamp_rctf_to_rcti(rcti *dst, const rctf *src)
72{
73 dst->xmin = clamp_float_to_int(src->xmin);
74 dst->xmax = clamp_float_to_int(src->xmax);
75 dst->ymin = clamp_float_to_int(src->ymin);
76 dst->ymax = clamp_float_to_int(src->ymax);
77}
78
79float view2d_page_size_y(const View2D &v2d)
80{
81 return v2d.page_size_y ? v2d.page_size_y : BLI_rcti_size_y(&v2d.mask);
82}
83
84/* XXX still unresolved: scrolls hide/unhide vs region mask handling */
85/* XXX there's V2D_SCROLL_HORIZONTAL_HIDE and V2D_SCROLL_HORIZONTAL_FULLR ... */
86
88
89/* -------------------------------------------------------------------- */
92
100static int view2d_scroll_mapped(int scroll)
101{
102 if (scroll & V2D_SCROLL_HORIZONTAL_FULLR) {
103 scroll &= ~V2D_SCROLL_HORIZONTAL;
104 }
105 if (scroll & V2D_SCROLL_VERTICAL_FULLR) {
106 scroll &= ~V2D_SCROLL_VERTICAL;
107 }
108 return scroll;
109}
110
111void view2d_mask_from_win(const View2D *v2d, rcti *r_mask)
112{
113 r_mask->xmin = 0;
114 r_mask->ymin = 0;
115 r_mask->xmax = v2d->winx - 1; /* -1 yes! masks are pixels */
116 r_mask->ymax = v2d->winy - 1;
117}
118
124static void view2d_masks(View2D *v2d, const rcti *mask_scroll)
125{
126 int scroll;
127
128 /* mask - view frame */
129 view2d_mask_from_win(v2d, &v2d->mask);
130 if (mask_scroll == nullptr) {
131 mask_scroll = &v2d->mask;
132 }
133
134 /* check size if hiding flag is set: */
137 if (BLI_rctf_size_x(&v2d->tot) > BLI_rctf_size_x(&v2d->cur)) {
139 }
140 else {
142 }
143 }
144 }
145 if (v2d->scroll & V2D_SCROLL_VERTICAL_HIDE) {
146 if (!(v2d->scroll & V2D_SCROLL_VERTICAL_HANDLES)) {
147 if (BLI_rctf_size_y(&v2d->tot) + 0.01f > BLI_rctf_size_y(&v2d->cur)) {
149 }
150 else {
152 }
153 }
154 }
155
156 /* Do not use mapped scroll here because we want to update scroller rects
157 * even if they are not displayed. For initialization purposes. See #75003. */
158 scroll = v2d->scroll;
159
160 /* Scrollers are based off region-size:
161 * - they can only be on one to two edges of the region they define
162 * - if they overlap, they must not occupy the corners (which are reserved for other widgets)
163 */
164 if (scroll) {
165 float scroll_width, scroll_height;
166
167 view2d_scroller_size_get(v2d, false, &scroll_width, &scroll_height);
168
169 /* vertical scroller */
170 if (scroll & V2D_SCROLL_LEFT) {
171 /* on left-hand edge of region */
172 v2d->vert = *mask_scroll;
173 v2d->vert.xmax = v2d->vert.xmin + scroll_width;
174 }
175 else if (scroll & V2D_SCROLL_RIGHT) {
176 /* on right-hand edge of region */
177 v2d->vert = *mask_scroll;
178 v2d->vert.xmax++; /* one pixel extra... was leaving a minor gap... */
179 v2d->vert.xmin = v2d->vert.xmax - scroll_width;
180 }
181
182 /* horizontal scroller */
183 if (scroll & V2D_SCROLL_BOTTOM) {
184 /* on bottom edge of region */
185 v2d->hor = *mask_scroll;
186 v2d->hor.ymax = scroll_height;
187 }
188 else if (scroll & V2D_SCROLL_TOP) {
189 /* on upper edge of region */
190 v2d->hor = *mask_scroll;
191 v2d->hor.ymin = v2d->hor.ymax - scroll_height;
192 }
193
194 /* Adjust horizontal scroller to avoid interfering with splitter areas. */
195 if (scroll & V2D_SCROLL_HORIZONTAL) {
198 }
199
200 /* Adjust vertical scroller to avoid horizontal scrollers and splitter areas. */
201 if (scroll & V2D_SCROLL_VERTICAL) {
202 /* Note that top splitter areas are in the header,
203 * outside of `mask_scroll`, so we can ignore them. */
204 v2d->vert.ymin += UI_AZONESPOTH;
205 if (scroll & V2D_SCROLL_BOTTOM) {
206 /* on bottom edge of region */
207 v2d->vert.ymin = max_ii(v2d->hor.ymax, v2d->vert.ymin);
208 }
209 else if (scroll & V2D_SCROLL_TOP) {
210 /* on upper edge of region */
211 v2d->vert.ymax = v2d->hor.ymin;
212 }
213 }
214 }
215}
216
218
219/* -------------------------------------------------------------------- */
222
223void view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
224{
225 bool tot_changed = false, do_init;
226 const uiStyle *style = style_get();
227
228 do_init = (v2d->flag & V2D_IS_INIT) == 0;
229
230 /* see eView2D_CommonViewTypes in UI_view2d.hh for available view presets */
231 switch (type) {
232 /* 'standard view' - optimum setup for 'standard' view behavior,
233 * that should be used new views as basis for their
234 * own unique View2D settings, which should be used instead of this in most cases...
235 */
237 /* for now, aspect ratio should be maintained,
238 * and zoom is clamped within sane default limits */
240 v2d->minzoom = 0.01f;
241 v2d->maxzoom = 1000.0f;
242
243 /* View2D tot rect and cur should be same size,
244 * and aligned using 'standard' OpenGL coordinates for now:
245 * - region can resize 'tot' later to fit other data
246 * - keeptot is only within bounds, as strict locking is not that critical
247 * - view is aligned for (0,0) -> (winx-1, winy-1) setup
248 */
251 if (do_init) {
252 v2d->tot.xmin = v2d->tot.ymin = 0.0f;
253 v2d->tot.xmax = float(winx - 1);
254 v2d->tot.ymax = float(winy - 1);
255
256 v2d->cur = v2d->tot;
257 }
258 /* scrollers - should we have these by default? */
259 /* XXX for now, we don't override this, or set it either! */
260 break;
261 }
262 /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
263 case V2D_COMMONVIEW_LIST: {
264 /* zoom + aspect ratio are locked */
266 v2d->minzoom = v2d->maxzoom = 1.0f;
267
268 /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
271 tot_changed = do_init;
272
273 /* scroller settings are currently not set here... that is left for regions... */
274 break;
275 }
276 /* 'stack view' - practically the same as list/channel view,
277 * except is located in the pos y half instead.
278 * Zoom, aspect ratio, and alignment restrictions are set here. */
280 /* zoom + aspect ratio are locked */
282 v2d->minzoom = v2d->maxzoom = 1.0f;
283
284 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
287 tot_changed = do_init;
288
289 /* scroller settings are currently not set here... that is left for regions... */
290 break;
291 }
292 /* 'header' regions - zoom, aspect ratio,
293 * alignment, and panning restrictions are set here */
295 /* zoom + aspect ratio are locked */
297 v2d->minzoom = v2d->maxzoom = 1.0f;
298
299 if (do_init) {
300 v2d->tot.xmin = 0.0f;
301 v2d->tot.xmax = winx;
302 v2d->tot.ymin = 0.0f;
303 v2d->tot.ymax = winy;
304 v2d->cur = v2d->tot;
305
306 v2d->min[0] = v2d->max[0] = float(winx - 1);
307 v2d->min[1] = v2d->max[1] = float(winy - 1);
308 }
309 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
312 tot_changed = do_init;
313
314 /* panning in y-axis is prohibited */
315 v2d->keepofs = V2D_LOCKOFS_Y;
316
317 /* absolutely no scrollers allowed */
318 v2d->scroll = eView2D_Scroll{};
319 break;
320 }
321 /* panels view, with horizontal/vertical align */
323
324 /* for now, aspect ratio should be maintained,
325 * and zoom is clamped within sane default limits */
327 v2d->minzoom = 0.5f;
328 v2d->maxzoom = 2.0f;
329
332
333 /* NOTE: scroll is being flipped in #ED_region_panels() drawing. */
335
336 if (do_init) {
337 const float panelzoom = (style) ? style->panelzoom : 1.0f;
338
339 v2d->tot.xmin = 0.0f;
340 v2d->tot.xmax = winx;
341
342 v2d->tot.ymax = 0.0f;
343 v2d->tot.ymin = -winy;
344
345 v2d->cur.xmin = 0.0f;
346 v2d->cur.xmax = (winx)*panelzoom;
347
348 v2d->cur.ymax = 0.0f;
349 v2d->cur.ymin = (-winy) * panelzoom;
350 }
351 break;
352 }
353 /* other view types are completely defined using their own settings already */
354 default:
355 /* we don't do anything here,
356 * as settings should be fine, but just make sure that rect */
357 break;
358 }
359
360 /* set initialized flag so that View2D doesn't get reinitialized next time again */
361 v2d->flag |= V2D_IS_INIT;
362
363 /* store view size */
364 v2d->winx = winx;
365 v2d->winy = winy;
366
367 view2d_masks(v2d, nullptr);
368
369 if (do_init) {
370 /* Visible by default. */
371 v2d->alpha_hor = v2d->alpha_vert = 255;
372 }
373
374 /* set 'tot' rect before setting cur? */
375 /* XXX confusing stuff here still */
376 if (tot_changed) {
377 view2d_totRect_set_resize(v2d, winx, winy, !do_init);
378 }
379 else {
380 view2d_curRect_validate_resize(v2d, !do_init);
381 }
382}
383
388static void view2d_curRect_validate_resize(View2D *v2d, bool resize)
389{
390 /* NOTE: #calculateZfac uses this logic, keep in sync. */
391 float curwidth, curheight, width, height;
392 float winx, winy;
393
394 /* use mask as size of region that View2D resides in, as it takes into account
395 * scroll-bars already - keep in sync with `zoomx/zoomy` in #view_zoomstep_apply_ex! */
396 winx = float(BLI_rcti_size_x(&v2d->mask) + 1);
397 winy = float(BLI_rcti_size_y(&v2d->mask) + 1);
398
399 /* get pointers to rcts for less typing */
400 rctf *cur = &v2d->cur;
401 rctf *tot = &v2d->tot;
402
403 /* we must satisfy the following constraints (in decreasing order of importance):
404 * - alignment restrictions are respected
405 * - cur must not fall outside of tot
406 * - axis locks (zoom and offset) must be maintained
407 * - zoom must not be excessive (check either sizes or zoom values)
408 * - aspect ratio should be respected (NOTE: this is quite closely related to zoom too)
409 */
410
411 /* Step 1: if keepzoom, adjust the sizes of the rects only
412 * - firstly, we calculate the sizes of the rects
413 * - curwidth and curheight are saved as reference... modify width and height values here
414 */
415 /* Keep in sync with `zoomx/zoomy` in #view_zoomstep_apply_ex! */
416 curwidth = width = BLI_rctf_size_x(cur);
417 curheight = height = BLI_rctf_size_y(cur);
418
419 /* if zoom is locked, size on the appropriate axis is reset to mask size */
420 if (v2d->keepzoom & V2D_LOCKZOOM_X) {
421 width = winx;
422 }
423 if (v2d->keepzoom & V2D_LOCKZOOM_Y) {
424 height = winy;
425 }
426
427 /* values used to divide, so make it safe
428 * NOTE: width and height must use FLT_MIN instead of 1, otherwise it is impossible to
429 * get enough resolution in Graph Editor for editing some curves
430 */
431 if (width < FLT_MIN) {
432 width = 1;
433 }
434 if (height < FLT_MIN) {
435 height = 1;
436 }
437 winx = std::max<float>(winx, 1);
438 winy = std::max<float>(winy, 1);
439 if (v2d->oldwinx == 0) {
440 v2d->oldwinx = winx;
441 }
442 if (v2d->oldwiny == 0) {
443 v2d->oldwiny = winy;
444 }
445
446 /* V2D_KEEPZOOM indicates that zoom level should be preserved when the window size changes. */
447 if (resize && (v2d->keepzoom & V2D_KEEPZOOM)) {
448 float zoom, oldzoom;
449
450 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0) {
451 zoom = winx / width;
452 oldzoom = v2d->oldwinx / curwidth;
453
454 if (oldzoom != zoom) {
455 width *= zoom / oldzoom;
456 }
457 }
458
459 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0) {
460 zoom = winy / height;
461 oldzoom = v2d->oldwiny / curheight;
462
463 if (oldzoom != zoom) {
464 height *= zoom / oldzoom;
465 }
466 }
467 }
468 /* keepzoom (V2D_LIMITZOOM set), indicates that zoom level on each axis must not exceed limits
469 * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
470 */
471 else if (v2d->keepzoom & V2D_LIMITZOOM) {
472
473 /* check if excessive zoom on x-axis */
474 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0) {
475 const float zoom = winx / width;
476 if (zoom < v2d->minzoom) {
477 width = winx / v2d->minzoom;
478 }
479 else if (zoom > v2d->maxzoom) {
480 width = winx / v2d->maxzoom;
481 }
482 }
483
484 /* check if excessive zoom on y-axis */
485 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0) {
486 const float zoom = winy / height;
487 if (zoom < v2d->minzoom) {
488 height = winy / v2d->minzoom;
489 }
490 else if (zoom > v2d->maxzoom) {
491 height = winy / v2d->maxzoom;
492 }
493 }
494 }
495 else {
496 /* make sure sizes don't exceed that of the min/max sizes
497 * (even though we're not doing zoom clamping) */
498 CLAMP(width, v2d->min[0], v2d->max[0]);
499 CLAMP(height, v2d->min[1], v2d->max[1]);
500 }
501
502 /* check if we should restore aspect ratio (if view size changed) */
503 if (v2d->keepzoom & V2D_KEEPASPECT) {
504 bool do_x = false, do_y = false, do_cur;
505 float curRatio, winRatio;
506
507 /* when a window edge changes, the aspect ratio can't be used to
508 * find which is the best new 'cur' rect. that's why it stores 'old'
509 */
510 if (winx != v2d->oldwinx) {
511 do_x = true;
512 }
513 if (winy != v2d->oldwiny) {
514 do_y = true;
515 }
516
517 curRatio = height / width;
518 winRatio = winy / winx;
519
520 /* Both sizes change (area/region maximized). */
521 if (do_x == do_y) {
522 if (do_x && do_y) {
523 /* here is 1,1 case, so all others must be 0,0 */
524 if (fabsf(winx - v2d->oldwinx) > fabsf(winy - v2d->oldwiny)) {
525 do_y = false;
526 }
527 else {
528 do_x = false;
529 }
530 }
531 else if (winRatio > curRatio) {
532 do_x = false;
533 }
534 else {
535 do_x = true;
536 }
537 }
538 do_cur = do_x;
539 // do_win = do_y; /* UNUSED. */
540
541 if (do_cur) {
542 if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
543 /* Special exception for Outliner (and later channel-lists):
544 * - The view may be moved left to avoid contents
545 * being pushed out of view when view shrinks.
546 * - The keeptot code will make sure cur->xmin will not be less than tot->xmin
547 * (which cannot be allowed).
548 * - width is not adjusted for changed ratios here.
549 */
550 if (winx < v2d->oldwinx) {
551 const float temp = v2d->oldwinx - winx;
552
553 cur->xmin -= temp;
554 cur->xmax -= temp;
555
556 /* Width does not get modified, as keep-aspect here is just set to make
557 * sure visible area adjusts to changing view shape! */
558 }
559 }
560 else {
561 /* portrait window: correct for x */
562 width = height / winRatio;
563 }
564 }
565 else {
566 /* landscape window: correct for y */
567 height = width * winRatio;
568 }
569 }
570
571 /* Store region size for next time. */
572 v2d->oldwinx = short(winx);
573 v2d->oldwiny = short(winy);
574
575 /* Step 2: apply new sizes to cur rect,
576 * but need to take into account alignment settings here... */
577 const bool do_keepofs = resize || !(v2d->flag & V2D_ZOOM_IGNORE_KEEPOFS);
578 if ((width != curwidth) || (height != curheight)) {
579 float temp, dh;
580
581 /* Resize from center-point, unless otherwise specified. */
582 if (width != curwidth) {
583 if (v2d->keepofs & V2D_LOCKOFS_X) {
584 cur->xmax += width - BLI_rctf_size_x(cur);
585 }
586 else if ((v2d->keepofs & V2D_KEEPOFS_X) && do_keepofs) {
587 if (v2d->align & V2D_ALIGN_NO_POS_X) {
588 cur->xmin -= width - BLI_rctf_size_x(cur);
589 }
590 else {
591 cur->xmax += width - BLI_rctf_size_x(cur);
592 }
593 }
594 else {
595 temp = BLI_rctf_cent_x(cur);
596 dh = width * 0.5f;
597
598 cur->xmin = temp - dh;
599 cur->xmax = temp + dh;
600 }
601 }
602 if (height != curheight) {
603 if (v2d->keepofs & V2D_LOCKOFS_Y) {
604 cur->ymax += height - BLI_rctf_size_y(cur);
605 }
606 else if ((v2d->keepofs & V2D_KEEPOFS_Y) && do_keepofs) {
607 if (v2d->align & V2D_ALIGN_NO_POS_Y) {
608 cur->ymin -= height - BLI_rctf_size_y(cur);
609 }
610 else {
611 cur->ymax += height - BLI_rctf_size_y(cur);
612 }
613 }
614 else {
615 temp = BLI_rctf_cent_y(cur);
616 dh = height * 0.5f;
617
618 cur->ymin = temp - dh;
619 cur->ymax = temp + dh;
620 }
621 }
622 }
623
624 const float totwidth = BLI_rctf_size_x(tot);
625 const float totheight = BLI_rctf_size_y(tot);
626
627 /* Step 3: adjust so that it doesn't fall outside of bounds of 'tot' */
628 if (v2d->keeptot) {
629 float temp, diff;
630
631 /* recalculate extents of cur */
632 curwidth = BLI_rctf_size_x(cur);
633 curheight = BLI_rctf_size_y(cur);
634
635 /* width */
636 if ((curwidth > totwidth) &&
638 {
639 /* if zoom doesn't have to be maintained, just clamp edges */
640 cur->xmin = std::max(cur->xmin, tot->xmin);
641 cur->xmax = std::min(cur->xmax, tot->xmax);
642 }
643 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
644 /* This is an exception for the outliner (and later channel-lists, headers)
645 * - must clamp within tot rect (absolutely no excuses)
646 * --> therefore, cur->xmin must not be less than tot->xmin
647 */
648 if (cur->xmin < tot->xmin) {
649 /* move cur across so that it sits at minimum of tot */
650 temp = tot->xmin - cur->xmin;
651
652 cur->xmin += temp;
653 cur->xmax += temp;
654 }
655 else if (cur->xmax > tot->xmax) {
656 /* - only offset by difference of cur-xmax and tot-xmax if that would not move
657 * cur-xmin to lie past tot-xmin
658 * - otherwise, simply shift to tot-xmin???
659 */
660 temp = cur->xmax - tot->xmax;
661
662 if ((cur->xmin - temp) < tot->xmin) {
663 /* only offset by difference from cur-min and tot-min */
664 temp = cur->xmin - tot->xmin;
665
666 cur->xmin -= temp;
667 cur->xmax -= temp;
668 }
669 else {
670 cur->xmin -= temp;
671 cur->xmax -= temp;
672 }
673 }
674 }
675 else {
676 /* This here occurs when:
677 * - width too big, but maintaining zoom (i.e. widths cannot be changed)
678 * - width is OK, but need to check if outside of boundaries
679 *
680 * So, resolution is to just shift view by the gap between the extremities.
681 * We favor moving the 'minimum' across, as that's origin for most things.
682 * (XXX: in the past, max was favored... if there are bugs, swap!)
683 */
684 if ((cur->xmin < tot->xmin) && (cur->xmax > tot->xmax)) {
685 /* outside boundaries on both sides,
686 * so take middle-point of tot, and place in balanced way */
687 temp = BLI_rctf_cent_x(tot);
688 diff = curwidth * 0.5f;
689
690 cur->xmin = temp - diff;
691 cur->xmax = temp + diff;
692 }
693 else if (cur->xmin < tot->xmin) {
694 /* move cur across so that it sits at minimum of tot */
695 temp = tot->xmin - cur->xmin;
696
697 cur->xmin += temp;
698 cur->xmax += temp;
699 }
700 else if (cur->xmax > tot->xmax) {
701 /* - only offset by difference of cur-xmax and tot-xmax if that would not move
702 * cur-xmin to lie past tot-xmin
703 * - otherwise, simply shift to tot-xmin???
704 */
705 temp = cur->xmax - tot->xmax;
706
707 if ((cur->xmin - temp) < tot->xmin) {
708 /* only offset by difference from cur-min and tot-min */
709 temp = cur->xmin - tot->xmin;
710
711 cur->xmin -= temp;
712 cur->xmax -= temp;
713 }
714 else {
715 cur->xmin -= temp;
716 cur->xmax -= temp;
717 }
718 }
719 }
720
721 /* height */
722 if ((curheight > totheight) &&
724 {
725 /* if zoom doesn't have to be maintained, just clamp edges */
726 cur->ymin = std::max(cur->ymin, tot->ymin);
727 cur->ymax = std::min(cur->ymax, tot->ymax);
728 }
729 else {
730 /* This here occurs when:
731 * - height too big, but maintaining zoom (i.e. heights cannot be changed)
732 * - height is OK, but need to check if outside of boundaries
733 *
734 * So, resolution is to just shift view by the gap between the extremities.
735 * We favor moving the 'minimum' across, as that's origin for most things.
736 */
737 if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
738 /* outside boundaries on both sides,
739 * so take middle-point of tot, and place in balanced way */
740 temp = BLI_rctf_cent_y(tot);
741 diff = curheight * 0.5f;
742
743 cur->ymin = temp - diff;
744 cur->ymax = temp + diff;
745 }
746 else if (cur->ymin < tot->ymin) {
747 /* there's still space remaining, so shift up */
748 temp = tot->ymin - cur->ymin;
749
750 cur->ymin += temp;
751 cur->ymax += temp;
752 }
753 else if (cur->ymax > tot->ymax) {
754 /* there's still space remaining, so shift down */
755 temp = cur->ymax - tot->ymax;
756
757 cur->ymin -= temp;
758 cur->ymax -= temp;
759 }
760 }
761 }
762
763 /* Step 4: Make sure alignment restrictions are respected */
764 if (v2d->align) {
765 /* If alignment flags are set (but keeptot is not), they must still be respected, as although
766 * they don't specify any particular bounds to stay within, they do define ranges which are
767 * invalid.
768 *
769 * Here, we only check to make sure that on each axis, the 'cur' rect doesn't stray into these
770 * invalid zones, otherwise we offset.
771 */
772
773 /* handle width - posx and negx flags are mutually exclusive, so watch out */
774 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
775 /* width is in negative-x half */
776 if (v2d->cur.xmax > 0) {
777 v2d->cur.xmin -= v2d->cur.xmax;
778 v2d->cur.xmax = 0.0f;
779 }
780 }
781 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
782 /* width is in positive-x half */
783 if (v2d->cur.xmin < 0) {
784 v2d->cur.xmax -= v2d->cur.xmin;
785 v2d->cur.xmin = 0.0f;
786 }
787 }
788
789 /* handle height - posx and negx flags are mutually exclusive, so watch out */
790 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
791 /* height is in negative-y half */
792 if (v2d->cur.ymax > 0) {
793 v2d->cur.ymin -= v2d->cur.ymax;
794 v2d->cur.ymax = 0.0f;
795 }
796 }
797 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
798 /* height is in positive-y half */
799 if (v2d->cur.ymin < 0) {
800 v2d->cur.ymax -= v2d->cur.ymin;
801 v2d->cur.ymin = 0.0f;
802 }
803 }
804 }
805
806 /* set masks */
807 view2d_masks(v2d, nullptr);
808}
809
814
816{
818
819 ARegion *region = CTX_wm_region(C);
820
821 if (region->runtime->type->on_view2d_changed != nullptr) {
822 region->runtime->type->on_view2d_changed(C, region);
823 }
824
825 /* Tag IME cursor refresh after the view changes (pan, zoom, etc). */
826 region->runtime->do_ime = true;
827}
828
830{
831 const float cur_height_y = BLI_rctf_size_y(&v2d->cur);
832
833 if (BLI_rctf_size_y(&v2d->cur) > BLI_rctf_size_y(&v2d->tot)) {
834 v2d->cur.ymin = -cur_height_y;
835 v2d->cur.ymax = 0;
836 }
837 else if (v2d->cur.ymin < v2d->tot.ymin) {
838 v2d->cur.ymin = v2d->tot.ymin;
839 v2d->cur.ymax = v2d->cur.ymin + cur_height_y;
840 }
841}
842
843/* ------------------ */
844
849
850void view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
851{
852 /* don't continue if no view syncing to be done */
854 return;
855 }
856
857 /* check if doing within area syncing (i.e. channels/vertical) */
858 if ((v2dcur->flag & V2D_VIEWSYNC_AREA_VERTICAL) && (area)) {
859 for (ARegion &region : area->regionbase) {
860 /* don't operate on self */
861 if (v2dcur != &region.v2d) {
862 /* only if view has vertical locks enabled */
863 if (region.v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) {
864 if (flag == V2D_LOCK_COPY) {
865 /* other views with locks on must copy active */
866 region.v2d.cur.ymin = v2dcur->cur.ymin;
867 region.v2d.cur.ymax = v2dcur->cur.ymax;
868 }
869 else { /* V2D_LOCK_SET */
870 /* active must copy others */
871 v2dcur->cur.ymin = region.v2d.cur.ymin;
872 v2dcur->cur.ymax = region.v2d.cur.ymax;
873 }
874
875 /* region possibly changed, so refresh */
877 }
878 }
879 }
880 }
881
882 /* check if doing whole screen syncing (i.e. time/horizontal) */
883 if ((v2dcur->flag & V2D_VIEWSYNC_SCREEN_TIME) && (screen)) {
884 for (ScrArea &area_iter : screen->areabase) {
885 if (!view2d_area_supports_sync(&area_iter)) {
886 continue;
887 }
888 for (ARegion &region : area_iter.regionbase) {
889 /* don't operate on self */
890 if (v2dcur != &region.v2d) {
891 /* only if view has horizontal locks enabled */
892 if (region.v2d.flag & V2D_VIEWSYNC_SCREEN_TIME) {
893 if (flag == V2D_LOCK_COPY) {
894 /* other views with locks on must copy active */
895 region.v2d.cur.xmin = v2dcur->cur.xmin;
896 region.v2d.cur.xmax = v2dcur->cur.xmax;
897 }
898 else { /* V2D_LOCK_SET */
899 /* active must copy others */
900 v2dcur->cur.xmin = region.v2d.cur.xmin;
901 v2dcur->cur.xmax = region.v2d.cur.xmax;
902 }
903
904 /* region possibly changed, so refresh */
906 }
907 }
908 }
909 }
910 }
911}
912
914{
915 float width, height;
916
917 /* assume width and height of 'cur' rect by default, should be same size as mask */
918 width = float(BLI_rcti_size_x(&v2d->mask) + 1);
919 height = float(BLI_rcti_size_y(&v2d->mask) + 1);
920
921 /* handle width - posx and negx flags are mutually exclusive, so watch out */
922 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
923 /* width is in negative-x half */
924 v2d->cur.xmin = -width;
925 v2d->cur.xmax = 0.0f;
926 }
927 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
928 /* width is in positive-x half */
929 v2d->cur.xmin = 0.0f;
930 v2d->cur.xmax = width;
931 }
932 else {
933 /* width is centered around (x == 0) */
934 const float dx = width / 2.0f;
935
936 v2d->cur.xmin = -dx;
937 v2d->cur.xmax = dx;
938 }
939
940 /* handle height - posx and negx flags are mutually exclusive, so watch out */
941 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
942 /* height is in negative-y half */
943 v2d->cur.ymin = -height;
944 v2d->cur.ymax = 0.0f;
945 }
946 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
947 /* height is in positive-y half */
948 v2d->cur.ymin = 0.0f;
949 v2d->cur.ymax = height;
950 }
951 else {
952 /* height is centered around (y == 0) */
953 const float dy = height / 2.0f;
954
955 v2d->cur.ymin = -dy;
956 v2d->cur.ymax = dy;
957 }
958}
959
960/* ------------------ */
961
962void view2d_totRect_set_resize(View2D *v2d, int width, int height, bool resize)
963{
964 /* don't do anything if either value is 0 */
965 width = abs(width);
966 height = abs(height);
967
968 if (ELEM(0, width, height)) {
969 if (G.debug & G_DEBUG) {
970 /* XXX: temp debug info. */
971 printf("Error: View2D totRect set exiting: v2d=%p width=%d height=%d\n",
972 static_cast<void *>(v2d),
973 width,
974 height);
975 }
976 return;
977 }
978
979 /* handle width - posx and negx flags are mutually exclusive, so watch out */
980 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
981 /* width is in negative-x half */
982 v2d->tot.xmin = float(-width);
983 v2d->tot.xmax = 0.0f;
984 }
985 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
986 /* width is in positive-x half */
987 v2d->tot.xmin = 0.0f;
988 v2d->tot.xmax = float(width);
989 }
990 else {
991 /* width is centered around (x == 0) */
992 const float dx = float(width) / 2.0f;
993
994 v2d->tot.xmin = -dx;
995 v2d->tot.xmax = dx;
996 }
997
998 /* handle height - posx and negx flags are mutually exclusive, so watch out */
999 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1000 /* height is in negative-y half */
1001 v2d->tot.ymin = float(-height);
1002 v2d->tot.ymax = 0.0f;
1003 }
1004 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
1005 /* height is in positive-y half */
1006 v2d->tot.ymin = 0.0f;
1007 v2d->tot.ymax = float(height);
1008 }
1009 else {
1010 /* height is centered around (y == 0) */
1011 const float dy = float(height) / 2.0f;
1012
1013 v2d->tot.ymin = -dy;
1014 v2d->tot.ymax = dy;
1015 }
1016
1017 /* make sure that 'cur' rect is in a valid state as a result of these changes */
1018 view2d_curRect_validate_resize(v2d, resize);
1019}
1020
1021void view2d_totRect_set(View2D *v2d, int width, int height)
1022{
1023 view2d_totRect_set_resize(v2d, width, height, false);
1024}
1025
1027{
1028 /* TODO(sergey): This way we avoid threading conflict with sequencer rendering
1029 * text strip. But ideally we want to make glyph cache to be fully safe
1030 * for threading.
1031 */
1032 if (G.is_rendering) {
1033 return;
1034 }
1035 /* While scaling we can accumulate fonts at many sizes (~20 or so).
1036 * Not an issue with embedded font, but can use over 500Mb with i18n ones! See #38244. */
1037
1038 /* NOTE: only some views draw text, we could check for this case to avoid cleaning cache. */
1040}
1041
1043
1044/* -------------------------------------------------------------------- */
1047
1048/* mapping function to ensure 'cur' draws extended over the area where sliders are */
1049static void view2d_map_cur_using_mask(const View2D *v2d, rctf *r_curmasked)
1050{
1051 *r_curmasked = v2d->cur;
1052
1053 if (view2d_scroll_mapped(v2d->scroll)) {
1054 const float sizex = BLI_rcti_size_x(&v2d->mask);
1055 const float sizey = BLI_rcti_size_y(&v2d->mask);
1056
1057 /* prevent tiny or narrow regions to get
1058 * invalid coordinates - mask can get negative even... */
1059 if (sizex > 0.0f && sizey > 0.0f) {
1060 const float dx = BLI_rctf_size_x(&v2d->cur) / (sizex + 1);
1061 const float dy = BLI_rctf_size_y(&v2d->cur) / (sizey + 1);
1062
1063 if (v2d->mask.xmin != 0) {
1064 r_curmasked->xmin -= dx * float(v2d->mask.xmin);
1065 }
1066 if (v2d->mask.xmax + 1 != v2d->winx) {
1067 r_curmasked->xmax += dx * float(v2d->winx - v2d->mask.xmax - 1);
1068 }
1069
1070 if (v2d->mask.ymin != 0) {
1071 r_curmasked->ymin -= dy * float(v2d->mask.ymin);
1072 }
1073 if (v2d->mask.ymax + 1 != v2d->winy) {
1074 r_curmasked->ymax += dy * float(v2d->winy - v2d->mask.ymax - 1);
1075 }
1076 }
1077 }
1078}
1079
1081{
1082 rctf curmasked;
1083 const int sizex = BLI_rcti_size_x(&v2d->mask);
1084 const int sizey = BLI_rcti_size_y(&v2d->mask);
1085 const float eps = 0.001f;
1086 float xofs = 0.0f, yofs = 0.0f;
1087
1088 /* Pixel offsets (-GLA_PIXEL_OFS) are needed to get 1:1
1089 * correspondence with pixels for smooth UI drawing,
1090 * but only applied where requested.
1091 */
1092 /* XXX(@brecht): instead of zero at least use a tiny offset, otherwise
1093 * pixel rounding is effectively random due to float inaccuracy */
1094 if (sizex > 0) {
1095 xofs = eps * BLI_rctf_size_x(&v2d->cur) / sizex;
1096 }
1097 if (sizey > 0) {
1098 yofs = eps * BLI_rctf_size_y(&v2d->cur) / sizey;
1099 }
1100
1101 /* apply mask-based adjustments to cur rect (due to scrollers),
1102 * to eliminate scaling artifacts */
1103 view2d_map_cur_using_mask(v2d, &curmasked);
1104
1105 BLI_rctf_translate(&curmasked, -xofs, -yofs);
1106
1107 /* XXX ton: this flag set by outliner, for icons */
1108 if (v2d->flag & V2D_PIXELOFS_X) {
1109 curmasked.xmin = floorf(curmasked.xmin) - (eps + xofs);
1110 curmasked.xmax = floorf(curmasked.xmax) - (eps + xofs);
1111 }
1112 if (v2d->flag & V2D_PIXELOFS_Y) {
1113 curmasked.ymin = floorf(curmasked.ymin) - (eps + yofs);
1114 curmasked.ymax = floorf(curmasked.ymax) - (eps + yofs);
1115 }
1116
1117 /* set matrix on all appropriate axes */
1118 wmOrtho2(curmasked.xmin, curmasked.xmax, curmasked.ymin, curmasked.ymax);
1119}
1120
1121void view2d_view_orthoSpecial(ARegion *region, View2D *v2d, const bool xaxis)
1122{
1123 rctf curmasked;
1124 float xofs, yofs;
1125
1126 /* Pixel offsets (-GLA_PIXEL_OFS) are needed to get 1:1
1127 * correspondence with pixels for smooth UI drawing,
1128 * but only applied where requested.
1129 */
1130 /* XXX(ton): temp. */
1131 xofs = 0.0f; // (v2d->flag & V2D_PIXELOFS_X) ? GLA_PIXEL_OFS : 0.0f;
1132 yofs = 0.0f; // (v2d->flag & V2D_PIXELOFS_Y) ? GLA_PIXEL_OFS : 0.0f;
1133
1134 /* apply mask-based adjustments to cur rect (due to scrollers),
1135 * to eliminate scaling artifacts */
1136 view2d_map_cur_using_mask(v2d, &curmasked);
1137
1138 /* only set matrix with 'cur' coordinates on relevant axes */
1139 if (xaxis) {
1140 wmOrtho2(curmasked.xmin - xofs, curmasked.xmax - xofs, -yofs, region->winy - yofs);
1141 }
1142 else {
1143 wmOrtho2(-xofs, region->winx - xofs, curmasked.ymin - yofs, curmasked.ymax - yofs);
1144 }
1145}
1146
1148{
1149 ARegion *region = CTX_wm_region(C);
1150 const int width = BLI_rcti_size_x(&region->winrct) + 1;
1151 const int height = BLI_rcti_size_y(&region->winrct) + 1;
1152
1153 wmOrtho2(0.0f, float(width), 0.0f, float(height));
1155
1156 // ED_region_pixelspace(CTX_wm_region(C));
1157}
1158
1160
1161/* -------------------------------------------------------------------- */
1164
1166 const View2D *v2d, int colorid, float step, int level_size, int totlevels)
1167{
1168 /* Exit if there is nothing to draw */
1169 if (totlevels == 0) {
1170 return;
1171 }
1172
1173 int offset = -10;
1174 float lstep = step;
1175 uchar grid_line_color[3];
1176
1177 /* Make an estimate of at least how many vertices will be needed */
1178 uint vertex_count = 4;
1179 vertex_count += 2 * (int((v2d->cur.xmax - v2d->cur.xmin) / lstep) + 1);
1180 vertex_count += 2 * (int((v2d->cur.ymax - v2d->cur.ymin) / lstep) + 1);
1181
1182 GPUVertFormat *format = immVertexFormat();
1183 const uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
1184 uint color = GPU_vertformat_attr_add(format, "color", gpu::VertAttrType::UNORM_8_8_8_8);
1185
1186 GPU_line_width(1.0f);
1187
1189 immBeginAtMost(GPU_PRIM_LINES, vertex_count);
1190
1191 for (int level = 0; level < totlevels; level++) {
1192 /* Blend the background color (colorid) with the grid color, to avoid either too low contrast
1193 * or high contrast grid lines. This only has an effect if colorid != TH_GRID. */
1194 theme::get_color_blend_shade_3ubv(colorid, TH_GRID, 0.25f, offset, grid_line_color);
1195
1196 int i = int(v2d->cur.xmin / lstep);
1197 if (v2d->cur.xmin > 0.0f) {
1198 i++;
1199 }
1200 float start = i * lstep;
1201
1202 for (; start < v2d->cur.xmax; start += lstep, i++) {
1203 if (i == 0 || (level < totlevels - 1 && i % level_size == 0)) {
1204 continue;
1205 }
1206
1208 immVertex2f(pos, start, v2d->cur.ymin);
1209 immAttr4ub(color, UNPACK3(grid_line_color), 255);
1210 immVertex2f(pos, start, v2d->cur.ymax);
1211 }
1212
1213 i = int(v2d->cur.ymin / lstep);
1214 if (v2d->cur.ymin > 0.0f) {
1215 i++;
1216 }
1217 start = i * lstep;
1218
1219 for (; start < v2d->cur.ymax; start += lstep, i++) {
1220 if (i == 0 || (level < totlevels - 1 && i % level_size == 0)) {
1221 continue;
1222 }
1223
1225 immVertex2f(pos, v2d->cur.xmin, start);
1226 immAttr4ub(color, UNPACK3(grid_line_color), 255);
1227 immVertex2f(pos, v2d->cur.xmax, start);
1228 }
1229
1230 lstep *= level_size;
1231 offset -= 6;
1232 }
1233
1234 /* X and Y axis */
1236 colorid, TH_GRID, 0.5f, -18 + ((totlevels - 1) * -6), grid_line_color);
1237
1239 immVertex2f(pos, 0.0f, v2d->cur.ymin);
1240 immAttr4ub(color, UNPACK3(grid_line_color), 255);
1241 immVertex2f(pos, 0.0f, v2d->cur.ymax);
1242
1244 immVertex2f(pos, v2d->cur.xmin, 0.0f);
1245 immAttr4ub(color, UNPACK3(grid_line_color), 255);
1246 immVertex2f(pos, v2d->cur.xmax, 0.0f);
1247
1248 immEnd();
1250}
1251
1253 const float step, const float min, const float max, float *r_start, int *r_count)
1254{
1255 *r_start = min;
1256 if (*r_start < 0.0f) {
1257 *r_start += -fmod(min, step);
1258 }
1259 else {
1260 *r_start += step - fabs(fmod(min, step));
1261 }
1262
1263 if (*r_start > max) {
1264 *r_count = 0;
1265 }
1266 else {
1267 *r_count = (max - *r_start) / step + 1;
1268 }
1269}
1270
1272 const int grid_color_id,
1273 const float min_step,
1274 const int grid_subdivisions)
1275{
1276 BLI_assert(grid_subdivisions >= 0 && grid_subdivisions < 4);
1277 if (grid_subdivisions == 0) {
1278 return;
1279 }
1280
1281 const float zoom_x = float(BLI_rcti_size_x(&v2d->mask) + 1) / BLI_rctf_size_x(&v2d->cur);
1282
1283 GPUVertFormat *format = immVertexFormat();
1284 const uint pos = GPU_vertformat_attr_add(format, "pos", gpu::VertAttrType::SFLOAT_32_32);
1287
1288 /* Scaling the dots fully with the zoom looks too busy, but a bit of size variation is nice. */
1289 const float min_point_size = 2.0f * U.pixelsize;
1290 const float point_size_factor = 1.5f;
1291 const float max_point_size = point_size_factor * min_point_size;
1292
1293 /* Each consecutive grid level is five times larger than the previous. */
1294 const int subdivision_scale = 5;
1295
1296 const float view_level = logf(min_step / zoom_x) / logf(subdivision_scale);
1297 const int largest_visible_level = int(view_level);
1298
1299 for (int level_offset = 0; level_offset <= grid_subdivisions; level_offset++) {
1300 const int level = largest_visible_level - level_offset;
1301
1302 if (level < 0) {
1303 break;
1304 }
1305
1306 const float level_scale = powf(subdivision_scale, level);
1307 const float point_size_precise = min_point_size * level_scale * zoom_x;
1308 const float point_size_draw = ceilf(
1309 clamp_f(point_size_precise, min_point_size, max_point_size));
1310
1311 /* Offset point by this amount to better align centers as size changes. */
1312 const float point_size_offset = (point_size_draw / 2.0f) - U.pixelsize;
1313
1314 /* To compensate the for the clamped point_size we adjust the alpha to make the overall
1315 * brightness of the grid background more consistent. */
1316 const float alpha = pow2f(point_size_precise / point_size_draw);
1317
1318 /* Make sure we don't draw points once the alpha gets too low. */
1319 const float alpha_cutoff = 0.01f;
1320 if (alpha < alpha_cutoff) {
1321 break;
1322 }
1323 const float alpha_clamped = clamp_f((1.0f + alpha_cutoff) * alpha - alpha_cutoff, 0.0f, 1.0f);
1324
1325 /* If we have don't draw enough subdivision levels so they fade out naturally, we apply an
1326 * additional fade to the last level to avoid pop in. */
1327 const bool last_level = level_offset == grid_subdivisions;
1328 const float subdivision_fade = last_level ? (1.0f - fractf(view_level)) : 1.0f;
1329
1330 float color[4];
1331 theme::get_color_3fv(grid_color_id, color);
1332 color[3] = alpha_clamped * subdivision_fade;
1333
1334 const float step = min_step * level_scale;
1335 int count_x;
1336 float start_x;
1337
1338 /* Count points that fit in viewport. */
1339 grid_axis_start_and_count(step, v2d->cur.xmin, v2d->cur.xmax, &start_x, &count_x);
1340 int count_y;
1341 float start_y;
1342 grid_axis_start_and_count(step, v2d->cur.ymin, v2d->cur.ymax, &start_y, &count_y);
1343 if (count_x == 0 || count_y == 0) {
1344 continue;
1345 }
1346
1347 immUniform1f("size", point_size_draw);
1348 immUniform4fv("color", color);
1349 immBegin(GPU_PRIM_POINTS, count_x * count_y);
1350
1351 /* Theoretically drawing on top of lower grid levels could be avoided, but it would also
1352 * increase the complexity of this loop, which isn't worth the time at the moment. */
1353 for (int i_y = 0; i_y < count_y; i_y++) {
1354 const float y = start_y + step * i_y;
1355 for (int i_x = 0; i_x < count_x; i_x++) {
1356 const float x = start_x + step * i_x;
1357 immVertex2f(pos, x + point_size_offset, y + point_size_offset);
1358 }
1359 }
1360
1361 immEnd();
1362 }
1363
1366}
1367
1369
1370/* -------------------------------------------------------------------- */
1373
1374void view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom, View2DScrollers *r_scrollers)
1375{
1376 rcti vert, hor;
1377 float fac1, fac2, totsize, scrollsize;
1378 const int scroll = view2d_scroll_mapped(v2d->scroll);
1379
1380 /* Always update before drawing (for dynamically sized scrollers). */
1381 view2d_masks(v2d, mask_custom);
1382
1383 vert = v2d->vert;
1384 hor = v2d->hor;
1385
1386 /* Pad scroll-bar drawing away from region edges. */
1387 const int edge_pad = int(0.1f * U.widget_unit);
1388 if (scroll & V2D_SCROLL_BOTTOM) {
1389 hor.ymin += edge_pad;
1390 }
1391 else {
1392 hor.ymax -= edge_pad;
1393 }
1394
1395 if (scroll & V2D_SCROLL_LEFT) {
1396 vert.xmin += edge_pad;
1397 }
1398 else {
1399 vert.xmax -= edge_pad;
1400 }
1401
1402 CLAMP_MAX(vert.ymin, vert.ymax - V2D_SCROLL_HANDLE_SIZE_HOTSPOT);
1404
1405 /* store in scrollers, used for drawing */
1406 r_scrollers->vert = vert;
1407 r_scrollers->hor = hor;
1408
1409 /* scroller 'buttons':
1410 * - These should always remain within the visible region of the scroll-bar
1411 * - They represent the region of 'tot' that is visible in 'cur'
1412 */
1413
1414 /* horizontal scrollers */
1415 if (scroll & V2D_SCROLL_HORIZONTAL) {
1416 /* scroller 'button' extents */
1417 totsize = BLI_rctf_size_x(&v2d->tot);
1418 scrollsize = float(BLI_rcti_size_x(&hor));
1419 if (totsize == 0.0f) {
1420 totsize = 1.0f; /* avoid divide by zero */
1421 }
1422
1423 fac1 = (v2d->cur.xmin - v2d->tot.xmin) / totsize;
1424 if (fac1 <= 0.0f) {
1425 r_scrollers->hor_min = hor.xmin;
1426 }
1427 else {
1428 r_scrollers->hor_min = int(hor.xmin + (fac1 * scrollsize));
1429 }
1430
1431 fac2 = (v2d->cur.xmax - v2d->tot.xmin) / totsize;
1432 if (fac2 >= 1.0f) {
1433 r_scrollers->hor_max = hor.xmax;
1434 }
1435 else {
1436 r_scrollers->hor_max = int(hor.xmin + (fac2 * scrollsize));
1437 }
1438
1439 /* prevent inverted sliders */
1440 r_scrollers->hor_min = std::min(r_scrollers->hor_min, r_scrollers->hor_max);
1441 /* prevent sliders from being too small to grab */
1442 if ((r_scrollers->hor_max - r_scrollers->hor_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
1443 r_scrollers->hor_max = r_scrollers->hor_min + V2D_SCROLL_THUMB_SIZE_MIN;
1444
1445 CLAMP(r_scrollers->hor_max, hor.xmin + V2D_SCROLL_THUMB_SIZE_MIN, hor.xmax);
1446 CLAMP(r_scrollers->hor_min, hor.xmin, hor.xmax - V2D_SCROLL_THUMB_SIZE_MIN);
1447 }
1448 }
1449
1450 /* vertical scrollers */
1451 if (scroll & V2D_SCROLL_VERTICAL) {
1452 /* scroller 'button' extents */
1453 totsize = BLI_rctf_size_y(&v2d->tot);
1454 scrollsize = float(BLI_rcti_size_y(&vert));
1455 if (totsize == 0.0f) {
1456 totsize = 1.0f; /* avoid divide by zero */
1457 }
1458
1459 fac1 = (v2d->cur.ymin - v2d->tot.ymin) / totsize;
1460 if (fac1 <= 0.0f) {
1461 r_scrollers->vert_min = vert.ymin;
1462 }
1463 else {
1464 r_scrollers->vert_min = int(vert.ymin + (fac1 * scrollsize));
1465 }
1466
1467 fac2 = (v2d->cur.ymax - v2d->tot.ymin) / totsize;
1468 if (fac2 >= 1.0f) {
1469 r_scrollers->vert_max = vert.ymax;
1470 }
1471 else {
1472 r_scrollers->vert_max = int(vert.ymin + (fac2 * scrollsize));
1473 }
1474
1475 /* prevent inverted sliders */
1476 r_scrollers->vert_min = std::min(r_scrollers->vert_min, r_scrollers->vert_max);
1477 /* prevent sliders from being too small to grab */
1478 if ((r_scrollers->vert_max - r_scrollers->vert_min) < V2D_SCROLL_THUMB_SIZE_MIN) {
1479 r_scrollers->vert_max = r_scrollers->vert_min + V2D_SCROLL_THUMB_SIZE_MIN;
1480
1481 CLAMP(r_scrollers->vert_max, vert.ymin + V2D_SCROLL_THUMB_SIZE_MIN, vert.ymax);
1482 CLAMP(r_scrollers->vert_min, vert.ymin, vert.ymax - V2D_SCROLL_THUMB_SIZE_MIN);
1483 }
1484 }
1485}
1486
1487void view2d_scrollers_draw(View2D *v2d, const rcti *mask_custom)
1488{
1489 View2DScrollers scrollers;
1490 view2d_scrollers_calc(v2d, mask_custom, &scrollers);
1491 const bTheme *btheme = theme::theme_get();
1492 rcti vert, hor;
1493 const int scroll = view2d_scroll_mapped(v2d->scroll);
1494 const char emboss_alpha = btheme->tui.widget_emboss[3];
1495 const float alpha_min = V2D_SCROLL_MIN_ALPHA;
1496
1497 uchar scrollers_back_color[4];
1498
1499 /* Color for scroll-bar backs. */
1500 theme::get_color_4ubv(TH_BACK, scrollers_back_color);
1501
1502 /* make copies of rects for less typing */
1503 vert = scrollers.vert;
1504 hor = scrollers.hor;
1505
1506 /* Horizontal scroll-bar. */
1507 if (scroll & V2D_SCROLL_HORIZONTAL) {
1508 uiWidgetColors wcol = btheme->tui.wcol_scroll;
1509 /* 0..255 -> min...1 */
1510 const float alpha_fac = ((v2d->alpha_hor / 255.0f) * (1.0f - alpha_min)) + alpha_min;
1511 rcti slider;
1512 int state;
1513
1514 slider.xmin = scrollers.hor_min;
1515 slider.xmax = scrollers.hor_max;
1516 slider.ymin = hor.ymin;
1517 slider.ymax = hor.ymax;
1518
1520
1521 /* In the case that scroll-bar track is invisible, range from 0 ->`final_alpha` instead to
1522 * avoid errors with users trying to click into the underlying view. */
1523 if (wcol.inner[3] == 0) {
1524 const float final_alpha = 0.25f;
1525 wcol.inner[3] = final_alpha * v2d->alpha_hor;
1526 }
1527 else {
1528 wcol.inner[3] *= alpha_fac;
1529 }
1530 wcol.item[3] *= alpha_fac;
1531 wcol.outline[3] = 0;
1532 const_cast<bTheme *>(btheme)->tui.widget_emboss[3] = 0; /* will be reset later */
1533
1534 /* show zoom handles if:
1535 * - zooming on x-axis is allowed (no scroll otherwise)
1536 * - slider bubble is large enough (no overdraw confusion)
1537 * - scale is shown on the scroller
1538 * (workaround to make sure that button windows don't show these,
1539 * and only the time-grids with their zoom-ability can do so).
1540 */
1541 if ((v2d->keepzoom & V2D_LOCKZOOM_X) == 0 && (v2d->scroll & V2D_SCROLL_HORIZONTAL_HANDLES) &&
1543 {
1545 }
1546
1547 draw_widget_scroll(&wcol, &hor, &slider, state);
1548 }
1549
1550 /* Vertical scroll-bar. */
1551 if (scroll & V2D_SCROLL_VERTICAL) {
1552 uiWidgetColors wcol = btheme->tui.wcol_scroll;
1553 rcti slider;
1554 /* 0..255 -> min...1 */
1555 const float alpha_fac = ((v2d->alpha_vert / 255.0f) * (1.0f - alpha_min)) + alpha_min;
1556 int state;
1557
1558 slider.xmin = vert.xmin;
1559 slider.xmax = vert.xmax;
1560 slider.ymin = scrollers.vert_min;
1561 slider.ymax = scrollers.vert_max;
1562
1564
1565 /* In the case that scroll-bar track is invisible, range from 0 ->`final_alpha` instead to
1566 * avoid errors with users trying to click into the underlying view. */
1567 if (wcol.inner[3] == 0) {
1568 const float final_alpha = 0.25f;
1569 wcol.inner[3] = final_alpha * v2d->alpha_vert;
1570 }
1571 else {
1572 wcol.inner[3] *= alpha_fac;
1573 }
1574 wcol.item[3] *= alpha_fac;
1575 wcol.outline[3] = 0;
1576 const_cast<bTheme *>(btheme)->tui.widget_emboss[3] = 0; /* will be reset later */
1577
1578 /* show zoom handles if:
1579 * - zooming on y-axis is allowed (no scroll otherwise)
1580 * - slider bubble is large enough (no overdraw confusion)
1581 * - scale is shown on the scroller
1582 * (workaround to make sure that button windows don't show these,
1583 * and only the time-grids with their zoomability can do so)
1584 */
1585 if ((v2d->keepzoom & V2D_LOCKZOOM_Y) == 0 && (v2d->scroll & V2D_SCROLL_VERTICAL_HANDLES) &&
1587 {
1589 }
1590
1591 draw_widget_scroll(&wcol, &vert, &slider, state);
1592 }
1593
1594 /* Was changed above, so reset. */
1595 const_cast<bTheme *>(btheme)->tui.widget_emboss[3] = emboss_alpha;
1596}
1597
1599
1600/* -------------------------------------------------------------------- */
1603
1604void view2d_listview_view_to_cell(float columnwidth,
1605 float rowheight,
1606 float startx,
1607 float starty,
1608 float viewx,
1609 float viewy,
1610 int *r_column,
1611 int *r_row)
1612{
1613 if (r_column) {
1614 if (columnwidth > 0) {
1615 /* Columns go from left to right (x increases). */
1616 *r_column = floorf((viewx - startx) / columnwidth);
1617 }
1618 else {
1619 *r_column = 0;
1620 }
1621 }
1622
1623 if (r_row) {
1624 if (rowheight > 0) {
1625 /* Rows got from top to bottom (y decreases). */
1626 *r_row = floorf((starty - viewy) / rowheight);
1627 }
1628 else {
1629 *r_row = 0;
1630 }
1631 }
1632}
1633
1635
1636/* -------------------------------------------------------------------- */
1639
1640float view2d_region_to_view_x(const View2D *v2d, float x)
1641{
1642 return (v2d->cur.xmin +
1643 (BLI_rctf_size_x(&v2d->cur) * (x - v2d->mask.xmin) / BLI_rcti_size_x(&v2d->mask)));
1644}
1645float view2d_region_to_view_y(const View2D *v2d, float y)
1646{
1647 return (v2d->cur.ymin +
1648 (BLI_rctf_size_y(&v2d->cur) * (y - v2d->mask.ymin) / BLI_rcti_size_y(&v2d->mask)));
1649}
1650
1651void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y)
1652{
1653 *r_view_x = view2d_region_to_view_x(v2d, x);
1654 *r_view_y = view2d_region_to_view_y(v2d, y);
1655}
1656
1657void view2d_region_to_view_rctf(const View2D *v2d, const rctf *rect_src, rctf *rect_dst)
1658{
1659 const float cur_size[2] = {BLI_rctf_size_x(&v2d->cur), BLI_rctf_size_y(&v2d->cur)};
1660 const float mask_size[2] = {float(BLI_rcti_size_x(&v2d->mask)),
1661 float(BLI_rcti_size_y(&v2d->mask))};
1662
1663 rect_dst->xmin = (v2d->cur.xmin +
1664 (cur_size[0] * (rect_src->xmin - v2d->mask.xmin) / mask_size[0]));
1665 rect_dst->xmax = (v2d->cur.xmin +
1666 (cur_size[0] * (rect_src->xmax - v2d->mask.xmin) / mask_size[0]));
1667 rect_dst->ymin = (v2d->cur.ymin +
1668 (cur_size[1] * (rect_src->ymin - v2d->mask.ymin) / mask_size[1]));
1669 rect_dst->ymax = (v2d->cur.ymin +
1670 (cur_size[1] * (rect_src->ymax - v2d->mask.ymin) / mask_size[1]));
1671}
1672
1673float view2d_view_to_region_x(const View2D *v2d, float x)
1674{
1675 return (v2d->mask.xmin + (((x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur)) *
1676 float(BLI_rcti_size_x(&v2d->mask) + 1)));
1677}
1678float view2d_view_to_region_y(const View2D *v2d, float y)
1679{
1680 return (v2d->mask.ymin + (((y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur)) *
1681 float(BLI_rcti_size_y(&v2d->mask) + 1)));
1682}
1683
1685 const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y)
1686{
1687 /* express given coordinates as proportional values */
1688 x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
1689 y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
1690
1691 /* check if values are within bounds */
1692 if ((x >= 0.0f) && (x <= 1.0f) && (y >= 0.0f) && (y <= 1.0f)) {
1693 *r_region_x = int(v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask)));
1694 *r_region_y = int(v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask)));
1695
1696 return true;
1697 }
1698
1699 /* set initial value in case coordinate lies outside of bounds */
1700 *r_region_x = *r_region_y = V2D_IS_CLIPPED;
1701
1702 return false;
1703}
1704
1705void view2d_view_to_region(const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y)
1706{
1707 /* Step 1: express given coordinates as proportional values. */
1708 x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
1709 y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
1710
1711 /* Step 2: convert proportional distances to screen coordinates. */
1712 x = v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask));
1713 y = v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask));
1714
1715 /* Although we don't clamp to lie within region bounds, we must avoid exceeding size of ints. */
1716 *r_region_x = clamp_float_to_int(x);
1717 *r_region_y = clamp_float_to_int(y);
1718}
1719
1721 const View2D *v2d, float x, float y, float *r_region_x, float *r_region_y)
1722{
1723 /* express given coordinates as proportional values */
1724 x = (x - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur);
1725 y = (y - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur);
1726
1727 /* convert proportional distances to screen coordinates */
1728 *r_region_x = v2d->mask.xmin + (x * BLI_rcti_size_x(&v2d->mask));
1729 *r_region_y = v2d->mask.ymin + (y * BLI_rcti_size_y(&v2d->mask));
1730}
1731
1733 const float xy_a[2],
1734 const float xy_b[2],
1735 int r_region_a[2],
1736 int r_region_b[2])
1737{
1738 rctf rect_unit;
1739 rect_unit.xmin = rect_unit.ymin = 0.0f;
1740 rect_unit.xmax = rect_unit.ymax = 1.0f;
1741
1742 /* Express given coordinates as proportional values. */
1743 const float s_a[2] = {
1744 (xy_a[0] - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur),
1745 (xy_a[1] - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur),
1746 };
1747 const float s_b[2] = {
1748 (xy_b[0] - v2d->cur.xmin) / BLI_rctf_size_x(&v2d->cur),
1749 (xy_b[1] - v2d->cur.ymin) / BLI_rctf_size_y(&v2d->cur),
1750 };
1751
1752 /* Set initial value in case coordinates lie outside bounds. */
1753 r_region_a[0] = r_region_b[0] = r_region_a[1] = r_region_b[1] = V2D_IS_CLIPPED;
1754
1755 if (BLI_rctf_isect_segment(&rect_unit, s_a, s_b)) {
1756 r_region_a[0] = int(v2d->mask.xmin + (s_a[0] * BLI_rcti_size_x(&v2d->mask)));
1757 r_region_a[1] = int(v2d->mask.ymin + (s_a[1] * BLI_rcti_size_y(&v2d->mask)));
1758 r_region_b[0] = int(v2d->mask.xmin + (s_b[0] * BLI_rcti_size_x(&v2d->mask)));
1759 r_region_b[1] = int(v2d->mask.ymin + (s_b[1] * BLI_rcti_size_y(&v2d->mask)));
1760
1761 return true;
1762 }
1763
1764 return false;
1765}
1766
1767void view2d_view_to_region_rcti(const View2D *v2d, const rctf *rect_src, rcti *rect_dst)
1768{
1769 const float cur_size[2] = {BLI_rctf_size_x(&v2d->cur), BLI_rctf_size_y(&v2d->cur)};
1770 const float mask_size[2] = {float(BLI_rcti_size_x(&v2d->mask)),
1771 float(BLI_rcti_size_y(&v2d->mask))};
1772 rctf rect_tmp;
1773
1774 /* Step 1: express given coordinates as proportional values. */
1775 rect_tmp.xmin = (rect_src->xmin - v2d->cur.xmin) / cur_size[0];
1776 rect_tmp.xmax = (rect_src->xmax - v2d->cur.xmin) / cur_size[0];
1777 rect_tmp.ymin = (rect_src->ymin - v2d->cur.ymin) / cur_size[1];
1778 rect_tmp.ymax = (rect_src->ymax - v2d->cur.ymin) / cur_size[1];
1779
1780 /* Step 2: convert proportional distances to screen coordinates. */
1781 rect_tmp.xmin = v2d->mask.xmin + (rect_tmp.xmin * mask_size[0]);
1782 rect_tmp.xmax = v2d->mask.xmin + (rect_tmp.xmax * mask_size[0]);
1783 rect_tmp.ymin = v2d->mask.ymin + (rect_tmp.ymin * mask_size[1]);
1784 rect_tmp.ymax = v2d->mask.ymin + (rect_tmp.ymax * mask_size[1]);
1785
1786 clamp_rctf_to_rcti(rect_dst, &rect_tmp);
1787}
1788
1789void view2d_view_to_region_m4(const View2D *v2d, float matrix[4][4])
1790{
1791 rctf mask;
1792 unit_m4(matrix);
1793 BLI_rctf_rcti_copy(&mask, &v2d->mask);
1795}
1796
1797bool view2d_view_to_region_rcti_clip(const View2D *v2d, const rctf *rect_src, rcti *rect_dst)
1798{
1799 const float cur_size[2] = {BLI_rctf_size_x(&v2d->cur), BLI_rctf_size_y(&v2d->cur)};
1800 const float mask_size[2] = {float(BLI_rcti_size_x(&v2d->mask) + 1),
1801 float(BLI_rcti_size_y(&v2d->mask) + 1)};
1802 rctf rect_tmp;
1803
1804 BLI_assert(rect_src->xmin <= rect_src->xmax && rect_src->ymin <= rect_src->ymax);
1805
1806 /* Step 1: express given coordinates as proportional values. */
1807 rect_tmp.xmin = (rect_src->xmin - v2d->cur.xmin) / cur_size[0];
1808 rect_tmp.xmax = (rect_src->xmax - v2d->cur.xmin) / cur_size[0];
1809 rect_tmp.ymin = (rect_src->ymin - v2d->cur.ymin) / cur_size[1];
1810 rect_tmp.ymax = (rect_src->ymax - v2d->cur.ymin) / cur_size[1];
1811
1812 if (((rect_tmp.xmax < 0.0f) || (rect_tmp.xmin > 1.0f) || (rect_tmp.ymax < 0.0f) ||
1813 (rect_tmp.ymin > 1.0f)) == 0)
1814 {
1815 /* Step 2: convert proportional distances to screen coordinates. */
1816 rect_tmp.xmin = v2d->mask.xmin + (rect_tmp.xmin * mask_size[0]);
1817 rect_tmp.xmax = v2d->mask.ymin + (rect_tmp.xmax * mask_size[0]);
1818 rect_tmp.ymin = v2d->mask.ymin + (rect_tmp.ymin * mask_size[1]);
1819 rect_tmp.ymax = v2d->mask.ymin + (rect_tmp.ymax * mask_size[1]);
1820
1821 clamp_rctf_to_rcti(rect_dst, &rect_tmp);
1822
1823 return true;
1824 }
1825
1826 rect_dst->xmin = rect_dst->xmax = rect_dst->ymin = rect_dst->ymax = V2D_IS_CLIPPED;
1827 return false;
1828}
1829
1831
1832/* -------------------------------------------------------------------- */
1835
1837{
1838 ScrArea *area = CTX_wm_area(C);
1839 ARegion *region = CTX_wm_region(C);
1840
1841 if (area == nullptr) {
1842 return nullptr;
1843 }
1844 if (region == nullptr) {
1845 return nullptr;
1846 }
1847 return &(region->v2d);
1848}
1849
1851{
1852 ScrArea *area = CTX_wm_area(C);
1853 ARegion *region = CTX_wm_region(C);
1854
1855 if (area == nullptr) {
1856 return nullptr;
1857 }
1858 if (region == nullptr) {
1859 return nullptr;
1860 }
1861 if (region->regiontype != RGN_TYPE_WINDOW) {
1863 return region_win ? &(region_win->v2d) : nullptr;
1864 }
1865 return &(region->v2d);
1866}
1867
1868void view2d_scroller_size_get(const View2D *v2d, bool mapped, float *r_x, float *r_y)
1869{
1870 const int scroll = (mapped) ? view2d_scroll_mapped(v2d->scroll) : v2d->scroll;
1871
1872 if (r_x) {
1873 if (scroll & V2D_SCROLL_VERTICAL) {
1875 *r_x = ((*r_x - V2D_SCROLL_MIN_WIDTH) * (v2d->alpha_vert / 255.0f)) + V2D_SCROLL_MIN_WIDTH;
1876 }
1877 else {
1878 *r_x = 0;
1879 }
1880 }
1881 if (r_y) {
1882 if (scroll & V2D_SCROLL_HORIZONTAL) {
1885 *r_y = ((*r_y - V2D_SCROLL_MIN_WIDTH) * (v2d->alpha_hor / 255.0f)) + V2D_SCROLL_MIN_WIDTH;
1886 }
1887 else {
1888 *r_y = 0;
1889 }
1890 }
1891}
1892
1893void view2d_scale_get(const View2D *v2d, float *r_x, float *r_y)
1894{
1895 if (r_x) {
1896 *r_x = view2d_scale_get_x(v2d);
1897 }
1898 if (r_y) {
1899 *r_y = view2d_scale_get_y(v2d);
1900 }
1901}
1903{
1904 return BLI_rcti_size_x(&v2d->mask) / BLI_rctf_size_x(&v2d->cur);
1905}
1907{
1908 return BLI_rcti_size_y(&v2d->mask) / BLI_rctf_size_y(&v2d->cur);
1909}
1910
1911void view2d_pixel_size_get(const View2D *v2d, float *r_x, float *r_y)
1912{
1913 if (r_x) {
1914 *r_x = view2d_pixel_size_get_x(v2d);
1915 }
1916 if (r_y) {
1917 *r_y = view2d_pixel_size_get_y(v2d);
1918 }
1919}
1921{
1922 return BLI_rctf_size_x(&v2d->cur) / (BLI_rcti_size_x(&v2d->mask) + 1);
1923}
1925{
1926 return BLI_rctf_size_y(&v2d->cur) / (BLI_rcti_size_y(&v2d->mask) + 1);
1927}
1928
1929void view2d_center_get(const View2D *v2d, float *r_x, float *r_y)
1930{
1931 /* get center */
1932 if (r_x) {
1933 *r_x = BLI_rctf_cent_x(&v2d->cur);
1934 }
1935 if (r_y) {
1936 *r_y = BLI_rctf_cent_y(&v2d->cur);
1937 }
1938}
1939void view2d_center_set(View2D *v2d, float x, float y)
1940{
1941 BLI_rctf_recenter(&v2d->cur, x, y);
1942
1943 /* make sure that 'cur' rect is in a valid state as a result of these changes */
1945}
1946
1947void view2d_size_x_set(View2D *v2d, float size_x)
1948{
1949 BLI_assert(BLI_rctf_size_y(&v2d->cur) != 0.0f);
1950 const float aspect = BLI_rctf_size_x(&v2d->cur) / BLI_rctf_size_y(&v2d->cur);
1951 BLI_rctf_resize(&v2d->cur, size_x, size_x / aspect);
1952}
1953
1954void view2d_offset(View2D *v2d, float xfac, float yfac)
1955{
1956 if (xfac != -1.0f) {
1957 const float xsize = BLI_rctf_size_x(&v2d->cur);
1958 const float xmin = v2d->tot.xmin;
1959 const float xmax = v2d->tot.xmax - xsize;
1960
1961 v2d->cur.xmin = (xmin * (1.0f - xfac)) + (xmax * xfac);
1962 v2d->cur.xmax = v2d->cur.xmin + xsize;
1963 }
1964
1965 if (yfac != -1.0f) {
1966 const float ysize = BLI_rctf_size_y(&v2d->cur);
1967 const float ymin = v2d->tot.ymin;
1968 const float ymax = v2d->tot.ymax - ysize;
1969
1970 v2d->cur.ymin = (ymin * (1.0f - yfac)) + (ymax * yfac);
1971 v2d->cur.ymax = v2d->cur.ymin + ysize;
1972 }
1973
1975}
1976
1978{
1979 const float cur_size_y = BLI_rctf_size_y(&v2d->cur);
1980 const float page_size_y = view2d_page_size_y(*v2d);
1981
1982 v2d->cur.ymax = roundf(v2d->cur.ymax / page_size_y) * page_size_y;
1983 v2d->cur.ymin = v2d->cur.ymax - cur_size_y;
1984
1986}
1987
1989 const View2D *v2d,
1990 const int xy[2],
1991 int *r_scroll)
1992{
1993 const int scroll = view2d_scroll_mapped(v2d->scroll);
1994 *r_scroll = scroll;
1995
1996 if (scroll) {
1997 /* Move to region-coordinates. */
1998 const int co[2] = {
1999 xy[0] - region->winrct.xmin,
2000 xy[1] - region->winrct.ymin,
2001 };
2002 if (scroll & V2D_SCROLL_HORIZONTAL) {
2003 if (IN_2D_HORIZ_SCROLL(v2d, co)) {
2004 return 'h';
2005 }
2006 }
2007 if (scroll & V2D_SCROLL_VERTICAL) {
2008 if (IN_2D_VERT_SCROLL(v2d, co)) {
2009 return 'v';
2010 }
2011 }
2012 }
2013
2014 return 0;
2015}
2016
2018 const View2D *v2d,
2019 const rcti *rect,
2020 int *r_scroll)
2021{
2022 const int scroll = view2d_scroll_mapped(v2d->scroll);
2023 *r_scroll = scroll;
2024
2025 if (scroll) {
2026 /* Move to region-coordinates. */
2027 rcti rect_region = *rect;
2028 BLI_rcti_translate(&rect_region, -region->winrct.xmin, region->winrct.ymin);
2029 if (scroll & V2D_SCROLL_HORIZONTAL) {
2030 if (IN_2D_HORIZ_SCROLL_RECT(v2d, &rect_region)) {
2031 return 'h';
2032 }
2033 }
2034 if (scroll & V2D_SCROLL_VERTICAL) {
2035 if (IN_2D_VERT_SCROLL_RECT(v2d, &rect_region)) {
2036 return 'v';
2037 }
2038 }
2039 }
2040
2041 return 0;
2042}
2043
2044char view2d_mouse_in_scrollers(const ARegion *region, const View2D *v2d, const int xy[2])
2045{
2046 int scroll_dummy = 0;
2047 return view2d_mouse_in_scrollers_ex(region, v2d, xy, &scroll_dummy);
2048}
2049
2050char view2d_rect_in_scrollers(const ARegion *region, const View2D *v2d, const rcti *rect)
2051{
2052 int scroll_dummy = 0;
2053 return view2d_rect_in_scrollers_ex(region, v2d, rect, &scroll_dummy);
2054}
2055
2057
2058/* -------------------------------------------------------------------- */
2061
2064 union {
2065 uchar ub[4];
2066 int pack;
2069 int mval[2];
2070
2071 /* str is allocated past the end */
2072 char str[0];
2073};
2074
2075/* assumes caches are used correctly, so for time being no local storage in v2d */
2077static View2DString *g_v2d_strings = nullptr;
2078
2080 View2D *v2d, float x, float y, const char *str, size_t str_len, const uchar col[4])
2081{
2082 int mval[2];
2083
2084 BLI_assert(str_len == strlen(str));
2085
2086 if (view2d_view_to_region_clip(v2d, x, y, &mval[0], &mval[1])) {
2087 const int alloc_len = str_len + 1;
2088
2089 if (g_v2d_strings_arena == nullptr) {
2091 }
2092
2093 View2DString *v2s = static_cast<View2DString *>(
2095
2097
2098 v2s->col.pack = *(reinterpret_cast<const int *>(col));
2099
2100 v2s->rect = rcti{};
2101
2102 v2s->mval[0] = mval[0];
2103 v2s->mval[1] = mval[1];
2104
2105 memcpy(v2s->str, str, alloc_len);
2106 }
2107}
2108
2110 View2D *v2d, const rctf *rect_view, const char *str, size_t str_len, const uchar col[4])
2111{
2112 rcti rect;
2113
2114 BLI_assert(str_len == strlen(str));
2115
2116 if (view2d_view_to_region_rcti_clip(v2d, rect_view, &rect)) {
2117 const int alloc_len = str_len + 1;
2118
2119 if (g_v2d_strings_arena == nullptr) {
2121 }
2122
2123 View2DString *v2s = static_cast<View2DString *>(
2125
2127
2128 v2s->col.pack = *(reinterpret_cast<const int *>(col));
2129
2130 v2s->rect = rect;
2131
2132 v2s->mval[0] = v2s->rect.xmin;
2133 v2s->mval[1] = v2s->rect.ymin;
2134
2135 memcpy(v2s->str, str, alloc_len);
2136 }
2137}
2138
2140{
2141 View2DString *v2s;
2142 int col_pack_prev = 0;
2143
2144 /* investigate using BLF_ascender() */
2145 const int font_id = BLF_default();
2146
2148 const float default_height = g_v2d_strings ? BLF_height(font_id, "28", 3) : 0.0f;
2149
2151
2152 for (v2s = g_v2d_strings; v2s; v2s = v2s->next) {
2153 int xofs = 0, yofs;
2154
2155 yofs = ceil(0.5f * (BLI_rcti_size_y(&v2s->rect) - default_height));
2156 yofs = std::max(yofs, 1);
2157
2158 if (col_pack_prev != v2s->col.pack) {
2159 BLF_color4ubv(font_id, v2s->col.ub);
2160 col_pack_prev = v2s->col.pack;
2161 }
2162
2163 /* Don't use clipping if `v2s->rect` is not set. */
2164 if (BLI_rcti_size_x(&v2s->rect) == 0 && BLI_rcti_size_y(&v2s->rect) == 0) {
2165 BLF_draw_default(float(v2s->mval[0] + xofs),
2166 float(v2s->mval[1] + yofs),
2167 0.0,
2168 v2s->str,
2170 }
2171 else {
2172 BLF_enable(font_id, BLF_CLIPPING);
2174 font_id, v2s->rect.xmin - 4, v2s->rect.ymin - 4, v2s->rect.xmax + 4, v2s->rect.ymax + 4);
2176 v2s->rect.xmin + xofs, v2s->rect.ymin + yofs, 0.0f, v2s->str, BLF_DRAW_STR_DUMMY_MAX);
2177 BLF_disable(font_id, BLF_CLIPPING);
2178 }
2179 }
2180 g_v2d_strings = nullptr;
2181
2182 if (g_v2d_strings_arena) {
2184 g_v2d_strings_arena = nullptr;
2185 }
2186}
2187
2189
2190} // namespace blender::ui
#define BLF_DRAW_STR_DUMMY_MAX
Definition BLF_api.hh:456
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define UI_AZONESPOTW_RIGHT
#define UI_AZONESPOTW_LEFT
#define UI_AZONESPOTH
#define V2D_SCROLL_HEIGHT
Definition UI_view2d.hh:78
#define V2D_LOCK_COPY
Definition UI_view2d.hh:110
#define V2D_SCROLL_MIN_ALPHA
Definition UI_view2d.hh:82
#define V2D_SCROLL_HANDLE_HEIGHT
Definition UI_view2d.hh:92
#define V2D_SCROLL_THUMB_SIZE_MIN
Definition UI_view2d.hh:99
#define IN_2D_HORIZ_SCROLL(v2d, co)
Definition UI_view2d.hh:120
#define V2D_SCROLL_WIDTH
Definition UI_view2d.hh:79
#define V2D_SCROLL_HANDLE_WIDTH
Definition UI_view2d.hh:93
#define IN_2D_VERT_SCROLL(v2d, co)
Definition UI_view2d.hh:119
#define IN_2D_VERT_SCROLL_RECT(v2d, rct)
Definition UI_view2d.hh:122
#define V2D_SCROLL_HANDLE_SIZE_HOTSPOT
Definition UI_view2d.hh:96
#define V2D_SCROLL_MIN_WIDTH
Definition UI_view2d.hh:85
#define V2D_IS_CLIPPED
Definition UI_view2d.hh:46
#define IN_2D_HORIZ_SCROLL_RECT(v2d, rct)
Definition UI_view2d.hh:123
#define CLAMP(a, b, c)
#define CLAMP_MAX(a, c)
#define BLI_INLINE
#define UNPACK3(a)
#define ELEM(...)
#define MEM_SIZE_OPTIMAL(size)
btScalar max
#define logf(x)
#define powf(x, y)
#define roundf(x)
#define str(s)
IMETHOD Vector diff(const Vector &a, const Vector &b, double dt)
Definition frames.inl:1166
#define FLT_MIN
#define INT_MAX
#define abs
#define ceil
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
const int state
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
ccl_device_inline float2 fmod(const float2 a, const float b)
void get_color_4ubv(int colorid, unsigned char col[4])
void get_color_3fv(int colorid, float col[3])
void get_color_blend_shade_3ubv(int colorid1, int colorid2, float fac, int offset, unsigned char col[3])
const bTheme * theme_get()
char char char char void view2d_text_cache_add(View2D *v2d, float x, float y, const char *str, size_t str_len, const unsigned char col[4])
Definition view2d.cc:2079
static void view2d_masks(View2D *v2d, const rcti *mask_scroll)
Definition view2d.cc:124
static View2DString * g_v2d_strings
Definition view2d.cc:2077
void view2d_view_to_region_fl(const View2D *v2d, float x, float y, float *r_region_x, float *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1720
void view2d_view_ortho(const View2D *v2d)
Definition view2d.cc:1080
void view2d_totRect_set(View2D *v2d, int width, int height)
Definition view2d.cc:1021
BLI_INLINE void clamp_rctf_to_rcti(rcti *dst, const rctf *src)
Definition view2d.cc:71
void view2d_curRect_validate(View2D *v2d)
Definition view2d.cc:810
const uiStyle * style_get()
BLI_INLINE int clamp_float_to_int(const float f)
Definition view2d.cc:53
void view2d_offset_y_snap_to_closest_page(View2D *v2d)
Definition view2d.cc:1977
float view2d_pixel_size_get_y(const View2D *v2d)
Definition view2d.cc:1924
void view2d_view_restore(const bContext *C)
Definition view2d.cc:1147
void view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
Definition view2d.cc:223
bool view2d_view_to_region_rcti_clip(const View2D *v2d, const rctf *rect_src, rcti *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1797
float view2d_view_to_region_y(const View2D *v2d, float y)
Definition view2d.cc:1678
void view2d_scrollers_calc(View2D *v2d, const rcti *mask_custom, View2DScrollers *r_scrollers)
Definition view2d.cc:1374
void view2d_mask_from_win(const View2D *v2d, rcti *r_mask)
Definition view2d.cc:111
void view2d_view_to_region_rcti(const View2D *v2d, const rctf *rect_src, rcti *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1767
void view2d_text_cache_draw(ARegion *region)
Definition view2d.cc:2139
char char view2d_mouse_in_scrollers(const ARegion *region, const View2D *v2d, const int xy[2]) ATTR_NONNULL(1
Definition view2d.cc:2044
void view2d_view_to_region(const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1705
void view2d_center_set(View2D *v2d, float x, float y)
Definition view2d.cc:1939
void view2d_curRect_reset(View2D *v2d)
Definition view2d.cc:913
bool view2d_view_to_region_segment_clip(const View2D *v2d, const float xy_a[2], const float xy_b[2], int r_region_a[2], int r_region_b[2]) ATTR_NONNULL()
Definition view2d.cc:1732
void view2d_scrollers_draw(View2D *v2d, const rcti *mask_custom)
Definition view2d.cc:1487
View2D * view2d_fromcontext_rwin(const bContext *C)
Definition view2d.cc:1850
float view2d_scale_get_y(const View2D *v2d)
Definition view2d.cc:1906
void view2d_region_to_view(const View2D *v2d, float x, float y, float *r_view_x, float *r_view_y) ATTR_NONNULL()
Definition view2d.cc:1651
void view2d_pixel_size_get(const View2D *v2d, float *r_x, float *r_y)
Definition view2d.cc:1911
void draw_widget_scroll(uiWidgetColors *wcol, const rcti *rect, const rcti *slider, int state)
void view2d_scroller_size_get(const View2D *v2d, bool mapped, float *r_x, float *r_y)
Definition view2d.cc:1868
void view2d_dot_grid_draw(const View2D *v2d, int grid_color_id, float min_step, int grid_subdivisions)
Definition view2d.cc:1271
bool view2d_view_to_region_clip(const View2D *v2d, float x, float y, int *r_region_x, int *r_region_y) ATTR_NONNULL()
Definition view2d.cc:1684
void view2d_text_cache_add_rectf(View2D *v2d, const rctf *rect_view, const char *str, size_t str_len, const unsigned char col[4])
Definition view2d.cc:2109
float view2d_region_to_view_x(const View2D *v2d, float x)
Definition view2d.cc:1640
float view2d_region_to_view_y(const View2D *v2d, float y)
Definition view2d.cc:1645
void view2d_listview_view_to_cell(float columnwidth, float rowheight, float startx, float starty, float viewx, float viewy, int *r_column, int *r_row)
Definition view2d.cc:1604
float view2d_pixel_size_get_x(const View2D *v2d)
Definition view2d.cc:1920
void view2d_center_get(const View2D *v2d, float *r_x, float *r_y)
Definition view2d.cc:1929
void view2d_curRect_clamp_y(View2D *v2d)
Definition view2d.cc:829
char view2d_mouse_in_scrollers_ex(const ARegion *region, const View2D *v2d, const int xy[2], int *r_scroll) ATTR_NONNULL(1
Definition view2d.cc:1988
static int view2d_scroll_mapped(int scroll)
Definition view2d.cc:100
void view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
Definition view2d.cc:850
float view2d_scale_get_x(const View2D *v2d)
Definition view2d.cc:1902
void view2d_curRect_changed(const bContext *C, View2D *v2d)
Definition view2d.cc:815
float view2d_page_size_y(const View2D &v2d)
Definition view2d.cc:79
void view2d_region_to_view_rctf(const View2D *v2d, const rctf *rect_src, rctf *rect_dst) ATTR_NONNULL()
Definition view2d.cc:1657
void view2d_view_to_region_m4(const View2D *v2d, float matrix[4][4]) ATTR_NONNULL()
Definition view2d.cc:1789
static void view2d_curRect_validate_resize(View2D *v2d, bool resize)
Definition view2d.cc:388
static void view2d_map_cur_using_mask(const View2D *v2d, rctf *r_curmasked)
Definition view2d.cc:1049
@ V2D_COMMONVIEW_LIST
Definition UI_view2d.hh:60
@ V2D_COMMONVIEW_STANDARD
Definition UI_view2d.hh:58
@ V2D_COMMONVIEW_STACK
Definition UI_view2d.hh:62
@ V2D_COMMONVIEW_HEADER
Definition UI_view2d.hh:64
@ V2D_COMMONVIEW_PANELS_UI
Definition UI_view2d.hh:66
View2D * view2d_fromcontext(const bContext *C)
Definition view2d.cc:1836
void view2d_totRect_set_resize(View2D *v2d, int width, int height, bool resize)
Definition view2d.cc:962
static MemArena * g_v2d_strings_arena
Definition view2d.cc:2076
char char char view2d_rect_in_scrollers_ex(const ARegion *region, const View2D *v2d, const rcti *rect, int *r_scroll) ATTR_NONNULL(1
Definition view2d.cc:2017
static void grid_axis_start_and_count(const float step, const float min, const float max, float *r_start, int *r_count)
Definition view2d.cc:1252
char char char char view2d_rect_in_scrollers(const ARegion *region, const View2D *v2d, const rcti *rect) ATTR_NONNULL(1
Definition view2d.cc:2050
void view2d_multi_grid_draw(const View2D *v2d, int colorid, float step, int level_size, int totlevels)
Definition view2d.cc:1165
void view2d_scale_get(const View2D *v2d, float *r_x, float *r_y)
Definition view2d.cc:1893
bool view2d_area_supports_sync(ScrArea *area)
Definition view2d.cc:845
void view2d_view_orthoSpecial(ARegion *region, View2D *v2d, bool xaxis)
Definition view2d.cc:1121
void view2d_size_x_set(View2D *v2d, float size_x)
Definition view2d.cc:1947
void view2d_zoom_cache_reset()
Definition view2d.cc:1026
void view2d_offset(View2D *v2d, float xfac, float yfac)
Definition view2d.cc:1954
float view2d_view_to_region_x(const View2D *v2d, float x)
Definition view2d.cc:1673
UserDef U
Definition blender.cc:58
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
ScrArea * CTX_wm_area(const bContext *C)
void wmOrtho2_region_pixelspace(const ARegion *region)
void immEnd()
MemArena * BLI_memarena_new(size_t bufsize, const char *name) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(2) ATTR_MALLOC
unsigned int uint
Definition BKE_text.h:55
void BLI_rctf_recenter(struct rctf *rect, float x, float y)
Definition rct.cc:616
@ V2D_VIEWSYNC_SCREEN_TIME
@ V2D_ZOOM_IGNORE_KEEPOFS
@ V2D_VIEWSYNC_AREA_VERTICAL
void immBeginAtMost(GPUPrimType, uint max_vertex_len)
void GPU_matrix_identity_set()
void immAttrSkip(uint attr_id)
GPUVertFormat * immVertexFormat()
void BLI_rctf_resize(struct rctf *rect, float x, float y)
Definition rct.cc:671
void immBegin(GPUPrimType, uint vertex_len)
MINLINE float fractf(float a)
MINLINE int max_ii(int a, int b)
void BLF_disable(int fontid, FontFlags flag)
Definition blf.cc:331
void immUnbindProgram()
void BLI_rctf_rcti_copy(struct rctf *dst, const struct rcti *src)
MINLINE float pow2f(float x)
void GPU_line_width(float width)
Definition gpu_state.cc:151
Global G
Definition blender.cc:57
void wmOrtho2(float x1, float x2, float y1, float y2)
void immBindBuiltinProgram(GPUBuiltinShader shader_id)
int BLF_set_default()
BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
Definition BLI_rect.hh:207
void immUniform1f(const char *name, float x)
void immVertex2f(uint attr_id, float x, float y)
unsigned char uchar
Definition BKE_text.h:58
int BLF_default()
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
void immUniform4fv(const char *name, const float data[4])
BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
Definition BLI_rect.hh:215
float BLF_height(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:870
void BLI_rcti_translate(struct rcti *rect, int x, int y)
Definition rct.cc:580
void unit_m4(float m[4][4])
void BLF_clipping(int fontid, int xmin, int ymin, int xmax, int ymax)
Definition blf.cc:945
void GPU_program_point_size(bool enable)
Definition gpu_state.cc:165
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
Definition BLI_rect.hh:203
void BLF_color4ubv(int fontid, const unsigned char rgba[4])
Definition blf.cc:454
void immAttr4ub(uint attr_id, unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void * BLI_memarena_alloc(MemArena *ma, size_t size) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_ALLOC_SIZE(2)
void BLF_draw_default(float x, float y, float z, const char *str, size_t str_len) ATTR_NONNULL()
ARegion * BKE_area_find_region_type(const ScrArea *area, int region_type)
Definition screen.cc:1024
@ V2D_SCROLL_VERTICAL_FULLR
@ V2D_SCROLL_HORIZONTAL
@ V2D_SCROLL_VERTICAL_HIDE
@ V2D_SCROLL_HORIZONTAL_HIDE
@ V2D_SCROLL_HORIZONTAL_HANDLES
@ V2D_SCROLL_VERTICAL_HANDLES
@ V2D_SCROLL_HORIZONTAL_FULLR
BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
Definition BLI_rect.hh:211
void BLF_cache_clear()
Definition blf.cc:100
BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
Definition BLI_rect.hh:198
void BLI_rctf_transform_calc_m4_pivot_min(const rctf *dst, const rctf *src, float matrix[4][4])
Definition rct.cc:565
void BLI_rctf_translate(struct rctf *rect, float x, float y)
Definition rct.cc:587
ARegion * CTX_wm_region(const bContext *C)
bool BLI_rctf_isect_segment(const struct rctf *rect, const float s1[2], const float s2[2])
void ED_region_tag_redraw_no_rebuild(ARegion *region)
Definition area.cc:727
BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
Definition BLI_rect.hh:194
void BLF_enable(int fontid, FontFlags flag)
Definition blf.cc:322
@ BLF_CLIPPING
Definition BLF_enums.hh:37
void BLI_memarena_free(MemArena *ma) ATTR_NONNULL(1)
MINLINE float clamp_f(float value, float min, float max)
const btScalar eps
Definition poly34.cpp:11
#define floorf
#define fabsf
#define ceilf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define min(a, b)
Definition sort.cc:38
uiWidgetColors wcol_scroll
unsigned char widget_emboss[4]
ThemeUI tui
void(*) on_view2d_changed(const bContext *C, ARegion *region)
bke::ARegionRuntime * runtime
eRegion_Type regiontype
ListBaseT< ARegion > regionbase
eView2D_KeepTot keeptot
eView2D_KeepOfs keepofs
eView2D_KeepZoom keepzoom
eView2D_Scroll scroll
eView2D_ScrollUI scroll_ui
eView2D_Align align
ListBaseT< struct ScrArea > areabase
struct ARegionType * type
View2DString * next
Definition view2d.cc:2063
union blender::ui::View2DString::@104124004337164152147207341016017141020021230132 col
float panelzoom
unsigned char inner[4]
unsigned char outline[4]
unsigned char item[4]