impr: Use ImGui's built-in drag n drop support for bookmarks
This commit is contained in:
parent
9530100455
commit
3a068b9719
@ -28,7 +28,6 @@ namespace hex::plugin::builtin {
|
|||||||
TextEditor editor;
|
TextEditor editor;
|
||||||
};
|
};
|
||||||
|
|
||||||
std::list<Bookmark>::iterator m_dragStartIterator;
|
|
||||||
PerProvider<std::list<Bookmark>> m_bookmarks;
|
PerProvider<std::list<Bookmark>> m_bookmarks;
|
||||||
PerProvider<u64> m_currBookmarkId;
|
PerProvider<u64> m_currBookmarkId;
|
||||||
};
|
};
|
||||||
|
@ -86,7 +86,7 @@ namespace hex::plugin::builtin {
|
|||||||
drawBitsSlider(&bitValue);
|
drawBitsSlider(&bitValue);
|
||||||
|
|
||||||
// Configure drag and drop source and target
|
// Configure drag and drop source and target
|
||||||
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_None)) {
|
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoPreviewTooltip)) {
|
||||||
// Set the current slider index as the payload
|
// Set the current slider index as the payload
|
||||||
ImGui::SetDragDropPayload("BIT_VALUE", &index, sizeof(u32));
|
ImGui::SetDragDropPayload("BIT_VALUE", &index, sizeof(u32));
|
||||||
|
|
||||||
|
@ -253,7 +253,6 @@ namespace hex::plugin::builtin {
|
|||||||
ImGuiExt::TextFormattedCentered("hex.builtin.view.bookmarks.no_bookmarks"_lang);
|
ImGuiExt::TextFormattedCentered("hex.builtin.view.bookmarks.no_bookmarks"_lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
int id = 1;
|
|
||||||
auto bookmarkToRemove = m_bookmarks->end();
|
auto bookmarkToRemove = m_bookmarks->end();
|
||||||
|
|
||||||
// Draw all bookmarks
|
// Draw all bookmarks
|
||||||
@ -272,34 +271,58 @@ namespace hex::plugin::builtin {
|
|||||||
hoverColor.Value.w *= 1.3F;
|
hoverColor.Value.w *= 1.3F;
|
||||||
|
|
||||||
// Draw bookmark header in the same color as the bookmark was set to
|
// Draw bookmark header in the same color as the bookmark was set to
|
||||||
ImGui::PushID(id);
|
ImGui::PushID(bookmarkId);
|
||||||
ImGui::PushStyleColor(ImGuiCol_Header, color);
|
ImGui::PushStyleColor(ImGuiCol_Header, color);
|
||||||
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
|
ImGui::PushStyleColor(ImGuiCol_HeaderActive, color);
|
||||||
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor));
|
ImGui::PushStyleColor(ImGuiCol_HeaderHovered, u32(hoverColor));
|
||||||
|
|
||||||
ON_SCOPE_EXIT {
|
ON_SCOPE_EXIT {
|
||||||
ImGui::PopID();
|
|
||||||
ImGui::PopStyleColor(3);
|
ImGui::PopStyleColor(3);
|
||||||
id++;
|
ImGui::PopID();
|
||||||
};
|
};
|
||||||
|
|
||||||
bool open = true;
|
bool open = true;
|
||||||
if (!ImGui::CollapsingHeader(hex::format("{}###bookmark", name).c_str(), locked ? nullptr : &open)) {
|
if (!ImGui::CollapsingHeader(hex::format("{}###bookmark", name).c_str(), locked ? nullptr : &open)) {
|
||||||
// Handle dragging bookmarks up and down when they're collapsed
|
// Handle dragging bookmarks up and down when they're collapsed
|
||||||
|
|
||||||
// Set the currently held bookmark as the one being dragged
|
if (ImGui::BeginDragDropSource(ImGuiDragDropFlags_SourceNoHoldToOpenOthers)) {
|
||||||
if (ImGui::IsMouseClicked(0) && ImGui::IsItemActivated() && m_dragStartIterator == m_bookmarks->end())
|
// Set the payload to the bookmark id
|
||||||
m_dragStartIterator = it;
|
ImGui::SetDragDropPayload("BOOKMARK_PAYLOAD", &bookmarkId, sizeof(bookmarkId));
|
||||||
|
|
||||||
// When the mouse moved away from the current bookmark, swap the dragged bookmark with the current one
|
// Draw drag and drop tooltip
|
||||||
if (ImGui::IsItemHovered() && m_dragStartIterator != m_bookmarks->end()) {
|
ImGui::ColorButton("##color", headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoTooltip | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha);
|
||||||
std::iter_swap(it, m_dragStartIterator);
|
ImGui::SameLine();
|
||||||
m_dragStartIterator = it;
|
ImGuiExt::TextFormatted("{}", name);
|
||||||
|
ImGui::Separator();
|
||||||
|
|
||||||
|
if (!comment.empty()) {
|
||||||
|
ImGui::PushTextWrapPos(300_scaled);
|
||||||
|
ImGuiExt::TextFormatted("{}", comment);
|
||||||
|
ImGui::PopTextWrapPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
// When the mouse is released, reset the dragged bookmark
|
ImGui::EndDragDropSource();
|
||||||
if (!ImGui::IsMouseDown(0))
|
}
|
||||||
m_dragStartIterator = m_bookmarks->end();
|
|
||||||
|
if (ImGui::BeginDragDropTarget()) {
|
||||||
|
|
||||||
|
if (auto payload = ImGui::AcceptDragDropPayload("BOOKMARK_PAYLOAD"); payload != nullptr) {
|
||||||
|
// Receive the bookmark id from the payload
|
||||||
|
u64 droppedBookmarkId = *static_cast<const u64*>(payload->Data);
|
||||||
|
|
||||||
|
// Find the correct bookmark with that id
|
||||||
|
auto droppedIter = std::ranges::find_if(m_bookmarks->begin(), m_bookmarks->end(), [droppedBookmarkId](const auto &bookmark) {
|
||||||
|
return bookmark.entry.id == droppedBookmarkId;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Swap the two bookmarks
|
||||||
|
if (droppedIter != m_bookmarks->end()) {
|
||||||
|
std::iter_swap(it, droppedIter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui::EndDragDropTarget();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const auto rowHeight = ImGui::GetTextLineHeightWithSpacing() + 2 * ImGui::GetStyle().FramePadding.y;
|
const auto rowHeight = ImGui::GetTextLineHeightWithSpacing() + 2 * ImGui::GetStyle().FramePadding.y;
|
||||||
if (ImGui::BeginTable("##bookmark_table", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) {
|
if (ImGui::BeginTable("##bookmark_table", 3, ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit)) {
|
||||||
@ -465,15 +488,14 @@ namespace hex::plugin::builtin {
|
|||||||
.comment = bookmark["comment"],
|
.comment = bookmark["comment"],
|
||||||
.color = bookmark["color"],
|
.color = bookmark["color"],
|
||||||
.locked = bookmark["locked"],
|
.locked = bookmark["locked"],
|
||||||
.id = bookmark.contains("id") ? bookmark["id"].get<u64>() : *m_currBookmarkId
|
.id = bookmark.contains("id") ? bookmark["id"].get<u64>() : m_currBookmarkId.get(provider)
|
||||||
},
|
},
|
||||||
editor
|
editor
|
||||||
});
|
});
|
||||||
|
|
||||||
if (bookmark.contains("id"))
|
if (bookmark.contains("id"))
|
||||||
m_currBookmarkId = std::max<u64>(m_currBookmarkId, bookmark["id"].get<i64>() + 1);
|
m_currBookmarkId.get(provider) = std::max<u64>(m_currBookmarkId.get(provider), bookmark["id"].get<i64>() + 1);
|
||||||
else
|
else
|
||||||
m_currBookmarkId += 1;
|
m_currBookmarkId.get(provider) += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user