| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Runtime.CompilerServices; |
| | 7 | | using UnityEngine.UIElements; |
| | 8 | |
|
| | 9 | | namespace GDX |
| | 10 | | { |
| | 11 | | public static class VisualElementStyles |
| | 12 | | { |
| | 13 | | public const string AlignmentTopLeftClass = "top-left"; |
| | 14 | | public const string AlignmentTopRightClass = "top-right"; |
| | 15 | | public const string AlignmentBottomLeftClass = "bottom-left"; |
| | 16 | | public const string AlignmentBottomRightClass = "bottom-right"; |
| | 17 | |
|
| 2 | 18 | | public static readonly StyleEnum<Position> PositionAbsolute = Position.Absolute; |
| 2 | 19 | | public static readonly StyleEnum<Position> PositionRelative = Position.Relative; |
| 2 | 20 | | public static readonly StyleEnum<DisplayStyle> DisplayHidden = DisplayStyle.None; |
| 2 | 21 | | public static readonly StyleEnum<DisplayStyle> DisplayVisible = DisplayStyle.Flex; |
| 2 | 22 | | public static readonly StyleLength LengthOneHundredPercent = new StyleLength(new Length(100f, LengthUnit.Percent |
| 2 | 23 | | public static readonly StyleLength LengthZeroPixel = new StyleLength(new Length(0, LengthUnit.Pixel)); |
| | 24 | |
|
| | 25 | | public enum Alignment |
| | 26 | | { |
| | 27 | | TopLeft = 0, |
| | 28 | | TopRight = 1, |
| | 29 | | BottomLeft = 2, |
| | 30 | | BottomRight = 3 |
| | 31 | | } |
| | 32 | |
|
| | 33 | | public static void ApplyAlignment(this VisualElement element, Alignment alignment) |
| 0 | 34 | | { |
| 0 | 35 | | switch (alignment) |
| | 36 | | { |
| | 37 | | case Alignment.TopLeft: |
| 0 | 38 | | element.EnableInClassList(AlignmentTopRightClass, false); |
| 0 | 39 | | element.EnableInClassList(AlignmentBottomLeftClass, false); |
| 0 | 40 | | element.EnableInClassList(AlignmentBottomRightClass, false); |
| | 41 | |
|
| 0 | 42 | | element.EnableInClassList(AlignmentTopLeftClass, true); |
| 0 | 43 | | break; |
| | 44 | | case Alignment.TopRight: |
| 0 | 45 | | element.EnableInClassList(AlignmentTopLeftClass, false); |
| 0 | 46 | | element.EnableInClassList(AlignmentBottomLeftClass, false); |
| 0 | 47 | | element.EnableInClassList(AlignmentBottomRightClass, false); |
| | 48 | |
|
| 0 | 49 | | element.EnableInClassList(AlignmentTopRightClass, true); |
| 0 | 50 | | break; |
| | 51 | | case Alignment.BottomLeft: |
| 0 | 52 | | element.EnableInClassList(AlignmentTopLeftClass, false); |
| 0 | 53 | | element.EnableInClassList(AlignmentTopRightClass, false); |
| 0 | 54 | | element.EnableInClassList(AlignmentBottomRightClass, false); |
| | 55 | |
|
| 0 | 56 | | element.EnableInClassList(AlignmentBottomLeftClass, true); |
| 0 | 57 | | break; |
| | 58 | | case Alignment.BottomRight: |
| 0 | 59 | | element.EnableInClassList(AlignmentTopLeftClass, false); |
| 0 | 60 | | element.EnableInClassList(AlignmentTopRightClass, false); |
| 0 | 61 | | element.EnableInClassList(AlignmentBottomLeftClass, false); |
| | 62 | |
|
| 0 | 63 | | element.EnableInClassList(AlignmentBottomRightClass, true); |
| 0 | 64 | | break; |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 69 | | public static void Hide(this VisualElement element) |
| 0 | 70 | | { |
| 0 | 71 | | element.style.display = DisplayHidden; |
| 0 | 72 | | } |
| | 73 | |
|
| | 74 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 75 | | public static bool IsVisible(this VisualElement element) |
| 0 | 76 | | { |
| 0 | 77 | | return element.style.display == DisplayVisible; |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 81 | | public static void Fullscreen(this VisualElement element) |
| 0 | 82 | | { |
| 0 | 83 | | element.style.position = PositionAbsolute; |
| 0 | 84 | | element.style.width = LengthOneHundredPercent; |
| 0 | 85 | | element.style.height = LengthOneHundredPercent; |
| 0 | 86 | | element.style.left = LengthZeroPixel; |
| 0 | 87 | | element.style.top = LengthZeroPixel; |
| 0 | 88 | | } |
| | 89 | |
|
| | 90 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 91 | | public static void Show(this VisualElement element) |
| 0 | 92 | | { |
| 0 | 93 | | element.style.display = DisplayVisible; |
| 0 | 94 | | } |
| | 95 | | } |
| | 96 | | } |