| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- using System.Numerics;
- using Content.Client.IoC;
- using Content.Client.Items;
- using Content.Client.Resources;
- using Content.Client.Stylesheets;
- using Content.Client.Weapons.Ranged.Components;
- using Content.Client.Weapons.Ranged.ItemStatus;
- using Robust.Client.Animations;
- using Robust.Client.Graphics;
- using Robust.Client.UserInterface;
- using Robust.Client.UserInterface.Controls;
- namespace Content.Client.Weapons.Ranged.Systems;
- public sealed partial class GunSystem
- {
- private void OnAmmoCounterCollect(EntityUid uid, AmmoCounterComponent component, ItemStatusCollectMessage args)
- {
- RefreshControl(uid, component);
- if (component.Control != null)
- args.Controls.Add(component.Control);
- }
- /// <summary>
- /// Refreshes the control being used to show ammo. Useful if you change the AmmoProvider.
- /// </summary>
- /// <param name="uid"></param>
- /// <param name="component"></param>
- private void RefreshControl(EntityUid uid, AmmoCounterComponent? component = null)
- {
- if (!Resolve(uid, ref component, false))
- return;
- component.Control?.Dispose();
- component.Control = null;
- var ev = new AmmoCounterControlEvent();
- RaiseLocalEvent(uid, ev, false);
- // Fallback to default if none specified
- ev.Control ??= new DefaultStatusControl();
- component.Control = ev.Control;
- UpdateAmmoCount(uid, component);
- }
- private void UpdateAmmoCount(EntityUid uid, AmmoCounterComponent component)
- {
- if (component.Control == null)
- return;
- var ev = new UpdateAmmoCounterEvent()
- {
- Control = component.Control
- };
- RaiseLocalEvent(uid, ev, false);
- }
- protected override void UpdateAmmoCount(EntityUid uid, bool prediction = true)
- {
- // Don't use resolves because the method is shared and there's no compref and I'm trying to
- // share as much code as possible
- if (prediction && !Timing.IsFirstTimePredicted ||
- !TryComp<AmmoCounterComponent>(uid, out var clientComp))
- {
- return;
- }
- UpdateAmmoCount(uid, clientComp);
- }
- /// <summary>
- /// Raised when an ammocounter is requesting a control.
- /// </summary>
- public sealed class AmmoCounterControlEvent : EntityEventArgs
- {
- public Control? Control;
- }
- /// <summary>
- /// Raised whenever the ammo count / magazine for a control needs updating.
- /// </summary>
- public sealed class UpdateAmmoCounterEvent : HandledEntityEventArgs
- {
- public Control Control = default!;
- }
- #region Controls
- private sealed class DefaultStatusControl : Control
- {
- private readonly BulletRender _bulletRender;
- public DefaultStatusControl()
- {
- MinHeight = 15;
- HorizontalExpand = true;
- VerticalAlignment = VAlignment.Center;
- AddChild(_bulletRender = new BulletRender
- {
- HorizontalAlignment = HAlignment.Right,
- VerticalAlignment = VAlignment.Bottom
- });
- }
- public void Update(int count, int capacity)
- {
- _bulletRender.Count = count;
- _bulletRender.Capacity = capacity;
- _bulletRender.Type = capacity > 50 ? BulletRender.BulletType.Tiny : BulletRender.BulletType.Normal;
- }
- }
- public sealed class BoxesStatusControl : Control
- {
- private readonly BatteryBulletRenderer _bullets;
- private readonly Label _ammoCount;
- public BoxesStatusControl()
- {
- MinHeight = 15;
- HorizontalExpand = true;
- VerticalAlignment = Control.VAlignment.Center;
- AddChild(new BoxContainer
- {
- Orientation = BoxContainer.LayoutOrientation.Horizontal,
- Children =
- {
- (_bullets = new BatteryBulletRenderer
- {
- Margin = new Thickness(0, 0, 5, 0),
- HorizontalExpand = true
- }),
- (_ammoCount = new Label
- {
- StyleClasses = { StyleNano.StyleClassItemStatus },
- HorizontalAlignment = HAlignment.Right,
- VerticalAlignment = VAlignment.Bottom
- }),
- }
- });
- }
- public void Update(int count, int max)
- {
- _ammoCount.Visible = true;
- _ammoCount.Text = $"x{count:00}";
- _bullets.Capacity = max;
- _bullets.Count = count;
- }
- }
- private sealed class ChamberMagazineStatusControl : Control
- {
- private readonly BulletRender _bulletRender;
- private readonly TextureRect _chamberedBullet;
- private readonly Label _noMagazineLabel;
- private readonly Label _ammoCount;
- public ChamberMagazineStatusControl()
- {
- MinHeight = 15;
- HorizontalExpand = true;
- VerticalAlignment = Control.VAlignment.Center;
- AddChild(new BoxContainer
- {
- Orientation = BoxContainer.LayoutOrientation.Horizontal,
- HorizontalExpand = true,
- Children =
- {
- new Control
- {
- HorizontalExpand = true,
- Margin = new Thickness(0, 0, 5, 0),
- Children =
- {
- (_bulletRender = new BulletRender
- {
- HorizontalAlignment = HAlignment.Right,
- VerticalAlignment = VAlignment.Bottom
- }),
- (_noMagazineLabel = new Label
- {
- Text = "No Magazine!",
- StyleClasses = {StyleNano.StyleClassItemStatus}
- })
- }
- },
- new BoxContainer
- {
- Orientation = BoxContainer.LayoutOrientation.Vertical,
- VerticalAlignment = VAlignment.Bottom,
- Margin = new Thickness(0, 0, 0, 2),
- Children =
- {
- (_ammoCount = new Label
- {
- StyleClasses = {StyleNano.StyleClassItemStatus},
- HorizontalAlignment = HAlignment.Right,
- }),
- (_chamberedBullet = new TextureRect
- {
- Texture = StaticIoC.ResC.GetTexture("/Textures/Interface/ItemStatus/Bullets/chambered.png"),
- HorizontalAlignment = HAlignment.Left,
- }),
- }
- }
- }
- });
- }
- public void Update(bool chambered, bool magazine, int count, int capacity)
- {
- _chamberedBullet.ModulateSelfOverride =
- chambered ? Color.FromHex("#d7df60") : Color.Black;
- if (!magazine)
- {
- _bulletRender.Visible = false;
- _noMagazineLabel.Visible = true;
- _ammoCount.Visible = false;
- return;
- }
- _bulletRender.Visible = true;
- _noMagazineLabel.Visible = false;
- _ammoCount.Visible = true;
- _bulletRender.Count = count;
- _bulletRender.Capacity = capacity;
- _bulletRender.Type = capacity > 50 ? BulletRender.BulletType.Tiny : BulletRender.BulletType.Normal;
- _ammoCount.Text = $"x{count:00}";
- }
- public void PlayAlarmAnimation(Animation animation)
- {
- _noMagazineLabel.PlayAnimation(animation, "alarm");
- }
- }
- private sealed class RevolverStatusControl : Control
- {
- private readonly BoxContainer _bulletsList;
- public RevolverStatusControl()
- {
- MinHeight = 15;
- HorizontalExpand = true;
- VerticalAlignment = Control.VAlignment.Center;
- AddChild((_bulletsList = new BoxContainer
- {
- Orientation = BoxContainer.LayoutOrientation.Horizontal,
- HorizontalExpand = true,
- VerticalAlignment = VAlignment.Center,
- SeparationOverride = 0
- }));
- }
- public void Update(int currentIndex, bool?[] bullets)
- {
- _bulletsList.RemoveAllChildren();
- var capacity = bullets.Length;
- string texturePath;
- if (capacity <= 20)
- {
- texturePath = "/Textures/Interface/ItemStatus/Bullets/normal.png";
- }
- else if (capacity <= 30)
- {
- texturePath = "/Textures/Interface/ItemStatus/Bullets/small.png";
- }
- else
- {
- texturePath = "/Textures/Interface/ItemStatus/Bullets/tiny.png";
- }
- var texture = StaticIoC.ResC.GetTexture(texturePath);
- var spentTexture = StaticIoC.ResC.GetTexture("/Textures/Interface/ItemStatus/Bullets/empty.png");
- FillBulletRow(currentIndex, bullets, _bulletsList, texture, spentTexture);
- }
- private void FillBulletRow(int currentIndex, bool?[] bullets, Control container, Texture texture, Texture emptyTexture)
- {
- var capacity = bullets.Length;
- var colorA = Color.FromHex("#b68f0e");
- var colorB = Color.FromHex("#d7df60");
- var colorSpentA = Color.FromHex("#b50e25");
- var colorSpentB = Color.FromHex("#d3745f");
- var colorGoneA = Color.FromHex("#000000");
- var colorGoneB = Color.FromHex("#222222");
- var altColor = false;
- var scale = 1.3f;
- for (var i = 0; i < capacity; i++)
- {
- var bulletFree = bullets[i];
- // Add a outline
- var box = new Control()
- {
- MinSize = texture.Size * scale,
- };
- if (i == currentIndex)
- {
- box.AddChild(new TextureRect
- {
- Texture = texture,
- TextureScale = new Vector2(scale, scale),
- ModulateSelfOverride = Color.LimeGreen,
- });
- }
- Color color;
- Texture bulletTexture = texture;
- if (bulletFree.HasValue)
- {
- if (bulletFree.Value)
- {
- color = altColor ? colorA : colorB;
- }
- else
- {
- color = altColor ? colorSpentA : colorSpentB;
- bulletTexture = emptyTexture;
- }
- }
- else
- {
- color = altColor ? colorGoneA : colorGoneB;
- }
- box.AddChild(new TextureRect
- {
- Stretch = TextureRect.StretchMode.KeepCentered,
- Texture = bulletTexture,
- ModulateSelfOverride = color,
- });
- altColor ^= true;
- container.AddChild(box);
- }
- }
- }
- #endregion
- }
|