Files
RobustToolbox/Robust.Client/UserInterface/UserInterfaceManager.Scaling.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

146 lines
5.2 KiB
C#

using System;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.Controls;
using Robust.Shared;
using Robust.Shared.Maths;
using Robust.Shared.ViewVariables;
namespace Robust.Client.UserInterface;
internal partial class UserInterfaceManager
{
[ViewVariables] public float DefaultUIScale => _clyde.DefaultWindowScale.X;
[ViewVariables] private Vector2i _resolutionAutoScaleUpper;
[ViewVariables] private Vector2i _resolutionAutoScaleLower;
[ViewVariables] private bool _autoScaleEnabled;
[ViewVariables] private float _resolutionAutoScaleMinValue;
private void _initScaling()
{
_clyde.OnWindowResized += WindowSizeChanged;
_clyde.OnWindowScaleChanged += WindowContentScaleChanged;
RegisterAutoscaleCVarListeners();
_uiScaleChanged(_configurationManager.GetCVar(CVars.DisplayUIScale));
}
private void _uiScaleChanged(float newValue)
{
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}
private void WindowContentScaleChanged(WindowContentScaleEventArgs args)
{
if (_windowsToRoot.TryGetValue(args.Window.Id, out var root))
{
UpdateUIScale(root);
_fontManager.ClearFontCache();
}
}
private void RegisterAutoscaleCVarListeners()
{
_configurationManager.OnValueChanged(CVars.ResAutoScaleEnabled, i =>
{
_autoScaleEnabled = i;
foreach (var root in _roots)
{
root.UIScaleSet = 1;
_propagateUIScaleChanged(root);
root.InvalidateMeasure();
}
}, true);
_configurationManager.OnValueChanged(CVars.ResAutoScaleLowX, i =>
{
_resolutionAutoScaleLower.X = i;
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}, true);
_configurationManager.OnValueChanged(CVars.ResAutoScaleLowY, i =>
{
_resolutionAutoScaleLower.Y = i;
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}, true);
_configurationManager.OnValueChanged(CVars.ResAutoScaleUpperX, i =>
{
_resolutionAutoScaleUpper.X = i;
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}, true);
_configurationManager.OnValueChanged(CVars.ResAutoScaleUpperY, i =>
{
_resolutionAutoScaleUpper.Y = i;
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}, true);
_configurationManager.OnValueChanged(CVars.ResAutoScaleMin, i =>
{
_resolutionAutoScaleMinValue = i;
foreach (var root in _roots)
{
UpdateUIScale(root);
}
}, true);
}
private float CalculateAutoScale(WindowRoot root)
{
//Grab the OS UIScale or the value set through CVAR debug
var osScale = _configurationManager.GetCVar(CVars.DisplayUIScale);
osScale = osScale == 0f ? root.Window.ContentScale.X : osScale;
var windowSize = root.Window.RenderTarget.Size;
//Only run autoscale if it is enabled, otherwise default to just use OS UIScale
if (!_autoScaleEnabled && (windowSize.X <= 0 || windowSize.Y <= 0)) return osScale;
var maxScaleRes = _resolutionAutoScaleUpper;
var minScaleRes = _resolutionAutoScaleLower;
var autoScaleMin = _resolutionAutoScaleMinValue;
float scaleRatioX;
float scaleRatioY;
//Calculate the scale ratios and clamp it between the maximums and minimums
scaleRatioX = Math.Clamp(((float) windowSize.X - minScaleRes.X) / (maxScaleRes.X - minScaleRes.X) * osScale, autoScaleMin, osScale);
scaleRatioY = Math.Clamp(((float) windowSize.Y - minScaleRes.Y) / (maxScaleRes.Y - minScaleRes.Y) * osScale, autoScaleMin, osScale);
//Take the smallest UIScale value and use it for UI scaling
return Math.Min(scaleRatioX, scaleRatioY);
}
private void UpdateUIScale(WindowRoot root)
{
root.UIScaleSet = CalculateAutoScale(root);
_propagateUIScaleChanged(root);
root.InvalidateMeasure();
}
private static void _propagateUIScaleChanged(Control control)
{
control.UIScaleChanged();
foreach (var child in control.Children)
{
_propagateUIScaleChanged(child);
}
}
private void WindowSizeChanged(WindowResizedEventArgs windowResizedEventArgs)
{
if (!_windowsToRoot.TryGetValue(windowResizedEventArgs.Window.Id, out var root))
return;
UpdateUIScale(root);
root.InvalidateMeasure();
}
}