Files
tbd-station-14/Content.Client/GameObjects/EntitySystems/DoAfter/DoAfterBar.cs
Paul Ritter 6602c8c972 Objectives (#2459)
* temp commit to save progress

* adds objectives

* refactors mind.addobjective a bit

* better names for my testobjectives which i'll remove later on anyways

* nullable errors

* some misc fixes

* no sorted or set, what was i thinking here?

* removes unused imports

* added commands

* fully implements stealcondition

* started uiwork

* moved prototypeicon to engine

* removes objective class & uiwork

* refactors ui to only update when opened
adds progresstexturerect

* adds some margin

* removes some testing code

* ignores objectiveprototypes on clientside

* fixes

* removes using statements for exp

* gets the job

* always show issuer

* locs & _

* giving commands some love

* Update Content.Client/GameObjects/EntitySystems/DoAfter/DoAfterBar.cs

Co-authored-by: Exp <theexp111@gmail.com>

* makes commands use new thingy

* string interpolation

* good catch exp

* loc'd

* linq gone

* runtime

* moves function from engine

* oopsie

* Update Content.Server/Objectives/Conditions/StealCondition.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* makes messages directed

* base call & validation

* shuffle once

* No? Money down!

Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
Co-authored-by: Exp <theexp111@gmail.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2020-11-22 18:38:07 +11:00

140 lines
3.7 KiB
C#

#nullable enable
using System;
using Robust.Client.Graphics.Drawing;
using Robust.Client.Graphics.Shaders;
using Robust.Client.UserInterface;
using Robust.Shared.Interfaces.Timing;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
namespace Content.Client.GameObjects.EntitySystems.DoAfter
{
public sealed class DoAfterBar : Control
{
private IGameTiming _gameTiming = default!;
private readonly ShaderInstance _shader;
/// <summary>
/// Set from 0.0f to 1.0f to reflect bar progress
/// </summary>
public float Ratio
{
get => _ratio;
set => _ratio = value;
}
private float _ratio = 1.0f;
/// <summary>
/// Flash red until removed
/// </summary>
public bool Cancelled
{
get => _cancelled;
set
{
if (_cancelled == value)
{
return;
}
_cancelled = value;
if (_cancelled)
{
_gameTiming = IoCManager.Resolve<IGameTiming>();
_lastFlash = _gameTiming.CurTime;
}
}
}
private bool _cancelled;
/// <summary>
/// Is the cancellation bar red?
/// </summary>
private bool _flash = true;
/// <summary>
/// Last time we swapped the flash.
/// </summary>
private TimeSpan _lastFlash;
/// <summary>
/// How long each cancellation bar flash lasts in seconds.
/// </summary>
private const float FlashTime = 0.125f;
private const int XPixelDiff = 20 * DoAfterBarScale;
public const byte DoAfterBarScale = 2;
public DoAfterBar()
{
IoCManager.InjectDependencies(this);
_shader = IoCManager.Resolve<IPrototypeManager>().Index<ShaderPrototype>("unshaded").Instance();
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (Cancelled)
{
if ((_gameTiming.CurTime - _lastFlash).TotalSeconds > FlashTime)
{
_lastFlash = _gameTiming.CurTime;
_flash = !_flash;
}
}
}
protected override void Draw(DrawingHandleScreen handle)
{
base.Draw(handle);
Color color;
if (Cancelled)
{
if ((_gameTiming.CurTime - _lastFlash).TotalSeconds > FlashTime)
{
_lastFlash = _gameTiming.CurTime;
_flash = !_flash;
}
color = new Color(1.0f, 0.0f, 0.0f, _flash ? 1.0f : 0.0f);
}
else
{
color = DoAfterHelpers.GetProgressColor(Ratio);
}
handle.UseShader(_shader);
// If you want to make this less hard-coded be my guest
var leftOffset = 2 * DoAfterBarScale;
var box = new UIBox2i(
leftOffset,
-2 + 2 * DoAfterBarScale,
leftOffset + (int) (XPixelDiff * Ratio),
-2);
handle.DrawRect(box, color);
}
}
public static class DoAfterHelpers
{
public static Color GetProgressColor(float progress)
{
if (progress >= 1.0f)
{
return new Color(0f, 1f, 0f);
}
// lerp
var hue = (5f / 18f) * progress;
return Color.FromHsv((hue, 1f, 0.75f, 1f));
}
}
}