UI/Player/Weapons.razor
@using Sandbox
@using Sandbox.UI
@inherits Panel
<style>
Weapons {
position: absolute;
width: auto;
left: 50%;
top: 50%;
height: auto;
margin-left: 0px;
margin-top: 228px;
transition: opacity 0.5s ease;
justify-content: center;
align-items: center;
align-content: center;
transform: translate(-50%, -50%);
z-index: 7;
&.hidden {
opacity: 0;
pointer-events: none;
}
.list {
flex-direction: row;
justify-content: center;
align-items: center;
align-content: center;
gap: 14px;
.entry {
flex-direction: column;
justify-content: center;
align-items: center;
align-content: center;
position: relative;
min-width: 0;
padding: 4px;
gap: 0;
.icon {
width: 58px;
aspect-ratio: 1;
}
.glyph {
position: relative;
left: 0px;
top: 0px;
min-width: 42px;
height: 42px;
justify-content: center;
align-items: center;
}
&.active {
transition: opacity 0.1s ease;
.icon { width: 76px; aspect-ratio: 1; opacity: 0.98; }
}
&.inactive {
opacity: 0.125;
}
}
}
}
</style>
<root>
<div class="list">
@foreach ( var pair in (LocalPlayer.Pawn?.Weapons ?? Enumerable.Empty<Weapon>()).Select( (weapon, index) => (weapon, index) ) )
{
var weapon = pair.weapon;
var slot = pair.index + 1;
if ( weapon == null || !weapon.IsValid() ) continue;
<div class="entry @(weapon == LocalPlayer.Pawn?.ActiveWeapon ? "active" : "inactive")">
<img class="icon" [email protected] />
<InputHint Action="@GetSlotAction(slot)" class="glyph" />
</div>
}
</div>
</root>
@code
{
public override void Tick()
{
SetClass( "hidden", !(LocalPlayer.Pawn?.IsAlive ?? false) );
}
protected override int BuildHash()
{
var h = new HashCode();
var pawn = LocalPlayer.Pawn;
h.Add( pawn?.ActiveWeapon );
if ( pawn != null )
foreach ( var w in pawn.Weapons )
h.Add( w );
return h.ToHashCode();
}
private static string GetSlotAction( int slot ) => $"slot{Math.Clamp( slot, 1, 9 )}";
}