Mapping Actions (#6877)

This commit is contained in:
Leon Friedrich
2022-03-02 12:12:34 +13:00
committed by GitHub
parent c3e118bf19
commit 36fcca8337
7 changed files with 1335 additions and 7 deletions

View File

@@ -601,6 +601,7 @@ namespace Content.Client.Actions
fillEvent.Action.ClientExclusive = true;
fillEvent.Action.Temporary = true;
fillEvent.Action.AutoPopulate = false;
Ui.Component.Actions.Add(fillEvent.Action);
Assignments.AssignSlot(hotbar, index, fillEvent.Action);
@@ -608,11 +609,9 @@ namespace Content.Client.Actions
Ui.UpdateUI();
}
public void SaveActionAssignments(string path)
/*public void SaveActionAssignments(string path)
{
// Disabled until YamlMappingFix's sandbox issues are resolved.
/*
// Currently only tested with temporary innate actions (i.e., mapping actions). No guarantee it works with
// other actions. If its meant to be used for full game state saving/loading, the entity that provides
// actions needs to keep the same uid.
@@ -630,8 +629,7 @@ namespace Content.Client.Actions
using var writer = _resourceManager.UserData.OpenWriteText(new ResourcePath(path).ToRootedPath());
var stream = new YamlStream { new(sequence.ToSequenceNode()) };
stream.Save(new YamlMappingFix(new Emitter(writer)), false);
*/
}
}*/
/// <summary>
/// Load actions and their toolbar assignments from a file.

View File

@@ -0,0 +1,88 @@
using Content.Client.Actions;
using Content.Client.Mapping;
using Content.Shared.Administration;
using Robust.Shared.Console;
namespace Content.Client.Commands;
// Disabled until sandoxing issues are resolved. In the meantime, if you want to create an acttions preset, just disable
// sandboxing and uncomment this code (and the SaveActionAssignments() function).
/*
[AnyCommand]
public sealed class SaveActionsCommand : IConsoleCommand
{
public string Command => "saveacts";
public string Description => "Saves the current action toolbar assignments to a file";
public string Help => $"Usage: {Command} <user resource path>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteLine(Help);
return;
}
try
{
EntitySystem.Get<ActionsSystem>().SaveActionAssignments(args[0]);
}
catch
{
shell.WriteLine("Failed to save action assignments");
}
}
}
*/
[AnyCommand]
public sealed class LoadActionsCommand : IConsoleCommand
{
public string Command => "loadacts";
public string Description => "Loads action toolbar assignments from a user-file.";
public string Help => $"Usage: {Command} <user resource path>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 1)
{
shell.WriteLine(Help);
return;
}
try
{
EntitySystem.Get<ActionsSystem>().LoadActionAssignments(args[0], true);
}
catch
{
shell.WriteLine("Failed to load action assignments");
}
}
}
[AnyCommand]
public sealed class LoadMappingActionsCommand : IConsoleCommand
{
public string Command => "loadmapacts";
public string Description => "Loads the mapping preset action toolbar assignments.";
public string Help => $"Usage: {Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
try
{
EntitySystem.Get<MappingSystem>().LoadMappingActions();
}
catch
{
shell.WriteLine("Failed to load action assignments");
}
}
}

View File

@@ -1,8 +1,14 @@
using Content.Shared.Decals;
using Content.Client.Actions;
using Content.Shared.Actions;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Decals;
using Robust.Client.GameObjects;
using Robust.Client.Input;
using Robust.Shared.Input;
using Robust.Shared.Input.Binding;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Client.Decals;
@@ -12,6 +18,8 @@ public sealed class DecalPlacementSystem : EntitySystem
{
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly InputSystem _inputSystem = default!;
[Dependency] private readonly IPrototypeManager _protoMan = default!;
[Dependency] private readonly IMapManager _mapMan = default!;
private string? _decalId;
private Color _decalColor = Color.White;
@@ -82,6 +90,70 @@ public sealed class DecalPlacementSystem : EntitySystem
return true;
}, true)).Register<DecalPlacementSystem>();
SubscribeLocalEvent<FillActionSlotEvent>(OnFillSlot);
SubscribeLocalEvent<PlaceDecalActionEvent>(OnPlaceDecalAction);
}
private void OnPlaceDecalAction(PlaceDecalActionEvent args)
{
if (args.Handled)
return;
if (!_mapMan.TryFindGridAt(args.Target, out var grid))
return;
args.Handled = true;
var coords = EntityCoordinates.FromMap(grid.GridEntityId, args.Target, EntityManager);
if (args.Snap)
{
var newPos = new Vector2(
(float) (MathF.Round(coords.X - 0.5f, MidpointRounding.AwayFromZero) + 0.5),
(float) (MathF.Round(coords.Y - 0.5f, MidpointRounding.AwayFromZero) + 0.5)
);
coords = coords.WithPosition(newPos);
}
coords = coords.Offset(new Vector2(-0.5f, -0.5f));
var decal = new Decal(coords.Position, args.DecalId, args.Color, Angle.FromDegrees(args.Rotation), args.ZIndex, args.Cleanable);
RaiseNetworkEvent(new RequestDecalPlacementEvent(decal, coords));
}
private void OnFillSlot(FillActionSlotEvent ev)
{
if (!_active || _placing)
return;
if (ev.Action != null)
return;
if (_decalId == null || !_protoMan.TryIndex<DecalPrototype>(_decalId, out var decalProto))
return;
var actionEvent = new PlaceDecalActionEvent()
{
DecalId = _decalId,
Color = _decalColor,
Rotation = _decalAngle.Degrees,
Snap = _snap,
ZIndex = _zIndex,
Cleanable = _cleanable,
};
ev.Action = new WorldTargetAction()
{
Name = $"{_decalId} ({_decalColor.ToHex()}, {(int) _decalAngle.Degrees})", // non-unique actions may be considered duplicates when saving/loading.
Icon = decalProto.Sprite,
Repeat = true,
CheckCanAccess = false,
CheckCanInteract = false,
Range = -1,
Event = actionEvent,
IconColor = _decalColor,
};
}
public override void Shutdown()
@@ -110,3 +182,24 @@ public sealed class DecalPlacementSystem : EntitySystem
_inputSystem.SetEntityContextActive();
}
}
public sealed class PlaceDecalActionEvent : PerformWorldTargetActionEvent
{
[DataField("decalId", customTypeSerializer:typeof(PrototypeIdSerializer<DecalPrototype>))]
public string DecalId = string.Empty;
[DataField("color")]
public Color Color;
[DataField("rotation")]
public double Rotation;
[DataField("snap")]
public bool Snap;
[DataField("zIndex")]
public int ZIndex;
[DataField("cleanable")]
public bool Cleanable;
}

