View2D: Various Fixes + Header Support
authorJoshua Leung <aligorith@gmail.com>
Thu, 11 Dec 2008 03:50:50 +0000 (03:50 +0000)
committerJoshua Leung <aligorith@gmail.com>
Thu, 11 Dec 2008 03:50:50 +0000 (03:50 +0000)
Added support for panning headers using View2D of the header regions (only done for TimeLine, as only that one has buttons on it).
Currently, this uses some do_versions magic to initialise the View2D rects required, however, I'm not sure if all the information was safely ported over yet (notably height and position of rects due to alignment issues).

In the process, I noticed a few bugs with the current View2D code:
* UI_view2d_curRect_validate() is now called after UI_view2d_size_update(), as the size of the 'mask' rect is closely related to the size of the 'cur' rect (especially when zooming is locked).
* Fixed a bug in UI_view2d_curRect_validate() which meant that if the 'cur' rect was slightly larger than the 'tot' rect, and keeptot was set, the view would flicker between the two closely related positions.

source/blender/blenloader/intern/readfile.c
source/blender/editors/interface/view2d.c
source/blender/editors/space_time/space_time.c

index fd56709c822889dec2e78673620cc3f742420819..b49ebe3a77544e9874b31addd0e0fdb5887ae76b 100644 (file)
@@ -5054,7 +5054,36 @@ static void do_versions_windowmanager_2_50(bScreen *screen)
                                ar->alignment= RGN_ALIGN_BOTTOM;
                        else
                                ar->alignment= RGN_ALIGN_TOP;
-                       // TODO: add conversion stuff for header scrolling to v2d of header region
+                       
+                       /* initialise view2d data for header region, to allow panning */
+                       ar->v2d.keepaspect= 1;
+                       ar->v2d.keepzoom = (V2D_LOCKZOOM_X|V2D_LOCKZOOM_Y|V2D_KEEPZOOM);
+                       ar->v2d.keepofs = V2D_LOCKOFS_Y;
+                       ar->v2d.keeptot = 2; // this keeps the view in place when region size changes...
+                       ar->v2d.align = V2D_ALIGN_NO_NEG_X;
+                       
+                       ar->v2d.minzoom= ar->v2d.maxzoom= 1.0f;
+                       
+                       ar->v2d.mask.xmin= ar->v2d.mask.ymin= 0;
+                       ar->v2d.mask.xmax= sa->winx;
+                       ar->v2d.mask.ymax= HEADERY;
+                       
+                       ar->v2d.cur.xmin= sa->headbutofs;
+                       ar->v2d.cur.xmax= sa->winx + sa->headbutofs;
+                       ar->v2d.tot.xmin= 0.0f;
+                       ar->v2d.tot.xmax= sa->headbutlen;
+                       
+                       if (ar->alignment == RGN_ALIGN_BOTTOM) {
+                               ar->v2d.align = V2D_ALIGN_NO_NEG_Y;
+                               ar->v2d.tot.ymin= ar->v2d.cur.ymin= 0.0f; // what was area->headrct.ymin?
+                               ar->v2d.tot.ymax= ar->v2d.cur.ymax= HEADERY;
+                       }
+                       else {
+                               // XXX what were the extents of the old headers?
+                               ar->v2d.align = V2D_ALIGN_NO_NEG_Y;
+                               ar->v2d.tot.ymin= ar->v2d.cur.ymin= 0.0f; // what was area->headrct.ymin?
+                               ar->v2d.tot.ymax= ar->v2d.cur.ymax= HEADERY;
+                       }
                }
                
                ar= MEM_callocN(sizeof(ARegion), "area region from do_versions");
index c3544f20a1559c3f4c6707ff2d9d078015f0d9d8..bdbda0087b0d4ced8d8d539982c977311a852ca9 100644 (file)
@@ -53,8 +53,8 @@
 /* Refresh and Validation */
 
 /* Adjust mask size in response to view size changes 
- *     - When drawing a region, this should be called before 
- *       any other drawing using View2D happens.
+ *     - This should only be called in region init() callbacks, which are
+ *       called when the region is resized or area changes...
  */
 // XXX pre2.5 -> this used to be called  calc_scrollrcts()
 void UI_view2d_size_update(View2D *v2d, int winx, int winy)
