Files
RobustToolbox/Robust.Client/UserInterface/CustomControls/BaseWindow.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

292 lines
9.5 KiB
C#

using System;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.Input;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Timing;
namespace Robust.Client.UserInterface.CustomControls
{
/// <summary>
/// Provides basic functionality for windows that can be opened, dragged around, etc...
/// </summary>
public abstract class BaseWindow : Control
{
private DragMode CurrentDrag = DragMode.None;
private Vector2 DragOffsetTopLeft;
private Vector2 DragOffsetBottomRight;
public bool Resizable { get; set; } = true;
public bool IsOpen => Parent != null;
/// <summary>
/// Invoked when the close button of this window is pressed.
/// </summary>
public event Action? OnClose;
public event Action? OnOpen;
public virtual void Close()
{
if (Parent == null)
{
return;
}
Parent.RemoveChild(this);
OnClose?.Invoke();
}
protected internal override void KeyBindDown(GUIBoundKeyEventArgs args)
{
base.KeyBindDown(args);
if (args.Function != EngineKeyFunctions.UIClick)
{
return;
}
CurrentDrag = GetDragModeFor(args.RelativePosition);
if (CurrentDrag != DragMode.None)
{
DragOffsetTopLeft = args.PointerLocation.Position / UIScale - Position;
DragOffsetBottomRight = Position + Size - args.PointerLocation.Position / UIScale;
}
MoveToFront();
}
protected internal override void KeyBindUp(GUIBoundKeyEventArgs args)
{
base.KeyBindUp(args);
if (args.Function != EngineKeyFunctions.UIClick)
{
return;
}
DragOffsetTopLeft = DragOffsetBottomRight = Vector2.Zero;
CurrentDrag = DragMode.None;
// If this is done in MouseDown, Godot won't fire MouseUp as you need focus to receive MouseUps.
UserInterfaceManager.KeyboardFocused?.ReleaseKeyboardFocus();
}
protected internal override void MouseMove(GUIMouseMoveEventArgs args)
{
base.MouseMove(args);
if (Parent == null)
{
return;
}
if (CurrentDrag == DragMode.Move)
{
var globalPos = args.GlobalPosition;
globalPos = Vector2.Clamp(globalPos, Vector2.Zero, Parent.Size);
LayoutContainer.SetPosition(this, globalPos - DragOffsetTopLeft);
return;
}
if (!Resizable)
{
return;
}
if (CurrentDrag == DragMode.None)
{
var cursor = CursorShape.Arrow;
var previewDragMode = GetDragModeFor(args.RelativePosition);
switch (previewDragMode)
{
case DragMode.Top:
case DragMode.Bottom:
cursor = CursorShape.VResize;
break;
case DragMode.Left:
case DragMode.Right:
cursor = CursorShape.HResize;
break;
case DragMode.Bottom | DragMode.Left:
case DragMode.Top | DragMode.Right:
cursor = CursorShape.Crosshair;
break;
case DragMode.Bottom | DragMode.Right:
case DragMode.Top | DragMode.Left:
cursor = CursorShape.Crosshair;
break;
}
DefaultCursorShape = cursor;
}
else
{
var (left, top) = Position;
var (right, bottom) = Position + SetSize;
if (float.IsNaN(SetSize.X)) {
right = Position.X + Size.X;
}
if (float.IsNaN(SetSize.Y)) {
bottom = Position.Y + Size.Y;
}
if ((CurrentDrag & DragMode.Top) == DragMode.Top)
{
top = Math.Min(args.GlobalPosition.Y - DragOffsetTopLeft.Y, Math.Min(bottom, bottom - MinSize.Y));
}
else if ((CurrentDrag & DragMode.Bottom) == DragMode.Bottom)
{
bottom = Math.Max(args.GlobalPosition.Y + DragOffsetBottomRight.Y, Math.Max(top, top + MinSize.Y));
}
if ((CurrentDrag & DragMode.Left) == DragMode.Left)
{
left = Math.Min(args.GlobalPosition.X - DragOffsetTopLeft.X, Math.Min(right, right - MinSize.X));
}
else if ((CurrentDrag & DragMode.Right) == DragMode.Right)
{
right = Math.Max(args.GlobalPosition.X + DragOffsetBottomRight.X, Math.Max(left, left + MinSize.X));
}
var rect = new UIBox2(left, top, right, bottom);
LayoutContainer.SetPosition(this, rect.TopLeft);
SetSize = rect.Size;
/*
var timing = IoCManager.Resolve<IGameTiming>();
var l = GetValue<float>(LayoutContainer.MarginLeftProperty);
var t = GetValue<float>(LayoutContainer.MarginTopProperty);
Logger.Debug($"{timing.CurFrame}: {rect.TopLeft}/({l}, {t}), {rect.Size}/{SetSize}");
*/
}
}
protected internal override void MouseExited()
{
base.MouseExited();
if (Resizable && CurrentDrag == DragMode.None)
{
DefaultCursorShape = CursorShape.Arrow;
}
}
public void MoveToFront()
{
if (Parent == null)
{
throw new InvalidOperationException("This window is not currently open.");
}
SetPositionLast();
}
public bool IsAtFront()
{
if (Parent == null)
{
throw new InvalidOperationException("This window is not currently open");
}
var siblingCount = Parent.ChildCount;
var ourPos = GetPositionInParent();
for (var i = ourPos + 1; i < siblingCount; i++)
{
if (Parent.GetChild(i).Visible)
{
// If we find a control after us that's visible, we're NOT in front.
return false;
}
}
return true;
}
public void Open()
{
if (!Visible)
{
Visible = true;
Logger.WarningS("ui", $"Window {this} had visibility false. Do not use visibility on DefaultWindow.");
}
if (!IsOpen)
{
UserInterfaceManager.WindowRoot.AddChild(this);
}
Opened();
OnOpen?.Invoke();
}
public void OpenCentered() => OpenCenteredAt((0.5f, 0.5f));
public void OpenToLeft() => OpenCenteredAt((0, 0.5f));
public void OpenCenteredLeft() => OpenCenteredAt((0.25f, 0.5f));
public void OpenToRight() => OpenCenteredAt((1, 0.5f));
public void OpenCenteredRight() => OpenCenteredAt((0.75f, 0.5f));
/// <summary>
/// Opens a window, attempting to place the center of the window at some relative point on the screen.
/// </summary>
/// <param name="relativePosition">Fractional screen position. So (0,0) is the upper left, and (1,1) is the
/// lower right.</param>
public void OpenCenteredAt(Vector2 relativePosition)
{
Measure(Vector2.Infinity);
SetSize = DesiredSize;
Open();
RecenterWindow(relativePosition);
}
/// <summary>
/// Repositions a window, attempting to place the center of the window at some relative point on the screen.
/// </summary>
/// <param name="relativePosition">Fractional screen position. So (0,0) is the upper left, and (1,1) is the
/// lower right.</param>
public void RecenterWindow(Vector2 relativePosition)
{
if (Parent == null)
return;
// Where we want the upper left corner of the window to be
var corner = Parent!.Size * Vector2.Clamp(relativePosition, Vector2.Zero, Vector2.One) - DesiredSize / 2;
// Attempt to keep the whole window is visible, regardless of the target position. e.g., if the target for
// the center is (0,0), this will actually open the window so that the upper left is at (0,0). If the window
// is bigger than the parent, this will currently prioritize showing the upper left corner.
var pos = Vector2.Clamp(corner, Vector2.Zero, Parent.Size - DesiredSize);
LayoutContainer.SetPosition(this, pos);
}
protected virtual void Opened()
{
}
protected virtual DragMode GetDragModeFor(Vector2 relativeMousePos)
{
return DragMode.None;
}
[Flags]
protected enum DragMode : byte
{
None = 0,
Move = 1,
Top = 1 << 1,
Bottom = 1 << 2,
Left = 1 << 3,
Right = 1 << 4,
}
}
}