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