Add new map editor UI.

This commit is contained in:
Paul Chote
2015-04-06 14:57:13 +01:00
parent d211fe9fe1
commit 469f47aeea
28 changed files with 2142 additions and 11 deletions

View File

@@ -0,0 +1,134 @@
#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 EditorActorBrush : IEditorBrush
{
public readonly ActorInfo Actor;
readonly WorldRenderer worldRenderer;
readonly World world;
readonly EditorActorLayer editorLayer;
readonly EditorViewportControllerWidget editorWidget;
readonly ActorPreviewWidget preview;
readonly CVec locationOffset;
readonly WVec previewOffset;
readonly PlayerReference owner;
int facing = 92;
public EditorActorBrush(EditorViewportControllerWidget editorWidget, ActorInfo actor, PlayerReference owner, WorldRenderer wr)
{
this.editorWidget = editorWidget;
worldRenderer = wr;
world = wr.World;
editorLayer = world.WorldActor.Trait<EditorActorLayer>();
Actor = actor;
this.owner = owner;
preview = editorWidget.Get<ActorPreviewWidget>("DRAG_ACTOR_PREVIEW");
preview.GetScale = () => worldRenderer.Viewport.Zoom;
preview.IsVisible = () => editorWidget.CurrentBrush == this;
var buildingInfo = actor.Traits.GetOrDefault<BuildingInfo>();
if (buildingInfo != null)
{
locationOffset = -FootprintUtils.AdjustForBuildingSize(buildingInfo);
previewOffset = FootprintUtils.CenterOffset(world, buildingInfo);
}
var td = new TypeDictionary();
td.Add(new FacingInit(facing));
td.Add(new TurretFacingInit(facing));
td.Add(new OwnerInit(owner.Name));
td.Add(new RaceInit(owner.Race));
preview.SetPreview(actor, td);
// The preview widget may be rendered by the higher-level code before it is ticked.
// Force a manual tick to ensure the bounds are set correctly for this first draw.
Tick();
}
public bool HandleMouseInput(MouseInput mi)
{
// Exclusively uses left and right mouse buttons, but nothing else
if (mi.Button != MouseButton.Left && mi.Button != MouseButton.Right)
return false;
if (mi.Button == MouseButton.Right)
{
editorWidget.ClearBrush();
return true;
}
var cell = worldRenderer.Viewport.ViewToWorld(mi.Location);
if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down && world.Map.Contains(cell))
{
var newActorReference = new ActorReference(Actor.Name);
newActorReference.Add(new OwnerInit(owner.Name));
cell += locationOffset;
newActorReference.Add(new LocationInit(cell));
var ios = Actor.Traits.GetOrDefault<IOccupySpaceInfo>();
if (ios != null && ios.SharesCell)
{
var subcell = editorLayer.FreeSubCellAt(cell);
if (subcell != SubCell.Invalid)
newActorReference.Add(new SubCellInit(subcell));
}
var initDict = newActorReference.InitDict;
if (Actor.Traits.Contains<IFacingInfo>())
initDict.Add(new FacingInit(facing));
if (Actor.Traits.Contains<TurretedInfo>())
initDict.Add(new TurretFacingInit(facing));
editorLayer.Add(newActorReference);
}
return true;
}
public void Tick()
{
var cell = worldRenderer.Viewport.ViewToWorld(Viewport.LastMousePos);
var pos = world.Map.CenterOfCell(cell + locationOffset) + previewOffset;
var origin = worldRenderer.Viewport.WorldToViewPx(worldRenderer.ScreenPxPosition(pos));
var zoom = worldRenderer.Viewport.Zoom;
var s = preview.IdealPreviewSize;
var o = preview.PreviewOffset;
preview.Bounds.X = origin.X - (int)(zoom * (o.X + s.X / 2));
preview.Bounds.Y = origin.Y - (int)(zoom * (o.Y + s.Y / 2));
preview.Bounds.Width = (int)(zoom * s.X);
preview.Bounds.Height = (int)(zoom * s.Y);
}
}
}

View File

