Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.31 KB | None | 0 0
  1. //
  2. // Created by icano on 20/1/20.
  3. //
  4.  
  5. #include <EddieEddieUI/CustomColors.hpp>
  6. #include "UiEntityListComponentTag.hpp"
  7. #include <EddieECS/Components/SoundComponent.hpp>
  8. #include <EddieECS/Components/Video.hpp>
  9. #include <EddieECS/Components/FSM.hpp>
  10. #include <EddieECS/Components/RelativeRotationPreservation.hpp>
  11. #include <EddieECS/Components/Animation.hpp>
  12. #include <EddieECS/Components/AnimationPlay.hpp>
  13. #include <EddieECS/Components/Text.hpp>
  14. #include <EddieECS/Components/NativeReel.hpp>
  15. #include <EddieECS/Components/IgnoreOnEditor.hpp>
  16. #include <EddieECS/Components/Sprite.hpp>
  17. #include <EddieEngine/GameContext/ContextOperations.hpp>
  18. #include <EddieECS/EcsBaseUtils.hpp>
  19.  
  20.  
  21. namespace {
  22.     constexpr auto ROTATOR_TAG = "Rot";
  23.     constexpr auto ROTATOR_REF_TAG = "RotRef";
  24.     constexpr auto ROTATOR_BASE_TAG = "RotBase";
  25.     constexpr auto SPRITE_TAG = "Sprite";
  26.     constexpr auto ANIMATION_TAG = "Animation";
  27.     constexpr auto ANIMATION_PLAY_TAG = "Playing";
  28.     constexpr auto TEXT_TAG = "DEPRECTAED-TEXT";
  29.     constexpr auto REEL_TAG = "NativeReel";
  30.     constexpr auto SOUND_TAG = "Sound";
  31.     constexpr auto ML_TEXT_TAG = "LayeredDynamicText";
  32.     constexpr auto VIDEO_TAG = "Video";
  33.     constexpr auto FSM_TAG = "Fsm";
  34.     constexpr auto HIDE_ON_EDITOR_TAG = "HideOnEditor";
  35. }
  36.  
  37. void UiEntityListComponentTag::GenericTag(const ECS::Entity& entity, const ECS::Signature& signature, ImColor color, std::string_view text) {
  38.     if (gContext) {
  39.         if (ECS::Match(entity.GetSignature(), signature)) {
  40.             if (keep_line) {
  41.                 ImGui::SameLine();
  42.             }
  43.             ImGui::TextColored(color, "+%s", text.data());
  44.             keep_line = true;
  45.         }
  46.     }
  47. }
  48.  
  49. void UiEntityListComponentTag::RotTag(const ECS::Entity &entity) {
  50.     GenericTag(entity, ROT_SIGNATURE, EddieColors().COLOR_ROTATE, ROTATOR_TAG);
  51. }
  52.  
  53. void UiEntityListComponentTag::RotRefTag(const ECS::Entity &entity) {
  54.     GenericTag(entity, ROT_REF_SIGNATURE, EddieColors().COLOR_ROTATE_REF, ROTATOR_REF_TAG);
  55. }
  56.  
  57. void UiEntityListComponentTag::RotBaseTag(const ECS::Entity &entity) {
  58.     GenericTag(entity, ROT_BASE_SIGNATURE, EddieColors().COLOR_ROTATE_BASE, ROTATOR_BASE_TAG);
  59. }
  60.  
  61. void UiEntityListComponentTag::SpriteTag(const ECS::Entity &entity) {
  62.     GenericTag(entity, SPRITE_SIGNATURE, EddieColors().COLOR_SPRITE, SPRITE_TAG);
  63. }
  64.  
  65. void UiEntityListComponentTag::AnimationTag(const ECS::Entity &entity) {
  66.     GenericTag(entity, ANIMATION_SIGNATURE, EddieColors().COLOR_ANIMATION, ANIMATION_TAG);
  67. }
  68.  
  69. void UiEntityListComponentTag::AnimationPlayingTag(const ECS::Entity &entity) {
  70.     GenericTag(entity, ANIMATION_PLAYING_SIGNATURE, EddieColors().COLOR_ANIMATION_PLAYING, ANIMATION_PLAY_TAG);
  71. }
  72.  
  73. void UiEntityListComponentTag::FontTag(const ECS::Entity &entity) {
  74.     GenericTag(entity, FONT_SIGNATURE, EddieColors().COLOR_FONT, TEXT_TAG);
  75. }
  76.  
  77. void UiEntityListComponentTag::NativeReelTag(const ECS::Entity &entity) {
  78.     GenericTag(entity, NATIVE_REEL_SIGNATURE, EddieColors().COLOR_NATIVE_REEL, REEL_TAG);
  79. }
  80.  
  81. void UiEntityListComponentTag::LayeredFontTag(const ECS::Entity &entity) {
  82.     GenericTag(entity, LAYERED_FONT_SIGNATURE, EddieColors().COLOR_FONT, ML_TEXT_TAG);
  83. }
  84.  
  85. void UiEntityListComponentTag::HideOnEditorTag(const ECS::Entity &entity) {
  86.     GenericTag(entity, HIDE_ON_EDITOR_SIGNATURE, EddieColors().COLOR_MISSING_DATA, HIDE_ON_EDITOR_TAG);
  87. }
  88.  
  89. void UiEntityListComponentTag::SoundTag(const ECS::Entity& entity) {
  90.     if (gContext) {
  91.         if (ECS::Match(entity.GetSignature(), SOUND_SIGNATURE)) {
  92.             if (keep_line) {
  93.                 ImGui::SameLine();
  94.             }
  95.             const auto is_play = entity.GetComponentHandler<ECS::Comp::SoundComponent>()->mSoundHandler->IsPlayingOnTime();
  96.             if (is_play) {
  97.                 ImGui::TextColored(EddieColors().COLOR_SOUND_COMPONENT, "+%s(Playing...)", SOUND_TAG);
  98.             } else {
  99.                 ImGui::TextColored(EddieColors().COLOR_SOUND_COMPONENT, "+%s", SOUND_TAG);
  100.             }
  101.             keep_line = true;
  102.         }
  103.     }
  104. }
  105.  
  106. bool UiEntityListComponentTag::FsmTag(const ECS::Entity& entity) {
  107.     if (gContext) {
  108.         if (entity.HasComponent<ECS::Comp::Video>()) {
  109.             if (keep_line) {
  110.                 ImGui::SameLine();
  111.             }
  112.             ImGui::TextColored(EddieColors().COLOR_VIDEO, "+%s", VIDEO_TAG);
  113.             keep_line = true;
  114.         }
  115.  
  116.         if (ECS::Match(entity.GetSignature(), FSM_SIGNATURE)) {
  117.  
  118.             if (keep_line) {
  119.                 ImGui::SameLine();
  120.             }
  121.             auto e = entity.GetComponentHandler<ECS::Comp::Fsm>()->fsmCode;
  122.             auto p_fsm = gContext->GetInternalEcs().FSM().GetFsm(e);
  123.             std::string stateName = "NoFsmState";
  124.             if (p_fsm) {
  125.                 auto state = p_fsm->GetCurrentState();
  126.                 if (state) {
  127.                     stateName = state->GetStateName();
  128.                 }
  129.                 ImGui::TextColored(EddieColors().COLOR_FSM, "+%s(%s)", FSM_TAG, stateName.data());
  130.  
  131.             } else {
  132.                 stateName = "NO_FSM_LOADED_BUT_ASSIGNED";
  133.                 ImGui::TextColored(EddieColors().COLOR_MISSING_DATA, "+%s(%s)", FSM_TAG, stateName.data());
  134.             }
  135.             keep_line = true;
  136.             return true;
  137.         }
  138.     }
  139.  
  140.     return false;
  141. }
  142.  
  143. EddieFSM::Fsm *UiEntityListComponentTag::GetFsm(EddieFSM::FsmId id) {
  144.     if (this->gContext) {
  145.         return Game::ContextFindFsm(*gContext, id);
  146.     }
  147.     return nullptr;
  148. }
  149.  
  150. void UiEntityListComponentTag::ShowContextualMenu(const std::set<U32>& selected, bool& selectedForRemove) {
  151.     //ImGui::Text("1: %s", ANIMATION_AND_PLAYING_SIGNATURE.to_string().data());
  152.     // ImGui::Text("2: %s", ANIMATION_SIGNATURE.to_string().data());
  153.     //::Text("3: %s", ANIMATION_PLAYING_SIGNATURE.to_string().data());
  154.     mSelectorSystem->ComputeSignature();
  155.     auto signature = mSelectorSystem->GetSelectionCommonSignature();
  156.     auto any_signature = mSelectorSystem->GetSelectionAnySignature();
  157.  
  158.     if (ImGui::MenuItem("Create Entity")) {
  159.         Game::CreateEntity(*gContext, "EmptyEntity");
  160.     }
  161.  
  162.     //ImGui::Text("R: %s", signature.to_string().data());
  163.     if (ImGui::MenuItem("Restore TAM Position")) {
  164.         mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  165.             EddieECS::RestoreTamPosition(*spriteProvider, *videoProvider, e);
  166.         });
  167.     }
  168.  
  169.     if (selected.size() == 1) {
  170.         if (ECS::Match(signature, FSM_SIGNATURE)) {
  171.             if (ImGui::MenuItem("Capture FSM")) {
  172.                 //some thing
  173.  
  174.                 auto CaptureOperation = [&](size_t idx, ECS::Entity &e) {
  175.                     auto fsm_cmp = e.GetComponentHandler<ECS::Comp::Fsm>();
  176.                     auto fsm = GetFsm(fsm_cmp->fsmCode);
  177.                     if (fsm) {
  178.                         mTemporalCapturedFsm.reset();
  179.                         mTemporalCapturedFsm = data::json();
  180.                         fsm->ToJson(*mTemporalCapturedFsm);
  181.                         std::cout << mTemporalCapturedFsm->dump(4) << std::endl;
  182.                     }
  183.                 };
  184.                 mSelectorSystem->ForEachSelected(CaptureOperation);
  185.             }
  186.         }
  187.     }
  188.  
  189.  
  190.     if (!ECS::Match(any_signature, FSM_SIGNATURE)) {
  191.         if (ImGui::MenuItem("Put captured FSM", nullptr, false, mTemporalCapturedFsm.has_value())) {
  192.  
  193.             auto &data = mTemporalCapturedFsm->front();
  194.             auto PutCaptureOperation = [&](size_t idx, ECS::Entity &e) {
  195.                 auto fsm = Game::ContextCreateNewFsm(*gContext);
  196.                 const auto id = fsm->GetFsmIdentity();
  197.                 //       std::cout << "Created fsm " << id << std::endl;
  198.                 Game::ContextRestoreFsmFromJson(*gContext, fsm, data);
  199.                 //     std::cout << "Loaded in " << fsm->GetFsmIdentity() << std::endl;
  200.                 fsm->PrepareAllStates();
  201.                 ECS::Comp::Fsm fsmComponent{};
  202.                 fsmComponent.fsmCode = id;
  203.  
  204.                 fsm->SetDescriptiveName("COPIED FSM " + fsm->GetDescriptiveName());
  205.  
  206.                 fsm->mOwnership = e.entityId;
  207.                 e.AddComponent<ECS::Comp::Fsm>(fsmComponent);
  208.             };
  209.             mSelectorSystem->ForEachSelected(PutCaptureOperation);
  210.         }
  211.     }
  212.  
  213.     if (ECS::Match(signature, FSM_SIGNATURE)) {
  214.         if (ImGui::MenuItem("Remove FSM")) {
  215.             selectedForRemove = true;
  216.         }
  217.     } else {
  218.         if (ImGui::MenuItem("Add FSM")) {
  219.             mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  220.                 if (!e.HasComponent<ECS::Comp::Fsm>()) {
  221.                     auto fsm = Game::ContextCreateNewFsm(*gContext);
  222.                     fsm->mOwnership = e.entityId;
  223.                     ECS::Comp::Fsm fsmComponent{fsm->GetFsmIdentity()};
  224.                     e.AddComponent<ECS::Comp::Fsm>(fsmComponent);
  225.                 }
  226.             });
  227.         }
  228.     }
  229.  
  230.     if (ECS::Match(any_signature, VISIBLE_SIGNATURE)) {
  231.         if (ImGui::MenuItem("Make Invisible")) {
  232.             mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  233.                 if (e.HasComponent<ECS::Comp::OnScene>()) {
  234.                     e.Remove<ECS::Comp::OnScene>();
  235.                 }
  236.             });
  237.         }
  238.     }
  239.  
  240.     if (!ECS::Match(signature, VISIBLE_SIGNATURE)) {
  241.         if (ImGui::MenuItem("Make Visible")) {
  242.             mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  243.                 if (!e.HasComponent<ECS::Comp::OnScene>()) {
  244.                     e.AddComponent<ECS::Comp::OnScene>();
  245.                 }
  246.             });
  247.         }
  248.     }
  249.  
  250.     ImGui::Separator();
  251.     if (ImGui::MenuItem("Hide on editor")) {
  252.         mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  253.             if (!e.HasComponent<ECS::Comp::IgnoreOnEditor>()) {
  254.                 e.AddComponent<ECS::Comp::IgnoreOnEditor>();
  255.             }
  256.         });
  257.     }
  258.  
  259.     if (ImGui::MenuItem("Show on editor")) {
  260.         mSelectorSystem->ForEachSelected([&](size_t idx, ECS::Entity &e) {
  261.             if (e.HasComponent<ECS::Comp::IgnoreOnEditor>()) {
  262.                 e.Remove<ECS::Comp::IgnoreOnEditor>();
  263.             }
  264.         });
  265.     }
  266.  
  267.     if (ImGui::BeginMenu("Add to entity vector")) {
  268.         auto names = gContext->GetInternalVariableManager().GetTypedVariablesName<std::vector<ECS::Entity>>();
  269.  
  270.         for (auto &n: names) {
  271.             if (ImGui::MenuItem(n.data())) {
  272.                 auto collection = gContext->GetInternalVariableManager().GetVariablePtr<std::vector<ECS::Entity>>(n);
  273.                 mSelectorSystem->ForEachSelected(
  274.                         [&](size_t idx, ECS::Entity &e) {
  275.                             collection->emplace_back(e);
  276.                         });
  277.             }
  278.         }
  279.         ImGui::EndMenu();
  280.     }
  281.  
  282.     if (ECS::Match(signature, ANIMATION_AND_PLAYING_SIGNATURE)) {
  283.         if (ImGui::MenuItem("Remove Animation Play")) {
  284.             mSelectorSystem->ForEachSelected([&](size_t, ECS::Entity &e) {
  285.                 if (e.HasComponent<ECS::Comp::AnimationPlay>()) {
  286.                     e.Remove<ECS::Comp::AnimationPlay>();
  287.                 }
  288.             });
  289.         }
  290.     }
  291.  
  292.     if (ECS::Match(signature, ANIMATION_SIGNATURE) &&
  293.         !ECS::Match(any_signature, ANIMATION_PLAYING_SIGNATURE)) {
  294.         if (ImGui::MenuItem("Add default animation play")) {
  295.             mSelectorSystem->ForEachSelected([&](size_t, ECS::Entity &e) {
  296.                 if (!e.HasComponent<ECS::Comp::AnimationPlay>()) {
  297.                     ECS::Comp::AnimationPlay play;
  298.                     e.AddComponent<ECS::Comp::AnimationPlay>(play);
  299.                 }
  300.             });
  301.         }
  302.     }
  303. }
  304.  
  305. void UiEntityListComponentTag::SetGameContext(Game::Context &context) {
  306.     this->gContext = &context;
  307.  
  308.     ROT_SIGNATURE = gContext->MakeSignature<ECS::Comp::RelativeRotationPreservation>();
  309.     ROT_REF_SIGNATURE = gContext->MakeSignature<ECS::Comp::RelativeRotationReference>();
  310.     ROT_BASE_SIGNATURE = gContext->MakeSignature<ECS::Comp::RelativeRotationBase>();
  311.     SPRITE_SIGNATURE = gContext->MakeSignature<ECS::Comp::Sprite>();
  312.     ANIMATION_SIGNATURE = gContext->MakeSignature<ECS::Comp::Animation>();
  313.     ANIMATION_PLAYING_SIGNATURE = gContext->MakeSignature<ECS::Comp::AnimationPlay>();
  314.     FONT_SIGNATURE = gContext->MakeSignature<ECS::Comp::VeryLargeText2D_Deprectaed>();
  315.     NATIVE_REEL_SIGNATURE = gContext->MakeSignature<ECS::Comp::NativeReel>();
  316.     SOUND_SIGNATURE = gContext->MakeSignature<ECS::Comp::SoundComponent>();
  317.     LAYERED_FONT_SIGNATURE = gContext->MakeSignature<ECS::Comp::MultiLayerVeryLargeText2D>();
  318.     FSM_SIGNATURE = gContext->MakeSignature<ECS::Comp::Fsm>();
  319.     HIDE_ON_EDITOR_SIGNATURE = gContext->MakeSignature<ECS::Comp::IgnoreOnEditor>();
  320.     VISIBLE_SIGNATURE = gContext->MakeSignature<ECS::Comp::OnScene>();
  321.     ANIMATION_AND_PLAYING_SIGNATURE = gContext->MakeSignature<ECS::Comp::Animation, ECS::Comp::AnimationPlay>();
  322. }
  323.  
  324. void UiEntityListComponentTag::SetEditorSelector(const std::shared_ptr<ECS::EditorSelector>& selector) {
  325.     this->mSelectorSystem = selector;
  326. }
  327.  
  328. void UiEntityListComponentTag::SetSpriteProvider(const std::shared_ptr<ECS::SpriteProvider> &provider) {
  329.     this->spriteProvider = provider;
  330. }
  331.  
  332. void UiEntityListComponentTag::SetVideoProvider(const std::shared_ptr<ECS::VideoProvider> &provider) {
  333.     this->videoProvider = provider;
  334. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement