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