svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r20855:20928
[blender.git] / source / blender / editors / interface / interface_icons.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributors: Blender Foundation, full recode
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include <math.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #ifndef WIN32
33 #include <unistd.h>
34 #else
35 #include <io.h>
36 #include <direct.h>
37 #endif   
38 #include "MEM_guardedalloc.h"
39
40 #include "BLI_arithb.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_storage_types.h"
43
44 #include "DNA_screen_types.h"
45 #include "DNA_userdef_types.h"
46
47
48 #include "BKE_utildefines.h"
49 #include "BKE_image.h"
50 #include "BKE_icons.h"
51
52 #include "IMB_imbuf.h"
53 #include "IMB_imbuf_types.h"
54
55 #include "BIF_gl.h"
56 #include "BIF_glutil.h"
57 #include "UI_interface.h"
58 #include "UI_interface_icons.h"
59
60 // XXX #include "BIF_previewrender.h"
61 // XXX #include "BIF_screen.h"
62
63 #include "UI_resources.h" /* elubie: should be removed once the enum for the ICONS is in BIF_preview_icons.h */
64 #include "interface_intern.h"
65 #include "ED_datafiles.h"
66
67 #define ICON_IMAGE_W            600
68 #define ICON_IMAGE_H            640
69
70 #define ICON_GRID_COLS          26
71 #define ICON_GRID_ROWS          30
72
73 #define ICON_GRID_MARGIN        5
74 #define ICON_GRID_W             16
75 #define ICON_GRID_H             16
76
77 typedef struct IconImage {
78         int w;
79         int h;
80         unsigned int *rect; 
81 } IconImage;
82
83 typedef void (*VectorDrawFunc)(int x, int y, int w, int h, float alpha);
84
85 typedef struct DrawInfo {
86         int w;
87         int h;
88         float aspect;
89         VectorDrawFunc drawFunc; /* If drawFunc is defined then it is a vector icon, otherwise use rect */              
90         IconImage* icon;
91 } DrawInfo;
92
93 /* ******************* STATIC LOCAL VARS ******************* */
94 /* static here to cache results of icon directory scan, so it's not 
95  * scanning the filesystem each time the menu is drawn */
96 static struct ListBase iconfilelist = {0, 0};
97
98
99 /* **************************************************** */
100
101 static void def_internal_icon(ImBuf *bbuf, int icon_id, int xofs, int yofs, int size)
102 {
103         Icon *new_icon = NULL;
104         IconImage *iimg = NULL;
105         DrawInfo *di;
106         int y = 0;
107         int imgsize = 0;
108
109         new_icon = MEM_callocN(sizeof(Icon), "texicon");
110
111         new_icon->obj = 0; /* icon is not for library object */
112         new_icon->type = 0;     
113
114         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
115         di->drawFunc = 0;
116         di->w = size;
117         di->h = size;
118         di->aspect = 1.0f;
119         
120         iimg = MEM_mallocN(sizeof(IconImage), "icon_img");
121         iimg->rect = MEM_mallocN(size*size*sizeof(unsigned int), "icon_rect");
122         iimg->w = size;
123         iimg->h = size;
124
125         /* Here we store the rect in the icon - same as before */
126         imgsize = bbuf->x;
127         for (y=0; y<size; y++) {
128                 memcpy(&iimg->rect[y*size], &bbuf->rect[(y+yofs)*imgsize+xofs], size*sizeof(int));
129         }
130
131         di->icon = iimg;
132
133         new_icon->drawinfo_free = UI_icons_free_drawinfo;
134         new_icon->drawinfo = di;
135
136         BKE_icon_set(icon_id, new_icon);
137 }
138
139 static void def_internal_vicon( int icon_id, VectorDrawFunc drawFunc)
140 {
141         Icon *new_icon = NULL;
142         DrawInfo* di;
143
144         new_icon = MEM_callocN(sizeof(Icon), "texicon");
145
146         new_icon->obj = 0; /* icon is not for library object */
147         new_icon->type = 0;
148
149         di = MEM_callocN(sizeof(DrawInfo), "drawinfo");
150         di->drawFunc =drawFunc;
151         di->w = ICON_DEFAULT_HEIGHT;
152         di->h = ICON_DEFAULT_HEIGHT;
153         di->aspect = 1.0f;
154         di->icon = NULL;
155
156         new_icon->drawinfo_free = 0;
157         new_icon->drawinfo = di;
158
159         BKE_icon_set(icon_id, new_icon);
160 }
161
162 /* Vector Icon Drawing Routines */
163
164         /* Utilities */
165
166 static void viconutil_set_point(GLint pt[2], int x, int y)
167 {
168         pt[0] = x;
169         pt[1] = y;
170 }
171
172 static void viconutil_draw_tri(GLint (*pts)[2])
173 {
174         glBegin(GL_TRIANGLES);
175         glVertex2iv(pts[0]);
176         glVertex2iv(pts[1]);
177         glVertex2iv(pts[2]);
178         glEnd();
179 }
180
181 static void viconutil_draw_lineloop(GLint (*pts)[2], int numPoints)
182 {
183         int i;
184
185         glBegin(GL_LINE_LOOP);
186         for (i=0; i<numPoints; i++) {
187                 glVertex2iv(pts[i]);
188         }
189         glEnd();
190 }
191
192 static void viconutil_draw_lineloop_smooth(GLint (*pts)[2], int numPoints)
193 {
194         glEnable(GL_LINE_SMOOTH);
195         viconutil_draw_lineloop(pts, numPoints);
196         glDisable(GL_LINE_SMOOTH);
197 }
198
199 static void viconutil_draw_points(GLint (*pts)[2], int numPoints, int pointSize)
200 {
201         int i;
202
203         glBegin(GL_QUADS);
204         for (i=0; i<numPoints; i++) {
205                 int x = pts[i][0], y = pts[i][1];
206
207                 glVertex2i(x-pointSize,y-pointSize);
208                 glVertex2i(x+pointSize,y-pointSize);
209                 glVertex2i(x+pointSize,y+pointSize);
210                 glVertex2i(x-pointSize,y+pointSize);
211         }
212         glEnd();
213 }
214
215         /* Drawing functions */
216
217 static void vicon_x_draw(int x, int y, int w, int h, float alpha)
218 {
219         x += 3;
220         y += 3;
221         w -= 6;
222         h -= 6;
223
224         glEnable( GL_LINE_SMOOTH );
225
226         glLineWidth(2.5);
227         
228         glColor4f(0.0, 0.0, 0.0, alpha);
229         glBegin(GL_LINES);
230         glVertex2i(x  ,y  );
231         glVertex2i(x+w,y+h);
232         glVertex2i(x+w,y  );
233         glVertex2i(x  ,y+h);
234         glEnd();
235
236         glLineWidth(1.0);
237         
238         glDisable( GL_LINE_SMOOTH );
239 }
240
241 static void vicon_view3d_draw(int x, int y, int w, int h, float alpha)
242 {
243         int cx = x + w/2;
244         int cy = y + h/2;
245         int d = MAX2(2, h/3);
246
247         glColor4f(0.5, 0.5, 0.5, alpha);
248         glBegin(GL_LINES);
249         glVertex2i(x  , cy-d);
250         glVertex2i(x+w, cy-d);
251         glVertex2i(x  , cy+d);
252         glVertex2i(x+w, cy+d);
253
254         glVertex2i(cx-d, y  );
255         glVertex2i(cx-d, y+h);
256         glVertex2i(cx+d, y  );
257         glVertex2i(cx+d, y+h);
258         glEnd();
259         
260         glColor4f(0.0, 0.0, 0.0, alpha);
261         glBegin(GL_LINES);
262         glVertex2i(x  , cy);
263         glVertex2i(x+w, cy);
264         glVertex2i(cx, y  );
265         glVertex2i(cx, y+h);
266         glEnd();
267 }
268
269 static void vicon_edit_draw(int x, int y, int w, int h, float alpha)
270 {
271         GLint pts[4][2];
272
273         viconutil_set_point(pts[0], x+3  , y+3  );
274         viconutil_set_point(pts[1], x+w-3, y+3  );
275         viconutil_set_point(pts[2], x+w-3, y+h-3);
276         viconutil_set_point(pts[3], x+3  , y+h-3);
277
278         glColor4f(0.0, 0.0, 0.0, alpha);
279         viconutil_draw_lineloop(pts, 4);
280
281         glColor3f(1, 1, 0.0);
282         viconutil_draw_points(pts, 4, 1);
283 }
284
285 static void vicon_editmode_hlt_draw(int x, int y, int w, int h, float alpha)
286 {
287         GLint pts[3][2];
288
289         viconutil_set_point(pts[0], x+w/2, y+h-2);
290         viconutil_set_point(pts[1], x+3, y+4);
291         viconutil_set_point(pts[2], x+w-3, y+4);
292
293         glColor4f(0.5, 0.5, 0.5, alpha);
294         viconutil_draw_tri(pts);
295
296         glColor4f(0.0, 0.0, 0.0, 1);
297         viconutil_draw_lineloop_smooth(pts, 3);
298
299         glColor3f(1, 1, 0.0);
300         viconutil_draw_points(pts, 3, 1);
301 }
302
303 static void vicon_editmode_dehlt_draw(int x, int y, int w, int h, float alpha)
304 {
305         GLint pts[3][2];
306
307         viconutil_set_point(pts[0], x+w/2, y+h-2);
308         viconutil_set_point(pts[1], x+3, y+4);
309         viconutil_set_point(pts[2], x+w-3, y+4);
310
311         glColor4f(0.0f, 0.0f, 0.0f, 1);
312         viconutil_draw_lineloop_smooth(pts, 3);
313
314         glColor3f(.9f, .9f, .9f);
315         viconutil_draw_points(pts, 3, 1);
316 }
317
318 static void vicon_disclosure_tri_right_draw(int x, int y, int w, int h, float alpha)
319 {
320         GLint pts[3][2];
321         int cx = x+w/2;
322         int cy = y+w/2;
323         int d = w/3, d2 = w/5;
324
325         viconutil_set_point(pts[0], cx-d2, cy+d);
326         viconutil_set_point(pts[1], cx-d2, cy-d);
327         viconutil_set_point(pts[2], cx+d2, cy);
328
329         glShadeModel(GL_SMOOTH);
330         glBegin(GL_TRIANGLES);
331         glColor4f(0.8f, 0.8f, 0.8f, alpha);
332         glVertex2iv(pts[0]);
333         glVertex2iv(pts[1]);
334         glColor4f(0.3f, 0.3f, 0.3f, alpha);
335         glVertex2iv(pts[2]);
336         glEnd();
337         glShadeModel(GL_FLAT);
338
339         glColor4f(0.0f, 0.0f, 0.0f, 1);
340         viconutil_draw_lineloop_smooth(pts, 3);
341 }
342
343 static void vicon_disclosure_tri_down_draw(int x, int y, int w, int h, float alpha)
344 {
345         GLint pts[3][2];
346         int cx = x+w/2;
347         int cy = y+w/2;
348         int d = w/3, d2 = w/5;
349
350         viconutil_set_point(pts[0], cx+d, cy+d2);
351         viconutil_set_point(pts[1], cx-d, cy+d2);
352         viconutil_set_point(pts[2], cx, cy-d2);
353
354         glShadeModel(GL_SMOOTH);
355         glBegin(GL_TRIANGLES);
356         glColor4f(0.8f, 0.8f, 0.8f, alpha);
357         glVertex2iv(pts[0]);
358         glVertex2iv(pts[1]);
359         glColor4f(0.3f, 0.3f, 0.3f, alpha);
360         glVertex2iv(pts[2]);
361         glEnd();
362         glShadeModel(GL_FLAT);
363
364         glColor4f(0.0f, 0.0f, 0.0f, 1);
365         viconutil_draw_lineloop_smooth(pts, 3);
366 }
367
368 static void vicon_move_up_draw(int x, int y, int w, int h, float alpha)
369 {
370         int d=-2;
371
372         glEnable(GL_LINE_SMOOTH);
373         glLineWidth(1);
374         glColor3f(0.0, 0.0, 0.0);
375
376         glBegin(GL_LINE_STRIP);
377         glVertex2i(x+w/2-d*2, y+h/2+d);
378         glVertex2i(x+w/2, y+h/2-d + 1);
379         glVertex2i(x+w/2+d*2, y+h/2+d);
380         glEnd();
381
382         glLineWidth(1.0);
383         glDisable(GL_LINE_SMOOTH);
384 }
385
386 static void vicon_move_down_draw(int x, int y, int w, int h, float alpha)
387 {
388         int d=2;
389
390         glEnable(GL_LINE_SMOOTH);
391         glLineWidth(1);
392         glColor3f(0.0, 0.0, 0.0);
393
394         glBegin(GL_LINE_STRIP);
395         glVertex2i(x+w/2-d*2, y+h/2+d);
396         glVertex2i(x+w/2, y+h/2-d - 1);
397         glVertex2i(x+w/2+d*2, y+h/2+d);
398         glEnd();
399
400         glLineWidth(1.0);
401         glDisable(GL_LINE_SMOOTH);
402 }
403
404 static void init_internal_icons()
405 {
406         bTheme *btheme= U.themes.first;
407         ImBuf *bbuf= NULL;
408         int x, y;
409         char iconfilestr[FILE_MAXDIR+FILE_MAXFILE];
410         char filenamestr[FILE_MAXFILE+16];      // 16 == strlen(".blender/icons/")+1
411         
412         if ((btheme!=NULL) && (strlen(btheme->tui.iconfile) > 0)) {
413         
414 #ifdef WIN32
415                 sprintf(filenamestr, "icons/%s", btheme->tui.iconfile);
416 #else
417                 sprintf(filenamestr, ".blender/icons/%s", btheme->tui.iconfile);
418 #endif
419                 
420                 BLI_make_file_string("/", iconfilestr, BLI_gethome(), filenamestr);
421                 
422                 if (BLI_exists(iconfilestr)) {
423                         bbuf = IMB_loadiffname(iconfilestr, IB_rect);
424                         if(bbuf->x < ICON_IMAGE_W || bbuf->y < ICON_IMAGE_H) {
425                                 printf("\n***WARNING***\nIcons file %s too small.\nUsing built-in Icons instead\n", iconfilestr);
426                                 IMB_freeImBuf(bbuf);
427                                 bbuf= NULL;
428                 }
429         }
430         }
431         if(bbuf==NULL)
432                 bbuf = IMB_ibImageFromMemory((int *)datatoc_blenderbuttons, datatoc_blenderbuttons_size, IB_rect);
433
434         for (y=0; y<ICON_GRID_ROWS; y++) {
435                 for (x=0; x<ICON_GRID_COLS; x++) {
436                         def_internal_icon(bbuf, BIFICONID_FIRST + y*ICON_GRID_COLS + x,
437                                 x*(ICON_GRID_W+ICON_GRID_MARGIN)+ICON_GRID_MARGIN,
438                                 y*(ICON_GRID_H+ICON_GRID_MARGIN)+ICON_GRID_MARGIN, ICON_GRID_W);
439                 }
440         }
441
442         def_internal_vicon(VICON_VIEW3D, vicon_view3d_draw);
443         def_internal_vicon(VICON_EDIT, vicon_edit_draw);
444         def_internal_vicon(VICON_EDITMODE_DEHLT, vicon_editmode_dehlt_draw);
445         def_internal_vicon(VICON_EDITMODE_HLT, vicon_editmode_hlt_draw);
446         def_internal_vicon(VICON_DISCLOSURE_TRI_RIGHT, vicon_disclosure_tri_right_draw);
447         def_internal_vicon(VICON_DISCLOSURE_TRI_DOWN, vicon_disclosure_tri_down_draw);
448         def_internal_vicon(VICON_MOVE_UP, vicon_move_up_draw);
449         def_internal_vicon(VICON_MOVE_DOWN, vicon_move_down_draw);
450         def_internal_vicon(VICON_X, vicon_x_draw);
451
452         IMB_freeImBuf(bbuf);
453 }
454
455
456 static void init_iconfile_list(struct ListBase *list)
457 {
458         IconFile *ifile;
459         ImBuf *bbuf= NULL;
460         struct direntry *dir;
461         int restoredir = 1; /* restore to current directory */
462         int totfile, i, index=1;
463         int ifilex, ifiley;
464         char icondirstr[FILE_MAX];
465         char iconfilestr[FILE_MAX+16]; /* allow 256 chars for file+dir */
466         char olddir[FILE_MAX];
467         
468         list->first = list->last = NULL;
469
470 #ifdef WIN32
471         BLI_make_file_string("/", icondirstr, BLI_gethome(), "icons");
472 #else
473         BLI_make_file_string("/", icondirstr, BLI_gethome(), ".blender/icons");
474 #endif
475         
476         if(BLI_exists(icondirstr)==0)
477                 return;
478         
479         /* since BLI_getdir changes the current working directory, restore it 
480            back to old value afterwards */
481         if(!BLI_getwdN(olddir)) 
482                 restoredir = 0;
483         totfile = BLI_getdir(icondirstr, &dir);
484         if (restoredir)
485                 chdir(olddir);
486
487         for(i=0; i<totfile; i++) {
488                 if( (dir[i].type & S_IFREG) ) {
489                         char *filename = dir[i].relname;
490                         
491                         if(BLI_testextensie(filename, ".png")) {
492                         
493                                 /* check to see if the image is the right size, continue if not */
494                                 /* copying strings here should go ok, assuming that we never get back
495                                    a complete path to file longer than 256 chars */
496                                 sprintf(iconfilestr, "%s/%s", icondirstr, filename);
497                                 if(BLI_exists(iconfilestr)) bbuf = IMB_loadiffname(iconfilestr, IB_rect);
498                                 
499                                 ifilex = bbuf->x;
500                                 ifiley = bbuf->y;
501                                 IMB_freeImBuf(bbuf);
502                                 
503                                 if ((ifilex != ICON_IMAGE_W) || (ifiley != ICON_IMAGE_H))
504                                         continue;
505                         
506                                 /* found a potential icon file, so make an entry for it in the cache list */
507                                 ifile = MEM_callocN(sizeof(IconFile), "IconFile");
508                                 
509                                 BLI_strncpy(ifile->filename, filename, sizeof(ifile->filename));
510                                 ifile->index = index;
511
512                                 BLI_addtail(list, ifile);
513                                 
514                                 index++;
515                         }
516                 }
517         }
518         
519         /* free temporary direntry structure that's been created by BLI_getdir() */
520         i= totfile-1;
521         
522         for(; i>=0; i--){
523                 MEM_freeN(dir[i].relname);
524                 if (dir[i].string) MEM_freeN(dir[i].string);
525         }
526         free(dir);
527         dir= 0;
528 }
529
530 static void free_iconfile_list(struct ListBase *list)
531 {
532         IconFile *ifile=NULL, *next_ifile=NULL;
533         
534         for(ifile=list->first; ifile; ifile=next_ifile) {
535                 next_ifile = ifile->next;
536                 BLI_freelinkN(list, ifile);
537         }
538 }
539
540 int UI_iconfile_get_index(char *filename)
541 {
542         IconFile *ifile;
543         ListBase *list=&(iconfilelist);
544         
545         for(ifile=list->first; ifile; ifile=ifile->next) {
546                 if ( BLI_streq(filename, ifile->filename)) {
547                         return ifile->index;
548                 }
549         }
550         
551         return 0;
552 }
553
554 ListBase *UI_iconfile_list(void)
555 {
556         ListBase *list=&(iconfilelist);
557         
558         return list;
559 }
560
561
562 void UI_icons_free()
563 {
564         free_iconfile_list(&iconfilelist);
565         BKE_icons_free();
566 }
567
568 void UI_icons_free_drawinfo(void *drawinfo)
569 {
570         DrawInfo *di = drawinfo;
571
572         if (di)
573         {
574                 if (di->icon) {
575                         MEM_freeN(di->icon->rect);
576                         MEM_freeN(di->icon);
577                 }
578                 MEM_freeN(di);
579         }
580 }
581
582 static DrawInfo *icon_create_drawinfo()
583 {
584         DrawInfo *di = NULL;
585
586         di = MEM_callocN(sizeof(DrawInfo), "di_icon");
587         
588         di->drawFunc = 0;
589         di->w = ICON_DEFAULT_HEIGHT;
590         di->h = ICON_DEFAULT_HEIGHT;
591         di->icon = NULL;
592         di->aspect = 1.0f;
593
594         return di;
595 }
596
597 int UI_icon_get_width(int icon_id)
598 {
599         Icon *icon = NULL;
600         DrawInfo *di = NULL;
601
602         icon = BKE_icon_get(icon_id);
603         
604         if (!icon) {
605                 printf("UI_icon_get_width: Internal error, no icon for icon ID: %d\n", icon_id);
606                 return 0;
607         }
608         
609         di = (DrawInfo *)icon->drawinfo;
610         if (!di) {
611                 di = icon_create_drawinfo();
612                 icon->drawinfo = di;
613         }
614
615         if (di)
616                 return di->w;
617
618         return 0;
619 }
620
621 int UI_icon_get_height(int icon_id)
622 {
623         Icon *icon = NULL;
624         DrawInfo *di = NULL;
625
626         icon = BKE_icon_get(icon_id);
627         
628         if (!icon) {
629                 printf("UI_icon_get_height: Internal error, no icon for icon ID: %d\n", icon_id);
630                 return 0;
631         }
632         
633         di = (DrawInfo*)icon->drawinfo;
634
635         if (!di) {
636                 di = icon_create_drawinfo();
637                 icon->drawinfo = di;
638         }
639         
640         if (di)
641                 return di->h;
642
643         return 0;
644 }
645
646 void UI_icons_init(int first_dyn_id)
647 {
648         init_iconfile_list(&iconfilelist);
649         BKE_icons_init(first_dyn_id);
650         init_internal_icons();
651 }
652
653 #if 0
654 static void icon_copy_rect(ImBuf *ibuf, unsigned int w, unsigned int h, unsigned int *rect)
655 {
656         struct ImBuf *ima;
657         unsigned int *drect, *srect;
658         float scaledx, scaledy;
659         short ex, ey, dx, dy;
660
661         /* paranoia test */
662         if(ibuf==NULL || (ibuf->rect==NULL && ibuf->rect_float==NULL))
663                 return;
664         
665         /* waste of cpu cyles... but the imbuf API has no other way to scale fast (ton) */
666         ima = IMB_dupImBuf(ibuf);
667         
668         if (!ima) 
669                 return;
670         
671         if (ima->x > ima->y) {
672                 scaledx = (float)w;
673                 scaledy =  ( (float)ima->y/(float)ima->x )*(float)w;
674         }
675         else {                  
676                 scaledx =  ( (float)ima->x/(float)ima->y )*(float)h;
677                 scaledy = (float)h;
678         }
679         
680         ex = (short)scaledx;
681         ey = (short)scaledy;
682         
683         dx = (w - ex) / 2;
684         dy = (h - ey) / 2;
685         
686         IMB_scalefastImBuf(ima, ex, ey);
687         
688         /* if needed, convert to 32 bits */
689         if(ima->rect==NULL)
690                 IMB_rect_from_float(ima);
691
692         srect = ima->rect;
693         drect = rect;
694
695         drect+= dy*w+dx;
696         for (;ey > 0; ey--){            
697                 memcpy(drect,srect, ex * sizeof(int));
698                 drect += w;
699                 srect += ima->x;
700         }
701         IMB_freeImBuf(ima);
702 }
703
704 /* Render size for preview images at level miplevel */
705 static int preview_render_size(int miplevel)
706 {
707         switch (miplevel) {
708                 case 0: return 32;
709                 case 1: return PREVIEW_DEFAULT_HEIGHT;
710         }
711         return 0;
712 }
713
714 static void icon_create_mipmap(struct PreviewImage* prv_img, int miplevel) 
715 {
716         unsigned int size = preview_render_size(miplevel);
717
718         if (!prv_img) {
719                 printf("Error: requested preview image does not exist");
720         }
721         if (!prv_img->rect[miplevel]) {
722                 prv_img->w[miplevel] = size;
723                 prv_img->h[miplevel] = size;
724                 prv_img->changed[miplevel] = 1;
725                 prv_img->rect[miplevel] = MEM_callocN(size*size*sizeof(unsigned int), "prv_rect"); 
726         }
727 }
728
729 /* create single icon from jpg, png etc. */
730 static void icon_from_image(Image *img, int miplevel)
731 {
732         unsigned int pr_size;
733         short image_loaded = 0;
734         struct ImBuf* ibuf=NULL;
735         PreviewImage* pi;
736
737         /* img->ok is zero when Image cannot load */
738         if (img==NULL || img->ok==0)
739                 return;
740
741         /* elubie: this needs to be changed: here image is always loaded if not
742            already there. Very expensive for large images. Need to find a way to 
743            only get existing ibuf */
744         ibuf = BKE_image_get_ibuf(img, NULL);
745         if(ibuf==NULL || ibuf->rect==NULL) {
746                 return;
747         }
748         
749         pi = BKE_previewimg_get((ID*)img);      
750         
751         if(!pi) {
752                 printf("preview image could'nt be allocated");
753                 return;
754         }
755         /* we can only create the preview rect here, since loading possibly deallocated
756            old preview */
757         icon_create_mipmap(pi, miplevel);
758
759         pr_size = img->preview->w[miplevel]*img->preview->h[miplevel]*sizeof(unsigned int);
760
761         image_loaded = 1;
762         icon_copy_rect(ibuf, img->preview->w[miplevel], img->preview->h[miplevel], img->preview->rect[miplevel]);       
763 }
764
765 static void set_alpha(char* cp, int sizex, int sizey, char alpha) 
766 {
767         int x,y;
768         for(y=0; y<sizey; y++) {
769                 for(x=0; x<sizex; x++, cp+=4) {
770                         cp[3]= alpha;
771                 }
772         }
773 }
774 #endif
775
776 /* only called when icon has changed */
777 /* only call with valid pointer from UI_icon_draw */
778 static void icon_set_image(ID *id, DrawInfo *di, PreviewImage* prv_img, int miplevel)
779 {
780 #if 0 // XXX - preview renders have to be redesigned - possibly low level op (elubie)
781         RenderInfo ri;  
782         unsigned int pr_size = 0;
783         
784         if (!di) return;                                
785         
786         if (!prv_img) {
787                 printf("No preview image for this ID: %s\n", id->name);
788                 return;
789         }       
790
791         /* no drawing (see last parameter doDraw, just calculate preview image 
792                 - hopefully small enough to be fast */
793         if (GS(id->name) == ID_IM)
794                 icon_from_image((struct Image*)id, miplevel);
795         else {  
796                 /* create the preview rect */
797                 icon_create_mipmap(prv_img, miplevel);
798
799                 ri.curtile= 0;
800                 ri.tottile= 0;
801                 ri.rect = NULL;
802                 ri.pr_rectx = prv_img->w[miplevel];
803                 ri.pr_recty = prv_img->h[miplevel];
804
805                 pr_size = ri.pr_rectx*ri.pr_recty*sizeof(unsigned int);
806
807                 BIF_previewrender(id, &ri, NULL, PR_ICON_RENDER);
808
809                 /* world is rendered with alpha=0, so it wasn't displayed 
810                    this could be render option for sky to, for later */
811                 if (GS(id->name) == ID_WO) { 
812                         set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
813                 } 
814                 else if (GS(id->name) == ID_MA) {
815                         Material* mat = (Material*)id;
816                         if (mat->material_type == MA_TYPE_HALO) {
817                                 set_alpha( (char*) ri.rect, ri.pr_rectx, ri.pr_recty, 255);
818                         }
819                 }
820
821                 if (ri.rect) {
822                         memcpy(prv_img->rect[miplevel], ri.rect, pr_size);
823
824                         /* and clean up */
825                         MEM_freeN(ri.rect);
826                         ri.rect = 0;
827                 }
828         }
829 #endif
830 }
831
832 static void icon_draw_rect(float x, float y, int w, int h, float aspect, int rw, int rh, unsigned int *rect)
833 {
834         
835         glRasterPos2f(x, y);
836         // XXX ui_rasterpos_safe(x, y, aspect);
837         
838         if((w<1 || h<1)) {
839                 // XXX - TODO 2.5 verify whether this case can happen
840                 // and only print in debug
841                 printf("what the heck! - icons are %i x %i pixels?\n", w, h);
842         }
843         /* rect contains image in 'rendersize', we only scale if needed */
844         else if(rw!=w && rh!=h) {
845                 ImBuf *ima;
846                 if(w>2000 || h>2000) { /* something has gone wrong! */
847                         printf("insane icon size w=%d h=%d\n",w,h);
848                         return;
849                 }
850                 /* first allocate imbuf for scaling and copy preview into it */
851                 ima = IMB_allocImBuf(rw, rh, 32, IB_rect, 0);
852                 memcpy(ima->rect, rect, rw*rh*sizeof(unsigned int));    
853                 
854                 /* scale it */
855                 IMB_scaleImBuf(ima, w, h);
856                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, ima->rect);
857                 
858                 IMB_freeImBuf(ima);
859         }
860         else
861                 glDrawPixels(w, h, GL_RGBA, GL_UNSIGNED_BYTE, rect);
862 }
863
864 /* Drawing size for preview images at level miplevel */
865 static int preview_size(int miplevel)
866 {
867         switch (miplevel) {
868                 case 0: return ICON_DEFAULT_HEIGHT;
869                 case 1: return PREVIEW_DEFAULT_HEIGHT;
870         }
871         return 0;
872 }
873
874 static void icon_draw_size(float x, float y, int icon_id, float aspect, int miplevel, int draw_size, int nocreate)
875 {
876         Icon *icon = NULL;
877         DrawInfo *di = NULL;
878         
879         icon = BKE_icon_get(icon_id);
880         
881         if (!icon) {
882                 printf("icon_draw_mipmap: Internal error, no icon for icon ID: %d\n", icon_id);
883                 return;
884         }
885         
886         di = (DrawInfo*)icon->drawinfo;
887         
888         if (!di) {
889                 di = icon_create_drawinfo();
890         
891                 icon->drawinfo = di;            
892                 icon->drawinfo_free = UI_icons_free_drawinfo;           
893         }
894         
895         di->aspect = aspect;
896         /* scale width and height according to aspect */
897         di->w = (int)(draw_size/di->aspect + 0.5f);
898         di->h = (int)(draw_size/di->aspect + 0.5f);
899         
900         if (di->drawFunc) {
901                 /* vector icons use the uiBlock transformation, they are not drawn
902                 with untransformed coordinates like the other icons */
903                 di->drawFunc(x, y, ICON_DEFAULT_HEIGHT, ICON_DEFAULT_HEIGHT, 1.0f); 
904         } 
905         else if (di->icon) {
906                 /* it is a builtin icon */              
907                 if (!di->icon->rect) return; /* something has gone wrong! */
908
909                 icon_draw_rect(x,y,di->w, di->h, di->aspect, di->icon->w, di->icon->h, di->icon->rect);
910         }
911         else {
912                 PreviewImage* pi = BKE_previewimg_get((ID*)icon->obj); 
913
914                 if (pi) {                       
915                         if (!nocreate && (pi->changed[miplevel] ||!pi->rect[miplevel])) /* changed only ever set by dynamic icons */
916                         {
917                                 // XXX waitcursor(1);
918                                 /* create the preview rect if necessary */                              
919                                 icon_set_image((ID*)icon->obj, icon->drawinfo, pi, miplevel);
920                                 pi->changed[miplevel] = 0;
921                                 // XXX waitcursor(0);
922                         }
923                         
924                         if (!pi->rect[miplevel]) return; /* something has gone wrong! */
925                         
926                         icon_draw_rect(x,y,di->w, di->h, di->aspect, pi->w[miplevel], pi->h[miplevel], pi->rect[miplevel]);             
927                 }
928         }
929 }
930
931 static void icon_draw_mipmap(float x, float y, int icon_id, float aspect, int miplevel, int nocreate)
932 {
933         int draw_size = preview_size(miplevel);
934         icon_draw_size(x,y,icon_id, aspect, miplevel, draw_size, nocreate);
935 }
936
937
938 void UI_icon_draw_aspect(float x, float y, int icon_id, float aspect)
939 {
940         icon_draw_mipmap(x,y,icon_id, aspect, PREVIEW_MIPMAP_ZERO, 0);
941 }
942
943 void UI_icon_draw(float x, float y, int icon_id)
944 {
945         UI_icon_draw_aspect(x, y, icon_id, 1.0f);
946 }
947
948 void UI_icon_draw_size_blended(float x, float y, int size, int icon_id, int shade)
949 {
950         if(shade < 0) {
951                 float r= (128+shade)/128.0f;
952                 glPixelTransferf(GL_ALPHA_SCALE, r);
953         }
954
955         icon_draw_size(x,y,icon_id, 1.0f, 0, size, 1);
956
957         if(shade < 0)
958                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
959 }
960
961 void UI_icon_draw_preview(float x, float y, int icon_id, int nocreate)
962 {
963         icon_draw_mipmap(x,y,icon_id, 1.0f, PREVIEW_MIPMAP_LARGE, nocreate);
964 }
965
966 void UI_icon_draw_aspect_blended(float x, float y, int icon_id, float aspect, int shade)
967 {
968         if(shade < 0) {
969                 float r= (128+shade)/128.0f;
970                 glPixelTransferf(GL_ALPHA_SCALE, r);
971         }
972
973         UI_icon_draw_aspect(x, y, icon_id, aspect);
974
975         if(shade < 0)
976                 glPixelTransferf(GL_ALPHA_SCALE, 1.0f);
977 }