implement graphical drag feedback for zooming
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / mandelbrot.js
index d07345ee81fe1d97b74489f94b22185b16674f8b..e4c7d9a77de84f7dcae94470317f5f3ac5b86319 100644 (file)
@@ -83,7 +83,7 @@ function adjustCoordsAndDraw(aC_min, aC_max) {
   if (aC_max.i > 2)
     aC_max.i = 2;
   if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
   if (aC_max.i > 2)
     aC_max.i = 2;
   if ((aC_min.i > 2) || (aC_max.i < -2) || (aC_min.i >= aC_max.i)) {
-    aC_min.i = -1.5; aC_max.i = 1.5;
+    aC_min.i = -1.3; aC_max.i = 1.3;
   }
 
   let CWidth = aC_max.r - aC_min.r;
   }
 
   let CWidth = aC_max.r - aC_min.r;
@@ -115,8 +115,8 @@ function drawImage() {
     Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
   }
   catch (e) { }
     Cr_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"));
   }
   catch (e) { }
-  if ((Cr_min < -2) || (Cr_min > 2) ||
-      (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
+  if ((Cr_min < -3) || (Cr_min > 2) ||
+      (Cr_max < -3) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
     Cr_min = -2.0; Cr_max = 1.0;
   }
   gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
     Cr_min = -2.0; Cr_max = 1.0;
   }
   gPref.setCharPref("mandelbrot.last_image.Cr_min", Cr_min);
@@ -129,8 +129,8 @@ function drawImage() {
     Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
   }
   catch (e) { }
     Ci_max = parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"));
   }
   catch (e) { }
-  if ((Ci_min < -2) || (Ci_min > 2) ||
-      (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
+  if ((Ci_min < -2.5) || (Ci_min > 2.5) ||
+      (Ci_max < -2.5) || (Ci_max > 2.5) || (Ci_min >= Ci_max)) {
     Ci_min = -1.5; Ci_max = 1.5;
   }
   gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
     Ci_min = -1.5; Ci_max = 1.5;
   }
   gPref.setCharPref("mandelbrot.last_image.Ci_min", Ci_min);
@@ -401,18 +401,23 @@ function drawPoint(context, img_x, img_y, C, iterMax, algorithm) {
 /***** pure UI functions *****/
 
 var zoomstart;
 /***** pure UI functions *****/
 
 var zoomstart;
+var imgBackup;
 
 function mouseevent(etype, event) {
   let canvas = document.getElementById("mbrotImage");
 
 function mouseevent(etype, event) {
   let canvas = document.getElementById("mbrotImage");
+  let context = canvas.getContext("2d");
   switch (etype) {
     case 'down':
   switch (etype) {
     case 'down':
-      if (event.button == 0)
+      if (event.button == 0) {
         // left button - start dragzoom
         zoomstart = {x: event.clientX - canvas.offsetLeft,
                      y: event.clientY - canvas.offsetTop};
         // left button - start dragzoom
         zoomstart = {x: event.clientX - canvas.offsetLeft,
                      y: event.clientY - canvas.offsetTop};
+        imgBackup = context.getImageData(0, 0, canvas.width, canvas.height);
+      }
       break;
     case 'up':
       if (event.button == 0 && zoomstart) {
       break;
     case 'up':
       if (event.button == 0 && zoomstart) {
+        context.putImageData(imgBackup, 0, 0);
         let zoomend = {x: event.clientX - canvas.offsetLeft,
                        y: event.clientY - canvas.offsetTop};
 
         let zoomend = {x: event.clientX - canvas.offsetLeft,
                        y: event.clientY - canvas.offsetTop};
 
@@ -447,6 +452,15 @@ function mouseevent(etype, event) {
         }
       }
       zoomstart = undefined;
         }
       }
       zoomstart = undefined;
+      break;
+    case 'move':
+      if (event.button == 0 && zoomstart) {
+        context.putImageData(imgBackup, 0, 0);
+        context.strokeStyle = "rgb(255,255,31)";
+        context.strokeRect(zoomstart.x, zoomstart.y,
+                           event.clientX - canvas.offsetLeft - zoomstart.x,
+                           event.clientY - canvas.offsetTop - zoomstart.y);
+      }
     break;
   }
 }
     break;
   }
 }
@@ -516,8 +530,35 @@ function callBookmark(evtarget) {
     adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
     return;
   }
     adjustCoordsAndDraw(new complex(0,0), new complex(0,0));
     return;
   }
-  alert(evtarget.getAttribute('label') + ', ' + evtarget.getAttribute('bmRowID'));
-  //gPref.setIntPref("mandelbrot.iteration_max", iterMax);
+
+  if (evtarget.getAttribute('bmRowID')) {
+    let iterMax = 0;
+    let C_min = null;
+    let C_max = null;
+
+    let file = Components.classes["@mozilla.org/file/directory_service;1"]
+                         .getService(Components.interfaces.nsIProperties)
+                         .get("ProfD", Components.interfaces.nsIFile);
+    file.append("mandelbookmarks.sqlite");
+    let connection = Components.classes["@mozilla.org/storage/service;1"]
+                               .getService(Components.interfaces.mozIStorageService)
+                               .openDatabase(file);
+    let statement = connection.createStatement(
+        "SELECT iteration_max,Cr_min,Cr_max,Ci_min,Ci_max FROM bookmarks WHERE ROWID=?1");
+    statement.bindStringParameter(0, evtarget.getAttribute('bmRowID'));
+    while (statement.executeStep()) {
+      iterMax = statement.getInt32(0);
+      C_min = new complex(statement.getDouble(1), statement.getDouble(3));
+      C_max = new complex(statement.getDouble(2), statement.getDouble(4));
+    }
+    statement.finalize();
+    connection.close();
+
+    if (iterMax && C_min && C_max) {
+      gPref.setIntPref("mandelbrot.iteration_max", iterMax);
+      adjustCoordsAndDraw(C_min, C_max);
+    }
+  }
 }
 
 function saveBookmark() {
 }
 
 function saveBookmark() {