From b073155018540128ded2703c67b534efef5f5f7b Mon Sep 17 00:00:00 2001 From: Gustas Date: Fri, 16 Aug 2024 17:53:49 +0300 Subject: [PATCH] Polish map editor code --- .../EditorBrushes/EditorDefaultBrush.cs | 10 +++---- .../EditorBrushes/EditorMarkerLayerBrush.cs | 6 +--- .../EditorBrushes/EditorTileBrush.cs | 2 -- .../Traits/World/EditorActorLayer.cs | 30 +++++++++++++++---- .../Traits/World/EditorCursorLayer.cs | 16 ---------- .../Logic/Editor/MapEditorSelectionLogic.cs | 26 +++++++--------- .../Logic/Editor/MapMarkerTilesLogic.cs | 6 ++-- 7 files changed, 42 insertions(+), 54 deletions(-) diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs index ba3decbc12..78e88164da 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorDefaultBrush.cs @@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Widgets readonly EditorActorLayer editorLayer; readonly EditorActionManager editorActionManager; readonly IResourceLayer resourceLayer; - readonly EditorCursorLayer cursorLayer; + readonly EditorActorLayer actorLayer; public CellRegion CurrentDragBounds => selectionBounds ?? Selection.Area; @@ -66,7 +66,7 @@ namespace OpenRA.Mods.Common.Widgets editorLayer = world.WorldActor.Trait(); editorActionManager = world.WorldActor.Trait(); resourceLayer = world.WorldActor.TraitOrDefault(); - cursorLayer = world.WorldActor.Trait(); + actorLayer = world.WorldActor.Trait(); } long CalculateActorSelectionPriority(EditorActorPreview actor) @@ -139,7 +139,7 @@ namespace OpenRA.Mods.Common.Widgets var cellViewPx = worldRenderer.Viewport.WorldToViewPx(worldRenderer.ScreenPosition(world.Map.CenterOfCell(cell))); var pixelOffset = cellViewPx - mi.Location; var cellOffset = underCursor.Location - cell; - moveAction = new MoveActorAction(underCursor, cursorLayer, worldRenderer, pixelOffset, cellOffset); + moveAction = new MoveActorAction(underCursor, actorLayer, worldRenderer, pixelOffset, cellOffset); draggingActor = true; return false; } @@ -408,7 +408,7 @@ namespace OpenRA.Mods.Common.Widgets public string Text { get; private set; } readonly EditorActorPreview actor; - readonly EditorCursorLayer layer; + readonly EditorActorLayer layer; readonly WorldRenderer worldRenderer; readonly int2 pixelOffset; readonly CVec cellOffset; @@ -418,7 +418,7 @@ namespace OpenRA.Mods.Common.Widgets public MoveActorAction( EditorActorPreview actor, - EditorCursorLayer layer, + EditorActorLayer layer, WorldRenderer worldRenderer, int2 pixelOffset, CVec cellOffset) diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs index 95d1d58d62..10e6752d2e 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorMarkerLayerBrush.cs @@ -39,8 +39,6 @@ namespace OpenRA.Mods.Common.Widgets markerLayerOverlay = world.WorldActor.Trait(); Template = id; - worldRenderer = wr; - world = wr.World; action = new PaintMarkerTileEditorAction(Template, markerLayerOverlay); } @@ -60,11 +58,9 @@ namespace OpenRA.Mods.Common.Widgets return false; } - var cell = worldRenderer.Viewport.ViewToWorld(mi.Location); - if (mi.Button == MouseButton.Left && mi.Event != MouseInputEvent.Up) { - action.Add(cell); + action.Add(worldRenderer.Viewport.ViewToWorld(mi.Location)); painting = true; } else if (painting && mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Up) diff --git a/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs b/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs index 99c4698456..2a54dc9d70 100644 --- a/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs +++ b/OpenRA.Mods.Common/EditorBrushes/EditorTileBrush.cs @@ -45,8 +45,6 @@ namespace OpenRA.Mods.Common.Widgets editorCursor = world.WorldActor.Trait(); Template = id; - worldRenderer = wr; - world = wr.World; var template = terrainInfo.Templates.First(t => t.Value.Id == id).Value; cursorToken = editorCursor.SetTerrainTemplate(wr, template); diff --git a/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs b/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs index 0b730c0e45..015cd1c6c6 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorActorLayer.cs @@ -25,12 +25,12 @@ namespace OpenRA.Mods.Common.Traits [Desc("Required for the map editor to work. Attach this to the world actor.")] public class EditorActorLayerInfo : TraitInfo, ICreatePlayersInfo { - [Desc("Size of partition bins (world pixels)")] + [Desc("Size of partition bins (world pixels).")] public readonly int BinSize = 250; void ICreatePlayersInfo.CreateServerPlayers(MapPreview map, Session lobbyInfo, List players, MersenneTwister playerRandom) { - throw new NotImplementedException("EditorActorLayer must not be defined on the world actor"); + throw new NotImplementedException("EditorActorLayer must not be defined on the world actor."); } public override object Create(ActorInitializer init) { return new EditorActorLayer(this); } @@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Traits public class EditorActorLayer : IWorldLoaded, ITickRender, IRender, IRadarSignature, ICreatePlayers, IRenderAnnotations { - readonly EditorActorLayerInfo info; + public readonly EditorActorLayerInfo Info; readonly List previews = new(); int2 cellOffset; @@ -51,7 +51,7 @@ namespace OpenRA.Mods.Common.Traits public EditorActorLayer(EditorActorLayerInfo info) { - this.info = info; + Info = info; } void ICreatePlayers.CreatePlayers(World w, MersenneTwister playerRandom) @@ -79,12 +79,12 @@ namespace OpenRA.Mods.Common.Traits var cellOffsetMax = new int2(world.Map.AllCells.Max(c => c.X), world.Map.AllCells.Max((c) => c.Y)); var mapCellSize = cellOffsetMax - cellOffset; cellMap = new SpatiallyPartitioned( - mapCellSize.X, mapCellSize.Y, Exts.IntegerDivisionRoundingAwayFromZero(info.BinSize, world.Map.Grid.TileSize.Width)); + mapCellSize.X, mapCellSize.Y, Exts.IntegerDivisionRoundingAwayFromZero(Info.BinSize, world.Map.Grid.TileSize.Width)); var ts = world.Map.Grid.TileSize; var width = world.Map.MapSize.X * ts.Width; var height = world.Map.MapSize.Y * ts.Height; - screenMap = new SpatiallyPartitioned(width, height, info.BinSize); + screenMap = new SpatiallyPartitioned(width, height, Info.BinSize); foreach (var kv in world.Map.ActorDefinitions) Add(kv.Key, new ActorReference(kv.Value.Value, kv.Value.ToDictionary()), true); @@ -191,6 +191,24 @@ namespace OpenRA.Mods.Common.Traits SyncMultiplayerCount(); } + public void MoveActor(EditorActorPreview preview, CPos location) + { + Remove(preview); + preview.ReplaceInit(new LocationInit(location)); + var ios = preview.Info.TraitInfoOrDefault(); + if (ios != null && ios.SharesCell) + { + var actorSubCell = FreeSubCellAt(location); + if (actorSubCell == SubCell.Invalid) + preview.RemoveInit(); + else + preview.ReplaceInit(new SubCellInit(actorSubCell)); + } + + preview.UpdateFromMove(); + Add(preview); + } + void SyncMultiplayerCount() { var newCount = previews.Count(p => p.Info.Name == "mpspawn"); diff --git a/OpenRA.Mods.Common/Traits/World/EditorCursorLayer.cs b/OpenRA.Mods.Common/Traits/World/EditorCursorLayer.cs index 0020ac89b4..4ab901d23c 100644 --- a/OpenRA.Mods.Common/Traits/World/EditorCursorLayer.cs +++ b/OpenRA.Mods.Common/Traits/World/EditorCursorLayer.cs @@ -189,22 +189,6 @@ namespace OpenRA.Mods.Common.Traits return ++CurrentToken; } - public void MoveActor(EditorActorPreview preview, CPos location) - { - editorLayer.Remove(preview); - preview.ReplaceInit(new LocationInit(location)); - var ios = preview.Info.TraitInfoOrDefault(); - if (ios != null && ios.SharesCell) - { - var actorSubCell = editorLayer.FreeSubCellAt(location); - if (actorSubCell != SubCell.Invalid) - preview.ReplaceInit(new SubCellInit(actorSubCell)); - } - - preview.UpdateFromMove(); - editorLayer.Add(preview); - } - public int SetTerrainTemplate(WorldRenderer wr, TerrainTemplateInfo template) { terrainOrResourceCell = wr.Viewport.ViewToWorld(wr.Viewport.WorldToViewPx(Viewport.LastMousePos)); diff --git a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorSelectionLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorSelectionLogic.cs index 4a0da84c17..13c0cdf202 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorSelectionLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorSelectionLogic.cs @@ -24,14 +24,8 @@ namespace OpenRA.Mods.Common.Widgets.Logic const string AreaSelection = "label-area-selection"; readonly EditorViewportControllerWidget editor; - readonly WorldRenderer worldRenderer; + readonly Map map; - readonly ContainerWidget actorEditPanel; - readonly ContainerWidget areaEditPanel; - - readonly CheckboxWidget copyTerrainCheckbox; - readonly CheckboxWidget copyResourcesCheckbox; - readonly CheckboxWidget copyActorsCheckbox; readonly EditorActorLayer editorActorLayer; readonly EditorResourceLayer editorResourceLayer; readonly IResourceLayer resourceLayer; @@ -46,7 +40,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic [ObjectCreator.UseCtor] public MapEditorSelectionLogic(Widget widget, World world, WorldRenderer worldRenderer) { - this.worldRenderer = worldRenderer; + map = worldRenderer.World.Map; editorActorLayer = world.WorldActor.Trait(); resourceLayer = world.WorldActor.TraitOrDefault(); @@ -55,15 +49,15 @@ namespace OpenRA.Mods.Common.Widgets.Logic editor = widget.Get("MAP_EDITOR"); editor.DefaultBrush.SelectionChanged += HandleSelectionChanged; var selectTabContainer = widget.Get("SELECT_WIDGETS"); - actorEditPanel = selectTabContainer.Get("ACTOR_EDIT_PANEL"); - areaEditPanel = selectTabContainer.Get("AREA_EDIT_PANEL"); + var actorEditPanel = selectTabContainer.Get("ACTOR_EDIT_PANEL"); + var areaEditPanel = selectTabContainer.Get("AREA_EDIT_PANEL"); actorEditPanel.IsVisible = () => editor.DefaultBrush.Selection.Actor != null; areaEditPanel.IsVisible = () => editor.DefaultBrush.Selection.Area != null; - copyTerrainCheckbox = areaEditPanel.Get("COPY_FILTER_TERRAIN_CHECKBOX"); - copyResourcesCheckbox = areaEditPanel.Get("COPY_FILTER_RESOURCES_CHECKBOX"); - copyActorsCheckbox = areaEditPanel.Get("COPY_FILTER_ACTORS_CHECKBOX"); + var copyTerrainCheckbox = areaEditPanel.Get("COPY_FILTER_TERRAIN_CHECKBOX"); + var copyResourcesCheckbox = areaEditPanel.Get("COPY_FILTER_RESOURCES_CHECKBOX"); + var copyActorsCheckbox = areaEditPanel.Get("COPY_FILTER_ACTORS_CHECKBOX"); copyTerrainCheckbox.IsDisabled = () => editor.CurrentBrush is EditorCopyPasteBrush; copyResourcesCheckbox.IsDisabled = () => editor.CurrentBrush is EditorCopyPasteBrush; @@ -107,9 +101,9 @@ namespace OpenRA.Mods.Common.Widgets.Logic var selection = editor.DefaultBrush.Selection.Area; var source = new CellCoordsRegion(selection.TopLeft, selection.BottomRight); - var mapTiles = worldRenderer.World.Map.Tiles; - var mapHeight = worldRenderer.World.Map.Height; - var mapResources = worldRenderer.World.Map.Resources; + var mapTiles = map.Tiles; + var mapHeight = map.Height; + var mapResources = map.Resources; var previews = new Dictionary(); var tiles = new Dictionary(); diff --git a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapMarkerTilesLogic.cs b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapMarkerTilesLogic.cs index dab7e52fdd..8bed572eeb 100644 --- a/OpenRA.Mods.Common/Widgets/Logic/Editor/MapMarkerTilesLogic.cs +++ b/OpenRA.Mods.Common/Widgets/Logic/Editor/MapMarkerTilesLogic.cs @@ -96,15 +96,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic ScrollItemWidget SetupEraseItem(ScrollItemWidget template) { - var item = ScrollItemWidget.Setup(template, - () => markerTile == null && editor.CurrentBrush != null && editor.CurrentBrush is EditorMarkerLayerBrush, + return ScrollItemWidget.Setup(template, + () => markerTile == null && editor.CurrentBrush is EditorMarkerLayerBrush, () => { markerTile = null; editor.SetBrush(new EditorMarkerLayerBrush(editor, null, worldRenderer)); }); - - return item; } }