Skip to content

Editor Layouts Management #431

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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Sources/Overload/OvEditor/include/OvEditor/Core/EditorActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ namespace OvEditor::Core
*/
void ResetLayout();

/**
* Save the editor layout to the given configuration file
* @param p_fileName
*/
void SaveLayout(const std::string& p_fileName);

/**
* Save the current editor layout to the last used configuration file
*/
void SaveCurrentLayout();

/**
* Set and load the editor layout from the given configuration file
* @param p_fileName
*/
void SetLayout(const std::string& p_fileName);

/**
* Defines the scene view camera speed
* @param p_speed
Expand Down
1 change: 1 addition & 0 deletions Sources/Overload/OvEditor/src/OvEditor/Core/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ OvEditor::Core::Context::Context(const std::string& p_projectPath, const std::st
ServiceLocator::Provide<OvAudio::Core::AudioPlayer>(*audioPlayer);
ServiceLocator::Provide<OvCore::Scripting::ScriptEngine>(*scriptEngine);
ServiceLocator::Provide<OvEditor::Utils::TextureRegistry>(*textureRegistry);
ServiceLocator::Provide<OvUI::Core::UIManager>(*uiManager);

ApplyProjectSettings();
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/Overload/OvEditor/src/OvEditor/Core/EditorActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,21 @@ void OvEditor::Core::EditorActions::ResetLayout()
DelayAction([this]() {m_context.uiManager->ResetLayout("Config\\layout.ini"); });
}

void OvEditor::Core::EditorActions::SaveLayout(const std::string& p_fileName)
{
DelayAction([&]() {m_context.uiManager->SaveLayout(std::ref(p_fileName)); });
}

void OvEditor::Core::EditorActions::SaveCurrentLayout()
{
DelayAction([&]() {m_context.uiManager->SaveCurrentLayout(); });
}

void OvEditor::Core::EditorActions::SetLayout(const std::string& p_fileName)
{
DelayAction([&]() {m_context.uiManager->SetLayout(std::ref(p_fileName)); });
}

void OvEditor::Core::EditorActions::SetSceneViewCameraSpeed(int p_speed)
{
EDITOR_PANEL(Panels::SceneView, "Scene View").GetCameraController().SetSpeed((float)p_speed);
Expand Down
89 changes: 87 additions & 2 deletions Sources/Overload/OvEditor/src/OvEditor/Panels/MenuBar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* @licence: MIT
*/

#include <filesystem>

#include <OvTools/Utils/SystemCalls.h>

#include <OvCore/ECS/Components/CCamera.h>
Expand All @@ -30,6 +32,7 @@
#include "OvEditor/Core/EditorActions.h"
#include "OvEditor/Settings/EditorSettings.h"
#include "OvEditor/Utils/ActorCreationMenu.h"
#include "OvUI/Plugins/ContextualMenu.h"

using namespace OvUI::Panels;
using namespace OvUI::Widgets;
Expand Down Expand Up @@ -197,8 +200,90 @@ void OvEditor::Panels::MenuBar::CreateSettingsMenu()

