Browse code

Fix thumb aspect ratio

Dario Rodriguez authored on 08/03/2025 22:10:42
Showing 1 changed files
... ...
@@ -21,6 +21,7 @@
21 21
  *      20250305 Scrollable left pane.
22 22
  *      20250308 Show statustooltip on image hover.
23 23
  *               Scrollwheel support for leftside.
24
+ *               Fix thumb aspect ratio.
24 25
  *
25 26
  * Author: Dario Rodriguez dario@darionomono.com
26 27
  * (c) Dario Rodriguez 2025
... ...
@@ -222,7 +223,7 @@ char *imutil_menu_get(char *menus, int targetn, int *len);
222 223
 int imutil_submenu_count(char *menus);
223 224
 char *imutil_submenu_get(char *menus, int targetn, int *len);
224 225
 char *imutil_strduplen(char *str, int len);
225
-int is_imutil_insidexywh(Vector2 pos, xywh_t *xywh);
226
+int is_imutil_insidexywh(Vector2 pos, xywh_t *xywh, int margin);
226 227
 int im_util_aspectmaximize(int w, int h, int maxw, int maxh, int *neww, int *newh);
227 228
 int menudata_pos2option(menudata_t *menudata, Vector2 pos);
228 229
 int *getcodepoints(int *sizecodepoints);
