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