void OvEditor::Panels::MenuBar::CreateLayoutMenu()
{
auto& layoutMenu = CreateWidget<MenuList>("Layout");
layoutMenu.CreateWidget<MenuItem>("Reset").ClickedEvent += EDITOR_BIND(ResetLayout);
auto& layoutMenuList = CreateWidget<MenuList>("Layout");

auto& saveMenuItem = layoutMenuList.CreateWidget<MenuItem>("Save");
saveMenuItem.ClickedEvent += EDITOR_BIND(SaveCurrentLayout);

auto& saveNewMenuList = layoutMenuList.CreateWidget<MenuList>("Save New");
auto& layoutInputText = saveNewMenuList.CreateWidget<InputFields::InputText>("Layout Name");
layoutInputText.selectAllOnClick = true;
layoutInputText.EnterPressedEvent += [this](std::string p_input)
{
if (p_input.empty())
return;

auto& UIManager = OvCore::Global::ServiceLocator::Get<OvUI::Core::UIManager>();
std::string layoutsPath = UIManager.GetLayoutsPath();
EDITOR_EXEC(DelayAction(std::bind(&OvUI::Core::UIManager::SaveLayout, &UIManager, layoutsPath + p_input + ".ini"), 1));
};

auto& loadMenuList = layoutMenuList.CreateWidget<MenuList>("Load");

loadMenuList.ClickedEvent += [&]
{
loadMenuList.RemoveAllWidgets();

auto& UIManager = OvCore::Global::ServiceLocator::Get<OvUI::Core::UIManager>();
std::string layoutsPath = UIManager.GetLayoutsPath();

for (const auto& entry : std::filesystem::directory_iterator(layoutsPath))
{
if (entry.is_regular_file() && entry.path().extension() == ".ini")
{
std::shared_ptr<std::string> layoutFileName = std::make_shared<std::string>(entry.path().filename().string());

auto& layoutMenuItem = loadMenuList.CreateWidget<MenuItem>(*layoutFileName);
layoutMenuItem.name = OvTools::Utils::PathParser::GetFileWithoutExtension(*layoutFileName);

layoutMenuItem.ClickedEvent += [this, layoutsPath, layoutFileName]
{
auto& UIManager = OvCore::Global::ServiceLocator::Get<OvUI::Core::UIManager>();
std::string layoutsPath = UIManager.GetLayoutsPath();
EDITOR_EXEC(DelayAction(std::bind(&OvUI::Core::UIManager::SetLayout, &UIManager, layoutsPath + *layoutFileName), 1));
};

auto& contextualMenu = layoutMenuItem.AddPlugin<OvUI::Plugins::ContextualMenu>();
auto& deleteMenuItem = contextualMenu.CreateWidget<MenuItem>("Delete");

deleteMenuItem.ClickedEvent += [this, layoutFileName, &layoutMenuItem]
{
//EDITOR_EXEC(ResetToDefaultLayout);
auto& UIManager = OvCore::Global::ServiceLocator::Get<OvUI::Core::UIManager>();
std::string layoutsPath = UIManager.GetLayoutsPath();
EDITOR_EXEC(DelayAction(std::bind(&OvUI::Core::UIManager::DeleteLayout, &UIManager, layoutsPath + *layoutFileName), 1));
layoutMenuItem.enabled = false;

};
auto& renameToMenuList = contextualMenu.CreateWidget<MenuList>("Rename to...");

auto& renameInputText = renameToMenuList.CreateWidget<InputFields::InputText>("");
renameInputText.content = OvTools::Utils::PathParser::GetFileWithoutExtension(*layoutFileName);
renameInputText.selectAllOnClick = true;

renameInputText.EnterPressedEvent += [this, layoutFileName, &contextualMenu, &layoutMenuItem](std::string p_newName)
{
if (p_newName.empty())
return;

layoutMenuItem.name = p_newName;
//EDITOR_EXEC(ResetToDefaultLayout);

auto& UIManager = OvCore::Global::ServiceLocator::Get<OvUI::Core::UIManager>();
std::string layoutsPath = UIManager.GetLayoutsPath();

std::string oldFileName = layoutsPath + *layoutFileName;
std::string newFileName = layoutsPath + p_newName + ".ini";
EDITOR_EXEC(DelayAction(std::bind(&OvUI::Core::UIManager::RenameLayout, &UIManager, oldFileName, newFileName), 1));

*layoutFileName = p_newName + ".ini";
contextualMenu.Close();
};
}
}
};

layoutMenuList.CreateWidget<MenuItem>("Reset").ClickedEvent += EDITOR_BIND(ResetLayout);
}

void OvEditor::Panels::MenuBar::CreateHelpMenu()
Expand Down
6 changes: 6 additions & 0 deletions Sources/Overload/OvTools/include/OvTools/Utils/PathParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ namespace OvTools::Utils
*/
static std::string GetExtension(const std::string& p_path);

/**
* Returns the file without the extension
* @param p_file
*/
static std::string GetFileWithoutExtension(const std::string& p_file);

/**
* Convert the EFileType value to a string
* @param p_fileType
Expand Down
12 changes: 12 additions & 0 deletions Sources/Overload/OvTools/src/OvTools/Utils/PathParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ std::string OvTools::Utils::PathParser::GetExtension(const std::string & p_path)
return result;
}

std::string OvTools::Utils::PathParser::GetFileWithoutExtension(const std::string& p_file)
{
const size_t end_pos = p_file.find_last_of('.');

if (end_pos > 0 && end_pos != std::string::npos)
{
return p_file.substr(0, end_pos);
}

return p_file;
}

std::string OvTools::Utils::PathParser::FileTypeToString(EFileType p_fileType)
{
switch (p_fileType)
Expand Down
50 changes: 45 additions & 5 deletions Sources/Overload/OvUI/include/OvUI/Core/UIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,47 @@ namespace OvUI::Core
*/
void EnableDocking(bool p_value);

/**
* Reset the UI layout to the given configuration file
* @param p_config
*/
void ResetLayout(const std::string & p_config) const;
/**
* Reset the UI layout to the given configuration file
* @param p_config
*/
void ResetLayout(const std::string& p_config) const;

/**
* Load the UI layout from the given configuration file
* @param p_fileName
*/
void LoadLayout(const std::string& p_fileName);

/**
* Save the UI layout to the given configuration file
* @param p_fileName
*/
void SaveLayout(const std::string& p_fileName);

/**
* Save the current UI layout to the last used configuration file
*/
void SaveCurrentLayout();

/**
* Set and load the UI layout from the given configuration file
* @param p_fileName
*/
void SetLayout(const std::string& p_fileName);

/**
* Delete the UI layout configuration file
* @param p_fileName
*/
void DeleteLayout(const std::string& p_fileName);

/**
* Rename a UI layout configuration file
* @param p_fileName
* @param p_newFileName
*/
void RenameLayout(const std::string& p_fileName, const std::string& p_newFileName);

/**
* Return true if the docking system is enabled
Expand All @@ -124,6 +160,8 @@ namespace OvUI::Core
*/
void Render();

std::string GetLayoutsPath();

private:
void PushCurrentFont();
void PopCurrentFont();
Expand All @@ -133,5 +171,7 @@ namespace OvUI::Core
Modules::Canvas* m_currentCanvas = nullptr;
std::unordered_map<std::string, ImFont*> m_fonts;
std::string m_layoutSaveFilename = "imgui.ini";
const std::string m_defaultLayout;
const std::string m_layoutsPath;
};
}
59 changes: 57 additions & 2 deletions Sources/Overload/OvUI/src/OvUI/Core/UIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@

#include "OvUI/Core/UIManager.h"

OvUI::Core::UIManager::UIManager(GLFWwindow* p_glfwWindow, Styling::EStyle p_style, std::string_view p_glslVersion)
#include <filesystem>

#include "OvTools/Utils/SystemCalls.h"

OvUI::Core::UIManager::UIManager(GLFWwindow* p_glfwWindow, Styling::EStyle p_style, std::string_view p_glslVersion) :
m_defaultLayout("Config\\layout.ini"),
m_layoutsPath(OvTools::Utils::SystemCalls::GetPathToAppdata() + "\\OverloadTech\\OvEditor\\")
{
ImGui::CreateContext();

Expand Down Expand Up @@ -259,6 +265,50 @@ void OvUI::Core::UIManager::ResetLayout(const std::string& p_config) const
ImGui::LoadIniSettingsFromDisk(p_config.c_str());
}

void OvUI::Core::UIManager::LoadLayout(const std::string& p_fileName)
{
ImGui::LoadIniSettingsFromDisk(p_fileName.c_str());
}

void OvUI::Core::UIManager::SaveLayout(const std::string& p_fileName)
{
SetEditorLayoutSaveFilename(p_fileName);

ImGui::SaveIniSettingsToDisk(m_layoutSaveFilename.c_str());
}

void OvUI::Core::UIManager::SaveCurrentLayout()
{
if (!std::filesystem::exists(m_layoutSaveFilename))
{
m_layoutSaveFilename = m_layoutsPath + "layout.ini";
SetEditorLayoutSaveFilename(m_layoutSaveFilename);
}
ImGui::SaveIniSettingsToDisk(m_layoutSaveFilename.c_str());
}

void OvUI::Core::UIManager::SetLayout(const std::string& p_fileName)
{
SetEditorLayoutSaveFilename(p_fileName);

ImGui::LoadIniSettingsFromDisk(p_fileName.c_str());
}

void OvUI::Core::UIManager::DeleteLayout(const std::string& p_fileName)
{
std::filesystem::remove(p_fileName);
}

void OvUI::Core::UIManager::RenameLayout(const std::string& p_fileName, const std::string& p_newFileName)
{
std::filesystem::rename(p_fileName, p_newFileName);

if (m_layoutSaveFilename == p_fileName)
{
SetEditorLayoutSaveFilename(p_newFileName);
}
}

bool OvUI::Core::UIManager::IsDockingEnabled() const
{
return m_dockingState;
Expand All @@ -285,6 +335,11 @@ void OvUI::Core::UIManager::Render()
}
}

std::string OvUI::Core::UIManager::GetLayoutsPath()
{
return m_layoutsPath;
}

void OvUI::Core::UIManager::PushCurrentFont()
{

Expand All @@ -293,4 +348,4 @@ void OvUI::Core::UIManager::PushCurrentFont()
void OvUI::Core::UIManager::PopCurrentFont()
{

}
}