From: Robert Kaiser Date: Sun, 2 Aug 2009 15:11:11 +0000 (+0200) Subject: set current image's data in a global var and use it for saving bookmarks as well... X-Git-Url: https://git-public.kairo.at/?p=mandelbrot.git;a=commitdiff_plain;h=86e67c44355487a4cb23dc5f2700a064440e77bc;hp=aad3502853e46a8da90fc751637a65bf1fcfeeb6 set current image's data in a global var and use it for saving bookmarks as well as zoom; implement function to adjust coordinates proportionally to the image and call into drawing; calc zoom coordinates and call that function to make zoom work; set an attribute for the DB row ID on bookmarks menu items; start callBookmark work --- diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.js b/xulapp/chrome/mandelbrot/content/mandelbrot.js index f698d45..34761ca 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.5; aC_max.i = 1.5; + } + + 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"); @@ -80,7 +131,7 @@ function drawImage() { 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; + 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,11 @@ function updateBookmarkMenu(aParent) { } function callBookmark(evtarget) { + if (evtarget.id == "bookmarkSave" || evtarget.id == "bookmarkSeparator") + return + + alert(evtarget.getAttribute('label') + ', ' + evtarget.getAttribute('bmRowID')); + //gPref.setIntPref("mandelbrot.iteration_max", iterMax); } function saveBookmark() { @@ -459,11 +549,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();