X-Git-Url: https://git-public.kairo.at/?p=mandelbrot.git;a=blobdiff_plain;f=xulapp%2Fchrome%2Fmandelbrot%2Fcontent%2Fmandelbrot.js;h=788e198bd6059fba5259f21dce6bb6ab2b8b1a06;hp=f698d4574a928eba66cd62d2baabffdc4fbe4a55;hb=305865993932a42afc891deb5acc046b62949c9e;hpb=aad3502853e46a8da90fc751637a65bf1fcfeeb6 diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.js b/xulapp/chrome/mandelbrot/content/mandelbrot.js index f698d45..788e198 100644 --- a/xulapp/chrome/mandelbrot/content/mandelbrot.js +++ b/xulapp/chrome/mandelbrot/content/mandelbrot.js @@ -41,6 +41,7 @@ var gPref = Components.classes["@mozilla.org/preferences-service;1"] .getBranch(null); var gStartTime = 0; var gMbrotBundle; +var gCurrentImageData; function Startup() { updateIterMenu(); @@ -49,6 +50,56 @@ function Startup() { document.getElementById("statusLabel").value = gMbrotBundle.getString("statusEmpty"); } +function adjustCoordsAndDraw(aC_min, aC_max) { + let iWidth = 0; + try { + iWidth = gPref.getIntPref("mandelbrot.image.width"); + } + catch (e) { } + if ((iWidth < 10) || (iWidth > 5000)) { + iWidth = 300; + gPref.setIntPref("mandelbrot.image.width", iWidth); + } + let iHeight = 0; + try { + iHeight = gPref.getIntPref("mandelbrot.image.height"); + } + catch (e) { } + if ((iHeight < 10) || (iHeight > 5000)) { + iHeight = 300; + gPref.setIntPref("mandelbrot.image.height", iHeight); + } + + // correct coordinates + if (aC_min.r < -2) + aC_min.r = -2; + if (aC_max.r > 2) + aC_max.r = 2; + if ((aC_min.r > 2) || (aC_max.r < -2) || (aC_min.r >= aC_max.r)) { + aC_min.r = -2.0; aC_max.r = 1.0; + } + if (aC_min.i < -2) + aC_min.i = -2; + 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.3; aC_max.i = 1.3; + } + + let CWidth = aC_max.r - aC_min.r; + let CHeight = aC_max.i - aC_min.i; + let C_mid = new complex(aC_min.r + CWidth / 2, aC_min.i + CHeight / 2); + + let CRatio = Math.max(CWidth / iWidth, CHeight / iHeight); + + gPref.setCharPref("mandelbrot.last_image.Cr_min", C_mid.r - iWidth * CRatio / 2); + gPref.setCharPref("mandelbrot.last_image.Cr_max", C_mid.r + iWidth * CRatio / 2); + gPref.setCharPref("mandelbrot.last_image.Ci_min", C_mid.i - iHeight * CRatio / 2); + gPref.setCharPref("mandelbrot.last_image.Ci_max", C_mid.i + iHeight * CRatio / 2); + + drawImage(); +} + function drawImage() { let canvas = document.getElementById("mbrotImage"); let context = canvas.getContext("2d"); @@ -64,8 +115,8 @@ function drawImage() { 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); @@ -78,9 +129,9 @@ function drawImage() { 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)) { - Ci_min = -2.0; Ci_max = 1.0; + 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); gPref.setCharPref("mandelbrot.last_image.Ci_max", Ci_max); @@ -107,6 +158,14 @@ function drawImage() { gPref.setIntPref("mandelbrot.image.height", iHeight); } + gCurrentImageData = { + C_min: new complex(Cr_min, Ci_min), + C_max: new complex(Cr_max, Ci_max), + iWidth: iWidth, + iHeight: iHeight, + iterMax: iterMax + }; + canvas.width = iWidth; canvas.height = iHeight; @@ -354,14 +413,38 @@ function mouseevent(etype, event) { break; case 'up': if (event.button == 0 && zoomstart) { + let zoomend = {x: event.clientX - canvas.offsetLeft, + y: event.clientY - canvas.offsetTop}; + + // make sure zoomend is bigger than zoomstart + if ((zoomend.x == zoomstart.x) || (zoomend.y == zoomstart.y)) { + // cannot zoom what has no area, discard it + zoomstart = undefined; + return; + } + if (zoomend.x < zoomstart.x) + [zoomend.x, zoomstart.x] = [zoomstart.x, zoomend.x]; + if (zoomend.y < zoomstart.y) + [zoomend.y, zoomstart.y] = [zoomstart.y, zoomend.y]; + let prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] .getService(Components.interfaces.nsIPromptService); let ok = prompts.confirm(null, gMbrotBundle.getString("zoomConfirmTitle"), - gMbrotBundle.getString("zoomConfirmLabel") + ' --- ' + - zoomstart.x + ',' + zoomstart.y + '-' + - (event.clientX - canvas.offsetLeft) + ',' + - (event.clientY - canvas.offsetTop)); + gMbrotBundle.getString("zoomConfirmLabel")); // ok is now true if OK was clicked, and false if cancel was clicked + if (ok) { + // determine new "coordinates" + let CWidth = gCurrentImageData.C_max.r - gCurrentImageData.C_min.r; + let CHeight = gCurrentImageData.C_max.i - gCurrentImageData.C_min.i; + let newC_min = new complex( + gCurrentImageData.C_min.r + zoomstart.x / gCurrentImageData.iWidth * CWidth, + gCurrentImageData.C_min.i + zoomstart.y / gCurrentImageData.iHeight * CHeight); + let newC_max = new complex( + gCurrentImageData.C_min.r + zoomend.x / gCurrentImageData.iWidth * CWidth, + gCurrentImageData.C_min.i + zoomend.y / gCurrentImageData.iHeight * CHeight); + + adjustCoordsAndDraw(newC_min, newC_max); + } } zoomstart = undefined; break; @@ -392,7 +475,7 @@ function updateBookmarkMenu(aParent) { (!document.getElementById("drawButton").hidden || (gStartTime > 0)); while (aParent.hasChildNodes() && - aParent.lastChild.id != 'bookmarkSeparator') + aParent.lastChild.id != "bookmarkSeparator") aParent.removeChild(aParent.lastChild); let file = Components.classes["@mozilla.org/file/directory_service;1"] @@ -406,10 +489,12 @@ function updateBookmarkMenu(aParent) { try { if (connection.tableExists("bookmarks")) { let statement = connection.createStatement( - "SELECT name FROM bookmarks ORDER BY ROWID DESC"); - while (statement.executeStep()) - aParent.appendChild(document.createElement("menuitem")) - .setAttribute("label", statement.getString(0)); + "SELECT name,ROWID FROM bookmarks ORDER BY ROWID ASC"); + while (statement.executeStep()) { + let newItem = aParent.appendChild(document.createElement("menuitem")); + newItem.setAttribute("label", statement.getString(0)); + newItem.setAttribute("bmRowID", statement.getString(1)); + } statement.reset(); statement.finalize(); return; @@ -425,6 +510,41 @@ function updateBookmarkMenu(aParent) { } function callBookmark(evtarget) { + if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator") + return; + if (evtarget.id == "bookmarkOverview") { + adjustCoordsAndDraw(new complex(0,0), new complex(0,0)); + return; + } + + 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() { @@ -459,11 +579,11 @@ function saveBookmark() { let statement = connection.createStatement( "INSERT INTO bookmarks (name,iteration_max,Cr_min,Cr_max,Ci_min,Ci_max) VALUES (?1,?2,?3,?4,?5,?6)"); statement.bindStringParameter(0, bmName); - statement.bindStringParameter(1, gPref.getIntPref("mandelbrot.iteration_max")); - statement.bindStringParameter(2, parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_min"))); - statement.bindStringParameter(3, parseFloat(gPref.getCharPref("mandelbrot.last_image.Cr_max"))); - statement.bindStringParameter(4, parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_min"))); - statement.bindStringParameter(5, parseFloat(gPref.getCharPref("mandelbrot.last_image.Ci_max"))); + statement.bindStringParameter(1, gCurrentImageData.iterMax); + statement.bindStringParameter(2, gCurrentImageData.C_min.r); + statement.bindStringParameter(3, gCurrentImageData.C_max.r); + statement.bindStringParameter(4, gCurrentImageData.C_min.i); + statement.bindStringParameter(5, gCurrentImageData.C_max.i); statement.execute(); statement.finalize(); connection.commitTransaction();