VotePopup.xaml.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using Content.Client.Stylesheets;
  3. using Content.Shared.Ghost;
  4. using Robust.Client.AutoGenerated;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.Controls;
  7. using Robust.Client.UserInterface.XAML;
  8. using Robust.Shared.Timing;
  9. using Robust.Shared.Utility;
  10. namespace Content.Client.Voting.UI
  11. {
  12. [GenerateTypedNameReferences]
  13. public sealed partial class VotePopup : Control
  14. {
  15. [Dependency] private readonly IGameTiming _gameTiming = default!;
  16. [Dependency] private readonly IVoteManager _voteManager = default!;
  17. [Dependency] private readonly IEntityNetworkManager _net = default!;
  18. private readonly VoteManager.ActiveVote _vote;
  19. private readonly Button[] _voteButtons;
  20. private readonly NetEntity? _targetEntity;
  21. public VotePopup(VoteManager.ActiveVote vote)
  22. {
  23. _vote = vote;
  24. IoCManager.InjectDependencies(this);
  25. RobustXamlLoader.Load(this);
  26. Stylesheet = IoCManager.Resolve<IStylesheetManager>().SheetSpace;
  27. if (_vote.TargetEntity != null && _vote.TargetEntity != 0)
  28. {
  29. _targetEntity = new NetEntity(_vote.TargetEntity.Value);
  30. FollowVoteTarget.Visible = true;
  31. FollowVoteTarget.OnPressed += _ => AttemptFollowVoteEntity();
  32. }
  33. Modulate = Color.White.WithAlpha(0.75f);
  34. _voteButtons = new Button[vote.Entries.Length];
  35. var group = new ButtonGroup();
  36. for (var i = 0; i < _voteButtons.Length; i++)
  37. {
  38. var button = new Button
  39. {
  40. ToggleMode = true,
  41. Group = group
  42. };
  43. _voteButtons[i] = button;
  44. VoteOptionsContainer.AddChild(button);
  45. var i1 = i;
  46. button.OnPressed += _ => _voteManager.SendCastVote(vote.Id, i1);
  47. }
  48. }
  49. public void UpdateData()
  50. {
  51. VoteTitle.SetMessage(FormattedMessage.FromUnformatted(_vote.Title));
  52. VoteCaller.Text = Loc.GetString("ui-vote-created", ("initiator", _vote.Initiator));
  53. for (var i = 0; i < _voteButtons.Length; i++)
  54. {
  55. var entry = _vote.Entries[i];
  56. if (_vote.DisplayVotes)
  57. {
  58. _voteButtons[i].Text = Loc.GetString("ui-vote-button", ("text", entry.Text), ("votes", entry.Votes));
  59. }
  60. else
  61. {
  62. _voteButtons[i].Text = Loc.GetString("ui-vote-button-no-votes", ("text", entry.Text));
  63. }
  64. if (_vote.OurVote == i)
  65. _voteButtons[i].Pressed = true;
  66. }
  67. }
  68. private void AttemptFollowVoteEntity()
  69. {
  70. if (_targetEntity != null)
  71. {
  72. var msg = new GhostWarpToTargetRequestEvent(_targetEntity.Value);
  73. _net.SendSystemNetworkMessage(msg);
  74. }
  75. }
  76. protected override void FrameUpdate(FrameEventArgs args)
  77. {
  78. // Logger.Debug($"{_gameTiming.ServerTime}, {_vote.StartTime}, {_vote.EndTime}");
  79. var curTime = _gameTiming.RealTime;
  80. var timeLeft = _vote.EndTime - curTime;
  81. if (timeLeft < TimeSpan.Zero)
  82. timeLeft = TimeSpan.Zero;
  83. // Round up a second.
  84. timeLeft = TimeSpan.FromSeconds(Math.Ceiling(timeLeft.TotalSeconds));
  85. TimeLeftBar.Value = Math.Min(1, (float) ((curTime.TotalSeconds - _vote.StartTime.TotalSeconds) /
  86. (_vote.EndTime.TotalSeconds - _vote.StartTime.TotalSeconds)));
  87. TimeLeftText.Text = $"{timeLeft:m\\:ss}";
  88. }
  89. }
  90. }