/* * Copyright 2022 Peter Han * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; namespace PeterHan.PLib.Actions { /// /// An Action managed by PLib. Actions have key bindings assigned to them. /// public sealed class PAction { /// /// The maximum action value (typically used to mean "no action") used in the currently /// running instance of the game. /// /// Since Action is compiled to a const int when a mod is built, any changes to the /// Action enum will break direct references to Action.NumActions. Use this property /// instead to always use the intended "no action" value. /// public static Action MaxAction { get; } static PAction() { if (!Enum.TryParse(nameof(Action.NumActions), out Action limit)) limit = Action.NumActions; MaxAction = limit; } /// /// The default key binding for this action. Not necessarily the current key binding. /// internal PKeyBinding DefaultBinding { get; } /// /// The action's non-localized identifier. Something like YOURMOD.CATEGORY.ACTIONNAME. /// public string Identifier { get; } /// /// The action's ID. This ID is assigned internally upon register and used for PLib /// indexing. Even if you somehow obtain it in your mod, it is not to be used! /// private readonly int id; /// /// The action's title. /// public LocString Title { get; } internal PAction(int id, string identifier, LocString title, PKeyBinding binding) { if (id <= 0) throw new ArgumentOutOfRangeException(nameof(id)); DefaultBinding = binding; Identifier = identifier; this.id = id; Title = title; } public override bool Equals(object obj) { return (obj is PAction other) && other.id == id; } /// /// Retrieves the Klei action for this PAction. /// /// The Klei action for use in game functions. public Action GetKAction() { return (Action)((int)MaxAction + id); } public override int GetHashCode() { return id; } public override string ToString() { return "PAction[" + Identifier + "]: " + Title; } } }