Bretch's patch for 2D transform. Thanks
[blender.git] / source / blender / src / drawimage.c
index 35dd0196fd06aaa6aaf7b9f1a721dbf5a355dda2..7897f95a197b6b74b6e3fb07a04414b8f24f6260 100644 (file)
 
 #ifdef WIN32
 #include <io.h>
 
 #ifdef WIN32
 #include <io.h>
-#include "BLI_winstuff.h"
 #else
 #include <unistd.h>
 #endif   
 #include "MEM_guardedalloc.h"
 
 #else
 #include <unistd.h>
 #endif   
 #include "MEM_guardedalloc.h"
 
+#include "BLI_arithb.h"
 #include "BLI_blenlib.h"
 
 #include "IMB_imbuf_types.h"
 #include "BLI_blenlib.h"
 
 #include "IMB_imbuf_types.h"
 
 #include "BKE_utildefines.h"
 #include "BKE_global.h"
 
 #include "BKE_utildefines.h"
 #include "BKE_global.h"
+#include "BKE_main.h"
 #include "BKE_mesh.h"
 #include "BKE_image.h"
 #include "BKE_mesh.h"
 #include "BKE_image.h"
+#include "BKE_DerivedMesh.h"
+#include "BKE_displist.h"
+#include "BKE_object.h"
 
 #include "BDR_editface.h"
 
 #include "BDR_editface.h"
-#include "BDR_editobject.h"
+#include "BDR_drawobject.h"
 #include "BDR_drawmesh.h"
 
 #include "BDR_drawmesh.h"
 
-       
 #include "BIF_gl.h"
 #include "BIF_gl.h"
-#include "BIF_space.h"
-#include "BIF_screen.h"
 #include "BIF_mywindow.h"
 #include "BIF_drawimage.h"
 #include "BIF_resources.h"
 #include "BIF_interface.h"
 #include "BIF_editsima.h"
 #include "BIF_glutil.h"
 #include "BIF_mywindow.h"
 #include "BIF_drawimage.h"
 #include "BIF_resources.h"
 #include "BIF_interface.h"
 #include "BIF_editsima.h"
 #include "BIF_glutil.h"
+#include "BIF_space.h"
+#include "BIF_screen.h"
+#include "BIF_transform.h"
 
 #include "BSE_trans_types.h"
 
 #include "BSE_trans_types.h"
+#include "BSE_view.h"
 
 /* Modules used */
 #include "mydevice.h"
 #include "blendef.h"
 #include "butspace.h"  // event codes
 
 
 /* Modules used */
 #include "mydevice.h"
 #include "blendef.h"
 #include "butspace.h"  // event codes
 
