3f257bb848ff9e250ca25de22730b6612c39aa1e
[blender-staging.git] / source / blender / src / editsima.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 <stdlib.h>
34 #include <string.h>
35 #include <math.h>
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #ifndef WIN32
42 #include <unistd.h>
43 #else
44 #include <io.h>
45 #endif   
46 #include "MEM_guardedalloc.h"
47
48 #include "BLI_blenlib.h"
49 #include "BLI_arithb.h"
50
51 #include "IMB_imbuf_types.h"
52 #include "IMB_imbuf.h"
53
54 #include "DNA_image_types.h"
55 #include "DNA_mesh_types.h"
56 #include "DNA_meshdata_types.h"
57 #include "DNA_node_types.h"
58 #include "DNA_object_types.h" // only for uvedit_selectionCB() (struct Object)
59 #include "DNA_packedFile_types.h"
60 #include "DNA_scene_types.h"
61 #include "DNA_space_types.h"
62 #include "DNA_screen_types.h"
63 #include "DNA_texture_types.h"
64 #include "DNA_userdef_types.h"
65 #include "DNA_view3d_types.h"
66
67 #include "BKE_colortools.h"
68 #include "BKE_depsgraph.h"
69 #include "BKE_displist.h"
70 #include "BKE_image.h"
71 #include "BKE_global.h"
72 #include "BKE_library.h"
73 #include "BKE_main.h"
74 #include "BKE_mesh.h"
75 #include "BKE_node.h"
76 #include "BKE_object.h"
77 #include "BKE_packedFile.h"
78 #include "BKE_utildefines.h"
79
80 #include "BIF_gl.h"
81 #include "BIF_glutil.h"
82 #include "BIF_interface.h"
83 #include "BIF_drawimage.h"
84 #include "BIF_editview.h"
85 #include "BIF_editsima.h"
86 #include "BIF_mywindow.h"
87 #include "BIF_previewrender.h"
88 #include "BIF_screen.h"
89 #include "BIF_space.h"
90 #include "BIF_toolbox.h"
91 #include "BIF_transform.h"
92 #include "BIF_writeimage.h"
93
94 #include "BSE_drawipo.h"
95 #include "BSE_edit.h"
96 #include "BSE_filesel.h"
97 #include "BSE_trans_types.h"
98
99 #include "BDR_editobject.h"
100 #include "BDR_unwrapper.h"
101
102 #include "BMF_Api.h"
103
104 #include "RE_pipeline.h"
105
106 #include "blendef.h"
107 #include "multires.h"
108 #include "mydevice.h"
109
110 /* local prototypes */
111 void clever_numbuts_sima(void);
112 void sel_uvco_inside_radius(short , MTFace *, int , float *, float *, short);
113 void uvedit_selectionCB(short , Object *, short *, float ); /* used in edit.c*/ 
114
115 void object_uvs_changed(Object *ob)
116 {
117         DAG_object_flush_update(G.scene, ob, OB_RECALC_DATA);
118
119         allqueue(REDRAWVIEW3D, 0);
120         allqueue(REDRAWIMAGE, 0);
121 }
122
123 void object_tface_flags_changed(Object *ob, int updateButtons)
124 {
125         if (updateButtons) allqueue(REDRAWBUTSEDIT, 0);
126         allqueue(REDRAWVIEW3D, 0);
127         allqueue(REDRAWIMAGE, 0);
128 }
129
130 int is_uv_tface_editing_allowed_silent(void)
131 {
132         Mesh *me;
133
134         if(G.obedit) return 0;
135         if(G.sima->mode!=SI_TEXTURE) return 0;
136         if(!(G.f & G_FACESELECT)) return 0;  
137         me= get_mesh(OBACT);
138         if(me==0 || me->mtface==0) return 0;
139         if(multires_level1_test()) return 0;
140         
141         return 1;
142 }
143
144 int is_uv_tface_editing_allowed(void)
145 {
146         if(G.obedit) error("Unable to perform action in Edit Mode");
147
148         return is_uv_tface_editing_allowed_silent();
149 }
150
151 void get_connected_limit_tface_uv(float *limit)
152 {
153         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
154         if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
155                 limit[0]= 0.05/(float)ibuf->x;
156                 limit[1]= 0.05/(float)ibuf->y;
157         }
158         else
159                 limit[0]= limit[1]= 0.05/256.0;
160 }
161
162 void clever_numbuts_sima(void)
163 {
164         float ocent[2], cent[2]= {0.0, 0.0};
165         int imx= 256, imy= 256;
166         int i, nactive= 0;
167         Mesh *me;
168         
169         if( is_uv_tface_editing_allowed()==0 ) return;
170         me= get_mesh(OBACT);
171         
172         if (G.sima->image) {
173                 ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
174                 if(ibuf) {
175                         imx= ibuf->x;
176                         imy= ibuf->y;
177                 }
178         }
179         
180         for (i=0; i<me->totface; i++) {
181                 MFace *mf= &((MFace*) me->mface)[i];
182                 MTFace *tf= &((MTFace*) me->mtface)[i];
183                 
184                 if (!(mf->flag & ME_FACE_SEL))
185                         continue;
186                 
187                 if (tf->flag & TF_SEL1) {
188                         cent[0]+= tf->uv[0][0];
189                         cent[1]+= tf->uv[0][1];
190                         nactive++;
191                 }
192                 if (tf->flag & TF_SEL2) {
193                         cent[0]+= tf->uv[1][0];
194                         cent[1]+= tf->uv[1][1];
195                         nactive++;
196                 }
197                 if (tf->flag & TF_SEL3) {
198                         cent[0]+= tf->uv[2][0];
199                         cent[1]+= tf->uv[2][1];
200                         nactive++;
201                 }
202                 if (mf->v4 && (tf->flag & TF_SEL4)) {
203                         cent[0]+= tf->uv[3][0];
204                         cent[1]+= tf->uv[3][1];
205                         nactive++;
206                 }
207         }
208         
209         if (nactive) {
210                 cent[0]= (cent[0]*imx)/nactive;
211                 cent[1]= (cent[1]*imy)/nactive;
212
213                 add_numbut(0, NUM|FLO, "LocX:", -imx*20, imx*20, &cent[0], NULL);
214                 add_numbut(1, NUM|FLO, "LocY:", -imy*20, imy*20, &cent[1], NULL);
215                 
216                 ocent[0]= cent[0];
217                 ocent[1]= cent[1];
218                 if (do_clever_numbuts((nactive==1)?"Active Vertex":"Selected Center", 2, REDRAW)) {
219                         float delta[2];
220                         
221                         delta[0]= (cent[0]-ocent[0])/imx;
222                         delta[1]= (cent[1]-ocent[1])/imy;
223
224                         for (i=0; i<me->totface; i++) {
225                                 MFace *mf= &((MFace*) me->mface)[i];
226                                 MTFace *tf= &((MTFace*) me->mtface)[i];
227                         
228                                 if (!(mf->flag & ME_FACE_SEL))
229                                         continue;
230                         
231                                 if (tf->flag & TF_SEL1) {
232                                         tf->uv[0][0]+= delta[0];
233                                         tf->uv[0][1]+= delta[1];
234                                 }
235                                 if (tf->flag & TF_SEL2) {
236                                         tf->uv[1][0]+= delta[0];
237                                         tf->uv[1][1]+= delta[1];
238                                 }
239                                 if (tf->flag & TF_SEL3) {
240                                         tf->uv[2][0]+= delta[0];
241                                         tf->uv[2][1]+= delta[1];
242                                 }
243                                 if (mf->v4 && (tf->flag & TF_SEL4)) {
244                                         tf->uv[3][0]+= delta[0];
245                                         tf->uv[3][1]+= delta[1];
246                                 }
247                         }
248                         
249                         object_uvs_changed(OBACT);
250                 }
251         }
252 }
253
254 void be_square_tface_uv(Mesh *me)
255 {
256         MTFace *tface;
257         MFace *mface;
258         int a;
259         
260         /* if 1 vertex selected: doit (with the selected vertex) */
261         mface= (MFace*)me->mface;
262         tface= (MTFace*)me->mtface;
263         for(a=me->totface; a>0; a--, tface++, mface++) {
264                 if(mface->v4) {
265                         if(mface->flag & ME_FACE_SEL) {
266                                 if(tface->flag & TF_SEL1) {
267                                         if( tface->uv[1][0] == tface->uv[2][0] ) {
268                                                 tface->uv[1][1]= tface->uv[0][1];
269                                                 tface->uv[3][0]= tface->uv[0][0];
270                                         }
271                                         else {  
272                                                 tface->uv[1][0]= tface->uv[0][0];
273                                                 tface->uv[3][1]= tface->uv[0][1];
274                                         }
275                                         
276                                 }
277                                 if(tface->flag & TF_SEL2) {
278                                         if( tface->uv[2][1] == tface->uv[3][1] ) {
279                                                 tface->uv[2][0]= tface->uv[1][0];
280                                                 tface->uv[0][1]= tface->uv[1][1];
281                                         }
282                                         else {
283                                                 tface->uv[2][1]= tface->uv[1][1];
284                                                 tface->uv[0][0]= tface->uv[1][0];
285                                         }
286
287                                 }
288                                 if(tface->flag & TF_SEL3) {
289                                         if( tface->uv[3][0] == tface->uv[0][0] ) {
290                                                 tface->uv[3][1]= tface->uv[2][1];
291                                                 tface->uv[1][0]= tface->uv[2][0];
292                                         }
293                                         else {
294                                                 tface->uv[3][0]= tface->uv[2][0];
295                                                 tface->uv[1][1]= tface->uv[2][1];
296                                         }
297                                 }
298                                 if(tface->flag & TF_SEL4) {
299                                         if( tface->uv[0][1] == tface->uv[1][1] ) {
300                                                 tface->uv[0][0]= tface->uv[3][0];
301                                                 tface->uv[2][1]= tface->uv[3][1];
302                                         }
303                                         else  {
304                                                 tface->uv[0][1]= tface->uv[3][1];
305                                                 tface->uv[2][0]= tface->uv[3][0];
306                                         }
307
308                                 }
309                         }
310                 }
311         }
312 }
313
314 void transform_aspect_ratio_tface_uv(float *aspx, float *aspy)
315 {
316         int w, h;
317
318         transform_width_height_tface_uv(&w, &h);
319         *aspx= (float)w/256.0f;
320         *aspy= (float)h/256.0f;
321 }
322
323 void transform_width_height_tface_uv(int *width, int *height)
324 {
325         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
326
327         if(ibuf) {
328                 *width= ibuf->x;
329                 *height= ibuf->y;
330         }
331         else {
332                 *width= 256;
333                 *height= 256;
334         }
335 }
336
337 void mirror_tface_uv(char mirroraxis)
338 {
339         if (mirroraxis == 'x')
340                 Mirror(1); /* global x */
341         else if (mirroraxis == 'y')
342                 Mirror(2); /* global y */
343 }
344
345 void mirrormenu_tface_uv(void)
346 {
347         short mode= 0;
348
349         if( is_uv_tface_editing_allowed()==0 ) return;
350
351         mode= pupmenu("Mirror%t|X Axis%x1|Y Axis%x2|");
352
353         if(mode==-1) return;
354
355         if(mode==1) mirror_tface_uv('x');
356         else if(mode==2) mirror_tface_uv('y');
357
358         BIF_undo_push("Mirror UV");
359 }
360
361 void weld_align_tface_uv(char tool)
362 {
363         MFace *mface;
364         MTFace *tface;
365         Mesh *me;
366         float min[2], max[2], cent[2];
367         int a;
368         
369         if( is_uv_tface_editing_allowed()==0 ) return;
370         me= get_mesh(OBACT);
371
372         if (!minmax_tface_uv(min, max))
373                 return;
374
375         cent[0]= (min[0]+max[0])/2.0;
376         cent[1]= (min[1]+max[1])/2.0;
377
378         if(tool == 'x' || tool == 'w') {
379                 tface= me->mtface;
380                 mface= me->mface;
381                 for(a=me->totface; a>0; a--, tface++, mface++) {
382                         if(mface->flag & ME_FACE_SEL) {
383                                 if(tface->flag & TF_SEL1)
384                                         tface->uv[0][0]= cent[0];
385                                 if(tface->flag & TF_SEL2)
386                                         tface->uv[1][0]= cent[0];
387                                 if(tface->flag & TF_SEL3)
388                                         tface->uv[2][0]= cent[0];
389                                 if(mface->v4 && (tface->flag & TF_SEL4))
390                                         tface->uv[3][0]= cent[0];
391                         }
392                 }
393         }
394
395         if(tool == 'y' || tool == 'w') {
396                 tface= me->mtface;
397                 mface= me->mface;
398                 for(a=me->totface; a>0; a--, tface++, mface++) {
399                         if(mface->flag & ME_FACE_SEL) {
400                                 if(tface->flag & TF_SEL1)
401                                         tface->uv[0][1]= cent[1];
402                                 if(tface->flag & TF_SEL2)
403                                         tface->uv[1][1]= cent[1];
404                                 if(tface->flag & TF_SEL3)
405                                         tface->uv[2][1]= cent[1];
406                                 if(mface->v4 && (tface->flag & TF_SEL4))
407                                         tface->uv[3][1]= cent[1];
408                         }
409                 }
410         }
411
412         object_uvs_changed(OBACT);
413 }
414
415 void weld_align_menu_tface_uv(void)
416 {
417         short mode= 0;
418
419         if( is_uv_tface_editing_allowed()==0 ) return;
420
421         mode= pupmenu("Weld/Align%t|Weld%x1|Align X%x2|Align Y%x3|");
422
423         if(mode==-1) return;
424
425         if(mode==1) weld_align_tface_uv('w');
426         else if(mode==2) weld_align_tface_uv('x');
427         else if(mode==3) weld_align_tface_uv('y');
428
429         if(mode==1) BIF_undo_push("Weld UV");
430         else if(mode==2 || mode==3) BIF_undo_push("Align UV");
431 }
432
433 void select_invert_tface_uv(void)
434 {
435         Mesh *me;
436         MTFace *tface;
437         MFace *mface;
438         int a;
439         
440         if( is_uv_tface_editing_allowed()==0 ) return;
441         me= get_mesh(OBACT);
442         mface= me->mface;
443
444         for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
445                 if(mface->flag & ME_FACE_SEL) { 
446                         tface->flag ^= TF_SEL1;
447                         tface->flag ^= TF_SEL2;
448                         tface->flag ^= TF_SEL3;
449                         if(mface->v4) tface->flag ^= TF_SEL4;
450                 }
451         }
452
453         BIF_undo_push("Select Inverse UV");
454
455         allqueue(REDRAWIMAGE, 0);
456 }
457
458 void select_swap_tface_uv(void)
459 {
460         Mesh *me;
461         MTFace *tface;
462         MFace *mface;
463         int a, sel=0;
464         
465         if( is_uv_tface_editing_allowed()==0 ) return;
466         me= get_mesh(OBACT);
467
468         mface= me->mface;
469         for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
470                 if(mface->flag & ME_FACE_SEL) { 
471                         if(tface->flag & (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4)) {
472                                 sel= 1;
473                                 break;
474                         }
475                 }
476         }
477         
478         mface= me->mface;
479         for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
480                 if(mface->flag & ME_FACE_SEL) {
481                         if(mface->v4) {
482                                 if(sel) tface->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
483                                 else tface->flag |= (TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
484                         }
485                         else {
486                                 if(sel) tface->flag &= ~(TF_SEL1+TF_SEL2+TF_SEL3+TF_SEL4);
487                                 else tface->flag |= (TF_SEL1+TF_SEL2+TF_SEL3);
488                         }
489                 }
490         }
491         
492         BIF_undo_push("Select swap UV");
493
494         allqueue(REDRAWIMAGE, 0);
495 }
496
497 static int msel_hit(float *limit, unsigned int *hitarray, unsigned int vertexid, float **uv, float *uv2, int sticky)
498 {
499         int i;
500         for(i=0; i< 4; i++) {
501                 if(hitarray[i] == vertexid) {
502                         if(sticky == 2) {
503                                 if(fabs(uv[i][0]-uv2[0]) < limit[0] &&
504                             fabs(uv[i][1]-uv2[1]) < limit[1])
505                                         return 1;
506                         }
507                         else return 1;
508                 }
509         }
510         return 0;
511 }
512
513 static void find_nearest_tface(MTFace **nearesttf, MFace **nearestmf)
514 {
515         Mesh *me;
516         MTFace *tf;
517         MFace *mf;
518         int a, i, nverts, mindist, dist, fcenter[2], uval[2];
519         short mval[2];
520
521         getmouseco_areawin(mval);       
522
523         mindist= 0x7FFFFFF;
524         *nearesttf= NULL;
525         *nearestmf= NULL;
526
527         me= get_mesh(OBACT);
528         mf= (MFace*)me  ->mface;
529         tf= (MTFace*)me->mtface;
530
531         for(a=me->totface; a>0; a--, tf++, mf++) {
532                 if(mf->flag & ME_FACE_SEL) {
533
534                         fcenter[0]= fcenter[1]= 0;
535                         nverts= mf->v4? 4: 3;
536                         for(i=0; i<nverts; i++) {
537                                 uvco_to_areaco_noclip(tf->uv[i], uval);
538                                 fcenter[0] += uval[0];
539                                 fcenter[1] += uval[1];
540                         }
541
542                         fcenter[0] /= nverts;
543                         fcenter[1] /= nverts;
544
545                         dist= abs(mval[0]- fcenter[0])+ abs(mval[1]- fcenter[1]);
546                         if (dist < mindist) {
547                                 *nearesttf= tf;
548                                 *nearestmf= mf;
549                                 mindist= dist;
550                         }
551                 }
552         }
553 }
554
555 static int nearest_uv_between(MTFace *tf, int nverts, int id, short *mval, int *uval)
556 {
557         float m[3], v1[3], v2[3], c1, c2;
558         int id1, id2;
559
560         id1= (id+nverts-1)%nverts;
561         id2= (id+nverts+1)%nverts;
562
563         m[0] = (float)(mval[0]-uval[0]);
564         m[1] = (float)(mval[1]-uval[1]);
565         Vec2Subf(v1, tf->uv[id1], tf->uv[id]);
566         Vec2Subf(v2, tf->uv[id2], tf->uv[id]);
567
568         /* m and v2 on same side of v-v1? */
569         c1= v1[0]*m[1] - v1[1]*m[0];
570         c2= v1[0]*v2[1] - v1[1]*v2[0];
571
572         if (c1*c2 < 0.0f)
573                 return 0;
574
575         /* m and v1 on same side of v-v2? */
576         c1= v2[0]*m[1] - v2[1]*m[0];
577         c2= v2[0]*v1[1] - v2[1]*v1[0];
578
579         return (c1*c2 >= 0.0f);
580 }
581
582 static void find_nearest_uv(MTFace **nearesttf, unsigned int *nearestv, int *nearestuv)
583 {
584         Mesh *me;
585         MTFace *tf;
586         MFace *mf;
587         int a, i, nverts, mindist, dist, uval[2];
588         short mval[2];
589
590         getmouseco_areawin(mval);       
591
592         mindist= 0x7FFFFFF;
593         *nearesttf= NULL;
594
595         me= get_mesh(OBACT);
596         mf= (MFace*)me->mface;
597         tf= (MTFace*)me->mtface;
598
599         for(a=me->totface; a>0; a--, tf++, mf++) {
600                 if(mf->flag & ME_FACE_SEL) {
601
602                         nverts= mf->v4? 4: 3;
603                         for(i=0; i<nverts; i++) {
604                                 uvco_to_areaco_noclip(tf->uv[i], uval);
605                                 dist= abs(mval[0]-uval[0]) + abs(mval[1]-uval[1]);
606
607                                 if(tf->flag & TF_SEL_MASK(i))
608                                         dist += 5;
609
610                                 if(dist<=mindist) {
611                                         if(dist==mindist)
612                                                 if (!nearest_uv_between(tf, nverts, i, mval, uval))
613                                                         continue;
614
615                                         mindist= dist; 
616
617                                         *nearesttf= tf;
618                                         *nearestuv= i;
619
620                                         if (i==0) *nearestv=  mf->v1;
621                                         else if (i==1) *nearestv=  mf->v2;
622                                         else if (i==2) *nearestv=  mf->v3;
623                                         else *nearestv=  mf->v4;
624                                 }
625                         }
626                 }
627         }
628 }
629
630 void mouse_select_sima(void)
631 {
632         Mesh *me;
633         MTFace *tf, *nearesttf;
634         MFace *mf, *nearestmf=NULL;
635         int a, selectsticky, sticky, actface, nearestuv, i;
636         unsigned int hitv[4], nearestv;
637         float *hituv[4], limit[2];
638         
639         if( is_uv_tface_editing_allowed()==0 ) return;
640         me= get_mesh(OBACT);
641
642         get_connected_limit_tface_uv(limit);
643         actface= (G.qual & LR_ALTKEY || G.sima->flag & SI_SELACTFACE);
644
645         if(G.qual & LR_CTRLKEY) {
646                 if(G.sima->flag & SI_STICKYUVS) sticky= 0;
647                 else sticky= 1;
648         }
649         else {
650                 if(G.sima->flag & SI_STICKYUVS) sticky= 1;
651                 else if(G.sima->flag & SI_LOCALSTICKY) sticky= 2;
652                 else sticky= 0;
653         }
654
655         if(actface) {
656                 find_nearest_tface(&nearesttf, &nearestmf);
657                 if(nearesttf==NULL)
658                         return;
659
660                 nearesttf->flag |= TF_ACTIVE;
661
662                 for (i=0; i<4; i++)
663                         hituv[i]= nearesttf->uv[i];
664
665                 hitv[0]= nearestmf->v1;
666                 hitv[1]= nearestmf->v2;
667                 hitv[2]= nearestmf->v3;
668                 hitv[3]= nearestmf->v4? nearestmf->v4: 0xFFFFFFFF;
669         }
670         else {
671                 find_nearest_uv(&nearesttf, &nearestv, &nearestuv);
672                 if(nearesttf==NULL)
673                         return;
674
675                 if(sticky) {
676                         for(i=0; i<4; i++)
677                                 hitv[i]= 0xFFFFFFFF;
678                         hitv[nearestuv]= nearestv;
679                         hituv[nearestuv]= nearesttf->uv[nearestuv];
680                 }
681         }
682
683         if(G.qual & LR_SHIFTKEY) {
684                 /* (de)select face */
685                 if(actface) {
686                         if(!(~nearesttf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
687                            && (!nearestmf->v4 || nearesttf->flag & TF_SEL4)) {
688                                 nearesttf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
689                                 selectsticky= 0;
690                         }
691                         else {
692                                 nearesttf->flag |= TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4;
693                                 selectsticky= 1;
694                         }
695                 }
696                 /* (de)select uv node */
697                 else {
698                         if(nearesttf->flag & TF_SEL_MASK(nearestuv)) {
699                                 nearesttf->flag &= ~TF_SEL_MASK(nearestuv);
700                                 selectsticky= 0;
701                         }
702                         else {
703                                 nearesttf->flag |= TF_SEL_MASK(nearestuv);
704                                 selectsticky= 1;
705                         }
706                 }
707
708                 /* (de)select sticky uv nodes */
709                 if(sticky || actface) {
710                         mf= (MFace*)me->mface;
711                         tf= (MTFace*)me->mtface;
712                         /* deselect */
713                         if(selectsticky==0) {
714                                 for(a=me->totface; a>0; a--, tf++, mf++) {
715                                         if(!(mf->flag & ME_FACE_SEL)) continue;
716                                         if(nearesttf && tf!=nearesttf) tf->flag &=~ TF_ACTIVE;
717                                         if (!sticky) continue;
718
719                                         if(msel_hit(limit, hitv, mf->v1, hituv, tf->uv[0], sticky))
720                                                 tf->flag &= ~TF_SEL1;
721                                         if(msel_hit(limit, hitv, mf->v2, hituv, tf->uv[1], sticky))
722                                                 tf->flag &= ~TF_SEL2;
723                                         if(msel_hit(limit, hitv, mf->v3, hituv, tf->uv[2], sticky))
724                                                 tf->flag &= ~TF_SEL3;
725                                         if (mf->v4)
726                                                 if(msel_hit(limit, hitv, mf->v4, hituv, tf->uv[3], sticky))
727                                                         tf->flag &= ~TF_SEL4;
728                                 }
729                         }
730                         /* select */
731                         else {
732                                 for(a=me->totface; a>0; a--, tf++, mf++) {
733                                         if(!(mf->flag & ME_FACE_SEL)) continue;
734                                         if(nearesttf && tf!=nearesttf)
735                                                 tf->flag &=~ TF_ACTIVE;
736                                         if (!sticky) continue;
737
738                                         if(msel_hit(limit, hitv, mf->v1, hituv, tf->uv[0], sticky))
739                                                 tf->flag |= TF_SEL1;
740                                         if(msel_hit(limit, hitv, mf->v2, hituv, tf->uv[1], sticky))
741                                                 tf->flag |= TF_SEL2;
742                                         if(msel_hit(limit, hitv, mf->v3, hituv, tf->uv[2], sticky))
743                                                 tf->flag |= TF_SEL3;
744                                         if (mf->v4)
745                                                 if(msel_hit(limit, hitv, mf->v4, hituv, tf->uv[3], sticky))
746                                                         tf->flag |= TF_SEL4;
747                                 }
748                         }
749                 }
750         }
751         else {
752                 /* select face and deselect other faces */ 
753                 if(actface) {
754                         mf= (MFace*)me->mface;
755                         tf= (MTFace*)me->mtface;
756                         for(a=me->totface; a>0; a--, tf++, mf++) {
757                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
758                                 if(nearesttf && tf!=nearesttf)
759                                         tf->flag &= ~TF_ACTIVE;
760                         }
761                         if(nearesttf)
762                                 nearesttf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
763                 }
764
765                 /* deselect uvs, and select sticky uvs */
766                 mf= (MFace*)me->mface;
767                 tf= (MTFace*)me->mtface;
768                 for(a=me->totface; a>0; a--, tf++, mf++) {
769                         if(mf->flag & ME_FACE_SEL) {
770                                 if(!actface) tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
771                                 if(!sticky) continue;
772
773                                 if(msel_hit(limit, hitv, mf->v1, hituv, tf->uv[0], sticky))
774                                         tf->flag |= TF_SEL1;
775                                 if(msel_hit(limit, hitv, mf->v2, hituv, tf->uv[1], sticky))
776                                         tf->flag |= TF_SEL2;
777                                 if(msel_hit(limit, hitv, mf->v3, hituv, tf->uv[2], sticky))
778                                         tf->flag |= TF_SEL3;
779                                 if(mf->v4)
780                                         if(msel_hit(limit, hitv, mf->v4, hituv, tf->uv[3], sticky))
781                                                 tf->flag |= TF_SEL4;
782                         }
783                 }
784                 
785                 if(!actface) 
786                         nearesttf->flag |= TF_SEL_MASK(nearestuv);
787         }
788         
789         force_draw(1);
790         
791         BIF_undo_push("Select UV");
792         rightmouse_transform();
793 }
794
795 void borderselect_sima(short whichuvs)
796 {
797         Mesh *me;
798         MTFace *tface;
799         MFace *mface;
800         rcti rect;
801         rctf rectf;
802         int a, val;
803         short mval[2];
804
805         if( is_uv_tface_editing_allowed()==0 ) return;
806         me= get_mesh(OBACT);
807
808         val= get_border(&rect, 3);
809
810         if(val) {
811                 mval[0]= rect.xmin;
812                 mval[1]= rect.ymin;
813                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmin, &rectf.ymin);
814                 mval[0]= rect.xmax;
815                 mval[1]= rect.ymax;
816                 areamouseco_to_ipoco(G.v2d, mval, &rectf.xmax, &rectf.ymax);
817
818                 mface= me->mface;
819                 for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
820                 
821                         if(mface->flag & ME_FACE_SEL) {
822                                 
823                                 if (whichuvs == UV_SELECT_ALL) {
824                                 
825                                         if(BLI_in_rctf(&rectf, (float)tface->uv[0][0], (float)tface->uv[0][1])) {
826                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL1;
827                                                 else tface->flag &= ~TF_SEL1;
828                                         }
829                                         if(BLI_in_rctf(&rectf, (float)tface->uv[1][0], (float)tface->uv[1][1])) {
830                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL2;
831                                                 else tface->flag &= ~TF_SEL2;
832                                         }
833                                         if(BLI_in_rctf(&rectf, (float)tface->uv[2][0], (float)tface->uv[2][1])) {
834                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL3;
835                                                 else tface->flag &= ~TF_SEL3;
836                                         }
837                                         if(mface->v4 && BLI_in_rctf(&rectf, (float)tface->uv[3][0], (float)tface->uv[3][1])) {
838                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL4;
839                                                 else tface->flag &= ~TF_SEL4;
840                                         }
841                                 } else if (whichuvs == UV_SELECT_PINNED) {
842                                         if ((tface->unwrap & TF_PIN1) && 
843                                                 BLI_in_rctf(&rectf, (float)tface->uv[0][0], (float)tface->uv[0][1])) {
844                                                 
845                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL1;
846                                                 else tface->flag &= ~TF_SEL1;
847                                         }
848                                         if ((tface->unwrap & TF_PIN2) && 
849                                                 BLI_in_rctf(&rectf, (float)tface->uv[1][0], (float)tface->uv[1][1])) {
850                                                 
851                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL2;
852                                                 else tface->flag &= ~TF_SEL2;
853                                         }
854                                         if ((tface->unwrap & TF_PIN3) && 
855                                                 BLI_in_rctf(&rectf, (float)tface->uv[2][0], (float)tface->uv[2][1])) {
856                                                 
857                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL3;
858                                                 else tface->flag &= ~TF_SEL3;
859                                         }
860                                         if ((mface->v4) && (tface->unwrap & TF_PIN4) && BLI_in_rctf(&rectf, (float)tface->uv[3][0], (float)tface->uv[3][1])) {
861                                                 if(val==LEFTMOUSE) tface->flag |= TF_SEL4;
862                                                 else tface->flag &= ~TF_SEL4;
863                                         }
864                                 }
865                         }
866                                                         
867                 }
868                 BIF_undo_push("Border select UV");
869                 scrarea_queue_winredraw(curarea);
870         }
871 }
872
873 /** This is an ugly function to set the Tface selection flags depending
874   * on whether its UV coordinates are inside the normalized 
875   * area with radius rad and offset offset. These coordinates must be
876   * normalized to 1.0 
877   * Just for readability...
878   */
879
880 void sel_uvco_inside_radius(short sel, MTFace *tface, int index, float *offset, float *ell, short select_mask)
881 {
882         // normalized ellipse: ell[0] = scaleX,
883         //                        [1] = scaleY
884
885         float *uv = tface->uv[index];
886         float x, y, r2;
887
888         x = (uv[0] - offset[0]) * ell[0];
889         y = (uv[1] - offset[1]) * ell[1];
890
891         r2 = x * x + y * y;
892         if (r2 < 1.0) {
893                 if (sel == LEFTMOUSE) tface->flag |= select_mask;
894                 else tface->flag &= ~select_mask;
895         }
896 }
897
898 // see below:
899 /** gets image dimensions of the 2D view 'v' */
900 static void getSpaceImageDimension(SpaceImage *sima, float *xy)
901 {
902         ImBuf *ibuf= BKE_image_get_ibuf(sima->image, &sima->iuser);
903         float z;
904
905         z = sima->zoom;
906
907         if (ibuf) {
908                 xy[0] = ibuf->x * z;
909                 xy[1] = ibuf->y * z;
910         } else {
911                 xy[0] = 256 * z;
912                 xy[1] = 256 * z;
913         }
914 }
915
916 /** Callback function called by circle_selectCB to enable 
917   * brush select in UV editor.
918   */
919
920 void uvedit_selectionCB(short selecting, Object *editobj, short *mval, float rad) 
921 {
922         float offset[2];
923         Mesh *me;
924         MFace *mface;
925         MTFace *tface;
926         int i;
927
928         float ellipse[2]; // we need to deal with ellipses, as
929                           // non square textures require for circle
930                                           // selection. this ellipse is normalized; r = 1.0
931         
932         me = get_mesh(editobj);
933
934         getSpaceImageDimension(curarea->spacedata.first, ellipse);
935         ellipse[0] /= rad;
936         ellipse[1] /= rad;
937
938         areamouseco_to_ipoco(G.v2d, mval, &offset[0], &offset[1]);
939
940         mface= me->mface;
941         tface= me->mtface;
942
943         if (selecting) {
944                 for(i = 0; i < me->totface; i++) {
945                         sel_uvco_inside_radius(selecting, tface, 0, offset, ellipse, TF_SEL1);
946                         sel_uvco_inside_radius(selecting, tface, 1, offset, ellipse, TF_SEL2);
947                         sel_uvco_inside_radius(selecting, tface, 2, offset, ellipse, TF_SEL3);
948                         if (mface->v4)
949                                 sel_uvco_inside_radius(selecting, tface, 3, offset, ellipse, TF_SEL4);
950                         
951                         tface++; mface++;
952
953                 }
954
955                 if(G.f & G_DRAWFACES) { /* full redraw only if necessary */
956                         draw_sel_circle(0, 0, 0, 0, 0); /* signal */
957                         force_draw(0);
958                 }
959                 else { /* force_draw() is no good here... */
960                         glDrawBuffer(GL_FRONT);
961                         draw_tfaces();
962                         bglFlush();
963                         glDrawBuffer(GL_BACK);
964                 }
965         }       
966 }
967
968
969 void mouseco_to_curtile(void)
970 {
971         float fx, fy;
972         short mval[2];
973         
974         if( is_uv_tface_editing_allowed()==0) return;
975
976         if(G.sima->image && G.sima->image->tpageflag & IMA_TILES) {
977                 
978                 G.sima->flag |= SI_EDITTILE;
979                 
980                 while(get_mbut()&L_MOUSE) {
981                         
982                         calc_image_view(G.sima, 'f');
983                         
984                         getmouseco_areawin(mval);
985                         areamouseco_to_ipoco(G.v2d, mval, &fx, &fy);
986
987                         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
988                         
989                                 fx= (fx)*G.sima->image->xrep;
990                                 fy= (fy)*G.sima->image->yrep;
991                                 
992                                 mval[0]= fx;
993                                 mval[1]= fy;
994                                 
995                                 G.sima->curtile= mval[1]*G.sima->image->xrep + mval[0];
996                         }
997
998                         scrarea_do_windraw(curarea);
999                         screen_swapbuffers();
1000                 }
1001                 
1002                 G.sima->flag &= ~SI_EDITTILE;
1003
1004                 image_changed(G.sima, 1);
1005
1006                 allqueue(REDRAWVIEW3D, 0);
1007                 scrarea_queue_winredraw(curarea);
1008         }
1009 }
1010
1011 void hide_tface_uv(int swap)
1012 {
1013         Mesh *me;
1014         MTFace *tface;
1015         MFace *mface;
1016         int a;
1017
1018         if( is_uv_tface_editing_allowed()==0 ) return;
1019         me= get_mesh(OBACT);
1020
1021         if(swap) {
1022                 mface= me->mface;
1023                 for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
1024                         if(mface->flag & ME_FACE_SEL) {
1025                                 if((tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))==0) {
1026                                         if(!mface->v4)
1027                                                 mface->flag &= ~ME_FACE_SEL;
1028                                         else if(!(tface->flag & TF_SEL4))
1029                                                 mface->flag &= ~ME_FACE_SEL;
1030                                 }
1031                         }
1032                 }
1033         } else {
1034                 mface= me->mface;
1035                 for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
1036                         if(mface->flag & ME_FACE_SEL) {
1037                                 if(tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1038                                                 mface->flag &= ~ME_FACE_SEL;
1039                                 else if(mface->v4 && tface->flag & TF_SEL4)
1040                                                 mface->flag &= ~ME_FACE_SEL;
1041                         }
1042                 }
1043         }
1044
1045         BIF_undo_push("Hide UV");
1046
1047         object_tface_flags_changed(OBACT, 0);
1048 }
1049
1050 void reveal_tface_uv(void)
1051 {
1052         Mesh *me;
1053         MTFace *tface;
1054         MFace *mface;
1055         int a;
1056
1057         if( is_uv_tface_editing_allowed()==0 ) return;
1058         me= get_mesh(OBACT);
1059
1060         mface= me->mface;
1061         for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
1062                 if(!(mface->flag & ME_HIDE)) {
1063                         if(!(mface->flag & ME_FACE_SEL)) {
1064                                 mface->flag |= ME_FACE_SEL;
1065                                 tface->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1066                         }
1067                 }
1068         }
1069         
1070         BIF_undo_push("Reveal UV");
1071
1072         object_tface_flags_changed(OBACT, 0);
1073 }
1074
1075 void stitch_uv_tface(int mode)
1076 {
1077         Mesh *me;
1078         MTFace *tf;
1079         int a, vtot;
1080         float newuv[2], limit[2];
1081         UvMapVert *vlist, *iterv, *v;
1082         UvVertMap *vmap;
1083         
1084         if(is_uv_tface_editing_allowed()==0)
1085                 return;
1086
1087         limit[0]= limit[1]= 20.0;
1088         if(mode==1) {
1089                 add_numbut(0, NUM|FLO, "Limit:", 0.1, 1000.0, &limit[0], NULL);
1090                 if (!do_clever_numbuts("Stitch UVs", 1, REDRAW))
1091                         return;
1092         }
1093
1094         limit[0]= limit[1]= limit[0]/256.0;
1095         if(G.sima->image) {
1096                 ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
1097
1098                 if(ibuf && ibuf->x > 0 && ibuf->y > 0) {
1099                         limit[1]= limit[0]/(float)ibuf->y;
1100                         limit[0]= limit[0]/(float)ibuf->x;
1101                 }
1102         }
1103
1104         me= get_mesh(OBACT);
1105         tf= me->mtface;
1106
1107         vmap= make_uv_vert_map(me->mface, tf, me->totface, me->totvert, 1, limit);
1108         if(vmap == NULL)
1109                 return;
1110
1111         if(mode==0) {
1112                 for(a=0; a<me->totvert; a++) {
1113                         v = get_uv_map_vert(vmap, a);
1114
1115                         if(v == NULL)
1116                                 continue;
1117
1118                         newuv[0]= 0; newuv[1]= 0;
1119                         vtot= 0;
1120
1121                         for(iterv=v; iterv; iterv=iterv->next) {
1122                                 if (tf[iterv->f].flag & TF_SEL_MASK(iterv->tfindex)) {
1123                                         newuv[0] += tf[iterv->f].uv[iterv->tfindex][0];
1124                                         newuv[1] += tf[iterv->f].uv[iterv->tfindex][1];
1125                                         vtot++;
1126                                 }
1127                         }
1128
1129                         if (vtot > 1) {
1130                                 newuv[0] /= vtot; newuv[1] /= vtot;
1131
1132                                 for(iterv=v; iterv; iterv=iterv->next) {
1133                                         if (tf[iterv->f].flag & TF_SEL_MASK(iterv->tfindex)) {
1134                                                 tf[iterv->f].uv[iterv->tfindex][0]= newuv[0];
1135                                                 tf[iterv->f].uv[iterv->tfindex][1]= newuv[1];
1136                                         }
1137                                 }
1138                         }
1139                 }
1140         } else if(mode==1) {
1141                 for(a=0; a<me->totvert; a++) {
1142                         vlist= get_uv_map_vert(vmap, a);
1143
1144                         while(vlist) {
1145                                 newuv[0]= 0; newuv[1]= 0;
1146                                 vtot= 0;
1147
1148                                 for(iterv=vlist; iterv; iterv=iterv->next) {
1149                                         if((iterv != vlist) && iterv->separate)
1150                                                 break;
1151                                         if (tf[iterv->f].flag & TF_SEL_MASK(iterv->tfindex)) {
1152                                                 newuv[0] += tf[iterv->f].uv[iterv->tfindex][0];
1153                                                 newuv[1] += tf[iterv->f].uv[iterv->tfindex][1];
1154                                                 vtot++;
1155                                         }
1156                                 }
1157
1158                                 if (vtot > 1) {
1159                                         newuv[0] /= vtot; newuv[1] /= vtot;
1160
1161                                         for(iterv=vlist; iterv; iterv=iterv->next) {
1162                                                 if((iterv != vlist) && iterv->separate)
1163                                                         break;
1164                                                 if (tf[iterv->f].flag & TF_SEL_MASK(iterv->tfindex)) {
1165                                                         tf[iterv->f].uv[iterv->tfindex][0]= newuv[0];
1166                                                         tf[iterv->f].uv[iterv->tfindex][1]= newuv[1];
1167                                                 }
1168                                         }
1169                                 }
1170
1171                                 vlist= iterv;
1172                         }
1173                 }
1174         }
1175
1176         free_uv_vert_map(vmap);
1177
1178         if(G.sima->flag & SI_BE_SQUARE) be_square_tface_uv(me);
1179
1180         BIF_undo_push("Stitch UV");
1181
1182         object_uvs_changed(OBACT);
1183 }
1184
1185 void select_linked_tface_uv(int mode)
1186 {
1187         Mesh *me;
1188         MFace *mf;
1189         MTFace *tf, *nearesttf=NULL;
1190         UvVertMap *vmap;
1191         UvMapVert *vlist, *iterv, *startv;
1192         unsigned int *stack, stacksize= 0, nearestv;
1193         char *flag;
1194         int a, nearestuv, i, nverts;
1195         float limit[2];
1196         
1197         if(is_uv_tface_editing_allowed()==0)
1198                 return;
1199
1200         me= get_mesh(OBACT);
1201
1202         if (mode == 2) {
1203                 nearesttf= NULL;
1204                 nearestuv= 0;
1205         }
1206         if (mode!=2) {
1207                 find_nearest_uv(&nearesttf, &nearestv, &nearestuv);
1208                 if(nearesttf==NULL)
1209                         return;
1210         }
1211
1212         get_connected_limit_tface_uv(limit);
1213         vmap= make_uv_vert_map(me->mface, me->mtface, me->totface, me->totvert, 1, limit);
1214         if(vmap == NULL)
1215                 return;
1216
1217         stack= MEM_mallocN(sizeof(*stack)*me->totface, "UvLinkStack");
1218         flag= MEM_callocN(sizeof(*flag)*me->totface, "UvLinkFlag");
1219
1220         if (mode == 2) {
1221                 tf= me->mtface;
1222                 mf= me->mface;
1223                 for(a=0; a<me->totface; a++, tf++, mf++)
1224                         if(!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL))
1225                                 if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)) {
1226                                         stack[stacksize]= a;
1227                                         stacksize++;
1228                                         flag[a]= 1;
1229                                 }
1230         }
1231         else {
1232                 tf= me->mtface;
1233                 for(a=0; a<me->totface; a++, tf++)
1234                         if(tf == nearesttf) {
1235                                 stack[stacksize]= a;
1236                                 stacksize++;
1237                                 flag[a]= 1;
1238                                 break;
1239                         }
1240         }
1241
1242         while(stacksize > 0) {
1243                 stacksize--;
1244                 a= stack[stacksize];
1245                 mf= me->mface+a;
1246                 tf= me->mtface+a;
1247
1248                 nverts= mf->v4? 4: 3;
1249
1250                 for(i=0; i<nverts; i++) {
1251                         vlist= get_uv_map_vert(vmap, *(&mf->v1 + i));
1252                         startv= vlist;
1253
1254                         for(iterv=vlist; iterv; iterv=iterv->next) {
1255                                 if(iterv->separate)
1256                                         startv= iterv;
1257                                 if(iterv->f == a)
1258                                         break;
1259                         }
1260
1261                         for(iterv=startv; iterv; iterv=iterv->next) {
1262                                 if((startv != iterv) && (iterv->separate))
1263                                         break;
1264                                 else if(!flag[iterv->f]) {
1265                                         flag[iterv->f]= 1;
1266                                         stack[stacksize]= iterv->f;;
1267                                         stacksize++;
1268                                 }
1269                         }
1270                 }
1271         }
1272
1273         if(mode==0 || mode==2) {
1274                 for(a=0, tf=me->mtface; a<me->totface; a++, tf++)
1275                         if(flag[a])
1276                                 tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1277                         else
1278                                 tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1279         }
1280         else if(mode==1) {
1281                 mf= me->mface;
1282                 for(a=0, tf=me->mtface; a<me->totface; a++, tf++, mf++) {
1283                         if(flag[a]) {
1284                                 if (mf->v4) {
1285                                         if((tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4)))
1286                                                 break;
1287                                 }
1288                                 else if(tf->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1289                                         break;
1290                         }
1291                 }
1292
1293                 if (a<me->totface) {
1294                         for(a=0, tf=me->mtface; a<me->totface; a++, tf++)
1295                                 if(flag[a])
1296                                         tf->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1297                 }
1298                 else {
1299                         for(a=0, tf=me->mtface; a<me->totface; a++, tf++)
1300                                 if(flag[a])
1301                                         tf->flag |= (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1302                 }
1303         }
1304         
1305         MEM_freeN(stack);
1306         MEM_freeN(flag);
1307         free_uv_vert_map(vmap);
1308
1309         BIF_undo_push("Select linked UV");
1310         scrarea_queue_winredraw(curarea);
1311 }
1312
1313 void unlink_selection(void)
1314 {
1315         Mesh *me;
1316         MTFace *tface;
1317         MFace *mface;
1318         int a;
1319
1320         if( is_uv_tface_editing_allowed()==0 ) return;
1321         me= get_mesh(OBACT);
1322
1323         mface= me->mface;
1324         for(a=me->totface, tface= me->mtface; a>0; a--, tface++, mface++) {
1325                 if(mface->flag & ME_FACE_SEL) {
1326                         if(mface->v4) {
1327                                 if(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4))
1328                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3|TF_SEL4);
1329                         } else {
1330                                 if(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3))
1331                                         tface->flag &= ~(TF_SEL1|TF_SEL2|TF_SEL3);
1332                         }
1333                 }
1334         }
1335         
1336         BIF_undo_push("Unlink UV selection");
1337         scrarea_queue_winredraw(curarea);
1338 }
1339
1340 void toggle_uv_select(int mode)
1341 {
1342         switch(mode){
1343         case 'f':
1344                 G.sima->flag ^= SI_SELACTFACE;
1345                 break;
1346         case 's':
1347                 G.sima->flag ^= SI_STICKYUVS; 
1348                 if (G.sima->flag & SI_STICKYUVS) G.sima->flag &= ~SI_LOCALSTICKY;
1349                 else G.sima->flag |= SI_LOCALSTICKY;
1350                 break;
1351         case 'l': 
1352                  G.sima->flag ^= SI_LOCALSTICKY;
1353                  if (G.sima->flag & SI_LOCALSTICKY) G.sima->flag &= ~SI_STICKYUVS;
1354                 break;
1355         case 'o':
1356                 G.sima->flag &= ~SI_STICKYUVS; 
1357                 G.sima->flag &= ~SI_LOCALSTICKY;
1358                 break;
1359         }
1360         allqueue(REDRAWIMAGE, 0);
1361 }
1362
1363 void pin_tface_uv(int mode)
1364 {
1365         Mesh *me;
1366         MTFace *tface;
1367         MFace *mface;
1368         int a;
1369         
1370         if( is_uv_tface_editing_allowed()==0 ) return;
1371         me= get_mesh(OBACT);
1372         
1373         mface= me->mface;
1374         tface= me->mtface;
1375         for(a=me->totface; a>0; a--, tface++, mface++) {
1376                 if(mface->flag & ME_FACE_SEL) {
1377                         if(mode ==1){
1378                                 if(tface->flag & TF_SEL1) tface->unwrap |= TF_PIN1;
1379                                 if(tface->flag & TF_SEL2) tface->unwrap |= TF_PIN2;
1380                                 if(tface->flag & TF_SEL3) tface->unwrap |= TF_PIN3;
1381                                 if(mface->v4)
1382                                         if(tface->flag & TF_SEL4) tface->unwrap |= TF_PIN4;
1383                         }
1384                         else if (mode ==0){
1385                                 if(tface->flag & TF_SEL1) tface->unwrap &= ~TF_PIN1;
1386                                 if(tface->flag & TF_SEL2) tface->unwrap &= ~TF_PIN2;
1387                                 if(tface->flag & TF_SEL3) tface->unwrap &= ~TF_PIN3;
1388                                 if(mface->v4)
1389                                         if(tface->flag & TF_SEL4) tface->unwrap &= ~TF_PIN4;
1390                         }
1391                 }
1392         }
1393         
1394         BIF_undo_push("Pin UV");
1395         scrarea_queue_winredraw(curarea);
1396 }
1397
1398 void select_pinned_tface_uv(void)
1399 {
1400         Mesh *me;
1401         MTFace *tface;
1402         MFace *mface;
1403         int a;
1404         
1405         if( is_uv_tface_editing_allowed()==0 ) return;
1406         me= get_mesh(OBACT);
1407         
1408         mface= me->mface;
1409         tface= me->mtface;
1410         for(a=me->totface; a>0; a--, tface++, mface++) {
1411                 if(mface->flag & ME_FACE_SEL) {
1412                 
1413                         if (tface->unwrap & TF_PIN1) tface->flag |= TF_SEL1;
1414                         if (tface->unwrap & TF_PIN2) tface->flag |= TF_SEL2;
1415                         if (tface->unwrap & TF_PIN3) tface->flag |= TF_SEL3;
1416                         if(mface->v4) {
1417                                 if (tface->unwrap & TF_PIN4) tface->flag |= TF_SEL4;
1418                         }
1419                         
1420                 }
1421         }
1422         
1423         BIF_undo_push("Select Pinned UVs");
1424         scrarea_queue_winredraw(curarea);
1425 }
1426
1427 int minmax_tface_uv(float *min, float *max)
1428 {
1429         Mesh *me;
1430         MTFace *tf;
1431         MFace *mf;
1432         int a, sel;
1433
1434         if( is_uv_tface_editing_allowed()==0 ) return 0;
1435         me= get_mesh(OBACT);
1436
1437         INIT_MINMAX2(min, max);
1438
1439         sel= 0;
1440         mf= (MFace*)me->mface;
1441         tf= (MTFace*)me->mtface;
1442         for(a=me->totface; a>0; a--, tf++, mf++) {
1443                 if(mf->flag & ME_HIDE);
1444                 else if(mf->flag & ME_FACE_SEL) {
1445
1446                         if (tf->flag & TF_SEL1) {
1447                                 DO_MINMAX2(tf->uv[0], min, max);
1448                         }
1449                         if (tf->flag & TF_SEL2) {
1450                                 DO_MINMAX2(tf->uv[1], min, max);
1451                         }
1452                         if (tf->flag & TF_SEL3) {
1453                                 DO_MINMAX2(tf->uv[2], min, max);
1454                         }
1455                         if (mf->v4 && tf->flag & TF_SEL4) {
1456                                 DO_MINMAX2(tf->uv[3], min, max);
1457                         }
1458
1459                         sel = 1;
1460                 }
1461         }
1462
1463         return sel;
1464 }
1465
1466 static void sima_show_info(int channels, int x, int y, char *cp, float *fp, int *zp, float *zpf)
1467 {
1468         short ofs;
1469         char str[256];
1470         
1471         ofs= sprintf(str, "X: %d Y: %d ", x, y);
1472         if(cp)
1473                 ofs+= sprintf(str+ofs, "| R: %d G: %d B: %d A: %d ", cp[0], cp[1], cp[2], cp[3]);
1474         if(fp) {
1475                 if(channels==4)
1476                         ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f A: %.3f ", fp[0], fp[1], fp[2], fp[3]);
1477                 else if(channels==1)
1478                         ofs+= sprintf(str+ofs, "| Val: %.3f ", fp[0]);
1479                 else if(channels==3)
1480                         ofs+= sprintf(str+ofs, "| R: %.3f G: %.3f B: %.3f ", fp[0], fp[1], fp[2]);
1481         }
1482         if(zp)
1483                 ofs+= sprintf(str+ofs, "| Z: %.4f ", 0.5+0.5*( ((float)*zp)/(float)0x7fffffff));
1484         if(zpf)
1485                 ofs+= sprintf(str+ofs, "| Z: %.3f ", *zpf);
1486         
1487         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1488         glEnable(GL_BLEND);
1489         
1490         glColor4f(.0,.0,.0,.25);
1491         glRectf(0.0, 0.0, curarea->winx, 30.0);
1492         glDisable(GL_BLEND);
1493         
1494         glColor3ub(255, 255, 255);
1495         glRasterPos2i(10, 10);
1496         
1497         BMF_DrawString(G.fonts, str);
1498
1499 }
1500
1501 void sima_sample_color(void)
1502 {
1503         ImBuf *ibuf= BKE_image_get_ibuf(G.sima->image, &G.sima->iuser);
1504         float fx, fy;
1505         short mval[2], mvalo[2], firsttime=1;
1506         
1507         if(ibuf==NULL)
1508                 return;
1509         
1510         calc_image_view(G.sima, 'f');
1511         getmouseco_areawin(mvalo);
1512         
1513         while(get_mbut() & L_MOUSE) {
1514                 
1515                 getmouseco_areawin(mval);
1516                 if(mval[0]!=mvalo[0] || mval[1]!=mvalo[1] || firsttime) {
1517                         firsttime= 0;
1518                         areamouseco_to_ipoco(G.v2d, mval, &fx, &fy);
1519                         
1520                         if(fx>=0.0 && fy>=0.0 && fx<1.0 && fy<1.0) {
1521                                 float *fp= NULL, *zpf= NULL;
1522                                 int *zp= NULL;
1523                                 char *cp= NULL;
1524                                 
1525                                 int x= (int) (fx*ibuf->x);
1526                                 int y= (int) (fy*ibuf->y);
1527                                 
1528                                 if(x>=ibuf->x) x= ibuf->x-1;
1529                                 if(y>=ibuf->y) y= ibuf->y-1;
1530                                 
1531                                 if(ibuf->rect)
1532                                         cp= (char *)(ibuf->rect + y*ibuf->x + x);
1533                                 if(ibuf->zbuf)
1534                                         zp= ibuf->zbuf + y*ibuf->x + x;
1535                                 if(ibuf->zbuf_float)
1536                                         zpf= ibuf->zbuf_float + y*ibuf->x + x;
1537                                 if(ibuf->rect_float)
1538                                         fp= (ibuf->rect_float + (ibuf->channels)*(y*ibuf->x + x));
1539                                         
1540                                 if(G.sima->cumap) {
1541                                         float vec[3];
1542                                         if(fp==NULL) {
1543                                                 fp= vec;
1544                                                 vec[0]= (float)cp[0]/255.0f;
1545                                                 vec[1]= (float)cp[1]/255.0f;
1546                                                 vec[2]= (float)cp[2]/255.0f;
1547                                         }
1548                                         
1549                                         if(ibuf->channels==4) {
1550                                                 if(G.qual & LR_CTRLKEY) {
1551                                                         curvemapping_set_black_white(G.sima->cumap, NULL, fp);
1552                                                         curvemapping_do_ibuf(G.sima->cumap, ibuf);
1553                                                 }
1554                                                 else if(G.qual & LR_SHIFTKEY) {
1555                                                         curvemapping_set_black_white(G.sima->cumap, fp, NULL);
1556                                                         curvemapping_do_ibuf(G.sima->cumap, ibuf);
1557                                                 }
1558                                         }
1559                                 }
1560                                 
1561                                 scrarea_do_windraw(curarea);
1562                                 myortho2(-0.375, curarea->winx-0.375, -0.375, curarea->winy-0.375);
1563                                 glLoadIdentity();
1564                                 sima_show_info(ibuf->channels, x, y, cp, fp, zp, zpf);
1565                                 screen_swapbuffers();
1566                         }
1567                         
1568                 }
1569                 BIF_wait_for_statechange();
1570         }
1571         
1572         scrarea_queue_winredraw(curarea);
1573 }
1574
1575 /* Image functions */
1576
1577 static void load_image_filesel(char *str)       /* called from fileselect */
1578 {
1579         Image *ima= NULL;
1580  
1581         if(G.obedit) {
1582                 error("Can't perfom this in editmode");
1583                 return;
1584         }
1585
1586         ima= BKE_add_image_file(str);
1587         if(ima) {
1588
1589                 G.sima->image= ima;
1590
1591                 BKE_image_signal(ima, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1592                 image_changed(G.sima, 0);
1593
1594         }
1595         BIF_undo_push("Load image UV");
1596         allqueue(REDRAWIMAGE, 0);
1597 }
1598
1599 static void image_replace(Image *old, Image *new)
1600 {
1601         MTFace *tface;
1602         Mesh *me;
1603         int a, rep=0;
1604
1605         new->tpageflag= old->tpageflag;
1606         new->twsta= old->twsta;
1607         new->twend= old->twend;
1608         new->xrep= old->xrep;
1609         new->yrep= old->yrep;
1610  
1611         me= G.main->mesh.first;
1612         while(me) {
1613
1614                 if(me->id.lib==NULL && me->mtface) {
1615                         tface= me->mtface;
1616                         a= me->totface;
1617                         while(a--) {
1618                                 if(tface->tpage==old) {
1619                                         tface->tpage= new;
1620                                         rep++;
1621                                 }
1622                                 tface++;
1623                         }
1624                 }
1625                 me= me->id.next;
1626  
1627         }
1628         if(rep) {
1629                 if(new->id.us==0) id_us_plus(&new->id);
1630                 else id_lib_extern(&new->id);
1631                 
1632         }
1633         else error("Nothing replaced");
1634 }
1635
1636 static void replace_image_filesel(char *str)            /* called from fileselect */
1637 {
1638         Image *ima=0;
1639
1640         if(G.obedit) {
1641                 error("Can't perfom this in editmode");
1642                 return;
1643         }
1644
1645         ima= BKE_add_image_file(str);
1646         if(ima) {
1647  
1648                 if(G.sima->image && G.sima->image != ima) {
1649                         image_replace(G.sima->image, ima);
1650                 }
1651  
1652                 G.sima->image= ima;
1653
1654                 BKE_image_signal(ima, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1655
1656                 /* replace also assigns: */
1657                 image_changed(G.sima, 0);
1658
1659         }
1660         BIF_undo_push("Replace image UV");
1661         allqueue(REDRAWIMAGE, 0);
1662 }
1663
1664
1665 static void save_image_doit(char *name)
1666 {
1667         Image *ima= G.sima->image;
1668         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1669         int len;
1670         char str[FILE_MAXDIR+FILE_MAXFILE];
1671
1672         if (ibuf) {
1673                 BLI_strncpy(str, name, sizeof(str));
1674
1675                 BLI_convertstringcode(str, G.sce, G.scene->r.cfra);
1676                 
1677                 if(G.scene->r.scemode & R_EXTENSION) 
1678                         BKE_add_image_extension(str, G.sima->imtypenr);
1679                 
1680                 if (saveover(str)) {
1681                         
1682                         /* enforce user setting for RGB or RGBA, but skip BW */
1683                         if(G.scene->r.planes==32)
1684                                 ibuf->depth= 32;
1685                         else if(G.scene->r.planes==24)
1686                                 ibuf->depth= 24;
1687                         
1688                         waitcursor(1);
1689                         if(G.sima->imtypenr==R_MULTILAYER) {
1690                                 RenderResult *rr= BKE_image_get_renderresult(ima);
1691                                 if(rr) {
1692                                         RE_WriteRenderResult(rr, str, G.scene->r.quality);
1693                                         
1694                                         BLI_strncpy(ima->name, name, sizeof(ima->name));
1695                                         BLI_strncpy(ibuf->name, str, sizeof(ibuf->name));
1696                                         
1697                                         /* should be function? nevertheless, saving only happens here */
1698                                         for(ibuf= ima->ibufs.first; ibuf; ibuf= ibuf->next)
1699                                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1700                                         
1701                                 }
1702                                 else error("Did not write, no Multilayer Image");
1703                         }
1704                         else if (BKE_write_ibuf(ibuf, str, G.sima->imtypenr, G.scene->r.subimtype, G.scene->r.quality)) {
1705                                 BLI_strncpy(ima->name, name, sizeof(ima->name));
1706                                 BLI_strncpy(ibuf->name, str, sizeof(ibuf->name));
1707                                 
1708                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1709                                 
1710                                 /* change type? */
1711                                 if( ELEM(ima->source, IMA_SRC_GENERATED, IMA_SRC_VIEWER)) {
1712                                         ima->source= IMA_SRC_FILE;
1713                                         ima->type= IMA_TYPE_IMAGE;
1714                                 }
1715                                 if(ima->type==IMA_TYPE_R_RESULT)
1716                                         ima->type= IMA_TYPE_IMAGE;
1717                                 
1718                                 /* name image as how we saved it */
1719                                 len= strlen(str);
1720                                 while (len > 0 && str[len - 1] != '/' && str[len - 1] != '\\') len--;
1721                                 rename_id(&ima->id, str+len);
1722                         } 
1723                         else {
1724                                 error("Couldn't write image: %s", str);
1725                         }
1726
1727                         allqueue(REDRAWHEADERS, 0);
1728                         allqueue(REDRAWBUTSSHADING, 0);
1729
1730                         waitcursor(0);
1731                 }
1732         }
1733 }
1734
1735 void open_image_sima(short imageselect)
1736 {
1737         char name[FILE_MAXDIR+FILE_MAXFILE];
1738
1739         if(G.sima->image)
1740                 strcpy(name, G.sima->image->name);
1741         else
1742                 strcpy(name, U.textudir);
1743
1744         if(imageselect)
1745                 activate_imageselect(FILE_SPECIAL, "Open Image", name, load_image_filesel);
1746         else
1747                 activate_fileselect(FILE_SPECIAL, "Open Image", name, load_image_filesel);
1748 }
1749
1750 void replace_image_sima(short imageselect)
1751 {
1752         char name[FILE_MAXDIR+FILE_MAXFILE];
1753
1754         if(G.sima->image)
1755                 strcpy(name, G.sima->image->name);
1756         else
1757                 strcpy(name, U.textudir);
1758         
1759         if(imageselect)
1760                 activate_imageselect(FILE_SPECIAL, "Replace Image", name, replace_image_filesel);
1761         else
1762                 activate_fileselect(FILE_SPECIAL, "Replace Image", name, replace_image_filesel);
1763 }
1764
1765
1766 static char *filesel_imagetype_string(Image *ima)
1767 {
1768         char *strp, *str= MEM_callocN(14*32, "menu for filesel");
1769         
1770         strp= str;
1771         str += sprintf(str, "Save Image as: %%t|");
1772         str += sprintf(str, "Targa %%x%d|", R_TARGA);
1773         str += sprintf(str, "Targa Raw %%x%d|", R_RAWTGA);
1774         str += sprintf(str, "PNG %%x%d|", R_PNG);
1775         str += sprintf(str, "BMP %%x%d|", R_BMP);
1776         str += sprintf(str, "Jpeg %%x%d|", R_JPEG90);
1777         str += sprintf(str, "Iris %%x%d|", R_IRIS);
1778         if(G.have_libtiff)
1779                 str += sprintf(str, "Tiff %%x%d|", R_TIFF);
1780         str += sprintf(str, "Radiance HDR %%x%d|", R_RADHDR);
1781         str += sprintf(str, "Cineon %%x%d|", R_CINEON);
1782         str += sprintf(str, "DPX %%x%d|", R_DPX);
1783 #ifdef WITH_OPENEXR
1784         str += sprintf(str, "OpenEXR %%x%d|", R_OPENEXR);
1785         /* saving sequences of multilayer won't work, they copy buffers  */
1786         if(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER);
1787         else str += sprintf(str, "MultiLayer %%x%d|", R_MULTILAYER);
1788 #endif  
1789         return strp;
1790 }
1791
1792 /* always opens fileselect */
1793 void save_as_image_sima(void)
1794 {
1795         Image *ima = G.sima->image;
1796         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1797         char name[FILE_MAXDIR+FILE_MAXFILE];
1798
1799         if (ima) {
1800                 strcpy(name, ima->name);
1801
1802                 if (ibuf) {
1803                         char *strp;
1804                         
1805                         strp= filesel_imagetype_string(ima);
1806                         
1807                         /* cant save multilayer sequence, ima->rr isn't valid for a specific frame */
1808                         if(ima->rr && !(ima->source==IMA_SRC_SEQUENCE && ima->type==IMA_TYPE_MULTILAYER))
1809                                 G.sima->imtypenr= R_MULTILAYER;
1810                         else if(ima->type==IMA_TYPE_R_RESULT)
1811                                 G.sima->imtypenr= R_MULTILAYER;
1812                         else G.sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1813                         
1814                         activate_fileselect_menu(FILE_SPECIAL, "Save Image", name, strp, &G.sima->imtypenr, save_image_doit);
1815                 }
1816         }
1817 }
1818
1819 /* if exists, saves over without fileselect */
1820 void save_image_sima(void)
1821 {
1822         Image *ima = G.sima->image;
1823         ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1824         char name[FILE_MAXDIR+FILE_MAXFILE];
1825
1826         if (ima) {
1827                 strcpy(name, ima->name);
1828
1829                 if (ibuf) {
1830                         if (BLI_exists(ibuf->name)) {
1831                                 if(BKE_image_get_renderresult(ima)) 
1832                                         G.sima->imtypenr= R_MULTILAYER;
1833                                 else 
1834                                         G.sima->imtypenr= BKE_ftype_to_imtype(ibuf->ftype);
1835                                 
1836                                 save_image_doit(ibuf->name);
1837                         }
1838                         else
1839                                 save_as_image_sima();
1840                 }
1841         }
1842 }
1843
1844 void save_image_sequence_sima(void)
1845 {
1846         ImBuf *ibuf;
1847         int tot= 0;
1848         char di[FILE_MAX], fi[FILE_MAX];
1849         
1850         if(G.sima->image==NULL)
1851                 return;
1852         if(G.sima->image->source!=IMA_SRC_SEQUENCE)
1853                 return;
1854         if(G.sima->image->type==IMA_TYPE_MULTILAYER) {
1855                 error("Cannot save Multilayer Sequences");
1856                 return;
1857         }
1858         
1859         /* get total */
1860         for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1861                 if(ibuf->userflags & IB_BITMAPDIRTY)
1862                         tot++;
1863         
1864         if(tot==0) {
1865                 notice("No Images have been changed");
1866                 return;
1867         }
1868         /* get a filename for menu */
1869         for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) 
1870                 if(ibuf->userflags & IB_BITMAPDIRTY)
1871                         break;
1872         
1873         BLI_strncpy(di, ibuf->name, FILE_MAX);
1874         BLI_splitdirstring(di, fi);
1875         
1876         sprintf(fi, "%d Image(s) will be saved in %s", tot, di);
1877         if(okee(fi)) {
1878                 
1879                 for(ibuf= G.sima->image->ibufs.first; ibuf; ibuf= ibuf->next) {
1880                         if(ibuf->userflags & IB_BITMAPDIRTY) {
1881                                 char name[FILE_MAX];
1882                                 BLI_strncpy(name, ibuf->name, sizeof(name));
1883                                 
1884                                 BLI_convertstringcode(name, G.sce, 0);
1885
1886                                 if(0 == IMB_saveiff(ibuf, name, IB_rect | IB_zbuf | IB_zbuffloat)) {
1887                                         error("Could not write image", name);
1888                                         break;
1889                                 }
1890                                 printf("Saved: %s\n", ibuf->name);
1891                                 ibuf->userflags &= ~IB_BITMAPDIRTY;
1892                         }
1893                 }
1894         }
1895 }
1896
1897 void reload_image_sima(void)
1898 {
1899
1900         if (G.sima ) {
1901                 BKE_image_signal(G.sima->image, &G.sima->iuser, IMA_SIGNAL_RELOAD);
1902                 image_changed(G.sima, 0);
1903         }
1904
1905         allqueue(REDRAWIMAGE, 0);
1906         allqueue(REDRAWVIEW3D, 0);
1907         BIF_preview_changed(ID_TE);
1908 }
1909
1910 void new_image_sima(void)
1911 {
1912         static int width= 256, height= 256;
1913         static short uvtestgrid= 0;
1914         static float color[] = {0, 0, 0, 1};
1915         char name[22];
1916
1917         strcpy(name, "Untitled");
1918
1919         add_numbut(0, TEX, "Name:", 0, 21, name, NULL);
1920         add_numbut(1, NUM|INT, "Width:", 1, 5000, &width, NULL);
1921         add_numbut(2, NUM|INT, "Height:", 1, 5000, &height, NULL);
1922         add_numbut(3, COL, "", 0, 0, &color, NULL);
1923         add_numbut(4, NUM|FLO, "Alpha:", 0.0, 1.0, &color[3], NULL);
1924         add_numbut(5, TOG|SHO, "UV Test Grid", 0, 0, &uvtestgrid, NULL);
1925         if (!do_clever_numbuts("New Image", 6, REDRAW))
1926                 return;
1927
1928         G.sima->image= BKE_add_image_size(width, height, name, uvtestgrid, color);
1929         BKE_image_signal(G.sima->image, &G.sima->iuser, IMA_SIGNAL_USER_NEW_IMAGE);
1930         image_changed(G.sima, 0);
1931
1932         BIF_undo_push("Add image");
1933
1934         allqueue(REDRAWIMAGE, 0);
1935         allqueue(REDRAWVIEW3D, 0);
1936 }
1937
1938 void pack_image_sima()
1939 {
1940         Image *ima = G.sima->image;
1941
1942         if (ima) {
1943                 if(ima->source!=IMA_SRC_SEQUENCE && ima->source!=IMA_SRC_MOVIE) {
1944                         if (ima->packedfile) {
1945                                 if (G.fileflags & G_AUTOPACK)
1946                                         if (okee("Disable AutoPack?"))
1947                                                 G.fileflags &= ~G_AUTOPACK;
1948                                 
1949                                 if ((G.fileflags & G_AUTOPACK) == 0) {
1950                                         unpackImage(ima, PF_ASK);
1951                                         BIF_undo_push("Unpack image");
1952                                 }
1953                         }
1954                         else {
1955                                 ImBuf *ibuf= BKE_image_get_ibuf(ima, &G.sima->iuser);
1956                                 if (ibuf && (ibuf->userflags & IB_BITMAPDIRTY)) {
1957                                         if(okee("Can't pack painted image. Use Repack as PNG?"))
1958                                                 BKE_image_memorypack(ima);
1959                                 }
1960                                 else {
1961                                         ima->packedfile = newPackedFile(ima->name);
1962                                         BIF_undo_push("Pack image");
1963                                 }
1964                         }
1965
1966                         allqueue(REDRAWBUTSSHADING, 0);
1967                         allqueue(REDRAWHEADERS, 0);
1968                 }
1969         }
1970 }
1971
1972
1973
1974 /* goes over all ImageUsers, and sets frame numbers if auto-refresh is set */
1975 void BIF_image_update_frame(void)
1976 {
1977         Tex *tex;
1978         
1979         /* texture users */
1980         for(tex= G.main->tex.first; tex; tex= tex->id.next) {
1981                 if(tex->type==TEX_IMAGE && tex->ima)
1982                         if(ELEM(tex->ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
1983                                 if(tex->iuser.flag & IMA_ANIM_ALWAYS)
1984                                         BKE_image_user_calc_imanr(&tex->iuser, G.scene->r.cfra, 0);
1985                 
1986         }
1987         /* image window, compo node users */
1988         if(G.curscreen) {
1989                 ScrArea *sa;
1990                 for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
1991                         if(sa->spacetype==SPACE_VIEW3D) {
1992                                 View3D *v3d= sa->spacedata.first;
1993                                 if(v3d->bgpic)
1994                                         if(v3d->bgpic->iuser.flag & IMA_ANIM_ALWAYS)
1995                                                 BKE_image_user_calc_imanr(&v3d->bgpic->iuser, G.scene->r.cfra, 0);
1996                         }
1997                         else if(sa->spacetype==SPACE_IMAGE) {
1998                                 SpaceImage *sima= sa->spacedata.first;
1999                                 if(sima->iuser.flag & IMA_ANIM_ALWAYS)
2000                                         BKE_image_user_calc_imanr(&sima->iuser, G.scene->r.cfra, 0);
2001                         }
2002                         else if(sa->spacetype==SPACE_NODE) {
2003                                 SpaceNode *snode= sa->spacedata.first;
2004                                 if((snode->treetype==NTREE_COMPOSIT) && (snode->nodetree)) {
2005                                         bNode *node;
2006                                         for(node= snode->nodetree->nodes.first; node; node= node->next) {
2007                                                 if(node->id && node->type==CMP_NODE_IMAGE) {
2008                                                         Image *ima= (Image *)node->id;
2009                                                         ImageUser *iuser= node->storage;
2010                                                         if(ELEM(ima->source, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE))
2011                                                                 if(iuser->flag & IMA_ANIM_ALWAYS)
2012                                                                         BKE_image_user_calc_imanr(iuser, G.scene->r.cfra, 0);
2013                                                 }
2014                                         }
2015                                 }
2016                         }
2017                 }
2018         }
2019 }
2020