Files
RobustToolbox/Robust.Client/UserInterface/Controls/TextureButton.cs
T
Pieter-Jan BriersandGitHub 17ebeac107 UI Scaling Support. (#809)
Not quite perfect, but quite usable.

Adds the ability to scale the UI. Scaling is controlled via a cvar,
and can be changed at runtime.
Fractional scaling is supported.

Some controls could be better (SpriteView, TextureRect),
but for now it's a good start.
2019-05-11 16:04:14 +02:00

107 lines
3.3 KiB
C#

using System;
using Robust.Client.Graphics;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Interfaces.ResourceManagement;
using Robust.Client.ResourceManagement;
using Robust.Client.Utility;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
namespace Robust.Client.UserInterface.Controls
{
[ControlWrap("TextureButton")]
public class TextureButton : BaseButton
{
public const string StylePropertyTexture = "texture";
public const string StylePseudoClassNormal = "normal";
public const string StylePseudoClassHover = "hover";
public const string StylePseudoClassDisabled = "disabled";
public const string StylePseudoClassPressed = "pressed";
public TextureButton()
{
}
public TextureButton(string name) : base(name)
{
}
public Texture TextureNormal { get; set; }
public Texture TextureHover { get; set; }
protected override void Initialize()
{
base.Initialize();
DrawModeChanged();
}
protected override void DrawModeChanged()
{
switch (DrawMode)
{
case DrawModeEnum.Normal:
StylePseudoClass = StylePseudoClassNormal;
break;
case DrawModeEnum.Pressed:
StylePseudoClass = StylePseudoClassPressed;
break;
case DrawModeEnum.Hover:
StylePseudoClass = StylePseudoClassHover;
break;
case DrawModeEnum.Disabled:
StylePseudoClass = StylePseudoClassDisabled;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
protected internal override void Draw(DrawingHandleScreen handle)
{
var texture = TextureNormal;
if (IsHovered && TextureHover != null)
{
texture = TextureHover;
}
if (texture == null)
{
TryGetStyleProperty(StylePropertyTexture, out texture);
if (texture == null)
{
return;
}
}
handle.DrawTextureRect(texture, PixelSizeBox, false);
}
private protected override void SetGodotProperty(string property, object value, GodotAssetScene context)
{
base.SetGodotProperty(property, value, context);
if (property == "texture_normal")
{
var extRef = context.GetExtResource((GodotAsset.TokenExtResource) value);
ResourcePath godotPathToResourcePath;
try
{
godotPathToResourcePath = GodotPathUtility.GodotPathToResourcePath(extRef.Path);
}
catch (ArgumentException)
{
Logger.Error("TextureButton is referencing non-VFS Godot path {0}.", extRef.Path);
return;
}
var texture = IoCManager.Resolve<IResourceCache>()
.GetResource<TextureResource>(godotPathToResourcePath);
TextureNormal = texture;
}
}
}
}