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

527 lines
15 KiB
C#

using System;
using System.Diagnostics.CodeAnalysis;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Input;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.ViewVariables;
namespace Robust.Client.UserInterface;
internal partial class UserInterfaceManager
{
private float _tooltipTimer;
private ICursor? _worldCursor;
private bool _needUpdateActiveCursor;
[ViewVariables] public Control? KeyboardFocused { get; private set; }
[ViewVariables] public Control? CurrentlyHovered { get; private set; } = default!;
private Control? _controlFocused;
[ViewVariables]
public Control? ControlFocused
{
get => _controlFocused;
set
{
if (_controlFocused == value)
return;
_controlFocused?.ControlFocusExited();
_controlFocused = value;
}
}
// set to null when not counting down
private float? _tooltipDelay;
private Tooltip _tooltip = default!;
private bool showingTooltip;
private Control? _suppliedTooltip;
private const float TooltipDelay = 1;
private static (Control control, Vector2 rel)? MouseFindControlAtPos(Control control, Vector2 position)
{
for (var i = control.ChildCount - 1; i >= 0; i--)
{
var child = control.GetChild(i);
if (!child.Visible || child.RectClipContent && !child.PixelRect.Contains((Vector2i) position))
{
continue;
}
var maybeFoundOnChild = MouseFindControlAtPos(child, position - child.PixelPosition);
if (maybeFoundOnChild != null)
{
return maybeFoundOnChild;
}
}
if (control.MouseFilter != Control.MouseFilterMode.Ignore && control.HasPoint(position / control.UIScale))
{
return (control, position);
}
return null;
}
public void KeyBindDown(BoundKeyEventArgs args)
{
if (args.Function == EngineKeyFunctions.CloseModals && _modalStack.Count != 0)
{
bool closedAny = false;
for (var i = _modalStack.Count - 1; i >= 0; i--)
{
var top = _modalStack[i];
if (top is not Popup {CloseOnEscape: false})
{
RemoveModal(top);
closedAny = true;
}
}
if (closedAny)
{
args.Handle();
}
return;
}
var control = ControlFocused ?? KeyboardFocused ?? MouseGetControl(args.PointerLocation);
if (control == null)
{
return;
}
var guiArgs = new GUIBoundKeyEventArgs(args.Function, args.State, args.PointerLocation, args.CanFocus,
args.PointerLocation.Position / control.UIScale - control.GlobalPosition,
args.PointerLocation.Position - control.GlobalPixelPosition);
_doGuiInput(control, guiArgs, (c, ev) => c.KeyBindDown(ev));
if (guiArgs.Handled)
{
args.Handle();
}
}
public void KeyBindUp(BoundKeyEventArgs args)
{
var control = ControlFocused ?? KeyboardFocused ?? MouseGetControl(args.PointerLocation);
if (control == null)
{
return;
}
var guiArgs = new GUIBoundKeyEventArgs(args.Function, args.State, args.PointerLocation, args.CanFocus,
args.PointerLocation.Position / control.UIScale - control.GlobalPosition,
args.PointerLocation.Position - control.GlobalPixelPosition);
_doGuiInput(control, guiArgs, (c, ev) => c.KeyBindUp(ev));
// Always mark this as handled.
// The only case it should not be is if we do not have a control to click on,
// in which case we never reach this.
args.Handle();
}
public void MouseMove(MouseMoveEventArgs mouseMoveEventArgs)
{
_resetTooltipTimer();
// Update which control is considered hovered.
var newHovered = MouseGetControl(mouseMoveEventArgs.Position);
if (newHovered != CurrentlyHovered)
{
_clearTooltip();
CurrentlyHovered?.MouseExited();
CurrentlyHovered = newHovered;
CurrentlyHovered?.MouseEntered();
if (CurrentlyHovered != null)
{
_tooltipDelay = CurrentlyHovered.TooltipDelay ?? TooltipDelay;
}
else
{
_tooltipDelay = null;
}
_needUpdateActiveCursor = true;
}
var target = ControlFocused ?? newHovered;
if (target != null)
{
var pos = mouseMoveEventArgs.Position.Position;
var guiArgs = new GUIMouseMoveEventArgs(mouseMoveEventArgs.Relative / target.UIScale,
target,
pos / target.UIScale, mouseMoveEventArgs.Position,
pos / target.UIScale - target.GlobalPosition,
pos - target.GlobalPixelPosition);
_doMouseGuiInput(target, guiArgs, (c, ev) => c.MouseMove(ev));
}
}
private void UpdateActiveCursor()
{
// Consider mouse input focus first so that dragging windows don't act up etc.
var cursorTarget = ControlFocused ?? CurrentlyHovered;
if (cursorTarget == null)
{
_clyde.SetCursor(_worldCursor);
return;
}
if (cursorTarget.CustomCursorShape != null)
{
_clyde.SetCursor(cursorTarget.CustomCursorShape);
return;
}
var shape = cursorTarget.DefaultCursorShape switch
{
Control.CursorShape.Arrow => StandardCursorShape.Arrow,
Control.CursorShape.IBeam => StandardCursorShape.IBeam,
Control.CursorShape.Hand => StandardCursorShape.Hand,
Control.CursorShape.Crosshair => StandardCursorShape.Crosshair,
Control.CursorShape.VResize => StandardCursorShape.VResize,
Control.CursorShape.HResize => StandardCursorShape.HResize,
_ => StandardCursorShape.Arrow
};
_clyde.SetCursor(_clyde.GetStandardCursor(shape));
}
public void MouseWheel(MouseWheelEventArgs args)
{
var control = MouseGetControl(args.Position);
if (control == null)
{
return;
}
args.Handle();
var pos = args.Position.Position;
var guiArgs = new GUIMouseWheelEventArgs(args.Delta, control,
pos / control.UIScale, args.Position,
pos / control.UIScale - control.GlobalPosition, pos - control.GlobalPixelPosition);
_doMouseGuiInput(control, guiArgs, (c, ev) => c.MouseWheel(ev), true);
}
public void TextEntered(TextEventArgs textEvent)
{
if (KeyboardFocused == null)
{
return;
}
var guiArgs = new GUITextEventArgs(KeyboardFocused, textEvent.CodePoint);
KeyboardFocused.TextEntered(guiArgs);
}
public ScreenCoordinates MousePositionScaled => ScreenToUIPosition(_inputManager.MouseScreenPosition);
private static void _doMouseGuiInput<T>(Control? control, T guiEvent, Action<Control, T> action,
bool ignoreStop = false)
where T : GUIMouseEventArgs
{
while (control != null)
{
guiEvent.SourceControl = control;
if (control.MouseFilter != Control.MouseFilterMode.Ignore)
{
action(control, guiEvent);
if (guiEvent.Handled || (!ignoreStop && control.MouseFilter == Control.MouseFilterMode.Stop))
{
break;
}
}
guiEvent.RelativePosition += control.Position;
guiEvent.RelativePixelPosition += control.PixelPosition;
control = control.Parent;
}
}
private static void _doGuiInput(
Control? control,
GUIBoundKeyEventArgs guiEvent,
Action<Control, GUIBoundKeyEventArgs> action,
bool ignoreStop = false)
{
while (control != null)
{
if (control.MouseFilter != Control.MouseFilterMode.Ignore)
{
action(control, guiEvent);
if (guiEvent.Handled || (!ignoreStop && control.MouseFilter == Control.MouseFilterMode.Stop))
{
break;
}
}
guiEvent.RelativePosition += control.Position;
guiEvent.RelativePixelPosition += control.PixelPosition;
control = control.Parent;
}
}
private void _clearTooltip()
{
if (!showingTooltip) return;
_tooltip.Visible = false;
if (_suppliedTooltip != null)
{
PopupRoot.RemoveChild(_suppliedTooltip);
_suppliedTooltip = null;
}
CurrentlyHovered?.PerformHideTooltip();
_resetTooltipTimer();
showingTooltip = false;
}
public void CursorChanged(Control control)
{
if (control == ControlFocused || control == CurrentlyHovered)
{
_needUpdateActiveCursor = true;
}
}
public void HideTooltipFor(Control control)
{
if (CurrentlyHovered == control)
{
_clearTooltip();
}
}
public bool HandleCanFocusDown(
ScreenCoordinates pointerPosition,
[NotNullWhen(true)] out (Control control, Vector2i rel)? hitData)
{
var hit = MouseGetControlAndRel(pointerPosition);
var pos = pointerPosition.Position;
// If we have a modal open and the mouse down was outside it, close said modal.
for (var i = _modalStack.Count - 1; i >= 0; i--)
{
var top = _modalStack[i];
var offset = pos - top.GlobalPixelPosition;
if (!top.HasPoint(offset / top.UIScale))
{
if (top.MouseFilter != Control.MouseFilterMode.Stop)
{
if (top is not Popup {CloseOnClick: false})
{
RemoveModal(top);
}
}
else
{
ControlFocused = top;
hitData = null;
return false; // prevent anything besides the top modal control from receiving input
}
}
else
{
break;
}
}
if (hit == null)
{
ReleaseKeyboardFocus();
hitData = null;
return false;
}
var (control, rel) = hit.Value;
if (control != KeyboardFocused)
{
ReleaseKeyboardFocus();
}
ControlFocused = control;
if (ControlFocused.CanKeyboardFocus && ControlFocused.KeyboardFocusOnClick)
{
ControlFocused.GrabKeyboardFocus();
}
hitData = (control, (Vector2i) rel);
return true;
}
public void HandleCanFocusUp()
{
ControlFocused = null;
}
public ScreenCoordinates ScreenToUIPosition(ScreenCoordinates coordinates)
{
if (!_windowsToRoot.TryGetValue(coordinates.Window, out var root))
return default;
return new ScreenCoordinates(coordinates.Position / root.UIScale, coordinates.Window);
}
public ICursor? WorldCursor
{
get => _worldCursor;
set
{
_worldCursor = value;
_needUpdateActiveCursor = true;
}
}
private (Control control, Vector2 rel)? MouseGetControlAndRel(ScreenCoordinates coordinates)
{
if (!_windowsToRoot.TryGetValue(coordinates.Window, out var root))
return null;
return MouseFindControlAtPos(root, coordinates.Position);
}
public Control? MouseGetControl(ScreenCoordinates coordinates)
{
return MouseGetControlAndRel(coordinates)?.control;
}
public Control? GetSuppliedTooltipFor(Control control)
{
return CurrentlyHovered == control ? _suppliedTooltip : null;
}
/// <summary>
/// Converts
/// </summary>
/// <param name="args">Event data values for a bound key state change.</param>
private bool OnUIKeyBindStateChanged(BoundKeyEventArgs args)
{
if (args.State == BoundKeyState.Down)
{
KeyBindDown(args);
}
else
{
KeyBindUp(args);
}
// If we are in a focused control or doing a CanFocus, return true
// So that InputManager doesn't propagate events to simulation.
if (!args.CanFocus && KeyboardFocused != null)
{
return true;
}
return false;
}
/// <inheritdoc />
public void GrabKeyboardFocus(Control control)
{
if (control == null)
{
throw new ArgumentNullException(nameof(control));
}
if (!control.CanKeyboardFocus)
{
throw new ArgumentException("Control cannot get keyboard focus.", nameof(control));
}
if (control == KeyboardFocused)
{
return;
}
ReleaseKeyboardFocus();
KeyboardFocused = control;
KeyboardFocused.KeyboardFocusEntered();
}
public void ReleaseKeyboardFocus()
{
var oldFocused = KeyboardFocused;
oldFocused?.KeyboardFocusExited();
KeyboardFocused = null;
}
public void ReleaseKeyboardFocus(Control ifControl)
{
if (ifControl == null)
{
throw new ArgumentNullException(nameof(ifControl));
}
if (ifControl == KeyboardFocused)
{
ReleaseKeyboardFocus();
}
}
private void _resetTooltipTimer()
{
_tooltipTimer = 0;
}
private void _showTooltip()
{
if (showingTooltip) return;
showingTooltip = true;
var hovered = CurrentlyHovered;
if (hovered == null)
{
return;
}
// show supplied tooltip if there is one
if (hovered.TooltipSupplier != null)
{
_suppliedTooltip = hovered.TooltipSupplier.Invoke(hovered);
if (_suppliedTooltip != null)
{
PopupRoot.AddChild(_suppliedTooltip);
Tooltips.PositionTooltip(_suppliedTooltip);
}
}
else if (!String.IsNullOrWhiteSpace(hovered.ToolTip))
{
// show simple tooltip if there is one
_tooltip.Visible = true;
_tooltip.Text = hovered.ToolTip;
Tooltips.PositionTooltip(_tooltip);
}
hovered.PerformShowTooltip();
}
public Vector2? CalcRelativeMousePositionFor(Control control, ScreenCoordinates mousePosScaled)
{
var (pos, window) = mousePosScaled;
var root = control.Root;
if (root?.Window == null || root.Window.Id != window)
return null;
return pos - control.GlobalPosition;
}
}