Files
XCEngine/editor/src/Actions/ProjectActionRouter.h

132 lines
4.3 KiB
C++

#pragma once
#include "EditorActions.h"
#include "Commands/ProjectCommands.h"
#include "Core/IEditorContext.h"
#include "Core/IProjectManager.h"
#include "UI/PopupState.h"
namespace XCEngine {
namespace Editor {
namespace Actions {
inline constexpr const char* ProjectAssetPayloadType() {
return "ASSET_ITEM";
}
inline int FindProjectItemIndex(IProjectManager& projectManager, const AssetItemPtr& item) {
if (!item) {
return -1;
}
const auto& items = projectManager.GetCurrentItems();
for (size_t i = 0; i < items.size(); ++i) {
if (items[i] == item) {
return static_cast<int>(i);
}
if (items[i] && items[i]->fullPath == item->fullPath) {
return static_cast<int>(i);
}
}
return -1;
}
inline const char* GetDraggedProjectAssetPath() {
const ImGuiPayload* payload = ImGui::GetDragDropPayload();
if (!payload || !payload->IsDataType(ProjectAssetPayloadType())) {
return nullptr;
}
return static_cast<const char*>(payload->Data);
}
inline bool IsProjectAssetBeingDragged(const AssetItemPtr& item) {
const char* draggedPath = GetDraggedProjectAssetPath();
return item != nullptr && draggedPath != nullptr && item->fullPath == draggedPath;
}
inline bool AcceptProjectAssetDrop(IProjectManager& projectManager, const AssetItemPtr& targetFolder) {
if (!targetFolder || !targetFolder->isFolder || !ImGui::BeginDragDropTarget()) {
return false;
}
bool accepted = false;
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(ProjectAssetPayloadType())) {
const char* draggedPath = static_cast<const char*>(payload->Data);
accepted = Commands::MoveAssetToFolder(projectManager, draggedPath, targetFolder);
}
ImGui::EndDragDropTarget();
return accepted;
}
inline bool BeginProjectAssetDrag(const AssetItemPtr& item, UI::AssetIconKind iconKind) {
if (!item || item->fullPath.empty() || !ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
return false;
}
ImGui::SetDragDropPayload(ProjectAssetPayloadType(), item->fullPath.c_str(), item->fullPath.length() + 1);
ImVec2 previewMin = ImGui::GetMousePos();
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();
return true;
}
inline void DrawProjectAssetContextActions(IEditorContext& context, const AssetItemPtr& item) {
auto& projectManager = context.GetProjectManager();
const int itemIndex = FindProjectItemIndex(projectManager, item);
DrawMenuAction(MakeOpenAssetAction(Commands::CanOpenAsset(item)), [&]() {
Commands::OpenAsset(context, item);
});
DrawMenuSeparator();
DrawMenuAction(MakeDeleteAssetAction(itemIndex >= 0), [&]() {
Commands::DeleteAsset(projectManager, itemIndex);
});
}
template <size_t BufferCapacity>
inline void DrawProjectEmptyContextActions(UI::TextInputPopupState<BufferCapacity>& createFolderDialog) {
DrawMenuAction(MakeCreateFolderAction(), [&]() {
createFolderDialog.RequestOpen("NewFolder");
});
}
template <size_t BufferCapacity>
inline void DrawProjectCreateFolderDialog(IEditorContext& context, UI::TextInputPopupState<BufferCapacity>& createFolderDialog) {
createFolderDialog.ConsumeOpenRequest("Create Folder");
if (!UI::BeginModalPopup("Create Folder")) {
return;
}
ImGui::InputText("Name", createFolderDialog.Buffer(), createFolderDialog.BufferSize());
ImGui::Separator();
switch (UI::DrawDialogActionRow("Create", "Cancel", !createFolderDialog.Empty())) {
case UI::DialogActionResult::Primary:
if (Commands::CreateFolder(context.GetProjectManager(), createFolderDialog.Buffer())) {
createFolderDialog.Clear();
ImGui::CloseCurrentPopup();
}
break;
case UI::DialogActionResult::Secondary:
createFolderDialog.Clear();
ImGui::CloseCurrentPopup();
break;
case UI::DialogActionResult::None:
break;
}
UI::EndPopup();
}
} // namespace Actions
} // namespace Editor
} // namespace XCEngine