From 3deacd8062510be104cdaeb0f136f93bd70f1007 Mon Sep 17 00:00:00 2001 From: Matthew Bowra-Dean Date: Thu, 15 Oct 2009 18:37:24 +1300 Subject: [PATCH] Beginnings of cursor support. --- OpenRa.Game/Cursor.cs | 29 ++++++++++++++++++ OpenRa.Game/Graphics/CursorSequence.cs | 39 ++++++++++++++++++++++++ OpenRa.Game/Graphics/SequenceProvider.cs | 18 +++++++++++ OpenRa.Game/OpenRa.Game.csproj | 2 ++ sequences.xml | 19 ++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 OpenRa.Game/Cursor.cs create mode 100644 OpenRa.Game/Graphics/CursorSequence.cs diff --git a/OpenRa.Game/Cursor.cs b/OpenRa.Game/Cursor.cs new file mode 100644 index 0000000000..4363dd7fbb --- /dev/null +++ b/OpenRa.Game/Cursor.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using OpenRa.Game.Graphics; +using System.IO; + +namespace OpenRa.Game +{ + public class Cursor + { + CursorSequence sequence; + Cursor(string cursor) + { + sequence = SequenceProvider.GetCursorSequence(cursor); + + } + + public static Cursor Default + { + get { return new Cursor("default"); } + } + + public static Cursor Move + { + get { return new Cursor("move"); } + } + } +} diff --git a/OpenRa.Game/Graphics/CursorSequence.cs b/OpenRa.Game/Graphics/CursorSequence.cs new file mode 100644 index 0000000000..c49ba51f28 --- /dev/null +++ b/OpenRa.Game/Graphics/CursorSequence.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; + +namespace OpenRa.Game.Graphics +{ + class CursorSequence + { + readonly int start, length; + + public int Start { get { return start; } } + public int End { get { return start + length; } } + public int Length { get { return length; } } + + Sprite[] sprites; + public CursorSequence(string cursorSrc, XmlElement e) + { + sprites = CursorSheetBuilder.LoadAllSprites(cursorSrc, ".shp"); + + start = int.Parse(e.GetAttribute("start")); + + if (e.GetAttribute("length") == "*" || e.GetAttribute("end") == "*") + length = sprites.Length - start; + else if (e.HasAttribute("length")) + length = int.Parse(e.GetAttribute("length")); + else if (e.HasAttribute("end")) + length = int.Parse(e.GetAttribute("end")) - start; + else + length = 1; + } + + public Sprite GetSprite(int frame) + { + return sprites[(frame % length) + start]; + } + } +} diff --git a/OpenRa.Game/Graphics/SequenceProvider.cs b/OpenRa.Game/Graphics/SequenceProvider.cs index c233ff2780..7d0b9fb357 100644 --- a/OpenRa.Game/Graphics/SequenceProvider.cs +++ b/OpenRa.Game/Graphics/SequenceProvider.cs @@ -9,6 +9,8 @@ namespace OpenRa.Game.Graphics static Dictionary> units = new Dictionary>(); + static Dictionary cursors = new Dictionary(); + static SequenceProvider() { XmlDocument document = new XmlDocument(); @@ -16,6 +18,17 @@ namespace OpenRa.Game.Graphics foreach (XmlElement eUnit in document.SelectNodes("/sequences/unit")) LoadSequencesForUnit(eUnit); + + foreach (XmlElement eCursor in document.SelectNodes("/sequences/cursor")) + LoadSequencesForCursor(eCursor); + } + + static void LoadSequencesForCursor(XmlElement eCursor) + { + string cursorSrc = eCursor.GetAttribute("src"); + + foreach (XmlElement eSequence in eCursor.SelectNodes("./sequence")) + cursors.Add(eSequence.GetAttribute("name"), new CursorSequence(cursorSrc, eSequence)); } public static void ForcePrecache() { } // force static ctor to run @@ -35,5 +48,10 @@ namespace OpenRa.Game.Graphics { return units[unitName][sequenceName]; } + + public static CursorSequence GetCursorSequence(string cursor) + { + return cursors[cursor]; + } } } diff --git a/OpenRa.Game/OpenRa.Game.csproj b/OpenRa.Game/OpenRa.Game.csproj index e53b7c6fc1..f5d6f3fddf 100644 --- a/OpenRa.Game/OpenRa.Game.csproj +++ b/OpenRa.Game/OpenRa.Game.csproj @@ -76,6 +76,7 @@ + @@ -86,6 +87,7 @@ + diff --git a/sequences.xml b/sequences.xml index 2bec7d02cb..59e6f56358 100644 --- a/sequences.xml +++ b/sequences.xml @@ -311,5 +311,24 @@ + + + + + + + + + + + + + + + + + + +