Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by enoc on 20/07/18.
- //
- #include <EddieECS/Components/Transform.hpp>
- #include <cstdlib>
- #include <EddieEddieUI/Helpers/Keyboard.hpp>
- #include <UI/EntityList.hpp>
- #include <EddieECS/Components/Visible.hpp>
- #include <EddieECS/Components/Sprite.hpp>
- #include <EddieECS/Components/Video.hpp>
- #include <EddieECS/Components/FSM.hpp>
- #include <EddieECS/Components/TamChangeError.hpp>
- #include <EddieECS/Components/IgnoreOnEditor.hpp>
- #include <EddieGraphics/ImGui/CustomWidgets.hpp>
- #include <EddieEddieUI/Events/EcsUiEvents.hpp>
- #include <EddieEngine/GameContext/ContextOperations.hpp>
- #include "UI/EntityList.hpp"
- #include <EddieEddieUI/Events/EcsUiEvents.hpp>
- #include <EddieEddieUI/CustomColors.hpp>
- #include <EddieECS/Components/NativeReel.hpp>
- #include <EddieECS/EcsBaseUtils.hpp>
- #include <EddieECS/Components/SoundComponent.hpp>
- #include <EddieEddieUI/UIContext.hpp>
- #include <EddieECS/Components/RelativeRotationPreservation.hpp>
- namespace {
- const auto REMOVE_FSM_WINDOW_ID = "Remove FSM##RemoveFsmWindowId";
- }
- // TODO: Quitar las inicializaciones...
- EddieUI::EntityList::EntityList() { }
- void EddieUI::EntityList::AdditionalKeyHandlers() {
- if (gContext) {
- if (ImGui::IsKeyPressed(Key::ESC, false)) {
- Game::ContextDropSelection(*gContext);
- }
- if (ImGui::IsKeyPressed(Key::SUPR, false) || ImGui::IsKeyPressed(Key::DEL, false)) {
- if (ImGui::IsKeyDown(Key::LEFT_CTRL)) {
- Game::ContextDeleteSelection(*gContext, true);
- } else {
- Game::ContextDeleteSelection(*gContext, false);
- }
- }
- }
- }
- void EddieUI::EntityList::Prepare() noexcept {
- if (gContext == nullptr) {
- FATAL_ERROR("GameContext Need to be linked before prepare!");
- }
- this->mSGSystem = gContext->GetEcsSystem<ECS::SceneGraphSystem>();
- this->mSelectorSystem = gContext->GetEcsSystem<ECS::EditorSelector>();
- this->mRegisterSystem = gContext->GetEcsSystem<ECS::EntityRegister>();
- componentTag.SetEditorSelector(this->mSelectorSystem);
- }
- void EddieUI::EntityList::RenderFrameUI(UIContext &context) {
- /* constexpr ImGuiTreeNodeFlags node_flags =
- ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_Leaf |
- ImGuiTreeNodeFlags_NoTreePushOnOpen ;*/
- std::set<uint64_t> allFsm;
- auto gameContext = context.__gameContextPtr;
- if (ImGui::TreeNode("World")) {
- ImGui::Columns(3);
- ImGui::Text("Name");
- ImGui::NextColumn();
- //ImGui::Text("Z-Layer"); ImGui::NextColumn();
- // ImGui::Text("R-Order"); ImGui::NextColumn();
- ImGui::Text("Object Type");
- ImGui::SameLine();
- ImGui::NextColumn();
- ImGui::Text("Visible");
- ImGui::NextColumn();
- ImGui::Separator();
- auto selected = mSelectorSystem->GetSelectedSet();
- int counter = 0;
- int firstSelected = 999999999;
- int lastSelected = 999999999;
- int endSelection = -1;
- ECS::Signature filter;
- bool selectPrevious = false;
- bool selectNext = false;
- auto ShowData = [this, &selected, &counter, &allFsm, &firstSelected, &endSelection, &lastSelected, /*&EntityListContextualMenu,*/ gameContext, &selectPrevious, &selectNext](
- ECS::Entity &ent, const std::string &name) {
- ImGui::PushID(counter);
- const bool isItemSelected = selected.find(ent.Index()) != selected.end();
- bool errorState = ent.HasComponent<ECS::Comp::TamChangeCorruption>();
- if (isItemSelected) {
- firstSelected = std::min(firstSelected, counter);
- lastSelected = counter;
- }
- // TODO: Esto serΓ‘ igual para las dos listas...
- if(ImGui::IsWindowFocused()) {
- if(ImGui::IsKeyPressed(Keyboard::TOP_ARROW, true)) {
- selectPrevious = true;
- }
- if(ImGui::IsKeyPressed(Keyboard::BOTTOM_ARROW, true)) {
- selectNext = true;
- }
- }
- if (ImGui::Selectable(name.c_str(), isItemSelected, ImGuiSelectableFlags_SpanAllColumns)) {
- if (ImGui::IsKeyDown(Key::LEFT_SHIFT)) {
- endSelection = counter;
- } else if (ImGui::IsKeyDown(Key::LEFT_CTRL)) {
- if (isItemSelected) {
- mSelectorSystem->RemoveSelected(ent);
- } else {
- mSelectorSystem->AddSelected(ent);
- }
- } else {
- mSelectorSystem->AddSelected(ent, true);
- }
- }
- struct EntityBatchDragAndDrop {
- ECS::Entity entities[1024];
- uint32_t entitiesCount = 0;
- };
- ImGui::SetItemAllowOverlap();
- if (ImGui::BeginDragDropTarget()) {
- if (const ImGuiPayload *payload = ImGui::AcceptDragDropPayload(ENTITY_BATCH_PAYLOAD)) {
- EntityBatchDragAndDrop acceptedPayload = *(EntityBatchDragAndDrop *) (payload->Data);
- if (acceptedPayload.entitiesCount > 0) {
- const auto destinationIndex = ent.Index();
- bool isMovementValid = true;
- for (auto idx = 0u; idx < acceptedPayload.entitiesCount; idx++) {
- auto &e = acceptedPayload.entities[idx];
- if (destinationIndex == e.Index()) {
- isMovementValid = false;
- }
- }
- if (isMovementValid) {
- if (ImGui::IsKeyDown(Key::LEFT_CTRL)) {
- for (auto idx = 0u; idx < acceptedPayload.entitiesCount; idx++) {
- auto &e = acceptedPayload.entities[idx];
- mSGSystem->MoveOver(::ECS::Index(e.Index()), destinationIndex);
- }
- } else {
- for (auto idx = acceptedPayload.entitiesCount; idx > 0; idx--) {
- auto &e = acceptedPayload.entities[idx - 1];
- mSGSystem->MoveUnder(::ECS::Index(e.Index()), destinationIndex);
- }
- }
- ent.GetComponentHandler<ECS::Comp::Transform>().ForceReplace();
- }
- }
- }
- ImGui::EndDragDropTarget();
- }
- ImGui::SetItemAllowOverlap();
- if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceAllowNullID)) {
- EntityBatchDragAndDrop ebdd;
- mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity e) {
- if (ebdd.entitiesCount < 1002) {
- ebdd.entities[ebdd.entitiesCount++] = e;
- } else {
- std::cerr << "[ERR] more than 1000 items moving at same time!!" << std::endl;
- }
- });
- ImGui::SetDragDropPayload(ENTITY_BATCH_PAYLOAD, &ebdd, sizeof(ebdd), ImGuiCond_Always);
- if (ebdd.entitiesCount == 1) {
- ImGui::Text("Moving node....%s", name.c_str());
- } else {
- std::stringstream ss;
- ImGui::Text("Moving %d entities.", ebdd.entitiesCount);
- }
- ImGui::EndDragDropSource();
- }
- ImGui::SetItemAllowOverlap();
- if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(Mouse::CLICK)) {
- }
- if (ImGui::BeginPopupContextWindow()) {
- // EntityListContextualMenu(counter);
- componentTag.ShowContextualMenu(selected, selectedForRemove);
- ImGui::EndPopup();
- }
- ImGui::NextColumn();
- //ImGui::Text("%d", c.z); ImGui::NextColumn();
- //ImGui::Text("%d", c.cheatOrder); ImGui::NextColumn();
- bool keep_line = false;
- const auto pre = ImGui::GetCursorPos();
- const auto CHAR_SIZE = ImGui::CalcTextSize("A");
- auto item_signature = ent.GetSignature();
- // const auto add_component_tag = [&](const ECS::Signature &signature, ImColor color, std::string_view text) {
- // if (ECS::Match(item_signature, signature)) {
- // if (keep_line) {
- // ImGui::SameLine();
- // }
- // ImGui::TextColored(color, "+%s", text.data());
- // keep_line = true;
- // }
- // };
- componentTag.RotTag(ent);
- componentTag.RotRefTag(ent);
- componentTag.RotBaseTag(ent);
- componentTag.SpriteTag(ent);
- componentTag.AnimationTag(ent);
- componentTag.AnimationPlayingTag(ent);
- componentTag.FontTag(ent);
- componentTag.NativeReelTag(ent);
- componentTag.SoundTag(ent);
- componentTag.LayeredFontTag(ent);
- if (ent.HasComponent<ECS::Comp::Video>()) {
- if (keep_line) {
- ImGui::SameLine();
- }
- const std::string VIDEO_TAG = "Video"; // TODO: Ver como ponemos esto....
- ImGui::TextColored(EddieColors().COLOR_VIDEO, "+%s", VIDEO_TAG.c_str());
- keep_line = true;
- }
- auto add_fsm = componentTag.FsmTag(ent);
- if (add_fsm) {
- ImGui::SameLine();
- const auto post = ImGui::GetCursorPos();
- const auto spacing = ImVec2(post.x - pre.x, CHAR_SIZE.y);
- ImGui::SetCursorPos(pre);
- ImGui::Dummy(spacing);
- auto fsm_obj = ent.GetComponentHandler<ECS::Comp::Fsm>();
- if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(Mouse::CLICK)) {
- gContext->EmitEvent<ECS::DirectSelectFsm>(fsm_obj->fsmCode);
- }
- if (isItemSelected) {
- allFsm.emplace(fsm_obj->fsmCode);
- }
- }
- ImGui::NextColumn();
- if (!errorState) {
- const auto cursor = ImGui::GetCursorPos();
- ImVec2 size;
- if (ent.HasComponent<ECS::Comp::OnScene>()) {
- size = ImGui::CalcTextSize("- Visible");
- ImGui::TextColored(EddieColors().COLOR_VISIBLE, "- Visible");
- } else {
- size = ImGui::CalcTextSize("- Invisible");
- ImGui::TextColored(EddieColors().COLOR_INVISIBLE, "- Invisible");
- }
- ImGui::SetCursorPos(cursor);
- ImGui::Dummy(size);
- if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
- if (ent.HasComponent<ECS::Comp::OnScene>()) {
- ent.Remove<ECS::Comp::OnScene>();
- } else {
- ent.AddComponent<ECS::Comp::OnScene>();
- }
- }
- ImGui::SameLine();
- componentTag.HideOnEditorTag(ent);
- } else {
- ImGui::TextColored(EddieColors().COLOR_MISSING_DATA, "MISSING TAM DATA ");
- ImGui::SameLine();
- }
- ImGui::NextColumn();
- ImGui::PopID();
- counter++;
- };
- auto rate = ImGui::GetIO().KeyRepeatRate;
- ImGui::GetIO().KeyRepeatRate = 0.04;
- mSGSystem->ForEachNamedTransform(filter, ShowData);
- ImGui::GetIO().KeyRepeatRate = rate;
- if(selectPrevious) {
- if(mSelectorSystem->CountRetainedEntities() > 0) {
- auto selected = mSelectorSystem->GetSelected();
- if(selected.size() == 1) {
- auto curr = selected.back();
- ECS::Entity prev;
- bool notFirstPrev = false;
- bool hasPrev = false;
- mSGSystem->ForEachNamedTransform(filter, [&]( ECS::Entity &ent, const std::string &name) {
- if(ent.Index() == curr.Index()) {
- hasPrev = true;
- }
- if(!hasPrev) {
- notFirstPrev = true;
- prev = ent;
- }
- });
- if(notFirstPrev) {
- mSelectorSystem->AddSelected(prev, true);
- }
- }
- }
- }
- if(selectNext) {
- if(mSelectorSystem->CountRetainedEntities() > 0) {
- auto selected = mSelectorSystem->GetSelected();
- if(selected.size() == 1) {
- auto curr = selected.back();
- ECS::Entity next;
- bool notLast = false;
- bool isThis = false;
- mSGSystem->ForEachNamedTransform(filter, [&]( ECS::Entity &ent, const std::string &name) {
- if(isThis) {
- notLast = true;
- next = ent;
- isThis = false;
- }
- if(ent.Index() == curr.Index()) {
- isThis = true;
- }
- });
- if(notLast) {
- mSelectorSystem->AddSelected(next, true);
- }
- }
- }
- }
- ImGui::Columns(1);
- ImGui::TreePop();
- if (endSelection > -1) {
- uint32_t first = 0;
- uint32_t second = 0;
- if (firstSelected < endSelection) {
- first = firstSelected;
- second = endSelection;
- } else if (endSelection < lastSelected) {
- first = endSelection;
- second = lastSelected;
- } else {
- first = endSelection;
- second = firstSelected;
- }
- auto data = mSGSystem->GetRetainedList(first, second - first + 1);
- bool is_p = true;
- for (auto &ent: data) {
- mSelectorSystem->AddSelected(ent, is_p);
- is_p = false;
- }
- }
- }
- if (ImGui::TreeNode("Out of world")) {
- ImGui::Text("Name");
- ImGui::NextColumn();
- ImGui::Separator();
- auto selected = mSelectorSystem->GetSelectedSet();
- const auto Iterator = [=](const std::string &name, ECS::Entity entity) {
- const bool isItemSelected = selected.find(entity.Index()) != selected.end();
- if (ImGui::Selectable(name.c_str(), isItemSelected, ImGuiSelectableFlags_SpanAllColumns)) {
- mSelectorSystem->DropSelecteds();
- // if(isItemSelected) {
- // mSelectorSystem->RemoveSelected(entity);
- // } else {
- mSelectorSystem->AddSelected(entity);
- // }
- }
- ImGui::NextColumn();
- };
- mRegisterSystem->IterateOverAllEntitiesNotMatching(Iterator, gContext->MakeSignature<ECS::Comp::Transform>());
- ImGui::TreePop();
- }
- if (ImGui::TreeNode("FSM")) {
- ImGui::Columns(5);
- ImGui::Text("ID");
- ImGui::NextColumn();
- ImGui::Text("States");
- ImGui::NextColumn();
- ImGui::Text("Enabled");
- ImGui::NextColumn();
- ImGui::Text("Finished");
- ImGui::NextColumn();
- ImGui::Text("CurrentState");
- ImGui::NextColumn();
- ImGui::Separator();
- Game::ContextIterateOverFsm(*gContext, [&](EddieFSM::FsmId id, EddieFSM::Fsm &fsm) {
- // ImGui::Text("%d", id); ImGui::NextColumn();
- if (ImGui::Selectable(std::to_string(id).data(), allFsm.find(id) != allFsm.end(),
- ImGuiSelectableFlags_SpanAllColumns)) {
- }
- if (ImGui::IsItemHovered()) {
- if (ImGui::IsMouseDoubleClicked(0)) {
- auto id = fsm.GetFsmIdentity();
- gContext->EmitEvent<ECS::DropSelection>();
- gContext->EmitEvent<ECS::DirectSelectFsm>(id);
- std::cout << "Selecting " << id << std::endl;
- allFsm.clear();
- allFsm.insert(id);
- }
- if (ImGui::IsKeyDown(Key::ALT)) {
- ImGui::BeginTooltip();
- ImGui::Text("%s", fsm.GetDescriptiveName().data());
- ImGui::EndTooltip();
- }
- }
- ImGui::NextColumn();
- ImGui::Text("%lu", fsm.States().size());
- ImGui::NextColumn();
- ImGui::Text("%s", fsm.IsEnabled() ? "True" : "False");
- ImGui::NextColumn();
- ImGui::Text("%s", fsm.IsFinished() ? "True" : "False");
- ImGui::NextColumn();
- if (fsm.GetCurrentState()) {
- ImGui::Text("%s", fsm.GetCurrentState()->GetStateName().data());
- } else {
- ImGui::Text("NO STATE");
- }
- ImGui::NextColumn();
- });
- ImGui::Columns(1);
- ImGui::TreePop();
- }
- //ImGui::TreePop();
- if(selectedForRemove) {
- ImGui::OpenPopup(REMOVE_FSM_WINDOW_ID);
- }
- RemoveFSMPopup();
- }
- void EddieUI::EntityList::RemoveFSMPopup() {
- if (ImGui::BeginPopupModal(REMOVE_FSM_WINDOW_ID, nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
- ImGui::Text("Are you sure you want to delete the FSM?\n\n");
- ImGui::Separator();
- if (ImGui::Button("NO", ImVec2(120, 0))) {
- selectedForRemove = false;
- ImGui::CloseCurrentPopup();
- }
- ImGui::SetItemDefaultFocus();
- ImGui::SameLine(0, 40);
- if (ImGui::Button("YES", ImVec2(120, 0))) {
- this->mSelectorSystem->ForEachSelected([](size_t idx, ECS::Entity &e) {
- e.Remove<ECS::Comp::Fsm>();
- });
- selectedForRemove = false;
- ImGui::CloseCurrentPopup();
- }
- ImGui::EndPopup();
- }
- }
- void EddieUI::EntityList::HandleEvent(const ECS::ResourceProvider<ECS::SpriteProvider> &event) {
- // this->spriteProvider = event.helper;
- componentTag.SetSpriteProvider(event.helper);
- }
- //EddieFSM::Fsm *EddieUI::EntityList::GetFsm(EddieFSM::FsmId id) {
- // if (this->gContext) {
- // return Game::ContextFindFsm(*gContext, id);
- // }
- // return nullptr;
- //}
- void EddieUI::EntityList::HandleEvent(const ECS::ResourceProvider<ECS::VideoProvider> &event) {
- // this->videoProvider = event.helper;
- componentTag.SetVideoProvider(event.helper);
- }
- void EddieUI::EntityList::SetGameContext(Game::Context &context) {
- this->gContext = &context;
- componentTag.SetGameContext(context);
- }
- void EddieUI::EntityList::ConfigureListeners(Game::Context &context) {
- context.Subscribe<ECS::ResourceProvider<ECS::SpriteProvider>>(this);
- context.Subscribe<ECS::ResourceProvider<ECS::VideoProvider> >(this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement