A game framework written with osu! in mind.
at master 75 lines 3.3 kB view raw
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 System; 5using osu.Framework.Graphics; 6 7namespace osu.Framework.Layout 8{ 9 /// <summary> 10 /// A member that represents the validation state 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), and validated on-demand when the layout has been re-computed. 12 /// </summary> 13 public class LayoutValue : LayoutMember 14 { 15 /// <summary> 16 /// Creates a new <see cref="LayoutValue"/>. 17 /// </summary> 18 /// <param name="invalidation">The <see cref="Invalidation"/> flags that will invalidate this <see cref="LayoutValue"/>.</param> 19 /// <param name="source">The source of the invalidation.</param> 20 /// <param name="conditions">Any extra conditions that must be satisfied before this <see cref="LayoutValue"/> is invalidated.</param> 21 public LayoutValue(Invalidation invalidation, InvalidationSource source = InvalidationSource.Default, InvalidationConditionDelegate conditions = null) 22 : base(invalidation, source, conditions) 23 { 24 } 25 26 /// <summary> 27 /// Validates this <see cref="LayoutValue"/>. 28 /// </summary> 29 public new void Validate() => base.Validate(); 30 } 31 32 /// <summary> 33 /// A member that represents the validation state of a value in the layout of a <see cref="Drawable"/>. 34 /// Can be invalidated according to state changes in a <see cref="Drawable"/> (via <see cref="Graphics.Invalidation"/> flags), and validated when an up-to-date value is set. 35 /// </summary> 36 /// <typeparam name="T">The type of value stored.</typeparam> 37 public class LayoutValue<T> : LayoutMember 38 { 39 /// <summary> 40 /// Creates a new <see cref="LayoutValue{T}"/>. 41 /// </summary> 42 /// <param name="invalidation">The <see cref="Invalidation"/> flags that will invalidate this <see cref="LayoutValue{T}"/>.</param> 43 /// <param name="source">The source of the invalidation.</param> 44 /// <param name="conditions">Any extra conditions that must be satisfied before this <see cref="LayoutValue{T}"/> is invalidated.</param> 45 public LayoutValue(Invalidation invalidation, InvalidationSource source = InvalidationSource.Default, InvalidationConditionDelegate conditions = null) 46 : base(invalidation, source, conditions) 47 { 48 } 49 50 private T value; 51 52 /// <summary> 53 /// Gets or sets the current value. 54 /// </summary> 55 /// <exception cref="InvalidOperationException">If accessed while <see cref="LayoutMember.IsValid"/> is <code>false</code>.</exception> 56 public T Value 57 { 58 get 59 { 60 if (!IsValid) 61 throw new InvalidOperationException($"May not query {nameof(Value)} of an invalid {nameof(LayoutValue<T>)}."); 62 63 return value; 64 } 65 set 66 { 67 this.value = value; 68 69 Validate(); 70 } 71 } 72 73 public static implicit operator T(LayoutValue<T> layoutValue) => layoutValue.Value; 74 } 75}