Bretch's patch for 2D transform. Thanks
[blender.git] / source / blender / src / drawimage.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
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. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33 #include <math.h>
34 #include <string.h>
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #ifdef WIN32
41 #include <io.h>
42 #else
43 #include <unistd.h>
44 #endif   
45 #include "MEM_guardedalloc.h"
46
47 #include "BLI_arithb.h"
48 #include "BLI_blenlib.h"
49
50 #include "IMB_imbuf_types.h"
51
52 #include "DNA_image_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_packedFile_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_userdef_types.h"
60
61 #include "BKE_utildefines.h"
62 #include "BKE_global.h"
63 #include "BKE_main.h"
64 #include "BKE_mesh.h"
65 #include "BKE_image.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_displist.h"
68 #include "BKE_object.h"
69
70 #include "BDR_editface.h"
71 #include "BDR_drawobject.h"
72 #include "BDR_drawmesh.h"
73
74 #include "BIF_gl.h"
75 #include "BIF_mywindow.h"
76 #include "BIF_drawimage.h"
77 #include "BIF_resources.h"
78 #include "BIF_interface.h"
79 #include "BIF_editsima.h"
80 #include "BIF_glutil.h"
81 #include "BIF_space.h"
82 #include "BIF_screen.h"
83 #include "BIF_transform.h"
84
85 #include "BSE_trans_types.h"
86 #include "BSE_view.h"
87
88 /* Modules used */
89 #include "mydevice.h"
90 #include "blendef.h"
91 #include "butspace.h"  // event codes
92
93 /**
94  * Sets up the fields of the View2D member of the SpaceImage struct
95  * This routine can be called in two modes:
96  * mode == 'f': float mode (0.0 - 1.0)
97  * mode == 'p': pixel mode (0 - size)
98  *
99  * @param     sima  the image space to update
100  * @param     mode  the mode to use for the update
101  * @return    void
102  *   
103  */
104 void calc_image_view(SpaceImage *sima, char mode)
105 {
106         float xim=256, yim=256;
107         float x1, y1;
108         float zoom;
109         
110         if(sima->image && sima->image->ibuf) {
111                 xim= sima->image->ibuf->x;
112                 yim= sima->image->ibuf->y;
113         }
114         
115         sima->v2d.tot.xmin= 0;
116         sima->v2d.tot.ymin= 0;
117         sima->v2d.tot.xmax= xim;
118         sima->v2d.tot.ymax= yim;
119         
120         sima->v2d.mask.xmin= sima->v2d.mask.ymin= 0;
121         sima->v2d.mask.xmax= curarea->winx;
122         sima->v2d.mask.ymax= curarea->winy;
123
124
125         /* Which part of the image space do we see? */
126         /* Same calculation as in lrectwrite: area left and down*/
127         x1= curarea->winrct.xmin+(curarea->winx-sima->zoom*xim)/2;
128         y1= curarea->winrct.ymin+(curarea->winy-sima->zoom*yim)/2;
129
130         x1-= sima->zoom*sima->xof;
131         y1-= sima->zoom*sima->yof;
132
133         /* float! */
134         zoom= sima->zoom;
135         
136         /* relative display right */
137         sima->v2d.cur.xmin= ((curarea->winrct.xmin - (float)x1)/zoom);
138         sima->v2d.cur.xmax= sima->v2d.cur.xmin + ((float)curarea->winx/zoom);
139         
140         /* relative display left */
141         sima->v2d.cur.ymin= ((curarea->winrct.ymin-(float)y1)/zoom);
142         sima->v2d.cur.ymax= sima->v2d.cur.ymin + ((float)curarea->winy/zoom);
143         
144         if(mode=='f') {         
145                 sima->v2d.cur.xmin/= xim;
146                 sima->v2d.cur.xmax/= xim;
147                 sima->v2d.cur.ymin/= yim;
148                 sima->v2d.cur.ymax/= yim;
149         }
150 }
151
152 void what_image(SpaceImage *sima)
153 {
154         extern TFace *lasttface;        /* editface.c */
155         Mesh *me;
156                 
157         if(sima->mode==SI_TEXTURE) {
158                 if(G.f & G_FACESELECT) {
159
160                         sima->image= 0;
161                         me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
162                         set_lasttface();
163                         
164                         if(me && me->tface && lasttface && lasttface->mode & TF_TEX) {
165                                 sima->image= lasttface->tpage;
166                                         
167                                 if(sima->flag & SI_EDITTILE);
168                                 else sima->curtile= lasttface->tile;
169                                 
170                                 if(sima->image) {
171                                         if(lasttface->mode & TF_TILES)
172                                                 sima->image->tpageflag |= IMA_TILES;
173                                         else sima->image->tpageflag &= ~IMA_TILES;
174                                 }
175                         }
176                 }
177         }
178 }
179
180 void image_changed(SpaceImage *sima, int dotile)
181 {
182         TFace *tface;
183         Mesh *me;
184         int a;
185         
186         if(sima->mode==SI_TEXTURE) {
187                 
188                 if(G.f & G_FACESELECT) {
189                         me= get_mesh(OBACT);
190                         if(me && me->tface) {
191                                 tface= me->tface;
192                                 a= me->totface;
193                                 while(a--) {
194                                         if(tface->flag & TF_SELECT) {
195                                                 
196                                                 if(dotile==2) {
197                                                         tface->mode &= ~TF_TILES;
198                                                 }
199                                                 else {
200                                                         tface->tpage= sima->image;
201                                                         tface->mode |= TF_TEX;
202                                                 
203                                                         if(dotile) tface->tile= sima->curtile;
204                                                 }
205                                                 
206                                                 if(sima->image) {
207                                                         if(sima->image->tpageflag & IMA_TILES) tface->mode |= TF_TILES;
208                                                         else tface->mode &= ~TF_TILES;
209                                                 
210                                                         if(sima->image->id.us==0) sima->image->id.us= 1;
211                                                 }
212                                         }
213                                         tface++;
214                                 }
215
216                                 object_uvs_changed(OBACT);
217                                 allqueue(REDRAWBUTSEDIT, 0);
218                         }
219                 }
220         }
221 }
222
223
224 void uvco_to_areaco(float *vec, short *mval)
225 {
226         float x, y;
227
228         mval[0]= IS_CLIPPED;
229         
230         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
231         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
232
233         if(x>=0.0 && x<=1.0) {
234                 if(y>=0.0 && y<=1.0) {          
235                         mval[0]= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
236                         mval[1]= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
237                 }
238         }
239 }
240
241 void uvco_to_areaco_noclip(float *vec, int *mval)
242 {
243         float x, y;
244
245         mval[0]= IS_CLIPPED;
246         
247         x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
248         y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
249
250         x= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
251         y= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
252         
253         mval[0]= x;
254         mval[1]= y;
255 }
256
257 void draw_tfaces(void)
258 {
259         TFace *tface,*activetface = NULL;
260         MFace *mface,*activemface = NULL;
261         Mesh *me;
262         int a;
263         char col1[4], col2[4];
264         float pointsize= BIF_GetThemeValuef(TH_VERTEX_SIZE);
265         
266         if(G.f & G_FACESELECT) {
267                 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
268                 if(me && me->tface) {
269                         calc_image_view(G.sima, 'f');   /* float */
270                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
271                         glLoadIdentity();
272                         
273                         /* draw shadow mesh */
274                         if((G.sima->flag & SI_DRAWSHADOW) && !(G.obedit==OBACT)){
275                                 int dmNeedsFree;
276                                 DerivedMesh *dm = mesh_get_derived_final(OBACT, &dmNeedsFree);
277
278                                 glColor3ub(112, 112, 112);
279                                 if (dm->drawUVEdges) dm->drawUVEdges(dm);
280
281                                 if (dmNeedsFree) dm->release(dm);
282                         }
283                         else if(G.sima->flag & SI_DRAWSHADOW){          
284                                 tface= me->tface;
285                                 mface= me->mface;
286                                 a= me->totface;                 
287                                 while(a--) {
288                                         if(!(tface->flag & TF_HIDE)) {
289                                                 glColor3ub(112, 112, 112);
290                                                 glBegin(GL_LINE_LOOP);
291                                                 glVertex2fv(tface->uv[0]);
292                                                 glVertex2fv(tface->uv[1]);
293                                                 glVertex2fv(tface->uv[2]);
294                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
295                                                 glEnd();
296                                         } 
297                                         tface++;
298                                         mface++;                                        
299                                 }
300                         }
301                         
302                         /* draw transparent faces */
303                         if(G.f & G_DRAWFACES) {
304                                 BIF_GetThemeColor4ubv(TH_FACE, col1);
305                                 BIF_GetThemeColor4ubv(TH_FACE_SELECT, col2);
306                                 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
307                                 glEnable(GL_BLEND);
308                                 tface= me->tface;
309                                 mface= me->mface;
310                                 a= me->totface;                 
311                                 while(a--) {
312                                         if(tface->flag & TF_SELECT) {
313                                                 if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
314                                                    (!mface->v4 || tface->flag & TF_SEL4))
315                                                         glColor4ubv(col2);
316                                                 else
317                                                         glColor4ubv(col1);
318                                                         
319                                                 glBegin(mface->v4?GL_QUADS:GL_TRIANGLES);
320                                                         glVertex2fv(tface->uv[0]);
321                                                         glVertex2fv(tface->uv[1]);
322                                                         glVertex2fv(tface->uv[2]);
323                                                         if(mface->v4) glVertex2fv(tface->uv[3]);
324                                                 glEnd();
325                                         }
326                                         tface++;
327                                         mface++;                                        
328                                 }
329                                 glDisable(GL_BLEND);
330                         }
331
332
333                         tface= me->tface;
334                         mface= me->mface;
335                         a= me->totface;
336                         while(a--) {
337                                 if(tface->flag & TF_SELECT) {
338                                         if(tface->flag & TF_ACTIVE){
339                                                 activetface= tface; 
340                                                 activemface= mface; 
341                                         }
342
343                                         cpack(0x0);
344                                         glBegin(GL_LINE_LOOP);
345                                                 glVertex2fv(tface->uv[0]);
346                                                 glVertex2fv(tface->uv[1]);
347                                                 glVertex2fv(tface->uv[2]);
348                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
349                                         glEnd();
350                                 
351                                         setlinestyle(2);
352                                         cpack(0xFFFFFF);
353                                         glBegin(GL_LINE_STRIP);
354                                                 glVertex2fv(tface->uv[0]);
355                                                 glVertex2fv(tface->uv[1]);
356                                         glEnd();
357
358                                         glBegin(GL_LINE_STRIP);
359                                                 glVertex2fv(tface->uv[0]);
360                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
361                                                 else glVertex2fv(tface->uv[2]);
362                                         glEnd();
363         
364                                         glBegin(GL_LINE_STRIP);
365                                                 glVertex2fv(tface->uv[1]);
366                                                 glVertex2fv(tface->uv[2]);
367                                                 if(mface->v4) glVertex2fv(tface->uv[3]);
368                                         glEnd();
369                                         setlinestyle(0);
370                                 }
371                                         
372                                 tface++;
373                                 mface++;
374                         }
375
376                         /* draw active face edges */
377                         if (activetface){
378                                 /* colors: R=u G=v */
379
380                                 setlinestyle(2);
381                                 tface=activetface; 
382                                 mface=activemface; 
383
384                                 cpack(0x0);
385                                 glBegin(GL_LINE_LOOP);
386                                 glVertex2fv(tface->uv[0]);
387                                         glVertex2fv(tface->uv[1]);
388                                         glVertex2fv(tface->uv[2]);
389                                         if(mface->v4) glVertex2fv(tface->uv[3]);
390                                 glEnd();
391                                         
392                                 cpack(0xFF00);
393                                 glBegin(GL_LINE_STRIP);
394                                         glVertex2fv(tface->uv[0]);
395                                         glVertex2fv(tface->uv[1]);
396                                 glEnd();
397
398                                 cpack(0xFF);
399                                 glBegin(GL_LINE_STRIP);
400                                         glVertex2fv(tface->uv[0]);
401                                         if(mface->v4) glVertex2fv(tface->uv[3]);
402                                         else glVertex2fv(tface->uv[2]);
403                                 glEnd();
404
405                                 cpack(0xFFFFFF);
406                                 glBegin(GL_LINE_STRIP);
407                                         glVertex2fv(tface->uv[1]);
408                                         glVertex2fv(tface->uv[2]);
409                                         if(mface->v4) glVertex2fv(tface->uv[3]);
410                                 glEnd();
411                                 
412                                 setlinestyle(0);
413                         }
414
415             /* unselected uv's */
416                         BIF_ThemeColor(TH_VERTEX);
417                         glPointSize(pointsize);
418
419                         bglBegin(GL_POINTS);
420                         tface= me->tface;
421                         mface= me->mface;
422                         a= me->totface;
423                         while(a--) {
424                                 if(tface->flag & TF_SELECT) {
425                                         
426                                         if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
427                                         if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
428                                         if(tface->flag & TF_SEL3); else bglVertex2fv(tface->uv[2]);
429                                         if(mface->v4) {
430                                                 if(tface->flag & TF_SEL4); else bglVertex2fv(tface->uv[3]);
431                                         }
432                                 }
433                                 tface++;
434                                 mface++;
435                         }
436                         bglEnd();
437
438                         /* pinned uv's */
439                         /* give odd pointsizes odd pin pointsizes */
440                 glPointSize(pointsize*2 + (((int)pointsize % 2)? (-1): 0));
441                         cpack(0xFF);
442
443                         bglBegin(GL_POINTS);
444                         tface= me->tface;
445                         mface= me->mface;
446                         a= me->totface;
447                         while(a--) {
448                                 if(tface->flag & TF_SELECT) {
449                                         
450                                         if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
451                                         if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
452                                         if(tface->unwrap & TF_PIN3) bglVertex2fv(tface->uv[2]);
453                                         if(mface->v4) {
454                                                 if(tface->unwrap & TF_PIN4) bglVertex2fv(tface->uv[3]);
455                                         }
456                                 }
457                                 tface++;
458                                 mface++;
459                         }
460                         bglEnd();
461
462                         /* selected uv's */
463                         BIF_ThemeColor(TH_VERTEX_SELECT);
464                 glPointSize(pointsize);
465
466                         bglBegin(GL_POINTS);
467                         tface= me->tface;
468                         mface= me->mface;
469                         a= me->totface;
470                         while(a--) {
471                                 if(tface->flag & TF_SELECT) {
472                                         
473                                         if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
474                                         if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
475                                         if(tface->flag & TF_SEL3) bglVertex2fv(tface->uv[2]);
476                                         if(mface->v4) {
477                                                 if(tface->flag & TF_SEL4) bglVertex2fv(tface->uv[3]);
478                                         }
479                                 }
480                                 tface++;
481                                 mface++;
482                         }
483                         bglEnd();
484                 }
485         }
486         glPointSize(1.0);
487 }
488
489 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
490 {
491         unsigned int *rt, *rp, *rectmain;
492         short y, heigth, len;
493
494         /* the right offset in rectot */
495
496         rt= ibuf->rect+ (starty*ibuf->x+ startx);
497
498         len= (endx-startx);
499         heigth= (endy-starty);
500
501         rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
502         
503         for(y=0; y<heigth; y++) {
504                 memcpy(rp, rt, len*4);
505                 rt+= ibuf->x;
506                 rp+= len;
507         }
508         return rectmain;
509 }
510
511 static void draw_image_transform(ImBuf *ibuf)
512 {
513         if(G.moving) {
514                 float aspx, aspy, center[3];
515
516         BIF_drawConstraint();
517
518                 if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
519                         aspx= aspy= 1.0;
520                 }
521                 else {
522                         aspx= 256.0/ibuf->x;
523                         aspy= 256.0/ibuf->y;
524                 }
525
526                 BIF_getPropCenter(center);
527
528                 /* scale and translate the circle into place and draw it */
529                 glPushMatrix();
530                 glScalef(aspx, aspy, 1.0);
531                 glTranslatef((1/aspx)*center[0] - center[0],
532                              (1/aspy)*center[1] - center[1], 0.0);
533
534                 BIF_drawPropCircle();
535
536                 glPopMatrix();
537         }
538 }
539
540 static void draw_image_view_icon(void)
541 {
542         float xPos = 5.0;
543
544         glEnable(GL_BLEND);
545         glBlendFunc(GL_SRC_ALPHA,  GL_ONE_MINUS_SRC_ALPHA); 
546         
547         if(G.sima->flag & SI_STICKYUVS) {
548                 BIF_draw_icon(xPos, 5.0, ICON_STICKY2_UVS);
549                 xPos = 25.0;
550         }
551         else if(G.sima->flag & SI_LOCALSTICKY) {
552                 BIF_draw_icon(xPos, 5.0, ICON_STICKY_UVS);
553                 xPos = 25.0;
554         }
555
556         if(G.sima->flag & SI_SELACTFACE) {
557                 BIF_draw_icon(xPos, 5.0, ICON_DRAW_UVFACES);
558         }
559         
560         glBlendFunc(GL_ONE,  GL_ZERO); 
561         glDisable(GL_BLEND);
562 }
563
564 /* ************ panel stuff ************* */
565
566 // button define is local, only events defined here possible
567 #define B_TRANS_IMAGE   1
568
569 /* is used for both read and write... */
570 static void image_editvertex_buts(uiBlock *block)
571 {
572         static float ocent[2];
573         float cent[2]= {0.0, 0.0};
574         int imx, imy;
575         int i, nactive= 0, step, digits;
576         Mesh *me;
577         
578         if( is_uv_tface_editing_allowed_silent()==0 ) return;
579         me= get_mesh(OBACT);
580         
581         if (G.sima->image && G.sima->image->ibuf) {
582                 imx= G.sima->image->ibuf->x;
583                 imy= G.sima->image->ibuf->y;
584         } else
585                 imx= imy= 256;
586         
587         for (i=0; i<me->totface; i++) {
588                 MFace *mf= &((MFace*) me->mface)[i];
589                 TFace *tf= &((TFace*) me->tface)[i];
590                 
591                 if (!(tf->flag & TF_SELECT))
592                         continue;
593                 
594                 if (tf->flag & TF_SEL1) {
595                         cent[0]+= tf->uv[0][0];
596                         cent[1]+= tf->uv[0][1];
597                         nactive++;
598                 }
599                 if (tf->flag & TF_SEL2) {
600                         cent[0]+= tf->uv[1][0];
601                         cent[1]+= tf->uv[1][1];
602                         nactive++;
603                 }
604                 if (tf->flag & TF_SEL3) {
605                         cent[0]+= tf->uv[2][0];
606                         cent[1]+= tf->uv[2][1];
607                         nactive++;
608                 }
609                 if (mf->v4 && (tf->flag & TF_SEL4)) {
610                         cent[0]+= tf->uv[3][0];
611                         cent[1]+= tf->uv[3][1];
612                         nactive++;
613                 }
614         }
615                 
616         if(block) {     // do the buttons
617                 if (nactive) {
618                         ocent[0]= cent[0]/nactive;
619                         ocent[1]= cent[1]/nactive;
620                         if (G.sima->flag & SI_COORDFLOATS) {
621                                 step= 1;
622                                 digits= 3;
623                         }
624                         else {
625                                 ocent[0] *= imx;
626                                 ocent[1] *= imy;
627                                 step= 100;
628                                 digits= 2;
629                         }
630                         
631                         uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
632                         uiBlockBeginAlign(block);
633                         if(nactive==1) {
634                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
635                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
636                         }
637                         else {
638                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:",       10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
639                                 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:",       10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
640                         }
641                         uiBlockEndAlign(block);
642                 }
643         }
644         else {  // apply event
645                 float delta[2];
646                 
647                 cent[0]= cent[0]/nactive;
648                 cent[1]= cent[1]/nactive;
649                         
650                 if (G.sima->flag & SI_COORDFLOATS) {
651                         delta[0]= ocent[0]-cent[0];
652                         delta[1]= ocent[1]-cent[1];
653                 }
654                 else {
655                         delta[0]= ocent[0]/imx - cent[0];
656                         delta[1]= ocent[1]/imy - cent[1];
657                 }
658
659                 for (i=0; i<me->totface; i++) {
660                         MFace *mf= &((MFace*) me->mface)[i];
661                         TFace *tf= &((TFace*) me->tface)[i];
662                 
663                         if (!(tf->flag & TF_SELECT))
664                                 continue;
665                 
666                         if (tf->flag & TF_SEL1) {
667                                 tf->uv[0][0]+= delta[0];
668                                 tf->uv[0][1]+= delta[1];
669                         }
670                         if (tf->flag & TF_SEL2) {
671                                 tf->uv[1][0]+= delta[0];
672                                 tf->uv[1][1]+= delta[1];
673                         }
674                         if (tf->flag & TF_SEL3) {
675                                 tf->uv[2][0]+= delta[0];
676                                 tf->uv[2][1]+= delta[1];
677                         }
678                         if (mf->v4 && (tf->flag & TF_SEL4)) {
679                                 tf->uv[3][0]+= delta[0];
680                                 tf->uv[3][1]+= delta[1];
681                         }
682                 }
683                         
684                 allqueue(REDRAWVIEW3D, 0);
685                 allqueue(REDRAWIMAGE, 0);
686         }
687 }
688
689
690 void do_imagebuts(unsigned short event)
691 {
692         switch(event) {
693         case B_TRANS_IMAGE:
694                 image_editvertex_buts(NULL);
695                 break;
696
697         case B_SIMAGEDRAW:
698                 if(G.f & G_FACESELECT) {
699                         make_repbind(G.sima->image);
700                         image_changed(G.sima, 1);
701                 }
702                 allqueue(REDRAWVIEW3D, 0);
703                 allqueue(REDRAWIMAGE, 0);
704                 break;
705
706         case B_SIMAGEDRAW1:
707                 image_changed(G.sima, 2);               /* 2: only tileflag */
708                 allqueue(REDRAWVIEW3D, 0);
709                 allqueue(REDRAWIMAGE, 0);
710                 break;
711                 
712         case B_TWINANIM:
713                 {
714                         Image *ima;
715                         int nr;
716
717                         ima = G.sima->image;
718                         if (ima) {
719                                 if(ima->flag & IMA_TWINANIM) {
720                                         nr= ima->xrep*ima->yrep;
721                                         if(ima->twsta>=nr) ima->twsta= 1;
722                                         if(ima->twend>=nr) ima->twend= nr-1;
723                                         if(ima->twsta>ima->twend) ima->twsta= 1;
724                                         allqueue(REDRAWIMAGE, 0);
725                                 }
726                         }
727                 }
728                 break;
729         }
730 }
731
732 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
733 {
734         uiBlock *block;
735
736         block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
737         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
738         uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES);  // for close and esc
739         if(uiNewPanel(curarea, block, "Properties", "Image", 10, 230, 318, 204)==0)
740                 return;
741
742         if (G.sima->image && G.sima->image->ibuf) {
743                 char str[32];
744
745                 sprintf(str, "Image: size %d x %d", G.sima->image->ibuf->x, G.sima->image->ibuf->y);
746                 uiDefBut(block, LABEL, B_NOP, str,              10,180,300,19, 0, 0, 0, 0, 0, "");
747
748                 uiBlockBeginAlign(block);
749                 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");
750                 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");
751                 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");
752                 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");
753                 uiBlockEndAlign(block);
754
755                 uiBlockBeginAlign(block);
756                 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");
757                 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");
758                 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");
759                 uiBlockBeginAlign(block);
760         }
761
762         image_editvertex_buts(block);
763 }
764
765 static void image_panel_paint(short cntrl)      // IMAGE_HANDLER_PROPERTIES
766 {
767         extern VPaint Gvp;         /* from vpaint - this was copied from the paint panel*/
768         static float hsv[3], old[3];    // used as temp mem for picker
769         uiBlock *block;
770
771         block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
772         uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
773         uiSetPanelHandler(IMAGE_HANDLER_PAINT);  // for close and esc
774         if(uiNewPanel(curarea, block, "Paint", "Image", 10, 230, 318, 204)==0)
775                 return;
776
777         uiBlockPickerButtons(block, &Gvp.r, hsv, old, 'f', B_NOP);      /* 'f' is for floating panel */
778
779         //      offset= FPICK+2*DPICK+BPICK in interface.c... this goes wrong when defines change
780         uiBlockBeginAlign(block);
781         uiDefButF(block, NUM, B_NOP, "A: ",             180+12+24,160,80,20, &Gvp.a, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
782         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");
783 }
784
785 static void image_blockhandlers(ScrArea *sa)
786 {
787         SpaceImage *sima= sa->spacedata.first;
788         short a;
789
790         /* warning; blocks need to be freed each time, handlers dont remove  */
791         uiFreeBlocksWin(&sa->uiblocks, sa->win);
792         
793         for(a=0; a<SPACE_MAXHANDLER; a+=2) {
794                 switch(sima->blockhandler[a]) {
795
796                 case IMAGE_HANDLER_PROPERTIES:
797                         image_panel_properties(sima->blockhandler[a+1]);
798                         break;
799                 case IMAGE_HANDLER_PAINT:
800                         image_panel_paint(sima->blockhandler[a+1]);
801                         break;          
802                 }
803                 /* clear action value for event */
804                 sima->blockhandler[a+1]= 0;
805         }
806         uiDrawBlocksPanels(sa, 0);
807 }
808
809
810
811 void drawimagespace(ScrArea *sa, void *spacedata)
812 {
813         ImBuf *ibuf= NULL;
814         float col[3];
815         unsigned int *rect;
816         int x1, y1;
817         short sx, sy, dx, dy;
818         
819                 /* If derived data is used then make sure that object
820                  * is up-to-date... might not be the case because updates
821                  * are normally done in drawview and could get here before
822                  * drawing a View3D.
823                  */
824         if (!G.obedit && OBACT && (G.sima->flag&SI_DRAWSHADOW)) {
825                 object_handle_update(OBACT);
826         }
827
828         BIF_GetThemeColor3fv(TH_BACK, col);
829         glClearColor(col[0], col[1], col[2], 0.0);
830         glClear(GL_COLOR_BUFFER_BIT);
831
832         bwin_clear_viewmat(sa->win);    /* clear buttons view */
833         glLoadIdentity();
834         
835         what_image(G.sima);
836         
837         if(G.sima->image) {
838                 if(G.sima->image->ibuf==0) {
839                         load_image(G.sima->image, IB_rect, G.sce, G.scene->r.cfra);
840                 }       
841                 tag_image_time(G.sima->image);
842                 ibuf= G.sima->image->ibuf;
843         }
844         
845         if(ibuf==0 || ibuf->rect==0) {
846                 calc_image_view(G.sima, 'f');
847                 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
848                 cpack(0x404040);
849                 glRectf(0.0, 0.0, 1.0, 1.0);
850                 draw_tfaces();
851         }
852         else {
853                 /* calc location */
854                 x1= (curarea->winx-G.sima->zoom*ibuf->x)/2;
855                 y1= (curarea->winy-G.sima->zoom*ibuf->y)/2;
856         
857                 x1-= G.sima->zoom*G.sima->xof;
858                 y1-= G.sima->zoom*G.sima->yof;
859                 
860                 /* needed for gla draw */
861                 glaDefine2DArea(&curarea->winrct);
862                 glPixelZoom((float)G.sima->zoom, (float)G.sima->zoom);
863                                 
864                 if(G.sima->flag & SI_EDITTILE) {
865                         glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
866                         
867                         glPixelZoom(1.0, 1.0);
868                         
869                         dx= ibuf->x/G.sima->image->xrep;
870                         dy= ibuf->y/G.sima->image->yrep;
871                         sy= (G.sima->curtile / G.sima->image->xrep);
872                         sx= G.sima->curtile - sy*G.sima->image->xrep;
873         
874                         sx*= dx;
875                         sy*= dy;
876                         
877                         calc_image_view(G.sima, 'p');   /* pixel */
878                         myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
879                         
880                         cpack(0x0);
881                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx,  sy,  sx+dx-1,  sy+dy-1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
882                         cpack(0xFFFFFF);
883                         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1,  sy+1,  sx+dx,  sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
884                 }
885                 else if(G.sima->mode==SI_TEXTURE) {
886                         
887                         if(G.sima->image->tpageflag & IMA_TILES) {
888                                 
889                                 /* just leave this a while */
890                                 if(G.sima->image->xrep<1) return;
891                                 if(G.sima->image->yrep<1) return;
892                                 
893                                 if(G.sima->curtile >= G.sima->image->xrep*G.sima->image->yrep) 
894                                         G.sima->curtile = G.sima->image->xrep*G.sima->image->yrep - 1; 
895                                 
896                                 dx= ibuf->x/G.sima->image->xrep;
897                                 dy= ibuf->y/G.sima->image->yrep;
898                                 
899                                 sy= (G.sima->curtile / G.sima->image->xrep);
900                                 sx= G.sima->curtile - sy*G.sima->image->xrep;
901                 
902                                 sx*= dx;
903                                 sy*= dy;
904                                 
905                                 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
906                                 
907                                 /* rect= ibuf->rect; */
908                                 for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
909                                         for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
910                                                 glaDrawPixelsSafe(x1+sx*G.sima->zoom, y1+sy*G.sima->zoom, dx, dy, rect);
911                                         }
912                                 }
913                                 
914                                 MEM_freeN(rect);
915                         }
916                         else 
917                                 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
918                         
919                         glPixelZoom(1.0, 1.0);
920                         
921                         draw_tfaces();
922                 }
923         
924                 calc_image_view(G.sima, 'f');   /* float */
925         }
926
927         draw_image_transform(ibuf);
928
929         myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
930         draw_image_view_icon();
931         draw_area_emboss(sa);
932
933         /* it is important to end a view in a transform compatible with buttons */
934         bwin_scalematrix(sa->win, G.sima->blockscale, G.sima->blockscale, G.sima->blockscale);
935         image_blockhandlers(sa);
936
937         curarea->win_swap= WIN_BACK_OK;
938 }
939
940 static void image_zoom_power_of_two(void)
941 {
942         /* Make zoom a power of 2 */
943
944         G.sima->zoom = 1 / G.sima->zoom;
945         G.sima->zoom = log(G.sima->zoom) / log(2);
946         G.sima->zoom = ceil(G.sima->zoom);
947         G.sima->zoom = pow(2, G.sima->zoom);
948         G.sima->zoom = 1 / G.sima->zoom;
949 }
950
951 static void image_zoom_set_factor(float zoomfac)
952 {
953         SpaceImage *sima= curarea->spacedata.first;
954         int width, height;
955
956         if (zoomfac <= 0.0f)
957                 return;
958
959         sima->zoom *= zoomfac;
960
961         if (sima->zoom > 0.1f && sima->zoom < 4.0f)
962                 return;
963
964         /* check zoom limits */
965
966         calc_image_view(G.sima, 'p');
967         width= 256;
968         height= 256;
969         if (sima->image) {
970                 if (sima->image->ibuf) {
971                         width= sima->image->ibuf->x;
972                         height= sima->image->ibuf->y;
973                 }
974         }
975         width *= sima->zoom;
976         height *= sima->zoom;
977
978         if ((width < 4) && (height < 4))
979                 sima->zoom /= zoomfac;
980         else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
981                 sima->zoom /= zoomfac;
982         else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
983                 sima->zoom /= zoomfac;
984 }
985
986 void image_viewmove(int mode)
987 {
988         short mval[2], mvalo[2], zoom0;
989         
990         getmouseco_sc(mvalo);
991         zoom0= G.sima->zoom;
992         
993         while(get_mbut()&(L_MOUSE|M_MOUSE)) {
994
995                 getmouseco_sc(mval);
996
997                 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
998                 
999                         if(mode==0) {
1000                                 G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
1001                                 G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
1002                         }
1003                         else if (mode==1) {
1004                                 float factor;
1005
1006                                 factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
1007                                 image_zoom_set_factor(factor);
1008                         }
1009
1010                         mvalo[0]= mval[0];
1011                         mvalo[1]= mval[1];
1012                         
1013                         scrarea_do_windraw(curarea);
1014                         screen_swapbuffers();
1015                 }
1016                 else BIF_wait_for_statechange();
1017         }
1018 }
1019
1020 void image_viewzoom(unsigned short event, int invert)
1021 {
1022         SpaceImage *sima= curarea->spacedata.first;
1023
1024         if(event==WHEELDOWNMOUSE || event==PADMINUS)
1025                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
1026         else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
1027                 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
1028         else if(event==PAD1)
1029                 sima->zoom= 1.0;
1030         else if(event==PAD2)
1031                 sima->zoom= (invert)? 2.0: 0.5;
1032         else if(event==PAD4)
1033                 sima->zoom= (invert)? 4.0: 0.25;
1034         else if(event==PAD8)
1035                 sima->zoom= (invert)? 8.0: 0.125;
1036         else
1037                 return;
1038 }
1039
1040 /**
1041  * Updates the fields of the View2D member of the SpaceImage struct.
1042  * Default behavior is to reset the position of the image and set the zoom to 1
1043  * If the image will not fit within the window rectangle, the zoom is adjusted
1044  *
1045  * @return    void
1046  *   
1047  */
1048 void image_home(void)
1049 {
1050         int width, height;
1051         float zoomX, zoomY;
1052
1053         if (curarea->spacetype != SPACE_IMAGE) return;
1054         if ((G.sima->image == 0) || (G.sima->image->ibuf == 0)) return;
1055
1056         /* Check if the image will fit in the image with zoom==1 */
1057         width = curarea->winx;
1058         height = curarea->winy;
1059         if (((G.sima->image->ibuf->x >= width) || (G.sima->image->ibuf->y >= height)) && 
1060                 ((width > 0) && (height > 0))) {
1061                 /* Find the zoom value that will fit the image in the image space */
1062                 zoomX = ((float)width) / ((float)G.sima->image->ibuf->x);
1063                 zoomY = ((float)height) / ((float)G.sima->image->ibuf->y);
1064                 G.sima->zoom= MIN2(zoomX, zoomY);
1065
1066                 image_zoom_power_of_two();
1067         }
1068         else {
1069                 G.sima->zoom= (float)1;
1070         }
1071
1072         G.sima->xof= G.sima->yof= 0;
1073         
1074         calc_image_view(G.sima, 'p');
1075         
1076         scrarea_queue_winredraw(curarea);
1077 }
1078
1079 void image_viewcentre(void)
1080 {
1081         float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
1082
1083         if( is_uv_tface_editing_allowed()==0 ) return;
1084
1085         if (!minmax_tface_uv(min, max)) return;
1086
1087         if(G.sima->image && G.sima->image->ibuf) {
1088                 xim= G.sima->image->ibuf->x;
1089                 yim= G.sima->image->ibuf->y;
1090         }
1091
1092         G.sima->xof= ((min[0] + max[0])*0.5f - 0.5f)*xim;
1093         G.sima->yof= ((min[1] + max[1])*0.5f - 0.5f)*yim;
1094
1095         d[0] = max[0] - min[0];
1096         d[1] = max[1] - min[1];
1097         size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
1098         
1099         if(size<=0.01) size= 0.01;
1100
1101         G.sima->zoom= 0.7/size;
1102
1103         calc_image_view(G.sima, 'p');
1104
1105         scrarea_queue_winredraw(curarea);
1106 }
1107