Add delete button to the map editor
(cherry picked from commit f820adab51734020600d9758c4b78a1f6585b133)
This commit is contained in:
@@ -11,7 +11,9 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.EditorBrushes;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Widgets;
|
||||
@@ -87,6 +89,12 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
return ((long)pixelDistance << 32) + worldZPosition;
|
||||
}
|
||||
|
||||
public void DeleteSelection(MapBlitFilters filters)
|
||||
{
|
||||
if (Selection.Area != null)
|
||||
editorActionManager.Add(new DeleteAreaAction(world.Map, filters, Selection.Area, resourceLayer, actorLayer));
|
||||
}
|
||||
|
||||
public void ClearSelection(bool updateSelectedTab = false)
|
||||
{
|
||||
if (Selection.HasSelection)
|
||||
@@ -335,6 +343,114 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
}
|
||||
}
|
||||
|
||||
sealed class DeleteAreaAction : IEditorAction
|
||||
{
|
||||
[FluentReference("x", "y", "width", "height")]
|
||||
const string RemovedArea = "notification-removed-area";
|
||||
|
||||
public string Text { get; }
|
||||
|
||||
readonly EditorBlitSource editorBlitSource;
|
||||
readonly MapBlitFilters blitFilters;
|
||||
readonly IResourceLayer resourceLayer;
|
||||
readonly EditorActorLayer editorActorLayer;
|
||||
readonly CellRegion area;
|
||||
readonly Map map;
|
||||
|
||||
public DeleteAreaAction(Map map, MapBlitFilters blitFilters, CellRegion area, IResourceLayer resourceLayer, EditorActorLayer editorActorLayer)
|
||||
{
|
||||
this.map = map;
|
||||
this.blitFilters = blitFilters;
|
||||
this.resourceLayer = resourceLayer;
|
||||
this.editorActorLayer = editorActorLayer;
|
||||
this.area = area;
|
||||
|
||||
editorBlitSource = EditorBlit.CopyRegionContents(map, editorActorLayer, resourceLayer, area, blitFilters);
|
||||
|
||||
Text = FluentProvider.GetMessage(RemovedArea,
|
||||
"x", area.TopLeft.X,
|
||||
"y", area.TopLeft.Y,
|
||||
"width", area.BottomRight.X - area.TopLeft.X,
|
||||
"height", area.BottomRight.Y - area.TopLeft.Y);
|
||||
}
|
||||
|
||||
public void Execute()
|
||||
{
|
||||
Do();
|
||||
}
|
||||
|
||||
public void Do()
|
||||
{
|
||||
if (blitFilters.HasFlag(MapBlitFilters.Actors))
|
||||
{
|
||||
// Clear any existing actors in the paste cells.
|
||||
foreach (var regionActor in editorActorLayer.PreviewsInCellRegion(area.CellCoords).ToList())
|
||||
editorActorLayer.Remove(regionActor);
|
||||
}
|
||||
|
||||
foreach (var tileKeyValuePair in editorBlitSource.Tiles)
|
||||
{
|
||||
var position = tileKeyValuePair.Key;
|
||||
if (!map.Tiles.Contains(position))
|
||||
continue;
|
||||
|
||||
// Clear any existing resources.
|
||||
if (resourceLayer != null && blitFilters.HasFlag(MapBlitFilters.Resources))
|
||||
resourceLayer.ClearResources(position);
|
||||
|
||||
if (blitFilters.HasFlag(MapBlitFilters.Terrain))
|
||||
{
|
||||
map.Tiles[position] = map.Rules.TerrainInfo.DefaultTerrainTile;
|
||||
map.Height[position] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Undo()
|
||||
{
|
||||
foreach (var tileKeyValuePair in editorBlitSource.Tiles)
|
||||
{
|
||||
var position = tileKeyValuePair.Key;
|
||||
if (!map.Tiles.Contains(position))
|
||||
continue;
|
||||
|
||||
var tile = tileKeyValuePair.Value;
|
||||
var resourceLayerContents = tile.ResourceLayerContents;
|
||||
|
||||
if (blitFilters.HasFlag(MapBlitFilters.Terrain))
|
||||
{
|
||||
map.Tiles[position] = tile.TerrainTile;
|
||||
map.Height[position] = tile.Height;
|
||||
}
|
||||
|
||||
if (blitFilters.HasFlag(MapBlitFilters.Resources) &&
|
||||
resourceLayerContents.HasValue &&
|
||||
!string.IsNullOrWhiteSpace(resourceLayerContents.Value.Type))
|
||||
resourceLayer.AddResource(resourceLayerContents.Value.Type, position, resourceLayerContents.Value.Density);
|
||||
}
|
||||
|
||||
if (blitFilters.HasFlag(MapBlitFilters.Actors))
|
||||
{
|
||||
// Create copies of the original actors, update their locations, and place.
|
||||
foreach (var actorKeyValuePair in editorBlitSource.Actors)
|
||||
{
|
||||
var copy = actorKeyValuePair.Value.Export();
|
||||
var locationInit = copy.GetOrDefault<LocationInit>();
|
||||
if (locationInit != null)
|
||||
{
|
||||
if (!map.Tiles.Contains(locationInit.Value))
|
||||
continue;
|
||||
|
||||
copy.RemoveAll<LocationInit>();
|
||||
copy.Add(new LocationInit(locationInit.Value));
|
||||
}
|
||||
|
||||
editorActorLayer.Add(copy);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sealed class RemoveSelectedActorAction : IEditorAction
|
||||
{
|
||||
[FluentReference("name", "id")]
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
public LabelWidget DiagonalLabel;
|
||||
public LabelWidget ResourceCounterLabel;
|
||||
|
||||
MapBlitFilters copyFilters = MapBlitFilters.All;
|
||||
MapBlitFilters selectionFilters = MapBlitFilters.All;
|
||||
EditorBlitSource? clipboard;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
@@ -81,12 +81,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
worldRenderer,
|
||||
clipboard.Value,
|
||||
resourceLayer,
|
||||
() => copyFilters));
|
||||
() => selectionFilters));
|
||||
};
|
||||
|
||||
pasteButton.IsDisabled = () => clipboard == null;
|
||||
pasteButton.IsHighlighted = () => editor.CurrentBrush is EditorCopyPasteBrush;
|
||||
|
||||
var deleteAreaSelectionButton = areaEditPanel.Get<ButtonWidget>("SELECTION_DELETE_BUTTON");
|
||||
deleteAreaSelectionButton.OnClick = () => editor.DefaultBrush.DeleteSelection(selectionFilters);
|
||||
|
||||
var closeAreaSelectionButton = areaEditPanel.Get<ButtonWidget>("SELECTION_CANCEL_BUTTON");
|
||||
closeAreaSelectionButton.OnClick = () => editor.DefaultBrush.ClearSelection(updateSelectedTab: true);
|
||||
|
||||
@@ -102,15 +105,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
editorActorLayer,
|
||||
resourceLayer,
|
||||
editor.DefaultBrush.Selection.Area,
|
||||
copyFilters);
|
||||
selectionFilters);
|
||||
}
|
||||
|
||||
void CreateCategoryPanel(MapBlitFilters copyFilter, CheckboxWidget checkbox)
|
||||
{
|
||||
checkbox.GetText = () => copyFilter.ToString();
|
||||
checkbox.IsChecked = () => copyFilters.HasFlag(copyFilter);
|
||||
checkbox.IsChecked = () => selectionFilters.HasFlag(copyFilter);
|
||||
checkbox.IsVisible = () => true;
|
||||
checkbox.OnClick = () => copyFilters ^= copyFilter;
|
||||
checkbox.OnClick = () => selectionFilters ^= copyFilter;
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
|
||||
Reference in New Issue
Block a user