@@ -96,6 +96,9 @@ void UI_view2d_size_update(View2D *v2d, int winx, int winy)
                        v2d->mask.ymax= v2d->hor.ymin;
                }
        }
+       
+       /* make sure that 'cur' rect is in a valid state as a result of these changes */
+       UI_view2d_curRect_validate(v2d);
 }
 
 /* Ensure View2D rects remain in a viable configuration 
@@ -104,7 +107,6 @@ void UI_view2d_size_update(View2D *v2d, int winx, int winy)
 // XXX pre2.5 -> this used to be called  test_view2d()
 void UI_view2d_curRect_validate(View2D *v2d)
 {
-       /* cur is not allowed to be larger than max, smaller than min, or outside of tot */
        float totwidth, totheight, curwidth, curheight, width, height;
        float winx, winy;
        rctf *cur, *tot;
@@ -146,17 +148,21 @@ void UI_view2d_curRect_validate(View2D *v2d)
                float zoom, fac;
                
                /* check if excessive zoom on x-axis */
-               zoom= winx / width;
-               if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
-                       fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
-                       width *= fac;
+               if ((v2d->keepzoom & V2D_LOCKZOOM_X)==0) {
+                       zoom= winx / width;
+                       if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
+                               fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
+                               width *= fac;
+                       }
                }
                
                /* check if excessive zoom on y-axis */