View File

@@ -0,0 +1,154 @@
using Robust.Shared.Utility;
using Robust.Client.Placement;
using Robust.Shared.Map;
using Content.Shared.Actions.ActionTypes;
using Content.Shared.Actions;
using Content.Client.Actions;
using Content.Shared.Maps;
namespace Content.Client.Mapping;
public sealed partial class MappingSystem : EntitySystem
{
[Dependency] private readonly IPlacementManager _placementMan = default!;
[Dependency] private readonly ITileDefinitionManager _tileMan = default!;
[Dependency] private readonly ActionsSystem _actionsSystem = default!;
/// <summary>
/// The icon to use for space tiles.
/// </summary>
private readonly SpriteSpecifier _spaceIcon = new SpriteSpecifier.Texture(new ResourcePath("Tiles/cropped_parallax.png"));
/// <summary>
/// The icon to use for entity-eraser.
/// </summary>
private readonly SpriteSpecifier _deleteIcon = new SpriteSpecifier.Texture(new ResourcePath("Interface/VerbIcons/delete.svg.192dpi.png"));
public string DefaultMappingActions = "/mapping_actions.yml";
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FillActionSlotEvent>(OnFillActionSlot);
SubscribeLocalEvent<StartPlacementActionEvent>(OnStartPlacementAction);
}
public void LoadMappingActions()
{
_actionsSystem.LoadActionAssignments(DefaultMappingActions, false);
}
/// <summary>
/// This checks if the placement manager is currently active, and attempts to copy the placement information for
/// some entity or tile into an action. This is somewhat janky, but it seem to work well enough. Though I'd
/// prefer if it were to function more like DecalPlacementSystem.
/// </summary>
private void OnFillActionSlot(FillActionSlotEvent ev)
{
if (!_placementMan.IsActive)
return;
if (ev.Action != null)
return;
var actionEvent = new StartPlacementActionEvent();
if (_placementMan.CurrentPermission != null)
{
actionEvent.EntityType = _placementMan.CurrentPermission.EntityType;
actionEvent.IsTile = _placementMan.CurrentPermission.IsTile;
actionEvent.TileType = _placementMan.CurrentPermission.TileType;
actionEvent.PlacementOption = _placementMan.CurrentPermission.PlacementOption;
}
else if (_placementMan.Eraser)
{
actionEvent.Eraser = true;
}
else
return;
if (actionEvent.IsTile)
{
var tileDef = _tileMan[actionEvent.TileType];
if (tileDef is not ContentTileDefinition contentTileDef)
return;
var tileIcon = contentTileDef.IsSpace
? _spaceIcon
: new SpriteSpecifier.Texture(new ResourcePath(tileDef.Path) / $"{tileDef.SpriteName}.png");
ev.Action = new InstantAction()
{
CheckCanInteract = false,
Event = actionEvent,
Name = tileDef.Name,
Icon = tileIcon
};
return;
}
if (actionEvent.Eraser)
{
ev.Action = new InstantAction()
{
CheckCanInteract = false,
Event = actionEvent,
Name = "action-name-mapping-erase",
Icon = _deleteIcon,
};
return;
}
if (string.IsNullOrWhiteSpace(actionEvent.EntityType))
return;
ev.Action = new InstantAction()
{
CheckCanInteract = false,
Event = actionEvent,
Name = actionEvent.EntityType,
Icon = new SpriteSpecifier.EntityPrototype(actionEvent.EntityType),
};
}
private void OnStartPlacementAction(StartPlacementActionEvent args)
{
if (args.Handled)
return;
args.Handled = true;
_placementMan.BeginPlacing(new()
{
EntityType = args.EntityType,
IsTile = args.IsTile,
TileType = args.TileType,
PlacementOption = args.PlacementOption,
});
if (_placementMan.Eraser != args.Eraser)
_placementMan.ToggleEraser();
}
}
public sealed class StartPlacementActionEvent : PerformActionEvent
{
[DataField("entityType")]
public string? EntityType;
[DataField("isTile")]
public bool IsTile;
[DataField("tileType")]
public ushort TileType;
[DataField("placementOption")]
public string? PlacementOption;
[DataField("eraser")]
public bool Eraser;
}