Tree
内容纲要
由于XML的操作和CSS样式的渲染很耗性能,未来会逐步被替代,这里写一个废弃XML结构的高效Tree控件示例,供未来使用。
样例仅供参考,禁止用于项目。
package tree {
import display.Sprite;
import events.MouseEvent;
public class TreeNode extends Sprite {
protected var bg:Sprite;
protected var nodeContainer:Sprite;
protected var isOpen:Boolean = true;
protected var isHide:Boolean;
protected var _c:Sprite;
public var isSelected:Boolean = false;
public var nodeArr:Array;
public var _w:int;
public var _h:int;
public var _space:int;
public var pNode:TreeNode;
public var value:Object;
public function TreeNode(container:Sprite, data:Object = null, width:int = 150, height:int = 20, space:int = 0, _isHide:Boolean = false) {
super();
_c = container ? container : this;
_w = width;
_h = height;
_space = space;
isHide = _isHide;
init();
if (!isHide) {
setValue(data);
}
}
private function clickHandler(e:MouseEvent):void {
if (nodeArr.length > 0) {
isOpen ? closeList() : openList();
} else {
_c.dispatchEvent(new TreeEvent(TreeEvent.SELECTED, this));
}
}
private function overHandler(e:MouseEvent):void {
if (isSelected) {
return;
}
draw(true);
}
private function outHandler(e:MouseEvent):void {
if (isSelected) {
return;
}
draw(false);
}
private function selectedHandler(e:TreeEvent):void {
if (nodeArr.length > 0) {
return;
}
unSelected();
e.node.selected();
}
private function changeHandler(e:TreeEvent):void {
update();
}
private function unSelected():void {
isSelected = false;
draw();
}
public function getNodeByIndex(index:int):TreeNode {
if (index < 0 || index >= nodeArr.length) {
return null;
}
return nodeArr[index];
}
public function addNode(node:TreeNode):void {
if (nodeArr.length > 0) {
node.y = nodeArr[nodeArr.length - 1].y + node._h + _space;
} else {
node.y = 0;
}
node.pNode = this;
nodeContainer.addChild(node);
nodeArr.push(node);
_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));
if (nodeArr.length == 1) {
draw();
bg.buttonMode = true;
}
}
public function removeNode(node:TreeNode):void {
var index:int = nodeArr.indexOf(node);
if (index != -1) {
var tempNode:TreeNode = nodeArr.splice(index, 1)[0];
nodeContainer.removeChild(tempNode);
tempNode.clear();
_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));
if (nodeArr.length == 0) {
bg.buttonMode = false;
draw();
}
}
}
public function update():void {
clean();
var currentNode:TreeNode;
var prevNode:TreeNode;
for (var i:int = 0; i < nodeArr.length; i++) {
currentNode = nodeArr[i];
currentNode.x = 0;
currentNode.update();
if (i == 0) {
currentNode.y = 0;
} else {
prevNode = nodeArr[i - 1];
currentNode.y = prevNode.y + prevNode.height + _space;
}
nodeContainer.addChild(currentNode);
}
}
public function clean():void {
for each (var node:TreeNode in nodeArr) {
if (node.stage) {
nodeContainer.removeChild(node);
}
}
}
public function clear():void {
bg.removeEventListener(MouseEvent.MOUSE_OVER, overHandler);
bg.removeEventListener(MouseEvent.MOUSE_OUT, outHandler);
bg.removeEventListener(MouseEvent.CLICK, clickHandler);
_c.removeEventListener(TreeEvent.CHANGE, changeHandler);
_c.removeEventListener(TreeEvent.SELECTED, selectedHandler);
}
public function openList():void {
isOpen = true;
addChild(nodeContainer);
_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));
}
public function closeList():void {
isOpen = false;
removeChild(nodeContainer);
_c.dispatchEvent(new TreeEvent(TreeEvent.CHANGE));
}
protected function init():void {
nodeArr = new Array();
bg = new Sprite();
addChild(bg);
draw();
nodeContainer = new Sprite();
nodeContainer.y = _h;
addChild(nodeContainer);
if (isHide) {
bg.visible = false;
nodeContainer.y = 0;
}
bg.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
bg.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
bg.addEventListener(MouseEvent.CLICK, clickHandler);
_c.addEventListener(TreeEvent.CHANGE, changeHandler);
_c.addEventListener(TreeEvent.SELECTED, selectedHandler);
}
protected function draw(f:Boolean = false):void {
}
public function selected():void {
isSelected = true;
}
public function setValue(o:Object):void {
value = o;
}
}
}
代码浅显易懂,无需解释。
希望很快由WEBGL替代XML/HTML/XHTML/MXML这种Tag。
code enjoy! 😄😄😁
作者:indeex
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。