Autoscale is back
authorveltr
Wed, 27 Mar 2013 18:11:18 +0100
changeset 116 07f9fc847d5d
parent 115 e905eb562074
child 117 803f14e35431
Autoscale is back
client/js/full-json.js
client/js/paper-renderer.js
client/js/save-once.js
--- a/client/js/full-json.js	Tue Mar 26 19:22:27 2013 +0100
+++ b/client/js/full-json.js	Wed Mar 27 18:11:18 2013 +0100
@@ -8,6 +8,7 @@
     var _load = function() {
         Rkns.$.getJSON(_opts.url, function(_data) {
             _proj.set(_data, {validate: true});
+            _renkan.renderer.autoScale();
         });
     }
     var _save = function() {
--- a/client/js/paper-renderer.js	Tue Mar 26 19:22:27 2013 +0100
+++ b/client/js/paper-renderer.js	Wed Mar 27 18:11:18 2013 +0100
@@ -24,6 +24,7 @@
     _CLICKMODE_ENDEDGE : 3,
     _IMAGE_MAX_KB : 500,
     _NODE_SIZE_STEP: Math.LN2/4,
+    _AUTOSCALE_MARGIN: 50,
     _MINIMAP_WIDTH: 160,
     _MINIMAP_HEIGHT: 120,
     _USER_PLACEHOLDER : {
@@ -1686,6 +1687,27 @@
     this.redraw();
 }
 
+Rkns.Renderer.Scene.prototype.autoScale = function() {
+	console.log("Autoscale")
+    var nodes = this.renkan.project.get("nodes")
+    if (nodes.length > 1) {
+        var _xx = nodes.map(function(_node) { return _node.get("position").x }),
+            _yy = nodes.map(function(_node) { return _node.get("position").y }),
+            _minx = Math.min.apply(Math, _xx),
+            _miny = Math.min.apply(Math, _yy),
+            _maxx = Math.max.apply(Math, _xx),
+            _maxy = Math.max.apply(Math, _yy);
+        var _scale = Math.min((paper.view.size.width - 2 * Rkns.Renderer._AUTOSCALE_MARGIN) / (_maxx - _minx), (paper.view.size.height - 2 * Rkns.Renderer._AUTOSCALE_MARGIN) / (_maxy - _miny));
+		console.log(_minx, _miny, _maxx, _maxy, _scale);
+        this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(_scale));
+        this.setScale(_scale);
+    }
+    if (nodes.length === 1) {
+        this.offset = paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y]));
+        this.setScale(1);
+    }
+}
+
 Rkns.Renderer.Scene.prototype.redrawMiniframe = function() {
 	var topleft = this.toMinimapCoords(this.toModelCoords(new paper.Point([0,0]))),
 		bottomright = this.toMinimapCoords(this.toModelCoords(paper.view.bounds.bottomRight));
--- a/client/js/save-once.js	Tue Mar 26 19:22:27 2013 +0100
+++ b/client/js/save-once.js	Wed Mar 27 18:11:18 2013 +0100
@@ -21,6 +21,7 @@
             data: getdata,
             success: function(_data) {
                 _proj.set(_data, {validate: true});
+            	_renkan.renderer.autoScale();
             }
         });
     }