📄 nodesprite.as
字号:
}
/**
* Removes all edges of the indicated edge type. Note that this method
* does not update the edges themselves or the other nodes.
* @param type the type of edges to remove. For example, IN_LINKS,
* OUT_LINKS, TREE_LINKS, etc.
*/
public function removeEdges(type:int):void
{
var e:EdgeSprite;
if (type & PARENT_LINK && _parentEdge) {
_parentEdge = null;
}
if (type & CHILD_LINKS && _childEdges) {
while (_childEdges.length > 0) { e=_childEdges.pop(); }
}
if (type & OUT_LINKS && _outEdges) {
while (_outEdges.length > 0) { e=_outEdges.pop(); }
}
if (type & IN_LINKS && _inEdges) {
while (_inEdges.length > 0) { e=_inEdges.pop(); }
}
}
/**
* Removes an edge from the child links list. Note that this method
* does not update the edge itself or the other node.
* @param e the edge to remove
*/
public function removeChildEdge(e:EdgeSprite):void
{
Arrays.remove(_childEdges, e);
}
/**
* Removes an edge from the inlinks list. Note that this method
* does not update the edge itself or the other node.
* @param e the edge to remove
*/
public function removeInEdge(e:EdgeSprite):void
{
Arrays.remove(_inEdges, e);
}
/**
* Removes an edge from the outlinks list. Note that this method
* does not update the edge itself or the other node.
* @param e the edge to remove
*/
public function removeOutEdge(e:EdgeSprite):void
{
Arrays.remove(_outEdges, e);
}
// -- Visitor Methods --------------------------------------------------
/**
* Sorts the order of connected edges according to their properties.
* Each type of edge (in, out, or child) is sorted separately.
* @param opt flag indicating which set(s) of edges should be sorted
* @param sort the sort arguments.
* If a String is provided, the data will be sorted in ascending order
* according to the data field named by the string.
* If an Array is provided, the data will be sorted according to the
* fields in the array. In addition, field names can optionally
* be followed by a boolean value. If true, the data is sorted in
* ascending order (the default). If false, the data is sorted in
* descending order.
*/
public function sortEdgesBy(opt:uint=ALL_LINKS, ...sort):void
{
if (sort.length == 0) return;
if (sort[0] is Array) sort = sort[0];
var s:Function = Sort.$(sort);
if (opt & IN_LINKS && _inEdges != null) _inEdges.sort(s);
if (opt & OUT_LINKS && _outEdges != null) _outEdges.sort(s);
if (opt & CHILD_LINKS && _childEdges != null) {
_childEdges.sort(s);
for (var i:uint=0; i<_childEdges.length; ++i)
_childEdges[i].other(this).parentIndex = i;
}
}
/**
* Visits this node's edges, invoking a function on each visited edge.
* @param f the function to invoke on the edges. If the function
* returns true, the visitation is ended with an early exit.
* @param opt flag indicating which sets of edges should be visited
* @return true if the visitation was interrupted with an early exit
*/
public function visitEdges(f:Function, opt:uint=ALL_LINKS,
filter:*=null):Boolean
{
var ff:Function = Filter.$(filter);
var rev:Boolean = (opt & REVERSE) > 0;
if (opt & IN_LINKS && _inEdges != null) {
if (visitEdgeHelper(f, _inEdges, rev, ff)) return true;
}
if (opt & OUT_LINKS && _outEdges != null) {
if (visitEdgeHelper(f, _outEdges, rev, ff)) return true;
}
if (opt & CHILD_LINKS && _childEdges != null) {
if (visitEdgeHelper(f, _childEdges, rev, ff)) return true;
}
if (opt & PARENT_LINK && _parentEdge != null) {
if ((ff==null || ff(_parentEdge)) && f(_parentEdge))
return true;
}
return false;
}
private function visitEdgeHelper(f:Function, a:Array, r:Boolean,
ff:Function):Boolean
{
var i:uint, n:uint=a.length, v:*;
if (r) {
for (i=n; --i>=0;) {
if ((ff==null || ff(a[i])) && f(a[i]) as Boolean)
return true;
}
} else {
for (i=0; i<n; ++i) {
if ((ff==null || ff(a[i])) && f(a[i]) as Boolean)
return true;
}
}
return false;
}
/**
* Visits the nodes connected to this node by edges, invoking a
* function on each visited node.
* @param f the function to invoke on the nodes. If the function
* returns true, the visitation is ended with an early exit.
* @param opt flag indicating which sets of edges should be traversed
* @return true if the visitation was interrupted with an early exit
*/
public function visitNodes(f:Function, opt:uint=ALL_LINKS,
filter:*=null):Boolean
{
var ff:Function = Filter.$(filter);
var rev:Boolean = (opt & REVERSE) > 0;
if (opt & IN_LINKS && _inEdges != null) {
if (visitNodeHelper(f, _inEdges, rev, ff)) return true;
}
if (opt & OUT_LINKS && _outEdges != null) {
if (visitNodeHelper(f, _outEdges, rev, ff)) return true;
}
if (opt & CHILD_LINKS && _childEdges != null) {
if (visitNodeHelper(f, _childEdges, rev, ff)) return true;
}
if (opt & PARENT_LINK && _parentEdge != null) {
if ((ff==null||ff(_parentEdge)) && f(_parentEdge.other(this)))
return true;
}
return false;
}
private function visitNodeHelper(f:Function, a:Array, r:Boolean,
ff:Function):Boolean
{
var i:uint, n:uint=a.length, u:NodeSprite;
if (r) {
for (i=n; --i>=0;) {
u = a[i].other(this);
if ((ff==null || ff(u)) && f(u) as Boolean)
return true;
}
} else {
for (i=0; i<n; ++i) {
u = a[i].other(this);
if ((ff==null || ff(u)) && f(u) as Boolean)
return true;
}
}
return false;
}
/**
* Visits the subtree rooted at this node using a depth first search,
* invoking the input function on each visited node.
* @param f the function to invoke on the nodes. If the function
* returns true, the visitation is ended with an early exit.
* @param preorder if true, nodes are visited in a pre-order traversal;
* if false, they are visited in a post-order traversal
* @return true if the visitation was interrupted with an early exit
*/
public function visitTreeDepthFirst(f:Function, preorder:Boolean=false):Boolean
{
if (preorder && (f(this) as Boolean)) return true;
for (var i:uint = 0; i<childDegree; ++i) {
if (getChildNode(i).visitTreeDepthFirst(f, preorder))
return true;
}
if (!preorder && (f(this) as Boolean)) return true;
return false;
}
/**
* Visits the subtree rooted at this node using a breadth first search,
* invoking the input function on each visited node.
* @param f the function to invoke on the nodes. If the function
* returns true, the visitation is ended with an early exit.
* @return true if the visitation was interrupted with an early exit
*/
public function visitTreeBreadthFirst(f:Function):Boolean
{
var q:Array = new Array(), x:NodeSprite;
q.push(this);
while (q.length > 0) {
if (f(x=q.shift()) as Boolean) return true;
for (var i:uint = 0; i<x.childDegree; ++i)
q.push(x.getChildNode(i));
}
return false;
}
/**
* Sets property values on edge sprites connected to this node.
* @param vals an object containing the properties and values to set.
* @param opt flag indicating which sets of edges should be traversed
* @param trans a transitioner or time span for updating object values.
* If the input is a transitioner, it will be used to store the
* updated values. If the input is a number, a new Transitioner with
* duration set to the input value will be used. The input is null by
* default, in which case object values are updated immediately.
* @param filter an optional Boolean-valued filter function for
* limiting which items are visited
* @return the transitioner used to update the values
*/
public function setEdgeProperties(vals:Object, opt:uint=ALL_LINKS,
trans:*=null, filter:*=null):Transitioner
{
var t:Transitioner = Transitioner.instance(trans);
for (var name:String in vals) {
var val:* = vals[name];
var v:Function = val is Function ? val as Function
: val is IEvaluable ? IEvaluable(val).eval : null;
visitEdges(function(s:EdgeSprite):void {
t.setValue(s, name, (v!=null ? v(t.$(s)) : val));
}, opt, filter);
}
return t;
}
/**
* Sets property values on node sprites connected to this node.
* @param vals an object containing the properties and values to set.
* @param opt flag indicating which sets of nodes should be traversed
* @param trans a transitioner or time span for updating object values.
* If the input is a transitioner, it will be used to store the
* updated values. If the input is a number, a new Transitioner with
* duration set to the input value will be used. The input is null by
* default, in which case object values are updated immediately.
* @param filter an optional Boolean-valued filter function for
* limiting which items are visited
* @return the transitioner used to update the values
*/
public function setNodeProperties(vals:Object, opt:uint=ALL_LINKS,
trans:*=null, filter:*=null):Transitioner
{
var t:Transitioner = Transitioner.instance(trans);
for (var name:String in vals) {
var val:* = vals[name];
var v:Function = val is Function ? val as Function
: val is IEvaluable ? IEvaluable(val).eval : null;
visitNodes(function(n:NodeSprite):void {
t.setValue(n, name, (v!=null ? v(t.$(n)) : val));
}, opt, filter);
}
return t;
}
} // end of class NodeSprite
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -