A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using osuTK;
5using System;
6using osu.Framework.Graphics.Transforms;
7using osu.Framework.Utils;
8
9namespace osu.Framework.Graphics
10{
11 /// <summary>
12 /// Holds data about the margin or padding of a <see cref="Drawable"/>.
13 /// The margin describes the size of an empty area around its <see cref="Drawable"/>, while the padding describes the size of an empty area inside its container.
14 /// </summary>
15 public struct MarginPadding : IInterpolable<MarginPadding>, IEquatable<MarginPadding>
16 {
17 /// <summary>
18 /// The absolute size of the space that should be left empty above the <see cref="Drawable"/> if used as margin, or
19 /// the absolute size of the space that should be left empty from the top of the container if used as padding.
20 /// </summary>
21 public float Top;
22
23 /// <summary>
24 /// The absolute size of the space that should be left empty to the left of the <see cref="Drawable"/> if used as margin, or
25 /// the absolute size of the space that should be left empty from the left of the container if used as padding.
26 /// </summary>
27 public float Left;
28
29 /// <summary>
30 /// The absolute size of the space that should be left empty below the <see cref="Drawable"/> if used as margin, or
31 /// the absolute size of the space that should be left empty from the bottom of the container if used as padding.
32 /// </summary>
33 public float Bottom;
34
35 /// <summary>
36 /// The absolute size of the space that should be left empty to the right of the <see cref="Drawable"/> if used as margin, or
37 /// the absolute size of the space that should be left empty from the right of the container if used as padding.
38 /// </summary>
39 public float Right;
40
41 /// <summary>
42 /// Gets the total absolute size of the empty space horizontally around the <see cref="Drawable"/> if used as margin, or
43 /// the absolute size of the space left empty from the right and left of the container if used as padding.
44 /// Effectively <see cref="Right"/> + <see cref="Left"/>.
45 /// </summary>
46 public readonly float TotalHorizontal => Left + Right;
47
48 /// <summary>
49 /// Sets the values of both <see cref="Left"/> and <see cref="Right"/> to the assigned value.
50 /// </summary>
51 public float Horizontal
52 {
53 set => Left = Right = value;
54 }
55
56 /// <summary>
57 /// Gets the total absolute size of the empty space vertically around the <see cref="Drawable"/> or
58 /// the absolute size of the space left empty from the top and bottom of the container if used as padding.
59 /// Effectively <see cref="Top"/> + <see cref="Bottom"/>.
60 /// </summary>
61 public readonly float TotalVertical => Top + Bottom;
62
63 /// <summary>
64 /// Sets the values of both <see cref="Top"/> and <see cref="Bottom"/> to the assigned value.
65 /// </summary>
66 public float Vertical
67 {
68 set => Top = Bottom = value;
69 }
70
71 /// <summary>
72 /// Gets the total absolute size of the empty space horizontally (x coordinate) and vertically (y coordinate) around the <see cref="Drawable"/> or inside the container if used as padding.
73 /// </summary>
74 public readonly Vector2 Total => new Vector2(TotalHorizontal, TotalVertical);
75
76 /// <summary>
77 /// Initializes all four sides (<see cref="Left"/>, <see cref="Right"/>, <see cref="Top"/> and <see cref="Bottom"/>) to the given value.
78 /// </summary>
79 /// <param name="allSides">The absolute size of the space that should be left around every side of the <see cref="Drawable"/>.</param>
80 public MarginPadding(float allSides)
81 {
82 Top = Left = Bottom = Right = allSides;
83 }
84
85 public readonly bool Equals(MarginPadding other) => Top == other.Top && Left == other.Left && Bottom == other.Bottom && Right == other.Right;
86
87 public override readonly string ToString() => $@"({Top}, {Left}, {Bottom}, {Right})";
88
89 public static MarginPadding operator -(MarginPadding mp) =>
90 new MarginPadding
91 {
92 Left = -mp.Left,
93 Top = -mp.Top,
94 Right = -mp.Right,
95 Bottom = -mp.Bottom,
96 };
97
98 public MarginPadding ValueAt<TEasing>(double time, MarginPadding startValue, MarginPadding endValue, double startTime, double endTime, in TEasing easing)
99 where TEasing : IEasingFunction
100 => new MarginPadding
101 {
102 Left = Interpolation.ValueAt(time, startValue.Left, endValue.Left, startTime, endTime, easing),
103 Top = Interpolation.ValueAt(time, startValue.Top, endValue.Top, startTime, endTime, easing),
104 Right = Interpolation.ValueAt(time, startValue.Right, endValue.Right, startTime, endTime, easing),
105 Bottom = Interpolation.ValueAt(time, startValue.Bottom, endValue.Bottom, startTime, endTime, easing),
106 };
107 }
108}