改进ProjectPanel右键菜单交互逻辑

This commit is contained in:
2026-03-12 17:34:39 +08:00
parent 64dd8339dd
commit f8fed72cb7
2 changed files with 56 additions and 43 deletions

View File

@@ -28,8 +28,7 @@ void ProjectPanel::Render() {
ImGui::Text("%s", manager.GetCurrentPath().c_str());
ImGui::SameLine();
float refreshBtnWidth = 60.0f;
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - refreshBtnWidth - 80.0f);
ImGui::SetCursorPosX(ImGui::GetWindowWidth() - 140.0f);
if (ImGui::Button("Refresh")) {
manager.RefreshCurrentFolder();
}
@@ -48,7 +47,7 @@ void ProjectPanel::Render() {
ImGui::Separator();
ImGui::BeginChild("AssetList", ImVec2(0, 0), false);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10, 10));
float buttonWidth = 80.0f;
float padding = 10.0f;
@@ -58,20 +57,46 @@ void ProjectPanel::Render() {
auto& items = manager.GetCurrentItems();
std::string searchStr = m_searchBuffer;
for (int i = 0; i < items.size(); i++) {
int itemIndex = 0;
for (int i = 0; i < (int)items.size(); i++) {
if (!searchStr.empty()) {
if (items[i]->name.find(searchStr) == std::string::npos) {
continue;
}
}
if (i > 0 && i % columns != 0) {
if (itemIndex > 0 && itemIndex % columns != 0) {
ImGui::SameLine();
}
RenderAssetItem(items[i], i);
itemIndex++;
}
if (ImGui::BeginPopupContextWindow("ProjectContextMenu")) {
ImGui::PopStyleVar();
if (ImGui::BeginPopup("ItemContextMenu")) {
if (m_contextMenuIndex >= 0 && m_contextMenuIndex < (int)items.size()) {
auto& item = items[m_contextMenuIndex];
if (item->isFolder) {
if (ImGui::MenuItem("Open")) {
manager.NavigateToFolder(item);
}
ImGui::Separator();
}
if (ImGui::MenuItem("Delete")) {
manager.DeleteItem(m_contextMenuIndex);
m_contextMenuIndex = -1;
}
}
ImGui::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");
@@ -83,10 +108,11 @@ void ProjectPanel::Render() {
ImGui::EndPopup();
}
ImGui::EndChild();
ImGui::End();
if (m_showCreateFolderPopup) {
ImGui::OpenPopup("Create Folder");
m_showCreateFolderPopup = false;
}
if (ImGui::BeginPopupModal("Create Folder", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
@@ -94,56 +120,42 @@ void ProjectPanel::Render() {
ImGui::Separator();
if (ImGui::Button("Create", ImVec2(80, 0))) {
CreateNewFolder(m_newFolderName);
m_showCreateFolderPopup = false;
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
if (ImGui::Button("Cancel", ImVec2(80, 0))) {
m_showCreateFolderPopup = false;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ImGui::End();
}
void ProjectPanel::RenderAssetItem(const AssetItemPtr& item, int index) {
auto& manager = ProjectManager::Get();
bool isSelected = (manager.GetSelectedIndex() == index);
std::string popupId = "ItemMenu" + std::to_string(index);
ImGui::PushID(index);
if (isSelected) {
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.40f, 0.40f, 0.40f, 0.50f));
}
ImVec2 buttonSize(80.0f, 90.0f);
if (ImGui::Button(("##Asset" + std::to_string(index)).c_str(), buttonSize)) {
if (ImGui::Button("##AssetBtn", buttonSize)) {
manager.SetSelectedIndex(index);
}
if (ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
bool doubleClicked = false;
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
doubleClicked = true;
}
bool openContextMenu = false;
if (ImGui::IsItemClicked(1)) {
manager.SetSelectedIndex(index);
ImGui::OpenPopup(popupId.c_str());
}
if (item->isFolder && ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(0)) {
manager.NavigateToFolder(item);
}
if (ImGui::BeginPopup(popupId.c_str())) {
if (item->isFolder) {
if (ImGui::MenuItem("Open")) {
manager.NavigateToFolder(item);
}
}
ImGui::Separator();
if (ImGui::MenuItem("Delete")) {
manager.SetSelectedIndex(index);
DeleteSelectedItem();
}
ImGui::EndPopup();
m_contextMenuIndex = index;
openContextMenu = true;
}
if (isSelected) {
@@ -180,6 +192,16 @@ void ProjectPanel::RenderAssetItem(const AssetItemPtr& item, int index) {
ImGui::PushClipRect(min, ImVec2(min.x + 80.0f, min.y + 90.0f), true);
drawList->AddText(ImVec2(min.x + textOffset, min.y + 60.0f), ImGui::GetColorU32(textColor), item->name.c_str());
ImGui::PopClipRect();
if (doubleClicked && item->isFolder) {
manager.NavigateToFolder(item);
}
ImGui::PopID();
if (openContextMenu) {
ImGui::OpenPopup("ItemContextMenu");
}
}
void ProjectPanel::CreateNewFolder(const std::string& name) {
@@ -187,12 +209,4 @@ void ProjectPanel::CreateNewFolder(const std::string& name) {
manager.CreateFolder(name);
}
void ProjectPanel::DeleteSelectedItem() {
auto& manager = ProjectManager::Get();
int selectedIndex = manager.GetSelectedIndex();
if (selectedIndex >= 0) {
manager.DeleteItem(selectedIndex);
}
}
}

View File

@@ -13,13 +13,12 @@ public:
private:
void RenderAssetItem(const AssetItemPtr& item, int index);
void RenderContextMenu();
void CreateNewFolder(const std::string& name);
void DeleteSelectedItem();
char m_searchBuffer[256] = "";
bool m_showCreateFolderPopup = false;
char m_newFolderName[256] = "NewFolder";
int m_contextMenuIndex = -1;
};
}