Performance optimization & bugfix

Only depth sort if we need to.
Fixed a bug where if you left the highest ancestor of a slider and re-entered it would still be sliding.
This commit is contained in:
2025-05-30 22:22:09 -04:00
parent c4ada58e0c
commit 7ac0436a1f
3 changed files with 35 additions and 3 deletions

View File

@@ -81,6 +81,9 @@ namespace JUI {
std::vector<Widget*> GetDescendants();
/// Returns a flat list of all nodes that are higher in the hierarchy list.
std::vector<Widget*> GetAncestors();
/// @returns the highest ancestor which is not the scene root.
Widget* GetHighestNonRootAncestor();
/// Returns the nodes directly descendant to this widget.
std::vector<Widget*> GetChildren();
@@ -123,7 +126,7 @@ namespace JUI {
/// @note The range of valid values is -MAX_INT to MAX_INT.
/// @note This does not manipulate the OpenGL Z buffer, rather, when rendering,
/// we sort the direct children of a widget widget and render.
int ZIndex() const;
[[nodiscard]] int ZIndex() const;
/// Sets this widgets z-index.
/// @see ZIndex().
@@ -384,6 +387,7 @@ namespace JUI {
Widget* next = nullptr;
Widget* prev = nullptr;
int zindex = 0;
bool z_dirty = false;
int layout_order = 0;
Vector2 viewport_size{0,0};

View File

@@ -292,7 +292,10 @@ struct {
zIndexSort;
void Widget::DrawChildWidgets() {
std::sort(children.begin(), children.end(), zIndexSort);
if (z_dirty)
z_dirty = false,
std::sort(children.begin(), children.end(), zIndexSort);
for (auto* child : children)
child->Draw();
}
@@ -419,7 +422,11 @@ void Widget::AnchorPoint(const Vector2& point) {
int Widget::ZIndex() const { return zindex;}
void Widget::ZIndex(int new_zindex) { zindex = new_zindex; }
void Widget::ZIndex(int new_zindex) {
zindex = new_zindex;
if (parent)
parent->z_dirty = true;
}
AABB2D Widget::GetActualRenderBounds() const {
return {GetAbsolutePosition(), GetAbsoluteSize()};
@@ -652,4 +659,19 @@ float Widget::ComputeElementPadding(float size, const UDim &padding) {
return padding.Pixels + (padding.Scale * size);
}
Widget* Widget::GetHighestNonRootAncestor() {
if (!parent)
return nullptr;
Widget* previous = this;
Widget* current = parent;
while (current->parent) {
previous = current;
current = current->parent;
}
return previous;
}

View File

@@ -74,6 +74,12 @@ namespace JUI
current = value;
//std::cout << "slider value: " << value << std::endl;
}
// TODO there is probably a more elegant fix for this.
// Fixes the slider not letting go - Redacted.
Widget* target = parent ? GetHighestNonRootAncestor() : this;
if (!target->IsMouseInside()) { dragging = false; mb_state = false; }
Rect::Update(delta);
}