... ...
@@ -494,7 +495,7 @@ im_menubar_mouse(menubar_t *menubar, Vector2 mousepos, int lmbpressed, int lmbre
494 495
         flag_outsideall=1;
495 496
         for(i=0;i<menubar->sizemenudata;i++) {
496 497
                 int insidetitle,currentoption;
497
-                insidetitle=is_imutil_insidexywh(mousepos,&(menubar->menudata[i]->xywh));
498
+                insidetitle=is_imutil_insidexywh(mousepos,&(menubar->menudata[i]->xywh),0);
498 499
                 currentoption=menudata_pos2option(menubar->menudata[i],mousepos);
499 500
                 flag_outsideall=(currentoption!=-1 || insidetitle)?0:flag_outsideall;
500 501
                 if(lmbreleased && insidetitle) {
... ...
@@ -730,7 +731,7 @@ im_body_mouse(body_t *body, Vector2 mousepos, Vector2 wheel, int lmbpressed, int
730 731
         if(*click_avail==0 || lmbreleased==0)
731 732
                 return(0); /* nothing else to do */
732 733
         /* leftside backbutton */
733
-        if(is_imutil_insidexywh(mousepos,&(body->backxywh))) {
734
+        if(is_imutil_insidexywh(mousepos,&(body->backxywh),0)) {
734 735
                 static char sep[]={SEP};
735 736
                 if((ptr=strrchr(dirdata->dirname,sep[0]))!=NULL) {
736 737
                         /* previous dir */
... ...
@@ -749,7 +750,7 @@ im_body_mouse(body_t *body, Vector2 mousepos, Vector2 wheel, int lmbpressed, int
749 750
                 ld=dirdata->listing.elems+i;
750 751
                 if(ld->name[0]!='d')
751 752
                         continue;
752
-                if(is_imutil_insidexywh(mousepos,&(ld->left.screenxywh))) {
753
+                if(is_imutil_insidexywh(mousepos,&(ld->left.screenxywh),0)) {
753 754
                         char *newname,*oldprefix;
754 755
                         int l,l0,l1;
755 756
                         static char sep[]={SEP};
... ...
@@ -916,7 +917,7 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
916 917
 #warning TODO: if !is_leftside, draw "..." in huge font using lastx,lasty as reference
917 918
                                         break;
918 919
                                 }
919
-                                if(is_leftside && is_imutil_insidexywh(mousepos,&(thumb->screenxywh))) {
920
+                                if(is_leftside && is_imutil_insidexywh(mousepos,&(thumb->screenxywh),margin)) {
920 921
                                         strncpy(statustooltip,elem->name+1,sizeof(statustooltip));
921 922
                                         statustooltip[sizeof(statustooltip)-1]='\0';
922 923
                                 }
... ...
@@ -931,12 +932,28 @@ DrawRectangle(UNROLLXYWH(body->backxywh),((Color){ 0,255,0,255 })); /* hit zone
931 932
                                                 fullpath[sizeof(fullpath)-1]='\0';
932 933
                                                 im=LoadImage(fullpath);
933 934
                                                 if(IsImageValid(im)) {
935
+                                                        int neww,newh;
936
+                                                        Image im2,impixel;
934 937
 #if 1
935 938
 fprintf(stderr,"Loaded %s\n",fullpath);
939
+{
940
+int oldw=im.width,oldh=im.height;
941
+#endif
942
+                                                        im2=GenImageColor(sidelen,sidelen,(Color){0,0,0,255});
943
+                                                        im_util_aspectmaximize(im.width,im.height,sidelen,sidelen,&neww,&newh);
944
+                                                        ImageResize(&im,neww,newh);
945
+                                                        impixel=ImageCopy(im);
946
+                                                        ImageResize(&impixel,1,1);
947
+#if 1
948
+if(neww>sidelen || newh>sidelen)
949
+fprintf(stderr,"elem:\"%s\" sidelen:%i old:%ix%i new:%ix%i\n",elem->name+1,sidelen,oldw,oldh,neww,newh);
950
+}
936 951
 #endif
937
-                                                        ImageResize(&im,sidelen,sidelen);
938
-                                                        *te=LoadTextureFromImage(im);
952
+                                                        ImageDraw(&im2,impixel,(Rectangle) {0,0,1,1},(Rectangle) {0,0,sidelen,sidelen},(Color){255,255,255,255});
953
+                                                        ImageDraw(&im2,im,(Rectangle) {0,0,neww,newh},(Rectangle) {(sidelen-neww)/2,(sidelen-newh)/2,neww,newh},(Color){255,255,255,255});
954
+                                                        *te=LoadTextureFromImage(im2);
939 955
                                                         UnloadImage(im);
956
+                                                        UnloadImage(im2);
940 957
                                                         thumb->has_texture=1;
941 958
                                                 }
942 959
                                         }
... ...
@@ -945,7 +962,7 @@ fprintf(stderr,"Loaded %s\n",fullpath);
945 962
                                                 DrawTexture(*te,thumb->screenxywh.x,thumb->screenxywh.y,WHITE);
946 963
                                                 has_imagedrawn=1;
947 964
                                                 lastx=xywh->x+xywh->w,lasty=xywh->y+xywh->h+yoff;
948
-                                                if(is_leftside && lmbdown==0 && is_imutil_insidexywh(mousepos,&(thumb->screenxywh))) {
965
+                                                if(is_leftside && lmbdown==0 && is_imutil_insidexywh(mousepos,&(thumb->screenxywh),margin)) {
949 966
                                                         /* draw image in rightside */
950 967
                                                         char path[2048];
951 968
                                                         int maxw,maxh;
... ...
@@ -1149,14 +1166,14 @@ imutil_strduplen(char *str, int len)
1149 1166
 }
1150 1167
 
1151 1168
 int
1152
-is_imutil_insidexywh(Vector2 pos, xywh_t *xywh)
1169
+is_imutil_insidexywh(Vector2 pos, xywh_t *xywh, int margin)
1153 1170
 {
1154 1171
         if(xywh==NULL)
1155 1172
                 return(0); /* sanity check error */
1156
-        if(pos.x>=(float)(xywh->x)
1157
-          && pos.x<=(float)(xywh->x+xywh->w)
1158
-          && pos.y>=(float)(xywh->y)
1159
-          && pos.y<=(float)(xywh->y+xywh->h)
1173
+        if(pos.x>=(float)(xywh->x-margin)
1174
+          && pos.x<=(float)(xywh->x+xywh->w+margin)
1175
+          && pos.y>=(float)(xywh->y-margin)
1176
+          && pos.y<=(float)(xywh->y+xywh->h+margin)
1160 1177
         ) {
1161 1178
                 return(1);
1162 1179
         }
... ...
@@ -1168,7 +1185,7 @@ im_util_aspectmaximize(int w, int h, int maxw, int maxh, int *neww, int *newh)
1168 1185
 {
1169 1186
         if(neww==NULL || newh==NULL || w==0 || h==0 || maxw==0 || maxh==0)
1170 1187
                 return(-1);
1171
-        if((w/h)>(maxw/maxh)) {
1188
+        if((w/h)>(maxw/maxh) || (maxw==maxh && w>h)) {
1172 1189
                 *neww=maxw;
1173 1190
                 *newh=h*maxw/w;
1174 1191
         } else { /* (w/h)<=(maxw/maxh) */
... ...
@@ -1185,7 +1202,7 @@ menudata_pos2option(menudata_t *menudata, Vector2 pos)
1185 1202
         int n,h;
1186 1203
         if(menudata==NULL
1187 1204
           || (menudata->flag_open==0 && menudata->flag_stickyopen==0)
1188
-          || is_imutil_insidexywh(pos, &(menudata->optionsxywh))==0
1205
+          || is_imutil_insidexywh(pos, &(menudata->optionsxywh),0)==0
1189 1206
         ) {
1190 1207
                 return(-1);
1191 1208
         }