-               zoom= winy / height;
-               if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
-                       fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
-                       height *= fac;
+               if ((v2d->keepzoom & V2D_LOCKZOOM_Y)==0) {
+                       zoom= winy / height;
+                       if ((zoom < v2d->minzoom) || (zoom > v2d->maxzoom)) {
+                               fac= (zoom < v2d->minzoom) ? (zoom / v2d->minzoom) : (zoom / v2d->maxzoom);
+                               height *= fac;
+                       }
                }
        }
        else {
@@ -248,14 +254,14 @@ void UI_view2d_curRect_validate(View2D *v2d)
        
        /* Step 3: adjust so that it doesn't fall outside of bounds of tot */
        if (v2d->keeptot) {
-               float temp;
+               float temp, diff;
                
                /* recalculate extents of cur */
                curwidth= cur->xmax - cur->xmin;
                curheight= cur->ymax - cur->ymin;
                
                /* width */
-               if ((curwidth > totwidth) && (v2d->keepzoom == 0)) {
+               if ( (curwidth > totwidth) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_X)) ) {
                        /* if zoom doesn't have to be maintained, just clamp edges */
                        if (cur->xmin < tot->xmin) cur->xmin= tot->xmin;
                        if (cur->xmax > tot->xmax) cur->xmax= tot->xmax;
@@ -301,7 +307,15 @@ void UI_view2d_curRect_validate(View2D *v2d)
                         * We favour moving the 'minimum' across, as that's origin for most things
                         * (XXX - in the past, max was favoured... if there are bugs, swap!)
                         */
-                       if (cur->xmin > tot->xmin) {
+                       if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
+                               /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
+                               temp= (tot->ymax + tot->ymin) * 0.5f;
+                               diff= curheight * 0.5f;
+                               
+                               cur->ymin= temp - diff;
+                               cur->ymax= temp + diff;
+                       }
+                       else if (cur->xmin > tot->xmin) {
                                /* there's still space remaining, so shift left */
                                temp= cur->xmin - tot->xmin;
                                
@@ -318,7 +332,7 @@ void UI_view2d_curRect_validate(View2D *v2d)
                }
                
                /* height */
-               if ((curheight > totheight) && (v2d->keepzoom == 0)) {
+               if ( (curheight > totheight) && !(v2d->keepzoom & (V2D_KEEPZOOM|V2D_LOCKZOOM_Y)) ) {
                        /* if zoom doesn't have to be maintained, just clamp edges */
                        if (cur->ymin < tot->ymin) cur->ymin= tot->ymin;
                        if (cur->ymax > tot->ymax) cur->ymax= tot->ymax;
@@ -331,7 +345,15 @@ void UI_view2d_curRect_validate(View2D *v2d)
                         * So, resolution is to just shift view by the gap between the extremities.
                         * We favour moving the 'minimum' across, as that's origin for most things
                         */
-                       if (cur->ymin < tot->ymin) {
+                       if ((cur->ymin < tot->ymin) && (cur->ymax > tot->ymax)) {
+                               /* outside boundaries on both sides, so take middle-point of tot, and place in balanced way */
+                               temp= (tot->ymax + tot->ymin) * 0.5f;
+                               diff= curheight * 0.5f;
+                               
+                               cur->ymin= temp - diff;
+                               cur->ymax= temp + diff;
+                       }
+                       else if (cur->ymin < tot->ymin) {
                                /* there's still space remaining, so shift up */
                                temp= tot->ymin - cur->ymin;
                                
@@ -460,6 +482,9 @@ void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
         * as they were causing some unwanted offsets when drawing 
         */
        wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, v2d->cur.ymin, v2d->cur.ymax);
+       
+       /* XXX is this necessary? */
+       wmLoadIdentity(C->window);
 }
 
 /* Set view matrices to only use one axis of 'cur' only
@@ -469,20 +494,18 @@ void UI_view2d_view_ortho(const bContext *C, View2D *v2d)
  */
 void UI_view2d_view_orthoSpecial(const bContext *C, View2D *v2d, short xaxis)
 {
-       ARegion *region= C->region;
-       int winx, winy;
-       
-       /* calculate extents of region */
-       winx= region->winrct.xmax - region->winrct.xmin + 1;
-       winy= region->winrct.ymax - region->winrct.ymin + 1;
+       ARegion *ar= C->region;
        
        /* set the matrix - pixel offsets (-0.375) for 1:1 correspondance are not applied, 
         * as they were causing some unwanted offsets when drawing 
         */
        if (xaxis)
-               wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, 0, winy);
+               wmOrtho2(C->window, v2d->cur.xmin, v2d->cur.xmax, 0, ar->winy);
        else
-               wmOrtho2(C->window, 0, winx, v2d->cur.ymin, v2d->cur.ymax);
+               wmOrtho2(C->window, 0, ar->winx, v2d->cur.ymin, v2d->cur.ymax);
+               
+       /* XXX is this necessary? */
+       wmLoadIdentity(C->window);
 } 
 
 
index 78afe75d013e91f738436995906a21fe25a2656c..de7573892aaf54ed3a35378db5d21048c4ccca2d 100644 (file)
@@ -166,6 +166,12 @@ static void time_main_area_listener(ARegion *ar, wmNotifier *wmn)
 
 /* ************************ header time area region *********************** */
 
+/* add handlers, stuff you only do once or on area/region changes */
+static void time_header_area_init(wmWindowManager *wm, ARegion *ar)
+{
+       UI_view2d_size_update(&ar->v2d, ar->winx, ar->winy);
+}
+
 static void time_header_area_draw(const bContext *C, ARegion *ar)
 {
        float col[3];
@@ -178,8 +184,14 @@ static void time_header_area_draw(const bContext *C, ARegion *ar)
                
        glClearColor(col[0], col[1], col[2], 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
-
+       
+       /* set view2d view matrix for scrolling (without scrollers) */
+       UI_view2d_view_ortho(C, &ar->v2d);
+       
        time_header_buttons(C, ar);
+       
+       /* restore view matrix? */
+       UI_view2d_view_restore(C);
 }
 
 static void time_header_area_free(ARegion *ar)
@@ -279,8 +291,9 @@ void ED_spacetype_time(void)
        art= MEM_callocN(sizeof(ARegionType), "spacetype time region");
        art->regionid = RGN_TYPE_HEADER;
        art->minsizey= HEADERY;
-       art->keymapflag= ED_KEYMAP_UI;
+       art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D;
        
+       art->init= time_header_area_init;
        art->draw= time_header_area_draw;
        art->free= time_header_area_free;
        BLI_addhead(&st->regiontypes, art);