diff events.pde @ 229:4f26dc75c448

Implement freeform image import, key ctrl+e. PNG export is now 'e'.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Sep 2018 22:32:45 +0300
parents caefc3c9e78c
children 2d2e313882b4
line wrap: on
line diff
--- a/events.pde	Wed Sep 05 22:28:15 2018 +0300
+++ b/events.pde	Wed Sep 05 22:32:45 2018 +0300
@@ -927,9 +927,6 @@
         restoreparameters();
     }
 
-    if (g_data[int('e')] == 1)
-        g_data[int('e')] = 0;
-
     if (g_data[int('h')] == 1)
     {
         g_data[int('h')] = 0;
@@ -1016,6 +1013,56 @@
             message("ERROR!");
     }
 
+    if (g_data[int('e')] == 1)
+    {
+        g_data[int('e')] = 0;
+
+        if (g_control)
+        {
+            message("PNG export|"+ (g_data[int('Q')] ? "w/border" : "w/o border"));
+
+            String fname = filename +"_"+ (g_spare ? "spare" : "main") +".png";
+            PImage simg = mpRenderImage(g_data[int('Q')], 64, 32, 1);
+
+            if (simg !== null)
+            {
+                simg.sourceImg.toBlob(function(idata){ mpSaveBinaryBlob(fname, idata); }, "image/png", 0.95);
+            }
+        }
+        else
+        {
+            message("PNG/JPEG|import");
+            mpLoadFileSelector("PNG/JPEG image", ".png,.jpg,.jpeg,.gif",
+            function (fh, fdata)
+            {
+                var fblob = new Blob([fdata], {'type': 'image/png'})
+                var furl = URL.createObjectURL(fblob);
+                var fimg = new Image;
+                fimg.onload = function()
+                {
+                    PImage ftmpi = new PImage(fimg);
+                    if (ftmpi != null)
+                    {
+                        store_undo();
+                        if (mpImportAnyImage(ftmpi))
+                        {
+                            filename = fh.name;
+                            refresh();
+                        }
+                        else
+                        {
+                            restore_undo();
+                            message("COULD NOT IMPORT.");
+                        }
+                    }
+                    else
+                        message("COULD NOT LOAD.");
+                }
+                fimg.src = furl;
+            });
+        }
+    }
+
     if (g_data[int('E')] == 1)
     {
         g_data[int('E')] = 0;
@@ -1069,19 +1116,6 @@
     {
         g_data[int('W')] = 0;
 
-        if (g_alt)
-        {
-            message("PNG|export");
-
-            String fname = filename +"_"+ (g_spare ? "spare" : "main") +".png";
-            PImage simg = mpRenderImage(true, 64, 32, 1);
-
-            if (simg !== null)
-            {
-                simg.sourceImg.toBlob(function(idata){ mpSaveBinaryBlob(fname, idata); }, "image/png", 0.95);
-            }
-        }
-        else
         if (g_formatext != "")
         {
             message("Format|export");