Browse code

Show big image on hover

Dario Rodriguez authored on 01/03/2025 22:32:30
Showing 1 changed files
... ...
@@ -16,6 +16,7 @@
16 16
  *      20250228 Navigate directories
17 17
  *      20250301 Aesthetic fixes for leftside.
18 18
  *               Basic image loading.
19
+ *               Show big image on hover.
19 20
  *
20 21
  * Author: Dario Rodriguez dario@darionomono.com
21 22
  * (c) Dario Rodriguez 2025
... ...
@@ -155,6 +156,9 @@ typedef struct body_t {
155 156
         font_t *ptrfont;
156 157
         font_t *ptrfontbig;
157 158
         font_t *ptrfonthuge;
159
+        char currenttexture[1024];
160
+        Texture2D texture;
161
+        int has_texture;
158 162
 } body_t;
159 163
 
160 164
 typedef struct im_t {
... ...
@@ -187,7 +191,7 @@ void im_body_free(body_t *body);
187 191
 int im_body_add(body_t *body,char *dir);
188 192
 
189 193
 int im_body_mouse(body_t *body, Vector2 mousepos, int lmbpressed, int lmbreleased, int lmbdown, int *click_avail);
190
-int im_body_draw(body_t *body, int windowwidth, int windowheight);
194
+int im_body_draw(body_t *body, Vector2 mousepos, int windowwidth, int windowheight);
191 195
 
192 196
 int listing_get(listing_t *listing, char *pathprefix, char *path, int flag_sort);
193 197
 void listing_freedata(listing_t *listing);
... ...
@@ -238,7 +242,7 @@ fprintf(stderr,"SELECTED: \"%s\"->\"%s\"\n",sel_menu,sel_submenu);
238 242
                 /* draw screen contents */
239 243
                 BeginDrawing();
240 244
                 ClearBackground(RAYWHITE);
241
-                im_body_draw(im->body,im->w,im->h);
245
+                im_body_draw(im->body,mousepos,im->w,im->h);
242 246
                 im_menubar_draw(im->menubar,im->w,im->h);
243 247
 #if 0
244 248
 {
... ...
@@ -641,6 +645,10 @@ im_body_free(body_t *body)
641 645
                 }
642 646
                 free(body->dirdata),body->dirdata=NULL,body->sizedirdata=0;
643 647
         }
648
+        if(body->has_texture) {
649
+                UnloadTexture(body->texture);
650
+                body->has_texture=0;
651
+        }
644 652
         free(body),body=NULL;
645 653
         return;
646 654
 }
... ...
@@ -737,7 +745,7 @@ im_body_mouse(body_t *body, Vector2 mousepos, int lmbpressed, int lmbreleased, i
737 745
 }
738 746
 
739 747
 int
740
-im_body_draw(body_t *body, int windowwidth, int windowheight)
748
+im_body_draw(body_t *body, Vector2 mousepos, int windowwidth, int windowheight)
741 749
 {
742 750
         int i,x,y,k,margin,righty;
743 751
         dirdata_t *dirdata;
... ...
@@ -745,6 +753,7 @@ im_body_draw(body_t *body, int windowwidth, int windowheight)
745 753
         Vector2 v2,m2;
746 754
         font_t *font,*fontbig,*fonthuge;
747 755
         int is_leftside;
756
+        int flag_skiprightside;
748 757
         if(body==NULL)
749 758
                 return(-1);
750 759
         font=body->ptrfont;
... ...
@@ -756,13 +765,13 @@ im_body_draw(body_t *body, int windowwidth, int windowheight)
756 765
         DrawRectangle(body->xywh.x,body->xywh.y,body->leftsize, body->xywh.h, (Color){ 215, 215, 215, 255 } );
757 766
         /* draw right side background */
758 767
         DrawRectangle(body->xywh.x+body->leftsize,body->xywh.y,body->xywh.w-body->leftsize, body->xywh.h, (Color){ 227, 227, 227, 255 } );
759
-        for(i=0,y=righty=body->xywh.y;i<body->sizedirdata;i++) {
760
-                if((dirdata=body->dirdata[i])==NULL)
761
-                        continue;
762
-                /* two passes for each dirdata, first leftside, then rightside */
763
-                for(is_leftside=1;is_leftside>=0;is_leftside--) {
768
+        /* first pass, draw leftside, second pass, draw all of rightside */
769
+        for(is_leftside=1,flag_skiprightside=0;is_leftside>=0 && flag_skiprightside==0;is_leftside--) {
770
+                for(i=(is_leftside)?body->currentdirdata:0,y=righty=body->xywh.y;(is_leftside && i==body->currentdirdata) || (!is_leftside && i<body->sizedirdata);i++) {
764 771
                         int x0,y0,x1,y1;
765 772
                         int sidelen;
773
+                        if((dirdata=body->dirdata[i])==NULL)
774
+                                continue;
766 775
                         if(is_leftside && !(i==body->currentdirdata))
767 776
                                 continue; /* this element is not in leftside */
768 777
                         margin=font->height/4;
... ...
@@ -922,8 +931,36 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
922 931
                                                                 *has_texture=1;
923 932
                                                         }
924 933
                                                 }
925
-                                                if(*has_texture!=0)
934
+                                                if(*has_texture!=0) {
926 935
                                                         DrawTexture(*te,pos->x,pos->y,WHITE);
936
+                                                        if(is_leftside && is_imutil_insidexywh(mousepos,pos)) {
937
+                                                                /* draw image in rightside */
938
+                                                                char path[2048];
939
+                                                                snprintf(path,sizeof(path),"%s/%s",dirdata->dirname,elem->name+1);
940
+                                                                path[sizeof(path)-1]='\0';
941
+                                                                if(body->has_texture==0 || strcmp(body->currenttexture,path)!=0) {
942
+                                                                        Image im;
943
+                                                                        char fullpath[2048];
944
+                                                                        if(body->has_texture) {
945
+                                                                                UnloadTexture(body->texture);
946
+                                                                                body->has_texture=0;
947
+                                                                        }
948
+                                                                        snprintf(fullpath,sizeof(fullpath),"%s/%s/%s",body->rootdir,dirdata->dirname,elem->name+1);
949
+                                                                        fullpath[sizeof(fullpath)-1]='\0';
950
+                                                                        im=LoadImage(fullpath);
951
+                                                                        if(IsImageValid(im)) {
952
+                                                                                ImageResize(&im,windowwidth-body->leftsize,windowheight-body->xywh.y);
953
+                                                                                body->texture=LoadTextureFromImage(im);
954
+                                                                                UnloadImage(im);
955
+                                                                                body->has_texture=1;
956
+                                                                        }
957
+                                                                }
958
+                                                                if(body->has_texture) {
959
+                                                                       DrawTexture(body->texture,body->leftsize,body->xywh.y,WHITE);
960
+                                                                       flag_skiprightside=1;
961
+                                                                }
962
+                                                        }
963
+                                                }
927 964
                                         }
928 965
                                 }
929 966
                                 x+=margin*2+sidelen;