Move hardcoded viewport keys into yaml.

This commit is contained in:
Paul Chote
2017-09-04 17:35:09 +00:00
committed by reaperrr
parent 2a6bb0678e
commit b3b2efa781
7 changed files with 170 additions and 107 deletions

View File

@@ -192,25 +192,25 @@ namespace OpenRA
public Hotkey SelectAllUnitsKey = new Hotkey(Keycode.Q, Modifiers.None); public Hotkey SelectAllUnitsKey = new Hotkey(Keycode.Q, Modifiers.None);
public Hotkey SelectUnitsByTypeKey = new Hotkey(Keycode.W, Modifiers.None); public Hotkey SelectUnitsByTypeKey = new Hotkey(Keycode.W, Modifiers.None);
public Hotkey MapScrollUp = new Hotkey(Keycode.UP, Modifiers.None); public Hotkey MapScrollUpKey = new Hotkey(Keycode.UP, Modifiers.None);
public Hotkey MapScrollDown = new Hotkey(Keycode.DOWN, Modifiers.None); public Hotkey MapScrollDownKey = new Hotkey(Keycode.DOWN, Modifiers.None);
public Hotkey MapScrollLeft = new Hotkey(Keycode.LEFT, Modifiers.None); public Hotkey MapScrollLeftKey = new Hotkey(Keycode.LEFT, Modifiers.None);
public Hotkey MapScrollRight = new Hotkey(Keycode.RIGHT, Modifiers.None); public Hotkey MapScrollRightKey = new Hotkey(Keycode.RIGHT, Modifiers.None);
public Hotkey MapPushTop = new Hotkey(Keycode.UP, Modifiers.Alt); public Hotkey MapJumpToTopEdgeKey = new Hotkey(Keycode.UP, Modifiers.Alt);
public Hotkey MapPushBottom = new Hotkey(Keycode.DOWN, Modifiers.Alt); public Hotkey MapJumpToBottomEdgeKey = new Hotkey(Keycode.DOWN, Modifiers.Alt);
public Hotkey MapPushLeftEdge = new Hotkey(Keycode.LEFT, Modifiers.Alt); public Hotkey MapJumpToLeftEdgeKey = new Hotkey(Keycode.LEFT, Modifiers.Alt);
public Hotkey MapPushRightEdge = new Hotkey(Keycode.RIGHT, Modifiers.Alt); public Hotkey MapJumpToRightEdgeKey = new Hotkey(Keycode.RIGHT, Modifiers.Alt);
public Hotkey ViewPortBookmarkSaveSlot1 = new Hotkey(Keycode.Q, Modifiers.Ctrl); public Hotkey MapBookmarkSave01Key = new Hotkey(Keycode.Q, Modifiers.Ctrl);
public Hotkey ViewPortBookmarkSaveSlot2 = new Hotkey(Keycode.W, Modifiers.Ctrl); public Hotkey MapBookmarkSave02Key = new Hotkey(Keycode.W, Modifiers.Ctrl);
public Hotkey ViewPortBookmarkSaveSlot3 = new Hotkey(Keycode.E, Modifiers.Ctrl); public Hotkey MapBookmarkSave03Key = new Hotkey(Keycode.E, Modifiers.Ctrl);
public Hotkey ViewPortBookmarkSaveSlot4 = new Hotkey(Keycode.R, Modifiers.Ctrl); public Hotkey MapBookmarkSave04Key = new Hotkey(Keycode.R, Modifiers.Ctrl);
public Hotkey ViewPortBookmarkUseSlot1 = new Hotkey(Keycode.Q, Modifiers.Alt); public Hotkey MapBookmarkRestore01Key = new Hotkey(Keycode.Q, Modifiers.Alt);
public Hotkey ViewPortBookmarkUseSlot2 = new Hotkey(Keycode.W, Modifiers.Alt); public Hotkey MapBookmarkRestore02Key = new Hotkey(Keycode.W, Modifiers.Alt);
public Hotkey ViewPortBookmarkUseSlot3 = new Hotkey(Keycode.E, Modifiers.Alt); public Hotkey MapBookmarkRestore03Key = new Hotkey(Keycode.E, Modifiers.Alt);
public Hotkey ViewPortBookmarkUseSlot4 = new Hotkey(Keycode.R, Modifiers.Alt); public Hotkey MapBookmarkRestore04Key = new Hotkey(Keycode.R, Modifiers.Alt);
public Hotkey PauseKey = new Hotkey(Keycode.PAUSE, Modifiers.None); public Hotkey PauseKey = new Hotkey(Keycode.PAUSE, Modifiers.None);
public Hotkey PlaceBeaconKey = new Hotkey(Keycode.B, Modifiers.None); public Hotkey PlaceBeaconKey = new Hotkey(Keycode.B, Modifiers.None);