-
-void rectwrite_part(int winxmin, int winymin, int winxmax, int winymax, int x1, int y1, int xim, int yim, float zoomx, float zoomy, unsigned int *rect)
-{
-       int cx, cy, oldxim, x2, y2;
-       
-       oldxim= xim;
-               
-       /* coordinates how its drawn at the screen */
-       x2= x1+ zoomx*xim;
-       y2= y1+ zoomy*yim;
-
-       /* partial clip */
-       if(x1<winxmin) {
-               /* with OpenGL, rects are not allowed to start outside of the left/bottom window edge */
-               cx= winxmin-x1+(int)zoomx;
-               /* make sure the rect will be drawn pixel-exact */
-               cx/= zoomx;
-               cx++;
-               x1+= zoomx*cx;
-               xim-= cx;
-               rect+= cx;
-       }
-       if(y1<winymin) {
-               cy= winymin-y1+(int)zoomy;
-               cy/= zoomy;
-               cy++;
-               y1+= zoomy*cy;
-               rect+= cy*oldxim;
-               yim-= cy;
-       }
-       if(x2>=winxmax) {
-               cx= x2-winxmax;
-               cx/= zoomx;
-               xim-= cx+3;
-       }
-       if(y2>=winymax) {
-               cy= y2-winymax;
-               cy/= zoomy;
-               yim-= cy+3;
-       }
-       
-       if(xim<=0) return;
-       if(yim<=0) return;
-
-       mywinset(G.curscreen->mainwin);
-       glScissor(winxmin, winymin, winxmax-winxmin+1, winymax-winymin+1);
-       
-       glPixelStorei(GL_UNPACK_ROW_LENGTH,  oldxim);
-       
-       glPixelZoom(zoomx,  zoomy);
-
-       glRasterPos2i(x1, y1);
-       glDrawPixels(xim, yim, GL_RGBA, GL_UNSIGNED_BYTE,  rect);
-
-       glPixelZoom(1.0,  1.0);
-
-       glPixelStorei(GL_UNPACK_ROW_LENGTH,  0);
-       
-       mywinset(curarea->win);
-}
-
 /**
  * Sets up the fields of the View2D member of the SpaceImage struct
  * This routine can be called in two modes:
 /**
  * Sets up the fields of the View2D member of the SpaceImage struct
  * This routine can be called in two modes:
- * mode == 'f': float mode ???
- * mode == 'p': pixel mode ???
+ * mode == 'f': float mode (0.0 - 1.0)
+ * mode == 'p': pixel mode (0 - size)
  *
  * @param     sima  the image space to update
  * @param     mode  the mode to use for the update
  *
  * @param     sima  the image space to update
  * @param     mode  the mode to use for the update
@@ -242,7 +186,7 @@ void image_changed(SpaceImage *sima, int dotile)
        if(sima->mode==SI_TEXTURE) {
                
                if(G.f & G_FACESELECT) {
        if(sima->mode==SI_TEXTURE) {
                
                if(G.f & G_FACESELECT) {
-                       me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
+                       me= get_mesh(OBACT);
                        if(me && me->tface) {
                                tface= me->tface;
                                a= me->totface;
                        if(me && me->tface) {
                                tface= me->tface;
                                a= me->totface;
@@ -268,7 +212,8 @@ void image_changed(SpaceImage *sima, int dotile)
                                        }
                                        tface++;
                                }
                                        }
                                        tface++;
                                }
-                               allqueue(REDRAWVIEW3D, 0);
+
+                               object_uvs_changed(OBACT);
                                allqueue(REDRAWBUTSEDIT, 0);
                        }
                }
                                allqueue(REDRAWBUTSEDIT, 0);
                        }
                }
@@ -280,7 +225,7 @@ void uvco_to_areaco(float *vec, short *mval)
 {
        float x, y;
 
 {
        float x, y;
 
-       mval[0]= 3200;
+       mval[0]= IS_CLIPPED;
        
        x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
        y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
        
        x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
        y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
@@ -293,11 +238,11 @@ void uvco_to_areaco(float *vec, short *mval)
        }
 }
 
        }
 }
 
-void uvco_to_areaco_noclip(float *vec, short *mval)
+void uvco_to_areaco_noclip(float *vec, int *mval)
 {
        float x, y;
 
 {
        float x, y;
 
-       mval[0]= 3200;
+       mval[0]= IS_CLIPPED;
        
        x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
        y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
        
        x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
        y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
@@ -326,14 +271,22 @@ void draw_tfaces(void)
                        glLoadIdentity();
                        
                        /* draw shadow mesh */
                        glLoadIdentity();
                        
                        /* draw shadow mesh */
