#region Copyright & License Information /* * Copyright (c) The OpenRA Developers and Contributors * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. For more * information, see COPYING. */ #endregion using System.Collections.Generic; using System.Linq; using OpenRA.Graphics; using OpenRA.Mods.Common.EditorBrushes; using OpenRA.Mods.Common.Traits; using OpenRA.Widgets; namespace OpenRA.Mods.Common.Widgets.Logic { public class MapEditorSelectionLogic : ChromeLogic { readonly EditorViewportControllerWidget editor; readonly WorldRenderer worldRenderer; readonly ContainerWidget actorEditPanel; readonly ContainerWidget areaEditPanel; readonly CheckboxWidget copyTerrainCheckbox; readonly CheckboxWidget copyResourcesCheckbox; readonly CheckboxWidget copyActorsCheckbox; readonly EditorActorLayer editorActorLayer; MapCopyFilters copyFilters = MapCopyFilters.All; EditorClipboard? clipboard; readonly IResourceLayer resourceLayer; [ObjectCreator.UseCtor] public MapEditorSelectionLogic(Widget widget, World world, WorldRenderer worldRenderer) { this.worldRenderer = worldRenderer; editorActorLayer = world.WorldActor.Trait(); resourceLayer = world.WorldActor.Trait(); editor = widget.Get("MAP_EDITOR"); var selectTabContainer = widget.Get("SELECT_WIDGETS"); actorEditPanel = selectTabContainer.Get("ACTOR_EDIT_PANEL"); areaEditPanel = selectTabContainer.Get("AREA_EDIT_PANEL"); actorEditPanel.IsVisible = () => editor.CurrentBrush == editor.DefaultBrush && editor.DefaultBrush.Selection.Actor != null; areaEditPanel.IsVisible = () => !actorEditPanel.IsVisible(); copyTerrainCheckbox = areaEditPanel.Get("COPY_FILTER_TERRAIN_CHECKBOX"); copyResourcesCheckbox = areaEditPanel.Get("COPY_FILTER_RESOURCES_CHECKBOX"); copyActorsCheckbox = areaEditPanel.Get("COPY_FILTER_ACTORS_CHECKBOX"); copyTerrainCheckbox.IsDisabled = () => editor.CurrentBrush is EditorCopyPasteBrush; copyResourcesCheckbox.IsDisabled = () => editor.CurrentBrush is EditorCopyPasteBrush; copyActorsCheckbox.IsDisabled = () => editor.CurrentBrush is EditorCopyPasteBrush; var copyButton = widget.Get("COPY_BUTTON"); copyButton.OnClick = () => clipboard = CopySelectionContents(); copyButton.IsDisabled = () => editor.DefaultBrush.Selection.Area == null; var pasteButton = widget.Get("PASTE_BUTTON"); pasteButton.OnClick = () => { if (clipboard == null) return; editor.SetBrush(new EditorCopyPasteBrush( editor, worldRenderer, clipboard.Value, resourceLayer, () => copyFilters)); }; pasteButton.IsDisabled = () => clipboard == null; pasteButton.IsHighlighted = () => editor.CurrentBrush is EditorCopyPasteBrush; var closeAreaSelectionButton = areaEditPanel.Get("SELECTION_CANCEL_BUTTON"); closeAreaSelectionButton.OnClick = () => editor.DefaultBrush.ClearSelection(); CreateCategoryPanel(MapCopyFilters.Terrain, copyTerrainCheckbox); CreateCategoryPanel(MapCopyFilters.Resources, copyResourcesCheckbox); CreateCategoryPanel(MapCopyFilters.Actors, copyActorsCheckbox); } EditorClipboard CopySelectionContents() { 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 previews = new Dictionary(); var tiles = new Dictionary(); foreach (var cell in source) { if (!mapTiles.Contains(cell)) continue; tiles.Add(cell, new ClipboardTile(mapTiles[cell], mapResources[cell], resourceLayer.GetResource(cell), mapHeight[cell])); if (copyFilters.HasFlag(MapCopyFilters.Actors)) foreach (var preview in selection.SelectMany(editorActorLayer.PreviewsAt).Distinct()) previews.TryAdd(preview.ID, preview); } return new EditorClipboard(selection, previews, tiles); } void CreateCategoryPanel(MapCopyFilters copyFilter, CheckboxWidget checkbox) { checkbox.GetText = () => copyFilter.ToString(); checkbox.IsChecked = () => copyFilters.HasFlag(copyFilter); checkbox.IsVisible = () => true; checkbox.OnClick = () => copyFilters ^= copyFilter; } } }