Files
wwdpublic/Content.Client/Power/PowerMonitoringWindow.xaml.cs
DEATHB4DEFEAT 47b10a01b0 Catch-Up Cherry Pick 2 (#944)
# Description

Picked 400 commits (and skipped many, many more) from WizDen since #540.
Stopped at commit 332f54a3aebe669f6e50d26e7b047f0bdc28e0fb (Lobby
Refactor).

---

# TODO

- [x] Pick
- [x] Compile
- [x] Fix runtime errors
- [ ] Fix up humanoid profile editor
- [ ] Test everything

---

# Changelog

🆑
- add: Merged 400 WizDen PRs. Happy testing!

---------

Co-authored-by: Plykiya <58439124+Plykiya@users.noreply.github.com>
Co-authored-by: FungiFellow <151778459+FungiFellow@users.noreply.github.com>
Co-authored-by: osjarw <62134478+osjarw@users.noreply.github.com>
Co-authored-by: Ubaser <134914314+UbaserB@users.noreply.github.com>
Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com>
Co-authored-by: Leon Friedrich <60421075+ElectroJr@users.noreply.github.com>
Co-authored-by: Magnus Larsen <i.am.larsenml@gmail.com>
Co-authored-by: Hanz <41141796+Hanzdegloker@users.noreply.github.com>
Co-authored-by: Kukutis96513 <146854220+Kukutis96513@users.noreply.github.com>
Co-authored-by: potato1234_x <79580518+potato1234x@users.noreply.github.com>
Co-authored-by: Gotimanga <127038462+Gotimanga@users.noreply.github.com>
Co-authored-by: Mangohydra <156087924+Mangohydra@users.noreply.github.com>
Co-authored-by: TsjipTsjip <19798667+TsjipTsjip@users.noreply.github.com>
Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com>
Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com>
Co-authored-by: Morb <14136326+Morb0@users.noreply.github.com>
Co-authored-by: MilenVolf <63782763+MilenVolf@users.noreply.github.com>
Co-authored-by: KrasnoshchekovPavel <119816022+KrasnoshchekovPavel@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com>
Co-authored-by: DrSmugleaf <10968691+DrSmugleaf@users.noreply.github.com>
Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com>
Co-authored-by: KittenColony <149278380+KittenColony@users.noreply.github.com>
Co-authored-by: ShadowCommander <shadowjjt@gmail.com>
Co-authored-by: Mr. 27 <45323883+Dutch-VanDerLinde@users.noreply.github.com>
Co-authored-by: T-Stalker <43253663+DogZeroX@users.noreply.github.com>
Co-authored-by: ERROR404 <100093430+ERORR404V1@users.noreply.github.com>
Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com>
Co-authored-by: Jezithyr <jezithyr@gmail.com>
Co-authored-by: Psychpsyo <60073468+Psychpsyo@users.noreply.github.com>
Co-authored-by: no <165581243+pissdemon@users.noreply.github.com>
Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com>
Co-authored-by: Ciac32 <aknoxlor@gmail.com>
Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com>
Co-authored-by: NotSoDana <75203942+NotSoDana@users.noreply.github.com>
Co-authored-by: Simon <63975668+Simyon264@users.noreply.github.com>
Co-authored-by: Repo <47093363+Titian3@users.noreply.github.com>
Co-authored-by: Tayrtahn <tayrtahn@gmail.com>
Co-authored-by: nao fujiwara <awkwarddryad@gmail.com>
Co-authored-by: Michael <107807667+Doc-Michael@users.noreply.github.com>
Co-authored-by: Vasilis <vasilis@pikachu.systems>
Co-authored-by: Lamrr <96937466+Lamrr@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Co-authored-by: Jay <67732946+DuskyJay@users.noreply.github.com>
Co-authored-by: Just-a-Unity-Dev <67359748+Just-a-Unity-Dev@users.noreply.github.com>
Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com>
Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com>
Co-authored-by: Tyzemol <85772526+Tyzemol@users.noreply.github.com>
Co-authored-by: Alzore <140123969+Blackern5000@users.noreply.github.com>
Co-authored-by: Pok <113675512+Pok27@users.noreply.github.com>
Co-authored-by: RumiTiger <154005209+RumiTiger@users.noreply.github.com>
Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com>
Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
Co-authored-by: Killerqu00 <47712032+Killerqu00@users.noreply.github.com>
Co-authored-by: Ty Ashley <42426760+TyAshley@users.noreply.github.com>
Co-authored-by: exincore <me@exin.xyz>
Co-authored-by: 0x6273 <0x40@keemail.me>
Co-authored-by: Kara <lunarautomaton6@gmail.com>
Co-authored-by: Ygg01 <y.laughing.man.y@gmail.com>
Co-authored-by: Łukasz Mędrek <lukasz@lukaszm.xyz>
Co-authored-by: Hannah Giovanna Dawson <karakkaraz@gmail.com>
Co-authored-by: TurboTracker <130304754+TurboTrackerss14@users.noreply.github.com>
Co-authored-by: OnsenCapy <101037138+LGRuthes@users.noreply.github.com>
Co-authored-by: pigeonpeas <147350443+pigeonpeas@users.noreply.github.com>
Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com>
Co-authored-by: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com>
Co-authored-by: Rio <110139251+Riolume@users.noreply.github.com>
Co-authored-by: vorkathbruh <152932728+vorkathbruh@users.noreply.github.com>
Co-authored-by: Sphiral <145869023+SphiraI@users.noreply.github.com>
Co-authored-by: PrPleGoo <PrPleGoo@users.noreply.github.com>
Co-authored-by: Moomoobeef <62638182+Moomoobeef@users.noreply.github.com>
Co-authored-by: username <113782077+whateverusername0@users.noreply.github.com>
Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com>
Co-authored-by: Джексон Миссиссиппи <tripwiregamer@gmail.com>
Co-authored-by: Brandon Li <48413902+aspiringLich@users.noreply.github.com>
Co-authored-by: Jajsha <101492056+Zap527@users.noreply.github.com>
Co-authored-by: RiceMar1244 <138547931+RiceMar1244@users.noreply.github.com>
Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com>
Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com>
Co-authored-by: ike709 <ike709@users.noreply.github.com>
Co-authored-by: icekot8 <93311212+icekot8@users.noreply.github.com>
Co-authored-by: keronshb <54602815+keronshb@users.noreply.github.com>
Co-authored-by: VMSolidus <evilexecutive@gmail.com>
Co-authored-by: Geekyhobo <66805063+Geekyhobo@users.noreply.github.com>
Co-authored-by: FoxxoTrystan <45297731+FoxxoTrystan@users.noreply.github.com>
# Conflicts:
#	Content.Client/Input/ContentContexts.cs
#	Content.Client/Lobby/LobbyState.cs
#	Content.Client/Lobby/UI/HumanoidProfileEditor.xaml
#	Content.Client/Lobby/UI/LobbyGui.xaml
#	Content.Client/Lobby/UI/LobbyGui.xaml.cs
#	Content.Client/Preferences/UI/CharacterSetupGui.xaml.cs
#	Content.Client/UserInterface/Systems/MenuBar/Widgets/GameTopMenuBar.xaml
#	Content.Server/Disposal/Unit/EntitySystems/DisposalUnitSystem.cs
#	Content.Server/Fluids/EntitySystems/PuddleSystem.Spillable.cs
#	Content.Server/GameTicking/GameTicker.Spawning.cs
#	Content.Shared/Alert/AlertType.cs
#	Content.Shared/Input/ContentKeyFunctions.cs
#	Content.Shared/Preferences/HumanoidCharacterProfile.cs
#	Content.Shared/Weapons/Melee/MeleeWeaponComponent.cs
#	Resources/ConfigPresets/EinsteinEngines/default.toml
#	Resources/Prototypes/Alerts/alerts.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/coats.yml
#	Resources/Prototypes/Entities/Clothing/OuterClothing/hardsuits.yml
#	Resources/Prototypes/Entities/Clothing/Uniforms/jumpskirts.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/e_sword.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Melee/sword.yml
#	Resources/Prototypes/Recipes/Crafting/Graphs/improvised/makeshiftstunprod.yml
#	Resources/Prototypes/Voice/speech_emotes.yml
#	Resources/keybinds.yml
2024-10-19 14:53:37 +07:00

315 lines
12 KiB
C#

using Content.Client.Pinpointer.UI;
using Content.Client.UserInterface.Controls;
using Content.Shared.Power;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Map;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using System.Linq;
namespace Content.Client.Power;
[GenerateTypedNameReferences]
public sealed partial class PowerMonitoringWindow : FancyWindow
{
private readonly IEntityManager _entManager;
private readonly SpriteSystem _spriteSystem;
private readonly IGameTiming _gameTiming;
private const float BlinkFrequency = 1f;
private EntityUid? _owner;
private NetEntity? _focusEntity;
public event Action<NetEntity?, PowerMonitoringConsoleGroup>? SendPowerMonitoringConsoleMessageAction;
private Dictionary<PowerMonitoringConsoleGroup, (SpriteSpecifier.Texture, Color)> _groupBlips = new()
{
{ PowerMonitoringConsoleGroup.Generator, (new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_circle.png")), Color.Purple) },
{ PowerMonitoringConsoleGroup.SMES, (new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_hexagon.png")), Color.OrangeRed) },
{ PowerMonitoringConsoleGroup.Substation, (new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_square.png")), Color.Yellow) },
{ PowerMonitoringConsoleGroup.APC, (new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_triangle.png")), Color.LimeGreen) },
};
public PowerMonitoringWindow(PowerMonitoringConsoleBoundUserInterface userInterface, EntityUid? owner)
{
RobustXamlLoader.Load(this);
_entManager = IoCManager.Resolve<IEntityManager>();
_gameTiming = IoCManager.Resolve<IGameTiming>();
_spriteSystem = _entManager.System<SpriteSystem>();
_owner = owner;
// Pass owner to nav map
NavMap.Owner = _owner;
// Set nav map grid uid
var stationName = Loc.GetString("power-monitoring-window-unknown-location");
if (_entManager.TryGetComponent<TransformComponent>(owner, out var xform))
{
NavMap.MapUid = xform.GridUid;
// Assign station name
if (_entManager.TryGetComponent<MetaDataComponent>(xform.GridUid, out var stationMetaData))
stationName = stationMetaData.EntityName;
var msg = new FormattedMessage();
msg.AddMarkup(Loc.GetString("power-monitoring-window-station-name", ("stationName", stationName)));
StationName.SetMessage(msg);
}
else
{
StationName.SetMessage(stationName);
NavMap.Visible = false;
}
// Set trackable entity selected action
NavMap.TrackedEntitySelectedAction += SetTrackedEntityFromNavMap;
// Update nav map
NavMap.ForceNavMapUpdate();
// Set UI tab titles
MasterTabContainer.SetTabTitle(0, Loc.GetString("power-monitoring-window-label-sources"));
MasterTabContainer.SetTabTitle(1, Loc.GetString("power-monitoring-window-label-smes"));
MasterTabContainer.SetTabTitle(2, Loc.GetString("power-monitoring-window-label-substation"));
MasterTabContainer.SetTabTitle(3, Loc.GetString("power-monitoring-window-label-apc"));
// Track when the MasterTabContainer changes its tab
MasterTabContainer.OnTabChanged += OnTabChanged;
// Set UI toggles
ShowHVCable.OnToggled += _ => OnShowCableToggled(PowerMonitoringConsoleLineGroup.HighVoltage);
ShowMVCable.OnToggled += _ => OnShowCableToggled(PowerMonitoringConsoleLineGroup.MediumVoltage);
ShowLVCable.OnToggled += _ => OnShowCableToggled(PowerMonitoringConsoleLineGroup.Apc);
// Set power monitoring message action
SendPowerMonitoringConsoleMessageAction += userInterface.SendPowerMonitoringConsoleMessage;
}
private void OnTabChanged(int tab)
{
SendPowerMonitoringConsoleMessageAction?.Invoke(_focusEntity, (PowerMonitoringConsoleGroup) tab);
}
private void OnShowCableToggled(PowerMonitoringConsoleLineGroup lineGroup)
{
if (!NavMap.HiddenLineGroups.Remove(lineGroup))
NavMap.HiddenLineGroups.Add(lineGroup);
}
public void ShowEntites
(double totalSources,
double totalBatteryUsage,
double totalLoads,
PowerMonitoringConsoleEntry[] allEntries,
PowerMonitoringConsoleEntry[] focusSources,
PowerMonitoringConsoleEntry[] focusLoads,
EntityCoordinates? monitorCoords)
{
if (_owner == null)
return;
if (!_entManager.TryGetComponent<PowerMonitoringConsoleComponent>(_owner.Value, out var console))
return;
// Update power status text
TotalSources.Text = Loc.GetString("power-monitoring-window-value", ("value", totalSources));
TotalBatteryUsage.Text = Loc.GetString("power-monitoring-window-value", ("value", totalBatteryUsage));
TotalLoads.Text = Loc.GetString("power-monitoring-window-value", ("value", totalLoads));
// 10+% of station power is being drawn from batteries
TotalBatteryUsage.FontColorOverride = (totalSources * 0.1111f) < totalBatteryUsage ? new Color(180, 0, 0) : Color.White;
// Station generator and battery output is less than the current demand
TotalLoads.FontColorOverride = (totalSources + totalBatteryUsage) < totalLoads &&
!MathHelper.CloseToPercent(totalSources + totalBatteryUsage, totalLoads, 0.1f) ? new Color(180, 0, 0) : Color.White;
// Update system warnings
UpdateWarningLabel(console.Flags);
// Reset nav map values
NavMap.TrackedCoordinates.Clear();
NavMap.TrackedEntities.Clear();
// Draw entities on the nav map
var entitiesOfInterest = new List<NetEntity>();
if (_focusEntity != null)
{
entitiesOfInterest.Add(_focusEntity.Value);
foreach (var entry in focusSources)
entitiesOfInterest.Add(entry.NetEntity);
foreach (var entry in focusLoads)
entitiesOfInterest.Add(entry.NetEntity);
}
focusSources.Concat(focusLoads);
foreach ((var netEntity, var metaData) in console.PowerMonitoringDeviceMetaData)
{
if (NavMap.Visible)
AddTrackedEntityToNavMap(netEntity, metaData, entitiesOfInterest);
}
// Show monitor location
var mon = _entManager.GetNetEntity(_owner);
if (monitorCoords != null && mon != null)
{
var texture = _spriteSystem.Frame0(new SpriteSpecifier.Texture(new ResPath("/Textures/Interface/NavMap/beveled_circle.png")));
var blip = new NavMapBlip(monitorCoords.Value, texture, Color.Cyan, true, false);
NavMap.TrackedEntities[mon.Value] = blip;
}
// If the entry group doesn't match the current tab, the data is out dated, do not use it
if (allEntries.Length > 0 && allEntries[0].Group != GetCurrentPowerMonitoringConsoleGroup())
return;
// Assign meta data to the console entries and sort them
allEntries = GetUpdatedPowerMonitoringConsoleEntries(allEntries, console);
focusSources = GetUpdatedPowerMonitoringConsoleEntries(focusSources, console);
focusLoads = GetUpdatedPowerMonitoringConsoleEntries(focusLoads, console);
// Get current console entry container
BoxContainer currentContainer = SourcesList;
switch (GetCurrentPowerMonitoringConsoleGroup())
{
case PowerMonitoringConsoleGroup.SMES:
currentContainer = SMESList; break;
case PowerMonitoringConsoleGroup.Substation:
currentContainer = SubstationList; break;
case PowerMonitoringConsoleGroup.APC:
currentContainer = ApcList; break;
}
// Clear excess children from the container
while (currentContainer.ChildCount > allEntries.Length)
currentContainer.RemoveChild(currentContainer.GetChild(currentContainer.ChildCount - 1));
// Update the remaining children
for (var index = 0; index < allEntries.Length; index++)
{
var entry = allEntries[index];
if (entry.NetEntity == _focusEntity)
UpdateWindowConsoleEntry(currentContainer, index, entry, focusSources, focusLoads);
else
UpdateWindowConsoleEntry(currentContainer, index, entry);
}
// Auto-scroll renable
if (_autoScrollAwaitsUpdate)
{
_autoScrollActive = true;
_autoScrollAwaitsUpdate = false;
}
}
private void AddTrackedEntityToNavMap(NetEntity netEntity, PowerMonitoringDeviceMetaData metaData, List<NetEntity> entitiesOfInterest)
{
if (!_groupBlips.TryGetValue(metaData.Group, out var data))
return;
var usedEntity = (metaData.CollectionMaster != null) ? metaData.CollectionMaster : netEntity;
var coords = _entManager.GetCoordinates(metaData.Coordinates);
var texture = data.Item1;
var color = data.Item2;
var blink = usedEntity == _focusEntity;
var modulator = Color.White;
if (_focusEntity != null && usedEntity != _focusEntity && !entitiesOfInterest.Contains(usedEntity.Value))
modulator = Color.DimGray;
var blip = new NavMapBlip(coords, _spriteSystem.Frame0(texture), color * modulator, blink);
NavMap.TrackedEntities[netEntity] = blip;
}
private void SetTrackedEntityFromNavMap(NetEntity? netEntity)
{
if (netEntity == null)
return;
if (!_entManager.TryGetComponent<PowerMonitoringConsoleComponent>(_owner, out var console))
return;
if (!console.PowerMonitoringDeviceMetaData.TryGetValue(netEntity.Value, out var metaData))
return;
// Switch entity for master, if applicable
// The master will always be in the same group as the entity
if (metaData.CollectionMaster != null)
netEntity = metaData.CollectionMaster;
_focusEntity = netEntity;
// Switch tabs
SwitchTabsBasedOnPowerMonitoringConsoleGroup(metaData.Group);
// Get the scroll position of the selected entity on the selected button the UI
ActivateAutoScrollToFocus();
// Send message to console that the focus has changed
SendPowerMonitoringConsoleMessageAction?.Invoke(_focusEntity, metaData.Group);
}
protected override void FrameUpdate(FrameEventArgs args)
{
AutoScrollToFocus();
// Warning sign pulse
var lit = _gameTiming.RealTime.TotalSeconds % BlinkFrequency > BlinkFrequency / 2f;
SystemWarningPanel.Modulate = lit ? Color.White : new Color(178, 178, 178);
}
private PowerMonitoringConsoleEntry[] GetUpdatedPowerMonitoringConsoleEntries(PowerMonitoringConsoleEntry[] entries, PowerMonitoringConsoleComponent console)
{
for (int i = 0; i < entries.Length; i++)
{
var entry = entries[i];
if (!console.PowerMonitoringDeviceMetaData.TryGetValue(entry.NetEntity, out var metaData))
continue;
entries[i].MetaData = metaData;
}
// Sort all devices alphabetically by their entity name (not by power usage; otherwise their position on the UI will shift)
Array.Sort(entries, AlphabeticalSort);
return entries;
}
private int AlphabeticalSort(PowerMonitoringConsoleEntry x, PowerMonitoringConsoleEntry y)
{
if (x.MetaData?.EntityName == null)
return -1;
if (y.MetaData?.EntityName == null)
return 1;
return x.MetaData.Value.EntityName.CompareTo(y.MetaData.Value.EntityName);
}
}
public struct PowerMonitoringConsoleTrackable
{
public EntityUid EntityUid;
public PowerMonitoringConsoleGroup Group;
public PowerMonitoringConsoleTrackable(EntityUid uid, PowerMonitoringConsoleGroup group)
{
EntityUid = uid;
Group = group;
}
}