-                       if(G.sima->flag & SI_DRAWSHADOW){               
+                       if((G.sima->flag & SI_DRAWSHADOW) && !(G.obedit==OBACT)){
+                               int dmNeedsFree;
+                               DerivedMesh *dm = mesh_get_derived_final(OBACT, &dmNeedsFree);
+
+                               glColor3ub(112, 112, 112);
+                               if (dm->drawUVEdges) dm->drawUVEdges(dm);
+
+                               if (dmNeedsFree) dm->release(dm);
+                       }
+                       else if(G.sima->flag & SI_DRAWSHADOW){          
                                tface= me->tface;
                                mface= me->mface;
                                a= me->totface;                 
                                while(a--) {
                                tface= me->tface;
                                mface= me->mface;
                                a= me->totface;                 
                                while(a--) {
-                                       if(tface->flag & TF_HIDE);
-                                       else if(mface->v3) {
-                                               cpack(0x707070);
+                                       if(!(tface->flag & TF_HIDE)) {
+                                               glColor3ub(112, 112, 112);
                                                glBegin(GL_LINE_LOOP);
                                                glVertex2fv(tface->uv[0]);
                                                glVertex2fv(tface->uv[1]);
                                                glBegin(GL_LINE_LOOP);
                                                glVertex2fv(tface->uv[0]);
                                                glVertex2fv(tface->uv[1]);
@@ -356,7 +309,7 @@ void draw_tfaces(void)
                                mface= me->mface;
                                a= me->totface;                 
                                while(a--) {
                                mface= me->mface;
                                a= me->totface;                 
                                while(a--) {
-                                       if(mface->v3 && (tface->flag & TF_SELECT)) {
+                                       if(tface->flag & TF_SELECT) {
                                                if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
                                                   (!mface->v4 || tface->flag & TF_SEL4))
                                                        glColor4ubv(col2);
                                                if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
                                                   (!mface->v4 || tface->flag & TF_SEL4))
                                                        glColor4ubv(col2);
@@ -381,7 +334,7 @@ void draw_tfaces(void)
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
-                               if(mface->v3 && (tface->flag & TF_SELECT) ) {
+                               if(tface->flag & TF_SELECT) {
                                        if(tface->flag & TF_ACTIVE){
                                                activetface= tface; 
                                                activemface= mface; 
                                        if(tface->flag & TF_ACTIVE){
                                                activetface= tface; 
                                                activemface= mface; 
@@ -468,7 +421,7 @@ void draw_tfaces(void)
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
-                               if(mface->v3  && (tface->flag & TF_SELECT) ) {
+                               if(tface->flag & TF_SELECT) {
                                        
                                        if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
                                        if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
                                        
                                        if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
                                        if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
@@ -492,7 +445,7 @@ void draw_tfaces(void)
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
-                               if(mface->v3  && (tface->flag & TF_SELECT) ) {
+                               if(tface->flag & TF_SELECT) {
                                        
                                        if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
                                        if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
                                        
                                        if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
                                        if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
@@ -515,7 +468,7 @@ void draw_tfaces(void)
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
                        mface= me->mface;
                        a= me->totface;
                        while(a--) {
-                               if(mface->v3  && (tface->flag & TF_SELECT) ) {
+                               if(tface->flag & TF_SELECT) {
                                        
                                        if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
                                        if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
                                        
                                        if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
                                        if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
@@ -555,12 +508,12 @@ static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty,
        return rectmain;
 }
 
        return rectmain;
 }
 
-static void draw_image_prop_circle(ImBuf *ibuf)
+static void draw_image_transform(ImBuf *ibuf)
 {
 {
-       float aspx, aspy;
-       extern float prop_cent[3];
+       if(G.moving) {
+               float aspx, aspy, center[3];
 
 
-       if(G.moving && G.f & G_PROPORTIONAL) {
+        BIF_drawConstraint();
 
                if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
                        aspx= aspy= 1.0;
 
                if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
                        aspx= aspy= 1.0;
@@ -570,34 +523,38 @@ static void draw_image_prop_circle(ImBuf *ibuf)
                        aspy= 256.0/ibuf->y;
                }
 
                        aspy= 256.0/ibuf->y;
                }
 
+               BIF_getPropCenter(center);
+
                /* scale and translate the circle into place and draw it */
                glPushMatrix();
                glScalef(aspx, aspy, 1.0);
                /* scale and translate the circle into place and draw it */
                glPushMatrix();
                glScalef(aspx, aspy, 1.0);
-               glTranslatef((1/aspx)*prop_cent[0] - prop_cent[0],
-                            (1/aspy)*prop_cent[1] - prop_cent[1], 0.0);
-               draw_prop_circle();
+               glTranslatef((1/aspx)*center[0] - center[0],
+                            (1/aspy)*center[1] - center[1], 0.0);
+
+               BIF_drawPropCircle();
+
                glPopMatrix();
        }
 }
 
 static void draw_image_view_icon(void)
 {
                glPopMatrix();
        }
 }
 
 static void draw_image_view_icon(void)
 {
+       float xPos = 5.0;
+
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
        
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
        
-       glRasterPos2f(5.0, 5.0);
-
        if(G.sima->flag & SI_STICKYUVS) {
        if(G.sima->flag & SI_STICKYUVS) {
-               BIF_draw_icon(ICON_STICKY2_UVS);
-               glRasterPos2f(25.0, 5.0);
+               BIF_draw_icon(xPos, 5.0, ICON_STICKY2_UVS);
+               xPos = 25.0;
        }
        else if(G.sima->flag & SI_LOCALSTICKY) {
        }
        else if(G.sima->flag & SI_LOCALSTICKY) {
-               BIF_draw_icon(ICON_STICKY_UVS);
-               glRasterPos2f(25.0, 5.0);
+               BIF_draw_icon(xPos, 5.0, ICON_STICKY_UVS);
+               xPos = 25.0;
        }
 
        if(G.sima->flag & SI_SELACTFACE) {
        }
 
        if(G.sima->flag & SI_SELACTFACE) {
-                       BIF_draw_icon(ICON_DRAW_UVFACES);
+               BIF_draw_icon(xPos, 5.0, ICON_DRAW_UVFACES);
        }
        
        glBlendFunc(GL_ONE,  GL_ZERO); 
        }
        
        glBlendFunc(GL_ONE,  GL_ZERO); 
@@ -615,7 +572,7 @@ static void image_editvertex_buts(uiBlock *block)
        static float ocent[2];
        float cent[2]= {0.0, 0.0};
        int imx, imy;
        static float ocent[2];
        float cent[2]= {0.0, 0.0};
        int imx, imy;
-       int i, nactive= 0;
+       int i, nactive= 0, step, digits;
        Mesh *me;
        
        if( is_uv_tface_editing_allowed_silent()==0 ) return;
        Mesh *me;
        
        if( is_uv_tface_editing_allowed_silent()==0 ) return;
@@ -631,7 +588,7 @@ static void image_editvertex_buts(uiBlock *block)
                MFace *mf= &((MFace*) me->mface)[i];
                TFace *tf= &((TFace*) me->tface)[i];
                
                MFace *mf= &((MFace*) me->mface)[i];
                TFace *tf= &((TFace*) me->tface)[i];
                
-               if (!mf->v3 || !(tf->flag & TF_SELECT))
+               if (!(tf->flag & TF_SELECT))
                        continue;
                
                if (tf->flag & TF_SEL1) {
                        continue;
                
                if (tf->flag & TF_SEL1) {
@@ -658,38 +615,52 @@ static void image_editvertex_buts(uiBlock *block)
                
        if(block) {     // do the buttons
                if (nactive) {
                
        if(block) {     // do the buttons
                if (nactive) {
-                       ocent[0]= (cent[0]*imx)/nactive;
-                       ocent[1]= (cent[1]*imy)/nactive;
+                       ocent[0]= cent[0]/nactive;
+                       ocent[1]= cent[1]/nactive;
+                       if (G.sima->flag & SI_COORDFLOATS) {
+                               step= 1;
+                               digits= 3;
+                       }
+                       else {
+                               ocent[0] *= imx;
+                               ocent[1] *= imy;
+                               step= 100;
+                               digits= 2;
+                       }
                        
                        uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
                        
                        uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
+                       uiBlockBeginAlign(block);
                        if(nactive==1) {
                        if(nactive==1) {
-                               uiBlockBeginAlign(block);
-                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, 100, 0, "");
-                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, 100, 0, "");
-                               uiBlockEndAlign(block);
+                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
+                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
                        }
                        else {
                        }
                        else {
-                               uiBlockBeginAlign(block);
-                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, 100, 0, "");
-                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, 100, 0, "");
-                               uiBlockEndAlign(block);
+                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
+                               uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
                        }
                        }
+                       uiBlockEndAlign(block);
                }
        }
        else {  // apply event
                float delta[2];
                
                }
        }
        else {  // apply event
                float delta[2];
                
-               cent[0]= (cent[0]*imx)/nactive;
-               cent[1]= (cent[1]*imy)/nactive;
+               cent[0]= cent[0]/nactive;
+               cent[1]= cent[1]/nactive;
                        
                        
-               delta[0]= (ocent[0]-cent[0])/imx;
-               delta[1]= (ocent[1]-cent[1])/imy;
+               if (G.sima->flag & SI_COORDFLOATS) {
+                       delta[0]= ocent[0]-cent[0];
+                       delta[1]= ocent[1]-cent[1];
+               }
+               else {
+                       delta[0]= ocent[0]/imx - cent[0];
+                       delta[1]= ocent[1]/imy - cent[1];
+               }
 
                for (i=0; i<me->totface; i++) {
                        MFace *mf= &((MFace*) me->mface)[i];
                        TFace *tf= &((TFace*) me->tface)[i];
                
 
                for (i=0; i<me->totface; i++) {
                        MFace *mf= &((MFace*) me->mface)[i];
                        TFace *tf= &((TFace*) me->tface)[i];
                
-                       if (!mf->v3 || !(tf->flag & TF_SELECT))
+                       if (!(tf->flag & TF_SELECT))
                                continue;
                
                        if (tf->flag & TF_SEL1) {
                                continue;
                
                        if (tf->flag & TF_SEL1) {
@@ -775,14 +746,14 @@ static void image_panel_properties(short cntrl)   // IMAGE_HANDLER_PROPERTIES
                uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
 
                uiBlockBeginAlign(block);
                uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
 
                uiBlockBeginAlign(block);
-               uiDefButS(block, TOG|BIT|1, B_TWINANIM, "Anim", 10,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of animated texture");
+               uiDefButBitS(block, TOG, IMA_TWINANIM, B_TWINANIM, "Anim", 10,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of animated texture");
                uiDefButS(block, NUM, B_TWINANIM, "Start:",             10,130,140,19, &G.sima->image->twsta, 0.0, 128.0, 0, 0, "Displays the start frame of an animated texture");
                uiDefButS(block, NUM, B_TWINANIM, "End:",               10,110,140,19, &G.sima->image->twend, 0.0, 128.0, 0, 0, "Displays the end frame of an animated texture");
                uiDefButS(block, NUM, B_NOP, "Speed",                           10,90,140,19, &G.sima->image->animspeed, 1.0, 100.0, 0, 0, "Displays Speed of the animation in frames per second");
                uiBlockEndAlign(block);
 
                uiBlockBeginAlign(block);
                uiDefButS(block, NUM, B_TWINANIM, "Start:",             10,130,140,19, &G.sima->image->twsta, 0.0, 128.0, 0, 0, "Displays the start frame of an animated texture");
                uiDefButS(block, NUM, B_TWINANIM, "End:",               10,110,140,19, &G.sima->image->twend, 0.0, 128.0, 0, 0, "Displays the end frame of an animated texture");
                uiDefButS(block, NUM, B_NOP, "Speed",                           10,90,140,19, &G.sima->image->animspeed, 1.0, 100.0, 0, 0, "Displays Speed of the animation in frames per second");
                uiBlockEndAlign(block);
 
                uiBlockBeginAlign(block);
-               uiDefButS(block, TOG|BIT|0, B_SIMAGEDRAW1, "Tiles",     160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces");
+               uiDefButBitS(block, TOG, IMA_TILES, B_SIMAGEDRAW1, "Tiles",     160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces");
                uiDefButS(block, NUM, B_SIMAGEDRAW, "X:",               160,130,70,19, &G.sima->image->xrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the X direction");
                uiDefButS(block, NUM, B_SIMAGEDRAW, "Y:",               230,130,70,19, &G.sima->image->yrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the Y direction");
                uiBlockBeginAlign(block);
                uiDefButS(block, NUM, B_SIMAGEDRAW, "X:",               160,130,70,19, &G.sima->image->xrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the X direction");
                uiDefButS(block, NUM, B_SIMAGEDRAW, "Y:",               230,130,70,19, &G.sima->image->yrep, 1.0, 16.0, 0, 0, "Sets the degree of repetition in the Y direction");
                uiBlockBeginAlign(block);
@@ -811,7 +782,6 @@ static void image_panel_paint(short cntrl)  // IMAGE_HANDLER_PROPERTIES
        uiDefButF(block, NUM, B_NOP, "Size ",   180+12+24,140,80,20, &Gvp.size, 2.0, 64.0, 0, 0, "The size of the brush");
 }
 
        uiDefButF(block, NUM, B_NOP, "Size ",   180+12+24,140,80,20, &Gvp.size, 2.0, 64.0, 0, 0, "The size of the brush");
 }
 
-
 static void image_blockhandlers(ScrArea *sa)
 {
        SpaceImage *sima= sa->spacedata.first;
 static void image_blockhandlers(ScrArea *sa)
 {
        SpaceImage *sima= sa->spacedata.first;
@@ -843,9 +813,18 @@ void drawimagespace(ScrArea *sa, void *spacedata)
        ImBuf *ibuf= NULL;
        float col[3];
        unsigned int *rect;
        ImBuf *ibuf= NULL;
        float col[3];
        unsigned int *rect;
-       int x1, y1, xmin, xmax, ymin, ymax;
+       int x1, y1;
        short sx, sy, dx, dy;
        
        short sx, sy, dx, dy;
        
+               /* If derived data is used then make sure that object
+                * is up-to-date... might not be the case because updates
+                * are normally done in drawview and could get here before
+                * drawing a View3D.
+                */
+       if (!G.obedit && OBACT && (G.sima->flag&SI_DRAWSHADOW)) {
+               object_handle_update(OBACT);
+       }
+
        BIF_GetThemeColor3fv(TH_BACK, col);
        glClearColor(col[0], col[1], col[2], 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        BIF_GetThemeColor3fv(TH_BACK, col);
        glClearColor(col[0], col[1], col[2], 0.0);
        glClear(GL_COLOR_BUFFER_BIT);
@@ -853,16 +832,13 @@ void drawimagespace(ScrArea *sa, void *spacedata)
        bwin_clear_viewmat(sa->win);    /* clear buttons view */
        glLoadIdentity();
        
        bwin_clear_viewmat(sa->win);    /* clear buttons view */
        glLoadIdentity();
        
-       xmin= curarea->winrct.xmin; xmax= curarea->winrct.xmax;
-       ymin= curarea->winrct.ymin; ymax= curarea->winrct.ymax;
-       
        what_image(G.sima);
        
        if(G.sima->image) {
        what_image(G.sima);
        
        if(G.sima->image) {
-       
                if(G.sima->image->ibuf==0) {
                        load_image(G.sima->image, IB_rect, G.sce, G.scene->r.cfra);
                }       
                if(G.sima->image->ibuf==0) {
                        load_image(G.sima->image, IB_rect, G.sce, G.scene->r.cfra);
                }       
+               tag_image_time(G.sima->image);
                ibuf= G.sima->image->ibuf;
        }
        
                ibuf= G.sima->image->ibuf;
        }
        
@@ -875,15 +851,20 @@ void drawimagespace(ScrArea *sa, void *spacedata)
        }
        else {
                /* calc location */
        }
        else {
                /* calc location */
-               x1= xmin+(curarea->winx-G.sima->zoom*ibuf->x)/2;
-               y1= ymin+(curarea->winy-G.sima->zoom*ibuf->y)/2;
+               x1= (curarea->winx-G.sima->zoom*ibuf->x)/2;
+               y1= (curarea->winy-G.sima->zoom*ibuf->y)/2;
        
                x1-= G.sima->zoom*G.sima->xof;
                y1-= G.sima->zoom*G.sima->yof;
        
                x1-= G.sima->zoom*G.sima->xof;
                y1-= G.sima->zoom*G.sima->yof;
-       
                
                
+               /* needed for gla draw */
+               glaDefine2DArea(&curarea->winrct);
+               glPixelZoom((float)G.sima->zoom, (float)G.sima->zoom);
+                               
                if(G.sima->flag & SI_EDITTILE) {
                if(G.sima->flag & SI_EDITTILE) {
-                       rectwrite_part(xmin, ymin, xmax, ymax, x1, y1, ibuf->x, ibuf->y, (float)G.sima->zoom, (float)G.sima->zoom, ibuf->rect);
+                       glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
+                       
+                       glPixelZoom(1.0, 1.0);
                        
                        dx= ibuf->x/G.sima->image->xrep;
                        dy= ibuf->y/G.sima->image->yrep;
                        
                        dx= ibuf->x/G.sima->image->xrep;
                        dy= ibuf->y/G.sima->image->yrep;
@@ -902,9 +883,9 @@ void drawimagespace(ScrArea *sa, void *spacedata)
                        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1,  sy+1,  sx+dx,  sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                }
                else if(G.sima->mode==SI_TEXTURE) {
                        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1,  sy+1,  sx+dx,  sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                }
                else if(G.sima->mode==SI_TEXTURE) {
+                       
                        if(G.sima->image->tpageflag & IMA_TILES) {
                                
                        if(G.sima->image->tpageflag & IMA_TILES) {
                                
-                               
                                /* just leave this a while */
                                if(G.sima->image->xrep<1) return;
                                if(G.sima->image->yrep<1) return;
                                /* just leave this a while */
                                if(G.sima->image->xrep<1) return;
                                if(G.sima->image->yrep<1) return;
@@ -926,24 +907,24 @@ void drawimagespace(ScrArea *sa, void *spacedata)
                                /* rect= ibuf->rect; */
                                for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
                                        for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
                                /* rect= ibuf->rect; */
                                for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
                                        for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
-                                               
-                                               rectwrite_part(xmin, ymin, xmax, ymax, 
-                                                       x1+sx*G.sima->zoom, y1+sy*G.sima->zoom, dx, dy, (float)G.sima->zoom, (float)G.sima->zoom, rect);
+                                               glaDrawPixelsSafe(x1+sx*G.sima->zoom, y1+sy*G.sima->zoom, dx, dy, rect);
                                        }
                                }
                                
                                MEM_freeN(rect);
                        }
                        else 
                                        }
                                }
                                
                                MEM_freeN(rect);
                        }
                        else 
-                               rectwrite_part(xmin, ymin, xmax, ymax, x1, y1, ibuf->x, ibuf->y, (float)G.sima->zoom,(float)G.sima->zoom, ibuf->rect);
-               
+                               glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
+                       
+                       glPixelZoom(1.0, 1.0);
+                       
                        draw_tfaces();
                }
        
                calc_image_view(G.sima, 'f');   /* float */
        }
 
                        draw_tfaces();
                }
        
                calc_image_view(G.sima, 'f');   /* float */
        }
 
-       draw_image_prop_circle(ibuf);
+       draw_image_transform(ibuf);
 
        myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
        draw_image_view_icon();
 
        myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
        draw_image_view_icon();
@@ -956,80 +937,104 @@ void drawimagespace(ScrArea *sa, void *spacedata)
        curarea->win_swap= WIN_BACK_OK;
 }
 
        curarea->win_swap= WIN_BACK_OK;
 }
 
-void image_viewmove(void)
+static void image_zoom_power_of_two(void)
+{
+       /* Make zoom a power of 2 */
+
+       G.sima->zoom = 1 / G.sima->zoom;
+       G.sima->zoom = log(G.sima->zoom) / log(2);
+       G.sima->zoom = ceil(G.sima->zoom);
+       G.sima->zoom = pow(2, G.sima->zoom);
+       G.sima->zoom = 1 / G.sima->zoom;
+}
+
+static void image_zoom_set_factor(float zoomfac)
+{
+       SpaceImage *sima= curarea->spacedata.first;
+       int width, height;
+
+       if (zoomfac <= 0.0f)
+               return;
+
+       sima->zoom *= zoomfac;
+
+       if (sima->zoom > 0.1f && sima->zoom < 4.0f)
+               return;
+
+       /* check zoom limits */
+
+       calc_image_view(G.sima, 'p');
+       width= 256;
+       height= 256;
+       if (sima->image) {
+               if (sima->image->ibuf) {
+                       width= sima->image->ibuf->x;
+                       height= sima->image->ibuf->y;
+               }
+       }
+       width *= sima->zoom;
+       height *= sima->zoom;
+
+       if ((width < 4) && (height < 4))
+               sima->zoom /= zoomfac;
+       else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
+               sima->zoom /= zoomfac;
+       else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
+               sima->zoom /= zoomfac;
+}
+
+void image_viewmove(int mode)
 {
 {
-       short mval[2], mvalo[2], xof, yof;
+       short mval[2], mvalo[2], zoom0;
        
        getmouseco_sc(mvalo);
        
        getmouseco_sc(mvalo);
+       zoom0= G.sima->zoom;
        
        while(get_mbut()&(L_MOUSE|M_MOUSE)) {
 
                getmouseco_sc(mval);
        
        while(get_mbut()&(L_MOUSE|M_MOUSE)) {
 
                getmouseco_sc(mval);
+
+               if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
                
                
-               xof= (mvalo[0]-mval[0])/G.sima->zoom;
-               yof= (mvalo[1]-mval[1])/G.sima->zoom;
-               
-               if(xof || yof) {
-                       
-                       G.sima->xof+= xof;
-                       G.sima->yof+= yof;
-                       
+                       if(mode==0) {
+                               G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
+                               G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
+                       }
+                       else if (mode==1) {
+                               float factor;
+
+                               factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
+                               image_zoom_set_factor(factor);
+                       }
+
                        mvalo[0]= mval[0];
                        mvalo[1]= mval[1];
                        
                        scrarea_do_windraw(curarea);
                        screen_swapbuffers();
                        mvalo[0]= mval[0];
                        mvalo[1]= mval[1];
                        
                        scrarea_do_windraw(curarea);
                        screen_swapbuffers();
-               }               
+               }
                else BIF_wait_for_statechange();
        }
 }
 
                else BIF_wait_for_statechange();
        }
 }
 
-void image_viewzoom(unsigned short event)
+void image_viewzoom(unsigned short event, int invert)
 {
        SpaceImage *sima= curarea->spacedata.first;
 {
        SpaceImage *sima= curarea->spacedata.first;
-       int width, height;
 
 
-       if(U.uiflag & USER_WHEELZOOMDIR) {
-               if (event==WHEELDOWNMOUSE || event == PADPLUSKEY) {
-                       sima->zoom *= 2;
-               } else {
-                       sima->zoom /= 2;
-                       /* Check if the image will still be visible after zooming out */
-                       if (sima->zoom < 1) {
-                               calc_image_view(G.sima, 'p');
-                               if (sima->image) {
-                                       if (sima->image->ibuf) {
-                                               width = sima->image->ibuf->x * sima->zoom;
-                                               height = sima->image->ibuf->y * sima->zoom;
-                                               if ((width < 4) && (height < 4)) {
-                                                       /* Image will become too small, reset value */
-                                                       sima->zoom *= 2;
-                                               }
-                                       }
-                               }
-                       }
-               }
-       } else {
-               if (event==WHEELUPMOUSE || event == PADPLUSKEY) {
-                       sima->zoom *= 2;
-               } else {
-                       sima->zoom /= 2;
-                       /* Check if the image will still be visible after zooming out */
-                       if (sima->zoom < 1) {
-                               calc_image_view(G.sima, 'p');
-                               if (sima->image) {
-                                       if (sima->image->ibuf) {
-                                               width = sima->image->ibuf->x * sima->zoom;
-                                               height = sima->image->ibuf->y * sima->zoom;
-                                               if ((width < 4) && (height < 4)) {
-                                                       /* Image will become too small, reset value */
-                                                       sima->zoom *= 2;
-                                               }
-                                       }
-                               }
-                       }
-               }
-       }
+       if(event==WHEELDOWNMOUSE || event==PADMINUS)
+               image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
+       else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
+               image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
+       else if(event==PAD1)
+               sima->zoom= 1.0;
+       else if(event==PAD2)
+               sima->zoom= (invert)? 2.0: 0.5;
+       else if(event==PAD4)
+               sima->zoom= (invert)? 4.0: 0.25;
+       else if(event==PAD8)
+               sima->zoom= (invert)? 8.0: 0.125;
+       else
+               return;
 }
 
 /**
 }
 
 /**
@@ -1058,12 +1063,7 @@ void image_home(void)
                zoomY = ((float)height) / ((float)G.sima->image->ibuf->y);
                G.sima->zoom= MIN2(zoomX, zoomY);
 
                zoomY = ((float)height) / ((float)G.sima->image->ibuf->y);
                G.sima->zoom= MIN2(zoomX, zoomY);
 
-               /* Now make it a power of 2 */
-               G.sima->zoom = 1 / G.sima->zoom;
-               G.sima->zoom = log(G.sima->zoom) / log(2);
-               G.sima->zoom = ceil(G.sima->zoom);
-               G.sima->zoom = pow(2, G.sima->zoom);
-               G.sima->zoom = 1 / G.sima->zoom;
+               image_zoom_power_of_two();
        }
        else {
                G.sima->zoom= (float)1;
        }
        else {
                G.sima->zoom= (float)1;
@@ -1076,3 +1076,32 @@ void image_home(void)
        scrarea_queue_winredraw(curarea);
 }
 
        scrarea_queue_winredraw(curarea);
 }
 
+void image_viewcentre(void)
+{
+       float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
+
+       if( is_uv_tface_editing_allowed()==0 ) return;
+
+       if (!minmax_tface_uv(min, max)) return;
+
+       if(G.sima->image && G.sima->image->ibuf) {
+               xim= G.sima->image->ibuf->x;
+               yim= G.sima->image->ibuf->y;
+       }
+
+       G.sima->xof= ((min[0] + max[0])*0.5f - 0.5f)*xim;
+       G.sima->yof= ((min[1] + max[1])*0.5f - 0.5f)*yim;
+
+       d[0] = max[0] - min[0];
+       d[1] = max[1] - min[1];
+       size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
+       
+       if(size<=0.01) size= 0.01;
+
+       G.sima->zoom= 0.7/size;
+
+       calc_image_view(G.sima, 'p');
+
+       scrarea_queue_winredraw(curarea);
+}
+