Files
RobustToolbox/Robust.Client/UserInterface/Controls/ScrollContainer.cs
Jezithyr 710371d7d1 UI refactor and UITheme implementations (#2712)
* UIControllerManager


Implemented UI Controller Manager

* added fetch function

* added note

* Hiding some internal stuff

* Implemented event on gamestate switch for ui

* Fix serialization field assigner emit

* fixing issues with ILEmitter stuff

* AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

Blame Smug

* fixing nullref

* Add checking for no backing field / property for ui system dependencies

* fixes Gamestate detection

* Implemented event on UIControllers on system load

* Updated systemload/unload listeners

* Had this backwards lol

* Fix nulling systems before calling OnSystemUnloaded, broke InventoryUIController.Hands.cs

* Created UI Window management system

- A manager that allows for easy creation and access of popup or gamestate windows

* Changing to use basewindow instead of default window

* Implemented UI Theming that isn't ass

* Updated default theme loading and validation

* Added path validation for themes

* Implemented UI Themes

* Implemented UI Theme prototypes

* Implementing theming for texture buttons and Texturerects

* fixing server error

* Remove IUILink

* Implemented default theme overriding and theme colors

* Fixing sandbox lul

* Added error for not finding UITheme

* fixing setting default theme in content

* Move entity and tile spawn window logic to controllers

* Add 2 TODOs

* Merge fixes

* Add IOnStateChanged for UI controllers

* Fix inventory window being slow to open
Caches resources when the UI theme is changed

* Remove caching on theme change
The real fix was fixing the path for resources

* Remove test method

* Fix crash when controllers implement non generic interfaces

* Add controllers frame update

* Split UserInterfaceManager into partials

- Created UI screen

* Converted more UI managers into partials

* Setup UIScreen manager system

* Added some widget utility funcs


updated adding widgets

* Started removing HUDManager

* Moved UiController Manager to Partials


Finished moving all UIController code to UIManager

* Fixed screen loading

* Fixed Screen scaling

* Fixed Screen scaling


cleanup

* wat

* IwantToDie

* Fixed resolving ResourceCache instead of IResourceCache

* Split IOnStateChanged into IOnStateEntered and IOnStateExited

* Implemented helpers for adjusting UIAutoscale for screens

* Fixed autoscale, removed archiving from autoscale

* Implemented popups and adjusted some stuff

* Fixing some popup related shinanegans

* Fixing some draw order issues

* fixing dumb shit

* Fix indentation in UserInterfaceManager.Input.cs

* Moved screen setup to post init (run after content)

* Fix updating theme

* Merge fixes

* Fix resolving sprite system on control creation

* Fix min size of tile spawn window

* Add UIController.Initialize method

* https://tenor.com/view/minor-spelling-mistake-gif-21179057

* Add doc comment to UIController

* Split UIController.cs and UISystemDependency.cs into their own files

* Add more documentation to ui controllers

* Add AttributeUsage to UISystemDependencyAttribute

* Fix method naming

* Add documentation for assigners

* Return casted widgets where relevant

* Fix entity spawner scroll (#1)

* Add CloseOnClick and CloseOnEscape for popups

* Remove named windows and popups

* Cleanup controller code

* Add IOnStateChanged, IOnSystemChanged, IOnSystemLoaded, IOnSystemUnloaded

* Add more docs to state and system change interfaces

* Fixing Window issues

* Fixing some window fuckery

* Added OnOpen event to windows, updated sandbox window

Sandbox windows now persist values and positions

* Recurse through controls to register widgets (#2)

* Allow path to be folder

* Fix local player shutdown

* Fixing escape menu

* Fix backing field in DataDefinition.Emitters

* Ent+Tile spawn no crash

* Skip no-spawn in entity spawn menu

Co-authored-by: Jezithyr <jmaster9999@gmail.com>
Co-authored-by: DrSmugleaf <DrSmugleaf@users.noreply.github.com>
Co-authored-by: Jezithyr <Jezithyr@gmail.com>
Co-authored-by: wrexbe <81056464+wrexbe@users.noreply.github.com>
Co-authored-by: Flipp Syder <76629141+vulppine@users.noreply.github.com>
Co-authored-by: wrexbe <wrexbe@protonmail.com>
2022-09-04 16:10:54 -07:00

281 lines
7.9 KiB
C#

using System;
using System.Diagnostics.Contracts;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface.Controls
{
[Virtual]
public class ScrollContainer : Container
{
private bool _queueScrolled = false;
private bool _vScrollEnabled = true;
private bool _hScrollEnabled = true;
private bool _vScrollVisible;
private bool _hScrollVisible;
private readonly VScrollBar _vScrollBar;
private readonly HScrollBar _hScrollBar;
private bool _suppressScrollValueChanged;
public int ScrollSpeedX { get; set; } = 50;
public int ScrollSpeedY { get; set; } = 50;
public bool ReturnMeasure { get; set; } = false;
public event Action? OnScrolled;
public ScrollContainer()
{
MouseFilter = MouseFilterMode.Pass;
RectClipContent = true;
Action<Range> ev = _scrollValueChanged;
_hScrollBar = new HScrollBar
{
Visible = false,
VerticalAlignment = VAlignment.Bottom,
HorizontalAlignment = HAlignment.Stretch
};
_vScrollBar = new VScrollBar
{
Visible = false,
VerticalAlignment = VAlignment.Stretch,
HorizontalAlignment = HAlignment.Right
};
AddChild(_hScrollBar);
AddChild(_vScrollBar);
_hScrollBar.OnValueChanged += ev;
_vScrollBar.OnValueChanged += ev;
}
public bool VScrollEnabled
{
get => _vScrollEnabled;
set
{
_vScrollEnabled = value;
InvalidateMeasure();
}
}
public bool HScrollEnabled
{
get => _hScrollEnabled;
set
{
_hScrollEnabled = value;
InvalidateMeasure();
}
}
protected override Vector2 MeasureOverride(Vector2 availableSize)
{
if (_vScrollEnabled)
{
_vScrollBar.Measure(availableSize);
availableSize.X -= _vScrollBar.DesiredSize.X;
}
if (_hScrollEnabled)
{
_hScrollBar.Measure(availableSize);
availableSize.Y -= _hScrollBar.DesiredSize.Y;
}
var constraint = new Vector2(
_hScrollEnabled ? float.PositiveInfinity : availableSize.X,
_vScrollEnabled ? float.PositiveInfinity : availableSize.Y);
var size = Vector2.Zero;
foreach (var child in Children)
{
child.Measure(constraint);
size = Vector2.ComponentMax(size, child.DesiredSize);
}
// Unlike WPF/Avalonia we default to reporting ZERO here instead of available size. This is to fix a bunch
// of jank with e.g. BoxContainer.
if (!ReturnMeasure)
return Vector2.Zero;
if (_vScrollEnabled)
size.X += _vScrollBar.DesiredSize.X;
if (_hScrollEnabled)
size.Y += _hScrollBar.DesiredSize.Y;
return size;
}
protected override Vector2 ArrangeOverride(Vector2 finalSize)
{
if (_vScrollBar?.Parent == null || _hScrollBar?.Parent == null)
{
// Just don't run this before we're properly initialized.
return Vector2.Zero;
}
var maxChildMinSize = Vector2.Zero;
foreach (var child in Children)
{
if (child == _vScrollBar || child == _hScrollBar)
{
continue;
}
maxChildMinSize = Vector2.ComponentMax(child.DesiredSize, maxChildMinSize);
}
var (cWidth, cHeight) = maxChildMinSize;
var hBarSize = _hScrollBar.DesiredSize.Y;
var vBarSize = _vScrollBar.DesiredSize.X;
var (sWidth, sHeight) = finalSize;
try
{
// Suppress events to avoid weird recursion.
_suppressScrollValueChanged = true;
if (sWidth < cWidth && _hScrollEnabled)
{
sHeight -= hBarSize;
}
if (sHeight < cHeight && _vScrollEnabled)
{
sWidth -= vBarSize;
}
if (sWidth < cWidth && _hScrollEnabled)
{
_hScrollBar.Visible = _hScrollVisible = true;
_hScrollBar.Page = sWidth;
_hScrollBar.MaxValue = cWidth;
}
else
{
_hScrollBar.Visible = _hScrollVisible = false;
}
if (sHeight < cHeight && _vScrollEnabled)
{
_vScrollBar.Visible = _vScrollVisible = true;
_vScrollBar.Page = sHeight;
_vScrollBar.MaxValue = cHeight;
}
else
{
_vScrollBar.Visible = _vScrollVisible = false;
}
}
finally
{
// I really don't think this can throw an exception but oh well let's finally it.
_suppressScrollValueChanged = false;
}
if (_vScrollVisible)
{
_vScrollBar.Arrange(UIBox2.FromDimensions(Vector2.Zero, finalSize));
}
if (_hScrollVisible)
{
_hScrollBar.Arrange(UIBox2.FromDimensions(Vector2.Zero, finalSize));
}
var realFinalSize = (
_hScrollEnabled ? Math.Max(cWidth, sWidth) : sWidth,
_vScrollEnabled ? Math.Max(cHeight, sHeight) : sHeight);
foreach (var child in Children)
{
if (child == _vScrollBar || child == _hScrollBar)
{
continue;
}
var position = -_getScrollValue();
var rect = UIBox2.FromDimensions(position, realFinalSize);
child.Arrange(rect);
}
return finalSize;
}
protected override void ArrangeCore(UIBox2 finalRect)
{
base.ArrangeCore(finalRect);
if (!_queueScrolled) return;
OnScrolled?.Invoke();
_queueScrolled = false;
}
protected internal override void MouseWheel(GUIMouseWheelEventArgs args)
{
base.MouseWheel(args);
if (_vScrollEnabled)
{
_vScrollBar.ValueTarget -= args.Delta.Y * ScrollSpeedY;
}
if (_hScrollEnabled)
{
_hScrollBar.ValueTarget += args.Delta.X * ScrollSpeedX;
}
args.Handle();
}
protected override void ChildAdded(Control newChild)
{
base.ChildAdded(newChild);
if (_vScrollBar?.Parent == null || _hScrollBar?.Parent == null)
{
// Just don't run this before we're properly initialized.
return;
}
_vScrollBar?.SetPositionLast();
_hScrollBar?.SetPositionLast();
}
[Pure]
private Vector2 _getScrollValue()
{
var h = _hScrollBar.Value;
var v = _vScrollBar.Value;
if (!_hScrollVisible)
{
h = 0;
}
if (!_vScrollVisible)
{
v = 0;
}
return new Vector2(h, v);
}
private void _scrollValueChanged(Range obj)
{
if (_suppressScrollValueChanged)
{
return;
}
InvalidateArrange();
_queueScrolled = true;
}
}
}