Files
RobustToolbox/Robust.Client/UserInterface/Controls/TextureRect.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

287 lines
9.6 KiB
C#

using System;
using Robust.Client.Graphics;
using Robust.Client.ResourceManagement;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface.Controls
{
/// <summary>
/// Simple control that draws a single texture using a variety of possible stretching modes.
/// </summary>
/// <seealso cref="AnimatedTextureRect"/>
[Virtual]
public class TextureRect : Control
{
public const string StylePropertyTexture = "texture";
public const string StylePropertyShader = "shader";
private bool _canShrink;
private Texture? _texture;
private Vector2 _textureScale = Vector2.One;
public ShaderInstance? ShaderOverride { get; set; }
/// <summary>
/// The texture to draw.
/// </summary>
public Texture? Texture
{
get => _texture;
set
{
var oldSize = _texture?.Size;
_texture = value;
if (value?.Size != oldSize)
{
InvalidateMeasure();
}
}
}
private string? _texturePath;
// TODO HUD REFACTOR BEFORE MERGE use or cleanup
public string TextureThemePath
{
set
{
Texture = Theme.ResolveTexture(value);
_texturePath = value;
}
}
// TODO HUD REFACTOR BEFORE MERGE use or cleanup
public string TexturePath
{
set
{
Texture = IoCManager.Resolve<IResourceCache>().GetResource<TextureResource>(value);
_texturePath = value;
}
}
protected override void OnThemeUpdated()
{
if (_texturePath != null) Texture = Theme.ResolveTexture(_texturePath);
base.OnThemeUpdated();
}
/// <summary>
/// Scales the texture displayed.
/// </summary>
/// <remarks>
/// This does not apply to the following stretch modes: <see cref="StretchMode.Scale"/>.
/// </remarks>
public Vector2 TextureScale
{
get => _textureScale;
set
{
_textureScale = value;
InvalidateMeasure();
}
}
/// <summary>
/// If true, this control can shrink below the size of <see cref="Texture"/>.
/// </summary>
/// <remarks>
/// This does not set <see cref="Control.RectClipContent"/>.
/// Certain stretch modes may display outside the area of the control unless it is set.
/// </remarks>
public bool CanShrink
{
get => _canShrink;
set
{
_canShrink = value;
InvalidateMeasure();
}
}
/// <summary>
/// Controls how the texture should be drawn if the control is larger than the size of the texture.
/// </summary>
public StretchMode Stretch { get; set; } = StretchMode.Keep;
protected internal override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
var texture = _texture;
ShaderInstance? shader = null;
if (texture == null)
{
TryGetStyleProperty(StylePropertyTexture, out texture);
if (texture == null)
{
return;
}
}
if (ShaderOverride != null)
{
shader = ShaderOverride;
}
else if (TryGetStyleProperty(StylePropertyShader, out ShaderInstance? styleShader))
{
shader = styleShader;
}
if (shader != null)
{
handle.UseShader(shader);
}
switch (Stretch)
{
case StretchMode.Scale:
case StretchMode.Tile:
// TODO: Implement Tile.
case StretchMode.Keep:
case StretchMode.KeepCentered:
case StretchMode.KeepAspect:
case StretchMode.KeepAspectCentered:
handle.DrawTextureRect(texture,
GetDrawDimensions(texture));
break;
case StretchMode.KeepAspectCovered:
var dimensions = GetDrawDimensions(texture);
var subRegion = CalcClipSubRegion(texture.Size, dimensions, PixelSizeBox);
handle.DrawTextureRectRegion(texture, PixelSizeBox, subRegion);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private static UIBox2 CalcClipSubRegion(Vector2 texSize, UIBox2 drawDimensions, UIBox2 size)
{
var normTL = (size.TopLeft - drawDimensions.TopLeft) / drawDimensions.Size;
var normBR = (size.BottomRight - drawDimensions.TopLeft) / drawDimensions.Size;
return new UIBox2(normTL * texSize, normBR * texSize);
}
protected UIBox2 GetDrawDimensions(Texture texture)
{
switch (Stretch)
{
case StretchMode.Scale:
return UIBox2.FromDimensions(Vector2.Zero, PixelSize);
case StretchMode.Tile:
// TODO: Implement Tile.
case StretchMode.Keep:
return UIBox2.FromDimensions(Vector2.Zero, texture.Size * _textureScale * UIScale);
case StretchMode.KeepCentered:
{
var position = (PixelSize - texture.Size * _textureScale * UIScale) / 2;
return UIBox2.FromDimensions(position, texture.Size * _textureScale * UIScale);
}
case StretchMode.KeepAspect:
case StretchMode.KeepAspectCentered:
{
var (texWidth, texHeight) = texture.Size * _textureScale;
var width = texWidth * (PixelSize.Y / texHeight);
var height = (float)PixelSize.Y;
if (width > PixelSize.X)
{
width = PixelSize.X;
height = texHeight * (PixelSize.X / texWidth);
}
var size = new Vector2(width, height);
var position = Vector2.Zero;
if (Stretch == StretchMode.KeepAspectCentered)
{
position = (PixelSize - size) / 2;
}
return UIBox2.FromDimensions(position, size);
}
case StretchMode.KeepAspectCovered:
var texSize = texture.Size * _textureScale;
// Calculate the scale necessary to fit width and height to control size.
var (scaleX, scaleY) = PixelSize / texSize;
// Use whichever scale is greater.
var scale = Math.Max(scaleX, scaleY);
// Offset inside the actual texture.
var texDrawSize = texSize * scale;
var offset = (PixelSize - texDrawSize) / 2f;
return UIBox2.FromDimensions(offset, texDrawSize);
default:
throw new ArgumentOutOfRangeException();
}
}
public enum StretchMode : byte
{
/// <summary>
/// The texture is stretched to fit the entire area of the control.
/// </summary>
Scale = 1,
/// <summary>
/// The texture is tiled to fit the entire area of the control, without stretching.
/// </summary>
Tile = 2,
/// <summary>
/// The texture is drawn in its correct size, in the top left corner of the control.
/// </summary>
Keep = 3,
/// <summary>
/// The texture is drawn in its correct size, in the center of the control.
/// </summary>
KeepCentered = 4,
/// <summary>
/// The texture is stretched to take as much space as possible,
/// while maintaining the original aspect ratio.
/// The texture is positioned from the top left corner of the control.
/// The texture remains completely visible, potentially leaving some sections of the control blank.
/// </summary>
KeepAspect = 5,
/// <summary>
/// <see cref="KeepAspect"/>, but the texture is centered instead.
/// </summary>
KeepAspectCentered = 7,
/// <summary>
/// <see cref="KeepAspectCentered"/>, but the texture covers the entire control,
/// potentially cutting out part of the texture.
/// </summary>
/// <example>
/// This effectively causes the entire control to be filled with the texture,
/// while preserving aspect ratio.
/// </example>
KeepAspectCovered = 8
}
protected override Vector2 MeasureOverride(Vector2 availableSize)
{
var texture = _texture;
if (texture == null)
{
TryGetStyleProperty(StylePropertyTexture, out texture);
}
if (texture == null || CanShrink)
{
return Vector2.Zero;
}
return texture.Size * TextureScale;
}
}
}