From 90b836ef12213c6f4ab8ddd12a9addf663eb7f6d Mon Sep 17 00:00:00 2001 From: Danny Yoo Date: Tue, 19 Jul 2011 14:07:26 -0400 Subject: [PATCH] debugging to-draw --- examples/counting-world-program.rkt | 6 ++ world/js-impl.js | 2 +- world/kernel.js | 105 ++++++++++++++++++++++------ world/main.rkt | 9 ++- world/{private => }/raw-jsworld.js | 0 5 files changed, 98 insertions(+), 24 deletions(-) rename world/{private => }/raw-jsworld.js (100%) diff --git a/examples/counting-world-program.rkt b/examples/counting-world-program.rkt index bf11595..8406920 100644 --- a/examples/counting-world-program.rkt +++ b/examples/counting-world-program.rkt @@ -8,10 +8,16 @@ handler "big bang should follow:" + +(define (draw w) + (circle w 'solid 'blue)) + + (big-bang 1 (on-tick add1 1) ;(on-tick (lambda (w) (* w 2)) 1) (stop-when (lambda (w) (> w 10))) + (to-draw draw) ) diff --git a/world/js-impl.js b/world/js-impl.js index 425066c..6f16849 100644 --- a/world/js-impl.js +++ b/world/js-impl.js @@ -71,7 +71,7 @@ EXPORTS['to-draw'] = 1, function(MACHINE) { var f = checkProcedure1(MACHINE, "on-tick", 0); - return new OnDraw(f); + return new ToDraw(f); }); diff --git a/world/kernel.js b/world/kernel.js index c2db5c5..5e3e2f0 100644 --- a/world/kernel.js +++ b/world/kernel.js @@ -1,3 +1,7 @@ +var imageLibrary = MACHINE.modules['whalesong/image/private/main.rkt'].privateExports; +var isImage = imageLibrary.isImage; + + var PAUSE = plt.runtime.PAUSE; @@ -26,7 +30,7 @@ var bigBang = function(MACHINE, initW, handlers) { var toplevelNode = $('
').css('border', '2').appendTo(document.body); var configs = []; - var isOnDrawSeen = false; + var isOutputConfigSeen = false; for (var i = 0 ; i < handlers.length; i++) { if (isWorldConfigOption(handlers[i])) { @@ -35,12 +39,12 @@ var bigBang = function(MACHINE, initW, handlers) { else { configs.push(handlers[i]); } - if (isOnDraw(handlers[i])) { isOnDrawSeen = true; } + if (isOutputConfig(handlers[i])) { isOutputConfigSeen = true; } } // If we haven't seen an onDraw function, use the default one. - if (! isOnDrawSeen) { - configs.push(new DefaultOnDraw(toplevelNode.get(0)).toRawHandler(MACHINE)); + if (! isOutputConfigSeen) { + configs.push(new DefaultDrawingOutput(toplevelNode.get(0)).toRawHandler(MACHINE)); } @@ -160,40 +164,95 @@ OnTick.prototype.toRawHandler = function(MACHINE) { -// // OnDraw +var OutputConfig = function() {} +OutputConfig.prototype = plt.baselib.heir(WorldConfigOption.prototype); +var isOutputConfig = plt.baselib.makeClassPredicate(OutputConfig); -var OnDraw = function(handler) { - WorldConfigOption.call(this, 'on-draw'); + + + + +// // ToDraw + +var ToDraw = function(handler) { + WorldConfigOption.call(this, 'to-draw'); this.handler = handler; }; -OnDraw.prototype = plt.baselib.heir(WorldConfigOption.prototype); +ToDraw.prototype = plt.baselib.heir(OutputConfig.prototype); -OnDraw.prototype.toRawHandler = function(MACHINE) { - var worldFunction = function(world, k) { - // FIXME: call the handler instead! - k(plt.baselib.format.toDomNode(world)); +ToDraw.prototype.toRawHandler = function(MACHINE) { + var that = this; + var reusableCanvas; + var reusableCanvasNode; + var toplevelNode = this.toplevelNode; + var adaptedWorldFunction = adaptWorldFunction(this.handler); + + var worldFunction = function(world, success) { + + adaptedWorldFunction( + world, + function(v) { + // fixme: once jsworld supports fail continuations, use them + // to check the status of the scene object and make sure it's an + // image. + + + if (isImage(v) ) { + var width = v.getWidth(); + var height = v.getHeight(); + + if (! reusableCanvas) { + reusableCanvas = imageLibrary.makeCanvas(width, height); + // Note: the canvas object may itself manage objects, + // as in the case of an excanvas. In that case, we must make + // sure jsworld doesn't try to disrupt its contents! + reusableCanvas.jsworldOpaque = true; + reusableCanvasNode = rawJsworld.node_to_tree(reusableCanvas); + } + reusableCanvas.width = width; + reusableCanvas.height = height; + var ctx = reusableCanvas.getContext("2d"); + v.render(ctx, 0, 0); + success(rawJsworld.node_to_tree(reusableCanvasNode)); + } else { + success(rawJsworld.node_to_tree(plt.baselib.format.toDomNode(v))); + } + }); }; - var cssFunction = function(w, k) { k([]); } + + var cssFunction = function(w, k) { + if (reusableCanvas) { + k([[reusableCanvas, + ["width", reusableCanvas.width + "px"], + ["height", reusableCanvas.height + "px"]]]); + } else { + k([]); + } + } + return rawJsworld.on_draw(worldFunction, cssFunction); }; -var isOnDraw = plt.baselib.makeClassPredicate(OnDraw); -var DefaultOnDraw = function(toplevelNode) { - WorldConfigOption.call(this, 'on-draw'); - this.toplevelNode = toplevelNode; + + + +var DefaultDrawingOutput = function(toplevelNode) { + WorldConfigOption.call(this, 'to-draw'); +// this.toplevelNode = toplevelNode; }; -DefaultOnDraw.prototype = plt.baselib.heir(WorldConfigOption.prototype); +DefaultDrawingOutput.prototype = plt.baselib.heir(WorldConfigOption.prototype); -DefaultOnDraw.prototype.toRawHandler = function(MACHINE) { +DefaultDrawingOutput.prototype.toRawHandler = function(MACHINE) { var that = this; var worldFunction = function(world, k) { - k([that.toplevelNode, - rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world))]); +// k([that.toplevelNode, +// rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world))]); + k(rawJsworld.node_to_tree(plt.baselib.format.toDomNode(world))); }; var cssFunction = function(w, k) { k([]); } return rawJsworld.on_draw(worldFunction, cssFunction); @@ -202,6 +261,10 @@ DefaultOnDraw.prototype.toRawHandler = function(MACHINE) { +////////////////////////////////////////////////////////////////////// + + + var StopWhen = function(handler) { WorldConfigOption.call(this, 'stop-when'); this.handler = handler; diff --git a/world/main.rkt b/world/main.rkt index 76760f2..58e019e 100644 --- a/world/main.rkt +++ b/world/main.rkt @@ -1,11 +1,13 @@ #lang s-exp "../lang/js/js.rkt" +(require "../image.rkt") + (declare-implementation #:racket "racket-impl.rkt" #:javascript ( ;; the raw implementation doesn't know anything about ;; Whalesong. - "private/raw-jsworld.js" + "raw-jsworld.js" ;; We add Whalesong-specific things here. "kernel.js" @@ -14,4 +16,7 @@ #:provided-values (big-bang on-tick to-draw - stop-when)) \ No newline at end of file + stop-when)) + + + diff --git a/world/private/raw-jsworld.js b/world/raw-jsworld.js similarity index 100% rename from world/private/raw-jsworld.js rename to world/raw-jsworld.js