Polish shared editor tree context behavior
This commit is contained in:
@@ -170,6 +170,14 @@ inline float NavigationTreePrefixWidth() {
|
||||
return 16.0f;
|
||||
}
|
||||
|
||||
inline float NavigationTreePrefixLabelGap() {
|
||||
return 6.0f;
|
||||
}
|
||||
|
||||
inline float NavigationTreePrefixStartOffset() {
|
||||
return 2.0f;
|
||||
}
|
||||
|
||||
inline ImVec4 NavigationTreePrefixColor(bool selected = false, bool hovered = false) {
|
||||
if (selected) {
|
||||
return ImVec4(0.86f, 0.86f, 0.86f, 1.0f);
|
||||
|
||||
@@ -94,28 +94,6 @@ struct TreeNodeDefinition {
|
||||
TreeNodeCallbacks callbacks;
|
||||
};
|
||||
|
||||
inline std::string MakeTreeNodeDisplayLabel(const char* label, float prefixWidth) {
|
||||
std::string result;
|
||||
if (!label) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (prefixWidth <= 0.0f) {
|
||||
result = label;
|
||||
return result;
|
||||
}
|
||||
|
||||
const float spaceWidth = ImGui::CalcTextSize(" ").x;
|
||||
int spaceCount = static_cast<int>(prefixWidth / (spaceWidth > 0.0f ? spaceWidth : 1.0f)) + 1;
|
||||
if (spaceCount < 1) {
|
||||
spaceCount = 1;
|
||||
}
|
||||
|
||||
result.assign(static_cast<size_t>(spaceCount), ' ');
|
||||
result += label;
|
||||
return result;
|
||||
}
|
||||
|
||||
inline TreeNodeResult DrawTreeNode(
|
||||
TreeViewState* state,
|
||||
const void* id,
|
||||
@@ -149,9 +127,20 @@ inline TreeNodeResult DrawTreeNode(
|
||||
flags |= ImGuiTreeNodeFlags_DefaultOpen;
|
||||
}
|
||||
|
||||
const std::string displayLabel = definition.prefix.IsVisible()
|
||||
? MakeTreeNodeDisplayLabel(label, definition.prefix.width)
|
||||
: std::string(label ? label : "");
|
||||
std::string displayLabel = label ? label : "";
|
||||
if (definition.prefix.IsVisible()) {
|
||||
const float reserveWidth =
|
||||
NavigationTreePrefixStartOffset() +
|
||||
definition.prefix.width +
|
||||
NavigationTreePrefixLabelGap();
|
||||
const float spaceWidth = ImGui::CalcTextSize(" ").x;
|
||||
int spaceCount = static_cast<int>(reserveWidth / (spaceWidth > 0.0f ? spaceWidth : 1.0f)) + 2;
|
||||
if (spaceCount < 1) {
|
||||
spaceCount = 1;
|
||||
}
|
||||
|
||||
displayLabel.insert(0, static_cast<size_t>(spaceCount), ' ');
|
||||
}
|
||||
|
||||
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, NavigationTreeNodeFramePadding());
|
||||
const bool open = ImGui::TreeNodeEx(id, flags, "%s", displayLabel.c_str());
|
||||
@@ -170,14 +159,17 @@ inline TreeNodeResult DrawTreeNode(
|
||||
state->SetExpanded(definition.persistenceKey, result.open);
|
||||
}
|
||||
|
||||
const ImVec2 itemMin = ImGui::GetItemRectMin();
|
||||
const ImVec2 itemMax = ImGui::GetItemRectMax();
|
||||
const float labelStartX = itemMin.x + ImGui::GetTreeNodeToLabelSpacing();
|
||||
|
||||
if (definition.prefix.IsVisible()) {
|
||||
const ImVec2 itemMin = ImGui::GetItemRectMin();
|
||||
const ImVec2 itemMax = ImGui::GetItemRectMax();
|
||||
const float prefixMinX = itemMin.x + ImGui::GetTreeNodeToLabelSpacing();
|
||||
const float prefixMinX = labelStartX + NavigationTreePrefixStartOffset();
|
||||
const float prefixMaxX = prefixMinX + definition.prefix.width;
|
||||
definition.prefix.draw(TreeNodePrefixContext{
|
||||
ImGui::GetWindowDrawList(),
|
||||
ImVec2(prefixMinX, itemMin.y),
|
||||
ImVec2(prefixMinX + definition.prefix.width, itemMax.y),
|
||||
ImVec2(prefixMaxX, itemMax.y),
|
||||
options.selected,
|
||||
result.hovered
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user