From ed82580d011c8e480da41bbbac139d3681fef7b9 Mon Sep 17 00:00:00 2001 From: austin Date: Wed, 14 May 2025 21:46:08 -0400 Subject: [PATCH] fix virtual scrollable --- docs/src/progress/renderertodo.md | 1 + .../ui/elements/VirtualScrollable.java | 31 ++++++++++++++----- .../ui/elementtypes/ContainerElement.java | 9 ++++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 609bf58a..690f46ee 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1785,6 +1785,7 @@ Move around entity debug tab classes Grid alignment data editing in debug menus Grid alignment offsets work Inventory state in non-creatures actually saves/loads to/from disk +Fix virtual scrollable mouse alignment for events diff --git a/src/main/java/electrosphere/renderer/ui/elements/VirtualScrollable.java b/src/main/java/electrosphere/renderer/ui/elements/VirtualScrollable.java index 6f3973b1..5daa8817 100644 --- a/src/main/java/electrosphere/renderer/ui/elements/VirtualScrollable.java +++ b/src/main/java/electrosphere/renderer/ui/elements/VirtualScrollable.java @@ -36,6 +36,11 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa */ boolean visible = true; + /** + * The child div + */ + Div childDiv; + /** * Constructor */ @@ -44,6 +49,9 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa this.setWidth(width); this.setHeight(height); Yoga.YGNodeStyleSetOverflow(this.yogaNode, Yoga.YGOverflowScroll); + this.childDiv = Div.createCol(); + this.childDiv.setPositionType(YogaPositionType.Relative); + super.addChild(this.childDiv); } @Override @@ -54,7 +62,7 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa int framebufferPosX, int framebufferPosY ) { - for(Element child : childList){ + for(Element child : this.childDiv.getChildren()){ if(child instanceof DrawableElement){ DrawableElement drawableChild = (DrawableElement) child; if(this.childIsInBounds(drawableChild)){ @@ -63,7 +71,7 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa openGLState, framebuffer, framebufferPosX, - framebufferPosY - (int)scroll + framebufferPosY ); } } @@ -77,7 +85,7 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa */ private boolean childIsInBounds(DrawableElement element){ boolean rVal = true; - if(element.getAbsoluteY() + scroll < this.getAbsoluteY() || element.getAbsoluteY() + scroll > this.getHeight() + this.getAbsoluteY()){ + if(element.getAbsoluteY() < this.getAbsoluteY() || element.getAbsoluteY() > this.getHeight() + this.getAbsoluteY()){ return false; } return rVal; @@ -114,17 +122,24 @@ public class VirtualScrollable extends StandardContainerElement implements Drawa } //calculate max scroll double maxScroll = 0; - for(Element child : this.getChildren()){ - if(child.getAbsoluteY() + child.getHeight() > maxScroll){ - maxScroll = child.getAbsoluteY() + child.getHeight() - this.getHeight(); - } - } + maxScroll = this.childDiv.getHeight() - this.getHeight(); if(scroll < - maxScroll){ scroll = -maxScroll; } + this.childDiv.setPositionY((int)scroll); // this.setPositionY((int)scroll); } + @Override + public void addChild(Element child) { + this.childDiv.addChild(child); + } + + @Override + public void removeChild(Element child) { + this.childDiv.removeChild(child); + } + @Override public boolean handleEvent(Event event){ boolean propagate = true; diff --git a/src/main/java/electrosphere/renderer/ui/elementtypes/ContainerElement.java b/src/main/java/electrosphere/renderer/ui/elementtypes/ContainerElement.java index 729c2605..3c5b5953 100644 --- a/src/main/java/electrosphere/renderer/ui/elementtypes/ContainerElement.java +++ b/src/main/java/electrosphere/renderer/ui/elementtypes/ContainerElement.java @@ -65,8 +65,17 @@ public interface ContainerElement extends Element { * The position type of a yoga node */ public static enum YogaPositionType { + /** + * Relative to the nearest positioned ancestor + */ Absolute, + /** + * Relative to its normal position + */ Relative, + /** + * Not affected by top/bottom/left/right properties + */ Static, }