make extra button to call image drawing really work
[mandelbrot.git] / xulapp / chrome / mandelbrot / content / image-settings.js
index c00c19a148be5aeb2eb7f3fa09dcd3067bc1f093..a025bde548cc0fd4d815a3e7ced8ff9ecc78e4f6 100644 (file)
  *
  * ***** END LICENSE BLOCK ***** */
 
+var gColorPalette = [];
+
 function initSettings() {
+  recalcCoord('Cr', 'scale');
+  recalcCoord('Ci', 'scale');
+  if (!window.opener)
+    window.getButton('extra1').disabled = true;
 }
+
+function drawPreview() {
+  let canvas = document.getElementById("mbrotPreview");
+  let context = canvas.getContext("2d");
+
+  if (document.getElementById("imgWidth").value /
+      document.getElementById("imgHeight").value
+        < 80 / 50) {
+    canvas.height = 50;
+    canvas.width = canvas.height *
+      document.getElementById("imgWidth").value /
+      document.getElementById("imgHeight").value;
+  }
+  else {
+    canvas.width = 80;
+    canvas.height = canvas.width *
+      document.getElementById("imgHeight").value /
+      document.getElementById("imgWidth").value;
+  }
+
+  let Cr_min = parseFloat(document.getElementById("Cr_min").value);
+  let Cr_max = parseFloat(document.getElementById("Cr_max").value);
+  if ((Cr_min < -2) || (Cr_min > 2) ||
+      (Cr_max < -2) || (Cr_max > 2) || (Cr_min >= Cr_max)) {
+    Cr_min = -2.0; Cr_max = 1.0;
+  }
+
+  let Ci_min = parseFloat(document.getElementById("Ci_min").value);
+  let Ci_max = parseFloat(document.getElementById("Ci_max").value);
+  if ((Ci_min < -2) || (Ci_min > 2) ||
+      (Ci_max < -2) || (Ci_max > 2) || (Ci_min >= Ci_max)) {
+    Ci_min = -2.0; Ci_max = 1.0;
+  }
+
+  let iterMax = gPref.getIntPref("mandelbrot.iteration_max");
+  let algorithm = gPref.getCharPref("mandelbrot.use_algorithm");
+
+  context.fillStyle = "rgba(255, 255, 255, 127)";
+  context.fillRect(0, 0, canvas.width, canvas.height);
+
+  try {
+    var currentPalette = gPref.getCharPref("mandelbrot.color_palette");
+  }
+  catch(e) {
+    var currentPalette = '';
+  }
+  if (!currentPalette.length) {
+    currentPalette = 'kairo';
+  }
+  gColorPalette = getColorPalette(currentPalette);
+
+  drawLine(0, [Cr_min, Cr_max, Ci_min, Ci_max],
+              canvas, context, iterMax, algorithm);
+}
+
+function recalcCoord(coord, target) {
+  let othercoord = (coord == 'Ci') ? 'Cr' : 'Ci';
+  let owndim = (coord == 'Ci') ? 'height' : 'width';
+  let otherdim = (coord == 'Ci') ? 'width' : 'height';
+  if (target == 'scale') {
+    var myscale =
+      parseFloat(document.getElementById(coord + '_max').value) -
+      parseFloat(document.getElementById(coord + '_min').value);
+    document.getElementById(coord + '_scale').value = roundCoord(myscale);
+  }
+  else if (target == 'max') {
+    let mymax =
+      parseFloat(document.getElementById(coord + '_min').value) +
+      parseFloat(document.getElementById(coord + '_scale').value);
+    document.getElementById('mandelbrot.last_image.' + coord + '_max').value = roundCoord(mymax);
+    var myscale = document.getElementById(coord + '_scale').value;
+  }
+  if (document.getElementById('syncProp').checked) {
+    let otherscale = myscale *
+      document.getElementById('mandelbrot.image.' + otherdim).value /
+      document.getElementById('mandelbrot.image.' + owndim).value;
+    document.getElementById(othercoord + '_scale').value = roundCoord(otherscale);
+    let othermax =
+      parseFloat(document.getElementById(othercoord + '_min').value) +
+      parseFloat(document.getElementById(othercoord + '_scale').value);
+    document.getElementById('mandelbrot.last_image.' + othercoord + '_max').value = roundCoord(othermax);
+  }
+}
+
+function checkProportions() {
+  if (!document.getElementById('syncProp').checked) {
+    recalcCoord('Cr', 'scale');
+  }
+}
+
+function roundCoord(floatval) {
+  // We should round to 10 decimals here or so
+  return floatval;
+}
+
+function callDrawImage() {
+  window.opener.drawImage();
+  window.close();
+}
\ No newline at end of file