Advertisement
LxrdKxnny

FrostEdModule.cpp

May 9th, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.42 KB | None | 0 0
  1. #include "FrostEdModule.h"
  2. #include "Modules/ModuleManager.h"
  3. #include "Logging/LogMacros.h"
  4. #include "Windows/WindowsApplication.h"
  5. #include "D3D11Device.h"
  6.  
  7. #include "imgui.h"
  8.  
  9. #if PLATFORM_WINDOWS
  10.     #include "backends/imgui_impl_dx11.h"
  11.     #include "backends/imgui_impl_win32.h"
  12. #endif
  13.  
  14. #include <iostream>
  15.  
  16. FConsoleModule* FFrostEdModule::ConsoleModule = nullptr;
  17. FContentBrowserModule* FFrostEdModule::ContentBrowserModule = nullptr;
  18. FHierarchyModule* FFrostEdModule::HierarchyModule = nullptr;
  19. FPropertiesModule* FFrostEdModule::PropertiesModule = nullptr;
  20. FSceneInteractionModule* FFrostEdModule::SceneInteractionModule = nullptr;
  21.  
  22. ImVec4 ClearColor = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  23.  
  24. void FFrostEdModule::StartupModule()
  25. {
  26.     FROST_LOG_INFO("Initializing FrostEd...");
  27.  
  28.     FrostLogo = CreateScope<FD3D11Texture>("../../Content/Editor/About/FrostLogo.png");
  29.  
  30.     ConsoleModule = &FModuleManager::Get().LoadModule<FConsoleModule>("Console");
  31.     ContentBrowserModule = &FModuleManager::Get().LoadModule<FContentBrowserModule>("ContentBrowser");
  32.     HierarchyModule = &FModuleManager::Get().LoadModule<FHierarchyModule>("Hierarchy");
  33.     PropertiesModule = &FModuleManager::Get().LoadModule<FPropertiesModule>("Properties");
  34.     SceneInteractionModule = &FModuleManager::Get().LoadModule<FSceneInteractionModule>("SceneInteraction");
  35.  
  36.     IMGUI_CHECKVERSION();
  37.  
  38.     ImGui::CreateContext();
  39.  
  40.     ImGuiIO& IO = ImGui::GetIO(); (void)IO;
  41.     IO.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
  42.     IO.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;
  43.     IO.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
  44.     IO.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
  45.  
  46.     ImGui::StyleColorsDark();
  47.  
  48.     ImGuiStyle& Style = ImGui::GetStyle();
  49.     if (IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  50.     {
  51.         Style.WindowRounding = 0.0f;
  52.         Style.Colors[ImGuiCol_WindowBg].w = 1.0f;
  53.     }
  54.  
  55.     ImGui_ImplWin32_Init(FWindowsApplication::GetWindow()->GetHwnd());
  56.     ImGui_ImplDX11_Init(FD3D11Device::GetDevice()->GetID3D11Device(), FD3D11Device::GetDevice()->GetID3D11DeviceContext());
  57.  
  58.     ImFontConfig ConfigRegular;
  59.     ConfigRegular.OversampleH = 3;
  60.     ConfigRegular.OversampleV = 3;
  61.     ConfigRegular.PixelSnapH = true;
  62.  
  63.     ImFont* FontRegular = IO.Fonts->AddFontFromFileTTF("../../Content/ImGui/Fonts/Inter-Regular.ttf", 16.0f, &ConfigRegular);
  64.  
  65.     ImFontConfig ConfigMedium;
  66.     ConfigRegular.OversampleH = 3;
  67.     ConfigRegular.OversampleV = 3;
  68.     ConfigRegular.PixelSnapH = true;
  69.     ConfigRegular.MergeMode = true;
  70.  
  71.     ImFont* FontMedium = IO.Fonts->AddFontFromFileTTF("../../Content/ImGui/Fonts/Inter-Medium.ttf", 16.0f, &ConfigMedium);
  72.  
  73.     ImFontConfig ConfigSemiBold;
  74.     ConfigSemiBold.OversampleH = 3;
  75.     ConfigSemiBold.OversampleV = 3;
  76.     ConfigSemiBold.PixelSnapH = true;
  77.     ConfigSemiBold.MergeMode = true;
  78.  
  79.     ImFont* FontSemiBold = IO.Fonts->AddFontFromFileTTF("../../Content/ImGui/Fonts/Inter-SemiBold.ttf", 16.0f, &ConfigSemiBold);
  80.  
  81.     ImFontConfig ConfigBold;
  82.     ConfigBold.OversampleH = 3;
  83.     ConfigBold.OversampleV = 3;
  84.     ConfigBold.PixelSnapH = true;
  85.     ConfigBold.MergeMode = true;
  86.  
  87.     ImFont* FontBold = IO.Fonts->AddFontFromFileTTF("../../Content/ImGui/Fonts/Inter-Bold.ttf", 16.0f, &ConfigBold);
  88.  
  89.     ImGui::GetIO().FontDefault = FontRegular;
  90.  
  91.     FROST_LOG_INFO("Initialized FrostEd!");
  92. }
  93.  
  94. void FFrostEdModule::ShutdownModule()
  95. {
  96.     FModuleManager::Get().UnloadModule("SceneInteraction");
  97.     FModuleManager::Get().UnloadModule("Properties");
  98.     FModuleManager::Get().UnloadModule("Hierarchy");
  99.     FModuleManager::Get().UnloadModule("ContentBrowser");
  100.     FModuleManager::Get().UnloadModule("Console");
  101. }
  102.  
  103. void FFrostEdModule::Update()
  104. {
  105.     ImGui_ImplDX11_NewFrame();
  106.     ImGui_ImplWin32_NewFrame();
  107.     ImGui::NewFrame();
  108.  
  109.     ImVec4* Colors = ImGui::GetStyle().Colors;
  110.  
  111.     Colors[ImGuiCol_Text]                   = ImVec4(0.98f, 0.98f, 0.98f, 1.00f);
  112.     Colors[ImGuiCol_TextDisabled]           = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
  113.     Colors[ImGuiCol_WindowBg]               = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  114.     Colors[ImGuiCol_PopupBg]                = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  115.     Colors[ImGuiCol_Border]                 = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  116.     Colors[ImGuiCol_FrameBg]                = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  117.     Colors[ImGuiCol_FrameBgHovered]         = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  118.     Colors[ImGuiCol_FrameBgActive]          = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  119.     Colors[ImGuiCol_TitleBg]                = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  120.     Colors[ImGuiCol_TitleBgActive]          = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  121.     Colors[ImGuiCol_TitleBgCollapsed]       = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  122.     Colors[ImGuiCol_MenuBarBg]              = ImVec4(0.13f, 0.13f, 0.13f, 1.00f);
  123.     Colors[ImGuiCol_ScrollbarBg]            = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  124.     Colors[ImGuiCol_ScrollbarGrab]          = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  125.     Colors[ImGuiCol_ScrollbarGrabHovered]   = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  126.     Colors[ImGuiCol_ScrollbarGrabActive]    = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  127.     Colors[ImGuiCol_CheckMark]              = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
  128.     Colors[ImGuiCol_SliderGrab]             = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  129.     Colors[ImGuiCol_SliderGrabActive]       = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  130.     Colors[ImGuiCol_Button]                 = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  131.     Colors[ImGuiCol_ButtonHovered]          = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  132.     Colors[ImGuiCol_ButtonActive]           = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  133.     Colors[ImGuiCol_Header]                 = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  134.     Colors[ImGuiCol_HeaderHovered]          = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  135.     Colors[ImGuiCol_HeaderActive]           = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
  136.     Colors[ImGuiCol_Separator]              = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  137.     Colors[ImGuiCol_SeparatorHovered]       = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  138.     Colors[ImGuiCol_SeparatorActive]        = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
  139.     Colors[ImGuiCol_ResizeGrip]             = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  140.     Colors[ImGuiCol_ResizeGripHovered]      = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  141.     Colors[ImGuiCol_ResizeGripActive]       = ImVec4(0.45f, 0.45f, 0.45f, 1.00f);
  142.     Colors[ImGuiCol_Tab]                    = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  143.     Colors[ImGuiCol_TabHovered]             = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  144.     Colors[ImGuiCol_TabActive]              = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  145.     Colors[ImGuiCol_TabUnfocused]           = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  146.     Colors[ImGuiCol_TabUnfocusedActive]     = ImVec4(0.15f, 0.15f, 0.15f, 1.00f);
  147.     Colors[ImGuiCol_DockingPreview]         = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  148.     Colors[ImGuiCol_DockingEmptyBg]         = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  149.     Colors[ImGuiCol_PlotLines]              = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
  150.     Colors[ImGuiCol_PlotLinesHovered]       = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  151.     Colors[ImGuiCol_PlotHistogram]          = ImVec4(0.64f, 0.64f, 0.64f, 1.00f);
  152.     Colors[ImGuiCol_PlotHistogramHovered]   = ImVec4(0.90f, 0.90f, 0.90f, 1.00f);
  153.     Colors[ImGuiCol_TableHeaderBg]          = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  154.     Colors[ImGuiCol_TableBorderStrong]      = ImVec4(0.32f, 0.32f, 0.32f, 1.00f);
  155.     Colors[ImGuiCol_TableBorderLight]       = ImVec4(0.25f, 0.25f, 0.25f, 1.00f);
  156.     Colors[ImGuiCol_TableRowBgAlt]          = ImVec4(0.09f, 0.09f, 0.09f, 1.00f);
  157.     Colors[ImGuiCol_DragDropTarget]         = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
  158.  
  159.     ImGuiStyle& Style = ImGui::GetStyle();
  160.    
  161.     Style.WindowPadding                     = ImVec2(12.0f, 12.0f);
  162.     Style.FramePadding                      = ImVec2(12.0f, 6.0f);
  163.     Style.ItemSpacing                       = ImVec2(12.0f, 6.0f);
  164.     Style.ItemInnerSpacing                  = ImVec2(12.0f, 6.0f);
  165.     Style.IndentSpacing                     = 6.0f;
  166.     Style.ScrollbarSize                     = 12.0f;
  167.     Style.GrabMinSize                       = 12.0f;
  168.     Style.WindowRounding                    = 6.0f;
  169.     Style.FrameRounding                     = 4.0f;
  170.     Style.TabRounding                       = 6.0f;
  171.  
  172.     Style.WindowTitleAlign = ImVec2(0.5f, 0.5f);
  173.     Style.WindowMenuButtonPosition = ImGuiDir_Right;
  174.     Style.ColorButtonPosition = ImGuiDir_Left;
  175.     Style.SeparatorTextBorderSize = 2.0f;
  176.  
  177.     RenderWindowToolbar();
  178.  
  179.     static bool OptFullscreen = true;
  180.     static bool OptPadding = false;
  181.  
  182.     static ImGuiDockNodeFlags DockspaceFlags = ImGuiDockNodeFlags_None;
  183.  
  184.     ImGuiWindowFlags WindowFlags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoDocking;
  185.  
  186.     if (OptFullscreen)
  187.     {
  188.         const ImGuiViewport* Viewport = ImGui::GetMainViewport();
  189.  
  190.         ImGui::SetNextWindowPos(ImVec2(Viewport->WorkPos.x, Viewport->WorkPos.y + 50.0f));
  191.         ImGui::SetNextWindowSize(ImVec2(Viewport->WorkSize.x, Viewport->WorkSize.y - 50.0f));
  192.  
  193.         ImGui::SetNextWindowViewport(Viewport->ID);
  194.  
  195.         ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  196.         ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
  197.  
  198.         WindowFlags |= ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove;
  199.         WindowFlags |= ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;
  200.     }
  201.     else
  202.     {
  203.         DockspaceFlags &= ~ImGuiDockNodeFlags_PassthruCentralNode;
  204.     }
  205.  
  206.     if (DockspaceFlags & ImGuiDockNodeFlags_PassthruCentralNode)
  207.     {
  208.         WindowFlags |= ImGuiWindowFlags_NoBackground;
  209.     }
  210.  
  211.     if (!OptPadding)
  212.     {
  213.         ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
  214.     }
  215.  
  216.     ImGui::Begin("FrostEd", (bool*)true, WindowFlags);
  217.  
  218.     if (!OptPadding)
  219.     {
  220.         ImGui::PopStyleVar();
  221.     }
  222.  
  223.     if (OptFullscreen)
  224.     {
  225.         ImGui::PopStyleVar(2);
  226.     }
  227.  
  228.     ImGuiIO& IO = ImGui::GetIO();
  229.     if (IO.ConfigFlags & ImGuiConfigFlags_DockingEnable)
  230.     {
  231.         ImGuiID DockspaceID = ImGui::GetID("FrostEd");
  232.         ImGui::DockSpace(DockspaceID, ImVec2(0.0f, 0.0f), DockspaceFlags);
  233.     }
  234.  
  235.     ConsoleModule->RenderConsole();
  236.     ContentBrowserModule->RenderContentBrowser();
  237.     HierarchyModule->RenderHierarchy();
  238.     PropertiesModule->RenderProperties();
  239.     SceneInteractionModule->RenderSceneInteraction();
  240.  
  241.     ImGui::End();
  242.  
  243.     ImGui::Render();
  244.  
  245.     const float ClearColorWithAlpha[4] = { ClearColor.x * ClearColor.w, ClearColor.y * ClearColor.w, ClearColor.z * ClearColor.w, ClearColor.w };
  246.  
  247.     FD3D11Device::GetDevice()->GetID3D11DeviceContext()->OMSetRenderTargets(1, &FD3D11RenderTarget::GetRenderTarget()->GetID3D11RenderTargetView(), NULL);
  248.     FD3D11Device::GetDevice()->GetID3D11DeviceContext()->ClearRenderTargetView(FD3D11RenderTarget::GetRenderTarget()->GetID3D11RenderTargetView(), ClearColorWithAlpha);
  249.  
  250.     ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
  251.  
  252.     if (IO.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  253.     {
  254.         ImGui::UpdatePlatformWindows();
  255.         ImGui::RenderPlatformWindowsDefault();
  256.     }
  257. }
  258.  
  259. void FFrostEdModule::RenderWindowToolbar()
  260. {
  261.     ImGuiIO& IO = ImGui::GetIO();
  262.  
  263.     ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f));
  264.     ImGui::SetNextWindowSize(ImVec2(IO.DisplaySize.x, 50.0f));
  265.  
  266.     ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
  267.     ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.0f, 0.0f));
  268.     ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
  269.  
  270.     ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0.09f, 0.09f, 0.09f, 1.00f));
  271.  
  272.     ImGui::Begin("WindowToolbar", (bool*)true, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav);
  273.  
  274.     ImGui::SetCursorPos(ImVec2(5.0f, 5.0f));
  275.     ImGui::Image((void*)FrostLogo->GetShaderResourceView(), ImVec2(40, 40));
  276.  
  277.     ImGui::SetCursorPos(ImVec2(40 + 12.0f, 8.0f));
  278.  
  279.     ImGui::BeginGroup();
  280.  
  281.     // Draw toolbar buttons
  282.     // ...
  283.  
  284.     ImGui::EndGroup();
  285.  
  286.     ImGui::SetCursorPos(ImVec2(5.0f, 20.0f));
  287.  
  288.     if (ImGui::BeginMenuBar())
  289.     {
  290.         if (ImGui::BeginMenu("File"))
  291.         {
  292.             // Handle menu item click
  293.             // ...
  294.  
  295.             ImGui::EndMenu();
  296.         }
  297.  
  298.         if (ImGui::BeginMenu("Edit"))
  299.         {
  300.             // Handle menu item click
  301.             // ...
  302.  
  303.             ImGui::EndMenu();
  304.         }
  305.  
  306.         if (ImGui::BeginMenu("Window"))
  307.         {
  308.             // Handle menu item click
  309.             // ...
  310.  
  311.             ImGui::EndMenu();
  312.         }
  313.  
  314.         if (ImGui::BeginMenu("Tools"))
  315.         {
  316.             // Handle menu item click
  317.             // ...
  318.  
  319.             ImGui::EndMenu();
  320.         }
  321.  
  322.         if (ImGui::BeginMenu("Build"))
  323.         {
  324.             // Handle menu item click
  325.             // ...
  326.  
  327.             ImGui::EndMenu();
  328.         }
  329.  
  330.         if (ImGui::BeginMenu("Help"))
  331.         {
  332.             // Handle menu item click
  333.             // ...
  334.  
  335.             ImGui::EndMenu();
  336.         }
  337.  
  338.         ImGui::EndMenuBar();
  339.     }
  340.  
  341.     ImGui::SetCursorPos(ImVec2(5.0f, 55.0f));
  342.  
  343.     ImGui::Text("Hello, world!");
  344.  
  345.     ImGui::End();
  346.  
  347.     ImGui::PopStyleColor();
  348.     ImGui::PopStyleVar(3);
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement