Refactor editor UI architecture
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
#include "Actions/EditorActions.h"
|
||||
#include "Commands/ProjectCommands.h"
|
||||
#include "ProjectPanel.h"
|
||||
#include "Core/IEditorContext.h"
|
||||
#include "Core/IProjectManager.h"
|
||||
#include "Core/ISceneManager.h"
|
||||
#include "Core/IUndoManager.h"
|
||||
#include "Core/ISelectionManager.h"
|
||||
#include "Core/AssetItem.h"
|
||||
#include "UI/Core.h"
|
||||
#include "UI/PanelChrome.h"
|
||||
#include "Utils/SceneEditorUtils.h"
|
||||
#include "UI/UI.h"
|
||||
#include <imgui.h>
|
||||
#include <imgui_internal.h>
|
||||
|
||||
@@ -16,31 +13,6 @@ namespace Editor {
|
||||
|
||||
const char* DRAG_DROP_TYPE = "ASSET_ITEM";
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr float kProjectToolbarHeight = 60.0f;
|
||||
|
||||
void DrawFolderIcon(ImDrawList* drawList, const ImVec2& min, const ImVec2& max, ImU32 fillColor, ImU32 lineColor) {
|
||||
const float width = max.x - min.x;
|
||||
const float height = max.y - min.y;
|
||||
const ImVec2 tabMax(min.x + width * 0.45f, min.y + height * 0.35f);
|
||||
drawList->AddRectFilled(ImVec2(min.x, min.y + height * 0.14f), tabMax, fillColor, 2.0f);
|
||||
drawList->AddRectFilled(ImVec2(min.x, min.y + height * 0.28f), max, fillColor, 2.0f);
|
||||
drawList->AddRect(ImVec2(min.x, min.y + height * 0.14f), tabMax, lineColor, 2.0f);
|
||||
drawList->AddRect(ImVec2(min.x, min.y + height * 0.28f), max, lineColor, 2.0f);
|
||||
}
|
||||
|
||||
void DrawFileIcon(ImDrawList* drawList, const ImVec2& min, const ImVec2& max, ImU32 fillColor, ImU32 lineColor) {
|
||||
const ImVec2 foldA(max.x - 8.0f, min.y);
|
||||
const ImVec2 foldB(max.x, min.y + 8.0f);
|
||||
drawList->AddRectFilled(min, max, fillColor, 2.0f);
|
||||
drawList->AddRect(min, max, lineColor, 2.0f);
|
||||
drawList->AddTriangleFilled(foldA, ImVec2(max.x, min.y), foldB, IM_COL32(235, 235, 235, 40));
|
||||
drawList->AddLine(foldA, foldB, lineColor);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
ProjectPanel::ProjectPanel() : Panel("Project") {
|
||||
}
|
||||
|
||||
@@ -63,46 +35,24 @@ void ProjectPanel::Render() {
|
||||
|
||||
auto& manager = m_context->GetProjectManager();
|
||||
|
||||
UI::PanelToolbarScope toolbar("ProjectToolbar", kProjectToolbarHeight);
|
||||
UI::PanelToolbarScope toolbar("ProjectToolbar", UI::ProjectPanelToolbarHeight());
|
||||
if (toolbar.IsOpen()) {
|
||||
|
||||
bool canGoBack = manager.CanNavigateBack();
|
||||
ImGui::BeginDisabled(!canGoBack);
|
||||
if (UI::ToolbarButton("<", false, ImVec2(28.0f, 0.0f))) {
|
||||
if (canGoBack) {
|
||||
manager.NavigateBack();
|
||||
}
|
||||
if (Actions::DrawToolbarAction(Actions::MakeNavigateBackAction(canGoBack), UI::ProjectBackButtonSize())) {
|
||||
manager.NavigateBack();
|
||||
}
|
||||
ImGui::EndDisabled();
|
||||
ImGui::SameLine();
|
||||
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.0f, 0.0f, 0.0f, 0.0f));
|
||||
size_t pathDepth = manager.GetPathDepth();
|
||||
if (pathDepth == 0) {
|
||||
ImGui::TextUnformatted("Assets");
|
||||
} else {
|
||||
for (size_t i = 0; i < pathDepth; i++) {
|
||||
if (i > 0) {
|
||||
ImGui::SameLine();
|
||||
ImGui::TextDisabled("/");
|
||||
ImGui::SameLine();
|
||||
}
|
||||
std::string name = manager.GetPathName(i);
|
||||
if (i < pathDepth - 1) {
|
||||
if (ImGui::Button(name.c_str())) {
|
||||
manager.NavigateToIndex(i);
|
||||
}
|
||||
} else {
|
||||
ImGui::Text("%s", name.c_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::PopStyleColor(2);
|
||||
UI::DrawToolbarBreadcrumbs(
|
||||
"Assets",
|
||||
pathDepth,
|
||||
[&](size_t index) { return manager.GetPathName(index); },
|
||||
[&](size_t index) { manager.NavigateToIndex(index); });
|
||||
|
||||
ImGui::Dummy(ImVec2(0.0f, 2.0f));
|
||||
ImGui::SetNextItemWidth(-1.0f);
|
||||
ImGui::InputTextWithHint("##Search", "Search assets", m_searchBuffer, sizeof(m_searchBuffer));
|
||||
UI::DrawToolbarRowGap();
|
||||
UI::ToolbarSearchField("##Search", "Search assets", m_searchBuffer, sizeof(m_searchBuffer));
|
||||
}
|
||||
|
||||
UI::PanelContentScope content(
|
||||
@@ -110,15 +60,15 @@ void ProjectPanel::Render() {
|
||||
UI::AssetPanelContentPadding(),
|
||||
ImGuiWindowFlags_None,
|
||||
true,
|
||||
ImVec2(8.0f, 10.0f));
|
||||
UI::AssetGridSpacing());
|
||||
if (!content.IsOpen()) {
|
||||
return;
|
||||
}
|
||||
|
||||
float buttonWidth = 104.0f;
|
||||
float padding = 8.0f;
|
||||
float panelWidth = ImGui::GetContentRegionAvail().x;
|
||||
int columns = (int)(panelWidth / (buttonWidth + padding));
|
||||
const float tileWidth = UI::AssetTileSize().x;
|
||||
const float spacing = UI::AssetGridSpacing().x;
|
||||
const float panelWidth = ImGui::GetContentRegionAvail().x;
|
||||
int columns = static_cast<int>((panelWidth + spacing) / (tileWidth + spacing));
|
||||
if (columns < 1) columns = 1;
|
||||
|
||||
auto& items = manager.GetCurrentItems();
|
||||
@@ -139,63 +89,60 @@ void ProjectPanel::Render() {
|
||||
displayedCount++;
|
||||
}
|
||||
|
||||
if (displayedCount == 0) {
|
||||
UI::DrawEmptyState(
|
||||
searchStr.empty() ? "No Assets" : "No Search Results",
|
||||
searchStr.empty() ? "Current folder is empty" : "No assets match the current search");
|
||||
}
|
||||
|
||||
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(0) && !ImGui::IsAnyItemHovered()) {
|
||||
manager.SetSelectedIndex(-1);
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopup("ItemContextMenu")) {
|
||||
if (UI::BeginPopup("ItemContextMenu")) {
|
||||
if (m_contextMenuIndex >= 0 && m_contextMenuIndex < (int)items.size()) {
|
||||
auto& item = items[m_contextMenuIndex];
|
||||
if (item->isFolder || item->type == "Scene") {
|
||||
if (ImGui::MenuItem("Open")) {
|
||||
if (item->isFolder) {
|
||||
manager.NavigateToFolder(item);
|
||||
} else if (SceneEditorUtils::ConfirmSceneSwitch(*m_context)) {
|
||||
if (m_context->GetSceneManager().LoadScene(item->fullPath)) {
|
||||
m_context->GetSelectionManager().ClearSelection();
|
||||
m_context->GetUndoManager().ClearHistory();
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::Separator();
|
||||
}
|
||||
if (ImGui::MenuItem("Delete")) {
|
||||
manager.DeleteItem(m_contextMenuIndex);
|
||||
const bool canOpen = item->isFolder || item->type == "Scene";
|
||||
Actions::DrawMenuAction(Actions::MakeOpenAssetAction(canOpen), [&]() {
|
||||
Commands::OpenAsset(*m_context, item);
|
||||
});
|
||||
Actions::DrawMenuSeparator();
|
||||
Actions::DrawMenuAction(Actions::MakeDeleteAssetAction(), [&]() {
|
||||
Commands::DeleteAsset(manager, m_contextMenuIndex);
|
||||
m_contextMenuIndex = -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
UI::EndPopup();
|
||||
}
|
||||
|
||||
if (ImGui::IsWindowHovered() && ImGui::IsMouseClicked(1) && !ImGui::IsAnyItemHovered()) {
|
||||
ImGui::OpenPopup("EmptyContextMenu");
|
||||
}
|
||||
|
||||
if (ImGui::BeginPopup("EmptyContextMenu")) {
|
||||
if (ImGui::MenuItem("Create Folder")) {
|
||||
m_showCreateFolderPopup = true;
|
||||
strcpy_s(m_newFolderName, "NewFolder");
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
if (UI::BeginPopup("EmptyContextMenu")) {
|
||||
Actions::DrawMenuAction(Actions::MakeCreateFolderAction(), [&]() {
|
||||
m_createFolderDialog.RequestOpen("NewFolder");
|
||||
});
|
||||
UI::EndPopup();
|
||||
}
|
||||
|
||||
if (m_showCreateFolderPopup) {
|
||||
ImGui::OpenPopup("Create Folder");
|
||||
m_showCreateFolderPopup = false;
|
||||
}
|
||||
m_createFolderDialog.ConsumeOpenRequest("Create Folder");
|
||||
|
||||
if (ImGui::BeginPopupModal("Create Folder", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
|
||||
ImGui::InputText("Name", m_newFolderName, sizeof(m_newFolderName));
|
||||
if (UI::BeginModalPopup("Create Folder")) {
|
||||
ImGui::InputText("Name", m_createFolderDialog.Buffer(), m_createFolderDialog.BufferSize());
|
||||
ImGui::Separator();
|
||||
if (ImGui::Button("Create", ImVec2(80, 0))) {
|
||||
CreateNewFolder(m_newFolderName);
|
||||
ImGui::CloseCurrentPopup();
|
||||
const Actions::ActionBinding createAction = Actions::MakeConfirmCreateAction(!m_createFolderDialog.Empty());
|
||||
const Actions::ActionBinding cancelAction = Actions::MakeCancelAction();
|
||||
if (Actions::DrawButtonAction(createAction, UI::DialogActionButtonSize())) {
|
||||
if (Commands::CreateFolder(manager, m_createFolderDialog.Buffer())) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (ImGui::Button("Cancel", ImVec2(80, 0))) {
|
||||
if (Actions::DrawButtonAction(cancelAction, UI::DialogActionButtonSize())) {
|
||||
ImGui::CloseCurrentPopup();
|
||||
}
|
||||
ImGui::EndPopup();
|
||||
UI::EndPopup();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,67 +151,33 @@ void ProjectPanel::RenderAssetItem(const AssetItemPtr& item, int index) {
|
||||
bool isSelected = (manager.GetSelectedIndex() == index);
|
||||
|
||||
ImGui::PushID(index);
|
||||
const bool isDraggingThisItem = !m_draggingPath.empty() && item->fullPath == m_draggingPath;
|
||||
const UI::AssetIconKind iconKind = item->isFolder ? UI::AssetIconKind::Folder : UI::AssetIconKind::File;
|
||||
|
||||
ImVec2 buttonSize(104.0f, 82.0f);
|
||||
const UI::AssetTileResult tile = UI::DrawAssetTile(
|
||||
item->name.c_str(),
|
||||
isSelected,
|
||||
isDraggingThisItem,
|
||||
[&](ImDrawList* drawList, const ImVec2& iconMin, const ImVec2& iconMax) {
|
||||
UI::DrawAssetIcon(drawList, iconMin, iconMax, iconKind);
|
||||
});
|
||||
|
||||
ImGui::InvisibleButton("##AssetBtn", buttonSize);
|
||||
if (ImGui::IsItemClicked(ImGuiMouseButton_Left)) {
|
||||
if (tile.clicked) {
|
||||
manager.SetSelectedIndex(index);
|
||||
}
|
||||
|
||||
bool doubleClicked = false;
|
||||
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
|
||||
doubleClicked = true;
|
||||
}
|
||||
|
||||
|
||||
bool openContextMenu = false;
|
||||
if (ImGui::IsItemClicked(1)) {
|
||||
if (tile.contextRequested) {
|
||||
manager.SetSelectedIndex(index);
|
||||
m_contextMenuIndex = index;
|
||||
openContextMenu = true;
|
||||
}
|
||||
|
||||
ImVec2 min = ImGui::GetItemRectMin();
|
||||
ImVec2 max = ImVec2(min.x + buttonSize.x, min.y + buttonSize.y);
|
||||
ImDrawList* drawList = ImGui::GetWindowDrawList();
|
||||
|
||||
const bool hovered = ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem);
|
||||
if (hovered || isSelected) {
|
||||
drawList->AddRectFilled(min, max, isSelected ? IM_COL32(156, 156, 156, 30) : IM_COL32(255, 255, 255, 10), 2.0f);
|
||||
}
|
||||
if (isSelected) {
|
||||
drawList->AddRect(min, max, IM_COL32(188, 188, 188, 110), 2.0f);
|
||||
}
|
||||
|
||||
if (!m_draggingPath.empty() && item->fullPath == m_draggingPath) {
|
||||
drawList->AddRectFilled(min, max, IM_COL32(0, 0, 0, 60), 0.0f);
|
||||
}
|
||||
|
||||
ImU32 iconFillColor = item->isFolder ? IM_COL32(118, 118, 118, 255) : IM_COL32(104, 104, 104, 255);
|
||||
ImU32 iconLineColor = item->isFolder ? IM_COL32(184, 184, 184, 220) : IM_COL32(166, 166, 166, 220);
|
||||
|
||||
const ImVec2 iconMin(min.x + 14.0f, min.y + 12.0f);
|
||||
const ImVec2 iconMax(iconMin.x + 28.0f, iconMin.y + 22.0f);
|
||||
if (item->isFolder) {
|
||||
DrawFolderIcon(drawList, iconMin, iconMax, iconFillColor, iconLineColor);
|
||||
} else {
|
||||
DrawFileIcon(drawList, iconMin, iconMax, iconFillColor, iconLineColor);
|
||||
}
|
||||
|
||||
ImVec4 textColor = isSelected ? ImVec4(0.93f, 0.93f, 0.93f, 1.0f) : ImVec4(0.76f, 0.76f, 0.76f, 1.0f);
|
||||
ImVec2 textSize = ImGui::CalcTextSize(item->name.c_str());
|
||||
float textY = max.y - textSize.y - 10.0f;
|
||||
|
||||
ImGui::PushClipRect(ImVec2(min.x + 6.0f, min.y), ImVec2(max.x - 6.0f, max.y), true);
|
||||
drawList->AddText(ImVec2(min.x + 6.0f, textY), ImGui::GetColorU32(textColor), item->name.c_str());
|
||||
ImGui::PopClipRect();
|
||||
|
||||
if (item->isFolder) {
|
||||
if (ImGui::BeginDragDropTarget()) {
|
||||
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(DRAG_DROP_TYPE)) {
|
||||
const char* draggedPath = (const char*)payload->Data;
|
||||
std::string sourcePath(draggedPath);
|
||||
manager.MoveItem(sourcePath, item->fullPath);
|
||||
Commands::MoveAssetToFolder(manager, draggedPath, item);
|
||||
}
|
||||
ImGui::EndDragDropTarget();
|
||||
}
|
||||
@@ -275,28 +188,16 @@ void ProjectPanel::RenderAssetItem(const AssetItemPtr& item, int index) {
|
||||
ImGui::SetDragDropPayload(DRAG_DROP_TYPE, item->fullPath.c_str(), item->fullPath.length() + 1);
|
||||
|
||||
ImVec2 previewMin = ImGui::GetMousePos();
|
||||
ImVec2 previewMax = ImVec2(previewMin.x + 24.0f, previewMin.y + 20.0f);
|
||||
if (item->isFolder) {
|
||||
DrawFolderIcon(ImGui::GetForegroundDrawList(), previewMin, previewMax, iconFillColor, iconLineColor);
|
||||
} else {
|
||||
DrawFileIcon(ImGui::GetForegroundDrawList(), previewMin, previewMax, iconFillColor, iconLineColor);
|
||||
}
|
||||
const ImVec2 previewSize = UI::AssetDragPreviewSize();
|
||||
ImVec2 previewMax = ImVec2(previewMin.x + previewSize.x, previewMin.y + previewSize.y);
|
||||
UI::DrawAssetIcon(ImGui::GetForegroundDrawList(), previewMin, previewMax, iconKind);
|
||||
|
||||
ImGui::EndDragDropSource();
|
||||
}
|
||||
}
|
||||
|
||||
if (doubleClicked) {
|
||||
if (item->isFolder) {
|
||||
manager.NavigateToFolder(item);
|
||||
} else if (item->type == "Scene") {
|
||||
if (SceneEditorUtils::ConfirmSceneSwitch(*m_context)) {
|
||||
if (m_context->GetSceneManager().LoadScene(item->fullPath)) {
|
||||
m_context->GetSelectionManager().ClearSelection();
|
||||
m_context->GetUndoManager().ClearHistory();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tile.openRequested) {
|
||||
Commands::OpenAsset(*m_context, item);
|
||||
}
|
||||
|
||||
ImGui::PopID();
|
||||
@@ -306,14 +207,5 @@ void ProjectPanel::RenderAssetItem(const AssetItemPtr& item, int index) {
|
||||
}
|
||||
}
|
||||
|
||||
void ProjectPanel::CreateNewFolder(const std::string& name) {
|
||||
auto& manager = m_context->GetProjectManager();
|
||||
manager.CreateFolder(name);
|
||||
}
|
||||
|
||||
bool ProjectPanel::HandleDrop(const AssetItemPtr& targetFolder) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user