improved opening/closing color picker gadget
authorHolger Schemel <holger.schemel@virtion.de>
Sat, 5 Oct 2024 09:54:33 +0000 (11:54 +0200)
committerHolger Schemel <holger.schemel@virtion.de>
Sat, 5 Oct 2024 09:54:33 +0000 (11:54 +0200)
src/libgame/gadgets.c
src/libgame/gadgets.h

index 3dfd2d04d5a1c2b19c3d6dc830b4816ca14aaff8..849f7b492694d3f81f21004f10619b73ab48b38f 100644 (file)
@@ -1158,66 +1158,84 @@ static void DrawGadget(struct GadgetInfo *gi, boolean pressed, boolean direct)
        int xsize = (width  - 2 * border_x) / border_x + 1;
        int ysize = (height - 2 * border_y) / border_y + 1;
 
-       // top left part of gadget border
-       BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
-                              border_x, border_y, x, y);
-
-       // top middle part of gadget border
-       for (i = 0; i < xsize; i++)
-         BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
-                                border_x, border_y,
-                                x + border_x + i * border_x, y);
-
-       // top right part of gadget border
-       BlitBitmapOnBackground(gd->bitmap, drawto,
-                              gd->x + gi->border.width - border_x, gd->y,
-                              border_x, border_y,
-                              x + width - border_x, y);
-
-       // left and right part of gadget border for each row
-       for (i = 0; i < ysize; i++)
+       if (pressed)
        {
-         BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
-                                border_x, border_y,
-                                x, y + border_y + i * border_y);
-         BlitBitmapOnBackground(gd->bitmap, drawto,
-                                gd->x + gi->border.width - border_x,
-                                gd->y + border_y,
-                                border_x, border_y,
-                                x + width - border_x,
-                                y + border_y + i * border_y);
-       }
-
-       // bottom left part of gadget border
-       BlitBitmapOnBackground(gd->bitmap, drawto,
-                              gd->x, gd->y + gi->border.height - border_y,
-                              border_x, border_y,
-                              x, y + height - border_y);
-
-       // bottom middle part of gadget border
-       for (i = 0; i < xsize; i++)
-         BlitBitmapOnBackground(gd->bitmap, drawto,
-                                gd->x + border_x,
-                                gd->y + gi->border.height - border_y,
-                                border_x, border_y,
-                                x + border_x + i * border_x,
-                                y + height - border_y);
-
-       // bottom right part of gadget border
-       BlitBitmapOnBackground(gd->bitmap, drawto,
-                              gd->x + gi->border.width - border_x,
-                              gd->y + gi->border.height - border_y,
-                              border_x, border_y,
-                              x + width - border_x,
-                              y + height - border_y);
-
-       ClearRectangleOnBackground(drawto,
-                                  x + border_x,
-                                  y + border_y,
-                                  width - 2 * border_x,
-                                  height - 2 * border_y);
+          if (!gi->colorpicker.open)
+          {
+           gi->colorpicker.open = TRUE;
+
+            // save background under color picker
+            BlitBitmap(drawto, gfx.field_save_buffer, x, y, width, height, x, y);
+          }
+
+          // top left part of gadget border
+          BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y,
+                                 border_x, border_y, x, y);
+
+          // top middle part of gadget border
+          for (i = 0; i < xsize; i++)
+            BlitBitmapOnBackground(gd->bitmap, drawto, gd->x + border_x, gd->y,
+                                   border_x, border_y,
+                                   x + border_x + i * border_x, y);
+
+          // top right part of gadget border
+          BlitBitmapOnBackground(gd->bitmap, drawto,
+                                 gd->x + gi->border.width - border_x, gd->y,
+                                 border_x, border_y,
+                                 x + width - border_x, y);
+
+          // left and right part of gadget border for each row
+          for (i = 0; i < ysize; i++)
+          {
+            BlitBitmapOnBackground(gd->bitmap, drawto, gd->x, gd->y + border_y,
+                                   border_x, border_y,
+                                   x, y + border_y + i * border_y);
+            BlitBitmapOnBackground(gd->bitmap, drawto,
+                                   gd->x + gi->border.width - border_x,
+                                   gd->y + border_y,
+                                   border_x, border_y,
+                                   x + width - border_x,
+                                   y + border_y + i * border_y);
+          }
+
+          // bottom left part of gadget border
+          BlitBitmapOnBackground(gd->bitmap, drawto,
+                                 gd->x, gd->y + gi->border.height - border_y,
+                                 border_x, border_y,
+                                 x, y + height - border_y);
+
+          // bottom middle part of gadget border
+          for (i = 0; i < xsize; i++)
+            BlitBitmapOnBackground(gd->bitmap, drawto,
+                                   gd->x + border_x,
+                                   gd->y + gi->border.height - border_y,
+                                   border_x, border_y,
+                                   x + border_x + i * border_x,
+                                   y + height - border_y);
+
+          // bottom right part of gadget border
+          BlitBitmapOnBackground(gd->bitmap, drawto,
+                                 gd->x + gi->border.width - border_x,
+                                 gd->y + gi->border.height - border_y,
+                                 border_x, border_y,
+                                 x + width - border_x,
+                                 y + height - border_y);
+
+          ClearRectangleOnBackground(drawto,
+                                     x + border_x,
+                                     y + border_y,
+                                     width - 2 * border_x,
+                                     height - 2 * border_y);
+
+          DrawColorPicker(gi);
+        }
+       else if (gi->colorpicker.open)
+       {
+         gi->colorpicker.open = FALSE;
 
-        DrawColorPicker(gi);
+         // restore background under color picker
+          BlitBitmap(gfx.field_save_buffer, drawto, x, y, width, height, x, y);
+        }
       }
       break;
 
@@ -1927,6 +1945,9 @@ static void HandleGadgetTags(struct GadgetInfo *gi, int first_tag, va_list ap)
     };
 
     cp_color_hsv = rgb_to_hsv(cp_color_rgb);
+
+    // always start with closed color picker
+    gi->colorpicker.open = FALSE;
   }
 }
 
@@ -2048,7 +2069,12 @@ static void MapGadgetExt(struct GadgetInfo *gi, boolean redraw)
 
   // when mapping color picker gadget, automatically activate it
   if (gi->type & GD_TYPE_COLOR_PICKER)
+  {
+    if (redraw)
+      DrawGadget(gi, DG_PRESSED, DG_BUFFERED);
+
     last_gi = gi;
+  }
 }
 
 void MapGadget(struct GadgetInfo *gi)
index c7e8d17e9aafb2dd854db4e88ce444a70e6698af..9d6162607ff380cf75d206635700a966a69d032a 100644 (file)
@@ -251,6 +251,9 @@ struct GadgetColorPicker
   int nr;                              // color slot (if using several colors)
   int type;                            // color type (RGB, C64, C64DTV, Atari)
   int value;                           // color value
+
+  // runtime values
+  boolean open;                                // opening state of color picker
 };
 
 struct GadgetInfo