2026-02-03 03:53:40 +01:00

102 lines
2.8 KiB
ActionScript

class EntityTreeRender {
EntityTreeRender(Entity _entity) {
entity = _entity;
}
Entity entity;
bool isActiveEntity() {
return entity == ActiveEntity::getActiveEntity();
}
void renderEntity() {
string displayName = entity.name;
if (displayName == "") {
displayName = "-";
} else {
displayName = "\uf1b2 " + displayName;
}
displayName += "##" + entity.id;
array<Entity>@ childs = entity.getChildrens();
if (childs.length() == 0) {
// END OF THE TREE
ImGui::treeNodeLeaf(displayName, isActiveEntity());
interaction();
} else {
// ADD ANOTHER NODE
bool opened = ImGui::treeNode(displayName, isActiveEntity(), SimpleFunction(this.renderChilds));
if (!opened) {
interaction();
}
}
}
void renderChilds() {
interaction();
array<Entity>@ childs = entity.getChildrens();
for (uint i = 0; i < childs.length(); i++) {
EntityTreeRender child(childs[i]);
child.renderEntity();
}
}
void interaction() {
ImGui::dragDropSource("ENTITY", any(entity), entity.name);
ImGui::dragDropTarget("ENTITY", ReciverFunction(this.entityDrop));
ImGui::contextItemPopup("POP_ENTITY_" + entity.id, SimpleFunction(this.renderContextMenu));
if (ImGui::isItemClicked(0)) {
ActiveEntity::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 (ImGui::menuItem("New Entity")) {
print("Child id : " + entity.id);
entity.createChild("node");
print("Child id : " + entity.id);
}
} else {
if (ImGui::menuItem("Add child")) {
print("Child id : " + entity.id);
entity.createChild("node");
}
if (ImGui::menuItem("Rename")) {
ImGui::openPopup("Rename entity", any(entity));
}
if (ImGui::menuItem("Destroy")) {
entity.destroy();
}
}
}
}
class TreePanel : Panel {
void onInit() {
print("Wtf2");
}
void onImGui() {
Entity root;
EntityTreeRender rootTree(root);
ImGui::contextMenuPopup("Window popup", SimpleFunction(rootTree.renderContextMenu));
rootTree.renderEntity();
}
}