4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2008 Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation, Joshua Leung
25 * ***** END GPL LICENSE BLOCK *****
32 #include "MEM_guardedalloc.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37 #include "DNA_userdef_types.h"
38 #include "DNA_view2d_types.h"
40 #include "BLI_blenlib.h"
42 #include "BKE_context.h"
43 #include "BKE_global.h"
44 #include "BKE_utildefines.h"
49 #include "BIF_glutil.h"
53 #include "ED_screen.h"
55 #include "UI_interface.h"
56 #include "UI_resources.h"
57 #include "UI_view2d.h"
59 #include "interface_intern.h"
61 /* *********************************************************************** */
63 /* helper to allow scrollbars to dynamically hide */
64 static int view2d_scroll_mapped(int scroll)
66 if(scroll & V2D_SCROLL_HORIZONTAL_HIDE)
67 scroll &= ~(V2D_SCROLL_HORIZONTAL);
68 if(scroll & V2D_SCROLL_VERTICAL_HIDE)
69 scroll &= ~(V2D_SCROLL_VERTICAL);
73 /* called each time cur changes, to dynamically update masks */
74 static void view2d_masks(View2D *v2d)
78 /* mask - view frame */
79 v2d->mask.xmin= v2d->mask.ymin= 0;
80 v2d->mask.xmax= v2d->winx - 1; /* -1 yes! masks are pixels */
81 v2d->mask.ymax= v2d->winy - 1;
84 v2d->scroll &= ~(V2D_SCROLL_HORIZONTAL_HIDE|V2D_SCROLL_VERTICAL_HIDE);
86 if (v2d->scroll & V2D_SCROLL_HORIZONTAL)
87 if(!(v2d->scroll & V2D_SCROLL_SCALE_HORIZONTAL))
88 if (v2d->tot.xmax-v2d->tot.xmin <= v2d->cur.xmax-v2d->cur.xmin)
89 v2d->scroll |= V2D_SCROLL_HORIZONTAL_HIDE;
90 if (v2d->scroll & V2D_SCROLL_VERTICAL)
91 if(!(v2d->scroll & V2D_SCROLL_SCALE_VERTICAL))
92 if (v2d->tot.ymax-v2d->tot.ymin <= v2d->cur.ymax-v2d->cur.ymin)
93 v2d->scroll |= V2D_SCROLL_VERTICAL_HIDE;
95 scroll= view2d_scroll_mapped(v2d->scroll);
97 /* scrollers shrink mask area, but should be based off regionsize
98 * - they can only be on one to two edges of the region they define
99 * - if they overlap, they must not occupy the corners (which are reserved for other widgets)
102 /* vertical scroller */
103 if (scroll & V2D_SCROLL_LEFT) {
104 /* on left-hand edge of region */
105 v2d->vert= v2d->mask;
106 v2d->vert.xmax= V2D_SCROLL_WIDTH;
107 v2d->mask.xmin= v2d->vert.xmax + 1;
109 else if (scroll & V2D_SCROLL_RIGHT) {
110 /* on right-hand edge of region */
111 v2d->vert= v2d->mask;
112 v2d->vert.xmax++; /* one pixel extra... was leaving a minor gap... */
113 v2d->vert.xmin= v2d->vert.xmax - V2D_SCROLL_WIDTH;
114 v2d->mask.xmax= v2d->vert.xmin - 1;
117 /* horizontal scroller */
118 if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
119 /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
121 v2d->hor.ymax= V2D_SCROLL_HEIGHT;
122 v2d->mask.ymin= v2d->hor.ymax + 1;
124 else if (scroll & V2D_SCROLL_TOP) {
125 /* on upper edge of region */
127 v2d->hor.ymin= v2d->hor.ymax - V2D_SCROLL_HEIGHT;
128 v2d->mask.ymax= v2d->hor.ymin - 1;
131 /* adjust vertical scroller if there's a horizontal scroller, to leave corner free */
132 if (scroll & V2D_SCROLL_VERTICAL) {
133 /* just set y min/max for vertical scroller to y min/max of mask as appropriate */
134 if (scroll & (V2D_SCROLL_BOTTOM|V2D_SCROLL_BOTTOM_O)) {
135 /* on bottom edge of region (V2D_SCROLL_BOTTOM_O is outliner, the other is for standard) */
136 v2d->vert.ymin= v2d->mask.ymin;
138 else if (scroll & V2D_SCROLL_TOP) {
139 /* on upper edge of region */
140 v2d->vert.ymax= v2d->mask.ymax;
147 /* Refresh and Validation */
149 /* Initialise all relevant View2D data (including view rects if first time) and/or refresh mask sizes after view resize
150 * - for some of these presets, it is expected that the region will have defined some
151 * additional settings necessary for the customisation of the 2D viewport to its requirements
152 * - this function should only be called from region init() callbacks, where it is expected that
153 * this is called before UI_view2d_size_update(), as this one checks that the rects are properly initialised.
155 void UI_view2d_region_reinit(View2D *v2d, short type, int winx, int winy)
157 short tot_changed= 0, init= 0;
158 uiStyle *style= U.uistyles.first;
160 /* initialise data if there is a need for such */
161 if ((v2d->flag & V2D_IS_INITIALISED) == 0) {
162 /* set initialised flag so that View2D doesn't get reinitialised next time again */
163 v2d->flag |= V2D_IS_INITIALISED;
167 /* see eView2D_CommonViewTypes in UI_view2d.h for available view presets */
169 /* 'standard view' - optimum setup for 'standard' view behaviour, that should be used new views as basis for their
170 * own unique View2D settings, which should be used instead of this in most cases...
172 case V2D_COMMONVIEW_STANDARD:
174 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
175 v2d->keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM);
177 v2d->maxzoom= 1000.0f;
179 /* tot rect and cur should be same size, and aligned using 'standard' OpenGL coordinates for now
180 * - region can resize 'tot' later to fit other data
181 * - keeptot is only within bounds, as strict locking is not that critical
182 * - view is aligned for (0,0) -> (winx-1, winy-1) setup
184 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
185 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
187 v2d->tot.xmin= v2d->tot.ymin= 0.0f;
188 v2d->tot.xmax= (float)(winx - 1);
189 v2d->tot.ymax= (float)(winy - 1);
193 /* scrollers - should we have these by default? */
194 // XXX for now, we don't override this, or set it either!
198 /* 'list/channel view' - zoom, aspect ratio, and alignment restrictions are set here */
199 case V2D_COMMONVIEW_LIST:
201 /* zoom + aspect ratio are locked */
202 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
203 v2d->minzoom= v2d->maxzoom= 1.0f;
205 /* tot rect has strictly regulated placement, and must only occur in +/- quadrant */
206 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
207 v2d->keeptot = V2D_KEEPTOT_STRICT;
210 /* scroller settings are currently not set here... that is left for regions... */
214 /* 'stack view' - practically the same as list/channel view, except is located in the pos y half instead.
215 * zoom, aspect ratio, and alignment restrictions are set here */
216 case V2D_COMMONVIEW_STACK:
218 /* zoom + aspect ratio are locked */
219 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
220 v2d->minzoom= v2d->maxzoom= 1.0f;
222 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
223 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
224 v2d->keeptot = V2D_KEEPTOT_STRICT;
227 /* scroller settings are currently not set here... that is left for regions... */
231 /* 'header' regions - zoom, aspect ratio, alignment, and panning restrictions are set here */
232 case V2D_COMMONVIEW_HEADER:
234 /* zoom + aspect ratio are locked */
235 v2d->keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_LIMITZOOM|V2D_KEEPASPECT);
236 v2d->minzoom= v2d->maxzoom= 1.0f;
237 v2d->min[0]= v2d->max[0]= (float)(winx-1);
238 v2d->min[1]= v2d->max[1]= (float)(winy-1);
240 /* tot rect has strictly regulated placement, and must only occur in +/+ quadrant */
241 v2d->align = (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_NEG_Y);
242 v2d->keeptot = V2D_KEEPTOT_STRICT;
245 /* panning in y-axis is prohibited */
246 v2d->keepofs= V2D_LOCKOFS_Y;
248 /* absolutely no scrollers allowed */
251 /* pixel offsets need to be applied for smooth UI controls */
252 v2d->flag |= (V2D_PIXELOFS_X|V2D_PIXELOFS_Y);
256 /* panels view, with horizontal/vertical align */
257 case V2D_COMMONVIEW_PANELS_UI:
259 float panelzoom= (style) ? style->panelzoom : 1.0f;
261 /* for now, aspect ratio should be maintained, and zoom is clamped within sane default limits */
262 v2d->keepzoom= (V2D_KEEPASPECT|V2D_LIMITZOOM|V2D_KEEPZOOM);
267 v2d->align= (V2D_ALIGN_NO_NEG_X|V2D_ALIGN_NO_POS_Y);
268 v2d->keeptot= V2D_KEEPTOT_BOUNDS;
270 v2d->scroll |= (V2D_SCROLL_RIGHT|V2D_SCROLL_BOTTOM);
276 v2d->tot.ymin= -winy;
279 /* bad workaround for keeping zoom level with scrollers */
280 v2d->cur.xmax= (winx - V2D_SCROLL_WIDTH)*panelzoom;
283 /* bad workaround for keeping zoom level with scrollers */
284 v2d->cur.ymin= (-winy + V2D_SCROLL_HEIGHT)*panelzoom;
288 /* other view types are completely defined using their own settings already */
290 /* we don't do anything here, as settings should be fine, but just make sure that rect */
295 /* store view size */
302 /* set 'tot' rect before setting cur? */
304 UI_view2d_totRect_set_resize(v2d, winx, winy, !init);
306 UI_view2d_curRect_validate_resize(v2d, !init);
309 /* Ensure View2D rects remain in a viable configuration
310 * - cur is not allowed to be: larger than max, smaller than min, or outside of tot
312 // XXX pre2.5 -> this used to be called test_view2d()
313 void UI_view2d_curRect_validate_resize(View2D *v2d, int resize)
315 float totwidth, totheight, curwidth, curheight, width, height;
319 /* use mask as size of region that View2D resides in, as it takes into account scrollbars already */
320 winx= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
321 winy= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
323 /* get pointers to rcts for less typing */
327 /* we must satisfy the following constraints (in decreasing order of importance):
328 * - alignment restrictions are respected
329 * - cur must not fall outside of tot
330 * - axis locks (zoom and offset) must be maintained
331 * - zoom must not be excessive (check either sizes or zoom values)
332 * - aspect ratio should be respected (NOTE: this is quite closely realted to zoom too)
335 /* Step 1: if keepzoom, adjust the sizes of the rects only
336 * - firstly, we calculate the sizes of the rects
337 * - curwidth and curheight are saved as reference... modify width and height values here
339 totwidth= tot->xmax - tot->xmin;
340 totheight= tot->ymax - tot->ymin;
341 curwidth= width= cur->xmax - cur->xmin;
342 curheight= height= cur->ymax - cur->ymin;
344 /* if zoom is locked, size on the appropriate axis is reset to mask size */
345 if (v2d->keepzoom & V2D_LOCKZOOM_X)
347 if (v2d->keepzoom & V2D_LOCKZOOM_Y)
350 /* values used to divide, so make it safe */
351 if(width<1) width= 1;
352 if(height<1) height= 1;
356 /* V2D_LIMITZOOM indicates that zoom level should be preserved when the window size changes */
357 if (resize && (v2d->keepzoom & V2D_KEEPZOOM)) {
360 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
362 oldzoom= v2d->oldwinx / curwidth;
365 width *= zoom/oldzoom;
368 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
370 oldzoom= v2d->oldwiny / curheight;
373 height *= zoom/oldzoom;
376 /* keepzoom (V2D_LIMITZOOM set), indicates that zoom level on each axis must not exceed limits
377 * NOTE: in general, it is not expected that the lock-zoom will be used in conjunction with this
379 else if (v2d->keepzoom & V2D_LIMITZOOM) {
382 /* check if excessive zoom on x-axis */
383 if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
385 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
386 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
391 /* check if excessive zoom on y-axis */
392 if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
394 if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
395 fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
401 /* make sure sizes don't exceed that of the min/max sizes (even though we're not doing zoom clamping) */
402 CLAMP(width, v2d->min[0], v2d->max[0]);
403 CLAMP(height, v2d->min[1], v2d->max[1]);
406 /* check if we should restore aspect ratio (if view size changed) */
407 if (v2d->keepzoom & V2D_KEEPASPECT) {
408 short do_x=0, do_y=0, do_cur, do_win;
409 float curRatio, winRatio;
411 /* when a window edge changes, the aspect ratio can't be used to
412 * find which is the best new 'cur' rect. thats why it stores 'old'
414 if (winx != v2d->oldwinx) do_x= 1;
415 if (winy != v2d->oldwiny) do_y= 1;
417 curRatio= height / width;
418 winRatio= winy / winx;
420 /* both sizes change (area/region maximised) */
423 /* here is 1,1 case, so all others must be 0,0 */
424 if (ABS(winx - v2d->oldwinx) > ABS(winy - v2d->oldwiny)) do_y= 0;
427 else if (winRatio > 1.0f) do_x= 0;
434 if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winx != v2d->oldwinx)) {
435 /* special exception for Outliner (and later channel-lists):
436 * - The view may be moved left to avoid contents being pushed out of view when view shrinks.
437 * - The keeptot code will make sure cur->xmin will not be less than tot->xmin (which cannot be allowed)
438 * - width is not adjusted for changed ratios here...
440 if (winx < v2d->oldwinx) {
441 float temp = v2d->oldwinx - winx;
446 /* width does not get modified, as keepaspect here is just set to make
447 * sure visible area adjusts to changing view shape!
452 /* portrait window: correct for x */
453 width= height / winRatio;
457 if ((v2d->keeptot == V2D_KEEPTOT_STRICT) && (winy != v2d->oldwiny)) {
458 /* special exception for Outliner (and later channel-lists):
459 * - Currently, no actions need to be taken here...
462 if (winy < v2d->oldwiny) {
463 float temp = v2d->oldwiny - winy;
471 /* landscape window: correct for y */
472 height = width * winRatio;
476 /* store region size for next time */
477 v2d->oldwinx= (short)winx;
478 v2d->oldwiny= (short)winy;
481 /* Step 2: apply new sizes to cur rect, but need to take into account alignment settings here... */
482 if ((width != curwidth) || (height != curheight)) {
485 /* resize from centerpoint, unless otherwise specified */
486 if (width != curwidth) {
487 if (v2d->keepofs & V2D_LOCKOFS_X) {
488 cur->xmax += width - (cur->xmax - cur->xmin);
490 else if (v2d->keepofs & V2D_KEEPOFS_X) {
491 if(v2d->align & V2D_ALIGN_NO_POS_X)
492 cur->xmin -= width - (cur->xmax - cur->xmin);
494 cur->xmax += width - (cur->xmax - cur->xmin);
497 temp= (cur->xmax + cur->xmin) * 0.5f;
500 cur->xmin = temp - dh;
501 cur->xmax = temp + dh;
504 if (height != curheight) {
505 if (v2d->keepofs & V2D_LOCKOFS_Y) {
506 cur->ymax += height - (cur->ymax - cur->ymin);
508 else if (v2d->keepofs & V2D_KEEPOFS_Y) {
509 if(v2d->align & V2D_ALIGN_NO_POS_Y)
510 cur->ymin -= height - (cur->ymax - cur->ymin);
512 cur->ymax += height - (cur->ymax - cur->ymin);
515 temp= (cur->ymax + cur->ymin) * 0.5f;
518 cur->ymin = temp - dh;
519 cur->ymax = temp + dh;
524 /* Step 3: adjust so that it doesn't fall outside of bounds of 'tot' */
528 /* recalculate extents of cur */
529 curwidth= cur->xmax - cur->xmin;
530 curheight= cur->ymax - cur->ymin;
533 if ( (curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_X|V2D_LIMITZOOM)) ) {
534 /* if zoom doesn't have to be maintained, just clamp edges */
535 if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
536 if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
538 else if (v2d->keeptot == V2D_KEEPTOT_STRICT) {
539 /* This is an exception for the outliner (and later channel-lists, headers)
540 * - must clamp within tot rect (absolutely no excuses)
541 * --> therefore, cur->xmin must not be less than tot->xmin
543 if (cur->xmin < tot->xmin) {
544 /* move cur across so that it sits at minimum of tot */
545 temp= tot->xmin - cur->xmin;
550 else if (cur->xmax > tot->xmax) {
551 /* - only offset by difference of cur-xmax and tot-xmax if that would not move
552 * cur-xmin to lie past tot-xmin
553 * - otherwise, simply shift to tot-xmin???
555 temp= cur->xmax - tot->xmax;
557 if ((cur->xmin - temp) < tot->xmin) {
558 /* only offset by difference from cur-min and tot-min */
559 temp= cur->xmin - tot->xmin;
571 /* This here occurs when:
572 * - width too big, but maintaining zoom (i.e. widths cannot be changed)
573 * - width is OK, but need to check if outside of boundaries
575 * So, resolution is to just shift view by the gap between the extremities.
576 * We favour moving the 'minimum' across, as that's origin for most things
577 * (XXX - in the past, max was favoured... if there are bugs, swap!)
579 if ((cur->xmin < tot->xmin) && (cur->xmax > tot->xmax)) {
580 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
581 temp= (tot->xmax + tot->xmin) * 0.5f;
582 diff= curheight * 0.5f;
584 cur->xmin= temp - diff;
585 cur->xmax= temp + diff;
587 else if (cur->xmin < tot->xmin) {
588 /* move cur across so that it sits at minimum of tot */
589 temp= tot->xmin - cur->xmin;
594 else if (cur->xmax > tot->xmax) {
595 /* - only offset by difference of cur-xmax and tot-xmax if that would not move
596 * cur-xmin to lie past tot-xmin
597 * - otherwise, simply shift to tot-xmin???
599 temp= cur->xmax - tot->xmax;
601 if ((cur->xmin - temp) < tot->xmin) {
602 /* only offset by difference from cur-min and tot-min */
603 temp= cur->xmin - tot->xmin;
616 if ( (curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_Y|V2D_LIMITZOOM)) ) {
617 /* if zoom doesn't have to be maintained, just clamp edges */
618 if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
619 if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
622 /* This here occurs when:
623 * - height too big, but maintaining zoom (i.e. heights cannot be changed)
624 * - height is OK, but need to check if outside of boundaries
626 * So, resolution is to just shift view by the gap between the extremities.
627 * We favour moving the 'minimum' across, as that's origin for most things
629 if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
630 /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
631 temp= (tot->ymax + tot->ymin) * 0.5f;
632 diff= curheight * 0.5f;
634 cur->ymin= temp - diff;
635 cur->ymax= temp + diff;
637 else if (cur->ymin < tot->ymin) {
638 /* there's still space remaining, so shift up */
639 temp= tot->ymin - cur->ymin;
644 else if (cur->ymax > tot->ymax) {
645 /* there's still space remaining, so shift down */
646 temp= cur->ymax - tot->ymax;
654 /* Step 4: Make sure alignment restrictions are respected */
656 /* If alignment flags are set (but keeptot is not), they must still be respected, as although
657 * they don't specify any particular bounds to stay within, they do define ranges which are
660 * Here, we only check to make sure that on each axis, the 'cur' rect doesn't stray into these
661 * invalid zones, otherwise we offset.
664 /* handle width - posx and negx flags are mutually exclusive, so watch out */
665 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
666 /* width is in negative-x half */
667 if (v2d->cur.xmax > 0) {
668 v2d->cur.xmin -= v2d->cur.xmax;
672 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
673 /* width is in positive-x half */
674 if (v2d->cur.xmin < 0) {
675 v2d->cur.xmax -= v2d->cur.xmin;
676 v2d->cur.xmin = 0.0f;
680 /* handle height - posx and negx flags are mutually exclusive, so watch out */
681 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
682 /* height is in negative-y half */
683 if (v2d->cur.ymax > 0) {
684 v2d->cur.ymin -= v2d->cur.ymax;
685 v2d->cur.ymax = 0.0f;
688 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
689 /* height is in positive-y half */
690 if (v2d->cur.ymin < 0) {
691 v2d->cur.ymax -= v2d->cur.ymin;
692 v2d->cur.ymin = 0.0f;
701 void UI_view2d_curRect_validate(View2D *v2d)
703 return UI_view2d_curRect_validate_resize(v2d, 0);
706 /* ------------------ */
708 /* Called by menus to activate it, or by view2d operators to make sure 'related' views stay in synchrony */
709 void UI_view2d_sync(bScreen *screen, ScrArea *area, View2D *v2dcur, int flag)
714 /* don't continue if no view syncing to be done */
715 if ((v2dcur->flag & (V2D_VIEWSYNC_SCREEN_TIME|V2D_VIEWSYNC_AREA_VERTICAL)) == 0)
718 /* check if doing within area syncing (i.e. channels/vertical) */
719 if ((v2dcur->flag & V2D_VIEWSYNC_AREA_VERTICAL) && (area)) {
720 for (ar= area->regionbase.first; ar; ar= ar->next) {
721 /* don't operate on self */
722 if (v2dcur != &ar->v2d) {
723 /* only if view has vertical locks enabled */
724 if (ar->v2d.flag & V2D_VIEWSYNC_AREA_VERTICAL) {
725 if (flag == V2D_LOCK_COPY) {
726 /* other views with locks on must copy active */
727 ar->v2d.cur.ymin= v2dcur->cur.ymin;
728 ar->v2d.cur.ymax= v2dcur->cur.ymax;
730 else { /* V2D_LOCK_SET */
731 /* active must copy others */
732 v2dcur->cur.ymin= ar->v2d.cur.ymin;
733 v2dcur->cur.ymax= ar->v2d.cur.ymax;
736 /* region possibly changed, so refresh */
737 ED_region_tag_redraw(ar);
743 /* check if doing whole screen syncing (i.e. time/horizontal) */
744 if ((v2dcur->flag & V2D_VIEWSYNC_SCREEN_TIME) && (screen)) {
745 for (sa= screen->areabase.first; sa; sa= sa->next) {
746 for (ar= sa->regionbase.first; ar; ar= ar->next) {
747 /* don't operate on self */
748 if (v2dcur != &ar->v2d) {
749 /* only if view has horizontal locks enabled */
750 if (ar->v2d.flag & V2D_VIEWSYNC_SCREEN_TIME) {
751 if (flag == V2D_LOCK_COPY) {
752 /* other views with locks on must copy active */
753 ar->v2d.cur.xmin= v2dcur->cur.xmin;
754 ar->v2d.cur.xmax= v2dcur->cur.xmax;
756 else { /* V2D_LOCK_SET */
757 /* active must copy others */
758 v2dcur->cur.xmin= ar->v2d.cur.xmin;
759 v2dcur->cur.xmax= ar->v2d.cur.xmax;
762 /* region possibly changed, so refresh */
763 ED_region_tag_redraw(ar);
772 /* Restore 'cur' rect to standard orientation (i.e. optimal maximum view of tot)
773 * This does not take into account if zooming the view on an axis will improve the view (if allowed)
775 void UI_view2d_curRect_reset (View2D *v2d)
779 /* assume width and height of 'cur' rect by default, should be same size as mask */
780 width= (float)(v2d->mask.xmax - v2d->mask.xmin + 1);
781 height= (float)(v2d->mask.ymax - v2d->mask.ymin + 1);
783 /* handle width - posx and negx flags are mutually exclusive, so watch out */
784 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
785 /* width is in negative-x half */
786 v2d->cur.xmin= (float)-width;
789 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
790 /* width is in positive-x half */
792 v2d->cur.xmax= (float)width;
795 /* width is centered around x==0 */
796 const float dx= (float)width / 2.0f;
802 /* handle height - posx and negx flags are mutually exclusive, so watch out */
803 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
804 /* height is in negative-y half */
805 v2d->cur.ymin= (float)-height;
808 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
809 /* height is in positive-y half */
811 v2d->cur.ymax= (float)height;
814 /* height is centered around y==0 */
815 const float dy= (float)height / 2.0f;
822 /* ------------------ */
824 /* Change the size of the maximum viewable area (i.e. 'tot' rect) */
825 void UI_view2d_totRect_set_resize (View2D *v2d, int width, int height, int resize)
827 int scroll= view2d_scroll_mapped(v2d->scroll);
829 /* don't do anything if either value is 0 */
834 if(scroll & V2D_SCROLL_HORIZONTAL)
835 width -= V2D_SCROLL_WIDTH;
836 if(scroll & V2D_SCROLL_VERTICAL)
837 height -= V2D_SCROLL_HEIGHT;
839 if (ELEM3(0, v2d, width, height)) {
840 printf("Error: View2D totRect set exiting: v2d=%p width=%d height=%d \n", v2d, width, height); // XXX temp debug info
844 /* handle width - posx and negx flags are mutually exclusive, so watch out */
845 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
846 /* width is in negative-x half */
847 v2d->tot.xmin= (float)-width;
850 else if ((v2d->align & V2D_ALIGN_NO_NEG_X) && !(v2d->align & V2D_ALIGN_NO_POS_X)) {
851 /* width is in positive-x half */
853 v2d->tot.xmax= (float)width;
856 /* width is centered around x==0 */
857 const float dx= (float)width / 2.0f;
863 /* handle height - posx and negx flags are mutually exclusive, so watch out */
864 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
865 /* height is in negative-y half */
866 v2d->tot.ymin= (float)-height;
869 else if ((v2d->align & V2D_ALIGN_NO_NEG_Y) && !(v2d->align & V2D_ALIGN_NO_POS_Y)) {
870 /* height is in positive-y half */
872 v2d->tot.ymax= (float)height;
875 /* height is centered around y==0 */
876 const float dy= (float)height / 2.0f;
882 /* make sure that 'cur' rect is in a valid state as a result of these changes */
883 UI_view2d_curRect_validate_resize(v2d, resize);
886 void UI_view2d_totRect_set(View2D *v2d, int width, int height)
888 UI_view2d_totRect_set_resize(v2d, width, height, 0);
891 int UI_view2d_tab_set(View2D *v2d, int tab)
893 float default_offset[2]= {0.0f, 0.0f};
894 float *offset, *new_offset;
897 /* if tab changed, change offset */
898 if(tab != v2d->tab_cur && v2d->tab_offset) {
899 if(tab < v2d->tab_num)
900 offset= &v2d->tab_offset[tab*2];
902 offset= default_offset;
904 v2d->cur.xmax += offset[0] - v2d->cur.xmin;
905 v2d->cur.xmin= offset[0];
907 v2d->cur.ymin += offset[1] - v2d->cur.ymax;
908 v2d->cur.ymax= offset[1];
910 /* validation should happen in subsequent totRect_set */
915 /* resize array if needed */
916 if(tab >= v2d->tab_num) {
917 new_offset= MEM_callocN(sizeof(float)*(tab+1)*2, "view2d tab offset");
919 if(v2d->tab_offset) {
920 memcpy(new_offset, v2d->tab_offset, sizeof(float)*v2d->tab_num*2);
921 MEM_freeN(v2d->tab_offset);
924 v2d->tab_offset= new_offset;
928 /* set current tab and offset */
930 v2d->tab_offset[2*tab+0]= v2d->cur.xmin;
931 v2d->tab_offset[2*tab+1]= v2d->cur.ymax;
936 /* *********************************************************************** */
937 /* View Matrix Setup */
939 /* mapping function to ensure 'cur' draws extended over the area where sliders are */
940 static void view2d_map_cur_using_mask(View2D *v2d, rctf *curmasked)
942 *curmasked= v2d->cur;
944 if (view2d_scroll_mapped(v2d->scroll)) {
945 float dx= (v2d->cur.xmax-v2d->cur.xmin)/((float)(v2d->mask.xmax-v2d->mask.xmin+1));
946 float dy= (v2d->cur.ymax-v2d->cur.ymin)/((float)(v2d->mask.ymax-v2d->mask.ymin+1));
948 if (v2d->mask.xmin != 0)
949 curmasked->xmin -= dx*(float)v2d->mask.xmin;
950 if (v2d->mask.xmax+1 != v2d->winx)
951 curmasked->xmax += dx*(float)(v2d->winx - v2d->mask.xmax-1);
953 if (v2d->mask.ymin != 0)
954 curmasked->ymin -= dy*(float)v2d->mask.ymin;
955 if (v2d->mask.ymax+1 != v2d->winy)
956 curmasked->ymax += dy*(float)(v2d->winy - v2d->mask.ymax-1);
961 /* Set view matrices to use 'cur' rect as viewing frame for View2D drawing */
962 void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
967 /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing,
968 * but only applied where requsted
970 /* XXX ton: fix this! */
971 xofs= 0.0; // (v2d->flag & V2D_PIXELOFS_X) ? 0.375f : 0.0f;
972 yofs= 0.0; // (v2d->flag & V2D_PIXELOFS_Y) ? 0.375f : 0.0f;
974 /* XXX brecht: instead of zero at least use a tiny offset, otherwise
975 * pixel rounding is effectively random due to float inaccuracy */
979 /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
980 view2d_map_cur_using_mask(v2d, &curmasked);
982 /* set matrix on all appropriate axes */
983 wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
985 /* XXX is this necessary? */
989 /* Set view matrices to only use one axis of 'cur' only
990 * - xaxis = if non-zero, only use cur x-axis, otherwise use cur-yaxis (mostly this will be used for x)
992 void UI_view2d_view_orthoSpecial(const bContext *C, View2D *v2d, short xaxis)
994 ARegion *ar= CTX_wm_region(C);
998 /* pixel offsets (-0.375f) are needed to get 1:1 correspondance with pixels for smooth UI drawing,
999 * but only applied where requsted
1001 /* XXX temp (ton) */
1002 xofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_X) ? 0.375f : 0.0f;
1003 yofs= 0.0f; // (v2d->flag & V2D_PIXELOFS_Y) ? 0.375f : 0.0f;
1005 /* apply mask-based adjustments to cur rect (due to scrollers), to eliminate scaling artifacts */
1006 view2d_map_cur_using_mask(v2d, &curmasked);
1008 /* only set matrix with 'cur' coordinates on relevant axes */
1010 wmOrtho2(curmasked.xmin-xofs, curmasked.xmax-xofs, -yofs, ar->winy-yofs);
1012 wmOrtho2(-xofs, ar->winx-xofs, curmasked.ymin-yofs, curmasked.ymax-yofs);
1014 /* XXX is this necessary? */
1019 /* Restore view matrices after drawing */
1020 void UI_view2d_view_restore(const bContext *C)
1022 ARegion *ar= CTX_wm_region(C);
1023 int width= ar->winrct.xmax-ar->winrct.xmin+1;
1024 int height= ar->winrct.ymax-ar->winrct.ymin+1;
1026 wmOrtho2(0.0f, (float)width, 0.0f, (float)height);
1029 // ED_region_pixelspace(CTX_wm_region(C));
1032 /* *********************************************************************** */
1035 /* minimum pixels per gridstep */
1036 #define MINGRIDSTEP 35
1038 /* View2DGrid is typedef'd in UI_view2d.h */
1040 float dx, dy; /* stepsize (in pixels) between gridlines */
1041 float startx, starty; /* initial coordinates to start drawing grid from */
1042 int powerx, powery; /* step as power of 10 */
1045 /* --------------- */
1047 /* try to write step as a power of 10 */
1048 static void step_to_grid(float *step, int *power, int unit)
1050 const float loga= (float)log10(*step);
1053 *power= (int)(loga);
1055 rem= loga - (*power);
1056 rem= (float)pow(10.0, rem);
1059 if (rem < 0.2f) rem= 0.2f;
1060 else if(rem < 0.5f) rem= 0.5f;
1063 *step= rem * (float)pow(10.0, (*power));
1065 /* for frames, we want 1.0 frame intervals only */
1066 if (unit == V2D_UNIT_FRAMES) {
1071 /* prevents printing 1.0 2.0 3.0 etc */
1072 if (rem == 1.0f) (*power)++;
1075 if (rem < 2.0f) rem= 2.0f;
1076 else if(rem < 5.0f) rem= 5.0f;
1079 *step= rem * (float)pow(10.0, (*power));
1082 /* prevents printing 1.0, 2.0, 3.0, etc. */
1083 if (rem == 10.0f) (*power)++;
1087 /* Intialise settings necessary for drawing gridlines in a 2d-view
1088 * - Currently, will return pointer to View2DGrid struct that needs to
1089 * be freed with UI_view2d_grid_free()
1090 * - Is used for scrollbar drawing too (for units drawing)
1091 * - Units + clamping args will be checked, to make sure they are valid values that can be used
1092 * so it is very possible that we won't return grid at all!
1094 * - xunits,yunits = V2D_UNIT_* grid steps in seconds or frames
1095 * - xclamp,yclamp = V2D_CLAMP_* only show whole-number intervals
1096 * - winx = width of region we're drawing to
1097 * - winy = height of region we're drawing into
1099 View2DGrid *UI_view2d_grid_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp, int winx, int winy)
1101 Scene *scene= CTX_data_scene(C);
1103 float space, pixels, seconddiv;
1106 /* check that there are at least some workable args */
1107 if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) && ELEM(V2D_ARG_DUMMY, yunits, yclamp))
1110 /* grid here is allocated... */
1111 grid= MEM_callocN(sizeof(View2DGrid), "View2DGrid");
1113 /* rule: gridstep is minimal GRIDSTEP pixels */
1114 if (xunits == V2D_UNIT_SECONDS) {
1116 seconddiv= (float)(0.01 * FPS);
1123 /* calculate x-axis grid scale (only if both args are valid) */
1124 if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1125 space= v2d->cur.xmax - v2d->cur.xmin;
1126 pixels= (float)(v2d->mask.xmax - v2d->mask.xmin);
1128 grid->dx= (MINGRIDSTEP * space) / (seconddiv * pixels);
1129 step_to_grid(&grid->dx, &grid->powerx, xunits);
1130 grid->dx *= seconddiv;
1132 if (xclamp == V2D_GRID_CLAMP) {
1133 if (grid->dx < 0.1f) grid->dx= 0.1f;
1135 if (grid->powerx < -2) grid->powerx= -2;
1139 /* calculate y-axis grid scale (only if both args are valid) */
1140 if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1141 space= v2d->cur.ymax - v2d->cur.ymin;
1142 pixels= (float)winy;
1144 grid->dy= MINGRIDSTEP * space / pixels;
1145 step_to_grid(&grid->dy, &grid->powery, yunits);
1147 if (yclamp == V2D_GRID_CLAMP) {
1148 if (grid->dy < 1.0f) grid->dy= 1.0f;
1149 if (grid->powery < 1) grid->powery= 1;
1153 /* calculate start position */
1154 if (ELEM(V2D_ARG_DUMMY, xunits, xclamp) == 0) {
1155 grid->startx= seconddiv*(v2d->cur.xmin/seconddiv - (float)fmod(v2d->cur.xmin/seconddiv, grid->dx/seconddiv));
1156 if (v2d->cur.xmin < 0.0f) grid->startx-= grid->dx;
1159 grid->startx= v2d->cur.xmin;
1161 if (ELEM(V2D_ARG_DUMMY, yunits, yclamp) == 0) {
1162 grid->starty= (v2d->cur.ymin - (float)fmod(v2d->cur.ymin, grid->dy));
1163 if (v2d->cur.ymin < 0.0f) grid->starty-= grid->dy;
1166 grid->starty= v2d->cur.ymin;
1171 /* Draw gridlines in the given 2d-region */
1172 void UI_view2d_grid_draw(const bContext *C, View2D *v2d, View2DGrid *grid, int flag)
1174 float vec1[2], vec2[2];
1177 /* check for grid first, as it may not exist */
1181 /* vertical lines */
1182 if (flag & V2D_VERTICAL_LINES) {
1183 /* initialise initial settings */
1184 vec1[0]= vec2[0]= grid->startx;
1185 vec1[1]= grid->starty;
1186 vec2[1]= v2d->cur.ymax;
1188 /* minor gridlines */
1189 step= (v2d->mask.xmax - v2d->mask.xmin + 1) / MINGRIDSTEP;
1190 UI_ThemeColor(TH_GRID);
1192 for (a=0; a<step; a++) {
1193 glBegin(GL_LINE_STRIP);
1198 vec2[0]= vec1[0]+= grid->dx;
1201 /* major gridlines */
1202 vec2[0]= vec1[0]-= 0.5f*grid->dx;
1203 UI_ThemeColorShade(TH_GRID, 16);
1206 for (a=0; a<=step; a++) {
1207 glBegin(GL_LINE_STRIP);
1212 vec2[0]= vec1[0]-= grid->dx;
1216 /* horizontal lines */
1217 if (flag & V2D_HORIZONTAL_LINES) {
1218 /* only major gridlines */
1219 vec1[1]= vec2[1]= grid->starty;
1220 vec1[0]= grid->startx;
1221 vec2[0]= v2d->cur.xmax;
1223 step= (v2d->mask.ymax - v2d->mask.ymin + 1) / MINGRIDSTEP;
1225 UI_ThemeColor(TH_GRID);
1226 for (a=0; a<=step; a++) {
1227 glBegin(GL_LINE_STRIP);
1232 vec2[1]= vec1[1]+= grid->dy;
1235 /* fine grid lines */
1236 vec2[1]= vec1[1]-= 0.5f*grid->dy;
1239 if (flag & V2D_HORIZONTAL_FINELINES) {
1240 UI_ThemeColorShade(TH_GRID, 16);
1241 for (a=0; a<step; a++) {
1242 glBegin(GL_LINE_STRIP);
1247 vec2[1]= vec1[1]-= grid->dy;
1252 /* Axes are drawn as darker lines */
1253 UI_ThemeColorShade(TH_GRID, -50);
1255 /* horizontal axis */
1256 if (flag & V2D_HORIZONTAL_AXIS) {
1257 vec1[0]= v2d->cur.xmin;
1258 vec2[0]= v2d->cur.xmax;
1259 vec1[1]= vec2[1]= 0.0f;
1261 glBegin(GL_LINE_STRIP);
1268 if (flag & V2D_VERTICAL_AXIS) {
1269 vec1[1]= v2d->cur.ymin;
1270 vec2[1]= v2d->cur.ymax;
1271 vec1[0]= vec2[0]= 0.0f;
1273 glBegin(GL_LINE_STRIP);
1280 /* Draw a constant grid in given 2d-region */
1281 void UI_view2d_constant_grid_draw(const bContext *C, View2D *v2d)
1283 float start, step= 25.0f;
1285 UI_ThemeColorShade(TH_BACK, -10);
1287 start= v2d->cur.xmin - (float)fmod(v2d->cur.xmin, step);
1290 for(; start<v2d->cur.xmax; start+=step) {
1291 glVertex2f(start, v2d->cur.ymin);
1292 glVertex2f(start, v2d->cur.ymax);
1295 start= v2d->cur.ymin - (float)fmod(v2d->cur.ymin, step);
1296 for(; start<v2d->cur.ymax; start+=step) {
1297 glVertex2f(v2d->cur.xmin, start);
1298 glVertex2f(v2d->cur.xmax, start);
1302 UI_ThemeColorShade(TH_BACK, -18);
1303 glVertex2f(0.0f, v2d->cur.ymin);
1304 glVertex2f(0.0f, v2d->cur.ymax);
1305 glVertex2f(v2d->cur.xmin, 0.0f);
1306 glVertex2f(v2d->cur.xmax, 0.0f);
1311 /* free temporary memory used for drawing grid */
1312 void UI_view2d_grid_free(View2DGrid *grid)
1314 /* only free if there's a grid */
1319 /* *********************************************************************** */
1322 /* View2DScrollers is typedef'd in UI_view2d.h
1323 * WARNING: the start of this struct must not change, as view2d_ops.c uses this too.
1324 * For now, we don't need to have a separate (internal) header for structs like this...
1326 struct View2DScrollers {
1328 int vert_min, vert_max; /* vertical scrollbar */
1329 int hor_min, hor_max; /* horizontal scrollbar */
1331 rcti hor, vert; /* exact size of slider backdrop */
1332 int horfull, vertfull; /* set if sliders are full, we don't draw them */
1335 View2DGrid *grid; /* grid for coordinate drawing */
1336 short xunits, xclamp; /* units and clamping options for x-axis */
1337 short yunits, yclamp; /* units and clamping options for y-axis */
1340 /* Calculate relevant scroller properties */
1341 View2DScrollers *UI_view2d_scrollers_calc(const bContext *C, View2D *v2d, short xunits, short xclamp, short yunits, short yclamp)
1343 View2DScrollers *scrollers;
1345 float fac1, fac2, totsize, scrollsize;
1346 int scroll= view2d_scroll_mapped(v2d->scroll);
1348 /* scrollers is allocated here... */
1349 scrollers= MEM_callocN(sizeof(View2DScrollers), "View2DScrollers");
1354 /* slider rects smaller than region */
1357 if (scroll & V2D_SCROLL_BOTTOM)
1362 if (scroll & V2D_SCROLL_LEFT)
1369 /* store in scrollers, used for drawing */
1370 scrollers->vert= vert;
1371 scrollers->hor= hor;
1373 /* scroller 'buttons':
1374 * - These should always remain within the visible region of the scrollbar
1375 * - They represent the region of 'tot' that is visible in 'cur'
1378 /* horizontal scrollers */
1379 if (scroll & V2D_SCROLL_HORIZONTAL) {
1380 /* scroller 'button' extents */
1381 totsize= v2d->tot.xmax - v2d->tot.xmin;
1382 scrollsize= (float)(hor.xmax - hor.xmin);
1384 fac1= (v2d->cur.xmin - v2d->tot.xmin) / totsize;
1386 scrollers->hor_min= hor.xmin;
1388 scrollers->hor_min= (int)(hor.xmin + (fac1 * scrollsize));
1390 fac2= (v2d->cur.xmax - v2d->tot.xmin) / totsize;
1392 scrollers->hor_max= hor.xmax;
1394 scrollers->hor_max= (int)(hor.xmin + (fac2 * scrollsize));
1396 if (scrollers->hor_min > scrollers->hor_max)
1397 scrollers->hor_min= scrollers->hor_max;
1399 /* check whether sliders can disappear */
1401 if(fac1 <= 0.0f && fac2 >= 1.0f)
1402 scrollers->horfull= 1;
1405 /* vertical scrollers */
1406 if (scroll & V2D_SCROLL_VERTICAL) {
1407 /* scroller 'button' extents */
1408 totsize= v2d->tot.ymax - v2d->tot.ymin;
1409 scrollsize= (float)(vert.ymax - vert.ymin);
1411 fac1= (v2d->cur.ymin- v2d->tot.ymin) / totsize;
1413 scrollers->vert_min= vert.ymin;
1415 scrollers->vert_min= (int)(vert.ymin + (fac1 * scrollsize));
1417 fac2= (v2d->cur.ymax - v2d->tot.ymin) / totsize;
1419 scrollers->vert_max= vert.ymax;
1421 scrollers->vert_max= (int)(vert.ymin + (fac2 * scrollsize));
1423 if (scrollers->vert_min > scrollers->vert_max)
1424 scrollers->vert_min= scrollers->vert_max;
1426 /* check whether sliders can disappear */
1428 if(fac1 <= 0.0f && fac2 >= 1.0f)
1429 scrollers->vertfull= 1;
1432 /* grid markings on scrollbars */
1433 if (scroll & (V2D_SCROLL_SCALE_HORIZONTAL|V2D_SCROLL_SCALE_VERTICAL)) {
1434 /* store clamping */
1435 scrollers->xclamp= xclamp;
1436 scrollers->xunits= xunits;
1437 scrollers->yclamp= yclamp;
1438 scrollers->yunits= yunits;
1440 scrollers->grid= UI_view2d_grid_calc(C, v2d, xunits, xclamp, yunits, yclamp, (hor.xmax - hor.xmin), (vert.ymax - vert.ymin));
1443 /* return scrollers */
1447 /* Print scale marking along a time scrollbar */
1448 static void scroll_printstr(View2DScrollers *scrollers, Scene *scene, float x, float y, float val, int power, short unit, char dir)
1453 /* adjust the scale unit to work ok */
1455 /* here we bump up the power by factor of 10, as
1456 * rotation values (hence 'degrees') are divided by 10 to
1457 * be able to show the curves at the same time
1459 if (ELEM(unit, V2D_UNIT_DEGREES, V2D_UNIT_TIME)) {
1465 /* get string to print */
1466 if (unit == V2D_UNIT_SECONDS) {
1468 * - In general, minutes and seconds should be shown, as most clips will be
1469 * within this length. Hours will only be included if relevant.
1470 * - Only show frames when zoomed in enough for them to be relevant
1471 * (using separator of '!' for frames).
1472 * When showing frames, use slightly different display to avoid confusion with mm:ss format
1473 * TODO: factor into reusable function.
1474 * Meanwhile keep in sync:
1475 * source/blender/editors/animation/anim_draw.c
1476 * source/blender/editors/interface/view2d.c
1478 int hours=0, minutes=0, seconds=0, frames=0;
1483 /* correction for negative values */
1489 /* XXX should we only display a single digit for hours since clips are
1490 * VERY UNLIKELY to be more than 1-2 hours max? However, that would
1491 * go against conventions...
1493 hours= (int)val / 3600;
1494 val= (float)fmod(val, 3600);
1498 minutes= (int)val / 60;
1499 val= (float)fmod(val, 60);
1503 * Frames are derived from 'fraction' of second. We need to perform some additional rounding
1504 * to cope with 'half' frames, etc., which should be fine in most cases
1507 frames= (int)floor( ((val - seconds) * FPS) + 0.5f );
1510 /* seconds (with pixel offset) */
1511 seconds= (int)floor(val + 0.375f);
1514 /* print timecode to temp string buffer */
1516 /* include "frames" in display */
1517 if (hours) sprintf(str, "%s%02d:%02d:%02d!%02d", neg, hours, minutes, seconds, frames);
1518 else if (minutes) sprintf(str, "%s%02d:%02d!%02d", neg, minutes, seconds, frames);
1519 else sprintf(str, "%s%d!%02d", neg, seconds, frames);
1522 /* don't include 'frames' in display */
1523 if (hours) sprintf(str, "%s%02d:%02d:%02d", neg, hours, minutes, seconds);
1524 else sprintf(str, "%s%02d:%02d", neg, minutes, seconds);
1528 /* round to whole numbers if power is >= 1 (i.e. scale is coarse) */
1529 if (power <= 0) sprintf(str, "%.*f", 1-power, val);
1530 else sprintf(str, "%d", (int)floor(val + 0.375f));
1533 /* get length of string, and adjust printing location to fit it into the horizontal scrollbar */
1536 /* seconds/timecode display has slightly longer strings... */
1537 if (unit == V2D_UNIT_SECONDS)
1543 /* Add degree sympbol to end of string for vertical scrollbar? */
1544 if ((dir == 'v') && (unit == V2D_UNIT_DEGREES)) {
1550 BLF_draw_default(x, y, 0.0f, str);
1553 /* local defines for scrollers drawing */
1554 /* radius of scroller 'button' caps */
1555 #define V2D_SCROLLCAP_RAD 5
1556 /* shading factor for scroller 'bar' */
1557 #define V2D_SCROLLBAR_SHADE 0.1f
1558 /* shading factor for scroller 'button' caps */
1559 #define V2D_SCROLLCAP_SHADE 0.2f
1561 /* Draw scrollbars in the given 2d-region */
1562 void UI_view2d_scrollers_draw(const bContext *C, View2D *v2d, View2DScrollers *vs)
1564 Scene *scene= CTX_data_scene(C);
1566 int scroll= view2d_scroll_mapped(v2d->scroll);
1568 /* make copies of rects for less typing */
1572 /* horizontal scrollbar */
1573 if (scroll & V2D_SCROLL_HORIZONTAL) {
1575 if(vs->horfull==0) {
1576 bTheme *btheme= U.themes.first;
1577 uiWidgetColors wcol= btheme->tui.wcol_scroll;
1581 slider.xmin= vs->hor_min;
1582 slider.xmax= vs->hor_max;
1583 slider.ymin= hor.ymin;
1584 slider.ymax= hor.ymax;
1586 state= (v2d->scroll_ui & V2D_SCROLL_H_ACTIVE)?UI_SCROLL_PRESSED:0;
1587 if (!(v2d->keepzoom & V2D_LOCKZOOM_X))
1588 state |= UI_SCROLL_ARROWS;
1589 uiWidgetScrollDraw(&wcol, &hor, &slider, state);
1592 /* scale indicators */
1593 // XXX will need to update the font drawing when the new stuff comes in
1594 if ((scroll & V2D_SCROLL_SCALE_HORIZONTAL) && (vs->grid)) {
1595 View2DGrid *grid= vs->grid;
1596 float fac, dfac, fac2, val;
1598 /* the numbers: convert grid->startx and -dx to scroll coordinates
1599 * - fac is x-coordinate to draw to
1600 * - dfac is gap between scale markings
1602 fac= (grid->startx - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1603 fac= (float)hor.xmin + fac*(hor.xmax - hor.xmin);
1605 dfac= (grid->dx) / (v2d->cur.xmax - v2d->cur.xmin);
1606 dfac= dfac * (hor.xmax - hor.xmin);
1608 /* set starting value, and text color */
1609 UI_ThemeColor(TH_TEXT);
1612 /* if we're clamping to whole numbers only, make sure entries won't be repeated */
1613 if (vs->xclamp == V2D_GRID_CLAMP) {
1614 while (grid->dx < 0.9999f) {
1619 if (vs->xunits == V2D_UNIT_FRAMES)
1622 /* draw numbers in the appropriate range */
1624 float h= 2.0f+(float)(hor.ymin);
1626 for (; fac < hor.xmax-10; fac+=dfac, val+=grid->dx) {
1628 /* make prints look nicer for scrollers */
1629 if(fac < hor.xmin+10)
1632 switch (vs->xunits) {
1633 case V2D_UNIT_FRAMES: /* frames (as whole numbers)*/
1634 scroll_printstr(vs, scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMES, 'h');
1637 case V2D_UNIT_FRAMESCALE: /* frames (not always as whole numbers) */
1638 scroll_printstr(vs, scene, fac, h, val, grid->powerx, V2D_UNIT_FRAMESCALE, 'h');
1641 case V2D_UNIT_SECONDS: /* seconds */
1642 fac2= val/(float)FPS;
1643 scroll_printstr(vs, scene, fac, h, fac2, grid->powerx, V2D_UNIT_SECONDS, 'h');
1646 case V2D_UNIT_SECONDSSEQ: /* seconds with special calculations (only used for sequencer only) */
1650 fac2= val/(float)FPS;
1651 time= (float)floor(fac2);
1654 scroll_printstr(vs, scene, fac, h, time+(float)FPS*fac2/100.0f, grid->powerx, V2D_UNIT_SECONDSSEQ, 'h');
1658 case V2D_UNIT_DEGREES: /* Graph Editor for rotation Drivers */
1659 /* HACK: although we're drawing horizontal, we make this draw as 'vertical', just to get degree signs */
1660 scroll_printstr(vs, scene, fac, h, val, grid->powerx, V2D_UNIT_DEGREES, 'v');
1668 /* vertical scrollbar */
1669 if (scroll & V2D_SCROLL_VERTICAL) {
1671 if(vs->vertfull==0) {
1672 bTheme *btheme= U.themes.first;
1673 uiWidgetColors wcol= btheme->tui.wcol_scroll;
1677 slider.xmin= vert.xmin;
1678 slider.xmax= vert.xmax;
1679 slider.ymin= vs->vert_min;
1680 slider.ymax= vs->vert_max;
1682 state= (v2d->scroll_ui & V2D_SCROLL_V_ACTIVE)?UI_SCROLL_PRESSED:0;
1683 if (!(v2d->keepzoom & V2D_LOCKZOOM_Y))
1684 state |= UI_SCROLL_ARROWS;
1685 uiWidgetScrollDraw(&wcol, &vert, &slider, state);
1689 /* scale indiators */
1690 // XXX will need to update the font drawing when the new stuff comes in
1691 if ((scroll & V2D_SCROLL_SCALE_VERTICAL) && (vs->grid)) {
1692 View2DGrid *grid= vs->grid;
1693 float fac, dfac, val;
1695 /* the numbers: convert grid->starty and dy to scroll coordinates
1696 * - fac is y-coordinate to draw to
1697 * - dfac is gap between scale markings
1698 * - these involve a correction for horizontal scrollbar
1699 * NOTE: it's assumed that that scrollbar is there if this is involved!
1701 fac= (grid->starty- v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1702 fac= (vert.ymin + V2D_SCROLL_HEIGHT) + fac*(vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1704 dfac= (grid->dy) / (v2d->cur.ymax - v2d->cur.ymin);
1705 dfac= dfac * (vert.ymax - vert.ymin - V2D_SCROLL_HEIGHT);
1707 /* set starting value, and text color */
1708 UI_ThemeColor(TH_TEXT);
1711 /* if vertical clamping (to whole numbers) is used (i.e. in Sequencer), apply correction */
1712 // XXX only relevant to Sequencer, so need to review this when we port that code
1713 if (vs->yclamp == V2D_GRID_CLAMP)
1716 /* draw vertical steps */
1719 BLF_default_rotation(90.0f);
1721 for (; fac < vert.ymax-10; fac+= dfac, val += grid->dy) {
1723 /* make prints look nicer for scrollers */
1724 if(fac < vert.ymin+10)
1727 scroll_printstr(vs, scene, (float)(vert.xmax)-2.0f, fac, val, grid->powery, vs->yunits, 'v');
1730 BLF_default_rotation(0.0f);
1737 /* free temporary memory used for drawing scrollers */
1738 void UI_view2d_scrollers_free(View2DScrollers *scrollers)
1740 /* need to free grid as well... */
1741 if (scrollers->grid) MEM_freeN(scrollers->grid);
1742 MEM_freeN(scrollers);
1745 /* *********************************************************************** */
1746 /* List View Utilities */
1748 /* Get the view-coordinates of the nominated cell
1749 * - columnwidth, rowheight = size of each 'cell'
1750 * - startx, starty = coordinates (in 'tot' rect space) that the list starts from
1751 * This should be (0,0) for most views. However, for those where the starting row was offsetted
1752 * (like for Animation Editor channel lists, to make the first entry more visible), these will be
1753 * the min-coordinates of the first item.
1754 * - column, row = the 2d-corodinates (in 2D-view / 'tot' rect space) the cell exists at
1755 * - rect = coordinates of the cell (passed as single var instead of 4 separate, as it's more useful this way)
1757 void UI_view2d_listview_cell_to_view(View2D *v2d, short columnwidth, short rowheight, float startx, float starty, int column, int row, rctf *rect)
1760 if ELEM(NULL, v2d, rect)
1762 if ((columnwidth <= 0) && (rowheight <= 0)) {
1763 rect->xmin= rect->xmax= 0.0f;
1764 rect->ymin= rect->ymax= 0.0f;
1769 rect->xmin= startx + (float)(columnwidth * column);
1770 rect->xmax= startx + (float)(columnwidth * (column + 1));
1772 if ((v2d->align & V2D_ALIGN_NO_POS_X) && !(v2d->align & V2D_ALIGN_NO_NEG_X)) {
1773 /* simply negate the values for the coordinates if in negative half */
1774 rect->xmin = -rect->xmin;
1775 rect->xmax = -rect->xmax;
1779 rect->ymin= starty + (float)(rowheight * row);
1780 rect->ymax= starty + (float)(rowheight * (row + 1));
1782 if ((v2d->align & V2D_ALIGN_NO_POS_Y) && !(v2d->align & V2D_ALIGN_NO_NEG_Y)) {
1783 /* simply negate the values for the coordinates if in negative half */
1784 rect->ymin = -rect->ymin;
1785 rect->ymax = -rect->ymax;
1789 /* Get the 'cell' (row, column) that the given 2D-view coordinates (i.e. in 'tot' rect space) lie in.
1790 * - columnwidth, rowheight = size of each 'cell'
1791 * - startx, starty = coordinates (in 'tot' rect space) that the list starts from
1792 * This should be (0,0) for most views. However, for those where the starting row was offsetted
1793 * (like for Animation Editor channel lists, to make the first entry more visible), these will be
1794 * the min-coordinates of the first item.
1795 * - viewx, viewy = 2D-coordinates (in 2D-view / 'tot' rect space) to get the cell for
1796 * - column, row = the 'coordinates' of the relevant 'cell'
1798 void UI_view2d_listview_view_to_cell(View2D *v2d, short columnwidth, short rowheight, float startx, float starty,
1799 float viewx, float viewy, int *column, int *row)
1801 /* adjust view coordinates to be all positive ints, corrected for the start offset */
1802 const int x= (int)(floor(fabs(viewx) + 0.5f) - startx);
1803 const int y= (int)(floor(fabs(viewy) + 0.5f) - starty);
1805 /* sizes must not be negative */
1806 if ( (v2d == NULL) || ((columnwidth <= 0) && (rowheight <= 0)) ) {
1807 if (column) *column= 0;
1814 if ((column) && (columnwidth > 0))
1815 *column= x / columnwidth;
1820 if ((row) && (rowheight > 0))
1821 *row= y / rowheight;
1826 /* Get the 'extreme' (min/max) column and row indices which are visible within the 'cur' rect
1827 * - columnwidth, rowheight = size of each 'cell'
1828 * - startx, starty = coordinates that the list starts from, which should be (0,0) for most views
1829 * - column/row_min/max = the starting and ending column/row indices
1831 void UI_view2d_listview_visible_cells(View2D *v2d, short columnwidth, short rowheight, float startx, float starty,
1832 int *column_min, int *column_max, int *row_min, int *row_max)
1834 /* using 'cur' rect coordinates, call the cell-getting function to get the cells for this */
1837 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty,
1838 v2d->cur.xmin, v2d->cur.ymin, column_min, row_min);
1841 UI_view2d_listview_view_to_cell(v2d, columnwidth, rowheight, startx, starty,
1842 v2d->cur.xmax, v2d->cur.ymax, column_max, row_max);
1846 /* *********************************************************************** */
1847 /* Coordinate Conversions */
1849 /* Convert from screen/region space to 2d-View space
1851 * - x,y = coordinates to convert
1852 * - viewx,viewy = resultant coordinates
1854 void UI_view2d_region_to_view(View2D *v2d, int x, int y, float *viewx, float *viewy)
1859 div= (float)(v2d->mask.xmax - v2d->mask.xmin);
1860 ofs= (float)v2d->mask.xmin;
1862 *viewx= v2d->cur.xmin + (v2d->cur.xmax-v2d->cur.xmin) * ((float)x - ofs) / div;
1866 div= (float)(v2d->mask.ymax - v2d->mask.ymin);
1867 ofs= (float)v2d->mask.ymin;
1869 *viewy= v2d->cur.ymin + (v2d->cur.ymax - v2d->cur.ymin) * ((float)y - ofs) / div;
1873 /* Convert from 2d-View space to screen/region space
1874 * - Coordinates are clamped to lie within bounds of region
1876 * - x,y = coordinates to convert
1877 * - regionx,regiony = resultant coordinates
1879 void UI_view2d_view_to_region(View2D *v2d, float x, float y, int *regionx, int *regiony)
1881 /* set initial value in case coordinate lies outside of bounds */
1883 *regionx= V2D_IS_CLIPPED;
1885 *regiony= V2D_IS_CLIPPED;
1887 /* express given coordinates as proportional values */
1888 x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1889 y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1891 /* check if values are within bounds */
1892 if ((x>=0.0f) && (x<=1.0f) && (y>=0.0f) && (y<=1.0f)) {
1894 *regionx= (int)(v2d->mask.xmin + x*(v2d->mask.xmax-v2d->mask.xmin));
1896 *regiony= (int)(v2d->mask.ymin + y*(v2d->mask.ymax-v2d->mask.ymin));
1900 /* Convert from 2d-view space to screen/region space
1901 * - Coordinates are NOT clamped to lie within bounds of region
1903 * - x,y = coordinates to convert
1904 * - regionx,regiony = resultant coordinates
1906 void UI_view2d_to_region_no_clip(View2D *v2d, float x, float y, int *regionx, int *regiony)
1908 /* step 1: express given coordinates as proportional values */
1909 x= (x - v2d->cur.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1910 y= (y - v2d->cur.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1912 /* step 2: convert proportional distances to screen coordinates */
1913 x= v2d->mask.xmin + x*(v2d->mask.xmax - v2d->mask.xmin);
1914 y= v2d->mask.ymin + y*(v2d->mask.ymax - v2d->mask.ymin);
1916 /* although we don't clamp to lie within region bounds, we must avoid exceeding size of ints */
1918 if (x < INT_MIN) *regionx= INT_MIN;
1919 else if(x > INT_MAX) *regionx= INT_MAX;
1920 else *regionx= (int)x;
1923 if (y < INT_MIN) *regiony= INT_MIN;
1924 else if(y > INT_MAX) *regiony= INT_MAX;
1925 else *regiony= (int)y;
1929 /* *********************************************************************** */
1932 /* View2D data by default resides in region, so get from region stored in context */
1933 View2D *UI_view2d_fromcontext(const bContext *C)
1935 ScrArea *area= CTX_wm_area(C);
1936 ARegion *region= CTX_wm_region(C);
1938 if (area == NULL) return NULL;
1939 if (region == NULL) return NULL;
1940 return &(region->v2d);
1943 /* same as above, but it returns regionwindow. Utility for pulldowns or buttons */
1944 View2D *UI_view2d_fromcontext_rwin(const bContext *C)
1946 ScrArea *area= CTX_wm_area(C);
1947 ARegion *region= CTX_wm_region(C);
1949 if (area == NULL) return NULL;
1950 if (region == NULL) return NULL;
1951 if (region->regiontype!=RGN_TYPE_WINDOW) {
1952 ARegion *ar= area->regionbase.first;
1953 for(; ar; ar= ar->next)
1954 if(ar->regiontype==RGN_TYPE_WINDOW)
1958 return &(region->v2d);
1962 /* Calculate the scale per-axis of the drawing-area
1963 * - Is used to inverse correct drawing of icons, etc. that need to follow view
1964 * but not be affected by scale
1966 * - x,y = scale on each axis
1968 void UI_view2d_getscale(View2D *v2d, float *x, float *y)
1970 if (x) *x = (v2d->mask.xmax - v2d->mask.xmin) / (v2d->cur.xmax - v2d->cur.xmin);
1971 if (y) *y = (v2d->mask.ymax - v2d->mask.ymin) / (v2d->cur.ymax - v2d->cur.ymin);
1974 /* Check if mouse is within scrollers
1975 * - Returns appropriate code for match
1976 * 'h' = in horizontal scroller
1977 * 'v' = in vertical scroller
1978 * 0 = not in scroller
1980 * - x,y = mouse coordinates in screen (not region) space
1982 short UI_view2d_mouse_in_scrollers (const bContext *C, View2D *v2d, int x, int y)
1984 ARegion *ar= CTX_wm_region(C);
1986 int scroll= view2d_scroll_mapped(v2d->scroll);
1988 /* clamp x,y to region-coordinates first */
1989 co[0]= x - ar->winrct.xmin;
1990 co[1]= y - ar->winrct.ymin;
1992 /* check if within scrollbars */
1993 if (scroll & V2D_SCROLL_HORIZONTAL) {
1994 if (IN_2D_HORIZ_SCROLL(v2d, co)) return 'h';
1996 if (scroll & V2D_SCROLL_VERTICAL) {
1997 if (IN_2D_VERT_SCROLL(v2d, co)) return 'v';
2004 /* ******************* view2d text drawing cache ******************** */
2006 /* assumes caches are used correctly, so for time being no local storage in v2d */
2007 static ListBase strings= {NULL, NULL};
2009 typedef struct View2DString {
2010 struct View2DString *next, *prev;
2018 void UI_view2d_text_cache_add(View2D *v2d, float x, float y, char *str)
2022 UI_view2d_view_to_region(v2d, x, y, mval, mval+1);
2024 if(mval[0]!=V2D_IS_CLIPPED && mval[1]!=V2D_IS_CLIPPED) {
2025 /* use calloc, rect has to be zeroe'd */
2026 View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString");
2028 BLI_addtail(&strings, v2s);
2029 BLI_strncpy(v2s->str, str, 128);
2030 v2s->mval[0]= mval[0];
2031 v2s->mval[1]= mval[1];
2032 glGetFloatv(GL_CURRENT_COLOR, v2s->col);
2037 void UI_view2d_text_cache_rectf(View2D *v2d, rctf *rect, char *str)
2039 View2DString *v2s= MEM_callocN(sizeof(View2DString), "View2DString");
2041 UI_view2d_to_region_no_clip(v2d, rect->xmin, rect->ymin, &v2s->rect.xmin, &v2s->rect.ymin);
2042 UI_view2d_to_region_no_clip(v2d, rect->xmax, rect->ymax, &v2s->rect.xmax, &v2s->rect.ymax);
2044 BLI_addtail(&strings, v2s);
2045 BLI_strncpy(v2s->str, str, 128);
2046 glGetFloatv(GL_CURRENT_COLOR, v2s->col);
2050 void UI_view2d_text_cache_draw(ARegion *ar)
2055 ED_region_pixelspace(ar);
2057 for(v2s= strings.first; v2s; v2s= v2s->next) {
2058 glColor3fv(v2s->col);
2059 if(v2s->rect.xmin==v2s->rect.xmax)
2060 BLF_draw_default((float)v2s->mval[0], (float)v2s->mval[1], 0.0, v2s->str);
2064 yofs= ceil( 0.5f*(v2s->rect.ymax - v2s->rect.ymin - BLF_height_default("28")));
2067 BLF_clipping(v2s->rect.xmin-4, v2s->rect.ymin-4, v2s->rect.xmax+4, v2s->rect.ymax+4);
2068 BLF_enable(BLF_CLIPPING);
2070 BLF_draw_default(v2s->rect.xmin+xofs, v2s->rect.ymin+yofs, 0.0f, v2s->str);
2072 BLF_disable(BLF_CLIPPING);
2079 BLI_freelistN(&strings);
2083 /* ******************************************************** */