From: Robert Kaiser Date: Sat, 23 Aug 2008 16:35:55 +0000 (+0200) Subject: make UI prefs actually work, add menus for debug options, i.e. turning TraceMonkey... X-Git-Url: https://git-public.kairo.at/?a=commitdiff_plain;h=6e98af87f333055b6896606602bacef3bb73c547;p=mandelbrot.git make UI prefs actually work, add menus for debug options, i.e. turning TraceMonkey on/off, changing algorithms, and rough display of calc time --- diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.js b/xulapp/chrome/mandelbrot/content/mandelbrot.js index 9937799..837c0f2 100644 --- a/xulapp/chrome/mandelbrot/content/mandelbrot.js +++ b/xulapp/chrome/mandelbrot/content/mandelbrot.js @@ -1,10 +1,24 @@ -var gColorPalette = getColorPalette('kairo'); +var gColorPalette = []; +var gPref = Components.classes["@mozilla.org/preferences-service;1"] + .getService(Components.interfaces.nsIPrefService) + .getBranch(null); +var gStartTime = 0; + +function Startup() { + updateIterMenu(); + updatePaletteMenu(); + document.getElementById("statusLabel").value = + document.getElementById("mbrotBundle").getString("statusEmpty"); +} function drawImage() { let canvas = document.getElementById("mbrotImage"); if (canvas.getContext) { let context = canvas.getContext("2d"); + document.getElementById("statusLabel").value = + document.getElementById("mbrotBundle").getString("statusDrawing"); + // example: // context.fillStyle = "rgb(200,0,0)"; // context.fillRect (10, 10, 55, 50); // x, y, width, height @@ -20,18 +34,29 @@ function drawImage() { let Ci_max = 1.5; let Ci_scale = Ci_max - Ci_min; - let iterMax = 500; + let iterMax = gPref.getIntPref("mandelbrot.iteration_max"); + let algorithm = gPref.getCharPref("mandelbrot.use_algorithm"); + + gStartTime = new Date(); for (let img_x = 0; img_x < canvas.width; img_x++) { for (let img_y = 0; img_y < canvas.height; img_y++) { let C = new complex(Cr_min + (img_x / canvas.width) * Cr_scale, Ci_min + (img_y / canvas.height) * Ci_scale); - window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax); + window.setTimeout(drawPoint, 0, context, img_x, img_y, C, iterMax, algorithm); } } + window.setTimeout(EndCalc, 0); } } +function EndCalc() { + let endTime = new Date(); + let timeUsed = (endTime.getTime() - gStartTime.getTime()) / 1000; + document.getElementById("statusLabel").value = + document.getElementById("mbrotBundle").getFormattedString("statusTime", [timeUsed.toFixed(3)]); +} + function complex(aReal, aImag) { this.r = aReal; this.i = aImag; @@ -47,16 +72,18 @@ function complex(aReal, aImag) { } } -function mandelbrotValue (aC, aIterMax) { - /* XXX: this would be nice code but it looks like JS objects are too heavy for this. +function mandelbrotValueOO (aC, aIterMax) { + // this would be nice code in general but it looks like JS objects are too heavy for normal use. let Z = new complex(0.0, 0.0); for (var iter = 0; iter < aIterMax; iter++) { Z = Z.square().add(aC); if (Z.r * Z.r + Z.i * Z.i > 256) { break; } } - */ + return iter; +} - // highly optimized code for fast calculation +function mandelbrotValueNumeric (aC, aIterMax) { + // optimized numeric code for fast calculation let Cr = aC.r, Ci = aC.i; let Zr = 0.0, Zi = 0.0; let Zr2 = Zr * Zr, Zi2 = Zi * Zi; @@ -72,14 +99,14 @@ function mandelbrotValue (aC, aIterMax) { function getColor(aIterValue, aIterMax) { let standardizedValue = Math.round(aIterValue * 1024 / aIterMax); - return gColorPalette[standardizedValue]; - if (aIterValue == aIterMax) { + if (gColorPalette && gColorPalette.length) + return gColorPalette[standardizedValue]; + + // fallback to simple b/w if for some reason we don't have a palette + if (aIterValue == aIterMax) return "rgb(0,0,0)"; - } - else { - //return "rgb(" + img_x + "," + img_y + ",0)"; + else return "rgb(255,255,255)"; - } } function getColorPalette(palName) { @@ -197,17 +224,132 @@ Case 5 'Regenbogen-Palette 2 (qu.) return palette; } -function drawPoint(context, img_x, img_y, C, iterMax) { - let itVal = mandelbrotValue(C, iterMax); +function drawPoint(context, img_x, img_y, C, iterMax, algorithm) { + var itVal; + switch (algorithm) { + case 'oo': + itVal = mandelbrotValueOO(C, iterMax); + break; + case 'numeric': + default: + itVal = mandelbrotValueNumeric(C, iterMax); + break; + } context.fillStyle = getColor(itVal, iterMax); context.fillRect (img_x, img_y, 1, 1); // x, y, width, height } +/***** pure UI functions *****/ + function saveImage() { - // should call filepicker! + // XXX: should call filepicker! saveCanvas(document.getElementById("mbrotImage"), "/home/robert/temp/canvas-save.png") } +function updateIterMenu() { + try { + var currentIter = gPref.getIntPref("mandelbrot.iteration_max"); + } + catch(e) { + var currentIter = 0; + } + if (currentIter < 10) { + currentIter = 500; + setIter(currentIter); + } + + var popup = document.getElementById("menu_iterPopup"); + var item = popup.firstChild; + while (item) { + if (item.getAttribute("name") == "iter") { + if (item.getAttribute("value") == currentIter) + item.setAttribute("checked","true"); + else + item.removeAttribute("checked"); + } + item = item.nextSibling; + } +} + +function setIter(aIter) { + gPref.setIntPref("mandelbrot.iteration_max", aIter); +} + +function updatePaletteMenu() { + try { + var currentPalette = gPref.getCharPref("mandelbrot.color_palette"); + } + catch(e) { + var currentPalette = ''; + } + if (!currentPalette.length) { + currentPalette = 'kairo'; + setPalette(currentPalette); + } + if (!gColorPalette || !gColorPalette.length) + gColorPalette = getColorPalette(currentPalette); + + var popup = document.getElementById("menu_palettePopup"); + var item = popup.firstChild; + while (item) { + if (item.getAttribute("name") == "palette") { + if (item.getAttribute("value") == currentPalette) + item.setAttribute("checked", "true"); + else + item.removeAttribute("checked"); + } + item = item.nextSibling; + } +} + +function setPalette(aPaletteID) { + gPref.setCharPref("mandelbrot.color_palette", aPaletteID); + gColorPalette = getColorPalette(aPaletteID); +} + +function updateDebugMenu() { + var jitMenuItem = document.getElementById("jitEnabled"); + jitMenuItem.setAttribute("checked", gPref.getBoolPref("javascript.options.jit.chrome")); +} + +function toggleJITState(jitMenuItem) { + var jitEnabled = !gPref.getBoolPref("javascript.options.jit.chrome"); + gPref.setBoolPref("javascript.options.jit.chrome", jitEnabled) + jitMenuItem.setAttribute("checked", jitEnabled? "true" : "false"); +} + +function updateAlgoMenu() { + try { + var currentAlgo = gPref.getCharPref("mandelbrot.use_algorithm"); + } + catch(e) { + var currentAlgo = ''; + } + if (!currentAlgo.length) { + currentAlgo = 'numeric'; + setAlgorithm(currentAlgo); + } + + var popup = document.getElementById("menu_algoPopup"); + var item = popup.firstChild; + while (item) { + if (item.getAttribute("name") == "algorithm") { + if (item.getAttribute("value") == currentAlgo) + item.setAttribute("checked", "true"); + else + item.removeAttribute("checked"); + } + item = item.nextSibling; + } +} + +function setAlgorithm(algoID) { + gPref.setCharPref("mandelbrot.use_algorithm", algoID); +} + + +/***** helper functions from external sources *****/ + // function below is from from http://developer.mozilla.org/en/docs/Code_snippets:Canvas function saveCanvas(canvas, destFile) { // convert string filepath to an nsIFile diff --git a/xulapp/chrome/mandelbrot/content/mandelbrot.xul b/xulapp/chrome/mandelbrot/content/mandelbrot.xul index be37ef3..821dc17 100644 --- a/xulapp/chrome/mandelbrot/content/mandelbrot.xul +++ b/xulapp/chrome/mandelbrot/content/mandelbrot.xul @@ -11,43 +11,63 @@ +