client/js/renderer/scene.js
changeset 403 96781c1a8bbe
parent 398 57f8d344fde9
child 411 38527e730186
equal deleted inserted replaced
402:c8eaff36abc7 403:96781c1a8bbe
    28         this.node_layer = new paper.Layer();
    28         this.node_layer = new paper.Layer();
    29         this.buttons_layer = new paper.Layer();
    29         this.buttons_layer = new paper.Layer();
    30         this.delete_list = [];
    30         this.delete_list = [];
    31         this.redrawActive = true;
    31         this.redrawActive = true;
    32 
    32 
    33         var currentWidth = this.canvas_$.width();
       
    34         var currentHeight = this.canvas_$.height();
       
    35 
       
    36         if (_renkan.options.show_minimap) {
    33         if (_renkan.options.show_minimap) {
    37             this.minimap = {
    34             this.minimap = {
    38                     background_layer: new paper.Layer(),
    35                     background_layer: new paper.Layer(),
    39                     edge_layer: new paper.Layer(),
    36                     edge_layer: new paper.Layer(),
    40                     node_layer: new paper.Layer(),
    37                     node_layer: new paper.Layer(),
    58             this.minimap.miniframe = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size);
    55             this.minimap.miniframe = new paper.Path.Rectangle(this.minimap.topleft, this.minimap.size);
    59             this.minimap.node_group.addChild(this.minimap.miniframe);
    56             this.minimap.node_group.addChild(this.minimap.miniframe);
    60             this.minimap.miniframe.fillColor = '#c0c0ff';
    57             this.minimap.miniframe.fillColor = '#c0c0ff';
    61             this.minimap.miniframe.opacity = 0.3;
    58             this.minimap.miniframe.opacity = 0.3;
    62             this.minimap.miniframe.strokeColor = '#000080';
    59             this.minimap.miniframe.strokeColor = '#000080';
    63             this.minimap.miniframe.strokeWidth = 3;
    60             this.minimap.miniframe.strokeWidth = 2;
    64             this.minimap.miniframe.__representation = new MiniFrame(this, null);
    61             this.minimap.miniframe.__representation = new MiniFrame(this, null);
    65         }
    62         }
    66 
    63 
    67         this.throttledPaperDraw = _(function() {
    64         this.throttledPaperDraw = _(function() {
    68             paper.view.draw();
    65             paper.view.draw();
   278         });
   275         });
   279         bindClick(".Rk-Fold-Bins", "foldBins");
   276         bindClick(".Rk-Fold-Bins", "foldBins");
   280 
   277 
   281         paper.view.onResize = function(_event) {
   278         paper.view.onResize = function(_event) {
   282             var _ratio,
   279             var _ratio,
   283                 newWidth= _this.canvas_$.parent().width(),
   280                 newWidth = _event.width,
   284                 newHeight = _this.canvas_$.parent().height();
   281                 newHeight = _event.height;
   285             // Because of paper bug which does not calculate the good height and width
       
   286             // We have to update manually the canvas's height and width
       
   287 
       
   288             paper.view._viewSize.height =  _event.size.height = _this.canvas_$.parent().height();
       
   289             paper.view._viewSize.width =  _event.size.height = _this.canvas_$.parent().width();
       
   290 
   282 
   291             if (_this.minimap) {
   283             if (_this.minimap) {
   292                 _this.minimap.topleft = paper.view.bounds.bottomRight.subtract(_this.minimap.size);
   284                 _this.minimap.topleft = paper.view.bounds.bottomRight.subtract(_this.minimap.size);
   293                 _this.minimap.rectangle.fitBounds(_this.minimap.topleft.subtract([2,2]), _this.minimap.size.add([4,4]));
   285                 _this.minimap.rectangle.fitBounds(_this.minimap.topleft.subtract([2,2]), _this.minimap.size.add([4,4]));
   294                 _this.minimap.cliprectangle.fitBounds(_this.minimap.topleft, _this.minimap.size);
   286                 _this.minimap.cliprectangle.fitBounds(_this.minimap.topleft, _this.minimap.size);
   295             }
   287             }
   296 
   288 
       
   289             var ratioH = newHeight/(newHeight-_event.delta.height),
       
   290                 ratioW = newWidth/(newWidth-_event.delta.width);
   297             if (newHeight < newWidth) {
   291             if (newHeight < newWidth) {
   298                     _ratio = (newHeight/currentHeight);
   292                     _ratio = ratioH;
   299             } else {
   293             } else {
   300                 _ratio = (newWidth/currentWidth);
   294                 _ratio = ratioW;
   301             }
   295             }
   302 
   296 
   303             _this.resizeZoom((newWidth/currentWidth), (newHeight/currentHeight), _ratio);
   297             _this.resizeZoom(ratioW, ratioH, _ratio);
   304 
       
   305             currentWidth = newWidth;
       
   306             currentHeight = newHeight;
       
   307 
   298 
   308             _this.redraw();
   299             _this.redraw();
   309 
   300 
   310         };
   301         };
   311 
   302 
   520                 '<% if (options.show_export_button) { %><div class="Rk-TopBar-Separator"></div><div class="Rk-TopBar-Button Rk-Export-Button"><div class="Rk-TopBar-Tooltip"><div class="Rk-TopBar-Tooltip-Contents"><%-translate("Download Project")%></div></div></div><div class="Rk-TopBar-Separator"></div><% } %>' +
   511                 '<% if (options.show_export_button) { %><div class="Rk-TopBar-Separator"></div><div class="Rk-TopBar-Button Rk-Export-Button"><div class="Rk-TopBar-Tooltip"><div class="Rk-TopBar-Tooltip-Contents"><%-translate("Download Project")%></div></div></div><div class="Rk-TopBar-Separator"></div><% } %>' +
   521                 '<% };' +
   512                 '<% };' +
   522                 'if (options.show_search_field) { %>' +
   513                 'if (options.show_search_field) { %>' +
   523                 '<form action="#" class="Rk-GraphSearch-Form"><input type="search" class="Rk-GraphSearch-Field" placeholder="<%- translate("Search in graph") %>" /></form><div class="Rk-TopBar-Separator"></div><% } %></div><% } %>' +
   514                 '<form action="#" class="Rk-GraphSearch-Form"><input type="search" class="Rk-GraphSearch-Field" placeholder="<%- translate("Search in graph") %>" /></form><div class="Rk-TopBar-Separator"></div><% } %></div><% } %>' +
   524                 '<div class="Rk-Editing-Space<% if (!options.show_top_bar) { %> Rk-Editing-Space-Full<% } %>">' +
   515                 '<div class="Rk-Editing-Space<% if (!options.show_top_bar) { %> Rk-Editing-Space-Full<% } %>">' +
   525                 '<div class="Rk-Labels"></div><canvas class="Rk-Canvas" resize></canvas><div class="Rk-Notifications"></div><div class="Rk-Editor">' +
   516                 '<div class="Rk-Labels"></div><canvas class="Rk-Canvas" <% if (options.resize) { %> resize="" <% } %>></canvas><div class="Rk-Notifications"></div><div class="Rk-Editor">' +
   526                 '<% if (options.show_bins) { %><div class="Rk-Fold-Bins">&laquo;</div><% } %>' +
   517                 '<% if (options.show_bins) { %><div class="Rk-Fold-Bins">&laquo;</div><% } %>' +
   527                 '<div class="Rk-ZoomButtons"><div class="Rk-ZoomIn" title="<%-translate("Zoom In")%>"></div><div class="Rk-ZoomFit" title="<%-translate("Zoom Fit")%>"></div><div class="Rk-ZoomOut" title="<%-translate("Zoom Out")%>"></div>' +
   518                 '<div class="Rk-ZoomButtons"><div class="Rk-ZoomIn" title="<%-translate("Zoom In")%>"></div><div class="Rk-ZoomFit" title="<%-translate("Zoom Fit")%>"></div><div class="Rk-ZoomOut" title="<%-translate("Zoom Out")%>"></div>' +
   528                 '<% if (options.editor_mode) { %><div class="Rk-ZoomSave" title="<%-translate("Zoom Save")%>"></div><% } %>' +
   519                 '<% if (options.editor_mode) { %><div class="Rk-ZoomSave" title="<%-translate("Zoom Save")%>"></div><% } %>' +
   529                 '<% if (options.editor_mode || !isNaN(parseInt(options.default_view))) { %><div class="Rk-ZoomSetSaved" title="<%-translate("View saved zoom")%>"></div><% } %></div>' +
   520                 '<% if (options.editor_mode || !isNaN(parseInt(options.default_view))) { %><div class="Rk-ZoomSetSaved" title="<%-translate("View saved zoom")%>"></div><% } %></div>' +
   530                 '</div></div>'
   521                 '</div></div>'
   531         ),
   522         ),
   532         fixSize: function(_autoscale) {
   523         fixSize: function(_autoscale) {
   533             var w = this.$.width(),
       
   534             h = this.$.height();
       
   535             if (this.renkan.options.show_top_bar) {
       
   536                 h -= this.$.find(".Rk-TopBar").height();
       
   537             }
       
   538             this.canvas_$.attr({
       
   539                 width: w,
       
   540                 height: h
       
   541             });
       
   542 
       
   543             paper.view.viewSize = new paper.Size([w, h]);
       
   544 
   524 
   545             if (_autoscale) {
   525             if (_autoscale) {
   546                 // If _autoscale, we get the initial view (zoom+offset) set in the project datas.
   526                 // If _autoscale, we get the initial view (zoom+offset) set in the project datas.
   547                 if(this.renkan.read_only && !isNaN(parseInt(this.renkan.options.default_view))){
   527                 if(this.renkan.read_only && !isNaN(parseInt(this.renkan.options.default_view))){
   548                     this.autoScale(this.renkan.project.get("views")[parseInt(this.renkan.options.default_view)]);
   528                     this.autoScale(this.renkan.project.get("views")[parseInt(this.renkan.options.default_view)]);
   732                 this.setScale(1, paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y])));
   712                 this.setScale(1, paper.view.center.subtract(new paper.Point([nodes.at(0).get("position").x, nodes.at(0).get("position").y])));
   733             }
   713             }
   734         },
   714         },
   735         redrawMiniframe: function() {
   715         redrawMiniframe: function() {
   736             var topleft = this.toMinimapCoords(this.toModelCoords(new paper.Point([0,0]))),
   716             var topleft = this.toMinimapCoords(this.toModelCoords(new paper.Point([0,0]))),
   737             bottomright = this.toMinimapCoords(this.toModelCoords(paper.view.bounds.bottomRight));
   717                 bottomright = this.toMinimapCoords(this.toModelCoords(paper.view.bounds.bottomRight));
   738             this.minimap.miniframe.fitBounds(topleft, bottomright);
   718             this.minimap.miniframe.fitBounds(topleft, bottomright);
   739         },
   719         },
   740         rescaleMinimap: function() {
   720         rescaleMinimap: function() {
   741             var nodes = this.renkan.project.get("nodes");
   721             var nodes = this.renkan.project.get("nodes");
   742             if (nodes.length > 1) {
   722             if (nodes.length > 1) {
   743                 var _xx = nodes.map(function(_node) { return _node.get("position").x; }),
   723                 var _xx = nodes.map(function(_node) { return _node.get("position").x; }),
   744                 _yy = nodes.map(function(_node) { return _node.get("position").y; }),
   724                     _yy = nodes.map(function(_node) { return _node.get("position").y; }),
   745                 _minx = Math.min.apply(Math, _xx),
   725                     _minx = Math.min.apply(Math, _xx),
   746                 _miny = Math.min.apply(Math, _yy),
   726                     _miny = Math.min.apply(Math, _yy),
   747                 _maxx = Math.max.apply(Math, _xx),
   727                     _maxx = Math.max.apply(Math, _xx),
   748                 _maxy = Math.max.apply(Math, _yy);
   728                     _maxy = Math.max.apply(Math, _yy);
   749                 var _scale = Math.min(
   729                 var _scale = Math.min(
   750                         this.scale * 0.8 * this.renkan.options.minimap_width / paper.view.bounds.width,
   730                         this.scale * 0.8 * this.renkan.options.minimap_width / paper.view.bounds.width,
   751                         this.scale * 0.8 * this.renkan.options.minimap_height / paper.view.bounds.height,
   731                         this.scale * 0.8 * this.renkan.options.minimap_height / paper.view.bounds.height,
   752                         ( this.renkan.options.minimap_width - 2 * this.renkan.options.minimap_padding ) / (_maxx - _minx),
   732                         ( this.renkan.options.minimap_width - 2 * this.renkan.options.minimap_padding ) / (_maxx - _minx),
   753                         ( this.renkan.options.minimap_height - 2 * this.renkan.options.minimap_padding ) / (_maxy - _miny)
   733                         ( this.renkan.options.minimap_height - 2 * this.renkan.options.minimap_padding ) / (_maxy - _miny)
  1184                 _repr.openEditor();
  1164                 _repr.openEditor();
  1185             }
  1165             }
  1186         },
  1166         },
  1187         fullScreen: function() {
  1167         fullScreen: function() {
  1188             var _isFull = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen,
  1168             var _isFull = document.fullScreen || document.mozFullScreen || document.webkitIsFullScreen,
  1189               _el = this.renkan.$[0],
  1169                 _el = this.renkan.$[0],
  1190               _requestMethods = ["requestFullScreen","mozRequestFullScreen","webkitRequestFullScreen"],
  1170                 _requestMethods = ["requestFullScreen","mozRequestFullScreen","webkitRequestFullScreen"],
  1191               _cancelMethods = ["cancelFullScreen","mozCancelFullScreen","webkitCancelFullScreen"],
  1171                 _cancelMethods = ["cancelFullScreen","mozCancelFullScreen","webkitCancelFullScreen"],
  1192               i;
  1172                 i;
  1193             if (_isFull) {
  1173             if (_isFull) {
  1194                 for (i = 0; i < _cancelMethods.length; i++) {
  1174                 for (i = 0; i < _cancelMethods.length; i++) {
  1195                     if (typeof document[_cancelMethods[i]] === "function") {
  1175                     if (typeof document[_cancelMethods[i]] === "function") {
  1196                         document[_cancelMethods[i]]();
  1176                         document[_cancelMethods[i]]();
  1197                         break;
  1177                         break;
  1205                 }
  1185                 }
  1206                 if (this.renkan.options.show_bins && (this.renkan.$.find(".Rk-Bins").position().left > 0)) {
  1186                 if (this.renkan.options.show_bins && (this.renkan.$.find(".Rk-Bins").position().left > 0)) {
  1207                     widthAft -= this.renkan.$.find(".Rk-Bins").width();
  1187                     widthAft -= this.renkan.$.find(".Rk-Bins").width();
  1208                 }
  1188                 }
  1209 
  1189 
  1210                 this.canvas_$.attr({
  1190                 paper.view.viewSize = new paper.Size([widthAft, heightAft]);
  1211                     width: widthAft,
       
  1212                     height: heightAft
       
  1213                 });
       
  1214 
       
  1215                 paper.view.viewSize = new paper.Size([this.canvas_$.width(), this.canvas_$.height()]);
       
  1216 
  1191 
  1217             } else {
  1192             } else {
  1218                 for (i = 0; i < _requestMethods.length; i++) {
  1193                 for (i = 0; i < _requestMethods.length; i++) {
  1219                     if (typeof _el[_requestMethods[i]] === "function") {
  1194                     if (typeof _el[_requestMethods[i]] === "function") {
  1220                         _el[_requestMethods[i]]();
  1195                         _el[_requestMethods[i]]();
  1221                         break;
  1196                         break;
  1222                     }
  1197                     }
  1223                 }
  1198                 }
       
  1199                 this.redraw();
  1224             }
  1200             }
  1225         },
  1201         },
  1226         zoomOut: function() {
  1202         zoomOut: function() {
  1227             var _newScale = this.scale * Math.SQRT1_2,
  1203             var _newScale = this.scale * Math.SQRT1_2,
  1228             _offset = new paper.Point([
  1204             _offset = new paper.Point([