Path: blob/main/projects/HexGL/libs/Editor_files/Diagram.js
4627 views
var pgli = pgli || {};1pgli.diagram = pgli.diagram || {};23pgli.diagram.Diagram = gamecore.Base.extend('Diagram',4{ // static56},7{ // instance89dom: null,10container: null,11width: 1024,12height: 768,1314timer: null,15redrawDelay: 1000/30,1617stage: null,18layers: {19background: null,20nodes: null,21links: null22},23background: null,24links: null,2526project: null,2728nodes: [],2930/**31* Bind a new renderer to given canvas32* @param HTMLElement domContainer [description]33* @param Integer width [description]34* @param Integer height [description]35* @param Integer autoRedraw If defined, sets redraw rate to given FPS36*/37init: function(domContainer, autoRedraw)38{39var self = this;4041this.dom = domContainer;42this.container = $('#'+domContainer);43this.width = this.container.width();44this.height = this.container.height();4546this.stage = new Kinetic.Stage({47container: this.dom,48width: this.width,49height: this.height50});5152this.layers.background = new Kinetic.Layer();53this.layers.nodes = new Kinetic.Layer();54this.layers.links = new Kinetic.Layer();5556this.background = new Kinetic.Rect({57width: this.width,58height: this.height,59fill: "#272822"60});6162this.layers.background.add(this.background);6364this.links = new pgli.diagram.Links(this);65this.layers.links.add(this.links.shape);6667this.stage.add(this.layers.background);68this.stage.add(this.layers.nodes);69this.stage.add(this.layers.links);7071// Stage drag hack to trigger only on background drag72var self = this;73this.background.on("mousedown", function(){74self.stage.setDraggable(true);75self.stage.on("dragmove", function(){76self.layers.background.setX(-this.getX());77self.layers.background.setY(-this.getY());78});79self.stage.on("dragend", function(){80this.off("dragend");81this.off("dragmove");82this.setDraggable(false);83});84});8586if(autoRedraw != undefined && autoRedraw != false && autoRedraw > 0)87{88this.redrawDelay = 1000 / autoRedraw;89this.timer = new bkcore.Timer();90this.timer.start();91this.autoRedraw(true);92}93},9495addNode: function(node)96{97this.nodes.push(node);98this.layers.nodes.add(node.shape);99this.layers.nodes.draw();100},101102getNode: function(nodeKey)103{104var i = 0, len = this.nodes.length;105while(i < len)106{107if(this.nodes[i].key == nodeKey)108return this.nodes[i];109i++;110}111console.warn("Error in Diagram: Unable to find node["+nodeKey+"]");112return false;113},114115draw: function()116{117this.layers.background.draw();118this.layers.nodes.draw();119this.layers.links.draw();120},121122autoRedraw: function(keep)123{124var self = this;125126if(this.timer.update() > this.redrawDelay)127{128this.timer.start();129this.draw();130}131132if(keep)133requestAnimFrame(function(){134self.autoRedraw(true);135});136},137138resize: function()139{140this.width = this.container.width();141this.height = this.container.height();142this.stage.setSize(this.width, this.height);143this.background.setSize(this.width, this.height);144this.draw();145}146147});148149