/* * 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 UnityEngine; namespace PeterHan.PLib.UI { /// /// Extension methods to deal with TextAnchor alignments. /// public static class TextAnchorUtils { /// /// Returns true if this text alignment is at the left. /// /// The anchoring to check. /// true if it denotes a Left alignment, or false otherwise. public static bool IsLeft(this TextAnchor anchor) { return anchor == TextAnchor.UpperLeft || anchor == TextAnchor.MiddleLeft || anchor == TextAnchor.LowerLeft; } /// /// Returns true if this text alignment is at the bottom. /// /// The anchoring to check. /// true if it denotes a Lower alignment, or false otherwise. public static bool IsLower(this TextAnchor anchor) { return anchor == TextAnchor.LowerCenter || anchor == TextAnchor.LowerLeft || anchor == TextAnchor.LowerRight; } /// /// Returns true if this text alignment is at the right. /// /// The anchoring to check. /// true if it denotes a Right alignment, or false otherwise. public static bool IsRight(this TextAnchor anchor) { return anchor == TextAnchor.UpperRight || anchor == TextAnchor.MiddleRight || anchor == TextAnchor.LowerRight; } /// /// Returns true if this text alignment is at the top. /// /// The anchoring to check. /// true if it denotes an Upper alignment, or false otherwise. public static bool IsUpper(this TextAnchor anchor) { return anchor == TextAnchor.UpperCenter || anchor == TextAnchor.UpperLeft || anchor == TextAnchor.UpperRight; } /// /// Mirrors a text alignment horizontally. UpperLeft becomes UpperRight, MiddleLeft /// becomes MiddleRight, and so forth. /// /// The anchoring to mirror. /// The horizontally reflected version of that mirror. public static TextAnchor MirrorHorizontal(this TextAnchor anchor) { int newAnchor = (int)anchor; // UL UC UR ML MC MR LL LC LR // Danger will robinson! newAnchor = 3 * (newAnchor / 3) + 2 - newAnchor % 3; return (TextAnchor)newAnchor; } /// /// Mirrors a text alignment vertically. UpperLeft becomes LowerLeft, LowerCenter /// becomes UpperCenter, and so forth. /// /// The anchoring to mirror. /// The vertically reflected version of that mirror. public static TextAnchor MirrorVertical(this TextAnchor anchor) { int newAnchor = (int)anchor; newAnchor = 6 - 3 * (newAnchor / 3) + newAnchor % 3; return (TextAnchor)newAnchor; } } }