Files
RobustToolbox/Robust.Client/UserInterface/Controls/ScrollBar.cs
ShadowCommander a8d6c294ab Input System Refactor (#840)
* Fixes right click menu stopping input

Removed a flag change when the modal stack was showing, which preventing the click from getting passed to the next function.

* Added parsing for mod2 and mod3 from Keybind YAML

* Fixes a crash on context change when a keybind is not defined in keybinds.yml

* Implemented ShowDebugConsole Hotkey

* Refactored input system

Refactored input system to run Key and Mouse input through the InputManager before doing stuff.

* Upgraded LineEdit and classes that use it. Fixed input while KeyboardFocused.

Upgraded LineEdit to use Keybinds.
Upgraded DebugConsole to use Keybinds.
Replaced all references to MouseDown, MouseUp, KeyDown, and KeyUp with KeyBindUp and KeyBindDown.
Moved UserInterfaceManager call from GameController.Input to InputManager event.
Stopped input going to simulation while a control has focus in UserInterfaceManager.

* Some fixes for input system

Fixed keybinds getting stuck when selecting a LineEdit.
Changed MenuBar to not error.
Fixed a few cases where LineEdit would eat input if you hovered over it and where mouse input got eaten when clicking in the world while a LineEdit was selected.

* Removed extra dependencies

* Added GUIBoundKeyEventArgs to ButtonEventArgs

* Fixes for input with LineEdit selected

Fixed multiple keybinds mapped to the same key not triggering.
Fixed keybind input not getting to LineEdit when hovering over a control.

* Implemented Key Repeat for LineEdit

* Fix for input on Robust.Lite Launcher

* Renames NonFocusKeybinds to EnableAllKeybinds

Renamed NonFocusKeybinds to EnableAllKeybinds and added comment to clarify usage

* Adds repeating keybinds

Used for TextBackspace, TextCursorLeft, and TextCursorRight
Reverts a change to LineEdit that implemented repeating keys
Adds some documentation comments
2019-08-21 17:13:48 +02:00

186 lines
4.9 KiB
C#

using System;
using JetBrains.Annotations;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Input;
using Robust.Shared.Input;
using Robust.Shared.Maths;
namespace Robust.Client.UserInterface.Controls
{
public abstract class ScrollBar : Range
{
public const string StylePropertyGrabber = "grabber";
public const string StylePseudoClassHover = "hover";
public const string StylePseudoClassGrabbed = "grabbed";
private readonly OrientationMode _orientation;
private bool _isHovered;
private (Vector2 pos, float value)? _grabData;
protected ScrollBar(OrientationMode orientation)
{
_orientation = orientation;
MouseFilter = MouseFilterMode.Pass;
}
public bool IsAtEnd
{
get
{
var offset = Value + Page;
return offset > MaxValue || FloatMath.CloseTo(offset, MaxValue);
}
}
public void MoveToEnd()
{
// Will be clamped as necessary.
Value = MaxValue;
}
protected internal override void Draw(DrawingHandleScreen handle)
{
var styleBox = _getGrabberStyleBox();
styleBox?.Draw(handle, _getGrabberBox());
}
protected internal override void MouseExited()
{
base.MouseExited();
_isHovered = false;
_updatePseudoClass();
}
protected internal override void KeyBindDown(GUIBoundKeyEventArgs args)
{
base.KeyBindDown(args);
if (args.Function != EngineKeyFunctions.Use)
{
return;
}
var box = _getGrabberBox();
if (!box.Contains(args.RelativePosition))
{
return;
}
_grabData = (args.RelativePosition, Value);
_updatePseudoClass();
}
protected internal override void KeyBindUp(GUIBoundKeyEventArgs args)
{
base.KeyBindUp(args);
if (args.Function != EngineKeyFunctions.Use)
{
return;
}
_grabData = null;
_updatePseudoClass();
}
protected internal override void MouseMove(GUIMouseMoveEventArgs args)
{
if (_grabData == null)
{
var box = _getGrabberBox();
_isHovered = box.Contains(args.RelativePosition);
_updatePseudoClass();
return;
}
var (grabPos, grabValue) = _grabData.Value;
var (grabRelX, grabRelY) = args.RelativePosition - grabPos;
float moved;
if (_orientation == OrientationMode.Horizontal)
{
moved = grabRelX;
}
else
{
moved = grabRelY;
}
var movedValue = moved / _getOrientationSize();
movedValue *= MaxValue - MinValue;
movedValue += MinValue + grabValue;
Value = movedValue;
}
[System.Diagnostics.Contracts.Pure]
private UIBox2 _getGrabberBox()
{
var grabberOffset = GetAsRatio() * _getOrientationSize();
grabberOffset = (float) Math.Round(grabberOffset);
var grabberEnd = (Value + Page - MinValue) / (MaxValue - MinValue) * _getOrientationSize();
grabberEnd = (float) Math.Round(grabberEnd);
if (_orientation == OrientationMode.Horizontal)
{
return new UIBox2(grabberOffset, 0, grabberEnd, PixelHeight);
}
return new UIBox2(0, grabberOffset, PixelWidth, grabberEnd);
}
[System.Diagnostics.Contracts.Pure]
[CanBeNull]
private StyleBox _getGrabberStyleBox()
{
if (TryGetStyleProperty(StylePropertyGrabber, out StyleBox styleBox))
{
return styleBox;
}
return null;
}
[System.Diagnostics.Contracts.Pure]
private float _getOrientationSize()
{
if (_orientation == OrientationMode.Horizontal)
{
return PixelWidth;
}
return PixelHeight;
}
private void _updatePseudoClass()
{
if (_grabData != null)
{
SetOnlyStylePseudoClass(StylePseudoClassGrabbed);
}
else if (_isHovered)
{
SetOnlyStylePseudoClass(StylePseudoClassHover);
}
else
{
SetOnlyStylePseudoClass(null);
}
}
protected override Vector2 CalculateMinimumSize()
{
return _getGrabberStyleBox()?.MinimumSize ?? Vector2.Zero;
}
protected enum OrientationMode
{
Horizontal,
Vertical
}
}
}