StampLabel.xaml.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Numerics;
  2. using Robust.Client.AutoGenerated;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.XAML;
  6. using Robust.Shared.Prototypes;
  7. namespace Content.Client.Paper.UI;
  8. [GenerateTypedNameReferences]
  9. public sealed partial class StampLabel : Label
  10. {
  11. /// A scale that's applied to the text to ensure it
  12. /// fits in the allowed space.
  13. private Vector2 _textScaling = Vector2.One;
  14. /// Shader used to draw the stamps
  15. private ShaderInstance? _stampShader;
  16. /// Allows an additional orientation to be applied to
  17. /// this control.
  18. public float Orientation = 0.0f;
  19. public StampLabel()
  20. {
  21. RobustXamlLoader.Load(this);
  22. var prototypes = IoCManager.Resolve<IPrototypeManager>();
  23. _stampShader = prototypes.Index<ShaderPrototype>("PaperStamp").InstanceUnique();
  24. }
  25. protected override Vector2 MeasureOverride(Vector2 availableSize)
  26. {
  27. var desiredTextSize = base.MeasureOverride(availableSize);
  28. var clampedScale = Vector2.Min(availableSize / desiredTextSize, Vector2.One);
  29. var keepAspectRatio = MathF.Min(clampedScale.X, clampedScale.Y);
  30. const float shimmerReduction = 0.1f;
  31. _textScaling = Vector2.One * MathF.Round(keepAspectRatio / shimmerReduction) * shimmerReduction;
  32. return desiredTextSize;
  33. }
  34. protected override void Draw(DrawingHandleScreen handle)
  35. {
  36. var offset = new Vector2(PixelPosition.X * MathF.Cos(Orientation) - PixelPosition.Y * MathF.Sin(Orientation),
  37. PixelPosition.Y * MathF.Cos(Orientation) + PixelPosition.X * MathF.Sin(Orientation));
  38. _stampShader?.SetParameter("objCoord", GlobalPosition * UIScale * new Vector2(1, -1));
  39. handle.UseShader(_stampShader);
  40. handle.SetTransform(GlobalPixelPosition - PixelPosition + offset, Orientation, _textScaling);
  41. base.Draw(handle);
  42. // Restore a sane transform+shader
  43. handle.SetTransform(Matrix3x2.Identity);
  44. handle.UseShader(null);
  45. }
  46. }