@@ -0,0 +1,114 @@
#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 interface IEditorBrush
{
bool HandleMouseInput(MouseInput mi);
void Tick();
}
public class EditorDefaultBrush : IEditorBrush
{
public readonly ActorInfo Actor;
readonly WorldRenderer worldRenderer;
readonly World world;
readonly EditorViewportControllerWidget editorWidget;
readonly EditorActorLayer editorLayer;
readonly Dictionary<int, ResourceType> resources;
public EditorDefaultBrush(EditorViewportControllerWidget editorWidget, WorldRenderer wr)
{
this.editorWidget = editorWidget;
worldRenderer = wr;
world = wr.World;
editorLayer = world.WorldActor.Trait<EditorActorLayer>();
resources = world.WorldActor.TraitsImplementing<ResourceType>()
.ToDictionary(r => r.Info.ResourceType, r => r);
}
public bool HandleMouseInput(MouseInput mi)
{
// Exclusively uses left and right mouse buttons, but nothing else
// Mouse move events are important for tooltips, so we always allow these through
if (mi.Button != MouseButton.Left && mi.Button != MouseButton.Right && mi.Event != MouseInputEvent.Move)
return false;
var cell = worldRenderer.Viewport.ViewToWorld(mi.Location);
if (mi.Event == MouseInputEvent.Up)
return true;
var underCursor = editorLayer.PreviewsAt(worldRenderer.Viewport.ViewToWorldPx(mi.Location))
.FirstOrDefault();
ResourceType type;
if (underCursor != null)
editorWidget.SetTooltip(underCursor.Tooltip);
else if (world.Map.Contains(cell) && resources.TryGetValue(world.Map.MapResources.Value[cell].Type, out type))
editorWidget.SetTooltip(type.Info.Name);
else
editorWidget.SetTooltip(null);
// Finished with mouse move events, so let them bubble up the widget tree
if (mi.Event == MouseInputEvent.Move)
return false;
if (mi.Button == MouseButton.Right)
{
editorWidget.SetTooltip(null);
if (underCursor != null)
editorLayer.Remove(underCursor);
if (world.Map.MapResources.Value[cell].Type != 0)
world.Map.MapResources.Value[cell] = new ResourceTile();
}
else if (mi.Button == MouseButton.Left && mi.Event == MouseInputEvent.Down)
{
if (underCursor != null)
{
// Test case / demonstration of how to edit an existing actor
var facing = underCursor.Init<FacingInit>();
if (facing != null)
underCursor.ReplaceInit(new FacingInit((facing.Value(world) + 32) % 256));
else if (underCursor.Info.Traits.WithInterface<UsesInit<FacingInit>>().Any())
underCursor.ReplaceInit(new FacingInit(32));
var turret = underCursor.Init<TurretFacingInit>();
if (turret != null)
underCursor.ReplaceInit(new TurretFacingInit((turret.Value(world) + 32) % 256));
else if (underCursor.Info.Traits.WithInterface<UsesInit<TurretFacingInit>>().Any())
underCursor.ReplaceInit(new TurretFacingInit(32));
}
}
return true;
}
public void Tick() { }
}
}

View File

@@ -0,0 +1,115 @@
#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 EditorResourceBrush : IEditorBrush
{
public readonly ResourceTypeInfo ResourceType;
readonly WorldRenderer worldRenderer;
readonly World world;
readonly EditorViewportControllerWidget editorWidget;
readonly SpriteWidget preview;
public EditorResourceBrush(EditorViewportControllerWidget editorWidget, ResourceTypeInfo resource, WorldRenderer wr)
{
this.editorWidget = editorWidget;
ResourceType = resource;
worldRenderer = wr;
world = wr.World;
preview = editorWidget.Get<SpriteWidget>("DRAG_LAYER_PREVIEW");
preview.Palette = resource.Palette;
preview.GetScale = () => worldRenderer.Viewport.Zoom;
preview.IsVisible = () => editorWidget.CurrentBrush == this;
var variant = resource.Variants.FirstOrDefault();
var sequenceProvider = wr.World.Map.Rules.Sequences[world.TileSet.Id];
var sequence = sequenceProvider.GetSequence("resources", variant);
var sprite = sequence.GetSprite(resource.MaxDensity - 1);
preview.GetSprite = () => sprite;
// The preview widget may be rendered by the higher-level code before it is ticked.
// Force a manual tick to ensure the bounds are set correctly for this first draw.
Tick();
}
public bool HandleMouseInput(MouseInput mi)
{
// Exclusively uses left and right mouse buttons, but nothing else
if (mi.Button != MouseButton.Left && mi.Button != MouseButton.Right)
return false;
if (mi.Button == MouseButton.Right)
{
editorWidget.ClearBrush();
return true;
}
var cell = worldRenderer.Viewport.ViewToWorld(mi.Location);
if (mi.Button == MouseButton.Left && AllowResourceAt(cell))
{
var type = (byte)ResourceType.ResourceType;
var index = (byte)ResourceType.MaxDensity;
world.Map.MapResources.Value[cell] = new ResourceTile(type, index);
}
return true;
}
public bool AllowResourceAt(CPos cell)
{
if (!world.Map.Contains(cell))
return false;
var tile = world.Map.MapTiles.Value[cell];
var tileInfo = world.TileSet.GetTileInfo(tile);
var terrainType = world.TileSet.TerrainInfo[tileInfo.TerrainType];
if (world.Map.MapResources.Value[cell].Type == ResourceType.ResourceType)
return false;
if (!ResourceType.AllowedTerrainTypes.Contains(terrainType.Type))
return false;
return ResourceType.AllowOnRamps || tileInfo == null || tileInfo.RampType == 0;
}
public void Tick()
{
var cell = worldRenderer.Viewport.ViewToWorld(Viewport.LastMousePos);
var offset = WVec.Zero;
var location = world.Map.CenterOfCell(cell) + offset;
var cellScreenPosition = worldRenderer.ScreenPxPosition(location);
var cellScreenPixel = worldRenderer.Viewport.WorldToViewPx(cellScreenPosition);
var zoom = worldRenderer.Viewport.Zoom;
preview.Bounds.X = cellScreenPixel.X;
preview.Bounds.Y = cellScreenPixel.Y;
}
}
}