View File

@@ -463,24 +463,24 @@ namespace OpenRA.Mods.Common.Widgets.Logic
{ {
var hotkeys = new Dictionary<string, string>() var hotkeys = new Dictionary<string, string>()
{ {
{ "MapScrollUp", "Scroll up" }, { "MapScrollUpKey", "Scroll up" },
{ "MapScrollDown", "Scroll down" }, { "MapScrollDownKey", "Scroll down" },
{ "MapScrollLeft", "Scroll left" }, { "MapScrollLeftKey", "Scroll left" },
{ "MapScrollRight", "Scroll right" }, { "MapScrollRightKey", "Scroll right" },
{ "MapPushTop", "Jump to top edge" }, { "MapJumpToTopEdgeKey", "Jump to top edge" },
{ "MapPushBottom", "Jump to bottom edge" }, { "MapJumpToBottomEdgeKey", "Jump to bottom edge" },
{ "MapPushLeftEdge", "Jump to left edge" }, { "MapJumpToLeftEdgeKey", "Jump to left edge" },
{ "MapPushRightEdge", "Jump to right edge" }, { "MapJumpToRightEdgeKey", "Jump to right edge" },
{ "ViewPortBookmarkSaveSlot1", "Record bookmark #1" }, { "MapBookmarkSave01Key", "Record bookmark #1" },
{ "ViewPortBookmarkUseSlot1", "Jump to bookmark #1" }, { "MapBookmarkRestore01Key", "Jump to bookmark #1" },
{ "ViewPortBookmarkSaveSlot2", "Record bookmark #2" }, { "MapBookmarkSave02Key", "Record bookmark #2" },
{ "ViewPortBookmarkUseSlot2", "Jump to bookmark #2" }, { "MapBookmarkRestore02Key", "Jump to bookmark #2" },
{ "ViewPortBookmarkSaveSlot3", "Record bookmark #3" }, { "MapBookmarkSave03Key", "Record bookmark #3" },
{ "ViewPortBookmarkUseSlot3", "Jump to bookmark #3" }, { "MapBookmarkRestore03Key", "Jump to bookmark #3" },
{ "ViewPortBookmarkSaveSlot4", "Record bookmark #4" }, { "MapBookmarkSave04Key", "Record bookmark #4" },
{ "ViewPortBookmarkUseSlot4", "Jump to bookmark #4" } { "MapBookmarkRestore04Key", "Jump to bookmark #4" }
}; };
var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing); var header = ScrollItemWidget.Setup(hotkeyHeader, returnTrue, doNothing);

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Lint;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -25,9 +26,23 @@ namespace OpenRA.Mods.Common.Widgets
{ {
readonly ResourceLayer resourceLayer; readonly ResourceLayer resourceLayer;
public readonly NamedHotkey ScrollUpKey = new NamedHotkey();
public readonly NamedHotkey ScrollDownKey = new NamedHotkey();
public readonly NamedHotkey ScrollLeftKey = new NamedHotkey();
public readonly NamedHotkey ScrollRightKey = new NamedHotkey();
public readonly NamedHotkey JumpToTopEdgeKey = new NamedHotkey();
public readonly NamedHotkey JumpToBottomEdgeKey = new NamedHotkey();
public readonly NamedHotkey JumpToLeftEdgeKey = new NamedHotkey();
public readonly NamedHotkey JumpToRightEdgeKey = new NamedHotkey();
// Note: LinterHotkeyNames assumes that these are disabled by default
public readonly string BookmarkSaveKeyPrefix = null;
public readonly string BookmarkRestoreKeyPrefix = null;
public readonly int BookmarkKeyCount = 0;
public readonly string TooltipTemplate = "WORLD_TOOLTIP"; public readonly string TooltipTemplate = "WORLD_TOOLTIP";
public readonly string TooltipContainer; public readonly string TooltipContainer;
Lazy<TooltipContainerWidget> tooltipContainer;
public WorldTooltipType TooltipType { get; private set; } public WorldTooltipType TooltipType { get; private set; }
public ITooltip ActorTooltip { get; private set; } public ITooltip ActorTooltip { get; private set; }
@@ -35,10 +50,6 @@ namespace OpenRA.Mods.Common.Widgets
public FrozenActor FrozenActorTooltip { get; private set; } public FrozenActor FrozenActorTooltip { get; private set; }
public ResourceType ResourceTooltip { get; private set; } public ResourceType ResourceTooltip { get; private set; }
int2? joystickScrollStart, joystickScrollEnd;
int2? standardScrollStart;
bool isStandardScrolling;
static readonly Dictionary<ScrollDirection, string> ScrollCursors = new Dictionary<ScrollDirection, string> static readonly Dictionary<ScrollDirection, string> ScrollCursors = new Dictionary<ScrollDirection, string>
{ {
{ ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" }, { ScrollDirection.Up | ScrollDirection.Left, "scroll-tl" },
@@ -71,32 +82,53 @@ namespace OpenRA.Mods.Common.Widgets
{ ScrollDirection.Right, new float2(1, 0) }, { ScrollDirection.Right, new float2(1, 0) },
}; };
Lazy<TooltipContainerWidget> tooltipContainer;
int2? joystickScrollStart, joystickScrollEnd;
int2? standardScrollStart;
bool isStandardScrolling;
ScrollDirection keyboardDirections; ScrollDirection keyboardDirections;
ScrollDirection edgeDirections; ScrollDirection edgeDirections;
World world; World world;
WorldRenderer worldRenderer; WorldRenderer worldRenderer;
WPos?[] viewPortBookmarkSlots = new WPos?[4];
void SaveBookmark(int index, WPos position) NamedHotkey[] saveBookmarkHotkeys;
NamedHotkey[] restoreBookmarkHotkeys;
WPos?[] bookmarkPositions;
[CustomLintableHotkeyNames]
public static IEnumerable<string> LinterHotkeyNames(MiniYamlNode widgetNode, Action<string> emitError, Action<string> emitWarning)
{ {
viewPortBookmarkSlots[index] = position; var savePrefix = "";
var savePrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "BookmarkSaveKeyPrefix");
if (savePrefixNode != null)
savePrefix = savePrefixNode.Value.Value;
var restorePrefix = "";
var restorePrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "BookmarkRestoreKeyPrefix");
if (restorePrefixNode != null)
restorePrefix = restorePrefixNode.Value.Value;
var count = 0;
var countNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "BookmarkKeyCount");
if (countNode != null)
count = FieldLoader.GetValue<int>("BookmarkKeyCount", countNode.Value.Value);
if (count == 0)
yield break;
if (string.IsNullOrEmpty(savePrefix))
emitError("{0} must define BookmarkSaveKeyPrefix if BookmarkKeyCount > 0.".F(widgetNode.Location));
if (string.IsNullOrEmpty(restorePrefix))
emitError("{0} must define BookmarkRestoreKeyPrefix if BookmarkKeyCount > 0.".F(widgetNode.Location));
for (var i = 0; i < count; i++)
{
var suffix = (i + 1).ToString("D2");
yield return savePrefix + suffix;
yield return restorePrefix + suffix;
} }
void SaveCurrentPositionToBookmark(int index)
{
SaveBookmark(index, worldRenderer.Viewport.CenterPosition);
}
WPos? JumpToBookmark(int index)
{
return viewPortBookmarkSlots[index];
}
void JumpToSavedBookmark(int index)
{
var bookmark = JumpToBookmark(index);
if (bookmark != null)
worldRenderer.Viewport.Center((WPos)bookmark);
} }
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
@@ -110,6 +142,19 @@ namespace OpenRA.Mods.Common.Widgets
resourceLayer = world.WorldActor.TraitOrDefault<ResourceLayer>(); resourceLayer = world.WorldActor.TraitOrDefault<ResourceLayer>();
} }
public override void Initialize(WidgetArgs args)
{
base.Initialize(args);
saveBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
i => new NamedHotkey(BookmarkSaveKeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys));
restoreBookmarkHotkeys = Exts.MakeArray(BookmarkKeyCount,
i => new NamedHotkey(BookmarkRestoreKeyPrefix + (i + 1).ToString("D2"), Game.Settings.Keys));
bookmarkPositions = new WPos?[BookmarkKeyCount];
}
public override void MouseEntered() public override void MouseEntered()
{ {
if (TooltipContainer == null) if (TooltipContainer == null)
@@ -381,97 +426,71 @@ namespace OpenRA.Mods.Common.Widgets
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
var key = Hotkey.FromKeyInput(e); var key = Hotkey.FromKeyInput(e);
var ks = Game.Settings.Keys;
Func<Hotkey, ScrollDirection, bool> handleMapScrollKey = (hotkey, scrollDirection) => Func<NamedHotkey, ScrollDirection, bool> handleMapScrollKey = (hotkey, scrollDirection) =>
{ {
var isHotkey = false; var isHotkey = false;
if (key.Key == hotkey.Key) var keyValue = hotkey.GetValue();
if (key.Key == keyValue.Key)
{ {
isHotkey = key == hotkey; isHotkey = key == keyValue;
keyboardDirections = keyboardDirections.Set(scrollDirection, e.Event == KeyInputEvent.Down && (isHotkey || hotkey.Modifiers == Modifiers.None)); keyboardDirections = keyboardDirections.Set(scrollDirection, e.Event == KeyInputEvent.Down && (isHotkey || keyValue.Modifiers == Modifiers.None));
} }
return isHotkey; return isHotkey;
}; };
if (handleMapScrollKey(ks.MapScrollUp, ScrollDirection.Up) || handleMapScrollKey(ks.MapScrollDown, ScrollDirection.Down) if (handleMapScrollKey(ScrollUpKey, ScrollDirection.Up) || handleMapScrollKey(ScrollDownKey, ScrollDirection.Down)
|| handleMapScrollKey(ks.MapScrollLeft, ScrollDirection.Left) || handleMapScrollKey(ks.MapScrollRight, ScrollDirection.Right)) || handleMapScrollKey(ScrollLeftKey, ScrollDirection.Left) || handleMapScrollKey(ScrollRightKey, ScrollDirection.Right))
return true; return true;
if (e.Event != KeyInputEvent.Down) if (e.Event != KeyInputEvent.Down)
return false; return false;
if (key == ks.MapPushTop) if (key == JumpToTopEdgeKey.GetValue())
{ {
worldRenderer.Viewport.Center(new WPos(worldRenderer.Viewport.CenterPosition.X, 0, 0)); worldRenderer.Viewport.Center(new WPos(worldRenderer.Viewport.CenterPosition.X, 0, 0));
return true; return true;
} }
if (key == ks.MapPushBottom) if (key == JumpToBottomEdgeKey.GetValue())
{ {
worldRenderer.Viewport.Center(new WPos(worldRenderer.Viewport.CenterPosition.X, worldRenderer.World.Map.ProjectedBottomRight.Y, 0)); worldRenderer.Viewport.Center(new WPos(worldRenderer.Viewport.CenterPosition.X, worldRenderer.World.Map.ProjectedBottomRight.Y, 0));
return true; return true;
} }
if (key == ks.MapPushLeftEdge) if (key == JumpToLeftEdgeKey.GetValue())
{ {
worldRenderer.Viewport.Center(new WPos(0, worldRenderer.Viewport.CenterPosition.Y, 0)); worldRenderer.Viewport.Center(new WPos(0, worldRenderer.Viewport.CenterPosition.Y, 0));
return true; return true;
} }
if (key == ks.MapPushRightEdge) if (key == JumpToRightEdgeKey.GetValue())
{ {
worldRenderer.Viewport.Center(new WPos(worldRenderer.World.Map.ProjectedBottomRight.X, worldRenderer.Viewport.CenterPosition.Y, 0)); worldRenderer.Viewport.Center(new WPos(worldRenderer.World.Map.ProjectedBottomRight.X, worldRenderer.Viewport.CenterPosition.Y, 0));
return true; return true;
} }
if (key == ks.ViewPortBookmarkSaveSlot1) for (var i = 0; i < saveBookmarkHotkeys.Length; i++)
{ {
SaveCurrentPositionToBookmark(0); if (key == saveBookmarkHotkeys[i].GetValue())
{
bookmarkPositions[i] = worldRenderer.Viewport.CenterPosition;
return true; return true;
} }
if (key == ks.ViewPortBookmarkSaveSlot2)
{
SaveCurrentPositionToBookmark(1);
return true;
} }
if (key == ks.ViewPortBookmarkSaveSlot3) for (var i = 0; i < restoreBookmarkHotkeys.Length; i++)
{ {
SaveCurrentPositionToBookmark(2); if (key == restoreBookmarkHotkeys[i].GetValue())
{
var bookmark = bookmarkPositions[i];
if (bookmark.HasValue)
{
worldRenderer.Viewport.Center(bookmark.Value);
return true; return true;
} }
if (key == ks.ViewPortBookmarkSaveSlot4)
{
SaveCurrentPositionToBookmark(3);
return true;
} }
if (key == ks.ViewPortBookmarkUseSlot1)
{
JumpToSavedBookmark(0);
return true;
}
if (key == ks.ViewPortBookmarkUseSlot2)
{
JumpToSavedBookmark(1);
return true;
}
if (key == ks.ViewPortBookmarkUseSlot3)
{
JumpToSavedBookmark(2);
return true;
}
if (key == ks.ViewPortBookmarkUseSlot4)
{
JumpToSavedBookmark(3);
return true;
} }
return false; return false;

View File

@@ -224,6 +224,17 @@ Container@EDITOR_WORLD_ROOT:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM
IgnoreMouseOver: True IgnoreMouseOver: True
ScrollUpKey: MapScrollUp
ScrollDownKey: MapScrollDown
ScrollLeftKey: MapScrollLeft
ScrollRightKey: MapScrollRight
JumpToTopEdgeKey: MapJumpToTopEdge
JumpToBottomEdgeKey: MapJumpToBottomEdge
JumpToLeftEdgeKey: MapJumpToLeftEdge
JumpToRightEdgeKey: MapJumpToRightEdge
BookmarkSaveKeyPrefix: MapBookmarkSave
BookmarkRestoreKeyPrefix: MapBookmarkRestore
BookmarkKeyCount: 4
Background@RADAR_BG: Background@RADAR_BG:
X: WINDOW_RIGHT - 255 X: WINDOW_RIGHT - 255
Y: 5 Y: 5

View File

@@ -33,6 +33,17 @@ Container@INGAME_ROOT:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM
TooltipContainer: TOOLTIP_CONTAINER TooltipContainer: TOOLTIP_CONTAINER
ScrollUpKey: MapScrollUp
ScrollDownKey: MapScrollDown
ScrollLeftKey: MapScrollLeft
ScrollRightKey: MapScrollRight
JumpToTopEdgeKey: MapJumpToTopEdge
JumpToBottomEdgeKey: MapJumpToBottomEdge
JumpToLeftEdgeKey: MapJumpToLeftEdge
JumpToRightEdgeKey: MapJumpToRightEdge
BookmarkSaveKeyPrefix: MapBookmarkSave
BookmarkRestoreKeyPrefix: MapBookmarkRestore
BookmarkKeyCount: 4
WorldCommand: WorldCommand:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM

View File

@@ -215,6 +215,17 @@ Container@EDITOR_WORLD_ROOT:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM
IgnoreMouseOver: True IgnoreMouseOver: True
ScrollUpKey: MapScrollUp
ScrollDownKey: MapScrollDown
ScrollLeftKey: MapScrollLeft
ScrollRightKey: MapScrollRight
JumpToTopEdgeKey: MapJumpToTopEdge
JumpToBottomEdgeKey: MapJumpToBottomEdge
JumpToLeftEdgeKey: MapJumpToLeftEdge
JumpToRightEdgeKey: MapJumpToRightEdge
BookmarkSaveKeyPrefix: MapBookmarkSave
BookmarkRestoreKeyPrefix: MapBookmarkRestore
BookmarkKeyCount: 4
Background@RADAR_BG: Background@RADAR_BG:
X: WINDOW_RIGHT - 255 X: WINDOW_RIGHT - 255
Y: 5 Y: 5

View File

@@ -29,6 +29,17 @@ Container@INGAME_ROOT:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM
TooltipContainer: TOOLTIP_CONTAINER TooltipContainer: TOOLTIP_CONTAINER
ScrollUpKey: MapScrollUp
ScrollDownKey: MapScrollDown
ScrollLeftKey: MapScrollLeft
ScrollRightKey: MapScrollRight
JumpToTopEdgeKey: MapJumpToTopEdge
JumpToBottomEdgeKey: MapJumpToBottomEdge
JumpToLeftEdgeKey: MapJumpToLeftEdge
JumpToRightEdgeKey: MapJumpToRightEdge
BookmarkSaveKeyPrefix: MapBookmarkSave
BookmarkRestoreKeyPrefix: MapBookmarkRestore
BookmarkKeyCount: 4
WorldCommand: WorldCommand:
Width: WINDOW_RIGHT Width: WINDOW_RIGHT
Height: WINDOW_BOTTOM Height: WINDOW_BOTTOM