From 929dccb75a1405737975d76648e015a3e7c00177 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 7 Oct 2017 23:07:10 -0400 Subject: reorganise repo structure --- src/SMAPI/Utilities/SButton.cs | 675 +++++++++++++++++++++++++++++++++++++++++ src/SMAPI/Utilities/SDate.cs | 232 ++++++++++++++ 2 files changed, 907 insertions(+) create mode 100644 src/SMAPI/Utilities/SButton.cs create mode 100644 src/SMAPI/Utilities/SDate.cs (limited to 'src/SMAPI/Utilities') diff --git a/src/SMAPI/Utilities/SButton.cs b/src/SMAPI/Utilities/SButton.cs new file mode 100644 index 00000000..fa5ae648 --- /dev/null +++ b/src/SMAPI/Utilities/SButton.cs @@ -0,0 +1,675 @@ +using System; +using Microsoft.Xna.Framework.Input; +using StardewValley; + +namespace StardewModdingAPI.Utilities +{ + /// A unified button constant which includes all controller, keyboard, and mouse buttons. + /// Derived from , , and . + public enum SButton + { + /// No valid key. + None = 0, + + /********* + ** Mouse + *********/ + /// The left mouse button. + MouseLeft = 1000, + + /// The right mouse button. + MouseRight = 1001, + + /// The middle mouse button. + MouseMiddle = 1002, + + /// The first mouse XButton. + MouseX1 = 1003, + + /// The second mouse XButton. + MouseX2 = 1004, + + /********* + ** Controller + *********/ + /// The 'A' button on a controller. + ControllerA = SButtonExtensions.ControllerOffset + Buttons.A, + + /// The 'B' button on a controller. + ControllerB = SButtonExtensions.ControllerOffset + Buttons.B, + + /// The 'X' button on a controller. + ControllerX = SButtonExtensions.ControllerOffset + Buttons.X, + + /// The 'Y' button on a controller. + ControllerY = SButtonExtensions.ControllerOffset + Buttons.Y, + + /// The back button on a controller. + ControllerBack = SButtonExtensions.ControllerOffset + Buttons.Back, + + /// The start button on a controller. + ControllerStart = SButtonExtensions.ControllerOffset + Buttons.Start, + + /// The up button on the directional pad of a controller. + DPadUp = SButtonExtensions.ControllerOffset + Buttons.DPadUp, + + /// The down button on the directional pad of a controller. + DPadDown = SButtonExtensions.ControllerOffset + Buttons.DPadDown, + + /// The left button on the directional pad of a controller. + DPadLeft = SButtonExtensions.ControllerOffset + Buttons.DPadLeft, + + /// The right button on the directional pad of a controller. + DPadRight = SButtonExtensions.ControllerOffset + Buttons.DPadRight, + + /// The left bumper (shoulder) button on a controller. + LeftShoulder = SButtonExtensions.ControllerOffset + Buttons.LeftShoulder, + + /// The right bumper (shoulder) button on a controller. + RightShoulder = SButtonExtensions.ControllerOffset + Buttons.RightShoulder, + + /// The left trigger on a controller. + LeftTrigger = SButtonExtensions.ControllerOffset + Buttons.LeftTrigger, + + /// The right trigger on a controller. + RightTrigger = SButtonExtensions.ControllerOffset + Buttons.RightTrigger, + + /// The left analog stick on a controller (when pressed). + LeftStick = SButtonExtensions.ControllerOffset + Buttons.LeftStick, + + /// The right analog stick on a controller (when pressed). + RightStick = SButtonExtensions.ControllerOffset + Buttons.RightStick, + + /// The 'big button' on a controller. + BigButton = SButtonExtensions.ControllerOffset + Buttons.BigButton, + + /// The left analog stick on a controller (when pushed left). + LeftThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickLeft, + + /// The left analog stick on a controller (when pushed right). + LeftThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickRight, + + /// The left analog stick on a controller (when pushed down). + LeftThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickDown, + + /// The left analog stick on a controller (when pushed up). + LeftThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickUp, + + /// The right analog stick on a controller (when pushed left). + RightThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickLeft, + + /// The right analog stick on a controller (when pushed right). + RightThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickRight, + + /// The right analog stick on a controller (when pushed down). + RightThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickDown, + + /// The right analog stick on a controller (when pushed up). + RightThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickUp, + + /********* + ** Keyboard + *********/ + /// The A button on a keyboard. + A = Keys.A, + + /// The Add button on a keyboard. + Add = Keys.Add, + + /// The Applications button on a keyboard. + Apps = Keys.Apps, + + /// The Attn button on a keyboard. + Attn = Keys.Attn, + + /// The B button on a keyboard. + B = Keys.B, + + /// The Backspace button on a keyboard. + Back = Keys.Back, + + /// The Browser Back button on a keyboard in Windows 2000/XP. + BrowserBack = Keys.BrowserBack, + + /// The Browser Favorites button on a keyboard in Windows 2000/XP. + BrowserFavorites = Keys.BrowserFavorites, + + /// The Browser Favorites button on a keyboard in Windows 2000/XP. + BrowserForward = Keys.BrowserForward, + + /// The Browser Home button on a keyboard in Windows 2000/XP. + BrowserHome = Keys.BrowserHome, + + /// The Browser Refresh button on a keyboard in Windows 2000/XP. + BrowserRefresh = Keys.BrowserRefresh, + + /// The Browser Search button on a keyboard in Windows 2000/XP. + BrowserSearch = Keys.BrowserSearch, + + /// The Browser Stop button on a keyboard in Windows 2000/XP. + BrowserStop = Keys.BrowserStop, + + /// The C button on a keyboard. + C = Keys.C, + + /// The Caps Lock button on a keyboard. + CapsLock = Keys.CapsLock, + + /// The Green ChatPad button on a keyboard. + ChatPadGreen = Keys.ChatPadGreen, + + /// The Orange ChatPad button on a keyboard. + ChatPadOrange = Keys.ChatPadOrange, + + /// The CrSel button on a keyboard. + Crsel = Keys.Crsel, + + /// The D button on a keyboard. + D = Keys.D, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D0 = Keys.D0, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D1 = Keys.D1, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D2 = Keys.D2, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D3 = Keys.D3, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D4 = Keys.D4, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D5 = Keys.D5, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D6 = Keys.D6, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D7 = Keys.D7, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D8 = Keys.D8, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D9 = Keys.D9, + + /// The Decimal button on a keyboard. + Decimal = Keys.Decimal, + + /// The Delete button on a keyboard. + Delete = Keys.Delete, + + /// The Divide button on a keyboard. + Divide = Keys.Divide, + + /// The Down arrow button on a keyboard. + Down = Keys.Down, + + /// The E button on a keyboard. + E = Keys.E, + + /// The End button on a keyboard. + End = Keys.End, + + /// The Enter button on a keyboard. + Enter = Keys.Enter, + + /// The Erase EOF button on a keyboard. + EraseEof = Keys.EraseEof, + + /// The Escape button on a keyboard. + Escape = Keys.Escape, + + /// The Execute button on a keyboard. + Execute = Keys.Execute, + + /// The ExSel button on a keyboard. + Exsel = Keys.Exsel, + + /// The F button on a keyboard. + F = Keys.F, + + /// The F1 button on a keyboard. + F1 = Keys.F1, + + /// The F10 button on a keyboard. + F10 = Keys.F10, + + /// The F11 button on a keyboard. + F11 = Keys.F11, + + /// The F12 button on a keyboard. + F12 = Keys.F12, + + /// The F13 button on a keyboard. + F13 = Keys.F13, + + /// The F14 button on a keyboard. + F14 = Keys.F14, + + /// The F15 button on a keyboard. + F15 = Keys.F15, + + /// The F16 button on a keyboard. + F16 = Keys.F16, + + /// The F17 button on a keyboard. + F17 = Keys.F17, + + /// The F18 button on a keyboard. + F18 = Keys.F18, + + /// The F19 button on a keyboard. + F19 = Keys.F19, + + /// The F2 button on a keyboard. + F2 = Keys.F2, + + /// The F20 button on a keyboard. + F20 = Keys.F20, + + /// The F21 button on a keyboard. + F21 = Keys.F21, + + /// The F22 button on a keyboard. + F22 = Keys.F22, + + /// The F23 button on a keyboard. + F23 = Keys.F23, + + /// The F24 button on a keyboard. + F24 = Keys.F24, + + /// The F3 button on a keyboard. + F3 = Keys.F3, + + /// The F4 button on a keyboard. + F4 = Keys.F4, + + /// The F5 button on a keyboard. + F5 = Keys.F5, + + /// The F6 button on a keyboard. + F6 = Keys.F6, + + /// The F7 button on a keyboard. + F7 = Keys.F7, + + /// The F8 button on a keyboard. + F8 = Keys.F8, + + /// The F9 button on a keyboard. + F9 = Keys.F9, + + /// The G button on a keyboard. + G = Keys.G, + + /// The H button on a keyboard. + H = Keys.H, + + /// The Help button on a keyboard. + Help = Keys.Help, + + /// The Home button on a keyboard. + Home = Keys.Home, + + /// The I button on a keyboard. + I = Keys.I, + + /// The IME Convert button on a keyboard. + ImeConvert = Keys.ImeConvert, + + /// The IME NoConvert button on a keyboard. + ImeNoConvert = Keys.ImeNoConvert, + + /// The INS button on a keyboard. + Insert = Keys.Insert, + + /// The J button on a keyboard. + J = Keys.J, + + /// The K button on a keyboard. + K = Keys.K, + + /// The Kana button on a Japanese keyboard. + Kana = Keys.Kana, + + /// The Kanji button on a Japanese keyboard. + Kanji = Keys.Kanji, + + /// The L button on a keyboard. + L = Keys.L, + + /// The Start Applications 1 button on a keyboard in Windows 2000/XP. + LaunchApplication1 = Keys.LaunchApplication1, + + /// The Start Applications 2 button on a keyboard in Windows 2000/XP. + LaunchApplication2 = Keys.LaunchApplication2, + + /// The Start Mail button on a keyboard in Windows 2000/XP. + LaunchMail = Keys.LaunchMail, + + /// The Left arrow button on a keyboard. + Left = Keys.Left, + + /// The Left Alt button on a keyboard. + LeftAlt = Keys.LeftAlt, + + /// The Left Control button on a keyboard. + LeftControl = Keys.LeftControl, + + /// The Left Shift button on a keyboard. + LeftShift = Keys.LeftShift, + + /// The Left Windows button on a keyboard. + LeftWindows = Keys.LeftWindows, + + /// The M button on a keyboard. + M = Keys.M, + + /// The MediaNextTrack button on a keyboard in Windows 2000/XP. + MediaNextTrack = Keys.MediaNextTrack, + + /// The MediaPlayPause button on a keyboard in Windows 2000/XP. + MediaPlayPause = Keys.MediaPlayPause, + + /// The MediaPreviousTrack button on a keyboard in Windows 2000/XP. + MediaPreviousTrack = Keys.MediaPreviousTrack, + + /// The MediaStop button on a keyboard in Windows 2000/XP. + MediaStop = Keys.MediaStop, + + /// The Multiply button on a keyboard. + Multiply = Keys.Multiply, + + /// The N button on a keyboard. + N = Keys.N, + + /// The Num Lock button on a keyboard. + NumLock = Keys.NumLock, + + /// The Numeric keypad 0 button on a keyboard. + NumPad0 = Keys.NumPad0, + + /// The Numeric keypad 1 button on a keyboard. + NumPad1 = Keys.NumPad1, + + /// The Numeric keypad 2 button on a keyboard. + NumPad2 = Keys.NumPad2, + + /// The Numeric keypad 3 button on a keyboard. + NumPad3 = Keys.NumPad3, + + /// The Numeric keypad 4 button on a keyboard. + NumPad4 = Keys.NumPad4, + + /// The Numeric keypad 5 button on a keyboard. + NumPad5 = Keys.NumPad5, + + /// The Numeric keypad 6 button on a keyboard. + NumPad6 = Keys.NumPad6, + + /// The Numeric keypad 7 button on a keyboard. + NumPad7 = Keys.NumPad7, + + /// The Numeric keypad 8 button on a keyboard. + NumPad8 = Keys.NumPad8, + + /// The Numeric keypad 9 button on a keyboard. + NumPad9 = Keys.NumPad9, + + /// The O button on a keyboard. + O = Keys.O, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + Oem8 = Keys.Oem8, + + /// The OEM Auto button on a keyboard. + OemAuto = Keys.OemAuto, + + /// The OEM Angle Bracket or Backslash button on the RT 102 keyboard in Windows 2000/XP. + OemBackslash = Keys.OemBackslash, + + /// The Clear button on a keyboard. + OemClear = Keys.OemClear, + + /// The OEM Close Bracket button on a US standard keyboard in Windows 2000/XP. + OemCloseBrackets = Keys.OemCloseBrackets, + + /// The ',' button on a keyboard in any country/region in Windows 2000/XP. + OemComma = Keys.OemComma, + + /// The OEM Copy button on a keyboard. + OemCopy = Keys.OemCopy, + + /// The OEM Enlarge Window button on a keyboard. + OemEnlW = Keys.OemEnlW, + + /// The '-' button on a keyboard in any country/region in Windows 2000/XP. + OemMinus = Keys.OemMinus, + + /// The OEM Open Bracket button on a US standard keyboard in Windows 2000/XP. + OemOpenBrackets = Keys.OemOpenBrackets, + + /// The '.' button on a keyboard in any country/region. + OemPeriod = Keys.OemPeriod, + + /// The OEM Pipe button on a US standard keyboard. + OemPipe = Keys.OemPipe, + + /// The '+' button on a keyboard in Windows 2000/XP. + OemPlus = Keys.OemPlus, + + /// The OEM Question Mark button on a US standard keyboard. + OemQuestion = Keys.OemQuestion, + + /// The OEM Single/Double Quote button on a US standard keyboard. + OemQuotes = Keys.OemQuotes, + + /// The OEM Semicolon button on a US standard keyboard. + OemSemicolon = Keys.OemSemicolon, + + /// The OEM Tilde button on a US standard keyboard. + OemTilde = Keys.OemTilde, + + /// The P button on a keyboard. + P = Keys.P, + + /// The PA1 button on a keyboard. + Pa1 = Keys.Pa1, + + /// The Page Down button on a keyboard. + PageDown = Keys.PageDown, + + /// The Page Up button on a keyboard. + PageUp = Keys.PageUp, + + /// The Pause button on a keyboard. + Pause = Keys.Pause, + + /// The Play button on a keyboard. + Play = Keys.Play, + + /// The Print button on a keyboard. + Print = Keys.Print, + + /// The Print Screen button on a keyboard. + PrintScreen = Keys.PrintScreen, + + /// The IME Process button on a keyboard in Windows 95/98/ME/NT 4.0/2000/XP. + ProcessKey = Keys.ProcessKey, + + /// The Q button on a keyboard. + Q = Keys.Q, + + /// The R button on a keyboard. + R = Keys.R, + + /// The Right Arrow button on a keyboard. + Right = Keys.Right, + + /// The Right Alt button on a keyboard. + RightAlt = Keys.RightAlt, + + /// The Right Control button on a keyboard. + RightControl = Keys.RightControl, + + /// The Right Shift button on a keyboard. + RightShift = Keys.RightShift, + + /// The Right Windows button on a keyboard. + RightWindows = Keys.RightWindows, + + /// The S button on a keyboard. + S = Keys.S, + + /// The Scroll Lock button on a keyboard. + Scroll = Keys.Scroll, + + /// The Select button on a keyboard. + Select = Keys.Select, + + /// The Select Media button on a keyboard in Windows 2000/XP. + SelectMedia = Keys.SelectMedia, + + /// The Separator button on a keyboard. + Separator = Keys.Separator, + + /// The Computer Sleep button on a keyboard. + Sleep = Keys.Sleep, + + /// The Space bar on a keyboard. + Space = Keys.Space, + + /// The Subtract button on a keyboard. + Subtract = Keys.Subtract, + + /// The T button on a keyboard. + T = Keys.T, + + /// The Tab button on a keyboard. + Tab = Keys.Tab, + + /// The U button on a keyboard. + U = Keys.U, + + /// The Up Arrow button on a keyboard. + Up = Keys.Up, + + /// The V button on a keyboard. + V = Keys.V, + + /// The Volume Down button on a keyboard in Windows 2000/XP. + VolumeDown = Keys.VolumeDown, + + /// The Volume Mute button on a keyboard in Windows 2000/XP. + VolumeMute = Keys.VolumeMute, + + /// The Volume Up button on a keyboard in Windows 2000/XP. + VolumeUp = Keys.VolumeUp, + + /// The W button on a keyboard. + W = Keys.W, + + /// The X button on a keyboard. + X = Keys.X, + + /// The Y button on a keyboard. + Y = Keys.Y, + + /// The Z button on a keyboard. + Z = Keys.Z, + + /// The Zoom button on a keyboard. + Zoom = Keys.Zoom + } + + /// Provides extension methods for . + public static class SButtonExtensions + { + /********* + ** Accessors + *********/ + /// The offset added to values when converting them to to avoid collisions with values. + internal const int ControllerOffset = 2000; + + + /********* + ** Public methods + *********/ + /// Get the equivalent for the given button. + /// The keyboard button to convert. + internal static SButton ToSButton(this Keys key) + { + return (SButton)key; + } + + /// Get the equivalent for the given button. + /// The controller button to convert. + internal static SButton ToSButton(this Buttons key) + { + return (SButton)(SButtonExtensions.ControllerOffset + key); + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The keyboard equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetKeyboard(this SButton input, out Keys key) + { + if (Enum.IsDefined(typeof(Keys), (int)input)) + { + key = (Keys)input; + return true; + } + + key = Keys.None; + return false; + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The controller equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetController(this SButton input, out Buttons button) + { + if (Enum.IsDefined(typeof(Buttons), (int)input - SButtonExtensions.ControllerOffset)) + { + button = (Buttons)(input - SButtonExtensions.ControllerOffset); + return true; + } + + button = 0; + return false; + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The Stardew Valley input button equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetStardewInput(this SButton input, out InputButton button) + { + // keyboard + if (input.TryGetKeyboard(out Keys key)) + { + button = new InputButton(key); + return true; + } + + // mouse + if (input == SButton.MouseLeft || input == SButton.MouseRight) + { + button = new InputButton(mouseLeft: input == SButton.MouseLeft); + return true; + } + + // not valid + button = default(InputButton); + return false; + } + } +} diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs new file mode 100644 index 00000000..326d7fc7 --- /dev/null +++ b/src/SMAPI/Utilities/SDate.cs @@ -0,0 +1,232 @@ +using System; +using System.Linq; +using StardewValley; + +namespace StardewModdingAPI.Utilities +{ + /// Represents a Stardew Valley date. + public class SDate : IEquatable + { + /********* + ** Properties + *********/ + /// The internal season names in order. + private readonly string[] Seasons = { "spring", "summer", "fall", "winter" }; + + /// The number of seasons in a year. + private int SeasonsInYear => this.Seasons.Length; + + /// The number of days in a season. + private readonly int DaysInSeason = 28; + + + /********* + ** Accessors + *********/ + /// The day of month. + public int Day { get; } + + /// The season name. + public string Season { get; } + + /// The year. + public int Year { get; } + + /// The day of week. + public DayOfWeek DayOfWeek { get; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The day of month. + /// The season name. + /// One of the arguments has an invalid value (like day 35). + public SDate(int day, string season) + : this(day, season, Game1.year) { } + + /// Construct an instance. + /// The day of month. + /// The season name. + /// The year. + /// One of the arguments has an invalid value (like day 35). + public SDate(int day, string season, int year) + { + // validate + if (season == null) + throw new ArgumentNullException(nameof(season)); + if (!this.Seasons.Contains(season)) + throw new ArgumentException($"Unknown season '{season}', must be one of [{string.Join(", ", this.Seasons)}]."); + if (day < 1 || day > this.DaysInSeason) + throw new ArgumentException($"Invalid day '{day}', must be a value from 1 to {this.DaysInSeason}."); + if (year < 1) + throw new ArgumentException($"Invalid year '{year}', must be at least 1."); + + // initialise + this.Day = day; + this.Season = season; + this.Year = year; + this.DayOfWeek = this.GetDayOfWeek(); + } + + /// Get the current in-game date. + public static SDate Now() + { + return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year); + } + + /// Get a new date with the given number of days added. + /// The number of days to add. + /// Returns the resulting date. + /// The offset would result in an invalid date (like year 0). + public SDate AddDays(int offset) + { + // get new hash code + int hashCode = this.GetHashCode() + offset; + if (hashCode < 1) + throw new ArithmeticException($"Adding {offset} days to {this} would result in a date before 01 spring Y1."); + + // get day + int day = hashCode % 28; + if (day == 0) + day = 28; + + // get season index + int seasonIndex = hashCode / 28; + if (seasonIndex > 0 && hashCode % 28 == 0) + seasonIndex -= 1; + seasonIndex %= 4; + + // get year + int year = hashCode / (this.Seasons.Length * this.DaysInSeason) + 1; + + // create date + return new SDate(day, this.Seasons[seasonIndex], year); + } + + /// Get a string representation of the date. This is mainly intended for debugging or console messages. + public override string ToString() + { + return $"{this.Day:00} {this.Season} Y{this.Year}"; + } + + /**** + ** IEquatable + ****/ + /// Get whether this instance is equal to another. + /// The other value to compare. + public bool Equals(SDate other) + { + return this == other; + } + + /// Get whether this instance is equal to another. + /// The other value to compare. + public override bool Equals(object obj) + { + return obj is SDate other && this == other; + } + + /// Get a hash code which uniquely identifies a date. + public override int GetHashCode() + { + // return the number of days since 01 spring Y1 (inclusively) + int yearIndex = this.Year - 1; + return + yearIndex * this.DaysInSeason * this.SeasonsInYear + + this.GetSeasonIndex() * this.DaysInSeason + + this.Day; + } + + /**** + ** Operators + ****/ + /// Get whether one date is equal to another. + /// The base date to compare. + /// The other date to compare. + /// The equality of the dates + public static bool operator ==(SDate date, SDate other) + { + return date?.GetHashCode() == other?.GetHashCode(); + } + + /// Get whether one date is not equal to another. + /// The base date to compare. + /// The other date to compare. + public static bool operator !=(SDate date, SDate other) + { + return date?.GetHashCode() != other?.GetHashCode(); + } + + /// Get whether one date is more than another. + /// The base date to compare. + /// The other date to compare. + public static bool operator >(SDate date, SDate other) + { + return date?.GetHashCode() > other?.GetHashCode(); + } + + /// Get whether one date is more than or equal to another. + /// The base date to compare. + /// The other date to compare. + public static bool operator >=(SDate date, SDate other) + { + return date?.GetHashCode() >= other?.GetHashCode(); + } + + /// Get whether one date is less than or equal to another. + /// The base date to compare. + /// The other date to compare. + public static bool operator <=(SDate date, SDate other) + { + return date?.GetHashCode() <= other?.GetHashCode(); + } + + /// Get whether one date is less than another. + /// The base date to compare. + /// The other date to compare. + public static bool operator <(SDate date, SDate other) + { + return date?.GetHashCode() < other?.GetHashCode(); + } + + + /********* + ** Private methods + *********/ + /// Get the day of week for the current date. + private DayOfWeek GetDayOfWeek() + { + switch (this.Day % 7) + { + case 0: + return DayOfWeek.Sunday; + case 1: + return DayOfWeek.Monday; + case 2: + return DayOfWeek.Tuesday; + case 3: + return DayOfWeek.Wednesday; + case 4: + return DayOfWeek.Thursday; + case 5: + return DayOfWeek.Friday; + case 6: + return DayOfWeek.Saturday; + default: + return 0; + } + } + + /// Get the current season index. + /// The current season wasn't recognised. + private int GetSeasonIndex() + { + int index = Array.IndexOf(this.Seasons, this.Season); + if (index == -1) + throw new InvalidOperationException($"The current season '{this.Season}' wasn't recognised."); + return index; + } + } +} -- cgit From aa5c5b27839b72343f751ffabd6c4625d1fc3f80 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 13 Oct 2017 00:33:05 -0400 Subject: fix SDate.Now() crashing during new-game intro (#369) --- docs/release-notes.md | 1 + src/SMAPI.Tests/Utilities/SDateTests.cs | 2 ++ src/SMAPI/Utilities/SDate.cs | 56 ++++++++++++++++++++++----------- 3 files changed, 41 insertions(+), 18 deletions(-) (limited to 'src/SMAPI/Utilities') diff --git a/docs/release-notes.md b/docs/release-notes.md index fd59bd07..64588744 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -29,6 +29,7 @@ For mod developers: * Removed support for mods with no `Name`, `Version`, or `UniqueID` in their manifest. * Removed support for mods with a non-unique `UniqueID` value in their manifest. * Removed access to SMAPI internals through the reflection helper, to discourage fragile mods. +* Fixed `SDate.Now()` crashing when called during the new-game intro. * Fixed `TimeEvents.AfterDayStarted` being raised during the new-game intro. * Fixed SMAPI allowing map tilesheets with absolute or directory-climbing paths. These are now rejected even if the path exists, to avoid problems when players install the mod. diff --git a/src/SMAPI.Tests/Utilities/SDateTests.cs b/src/SMAPI.Tests/Utilities/SDateTests.cs index 25acbaf3..86a0d3d0 100644 --- a/src/SMAPI.Tests/Utilities/SDateTests.cs +++ b/src/SMAPI.Tests/Utilities/SDateTests.cs @@ -69,6 +69,8 @@ namespace StardewModdingAPI.Tests.Utilities [TestCase(01, "Spring", 1)] // seasons are case-sensitive [TestCase(01, "springs", 1)] // invalid season name [TestCase(-1, "spring", 1)] // day < 0 + [TestCase(0, "spring", 1)] // day zero + [TestCase(0, "spring", 2)] // day zero [TestCase(29, "spring", 1)] // day > 28 [TestCase(01, "spring", -1)] // year < 1 [TestCase(01, "spring", 0)] // year < 1 diff --git a/src/SMAPI/Utilities/SDate.cs b/src/SMAPI/Utilities/SDate.cs index 326d7fc7..2630731a 100644 --- a/src/SMAPI/Utilities/SDate.cs +++ b/src/SMAPI/Utilities/SDate.cs @@ -52,28 +52,12 @@ namespace StardewModdingAPI.Utilities /// The year. /// One of the arguments has an invalid value (like day 35). public SDate(int day, string season, int year) - { - // validate - if (season == null) - throw new ArgumentNullException(nameof(season)); - if (!this.Seasons.Contains(season)) - throw new ArgumentException($"Unknown season '{season}', must be one of [{string.Join(", ", this.Seasons)}]."); - if (day < 1 || day > this.DaysInSeason) - throw new ArgumentException($"Invalid day '{day}', must be a value from 1 to {this.DaysInSeason}."); - if (year < 1) - throw new ArgumentException($"Invalid year '{year}', must be at least 1."); - - // initialise - this.Day = day; - this.Season = season; - this.Year = year; - this.DayOfWeek = this.GetDayOfWeek(); - } + : this(day, season, year, allowDayZero: false) { } /// Get the current in-game date. public static SDate Now() { - return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year); + return new SDate(Game1.dayOfMonth, Game1.currentSeason, Game1.year, allowDayZero: true); } /// Get a new date with the given number of days added. @@ -195,6 +179,42 @@ namespace StardewModdingAPI.Utilities /********* ** Private methods *********/ + /// Construct an instance. + /// The day of month. + /// The season name. + /// The year. + /// Whether to allow 0 spring Y1 as a valid date. + /// One of the arguments has an invalid value (like day 35). + private SDate(int day, string season, int year, bool allowDayZero) + { + // validate + if (season == null) + throw new ArgumentNullException(nameof(season)); + if (!this.Seasons.Contains(season)) + throw new ArgumentException($"Unknown season '{season}', must be one of [{string.Join(", ", this.Seasons)}]."); + if (day < 0 || day > this.DaysInSeason) + throw new ArgumentException($"Invalid day '{day}', must be a value from 1 to {this.DaysInSeason}."); + if(day == 0 && !(allowDayZero && this.IsDayZero(day, season, year))) + throw new ArgumentException($"Invalid day '{day}', must be a value from 1 to {this.DaysInSeason}."); + if (year < 1) + throw new ArgumentException($"Invalid year '{year}', must be at least 1."); + + // initialise + this.Day = day; + this.Season = season; + this.Year = year; + this.DayOfWeek = this.GetDayOfWeek(); + } + + /// Get whether a date represents 0 spring Y1, which is the date during the in-game intro. + /// The day of month. + /// The season name. + /// The year. + private bool IsDayZero(int day, string season, int year) + { + return day == 0 && season == "spring" && year == 1; + } + /// Get the day of week for the current date. private DayOfWeek GetDayOfWeek() { -- cgit From 11b889992cd4d29099116e08e5560e040edd4506 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 13 Oct 2017 23:29:24 -0400 Subject: move SButtons into root --- src/SMAPI/SButton.cs | 675 +++++++++++++++++++++++++++++++++++++ src/SMAPI/StardewModdingAPI.csproj | 2 +- src/SMAPI/Utilities/SButton.cs | 675 ------------------------------------- 3 files changed, 676 insertions(+), 676 deletions(-) create mode 100644 src/SMAPI/SButton.cs delete mode 100644 src/SMAPI/Utilities/SButton.cs (limited to 'src/SMAPI/Utilities') diff --git a/src/SMAPI/SButton.cs b/src/SMAPI/SButton.cs new file mode 100644 index 00000000..0ec799db --- /dev/null +++ b/src/SMAPI/SButton.cs @@ -0,0 +1,675 @@ +using System; +using Microsoft.Xna.Framework.Input; +using StardewValley; + +namespace StardewModdingAPI +{ + /// A unified button constant which includes all controller, keyboard, and mouse buttons. + /// Derived from , , and . + public enum SButton + { + /// No valid key. + None = 0, + + /********* + ** Mouse + *********/ + /// The left mouse button. + MouseLeft = 1000, + + /// The right mouse button. + MouseRight = 1001, + + /// The middle mouse button. + MouseMiddle = 1002, + + /// The first mouse XButton. + MouseX1 = 1003, + + /// The second mouse XButton. + MouseX2 = 1004, + + /********* + ** Controller + *********/ + /// The 'A' button on a controller. + ControllerA = SButtonExtensions.ControllerOffset + Buttons.A, + + /// The 'B' button on a controller. + ControllerB = SButtonExtensions.ControllerOffset + Buttons.B, + + /// The 'X' button on a controller. + ControllerX = SButtonExtensions.ControllerOffset + Buttons.X, + + /// The 'Y' button on a controller. + ControllerY = SButtonExtensions.ControllerOffset + Buttons.Y, + + /// The back button on a controller. + ControllerBack = SButtonExtensions.ControllerOffset + Buttons.Back, + + /// The start button on a controller. + ControllerStart = SButtonExtensions.ControllerOffset + Buttons.Start, + + /// The up button on the directional pad of a controller. + DPadUp = SButtonExtensions.ControllerOffset + Buttons.DPadUp, + + /// The down button on the directional pad of a controller. + DPadDown = SButtonExtensions.ControllerOffset + Buttons.DPadDown, + + /// The left button on the directional pad of a controller. + DPadLeft = SButtonExtensions.ControllerOffset + Buttons.DPadLeft, + + /// The right button on the directional pad of a controller. + DPadRight = SButtonExtensions.ControllerOffset + Buttons.DPadRight, + + /// The left bumper (shoulder) button on a controller. + LeftShoulder = SButtonExtensions.ControllerOffset + Buttons.LeftShoulder, + + /// The right bumper (shoulder) button on a controller. + RightShoulder = SButtonExtensions.ControllerOffset + Buttons.RightShoulder, + + /// The left trigger on a controller. + LeftTrigger = SButtonExtensions.ControllerOffset + Buttons.LeftTrigger, + + /// The right trigger on a controller. + RightTrigger = SButtonExtensions.ControllerOffset + Buttons.RightTrigger, + + /// The left analog stick on a controller (when pressed). + LeftStick = SButtonExtensions.ControllerOffset + Buttons.LeftStick, + + /// The right analog stick on a controller (when pressed). + RightStick = SButtonExtensions.ControllerOffset + Buttons.RightStick, + + /// The 'big button' on a controller. + BigButton = SButtonExtensions.ControllerOffset + Buttons.BigButton, + + /// The left analog stick on a controller (when pushed left). + LeftThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickLeft, + + /// The left analog stick on a controller (when pushed right). + LeftThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickRight, + + /// The left analog stick on a controller (when pushed down). + LeftThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickDown, + + /// The left analog stick on a controller (when pushed up). + LeftThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickUp, + + /// The right analog stick on a controller (when pushed left). + RightThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickLeft, + + /// The right analog stick on a controller (when pushed right). + RightThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickRight, + + /// The right analog stick on a controller (when pushed down). + RightThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickDown, + + /// The right analog stick on a controller (when pushed up). + RightThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickUp, + + /********* + ** Keyboard + *********/ + /// The A button on a keyboard. + A = Keys.A, + + /// The Add button on a keyboard. + Add = Keys.Add, + + /// The Applications button on a keyboard. + Apps = Keys.Apps, + + /// The Attn button on a keyboard. + Attn = Keys.Attn, + + /// The B button on a keyboard. + B = Keys.B, + + /// The Backspace button on a keyboard. + Back = Keys.Back, + + /// The Browser Back button on a keyboard in Windows 2000/XP. + BrowserBack = Keys.BrowserBack, + + /// The Browser Favorites button on a keyboard in Windows 2000/XP. + BrowserFavorites = Keys.BrowserFavorites, + + /// The Browser Favorites button on a keyboard in Windows 2000/XP. + BrowserForward = Keys.BrowserForward, + + /// The Browser Home button on a keyboard in Windows 2000/XP. + BrowserHome = Keys.BrowserHome, + + /// The Browser Refresh button on a keyboard in Windows 2000/XP. + BrowserRefresh = Keys.BrowserRefresh, + + /// The Browser Search button on a keyboard in Windows 2000/XP. + BrowserSearch = Keys.BrowserSearch, + + /// The Browser Stop button on a keyboard in Windows 2000/XP. + BrowserStop = Keys.BrowserStop, + + /// The C button on a keyboard. + C = Keys.C, + + /// The Caps Lock button on a keyboard. + CapsLock = Keys.CapsLock, + + /// The Green ChatPad button on a keyboard. + ChatPadGreen = Keys.ChatPadGreen, + + /// The Orange ChatPad button on a keyboard. + ChatPadOrange = Keys.ChatPadOrange, + + /// The CrSel button on a keyboard. + Crsel = Keys.Crsel, + + /// The D button on a keyboard. + D = Keys.D, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D0 = Keys.D0, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D1 = Keys.D1, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D2 = Keys.D2, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D3 = Keys.D3, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D4 = Keys.D4, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D5 = Keys.D5, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D6 = Keys.D6, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D7 = Keys.D7, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D8 = Keys.D8, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + D9 = Keys.D9, + + /// The Decimal button on a keyboard. + Decimal = Keys.Decimal, + + /// The Delete button on a keyboard. + Delete = Keys.Delete, + + /// The Divide button on a keyboard. + Divide = Keys.Divide, + + /// The Down arrow button on a keyboard. + Down = Keys.Down, + + /// The E button on a keyboard. + E = Keys.E, + + /// The End button on a keyboard. + End = Keys.End, + + /// The Enter button on a keyboard. + Enter = Keys.Enter, + + /// The Erase EOF button on a keyboard. + EraseEof = Keys.EraseEof, + + /// The Escape button on a keyboard. + Escape = Keys.Escape, + + /// The Execute button on a keyboard. + Execute = Keys.Execute, + + /// The ExSel button on a keyboard. + Exsel = Keys.Exsel, + + /// The F button on a keyboard. + F = Keys.F, + + /// The F1 button on a keyboard. + F1 = Keys.F1, + + /// The F10 button on a keyboard. + F10 = Keys.F10, + + /// The F11 button on a keyboard. + F11 = Keys.F11, + + /// The F12 button on a keyboard. + F12 = Keys.F12, + + /// The F13 button on a keyboard. + F13 = Keys.F13, + + /// The F14 button on a keyboard. + F14 = Keys.F14, + + /// The F15 button on a keyboard. + F15 = Keys.F15, + + /// The F16 button on a keyboard. + F16 = Keys.F16, + + /// The F17 button on a keyboard. + F17 = Keys.F17, + + /// The F18 button on a keyboard. + F18 = Keys.F18, + + /// The F19 button on a keyboard. + F19 = Keys.F19, + + /// The F2 button on a keyboard. + F2 = Keys.F2, + + /// The F20 button on a keyboard. + F20 = Keys.F20, + + /// The F21 button on a keyboard. + F21 = Keys.F21, + + /// The F22 button on a keyboard. + F22 = Keys.F22, + + /// The F23 button on a keyboard. + F23 = Keys.F23, + + /// The F24 button on a keyboard. + F24 = Keys.F24, + + /// The F3 button on a keyboard. + F3 = Keys.F3, + + /// The F4 button on a keyboard. + F4 = Keys.F4, + + /// The F5 button on a keyboard. + F5 = Keys.F5, + + /// The F6 button on a keyboard. + F6 = Keys.F6, + + /// The F7 button on a keyboard. + F7 = Keys.F7, + + /// The F8 button on a keyboard. + F8 = Keys.F8, + + /// The F9 button on a keyboard. + F9 = Keys.F9, + + /// The G button on a keyboard. + G = Keys.G, + + /// The H button on a keyboard. + H = Keys.H, + + /// The Help button on a keyboard. + Help = Keys.Help, + + /// The Home button on a keyboard. + Home = Keys.Home, + + /// The I button on a keyboard. + I = Keys.I, + + /// The IME Convert button on a keyboard. + ImeConvert = Keys.ImeConvert, + + /// The IME NoConvert button on a keyboard. + ImeNoConvert = Keys.ImeNoConvert, + + /// The INS button on a keyboard. + Insert = Keys.Insert, + + /// The J button on a keyboard. + J = Keys.J, + + /// The K button on a keyboard. + K = Keys.K, + + /// The Kana button on a Japanese keyboard. + Kana = Keys.Kana, + + /// The Kanji button on a Japanese keyboard. + Kanji = Keys.Kanji, + + /// The L button on a keyboard. + L = Keys.L, + + /// The Start Applications 1 button on a keyboard in Windows 2000/XP. + LaunchApplication1 = Keys.LaunchApplication1, + + /// The Start Applications 2 button on a keyboard in Windows 2000/XP. + LaunchApplication2 = Keys.LaunchApplication2, + + /// The Start Mail button on a keyboard in Windows 2000/XP. + LaunchMail = Keys.LaunchMail, + + /// The Left arrow button on a keyboard. + Left = Keys.Left, + + /// The Left Alt button on a keyboard. + LeftAlt = Keys.LeftAlt, + + /// The Left Control button on a keyboard. + LeftControl = Keys.LeftControl, + + /// The Left Shift button on a keyboard. + LeftShift = Keys.LeftShift, + + /// The Left Windows button on a keyboard. + LeftWindows = Keys.LeftWindows, + + /// The M button on a keyboard. + M = Keys.M, + + /// The MediaNextTrack button on a keyboard in Windows 2000/XP. + MediaNextTrack = Keys.MediaNextTrack, + + /// The MediaPlayPause button on a keyboard in Windows 2000/XP. + MediaPlayPause = Keys.MediaPlayPause, + + /// The MediaPreviousTrack button on a keyboard in Windows 2000/XP. + MediaPreviousTrack = Keys.MediaPreviousTrack, + + /// The MediaStop button on a keyboard in Windows 2000/XP. + MediaStop = Keys.MediaStop, + + /// The Multiply button on a keyboard. + Multiply = Keys.Multiply, + + /// The N button on a keyboard. + N = Keys.N, + + /// The Num Lock button on a keyboard. + NumLock = Keys.NumLock, + + /// The Numeric keypad 0 button on a keyboard. + NumPad0 = Keys.NumPad0, + + /// The Numeric keypad 1 button on a keyboard. + NumPad1 = Keys.NumPad1, + + /// The Numeric keypad 2 button on a keyboard. + NumPad2 = Keys.NumPad2, + + /// The Numeric keypad 3 button on a keyboard. + NumPad3 = Keys.NumPad3, + + /// The Numeric keypad 4 button on a keyboard. + NumPad4 = Keys.NumPad4, + + /// The Numeric keypad 5 button on a keyboard. + NumPad5 = Keys.NumPad5, + + /// The Numeric keypad 6 button on a keyboard. + NumPad6 = Keys.NumPad6, + + /// The Numeric keypad 7 button on a keyboard. + NumPad7 = Keys.NumPad7, + + /// The Numeric keypad 8 button on a keyboard. + NumPad8 = Keys.NumPad8, + + /// The Numeric keypad 9 button on a keyboard. + NumPad9 = Keys.NumPad9, + + /// The O button on a keyboard. + O = Keys.O, + + /// A miscellaneous button on a keyboard; can vary by keyboard. + Oem8 = Keys.Oem8, + + /// The OEM Auto button on a keyboard. + OemAuto = Keys.OemAuto, + + /// The OEM Angle Bracket or Backslash button on the RT 102 keyboard in Windows 2000/XP. + OemBackslash = Keys.OemBackslash, + + /// The Clear button on a keyboard. + OemClear = Keys.OemClear, + + /// The OEM Close Bracket button on a US standard keyboard in Windows 2000/XP. + OemCloseBrackets = Keys.OemCloseBrackets, + + /// The ',' button on a keyboard in any country/region in Windows 2000/XP. + OemComma = Keys.OemComma, + + /// The OEM Copy button on a keyboard. + OemCopy = Keys.OemCopy, + + /// The OEM Enlarge Window button on a keyboard. + OemEnlW = Keys.OemEnlW, + + /// The '-' button on a keyboard in any country/region in Windows 2000/XP. + OemMinus = Keys.OemMinus, + + /// The OEM Open Bracket button on a US standard keyboard in Windows 2000/XP. + OemOpenBrackets = Keys.OemOpenBrackets, + + /// The '.' button on a keyboard in any country/region. + OemPeriod = Keys.OemPeriod, + + /// The OEM Pipe button on a US standard keyboard. + OemPipe = Keys.OemPipe, + + /// The '+' button on a keyboard in Windows 2000/XP. + OemPlus = Keys.OemPlus, + + /// The OEM Question Mark button on a US standard keyboard. + OemQuestion = Keys.OemQuestion, + + /// The OEM Single/Double Quote button on a US standard keyboard. + OemQuotes = Keys.OemQuotes, + + /// The OEM Semicolon button on a US standard keyboard. + OemSemicolon = Keys.OemSemicolon, + + /// The OEM Tilde button on a US standard keyboard. + OemTilde = Keys.OemTilde, + + /// The P button on a keyboard. + P = Keys.P, + + /// The PA1 button on a keyboard. + Pa1 = Keys.Pa1, + + /// The Page Down button on a keyboard. + PageDown = Keys.PageDown, + + /// The Page Up button on a keyboard. + PageUp = Keys.PageUp, + + /// The Pause button on a keyboard. + Pause = Keys.Pause, + + /// The Play button on a keyboard. + Play = Keys.Play, + + /// The Print button on a keyboard. + Print = Keys.Print, + + /// The Print Screen button on a keyboard. + PrintScreen = Keys.PrintScreen, + + /// The IME Process button on a keyboard in Windows 95/98/ME/NT 4.0/2000/XP. + ProcessKey = Keys.ProcessKey, + + /// The Q button on a keyboard. + Q = Keys.Q, + + /// The R button on a keyboard. + R = Keys.R, + + /// The Right Arrow button on a keyboard. + Right = Keys.Right, + + /// The Right Alt button on a keyboard. + RightAlt = Keys.RightAlt, + + /// The Right Control button on a keyboard. + RightControl = Keys.RightControl, + + /// The Right Shift button on a keyboard. + RightShift = Keys.RightShift, + + /// The Right Windows button on a keyboard. + RightWindows = Keys.RightWindows, + + /// The S button on a keyboard. + S = Keys.S, + + /// The Scroll Lock button on a keyboard. + Scroll = Keys.Scroll, + + /// The Select button on a keyboard. + Select = Keys.Select, + + /// The Select Media button on a keyboard in Windows 2000/XP. + SelectMedia = Keys.SelectMedia, + + /// The Separator button on a keyboard. + Separator = Keys.Separator, + + /// The Computer Sleep button on a keyboard. + Sleep = Keys.Sleep, + + /// The Space bar on a keyboard. + Space = Keys.Space, + + /// The Subtract button on a keyboard. + Subtract = Keys.Subtract, + + /// The T button on a keyboard. + T = Keys.T, + + /// The Tab button on a keyboard. + Tab = Keys.Tab, + + /// The U button on a keyboard. + U = Keys.U, + + /// The Up Arrow button on a keyboard. + Up = Keys.Up, + + /// The V button on a keyboard. + V = Keys.V, + + /// The Volume Down button on a keyboard in Windows 2000/XP. + VolumeDown = Keys.VolumeDown, + + /// The Volume Mute button on a keyboard in Windows 2000/XP. + VolumeMute = Keys.VolumeMute, + + /// The Volume Up button on a keyboard in Windows 2000/XP. + VolumeUp = Keys.VolumeUp, + + /// The W button on a keyboard. + W = Keys.W, + + /// The X button on a keyboard. + X = Keys.X, + + /// The Y button on a keyboard. + Y = Keys.Y, + + /// The Z button on a keyboard. + Z = Keys.Z, + + /// The Zoom button on a keyboard. + Zoom = Keys.Zoom + } + + /// Provides extension methods for . + public static class SButtonExtensions + { + /********* + ** Accessors + *********/ + /// The offset added to values when converting them to to avoid collisions with values. + internal const int ControllerOffset = 2000; + + + /********* + ** Public methods + *********/ + /// Get the equivalent for the given button. + /// The keyboard button to convert. + internal static SButton ToSButton(this Keys key) + { + return (SButton)key; + } + + /// Get the equivalent for the given button. + /// The controller button to convert. + internal static SButton ToSButton(this Buttons key) + { + return (SButton)(SButtonExtensions.ControllerOffset + key); + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The keyboard equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetKeyboard(this SButton input, out Keys key) + { + if (Enum.IsDefined(typeof(Keys), (int)input)) + { + key = (Keys)input; + return true; + } + + key = Keys.None; + return false; + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The controller equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetController(this SButton input, out Buttons button) + { + if (Enum.IsDefined(typeof(Buttons), (int)input - SButtonExtensions.ControllerOffset)) + { + button = (Buttons)(input - SButtonExtensions.ControllerOffset); + return true; + } + + button = 0; + return false; + } + + /// Get the equivalent for the given button. + /// The button to convert. + /// The Stardew Valley input button equivalent. + /// Returns whether the value was converted successfully. + public static bool TryGetStardewInput(this SButton input, out InputButton button) + { + // keyboard + if (input.TryGetKeyboard(out Keys key)) + { + button = new InputButton(key); + return true; + } + + // mouse + if (input == SButton.MouseLeft || input == SButton.MouseRight) + { + button = new InputButton(mouseLeft: input == SButton.MouseLeft); + return true; + } + + // not valid + button = default(InputButton); + return false; + } + } +} diff --git a/src/SMAPI/StardewModdingAPI.csproj b/src/SMAPI/StardewModdingAPI.csproj index 6bd803c3..b8d5990e 100644 --- a/src/SMAPI/StardewModdingAPI.csproj +++ b/src/SMAPI/StardewModdingAPI.csproj @@ -229,7 +229,7 @@ - + diff --git a/src/SMAPI/Utilities/SButton.cs b/src/SMAPI/Utilities/SButton.cs deleted file mode 100644 index fa5ae648..00000000 --- a/src/SMAPI/Utilities/SButton.cs +++ /dev/null @@ -1,675 +0,0 @@ -using System; -using Microsoft.Xna.Framework.Input; -using StardewValley; - -namespace StardewModdingAPI.Utilities -{ - /// A unified button constant which includes all controller, keyboard, and mouse buttons. - /// Derived from , , and . - public enum SButton - { - /// No valid key. - None = 0, - - /********* - ** Mouse - *********/ - /// The left mouse button. - MouseLeft = 1000, - - /// The right mouse button. - MouseRight = 1001, - - /// The middle mouse button. - MouseMiddle = 1002, - - /// The first mouse XButton. - MouseX1 = 1003, - - /// The second mouse XButton. - MouseX2 = 1004, - - /********* - ** Controller - *********/ - /// The 'A' button on a controller. - ControllerA = SButtonExtensions.ControllerOffset + Buttons.A, - - /// The 'B' button on a controller. - ControllerB = SButtonExtensions.ControllerOffset + Buttons.B, - - /// The 'X' button on a controller. - ControllerX = SButtonExtensions.ControllerOffset + Buttons.X, - - /// The 'Y' button on a controller. - ControllerY = SButtonExtensions.ControllerOffset + Buttons.Y, - - /// The back button on a controller. - ControllerBack = SButtonExtensions.ControllerOffset + Buttons.Back, - - /// The start button on a controller. - ControllerStart = SButtonExtensions.ControllerOffset + Buttons.Start, - - /// The up button on the directional pad of a controller. - DPadUp = SButtonExtensions.ControllerOffset + Buttons.DPadUp, - - /// The down button on the directional pad of a controller. - DPadDown = SButtonExtensions.ControllerOffset + Buttons.DPadDown, - - /// The left button on the directional pad of a controller. - DPadLeft = SButtonExtensions.ControllerOffset + Buttons.DPadLeft, - - /// The right button on the directional pad of a controller. - DPadRight = SButtonExtensions.ControllerOffset + Buttons.DPadRight, - - /// The left bumper (shoulder) button on a controller. - LeftShoulder = SButtonExtensions.ControllerOffset + Buttons.LeftShoulder, - - /// The right bumper (shoulder) button on a controller. - RightShoulder = SButtonExtensions.ControllerOffset + Buttons.RightShoulder, - - /// The left trigger on a controller. - LeftTrigger = SButtonExtensions.ControllerOffset + Buttons.LeftTrigger, - - /// The right trigger on a controller. - RightTrigger = SButtonExtensions.ControllerOffset + Buttons.RightTrigger, - - /// The left analog stick on a controller (when pressed). - LeftStick = SButtonExtensions.ControllerOffset + Buttons.LeftStick, - - /// The right analog stick on a controller (when pressed). - RightStick = SButtonExtensions.ControllerOffset + Buttons.RightStick, - - /// The 'big button' on a controller. - BigButton = SButtonExtensions.ControllerOffset + Buttons.BigButton, - - /// The left analog stick on a controller (when pushed left). - LeftThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickLeft, - - /// The left analog stick on a controller (when pushed right). - LeftThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickRight, - - /// The left analog stick on a controller (when pushed down). - LeftThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickDown, - - /// The left analog stick on a controller (when pushed up). - LeftThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.LeftThumbstickUp, - - /// The right analog stick on a controller (when pushed left). - RightThumbstickLeft = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickLeft, - - /// The right analog stick on a controller (when pushed right). - RightThumbstickRight = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickRight, - - /// The right analog stick on a controller (when pushed down). - RightThumbstickDown = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickDown, - - /// The right analog stick on a controller (when pushed up). - RightThumbstickUp = SButtonExtensions.ControllerOffset + Buttons.RightThumbstickUp, - - /********* - ** Keyboard - *********/ - /// The A button on a keyboard. - A = Keys.A, - - /// The Add button on a keyboard. - Add = Keys.Add, - - /// The Applications button on a keyboard. - Apps = Keys.Apps, - - /// The Attn button on a keyboard. - Attn = Keys.Attn, - - /// The B button on a keyboard. - B = Keys.B, - - /// The Backspace button on a keyboard. - Back = Keys.Back, - - /// The Browser Back button on a keyboard in Windows 2000/XP. - BrowserBack = Keys.BrowserBack, - - /// The Browser Favorites button on a keyboard in Windows 2000/XP. - BrowserFavorites = Keys.BrowserFavorites, - - /// The Browser Favorites button on a keyboard in Windows 2000/XP. - BrowserForward = Keys.BrowserForward, - - /// The Browser Home button on a keyboard in Windows 2000/XP. - BrowserHome = Keys.BrowserHome, - - /// The Browser Refresh button on a keyboard in Windows 2000/XP. - BrowserRefresh = Keys.BrowserRefresh, - - /// The Browser Search button on a keyboard in Windows 2000/XP. - BrowserSearch = Keys.BrowserSearch, - - /// The Browser Stop button on a keyboard in Windows 2000/XP. - BrowserStop = Keys.BrowserStop, - - /// The C button on a keyboard. - C = Keys.C, - - /// The Caps Lock button on a keyboard. - CapsLock = Keys.CapsLock, - - /// The Green ChatPad button on a keyboard. - ChatPadGreen = Keys.ChatPadGreen, - - /// The Orange ChatPad button on a keyboard. - ChatPadOrange = Keys.ChatPadOrange, - - /// The CrSel button on a keyboard. - Crsel = Keys.Crsel, - - /// The D button on a keyboard. - D = Keys.D, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D0 = Keys.D0, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D1 = Keys.D1, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D2 = Keys.D2, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D3 = Keys.D3, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D4 = Keys.D4, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D5 = Keys.D5, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D6 = Keys.D6, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D7 = Keys.D7, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D8 = Keys.D8, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - D9 = Keys.D9, - - /// The Decimal button on a keyboard. - Decimal = Keys.Decimal, - - /// The Delete button on a keyboard. - Delete = Keys.Delete, - - /// The Divide button on a keyboard. - Divide = Keys.Divide, - - /// The Down arrow button on a keyboard. - Down = Keys.Down, - - /// The E button on a keyboard. - E = Keys.E, - - /// The End button on a keyboard. - End = Keys.End, - - /// The Enter button on a keyboard. - Enter = Keys.Enter, - - /// The Erase EOF button on a keyboard. - EraseEof = Keys.EraseEof, - - /// The Escape button on a keyboard. - Escape = Keys.Escape, - - /// The Execute button on a keyboard. - Execute = Keys.Execute, - - /// The ExSel button on a keyboard. - Exsel = Keys.Exsel, - - /// The F button on a keyboard. - F = Keys.F, - - /// The F1 button on a keyboard. - F1 = Keys.F1, - - /// The F10 button on a keyboard. - F10 = Keys.F10, - - /// The F11 button on a keyboard. - F11 = Keys.F11, - - /// The F12 button on a keyboard. - F12 = Keys.F12, - - /// The F13 button on a keyboard. - F13 = Keys.F13, - - /// The F14 button on a keyboard. - F14 = Keys.F14, - - /// The F15 button on a keyboard. - F15 = Keys.F15, - - /// The F16 button on a keyboard. - F16 = Keys.F16, - - /// The F17 button on a keyboard. - F17 = Keys.F17, - - /// The F18 button on a keyboard. - F18 = Keys.F18, - - /// The F19 button on a keyboard. - F19 = Keys.F19, - - /// The F2 button on a keyboard. - F2 = Keys.F2, - - /// The F20 button on a keyboard. - F20 = Keys.F20, - - /// The F21 button on a keyboard. - F21 = Keys.F21, - - /// The F22 button on a keyboard. - F22 = Keys.F22, - - /// The F23 button on a keyboard. - F23 = Keys.F23, - - /// The F24 button on a keyboard. - F24 = Keys.F24, - - /// The F3 button on a keyboard. - F3 = Keys.F3, - - /// The F4 button on a keyboard. - F4 = Keys.F4, - - /// The F5 button on a keyboard. - F5 = Keys.F5, - - /// The F6 button on a keyboard. - F6 = Keys.F6, - - /// The F7 button on a keyboard. - F7 = Keys.F7, - - /// The F8 button on a keyboard. - F8 = Keys.F8, - - /// The F9 button on a keyboard. - F9 = Keys.F9, - - /// The G button on a keyboard. - G = Keys.G, - - /// The H button on a keyboard. - H = Keys.H, - - /// The Help button on a keyboard. - Help = Keys.Help, - - /// The Home button on a keyboard. - Home = Keys.Home, - - /// The I button on a keyboard. - I = Keys.I, - - /// The IME Convert button on a keyboard. - ImeConvert = Keys.ImeConvert, - - /// The IME NoConvert button on a keyboard. - ImeNoConvert = Keys.ImeNoConvert, - - /// The INS button on a keyboard. - Insert = Keys.Insert, - - /// The J button on a keyboard. - J = Keys.J, - - /// The K button on a keyboard. - K = Keys.K, - - /// The Kana button on a Japanese keyboard. - Kana = Keys.Kana, - - /// The Kanji button on a Japanese keyboard. - Kanji = Keys.Kanji, - - /// The L button on a keyboard. - L = Keys.L, - - /// The Start Applications 1 button on a keyboard in Windows 2000/XP. - LaunchApplication1 = Keys.LaunchApplication1, - - /// The Start Applications 2 button on a keyboard in Windows 2000/XP. - LaunchApplication2 = Keys.LaunchApplication2, - - /// The Start Mail button on a keyboard in Windows 2000/XP. - LaunchMail = Keys.LaunchMail, - - /// The Left arrow button on a keyboard. - Left = Keys.Left, - - /// The Left Alt button on a keyboard. - LeftAlt = Keys.LeftAlt, - - /// The Left Control button on a keyboard. - LeftControl = Keys.LeftControl, - - /// The Left Shift button on a keyboard. - LeftShift = Keys.LeftShift, - - /// The Left Windows button on a keyboard. - LeftWindows = Keys.LeftWindows, - - /// The M button on a keyboard. - M = Keys.M, - - /// The MediaNextTrack button on a keyboard in Windows 2000/XP. - MediaNextTrack = Keys.MediaNextTrack, - - /// The MediaPlayPause button on a keyboard in Windows 2000/XP. - MediaPlayPause = Keys.MediaPlayPause, - - /// The MediaPreviousTrack button on a keyboard in Windows 2000/XP. - MediaPreviousTrack = Keys.MediaPreviousTrack, - - /// The MediaStop button on a keyboard in Windows 2000/XP. - MediaStop = Keys.MediaStop, - - /// The Multiply button on a keyboard. - Multiply = Keys.Multiply, - - /// The N button on a keyboard. - N = Keys.N, - - /// The Num Lock button on a keyboard. - NumLock = Keys.NumLock, - - /// The Numeric keypad 0 button on a keyboard. - NumPad0 = Keys.NumPad0, - - /// The Numeric keypad 1 button on a keyboard. - NumPad1 = Keys.NumPad1, - - /// The Numeric keypad 2 button on a keyboard. - NumPad2 = Keys.NumPad2, - - /// The Numeric keypad 3 button on a keyboard. - NumPad3 = Keys.NumPad3, - - /// The Numeric keypad 4 button on a keyboard. - NumPad4 = Keys.NumPad4, - - /// The Numeric keypad 5 button on a keyboard. - NumPad5 = Keys.NumPad5, - - /// The Numeric keypad 6 button on a keyboard. - NumPad6 = Keys.NumPad6, - - /// The Numeric keypad 7 button on a keyboard. - NumPad7 = Keys.NumPad7, - - /// The Numeric keypad 8 button on a keyboard. - NumPad8 = Keys.NumPad8, - - /// The Numeric keypad 9 button on a keyboard. - NumPad9 = Keys.NumPad9, - - /// The O button on a keyboard. - O = Keys.O, - - /// A miscellaneous button on a keyboard; can vary by keyboard. - Oem8 = Keys.Oem8, - - /// The OEM Auto button on a keyboard. - OemAuto = Keys.OemAuto, - - /// The OEM Angle Bracket or Backslash button on the RT 102 keyboard in Windows 2000/XP. - OemBackslash = Keys.OemBackslash, - - /// The Clear button on a keyboard. - OemClear = Keys.OemClear, - - /// The OEM Close Bracket button on a US standard keyboard in Windows 2000/XP. - OemCloseBrackets = Keys.OemCloseBrackets, - - /// The ',' button on a keyboard in any country/region in Windows 2000/XP. - OemComma = Keys.OemComma, - - /// The OEM Copy button on a keyboard. - OemCopy = Keys.OemCopy, - - /// The OEM Enlarge Window button on a keyboard. - OemEnlW = Keys.OemEnlW, - - /// The '-' button on a keyboard in any country/region in Windows 2000/XP. - OemMinus = Keys.OemMinus, - - /// The OEM Open Bracket button on a US standard keyboard in Windows 2000/XP. - OemOpenBrackets = Keys.OemOpenBrackets, - - /// The '.' button on a keyboard in any country/region. - OemPeriod = Keys.OemPeriod, - - /// The OEM Pipe button on a US standard keyboard. - OemPipe = Keys.OemPipe, - - /// The '+' button on a keyboard in Windows 2000/XP. - OemPlus = Keys.OemPlus, - - /// The OEM Question Mark button on a US standard keyboard. - OemQuestion = Keys.OemQuestion, - - /// The OEM Single/Double Quote button on a US standard keyboard. - OemQuotes = Keys.OemQuotes, - - /// The OEM Semicolon button on a US standard keyboard. - OemSemicolon = Keys.OemSemicolon, - - /// The OEM Tilde button on a US standard keyboard. - OemTilde = Keys.OemTilde, - - /// The P button on a keyboard. - P = Keys.P, - - /// The PA1 button on a keyboard. - Pa1 = Keys.Pa1, - - /// The Page Down button on a keyboard. - PageDown = Keys.PageDown, - - /// The Page Up button on a keyboard. - PageUp = Keys.PageUp, - - /// The Pause button on a keyboard. - Pause = Keys.Pause, - - /// The Play button on a keyboard. - Play = Keys.Play, - - /// The Print button on a keyboard. - Print = Keys.Print, - - /// The Print Screen button on a keyboard. - PrintScreen = Keys.PrintScreen, - - /// The IME Process button on a keyboard in Windows 95/98/ME/NT 4.0/2000/XP. - ProcessKey = Keys.ProcessKey, - - /// The Q button on a keyboard. - Q = Keys.Q, - - /// The R button on a keyboard. - R = Keys.R, - - /// The Right Arrow button on a keyboard. - Right = Keys.Right, - - /// The Right Alt button on a keyboard. - RightAlt = Keys.RightAlt, - - /// The Right Control button on a keyboard. - RightControl = Keys.RightControl, - - /// The Right Shift button on a keyboard. - RightShift = Keys.RightShift, - - /// The Right Windows button on a keyboard. - RightWindows = Keys.RightWindows, - - /// The S button on a keyboard. - S = Keys.S, - - /// The Scroll Lock button on a keyboard. - Scroll = Keys.Scroll, - - /// The Select button on a keyboard. - Select = Keys.Select, - - /// The Select Media button on a keyboard in Windows 2000/XP. - SelectMedia = Keys.SelectMedia, - - /// The Separator button on a keyboard. - Separator = Keys.Separator, - - /// The Computer Sleep button on a keyboard. - Sleep = Keys.Sleep, - - /// The Space bar on a keyboard. - Space = Keys.Space, - - /// The Subtract button on a keyboard. - Subtract = Keys.Subtract, - - /// The T button on a keyboard. - T = Keys.T, - - /// The Tab button on a keyboard. - Tab = Keys.Tab, - - /// The U button on a keyboard. - U = Keys.U, - - /// The Up Arrow button on a keyboard. - Up = Keys.Up, - - /// The V button on a keyboard. - V = Keys.V, - - /// The Volume Down button on a keyboard in Windows 2000/XP. - VolumeDown = Keys.VolumeDown, - - /// The Volume Mute button on a keyboard in Windows 2000/XP. - VolumeMute = Keys.VolumeMute, - - /// The Volume Up button on a keyboard in Windows 2000/XP. - VolumeUp = Keys.VolumeUp, - - /// The W button on a keyboard. - W = Keys.W, - - /// The X button on a keyboard. - X = Keys.X, - - /// The Y button on a keyboard. - Y = Keys.Y, - - /// The Z button on a keyboard. - Z = Keys.Z, - - /// The Zoom button on a keyboard. - Zoom = Keys.Zoom - } - - /// Provides extension methods for . - public static class SButtonExtensions - { - /********* - ** Accessors - *********/ - /// The offset added to values when converting them to to avoid collisions with values. - internal const int ControllerOffset = 2000; - - - /********* - ** Public methods - *********/ - /// Get the equivalent for the given button. - /// The keyboard button to convert. - internal static SButton ToSButton(this Keys key) - { - return (SButton)key; - } - - /// Get the equivalent for the given button. - /// The controller button to convert. - internal static SButton ToSButton(this Buttons key) - { - return (SButton)(SButtonExtensions.ControllerOffset + key); - } - - /// Get the equivalent for the given button. - /// The button to convert. - /// The keyboard equivalent. - /// Returns whether the value was converted successfully. - public static bool TryGetKeyboard(this SButton input, out Keys key) - { - if (Enum.IsDefined(typeof(Keys), (int)input)) - { - key = (Keys)input; - return true; - } - - key = Keys.None; - return false; - } - - /// Get the equivalent for the given button. - /// The button to convert. - /// The controller equivalent. - /// Returns whether the value was converted successfully. - public static bool TryGetController(this SButton input, out Buttons button) - { - if (Enum.IsDefined(typeof(Buttons), (int)input - SButtonExtensions.ControllerOffset)) - { - button = (Buttons)(input - SButtonExtensions.ControllerOffset); - return true; - } - - button = 0; - return false; - } - - /// Get the equivalent for the given button. - /// The button to convert. - /// The Stardew Valley input button equivalent. - /// Returns whether the value was converted successfully. - public static bool TryGetStardewInput(this SButton input, out InputButton button) - { - // keyboard - if (input.TryGetKeyboard(out Keys key)) - { - button = new InputButton(key); - return true; - } - - // mouse - if (input == SButton.MouseLeft || input == SButton.MouseRight) - { - button = new InputButton(mouseLeft: input == SButton.MouseLeft); - return true; - } - - // not valid - button = default(InputButton); - return false; - } - } -} -- cgit