View File

@@ -0,0 +1,151 @@
#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 EditorTileBrush : IEditorBrush
{
public readonly ushort Template;
readonly WorldRenderer worldRenderer;
readonly World world;
readonly EditorViewportControllerWidget editorWidget;
readonly TerrainTemplatePreviewWidget preview;
readonly Rectangle bounds;
bool painting;
public EditorTileBrush(EditorViewportControllerWidget editorWidget, ushort template, WorldRenderer wr)
{
this.editorWidget = editorWidget;
Template = template;
worldRenderer = wr;
world = wr.World;
preview = editorWidget.Get<TerrainTemplatePreviewWidget>("DRAG_TILE_PREVIEW");
preview.GetScale = () => worldRenderer.Viewport.Zoom;
preview.IsVisible = () => editorWidget.CurrentBrush == this;
preview.Template = world.TileSet.Templates.First(t => t.Value.Id == template).Value;
bounds = worldRenderer.Theater.TemplateBounds(preview.Template, Game.ModData.Manifest.TileSize, world.Map.TileShape);
// The preview widget may be rendered by the higher-level code before it is ticked.
// Force a manual tick to ensure the bounds are set correctly for this first draw.
Tick();
}
public bool HandleMouseInput(MouseInput mi)
{
// Exclusively uses left and right mouse buttons, but nothing else
if (mi.Button != MouseButton.Left && mi.Button != MouseButton.Right)
return false;
if (mi.Button == MouseButton.Right)
{
editorWidget.ClearBrush();
return true;
}
if (mi.Button == MouseButton.Left)
{
if (mi.Event == MouseInputEvent.Down)
painting = true;
else if (mi.Event == MouseInputEvent.Up)
painting = false;
}
if (!painting)
return true;
var map = world.Map;
var cell = worldRenderer.Viewport.ViewToWorld(mi.Location);
if (mi.Event != MouseInputEvent.Down && mi.Event != MouseInputEvent.Move)
return true;
var rules = map.Rules;
var tileset = rules.TileSets[map.Tileset];
var template = tileset.Templates[Template];
var baseHeight = map.Contains(cell) ? map.MapHeight.Value[cell] : 0;
if (mi.Event == MouseInputEvent.Move && PlacementOverlapsSameTemplate(template, cell))
return true;
var i = 0;
for (var y = 0; y < template.Size.Y; y++)
{
for (var x = 0; x < template.Size.X; x++, i++)
{
if (template.Contains(i) && template[i] != null)
{
var index = template.PickAny ? (byte)Game.CosmeticRandom.Next(0, template.TilesCount) : (byte)i;
var c = cell + new CVec(x, y);
if (!map.Contains(c))
continue;
map.MapTiles.Value[c] = new TerrainTile(Template, index);
map.MapHeight.Value[c] = (byte)(baseHeight + template[index].Height).Clamp(0, world.TileSet.MaxGroundHeight);
}
}
}
return true;
}
bool PlacementOverlapsSameTemplate(TerrainTemplateInfo template, CPos cell)
{
var map = world.Map;
var i = 0;
for (var y = 0; y < template.Size.Y; y++)
{
for (var x = 0; x < template.Size.X; x++, i++)
{
if (template.Contains(i) && template[i] != null)
{
var c = cell + new CVec(x, y);
if (map.Contains(c) && map.MapTiles.Value[c].Type == template.Id)
return true;
}
}
}
return false;
}
public void Tick()
{
var cell = worldRenderer.Viewport.ViewToWorld(Viewport.LastMousePos);
var offset = WVec.Zero;
var location = world.Map.CenterOfCell(cell) + offset;
var cellScreenPosition = worldRenderer.ScreenPxPosition(location);
var cellScreenPixel = worldRenderer.Viewport.WorldToViewPx(cellScreenPosition);
var zoom = worldRenderer.Viewport.Zoom;
preview.Bounds.X = cellScreenPixel.X + (int)(zoom * bounds.X);
preview.Bounds.Y = cellScreenPixel.Y + (int)(zoom * bounds.Y);
preview.Bounds.Width = (int)(zoom * bounds.Width);
preview.Bounds.Height = (int)(zoom * bounds.Height);
}
}
}