Created
November 7, 2021 18:54
-
-
Save Fulgen301/85680bd9740768683ae0365e5ae1493e to your computer and use it in GitHub Desktop.
GuiScrollableArea.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using Vintagestory.API.Client; | |
namespace GuiExtensions | |
{ | |
public class GuiScrollableArea : GuiElement | |
{ | |
public ElementBounds ClippingBounds { get; private init; } | |
public ElementBounds ContentBounds { get; private init; } | |
public GuiElementScrollbar Scrollbar { get; private init; } | |
public GuiScrollableArea(GuiComposer composer, ElementBounds bounds, Vintagestory.API.Common.Action<GuiComposer, ElementBounds> addElements, double padding = 3.0, Vintagestory.API.Common.Action<float> onNewScrollbarValue = null, int insetDepth = 4, float insetBrightness = 0.85f) : base(composer.Api, bounds) | |
{ | |
ClippingBounds = bounds.ForkContainingChild(padding, padding, padding, padding); | |
ContentBounds = ClippingBounds.ForkContainingChild(0.0, 0.0, 0.0, -padding); | |
ContentBounds.fixedY = 0; | |
Scrollbar = new(composer.Api, value => | |
{ | |
ContentBounds.fixedY = -value; | |
ContentBounds.CalcWorldBounds(); | |
onNewScrollbarValue?.Invoke(value); | |
}, ElementStdBounds.VerticalScrollbar(bounds)); | |
composer | |
.AddInset(bounds, insetDepth, insetBrightness) | |
.AddInteractiveElement(Scrollbar) | |
.BeginClip(ClippingBounds); | |
addElements(composer, ContentBounds); | |
composer.EndClip(); | |
} | |
public void ScrollToTop() | |
{ | |
typeof(GuiElementScrollbar).GetMethod("SetScrollbarPosition", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(Scrollbar, new object[] { 0 }); | |
} | |
public void ScrollToBottom() | |
{ | |
Scrollbar.ScrollToBottom(); | |
} | |
public void CalcTotalHeight() | |
{ | |
Scrollbar.SetHeights((float) ClippingBounds.fixedHeight, (float) ContentBounds.fixedHeight); | |
} | |
} | |
public static partial class GuiComposerHelpers | |
{ | |
public static GuiComposer AddScrollableArea(this GuiComposer composer, ElementBounds bounds, Vintagestory.API.Common.Action<GuiComposer, ElementBounds> addElements, double padding = 3.0, Vintagestory.API.Common.Action<float> onNewScrollbarValue = null, int insetDepth = 4, float insetBrightness = 0.85f, string key = null) | |
{ | |
if (!(bool) typeof(GuiComposer).GetField("composed", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(composer)) | |
{ | |
composer.AddStaticElement(new GuiScrollableArea(composer, bounds, addElements, padding, onNewScrollbarValue, insetDepth, insetBrightness), key); | |
} | |
return composer; | |
} | |
public static GuiScrollableArea GetScrollableArea(this GuiComposer composer, string key) | |
{ | |
return (GuiScrollableArea) composer.GetElement(key); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment