Adds nice-looking dynamic underline animation for UI buttons (Stride Game Engine) :>
| !CopyPasteData | |
| ItemType: '!System.Collections.Generic.List%601[[Stride.Core.Assets.AssetItem,Stride.Core.Assets]],System.Private.CoreLib' | |
| Items: | |
| - Data: !System.Collections.Generic.List%25601[[Stride.Core.Assets.AssetItem,Stride.Core.Assets]],System.Private.CoreLib | |
| c05f59dbe2a1e13cfef8473ed5e145a8: | |
| Location: !file ButtonHoverScript | |
| Asset: !ScriptSourceFileAsset | |
| Id: 2c21b3a3-3990-cf4b-9797-8bd5f73d8c41 | |
| Tags: [] | |
| InternalSerializableTextAccessor: !Stride.Core.Assets.TextAccessors.StringTextAccessor,Stride.Core.Assets | |
| Text: |- | |
| using System.Collections.Generic; | |
| using Stride.Core.Mathematics; | |
| using Stride.Engine; | |
| using Stride.UI; | |
| using Stride.UI.Controls; | |
| using Stride.UI.Panels; | |
| namespace YourGameNamespaceHereYO | |
| { | |
| public class ButtonHoverScaler : SyncScript | |
| { | |
| public UIComponent UiComponent { get; set; } | |
| public float LineHeight = 3f; | |
| public float LineWidth = 0.8f; | |
| public float AnimationSpeed = 8f; | |
| public Color LineColor = new Color(0, 120, 255, 255); | |
| private readonly List | |
| private readonly List lines = new List(); | |
| private readonly List currentWidths = new List(); | |
| public override void Start() | |
| { | |
| if (UiComponent?.Page?.RootElement == null) | |
| return; | |
| FindAllButtons(UiComponent.Page.RootElement, buttons); | |
| foreach (var btn in buttons) | |
| { | |
| btn.RequiresMouseOverUpdate = true; | |
| if (btn.Parent is Panel parent) | |
| { | |
| int index = parent.Children.IndexOf(btn); | |
| var wrapper = new Grid(); | |
| parent.Children.RemoveAt(index); | |
| parent.Children.Insert(index, wrapper); | |
| wrapper.Children.Add(btn); | |
| var line = new Border | |
| { | |
| BackgroundColor = LineColor, | |
| Height = LineHeight, | |
| Width = 0, | |
| VerticalAlignment = VerticalAlignment.Bottom, | |
| HorizontalAlignment = HorizontalAlignment.Center, | |
| Margin = new Thickness(0, 0, 0, 5), | |
| Visibility = Visibility.Collapsed | |
| }; | |
| wrapper.Children.Add(line); | |
| lines.Add(line); | |
| currentWidths.Add(0f); | |
| } | |
| } | |
| } | |
| public override void Update() | |
| { | |
| float dt = (float)Game.UpdateTime.Elapsed.TotalSeconds; | |
| for (int i = 0; i < buttons.Count; i++) | |
| { | |
| var btn = buttons[i]; | |
| var line = lines[i]; | |
| if (btn == null || line == null) | |
| continue; | |
| bool hovering = btn.MouseOverState == MouseOverState.MouseOverElement; | |
| float targetWidth = hovering ? btn.RenderSize.X * LineWidth : 0f; | |
| currentWidths[i] = MathUtil.Lerp(currentWidths[i], targetWidth, AnimationSpeed * dt); | |
| if (currentWidths[i] > 0.01f) | |
| { | |
| line.Visibility = Visibility.Visible; | |
| line.Width = currentWidths[i]; | |
| } | |
| else | |
| { | |
| line.Visibility = Visibility.Collapsed; | |
| } | |
| } | |
| } | |
| private void FindAllButtons(UIElement element, List | |
| { | |
| if (element is Button btn) | |
| { | |
| resultList.Add(btn); | |
| return; | |
| } | |
| if (element is Panel panel) | |
| { | |
| foreach (var child in panel.Children) | |
| { | |
| FindAllButtons(child, resultList); | |
| } | |
| } | |
| else if (element is ContentDecorator decorator && decorator.Content != null) | |
| { | |
| FindAllButtons(decorator.Content, resultList); | |
| } | |
| } | |
| } | |
| } | |
| AlternativePath: null |
评论
?
参与讨论