Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

99 add editor layout loadingsaving #253

Merged
merged 9 commits into from
Jul 4, 2022
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ add_subdirectory(${SRC_DIR}/Core/Editor/)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_ThemeManager)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_FontManager)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_UserProfileManager)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_LayoutManager)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_3DCursor)
add_subdirectory(${SRC_DIR}/Core/Editor/Utils/ERS_Editor_WindowManager)
add_subdirectory(${SRC_DIR}/Core/Editor/Windows)
Expand Down
5 changes: 5 additions & 0 deletions Source/Core/Editor/GUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ void GUISystem::UpdateGUI() {
ImGui::EndMainMenuBar();
}


size_t settings_size = 0;
const char* settings = ImGui::SaveIniSettingsToMemory(&settings_size);

// Updates all the windows, draws their content if enabled
WindowManager_->UpdateAllWindows();


}

void GUISystem::DeferredFrameUpdate() {
Expand Down
30 changes: 30 additions & 0 deletions Source/Core/Editor/Utils/ERS_Editor_LayoutManager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
########################################################################
# This file is part of the BrainGenix-ERS Environment Rendering System #
########################################################################


# Create Library (Name Should Be Parent Dir Name)
add_library(ERS_Editor_LayoutManager

# Add Source Files (.cpp)
"ERS_Editor_LayoutManager.cpp"

# Add Header Files (.h)
"ERS_Editor_LayoutManager.h"

${BACKWARD_ENABLE}
)
set_property(TARGET ERS_Editor_LayoutManager PROPERTY CXX_STANDARD 17)

# Link 3rd Party Libs
target_link_libraries(ERS_Editor_LayoutManager
yaml-cpp
IMGUI
)

# Link Internal Libs
target_link_libraries(ERS_Editor_LayoutManager
ERS_CLASS_LoggingSystem
)

target_include_directories(ERS_Editor_LayoutManager PUBLIC ./)
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
//======================================================================//
// This file is part of the BrainGenix-ERS Environment Rendering System //
//======================================================================//

#include <ERS_Editor_LayoutManager.h>
#include <filesystem>
#include <imgui.h>
#include <fstream>

ERS_CLASS_LayoutManager::ERS_CLASS_LayoutManager(ERS_CLASS_LoggingSystem* Logger, const char* LayoutDirectory) {

Logger_ = Logger;
LayoutDirectory_ = LayoutDirectory;
Logger_->Log("Initializing Layout Manager", 5);

}


ERS_CLASS_LayoutManager::~ERS_CLASS_LayoutManager() {

Logger_->Log("Layout Manager Destructor Called", 6);

}

void ERS_CLASS_LayoutManager::LoadLayouts() {

// Create List Of Files
for (const auto& Entry : std::filesystem::directory_iterator(std::string(LayoutDirectory_))) {

// Get File Path
std::string FilePath{ Entry.path().u8string() };

// Load YAML::Node
YAML::Node LayoutNode = YAML::LoadFile(FilePath.c_str());

// Build Temp Layout
ERS_STRUCT_EditorLayout Layout;
Layout.index = Index;

// Parse Out Display Name From File
std::string LayoutName;
LayoutName = LayoutNode["DisplayName"].as<std::string>();
Layout.name = LayoutName;

// Parse Out Ini String From File
std::string IniStr;
IniStr = LayoutNode["ImGuiIni"].as<std::string>();
Layout.IniString = IniStr;

// Add To Names and Layouts Vector
LayoutNames_.push_back(LayoutName);
Layouts_.push_back(Layout);

// Log Layout Indexed
Logger_->Log(std::string(std::string("Indexed Layout: ") + FilePath).c_str(), 1);

Index++;
}


Logger_->Log(std::string(std::string("Found ") + std::to_string(LayoutNames_.size()) + std::string(" Layouts")).c_str(), 1);

}

void ERS_CLASS_LayoutManager::SaveLayout(std::string LayoutName) {

// Save the Ini String
std::string IniStr;
size_t settings_size = 0;
IniStr = static_cast<std::string> (ImGui::SaveIniSettingsToMemory(&settings_size));

// Add To Names Vector
LayoutNames_.push_back(LayoutName);

// Construct the New Layout Struct
ERS_STRUCT_EditorLayout newLayout;
newLayout.index = Index++;
newLayout.name = LayoutName;
newLayout.IniString = IniStr;
Layouts_.push_back(newLayout);

// Save YAML Node
YAML::Node Layout;
Layout["ImGuiIni"] = IniStr;
Layout["DisplayName"] = LayoutName;

// Export the YAML string
YAML::Emitter LayoutYAML;
LayoutYAML << YAML::BeginMap;

// Set Constant Info
LayoutYAML << YAML::Key << "DisplayName" << YAML::Value << LayoutName;
LayoutYAML << YAML::Key << "ImGuiIni" << YAML::Value << IniStr;

// Stop Writing, Generate LayoutYAML
LayoutYAML << YAML::EndMap;
std::string YAMLstring = std::string(LayoutYAML.c_str());

// Write the string into a YAML file in the directory
std::ofstream file(std::string(LayoutDirectory_) + "/" + LayoutName + ".yaml");

if (!file.fail())
file << YAMLstring;

file.close();

}

void ERS_CLASS_LayoutManager::ApplyLayout(std::string LayoutName) {

int Index;
bool HasFoundLayout = false;
for (Index = 0; (long)Index < (long)LayoutNames_.size(); Index++) {
if (LayoutNames_[Index] == std::string(LayoutName)) {
HasFoundLayout = true;
break;
}
}

if (HasFoundLayout) {
ApplyLayout(Index);
}
else {
Logger_->Log("Failed To Find Layout, Skipping", 5);
}

}

void ERS_CLASS_LayoutManager::ApplyLayout(int LayoutID) {

// Get Layout Name
std::string LayoutName = LayoutNames_[LayoutID];
ERS_STRUCT_EditorLayout Layout = Layouts_[LayoutID];

Logger_->Log(std::string(std::string("Applying Layout: ") + LayoutName).c_str(), 4);

ImGui::LoadIniSettingsFromMemory(Layout.IniString.c_str());

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//======================================================================//
// This file is part of the BrainGenix-ERS Environment Rendering System //
//======================================================================//

#pragma once


// Standard Libraries (BG convention: use <> instead of "")
#include <string>

// Third-Party Libraries (BG convention: use <> instead of "")
#include <yaml-cpp/yaml.h>

// Internal Libraries (BG convention: use <> instead of "")
#include <ERS_CLASS_LoggingSystem.h>

struct ERS_STRUCT_EditorLayout {
int index;
std::string name;
std::string IniString;
};

/**
* @brief Creates the user profile manager class.
*
*/
class ERS_CLASS_LayoutManager {


private:

ERS_CLASS_LoggingSystem* Logger_; /**<ERS_CLASS_LoggingSystem Instance Pointer*/
std::string LayoutDirectory_; /**<This string stores the path to the editor's layout directory ending in a trailing slash*/

std::vector<YAML::Node> LayoutFiles_; /**<List Of YAML::Node files*/
int Index = 0; /**<The index of Layout structs*/

public:

std::vector<std::string> LayoutNames_; /**<List of layout display names (based on display name entry in YAML file)*/
std::vector<ERS_STRUCT_EditorLayout> Layouts_; /**<List of Layout structs that store the layouts/

/**
* @brief Construct a new Layout Manager object
*
* @param Logger
*/
ERS_CLASS_LayoutManager(ERS_CLASS_LoggingSystem* Logger, const char* LayoutDirectory = "EditorAssets/Layouts/");

/**
* @brief Destroy the Layout Manager object
*
*/
~ERS_CLASS_LayoutManager();

/**
* @brief Save the current layout the user is using.
* It will return the set YAML string for the layout
*/
void SaveLayout(std::string LayoutName);

/**
* @brief Load layouts from disk.
*/
void LoadLayouts();

/**
* @brief Apply the selected layout.
*
* @param LayoutID
*/
void ApplyLayout(int LayoutID);

/**
* @brief Apply the selected layout.
*
* @param LayoutName
*/
void ApplyLayout(std::string LayoutName);
};
4 changes: 2 additions & 2 deletions Source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ int main() {


// Log Logo Text
SystemUtils->Logger_->Log("Starting BrainGenix-ERS Instance", 2);
/*SystemUtils->Logger_->Log("Starting BrainGenix-ERS Instance", 2);
SystemUtils->Logger_->Log("", 5);
SystemUtils->Logger_->Log("---------------------------------------------------------------------------", 5);
SystemUtils->Logger_->Log("\x1b[38;2;0;128;55m██████╗ ██████╗ █████╗ ██╗███╗ ██╗\x1b[38;2;130;68;208m ██████╗ ███████╗███╗ ██╗██╗██╗ ██╗", 5);
Expand All @@ -170,7 +170,7 @@ int main() {
SystemUtils->Logger_->Log(" +-----------------------------------------------------------------+", 4);
SystemUtils->Logger_->Log(" | BrainGenix-ERS Real-Time Environment Rendering System |", 4);
SystemUtils->Logger_->Log(" +-----------------------------------------------------------------+", 4);
SystemUtils->Logger_->Log("", 4);
SystemUtils->Logger_->Log("", 4);*/


// Initialize Times
Expand Down