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 osu.Framework.Graphics;
5using osu.Framework.Statistics;
6
7namespace osu.Framework.Layout
8{
9 /// <summary>
10 /// A member that represents a part of the layout of a <see cref="Drawable"/>.
11 /// Can be invalidated according to state changes in a <see cref="Drawable"/> (via <see cref="Graphics.Invalidation"/> flags).
12 /// </summary>
13 public abstract class LayoutMember
14 {
15 /// <summary>
16 /// The <see cref="Graphics.Invalidation"/> flags this <see cref="LayoutMember"/> responds to.
17 /// </summary>
18 public readonly Invalidation Invalidation;
19
20 /// <summary>
21 /// Any extra conditions that must be satisfied before this <see cref="LayoutMember"/> is invalidated.
22 /// </summary>
23 public readonly InvalidationConditionDelegate Conditions;
24
25 /// <summary>
26 /// The source of <see cref="Invalidation"/> this <see cref="LayoutMember"/> responds to.
27 /// </summary>
28 public readonly InvalidationSource Source;
29
30 /// <summary>
31 /// The <see cref="Drawable"/> containing this <see cref="LayoutMember"/>.
32 /// </summary>
33 internal Drawable Parent;
34
35 /// <summary>
36 /// Creates a new <see cref="LayoutMember"/>.
37 /// </summary>
38 /// <param name="invalidation">The <see cref="Graphics.Invalidation"/> flags that will invalidate this <see cref="LayoutMember"/>.</param>
39 /// <param name="source">The source of the invalidation.</param>
40 /// <param name="conditions">Any extra conditions that must be satisfied before this <see cref="LayoutMember"/> is invalidated.</param>
41 protected LayoutMember(Invalidation invalidation, InvalidationSource source = InvalidationSource.Default, InvalidationConditionDelegate conditions = null)
42 {
43 Invalidation = invalidation;
44 Conditions = conditions;
45 Source = source;
46 }
47
48 /// <summary>
49 /// Whether this <see cref="LayoutMember"/> is valid.
50 /// </summary>
51 public bool IsValid { get; private set; }
52
53 /// <summary>
54 /// Invalidates this <see cref="LayoutMember"/>.
55 /// </summary>
56 /// <returns>Whether any invalidation occurred.</returns>
57 public bool Invalidate()
58 {
59 if (!IsValid)
60 return false;
61
62 IsValid = false;
63 FrameStatistics.Increment(StatisticsCounterType.Invalidations);
64 return true;
65 }
66
67 /// <summary>
68 /// Validates this <see cref="LayoutMember"/>.
69 /// </summary>
70 protected void Validate()
71 {
72 if (IsValid)
73 return;
74
75 IsValid = true;
76 Parent?.ValidateSuperTree(Invalidation);
77 FrameStatistics.Increment(StatisticsCounterType.Refreshes);
78 }
79 }
80
81 /// <summary>
82 /// The delegate that provides extra conditions for an invalidation to occur.
83 /// </summary>
84 /// <param name="source">The <see cref="Drawable"/> to be invalidated.</param>
85 /// <param name="invalidation">The <see cref="Invalidation"/> flags.</param>
86 public delegate bool InvalidationConditionDelegate(Drawable source, Invalidation invalidation);
87}