96 lines
2.5 KiB
ActionScript

import Entity getActiveEntity() from "ActiveEntity";
import void setActiveEntity(Entity) from "ActiveEntity";
class EntityTreeRender {
EntityTreeRender(Entity _entity) {
entity = _entity;
}
Entity entity;
bool isActiveEntity() {
return entity == getActiveEntity();
}
void renderEntity() {
string displayName = entity.name;
if (displayName == "") {
displayName = "-";
} else {
displayName = "\uf1b2 " + displayName;
}
displayName += "##" + entity.id;
if (entity.childs.count == 0) {
// END OF THE TREE
UI::treeNodeLeaf(displayName, isActiveEntity());
interaction();
} else {
// ADD ANOTHER NODE
bool opened = UI::treeNode(displayName, isActiveEntity(), Callback(this.renderChilds));
if (!opened) {
interaction();
}
}
}
void renderChilds() {
interaction();
for (int i = 0; i < entity.childs.count; i++) {
EntityTreeRender child(entity.childs[i]);
child.renderEntity();
}
}
void interaction() {
UI::dragDropSource("ENTITY", any(entity), entity.name);
UI::dragDropTarget("ENTITY", AnyCallback(this.entityDrop));
UI::contextItemPopup("POP_ENTITY_" + entity.id, Callback(this.renderContextMenu));
if (UI::isItemClicked(0)) {
setActiveEntity(entity);
}
}
void entityDrop(any@ data) {
Entity data_entity;
data.retrieve(data_entity);
// You cant be the father of your father
if (data_entity.isRoot || data_entity.isDescendantOf(entity)) {
return;
}
data_entity.parent = entity;
}
void renderContextMenu() {
if (entity.isRoot) {
if (UI::menuItem("New Entity")) {
entity.createChild("node");
}
} else {
if (UI::menuItem("Add child")) {
entity.createChild("node");
}
if (UI::menuItem("Rename")) {
UI::openPopup("Rename entity", any(entity));
}
if (UI::menuItem("Destroy")) {
entity.destroy();
}
}
}
}
class TreePanel : Panel {
void render() {
Entity root = Engine::getRoot();
EntityTreeRender rootTree(root);
UI::contextMenuPopup("Window popup", Callback(rootTree.renderContextMenu));
rootTree.renderEntity();
}
}