4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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
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.
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.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
45 #include "MEM_guardedalloc.h"
47 #include "BLI_arithb.h"
48 #include "BLI_blenlib.h"
50 #include "IMB_imbuf_types.h"
52 #include "DNA_image_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_meshdata_types.h"
55 #include "DNA_packedFile_types.h"
56 #include "DNA_scene_types.h"
57 #include "DNA_screen_types.h"
58 #include "DNA_space_types.h"
59 #include "DNA_userdef_types.h"
61 #include "BKE_utildefines.h"
62 #include "BKE_global.h"
65 #include "BKE_image.h"
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_displist.h"
68 #include "BKE_object.h"
70 #include "BDR_editface.h"
71 #include "BDR_drawobject.h"
72 #include "BDR_drawmesh.h"
75 #include "BIF_mywindow.h"
76 #include "BIF_drawimage.h"
77 #include "BIF_resources.h"
78 #include "BIF_interface.h"
79 #include "BIF_editsima.h"
80 #include "BIF_glutil.h"
81 #include "BIF_space.h"
82 #include "BIF_screen.h"
83 #include "BIF_transform.h"
85 #include "BSE_trans_types.h"
91 #include "butspace.h" // event codes
94 * Sets up the fields of the View2D member of the SpaceImage struct
95 * This routine can be called in two modes:
96 * mode == 'f': float mode (0.0 - 1.0)
97 * mode == 'p': pixel mode (0 - size)
99 * @param sima the image space to update
100 * @param mode the mode to use for the update
104 void calc_image_view(SpaceImage *sima, char mode)
106 float xim=256, yim=256;
110 if(sima->image && sima->image->ibuf) {
111 xim= sima->image->ibuf->x;
112 yim= sima->image->ibuf->y;
115 sima->v2d.tot.xmin= 0;
116 sima->v2d.tot.ymin= 0;
117 sima->v2d.tot.xmax= xim;
118 sima->v2d.tot.ymax= yim;
120 sima->v2d.mask.xmin= sima->v2d.mask.ymin= 0;
121 sima->v2d.mask.xmax= curarea->winx;
122 sima->v2d.mask.ymax= curarea->winy;
125 /* Which part of the image space do we see? */
126 /* Same calculation as in lrectwrite: area left and down*/
127 x1= curarea->winrct.xmin+(curarea->winx-sima->zoom*xim)/2;
128 y1= curarea->winrct.ymin+(curarea->winy-sima->zoom*yim)/2;
130 x1-= sima->zoom*sima->xof;
131 y1-= sima->zoom*sima->yof;
136 /* relative display right */
137 sima->v2d.cur.xmin= ((curarea->winrct.xmin - (float)x1)/zoom);
138 sima->v2d.cur.xmax= sima->v2d.cur.xmin + ((float)curarea->winx/zoom);
140 /* relative display left */
141 sima->v2d.cur.ymin= ((curarea->winrct.ymin-(float)y1)/zoom);
142 sima->v2d.cur.ymax= sima->v2d.cur.ymin + ((float)curarea->winy/zoom);
145 sima->v2d.cur.xmin/= xim;
146 sima->v2d.cur.xmax/= xim;
147 sima->v2d.cur.ymin/= yim;
148 sima->v2d.cur.ymax/= yim;
152 void what_image(SpaceImage *sima)
154 extern TFace *lasttface; /* editface.c */
157 if(sima->mode==SI_TEXTURE) {
158 if(G.f & G_FACESELECT) {
161 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
164 if(me && me->tface && lasttface && lasttface->mode & TF_TEX) {
165 sima->image= lasttface->tpage;
167 if(sima->flag & SI_EDITTILE);
168 else sima->curtile= lasttface->tile;
171 if(lasttface->mode & TF_TILES)
172 sima->image->tpageflag |= IMA_TILES;
173 else sima->image->tpageflag &= ~IMA_TILES;
180 void image_changed(SpaceImage *sima, int dotile)
186 if(sima->mode==SI_TEXTURE) {
188 if(G.f & G_FACESELECT) {
190 if(me && me->tface) {
194 if(tface->flag & TF_SELECT) {
197 tface->mode &= ~TF_TILES;
200 tface->tpage= sima->image;
201 tface->mode |= TF_TEX;
203 if(dotile) tface->tile= sima->curtile;
207 if(sima->image->tpageflag & IMA_TILES) tface->mode |= TF_TILES;
208 else tface->mode &= ~TF_TILES;
210 if(sima->image->id.us==0) sima->image->id.us= 1;
216 object_uvs_changed(OBACT);
217 allqueue(REDRAWBUTSEDIT, 0);
224 void uvco_to_areaco(float *vec, short *mval)
230 x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
231 y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
233 if(x>=0.0 && x<=1.0) {
234 if(y>=0.0 && y<=1.0) {
235 mval[0]= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
236 mval[1]= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
241 void uvco_to_areaco_noclip(float *vec, int *mval)
247 x= (vec[0] - G.v2d->cur.xmin)/(G.v2d->cur.xmax-G.v2d->cur.xmin);
248 y= (vec[1] - G.v2d->cur.ymin)/(G.v2d->cur.ymax-G.v2d->cur.ymin);
250 x= G.v2d->mask.xmin + x*(G.v2d->mask.xmax-G.v2d->mask.xmin);
251 y= G.v2d->mask.ymin + y*(G.v2d->mask.ymax-G.v2d->mask.ymin);
257 void draw_tfaces(void)
259 TFace *tface,*activetface = NULL;
260 MFace *mface,*activemface = NULL;
263 char col1[4], col2[4];
264 float pointsize= BIF_GetThemeValuef(TH_VERTEX_SIZE);
266 if(G.f & G_FACESELECT) {
267 me= get_mesh((G.scene->basact) ? (G.scene->basact->object) : 0);
268 if(me && me->tface) {
269 calc_image_view(G.sima, 'f'); /* float */
270 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
273 /* draw shadow mesh */
274 if((G.sima->flag & SI_DRAWSHADOW) && !(G.obedit==OBACT)){
276 DerivedMesh *dm = mesh_get_derived_final(OBACT, &dmNeedsFree);
278 glColor3ub(112, 112, 112);
279 if (dm->drawUVEdges) dm->drawUVEdges(dm);
281 if (dmNeedsFree) dm->release(dm);
283 else if(G.sima->flag & SI_DRAWSHADOW){
288 if(!(tface->flag & TF_HIDE)) {
289 glColor3ub(112, 112, 112);
290 glBegin(GL_LINE_LOOP);
291 glVertex2fv(tface->uv[0]);
292 glVertex2fv(tface->uv[1]);
293 glVertex2fv(tface->uv[2]);
294 if(mface->v4) glVertex2fv(tface->uv[3]);
302 /* draw transparent faces */
303 if(G.f & G_DRAWFACES) {
304 BIF_GetThemeColor4ubv(TH_FACE, col1);
305 BIF_GetThemeColor4ubv(TH_FACE_SELECT, col2);
306 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
312 if(tface->flag & TF_SELECT) {
313 if(!(~tface->flag & (TF_SEL1|TF_SEL2|TF_SEL3)) &&
314 (!mface->v4 || tface->flag & TF_SEL4))
319 glBegin(mface->v4?GL_QUADS:GL_TRIANGLES);
320 glVertex2fv(tface->uv[0]);
321 glVertex2fv(tface->uv[1]);
322 glVertex2fv(tface->uv[2]);
323 if(mface->v4) glVertex2fv(tface->uv[3]);
337 if(tface->flag & TF_SELECT) {
338 if(tface->flag & TF_ACTIVE){
344 glBegin(GL_LINE_LOOP);
345 glVertex2fv(tface->uv[0]);
346 glVertex2fv(tface->uv[1]);
347 glVertex2fv(tface->uv[2]);
348 if(mface->v4) glVertex2fv(tface->uv[3]);
353 glBegin(GL_LINE_STRIP);
354 glVertex2fv(tface->uv[0]);
355 glVertex2fv(tface->uv[1]);
358 glBegin(GL_LINE_STRIP);
359 glVertex2fv(tface->uv[0]);
360 if(mface->v4) glVertex2fv(tface->uv[3]);
361 else glVertex2fv(tface->uv[2]);
364 glBegin(GL_LINE_STRIP);
365 glVertex2fv(tface->uv[1]);
366 glVertex2fv(tface->uv[2]);
367 if(mface->v4) glVertex2fv(tface->uv[3]);
376 /* draw active face edges */
378 /* colors: R=u G=v */
385 glBegin(GL_LINE_LOOP);
386 glVertex2fv(tface->uv[0]);
387 glVertex2fv(tface->uv[1]);
388 glVertex2fv(tface->uv[2]);
389 if(mface->v4) glVertex2fv(tface->uv[3]);
393 glBegin(GL_LINE_STRIP);
394 glVertex2fv(tface->uv[0]);
395 glVertex2fv(tface->uv[1]);
399 glBegin(GL_LINE_STRIP);
400 glVertex2fv(tface->uv[0]);
401 if(mface->v4) glVertex2fv(tface->uv[3]);
402 else glVertex2fv(tface->uv[2]);
406 glBegin(GL_LINE_STRIP);
407 glVertex2fv(tface->uv[1]);
408 glVertex2fv(tface->uv[2]);
409 if(mface->v4) glVertex2fv(tface->uv[3]);
415 /* unselected uv's */
416 BIF_ThemeColor(TH_VERTEX);
417 glPointSize(pointsize);
424 if(tface->flag & TF_SELECT) {
426 if(tface->flag & TF_SEL1); else bglVertex2fv(tface->uv[0]);
427 if(tface->flag & TF_SEL2); else bglVertex2fv(tface->uv[1]);
428 if(tface->flag & TF_SEL3); else bglVertex2fv(tface->uv[2]);
430 if(tface->flag & TF_SEL4); else bglVertex2fv(tface->uv[3]);
439 /* give odd pointsizes odd pin pointsizes */
440 glPointSize(pointsize*2 + (((int)pointsize % 2)? (-1): 0));
448 if(tface->flag & TF_SELECT) {
450 if(tface->unwrap & TF_PIN1) bglVertex2fv(tface->uv[0]);
451 if(tface->unwrap & TF_PIN2) bglVertex2fv(tface->uv[1]);
452 if(tface->unwrap & TF_PIN3) bglVertex2fv(tface->uv[2]);
454 if(tface->unwrap & TF_PIN4) bglVertex2fv(tface->uv[3]);
463 BIF_ThemeColor(TH_VERTEX_SELECT);
464 glPointSize(pointsize);
471 if(tface->flag & TF_SELECT) {
473 if(tface->flag & TF_SEL1) bglVertex2fv(tface->uv[0]);
474 if(tface->flag & TF_SEL2) bglVertex2fv(tface->uv[1]);
475 if(tface->flag & TF_SEL3) bglVertex2fv(tface->uv[2]);
477 if(tface->flag & TF_SEL4) bglVertex2fv(tface->uv[3]);
489 static unsigned int *get_part_from_ibuf(ImBuf *ibuf, short startx, short starty, short endx, short endy)
491 unsigned int *rt, *rp, *rectmain;
492 short y, heigth, len;
494 /* the right offset in rectot */
496 rt= ibuf->rect+ (starty*ibuf->x+ startx);
499 heigth= (endy-starty);
501 rp=rectmain= MEM_mallocN(heigth*len*sizeof(int), "rect");
503 for(y=0; y<heigth; y++) {
504 memcpy(rp, rt, len*4);
511 static void draw_image_transform(ImBuf *ibuf)
514 float aspx, aspy, center[3];
516 BIF_drawConstraint();
518 if(ibuf==0 || ibuf->rect==0 || ibuf->x==0 || ibuf->y==0) {
526 BIF_getPropCenter(center);
528 /* scale and translate the circle into place and draw it */
530 glScalef(aspx, aspy, 1.0);
531 glTranslatef((1/aspx)*center[0] - center[0],
532 (1/aspy)*center[1] - center[1], 0.0);
534 BIF_drawPropCircle();
540 static void draw_image_view_icon(void)
545 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
547 if(G.sima->flag & SI_STICKYUVS) {
548 BIF_draw_icon(xPos, 5.0, ICON_STICKY2_UVS);
551 else if(G.sima->flag & SI_LOCALSTICKY) {
552 BIF_draw_icon(xPos, 5.0, ICON_STICKY_UVS);
556 if(G.sima->flag & SI_SELACTFACE) {
557 BIF_draw_icon(xPos, 5.0, ICON_DRAW_UVFACES);
560 glBlendFunc(GL_ONE, GL_ZERO);
564 /* ************ panel stuff ************* */
566 // button define is local, only events defined here possible
567 #define B_TRANS_IMAGE 1
569 /* is used for both read and write... */
570 static void image_editvertex_buts(uiBlock *block)
572 static float ocent[2];
573 float cent[2]= {0.0, 0.0};
575 int i, nactive= 0, step, digits;
578 if( is_uv_tface_editing_allowed_silent()==0 ) return;
581 if (G.sima->image && G.sima->image->ibuf) {
582 imx= G.sima->image->ibuf->x;
583 imy= G.sima->image->ibuf->y;
587 for (i=0; i<me->totface; i++) {
588 MFace *mf= &((MFace*) me->mface)[i];
589 TFace *tf= &((TFace*) me->tface)[i];
591 if (!(tf->flag & TF_SELECT))
594 if (tf->flag & TF_SEL1) {
595 cent[0]+= tf->uv[0][0];
596 cent[1]+= tf->uv[0][1];
599 if (tf->flag & TF_SEL2) {
600 cent[0]+= tf->uv[1][0];
601 cent[1]+= tf->uv[1][1];
604 if (tf->flag & TF_SEL3) {
605 cent[0]+= tf->uv[2][0];
606 cent[1]+= tf->uv[2][1];
609 if (mf->v4 && (tf->flag & TF_SEL4)) {
610 cent[0]+= tf->uv[3][0];
611 cent[1]+= tf->uv[3][1];
616 if(block) { // do the buttons
618 ocent[0]= cent[0]/nactive;
619 ocent[1]= cent[1]/nactive;
620 if (G.sima->flag & SI_COORDFLOATS) {
631 uiDefBut(block, LABEL, 0, "UV Vertex:",10,55,302,19,0,0,0,0,0,"");
632 uiBlockBeginAlign(block);
634 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex X:", 10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
635 uiDefButF(block, NUM, B_TRANS_IMAGE, "Vertex Y:", 10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
638 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median X:", 10, 35, 290, 19, &ocent[0], -10*imx, 10.0*imx, step, digits, "");
639 uiDefButF(block, NUM, B_TRANS_IMAGE, "Median Y:", 10, 15, 290, 19, &ocent[1], -10*imy, 10.0*imy, step, digits, "");
641 uiBlockEndAlign(block);
644 else { // apply event
647 cent[0]= cent[0]/nactive;
648 cent[1]= cent[1]/nactive;
650 if (G.sima->flag & SI_COORDFLOATS) {
651 delta[0]= ocent[0]-cent[0];
652 delta[1]= ocent[1]-cent[1];
655 delta[0]= ocent[0]/imx - cent[0];
656 delta[1]= ocent[1]/imy - cent[1];
659 for (i=0; i<me->totface; i++) {
660 MFace *mf= &((MFace*) me->mface)[i];
661 TFace *tf= &((TFace*) me->tface)[i];
663 if (!(tf->flag & TF_SELECT))
666 if (tf->flag & TF_SEL1) {
667 tf->uv[0][0]+= delta[0];
668 tf->uv[0][1]+= delta[1];
670 if (tf->flag & TF_SEL2) {
671 tf->uv[1][0]+= delta[0];
672 tf->uv[1][1]+= delta[1];
674 if (tf->flag & TF_SEL3) {
675 tf->uv[2][0]+= delta[0];
676 tf->uv[2][1]+= delta[1];
678 if (mf->v4 && (tf->flag & TF_SEL4)) {
679 tf->uv[3][0]+= delta[0];
680 tf->uv[3][1]+= delta[1];
684 allqueue(REDRAWVIEW3D, 0);
685 allqueue(REDRAWIMAGE, 0);
690 void do_imagebuts(unsigned short event)
694 image_editvertex_buts(NULL);
698 if(G.f & G_FACESELECT) {
699 make_repbind(G.sima->image);
700 image_changed(G.sima, 1);
702 allqueue(REDRAWVIEW3D, 0);
703 allqueue(REDRAWIMAGE, 0);
707 image_changed(G.sima, 2); /* 2: only tileflag */
708 allqueue(REDRAWVIEW3D, 0);
709 allqueue(REDRAWIMAGE, 0);
719 if(ima->flag & IMA_TWINANIM) {
720 nr= ima->xrep*ima->yrep;
721 if(ima->twsta>=nr) ima->twsta= 1;
722 if(ima->twend>=nr) ima->twend= nr-1;
723 if(ima->twsta>ima->twend) ima->twsta= 1;
724 allqueue(REDRAWIMAGE, 0);
732 static void image_panel_properties(short cntrl) // IMAGE_HANDLER_PROPERTIES
736 block= uiNewBlock(&curarea->uiblocks, "image_panel_properties", UI_EMBOSS, UI_HELV, curarea->win);
737 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
738 uiSetPanelHandler(IMAGE_HANDLER_PROPERTIES); // for close and esc
739 if(uiNewPanel(curarea, block, "Properties", "Image", 10, 230, 318, 204)==0)
742 if (G.sima->image && G.sima->image->ibuf) {
745 sprintf(str, "Image: size %d x %d", G.sima->image->ibuf->x, G.sima->image->ibuf->y);
746 uiDefBut(block, LABEL, B_NOP, str, 10,180,300,19, 0, 0, 0, 0, 0, "");
748 uiBlockBeginAlign(block);
749 uiDefButBitS(block, TOG, IMA_TWINANIM, B_TWINANIM, "Anim", 10,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of animated texture");
750 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");
751 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");
752 uiDefButS(block, NUM, B_NOP, "Speed", 10,90,140,19, &G.sima->image->animspeed, 1.0, 100.0, 0, 0, "Displays Speed of the animation in frames per second");
753 uiBlockEndAlign(block);
755 uiBlockBeginAlign(block);
756 uiDefButBitS(block, TOG, IMA_TILES, B_SIMAGEDRAW1, "Tiles", 160,150,140,19, &G.sima->image->tpageflag, 0, 0, 0, 0, "Toggles use of tilemode for faces");
757 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");
758 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");
759 uiBlockBeginAlign(block);
762 image_editvertex_buts(block);
765 static void image_panel_paint(short cntrl) // IMAGE_HANDLER_PROPERTIES
767 extern VPaint Gvp; /* from vpaint - this was copied from the paint panel*/
768 static float hsv[3], old[3]; // used as temp mem for picker
771 block= uiNewBlock(&curarea->uiblocks, "image_panel_paint", UI_EMBOSS, UI_HELV, curarea->win);
772 uiPanelControl(UI_PNL_SOLID | UI_PNL_CLOSE | cntrl);
773 uiSetPanelHandler(IMAGE_HANDLER_PAINT); // for close and esc
774 if(uiNewPanel(curarea, block, "Paint", "Image", 10, 230, 318, 204)==0)
777 uiBlockPickerButtons(block, &Gvp.r, hsv, old, 'f', B_NOP); /* 'f' is for floating panel */
779 // offset= FPICK+2*DPICK+BPICK in interface.c... this goes wrong when defines change
780 uiBlockBeginAlign(block);
781 uiDefButF(block, NUM, B_NOP, "A: ", 180+12+24,160,80,20, &Gvp.a, 0.0, 1.0, 0, 0, "The amount of pressure on the brush");
782 uiDefButF(block, NUM, B_NOP, "Size ", 180+12+24,140,80,20, &Gvp.size, 2.0, 64.0, 0, 0, "The size of the brush");
785 static void image_blockhandlers(ScrArea *sa)
787 SpaceImage *sima= sa->spacedata.first;
790 /* warning; blocks need to be freed each time, handlers dont remove */
791 uiFreeBlocksWin(&sa->uiblocks, sa->win);
793 for(a=0; a<SPACE_MAXHANDLER; a+=2) {
794 switch(sima->blockhandler[a]) {
796 case IMAGE_HANDLER_PROPERTIES:
797 image_panel_properties(sima->blockhandler[a+1]);
799 case IMAGE_HANDLER_PAINT:
800 image_panel_paint(sima->blockhandler[a+1]);
803 /* clear action value for event */
804 sima->blockhandler[a+1]= 0;
806 uiDrawBlocksPanels(sa, 0);
811 void drawimagespace(ScrArea *sa, void *spacedata)
817 short sx, sy, dx, dy;
819 /* If derived data is used then make sure that object
820 * is up-to-date... might not be the case because updates
821 * are normally done in drawview and could get here before
824 if (!G.obedit && OBACT && (G.sima->flag&SI_DRAWSHADOW)) {
825 object_handle_update(OBACT);
828 BIF_GetThemeColor3fv(TH_BACK, col);
829 glClearColor(col[0], col[1], col[2], 0.0);
830 glClear(GL_COLOR_BUFFER_BIT);
832 bwin_clear_viewmat(sa->win); /* clear buttons view */
838 if(G.sima->image->ibuf==0) {
839 load_image(G.sima->image, IB_rect, G.sce, G.scene->r.cfra);
841 tag_image_time(G.sima->image);
842 ibuf= G.sima->image->ibuf;
845 if(ibuf==0 || ibuf->rect==0) {
846 calc_image_view(G.sima, 'f');
847 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
849 glRectf(0.0, 0.0, 1.0, 1.0);
854 x1= (curarea->winx-G.sima->zoom*ibuf->x)/2;
855 y1= (curarea->winy-G.sima->zoom*ibuf->y)/2;
857 x1-= G.sima->zoom*G.sima->xof;
858 y1-= G.sima->zoom*G.sima->yof;
860 /* needed for gla draw */
861 glaDefine2DArea(&curarea->winrct);
862 glPixelZoom((float)G.sima->zoom, (float)G.sima->zoom);
864 if(G.sima->flag & SI_EDITTILE) {
865 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
867 glPixelZoom(1.0, 1.0);
869 dx= ibuf->x/G.sima->image->xrep;
870 dy= ibuf->y/G.sima->image->yrep;
871 sy= (G.sima->curtile / G.sima->image->xrep);
872 sx= G.sima->curtile - sy*G.sima->image->xrep;
877 calc_image_view(G.sima, 'p'); /* pixel */
878 myortho2(G.v2d->cur.xmin, G.v2d->cur.xmax, G.v2d->cur.ymin, G.v2d->cur.ymax);
881 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx, sy, sx+dx-1, sy+dy-1); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
883 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glRects(sx+1, sy+1, sx+dx, sy+dy); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
885 else if(G.sima->mode==SI_TEXTURE) {
887 if(G.sima->image->tpageflag & IMA_TILES) {
889 /* just leave this a while */
890 if(G.sima->image->xrep<1) return;
891 if(G.sima->image->yrep<1) return;
893 if(G.sima->curtile >= G.sima->image->xrep*G.sima->image->yrep)
894 G.sima->curtile = G.sima->image->xrep*G.sima->image->yrep - 1;
896 dx= ibuf->x/G.sima->image->xrep;
897 dy= ibuf->y/G.sima->image->yrep;
899 sy= (G.sima->curtile / G.sima->image->xrep);
900 sx= G.sima->curtile - sy*G.sima->image->xrep;
905 rect= get_part_from_ibuf(ibuf, sx, sy, sx+dx, sy+dy);
907 /* rect= ibuf->rect; */
908 for(sy= 0; sy+dy<=ibuf->y; sy+= dy) {
909 for(sx= 0; sx+dx<=ibuf->x; sx+= dx) {
910 glaDrawPixelsSafe(x1+sx*G.sima->zoom, y1+sy*G.sima->zoom, dx, dy, rect);
917 glaDrawPixelsSafe(x1, y1, ibuf->x, ibuf->y, ibuf->rect);
919 glPixelZoom(1.0, 1.0);
924 calc_image_view(G.sima, 'f'); /* float */
927 draw_image_transform(ibuf);
929 myortho2(-0.375, sa->winx-0.375, -0.375, sa->winy-0.375);
930 draw_image_view_icon();
931 draw_area_emboss(sa);
933 /* it is important to end a view in a transform compatible with buttons */
934 bwin_scalematrix(sa->win, G.sima->blockscale, G.sima->blockscale, G.sima->blockscale);
935 image_blockhandlers(sa);
937 curarea->win_swap= WIN_BACK_OK;
940 static void image_zoom_power_of_two(void)
942 /* Make zoom a power of 2 */
944 G.sima->zoom = 1 / G.sima->zoom;
945 G.sima->zoom = log(G.sima->zoom) / log(2);
946 G.sima->zoom = ceil(G.sima->zoom);
947 G.sima->zoom = pow(2, G.sima->zoom);
948 G.sima->zoom = 1 / G.sima->zoom;
951 static void image_zoom_set_factor(float zoomfac)
953 SpaceImage *sima= curarea->spacedata.first;
959 sima->zoom *= zoomfac;
961 if (sima->zoom > 0.1f && sima->zoom < 4.0f)
964 /* check zoom limits */
966 calc_image_view(G.sima, 'p');
970 if (sima->image->ibuf) {
971 width= sima->image->ibuf->x;
972 height= sima->image->ibuf->y;
976 height *= sima->zoom;
978 if ((width < 4) && (height < 4))
979 sima->zoom /= zoomfac;
980 else if((curarea->winrct.xmax - curarea->winrct.xmin) <= sima->zoom)
981 sima->zoom /= zoomfac;
982 else if((curarea->winrct.ymax - curarea->winrct.ymin) <= sima->zoom)
983 sima->zoom /= zoomfac;
986 void image_viewmove(int mode)
988 short mval[2], mvalo[2], zoom0;
990 getmouseco_sc(mvalo);
993 while(get_mbut()&(L_MOUSE|M_MOUSE)) {
997 if(mvalo[0]!=mval[0] || mvalo[1]!=mval[1]) {
1000 G.sima->xof += (mvalo[0]-mval[0])/G.sima->zoom;
1001 G.sima->yof += (mvalo[1]-mval[1])/G.sima->zoom;
1006 factor= 1.0+(float)(mvalo[0]-mval[0]+mvalo[1]-mval[1])/300.0;
1007 image_zoom_set_factor(factor);
1013 scrarea_do_windraw(curarea);
1014 screen_swapbuffers();
1016 else BIF_wait_for_statechange();
1020 void image_viewzoom(unsigned short event, int invert)
1022 SpaceImage *sima= curarea->spacedata.first;
1024 if(event==WHEELDOWNMOUSE || event==PADMINUS)
1025 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 1.25: 0.8);
1026 else if(event==WHEELUPMOUSE || event==PADPLUSKEY)
1027 image_zoom_set_factor((U.uiflag & USER_WHEELZOOMDIR)? 0.8: 1.25);
1028 else if(event==PAD1)
1030 else if(event==PAD2)
1031 sima->zoom= (invert)? 2.0: 0.5;
1032 else if(event==PAD4)
1033 sima->zoom= (invert)? 4.0: 0.25;
1034 else if(event==PAD8)
1035 sima->zoom= (invert)? 8.0: 0.125;
1041 * Updates the fields of the View2D member of the SpaceImage struct.
1042 * Default behavior is to reset the position of the image and set the zoom to 1
1043 * If the image will not fit within the window rectangle, the zoom is adjusted
1048 void image_home(void)
1053 if (curarea->spacetype != SPACE_IMAGE) return;
1054 if ((G.sima->image == 0) || (G.sima->image->ibuf == 0)) return;
1056 /* Check if the image will fit in the image with zoom==1 */
1057 width = curarea->winx;
1058 height = curarea->winy;
1059 if (((G.sima->image->ibuf->x >= width) || (G.sima->image->ibuf->y >= height)) &&
1060 ((width > 0) && (height > 0))) {
1061 /* Find the zoom value that will fit the image in the image space */
1062 zoomX = ((float)width) / ((float)G.sima->image->ibuf->x);
1063 zoomY = ((float)height) / ((float)G.sima->image->ibuf->y);
1064 G.sima->zoom= MIN2(zoomX, zoomY);
1066 image_zoom_power_of_two();
1069 G.sima->zoom= (float)1;
1072 G.sima->xof= G.sima->yof= 0;
1074 calc_image_view(G.sima, 'p');
1076 scrarea_queue_winredraw(curarea);
1079 void image_viewcentre(void)
1081 float size, min[2], max[2], d[2], xim=256.0f, yim=256.0f;
1083 if( is_uv_tface_editing_allowed()==0 ) return;
1085 if (!minmax_tface_uv(min, max)) return;
1087 if(G.sima->image && G.sima->image->ibuf) {
1088 xim= G.sima->image->ibuf->x;
1089 yim= G.sima->image->ibuf->y;
1092 G.sima->xof= ((min[0] + max[0])*0.5f - 0.5f)*xim;
1093 G.sima->yof= ((min[1] + max[1])*0.5f - 0.5f)*yim;
1095 d[0] = max[0] - min[0];
1096 d[1] = max[1] - min[1];
1097 size= 0.5*MAX2(d[0], d[1])*MAX2(xim, yim)/256.0f;
1099 if(size<=0.01) size= 0.01;
1101 G.sima->zoom= 0.7/size;
1103 calc_image_view(G.sima, 'p');
1105 scrarea_queue_winredraw(curarea);