Add new map editor UI.
This commit is contained in:
98
OpenRA.Mods.Common/Widgets/EditorViewportControllerWidget.cs
Normal file
98
OpenRA.Mods.Common/Widgets/EditorViewportControllerWidget.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Orders;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
public class EditorViewportControllerWidget : Widget
|
||||
{
|
||||
public IEditorBrush CurrentBrush { get; private set; }
|
||||
|
||||
public readonly string TooltipContainer;
|
||||
public readonly string TooltipTemplate;
|
||||
|
||||
readonly Lazy<TooltipContainerWidget> tooltipContainer;
|
||||
readonly EditorDefaultBrush defaultBrush;
|
||||
readonly WorldRenderer worldRenderer;
|
||||
|
||||
bool enableTooltips;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public EditorViewportControllerWidget(World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
this.worldRenderer = worldRenderer;
|
||||
tooltipContainer = Exts.Lazy(() => Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
|
||||
CurrentBrush = defaultBrush = new EditorDefaultBrush(this, worldRenderer);
|
||||
}
|
||||
|
||||
public void ClearBrush() { SetBrush(null); }
|
||||
public void SetBrush(IEditorBrush brush)
|
||||
{
|
||||
CurrentBrush = brush ?? defaultBrush;
|
||||
}
|
||||
|
||||
public override void MouseEntered()
|
||||
{
|
||||
enableTooltips = true;
|
||||
}
|
||||
|
||||
public override void MouseExited()
|
||||
{
|
||||
tooltipContainer.Value.RemoveTooltip();
|
||||
enableTooltips = false;
|
||||
}
|
||||
|
||||
public void SetTooltip(string tooltip)
|
||||
{
|
||||
if (!enableTooltips)
|
||||
return;
|
||||
|
||||
if (tooltip != null)
|
||||
{
|
||||
Func<string> getTooltip = () => tooltip;
|
||||
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() { { "getText", getTooltip } });
|
||||
}
|
||||
else
|
||||
tooltipContainer.Value.RemoveTooltip();
|
||||
}
|
||||
|
||||
public override bool HandleMouseInput(MouseInput mi)
|
||||
{
|
||||
if (CurrentBrush.HandleMouseInput(mi))
|
||||
return true;
|
||||
|
||||
return base.HandleMouseInput(mi);
|
||||
}
|
||||
|
||||
WPos cachedViewportPosition;
|
||||
public override void Tick()
|
||||
{
|
||||
// Clear any tooltips when the viewport is scrolled using the keyboard
|
||||
if (worldRenderer.Viewport.CenterPosition != cachedViewportPosition)
|
||||
SetTooltip(null);
|
||||
|
||||
cachedViewportPosition = worldRenderer.Viewport.CenterPosition;
|
||||
CurrentBrush.Tick();
|
||||
}
|
||||
}
|
||||
}
|
||||
150
OpenRA.Mods.Common/Widgets/Logic/Editor/ActorSelectorLogic.cs
Normal file
150
OpenRA.Mods.Common/Widgets/Logic/Editor/ActorSelectorLogic.cs
Normal file
@@ -0,0 +1,150 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common;
|
||||
using OpenRA.Mods.Common.Graphics;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Mods.Common.Widgets;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class ActorSelectorLogic
|
||||
{
|
||||
readonly EditorViewportControllerWidget editor;
|
||||
readonly DropDownButtonWidget ownersDropDown;
|
||||
readonly ScrollPanelWidget panel;
|
||||
readonly ScrollItemWidget itemTemplate;
|
||||
readonly Ruleset modRules;
|
||||
readonly World world;
|
||||
readonly WorldRenderer worldRenderer;
|
||||
|
||||
PlayerReference selectedOwner;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public ActorSelectorLogic(Widget widget, World world, WorldRenderer worldRenderer, Ruleset modRules)
|
||||
{
|
||||
this.modRules = modRules;
|
||||
this.world = world;
|
||||
this.worldRenderer = worldRenderer;
|
||||
|
||||
editor = widget.Parent.Get<EditorViewportControllerWidget>("MAP_EDITOR");
|
||||
ownersDropDown = widget.Get<DropDownButtonWidget>("OWNERS_DROPDOWN");
|
||||
|
||||
panel = widget.Get<ScrollPanelWidget>("ACTORTEMPLATE_LIST");
|
||||
itemTemplate = panel.Get<ScrollItemWidget>("ACTORPREVIEW_TEMPLATE");
|
||||
panel.Layout = new GridLayout(panel);
|
||||
|
||||
var editorLayer = world.WorldActor.Trait<EditorActorLayer>();
|
||||
|
||||
selectedOwner = editorLayer.Players.Players.Values.First();
|
||||
Func<PlayerReference, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(template, () => selectedOwner == option, () =>
|
||||
{
|
||||
selectedOwner = option;
|
||||
|
||||
ownersDropDown.Text = selectedOwner.Name;
|
||||
ownersDropDown.TextColor = selectedOwner.Color.RGB;
|
||||
|
||||
IntializeActorPreviews();
|
||||
});
|
||||
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => option.Name;
|
||||
item.GetColor = () => option.Color.RGB;
|
||||
|
||||
return item;
|
||||
};
|
||||
|
||||
ownersDropDown.OnClick = () =>
|
||||
{
|
||||
var owners = editorLayer.Players.Players.Values.OrderBy(p => p.Name);
|
||||
ownersDropDown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 270, owners, setupItem);
|
||||
};
|
||||
|
||||
ownersDropDown.Text = selectedOwner.Name;
|
||||
ownersDropDown.TextColor = selectedOwner.Color.RGB;
|
||||
|
||||
IntializeActorPreviews();
|
||||
}
|
||||
|
||||
void IntializeActorPreviews()
|
||||
{
|
||||
panel.RemoveChildren();
|
||||
|
||||
var actors = modRules.Actors.Where(a => !a.Value.Name.Contains('^'))
|
||||
.Select(a => a.Value);
|
||||
|
||||
foreach (var a in actors)
|
||||
{
|
||||
var actor = a;
|
||||
if (actor.Traits.Contains<BridgeInfo>()) // bridge layer takes care about that automatically
|
||||
continue;
|
||||
|
||||
if (!actor.Traits.Contains<IRenderActorPreviewInfo>())
|
||||
continue;
|
||||
|
||||
var filter = actor.Traits.GetOrDefault<EditorTilesetFilterInfo>();
|
||||
if (filter != null)
|
||||
{
|
||||
if (filter.ExcludeTilesets != null && filter.ExcludeTilesets.Contains(world.TileSet.Id))
|
||||
continue;
|
||||
if (filter.RequireTilesets != null && !filter.RequireTilesets.Contains(world.TileSet.Id))
|
||||
continue;
|
||||
}
|
||||
|
||||
var td = new TypeDictionary();
|
||||
td.Add(new FacingInit(92));
|
||||
td.Add(new TurretFacingInit(92));
|
||||
td.Add(new HideBibPreviewInit());
|
||||
td.Add(new OwnerInit(selectedOwner.Name));
|
||||
td.Add(new RaceInit(selectedOwner.Race));
|
||||
|
||||
try
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(itemTemplate,
|
||||
() => { var brush = editor.CurrentBrush as EditorActorBrush; return brush != null && brush.Actor == actor; },
|
||||
() => editor.SetBrush(new EditorActorBrush(editor, actor, selectedOwner, worldRenderer)));
|
||||
|
||||
var preview = item.Get<ActorPreviewWidget>("ACTOR_PREVIEW");
|
||||
preview.SetPreview(actor, td);
|
||||
|
||||
// Scale templates to fit within the panel
|
||||
var scale = 1f;
|
||||
if (scale * preview.IdealPreviewSize.X > itemTemplate.Bounds.Width)
|
||||
scale = (float)(itemTemplate.Bounds.Width - panel.ItemSpacing) / (float)preview.IdealPreviewSize.X;
|
||||
|
||||
preview.GetScale = () => scale;
|
||||
preview.Bounds.Width = (int)(scale * preview.IdealPreviewSize.X);
|
||||
preview.Bounds.Height = (int)(scale * preview.IdealPreviewSize.Y);
|
||||
|
||||
item.Bounds.Width = preview.Bounds.Width + 2 * preview.Bounds.X;
|
||||
item.Bounds.Height = preview.Bounds.Height + 2 * preview.Bounds.Y;
|
||||
item.IsVisible = () => true;
|
||||
item.GetTooltipText = () => actor.Name;
|
||||
panel.AddChild(item);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Log.Write("debug", "Map editor ignoring actor {0}, because of missing sprites for tileset {1}.",
|
||||
actor.Name, world.TileSet.Id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class LayerSelectorLogic
|
||||
{
|
||||
readonly EditorViewportControllerWidget editor;
|
||||
readonly Ruleset modRules;
|
||||
readonly World world;
|
||||
readonly WorldRenderer worldRenderer;
|
||||
|
||||
readonly ScrollPanelWidget layerTemplateList;
|
||||
readonly ScrollItemWidget layerPreviewTemplate;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public LayerSelectorLogic(Widget widget, WorldRenderer worldRenderer, Ruleset modRules)
|
||||
{
|
||||
this.modRules = modRules;
|
||||
this.worldRenderer = worldRenderer;
|
||||
this.world = worldRenderer.World;
|
||||
|
||||
editor = widget.Parent.Get<EditorViewportControllerWidget>("MAP_EDITOR");
|
||||
|
||||
layerTemplateList = widget.Get<ScrollPanelWidget>("LAYERTEMPLATE_LIST");
|
||||
layerTemplateList.Layout = new GridLayout(layerTemplateList);
|
||||
layerPreviewTemplate = layerTemplateList.Get<ScrollItemWidget>("LAYERPREVIEW_TEMPLATE");
|
||||
|
||||
IntializeLayerPreview(widget);
|
||||
}
|
||||
|
||||
void IntializeLayerPreview(Widget widget)
|
||||
{
|
||||
layerTemplateList.RemoveChildren();
|
||||
|
||||
var resources = modRules.Actors["world"].Traits.WithInterface<ResourceTypeInfo>();
|
||||
foreach (var resource in resources)
|
||||
{
|
||||
var newResourcePreviewTemplate = ScrollItemWidget.Setup(layerPreviewTemplate,
|
||||
() => { var brush = editor.CurrentBrush as EditorResourceBrush; return brush != null && brush.ResourceType == resource; },
|
||||
() => editor.SetBrush(new EditorResourceBrush(editor, resource, worldRenderer)));
|
||||
|
||||
newResourcePreviewTemplate.Bounds.X = 0;
|
||||
newResourcePreviewTemplate.Bounds.Y = 0;
|
||||
|
||||
var layerPreview = newResourcePreviewTemplate.Get<SpriteWidget>("LAYER_PREVIEW");
|
||||
layerPreview.IsVisible = () => true;
|
||||
layerPreview.GetPalette = () => resource.Palette;
|
||||
|
||||
var variant = resource.Variants.FirstOrDefault();
|
||||
var sequenceProvier = modRules.Sequences[world.TileSet.Id];
|
||||
var sequence = sequenceProvier.GetSequence("resources", variant);
|
||||
var frame = sequence.Frames != null ? sequence.Frames.Last() : resource.MaxDensity - 1;
|
||||
layerPreview.GetSprite = () => sequence.GetSprite(frame);
|
||||
|
||||
var tileWidth = Game.ModData.Manifest.TileSize.Width;
|
||||
var tileHeight = Game.ModData.Manifest.TileSize.Height;
|
||||
layerPreview.Bounds.Width = tileWidth;
|
||||
layerPreview.Bounds.Height = tileHeight;
|
||||
newResourcePreviewTemplate.Bounds.Width = tileWidth + (layerPreview.Bounds.X * 2);
|
||||
newResourcePreviewTemplate.Bounds.Height = tileHeight + (layerPreview.Bounds.Y * 2);
|
||||
|
||||
newResourcePreviewTemplate.IsVisible = () => true;
|
||||
newResourcePreviewTemplate.GetTooltipText = () => resource.Name;
|
||||
|
||||
layerTemplateList.AddChild(newResourcePreviewTemplate);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
70
OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorLogic.cs
Normal file
70
OpenRA.Mods.Common/Widgets/Logic/Editor/MapEditorLogic.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class MapEditorLogic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public MapEditorLogic(Widget widget, World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
var gridButton = widget.GetOrNull<ButtonWidget>("GRID_BUTTON");
|
||||
var terrainGeometryTrait = world.WorldActor.Trait<TerrainGeometryOverlay>();
|
||||
|
||||
if (gridButton != null && terrainGeometryTrait != null)
|
||||
{
|
||||
gridButton.OnClick = () => terrainGeometryTrait.Enabled ^= true;
|
||||
gridButton.IsHighlighted = () => terrainGeometryTrait.Enabled;
|
||||
}
|
||||
|
||||
var zoomDropdown = widget.Get<DropDownButtonWidget>("ZOOM_BUTTON");
|
||||
if (zoomDropdown != null)
|
||||
{
|
||||
var selectedZoom = Game.Settings.Graphics.PixelDouble ? 2f : 1f;
|
||||
var selectedLabel = selectedZoom.ToString();
|
||||
Func<float, ScrollItemWidget, ScrollItemWidget> setupItem = (zoom, itemTemplate) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(itemTemplate,
|
||||
() => selectedZoom == zoom,
|
||||
() => { worldRenderer.Viewport.Zoom = selectedZoom = zoom; selectedLabel = zoom.ToString(); });
|
||||
|
||||
var label = zoom.ToString();
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => label;
|
||||
|
||||
return item;
|
||||
};
|
||||
|
||||
var options = new[] { 2f, 1f, 0.5f, 0.25f };
|
||||
zoomDropdown.OnMouseDown = _ => zoomDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 150, options, setupItem);
|
||||
zoomDropdown.GetText = () => selectedLabel;
|
||||
zoomDropdown.GetKey = _ => Game.Settings.Keys.TogglePixelDoubleKey;
|
||||
zoomDropdown.OnKeyPress = e =>
|
||||
{
|
||||
var key = Hotkey.FromKeyInput(e);
|
||||
if (key != Game.Settings.Keys.TogglePixelDoubleKey)
|
||||
return;
|
||||
|
||||
var selected = (options.IndexOf(selectedZoom) + 1) % options.Length;
|
||||
worldRenderer.Viewport.Zoom = selectedZoom = options[selected];
|
||||
selectedLabel = selectedZoom.ToString();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class MapEditorTabsLogic
|
||||
{
|
||||
protected enum MenuType { Tiles, Layers, Actors }
|
||||
protected MenuType menuType = MenuType.Tiles;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public MapEditorTabsLogic(Widget widget, WorldRenderer worldRenderer)
|
||||
{
|
||||
var tabContainer = widget.Get("MAP_EDITOR_TAB_CONTAINER");
|
||||
|
||||
var tilesTab = tabContainer.Get<ButtonWidget>("TILES_TAB");
|
||||
tilesTab.IsHighlighted = () => menuType == MenuType.Tiles;
|
||||
tilesTab.OnClick = () => { menuType = MenuType.Tiles; };
|
||||
|
||||
var overlaysTab = tabContainer.Get<ButtonWidget>("OVERLAYS_TAB");
|
||||
overlaysTab.IsHighlighted = () => menuType == MenuType.Layers;
|
||||
overlaysTab.OnClick = () => { menuType = MenuType.Layers; };
|
||||
|
||||
var actorsTab = tabContainer.Get<ButtonWidget>("ACTORS_TAB");
|
||||
actorsTab.IsHighlighted = () => menuType == MenuType.Actors;
|
||||
actorsTab.OnClick = () => { menuType = MenuType.Actors; };
|
||||
|
||||
var tileContainer = widget.Parent.Get<ContainerWidget>("TILE_WIDGETS");
|
||||
tileContainer.IsVisible = () => menuType == MenuType.Tiles;
|
||||
|
||||
var layerContainer = widget.Parent.Get<ContainerWidget>("LAYER_WIDGETS");
|
||||
layerContainer.IsVisible = () => menuType == MenuType.Layers;
|
||||
|
||||
var actorContainer = widget.Parent.Get<ContainerWidget>("ACTOR_WIDGETS");
|
||||
actorContainer.IsVisible = () => menuType == MenuType.Actors;
|
||||
}
|
||||
}
|
||||
}
|
||||
94
OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs
Normal file
94
OpenRA.Mods.Common/Widgets/Logic/Editor/NewMapLogic.cs
Normal file
@@ -0,0 +1,94 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Mods.Common.Widgets;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class NewMapLogic
|
||||
{
|
||||
Widget panel;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public NewMapLogic(Action onExit, Action<string> onSelect, Ruleset modRules, Widget widget, World world)
|
||||
{
|
||||
panel = widget;
|
||||
|
||||
panel.Get<ButtonWidget>("CANCEL_BUTTON").OnClick = () => { Ui.CloseWindow(); onExit(); };
|
||||
|
||||
var tilesetDropDown = panel.Get<DropDownButtonWidget>("TILESET");
|
||||
var tilesets = modRules.TileSets.Select(t => t.Key).ToList();
|
||||
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(template,
|
||||
() => tilesetDropDown.Text == option,
|
||||
() => { tilesetDropDown.Text = option; });
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => option;
|
||||
return item;
|
||||
};
|
||||
tilesetDropDown.Text = tilesets.First();
|
||||
tilesetDropDown.OnClick = () =>
|
||||
tilesetDropDown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, tilesets, setupItem);
|
||||
|
||||
var widthTextField = panel.Get<TextFieldWidget>("WIDTH");
|
||||
var heightTextField = panel.Get<TextFieldWidget>("HEIGHT");
|
||||
|
||||
panel.Get<ButtonWidget>("CREATE_BUTTON").OnClick = () =>
|
||||
{
|
||||
var tileset = modRules.TileSets[tilesetDropDown.Text];
|
||||
var map = Map.FromTileset(tileset);
|
||||
|
||||
int width, height;
|
||||
int.TryParse(widthTextField.Text, out width);
|
||||
int.TryParse(heightTextField.Text, out height);
|
||||
|
||||
// Require at least a 2x2 playable area so that the
|
||||
// ground is visible through the edge shroud
|
||||
width = Math.Max(2, width);
|
||||
height = Math.Max(2, height);
|
||||
|
||||
map.Resize(width + 2, height + tileset.MaxGroundHeight + 2);
|
||||
map.ResizeCordon(1, 1, width + 1, height + tileset.MaxGroundHeight + 1);
|
||||
map.PlayerDefinitions = new MapPlayers(map.Rules, map.SpawnPoints.Value.Length).ToMiniYaml();
|
||||
map.FixOpenAreas(modRules);
|
||||
|
||||
var userMapFolder = Game.ModData.Manifest.MapFolders.First(f => f.Value == "User").Key;
|
||||
|
||||
// Ignore optional flag
|
||||
if (userMapFolder.StartsWith("~"))
|
||||
userMapFolder = userMapFolder.Substring(1);
|
||||
|
||||
var mapDir = Platform.ResolvePath(userMapFolder);
|
||||
Directory.CreateDirectory(mapDir);
|
||||
var tempLocation = Path.Combine(mapDir, "temp") + ".oramap";
|
||||
map.Save(tempLocation); // TODO: load it right away and save later properly
|
||||
|
||||
var newMap = new Map(tempLocation);
|
||||
Game.ModData.MapCache[newMap.Uid].UpdateFromMap(newMap, MapClassification.User);
|
||||
|
||||
ConnectionLogic.Connect(System.Net.IPAddress.Loopback.ToString(),
|
||||
Game.CreateLocalServer(newMap.Uid),
|
||||
"",
|
||||
() => { Game.LoadEditor(newMap.Uid); },
|
||||
() => { Game.CloseServer(); onExit(); });
|
||||
onSelect(newMap.Uid);
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
122
OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs
Normal file
122
OpenRA.Mods.Common/Widgets/Logic/Editor/SaveMapLogic.cs
Normal file
@@ -0,0 +1,122 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class SaveMapLogic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public SaveMapLogic(Widget widget, Action onExit, World world)
|
||||
{
|
||||
var newMap = world.Map;
|
||||
|
||||
var title = widget.GetOrNull<TextFieldWidget>("TITLE");
|
||||
if (title != null)
|
||||
title.Text = newMap.Title;
|
||||
|
||||
var description = widget.GetOrNull<TextFieldWidget>("DESCRIPTION");
|
||||
if (description != null)
|
||||
description.Text = newMap.Description;
|
||||
|
||||
var author = widget.GetOrNull<TextFieldWidget>("AUTHOR");
|
||||
if (author != null)
|
||||
author.Text = newMap.Author;
|
||||
|
||||
var visibilityDropdown = widget.GetOrNull<DropDownButtonWidget>("CLASS_DROPDOWN");
|
||||
if (visibilityDropdown != null)
|
||||
{
|
||||
var mapVisibility = new List<string>(Enum.GetNames(typeof(MapVisibility)));
|
||||
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(template,
|
||||
() => visibilityDropdown.Text == option,
|
||||
() => { visibilityDropdown.Text = option; });
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => option;
|
||||
return item;
|
||||
};
|
||||
visibilityDropdown.Text = Enum.GetName(typeof(MapVisibility), newMap.Visibility);
|
||||
visibilityDropdown.OnClick = () =>
|
||||
visibilityDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapVisibility, setupItem);
|
||||
}
|
||||
|
||||
var pathDropdown = widget.GetOrNull<DropDownButtonWidget>("PATH_DROPDOWN");
|
||||
if (pathDropdown != null)
|
||||
{
|
||||
var mapFolders = new List<string>();
|
||||
foreach (var mapFolder in Game.ModData.Manifest.MapFolders.Keys)
|
||||
{
|
||||
var folder = mapFolder;
|
||||
if (mapFolder.StartsWith("~"))
|
||||
folder = mapFolder.Substring(1);
|
||||
|
||||
mapFolders.Add(Platform.ResolvePath(folder));
|
||||
}
|
||||
|
||||
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(template,
|
||||
() => pathDropdown.Text == Platform.UnresolvePath(option),
|
||||
() => { pathDropdown.Text = Platform.UnresolvePath(option); });
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => option;
|
||||
return item;
|
||||
};
|
||||
|
||||
var userMapFolder = Game.ModData.Manifest.MapFolders.First(f => f.Value == "User").Key;
|
||||
if (userMapFolder.StartsWith("~"))
|
||||
userMapFolder = userMapFolder.Substring(1);
|
||||
pathDropdown.Text = Platform.UnresolvePath(userMapFolder);
|
||||
pathDropdown.OnClick = () =>
|
||||
pathDropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 210, mapFolders, setupItem);
|
||||
}
|
||||
|
||||
var filename = widget.GetOrNull<TextFieldWidget>("FILENAME");
|
||||
if (filename != null)
|
||||
filename.Text = Path.GetFileName(world.Map.Path);
|
||||
|
||||
var close = widget.GetOrNull<ButtonWidget>("CLOSE");
|
||||
if (close != null)
|
||||
close.OnClick = () => { Ui.CloseWindow(); onExit(); };
|
||||
|
||||
var save = widget.GetOrNull<ButtonWidget>("SAVE");
|
||||
if (save != null && !string.IsNullOrEmpty(filename.Text))
|
||||
{
|
||||
var editorLayer = world.WorldActor.Trait<EditorActorLayer>();
|
||||
save.OnClick = () =>
|
||||
{
|
||||
newMap.Title = title.Text;
|
||||
newMap.Description = description.Text;
|
||||
newMap.Author = author.Text;
|
||||
newMap.Visibility = (MapVisibility)Enum.Parse(typeof(MapVisibility), visibilityDropdown.Text);
|
||||
newMap.ActorDefinitions = editorLayer.Save();
|
||||
newMap.PlayerDefinitions = editorLayer.Players.ToMiniYaml();
|
||||
newMap.RequiresMod = Game.ModData.Manifest.Mod.Id;
|
||||
|
||||
var combinedPath = Path.Combine(pathDropdown.Text, filename.Text);
|
||||
var resolvedPath = Platform.ResolvePath(combinedPath);
|
||||
newMap.Save(resolvedPath);
|
||||
|
||||
// Update the map cache so it can be loaded without restarting the game
|
||||
Game.ModData.MapCache[newMap.Uid].UpdateFromMap(newMap, MapClassification.User);
|
||||
|
||||
Console.WriteLine("Saved current map at {0}", resolvedPath);
|
||||
Ui.CloseWindow();
|
||||
onExit();
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
92
OpenRA.Mods.Common/Widgets/Logic/Editor/TileSelectorLogic.cs
Normal file
92
OpenRA.Mods.Common/Widgets/Logic/Editor/TileSelectorLogic.cs
Normal file
@@ -0,0 +1,92 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using OpenRA.FileFormats;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class TileSelectorLogic
|
||||
{
|
||||
readonly EditorViewportControllerWidget editor;
|
||||
readonly ScrollPanelWidget panel;
|
||||
readonly ScrollItemWidget itemTemplate;
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public TileSelectorLogic(Widget widget, WorldRenderer worldRenderer, Ruleset modRules)
|
||||
{
|
||||
var tileset = modRules.TileSets[worldRenderer.World.Map.Tileset];
|
||||
|
||||
editor = widget.Parent.Get<EditorViewportControllerWidget>("MAP_EDITOR");
|
||||
panel = widget.Get<ScrollPanelWidget>("TILETEMPLATE_LIST");
|
||||
itemTemplate = panel.Get<ScrollItemWidget>("TILEPREVIEW_TEMPLATE");
|
||||
panel.Layout = new GridLayout(panel);
|
||||
|
||||
var tileCategorySelector = widget.Get<DropDownButtonWidget>("TILE_CATEGORY");
|
||||
var categories = tileset.EditorTemplateOrder;
|
||||
Func<string, ScrollItemWidget, ScrollItemWidget> setupItem = (option, template) =>
|
||||
{
|
||||
var item = ScrollItemWidget.Setup(template,
|
||||
() => tileCategorySelector.Text == option,
|
||||
() => { tileCategorySelector.Text = option; IntializeTilePreview(widget, worldRenderer, tileset, option); });
|
||||
|
||||
item.Get<LabelWidget>("LABEL").GetText = () => option;
|
||||
return item;
|
||||
};
|
||||
|
||||
tileCategorySelector.OnClick = () =>
|
||||
tileCategorySelector.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 270, categories, setupItem);
|
||||
|
||||
tileCategorySelector.Text = categories.First();
|
||||
IntializeTilePreview(widget, worldRenderer, tileset, categories.First());
|
||||
}
|
||||
|
||||
void IntializeTilePreview(Widget widget, WorldRenderer worldRenderer, TileSet tileset, string category)
|
||||
{
|
||||
panel.RemoveChildren();
|
||||
|
||||
var tileIds = tileset.Templates
|
||||
.Where(t => t.Value.Category == category)
|
||||
.Select(t => t.Value.Id);
|
||||
|
||||
foreach (var t in tileIds)
|
||||
{
|
||||
var tileId = t;
|
||||
var item = ScrollItemWidget.Setup(itemTemplate,
|
||||
() => { var brush = editor.CurrentBrush as EditorTileBrush; return brush != null && brush.Template == tileId; },
|
||||
() => editor.SetBrush(new EditorTileBrush(editor, tileId, worldRenderer)));
|
||||
|
||||
var preview = item.Get<TerrainTemplatePreviewWidget>("TILE_PREVIEW");
|
||||
var template = tileset.Templates[tileId];
|
||||
var bounds = worldRenderer.Theater.TemplateBounds(template, Game.ModData.Manifest.TileSize, worldRenderer.World.Map.TileShape);
|
||||
|
||||
// Scale templates to fit within the panel
|
||||
var scale = 1f;
|
||||
while (scale * bounds.Width > itemTemplate.Bounds.Width)
|
||||
scale /= 2;
|
||||
|
||||
preview.Template = template;
|
||||
preview.GetScale = () => scale;
|
||||
preview.Bounds.Width = (int)(scale * bounds.Width);
|
||||
preview.Bounds.Height = (int)(scale * bounds.Height);
|
||||
|
||||
item.Bounds.Width = preview.Bounds.Width + 2 * preview.Bounds.X;
|
||||
item.Bounds.Height = preview.Bounds.Height + 2 * preview.Bounds.Y;
|
||||
item.IsVisible = () => true;
|
||||
item.GetTooltipText = () => tileId.ToString();
|
||||
|
||||
panel.AddChild(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2015 The OpenRA Developers (see AUTHORS)
|
||||
* 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. For more information,
|
||||
* see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class LoadMapEditorLogic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public LoadMapEditorLogic(Widget widget, World world)
|
||||
{
|
||||
var editorRoot = widget.Get("WORLD_ROOT");
|
||||
Game.LoadWidget(world, "EDITOR_WORLD_ROOT", editorRoot, new WidgetArgs());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
public class SpriteWidget : Widget
|
||||
{
|
||||
public Func<float> GetScale = () => 1f;
|
||||
public string Palette = "chrome";
|
||||
public Func<string> GetPalette;
|
||||
public Func<Sprite> GetSprite;
|
||||
@@ -44,6 +45,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
Sprite cachedSprite = null;
|
||||
string cachedPalette = null;
|
||||
float cachedScale;
|
||||
PaletteReference pr;
|
||||
float2 offset = float2.Zero;
|
||||
|
||||
@@ -51,6 +53,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
var sprite = GetSprite();
|
||||
var palette = GetPalette();
|
||||
var scale = GetScale();
|
||||
|
||||
if (sprite == null || palette == null)
|
||||
return;
|
||||
@@ -67,7 +70,14 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
cachedPalette = palette;
|
||||
}
|
||||
|
||||
Game.Renderer.SpriteRenderer.DrawSprite(sprite, RenderOrigin + offset, pr);
|
||||
if (scale != cachedScale)
|
||||
{
|
||||
offset *= scale;
|
||||
cachedScale = scale;
|
||||
}
|
||||
|
||||
var size = new float2(sprite.Size.X * scale, sprite.Size.Y * scale);
|
||||
Game.Renderer.SpriteRenderer.DrawSprite(sprite, RenderOrigin + offset, pr, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user