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