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 System;
5
6namespace osu.Framework.Graphics.Performance
7{
8 /// <summary>
9 /// An object for a <see cref="LifetimeEntryManager"/> to consume, which provides a <see cref="LifetimeStart"/> and <see cref="LifetimeEnd"/>.
10 /// </summary>
11 /// <remarks>
12 /// Management of the object which the <see cref="LifetimeEntry"/> refers to is left up to the consumer.
13 /// </remarks>
14 public class LifetimeEntry
15 {
16 private double lifetimeStart = double.MinValue;
17
18 /// <summary>
19 /// The time at which this <see cref="LifetimeEntry"/> becomes alive in a <see cref="LifetimeEntryManager"/>.
20 /// </summary>
21 public double LifetimeStart
22 {
23 get => lifetimeStart;
24 // A method is used as C# doesn't allow the combination of a non-virtual getter and a virtual setter.
25 set => SetLifetimeStart(value);
26 }
27
28 private double lifetimeEnd = double.MaxValue;
29
30 /// <summary>
31 /// The time at which this <see cref="LifetimeEntry"/> becomes dead in a <see cref="LifetimeEntryManager"/>.
32 /// </summary>
33 public double LifetimeEnd
34 {
35 get => lifetimeEnd;
36 set => SetLifetimeEnd(value);
37 }
38
39 /// <summary>
40 /// Invoked before <see cref="LifetimeStart"/> or <see cref="LifetimeEnd"/> changes.
41 /// It is used because <see cref="LifetimeChanged"/> cannot be used to ensure comparator stability.
42 /// </summary>
43 internal event Action<LifetimeEntry> RequestLifetimeUpdate;
44
45 /// <summary>
46 /// Invoked after <see cref="LifetimeStart"/> or <see cref="LifetimeEnd"/> changes.
47 /// </summary>
48 public event Action<LifetimeEntry> LifetimeChanged;
49
50 /// <summary>
51 /// Update <see cref="LifetimeStart"/> of this <see cref="LifetimeEntry"/>.
52 /// </summary>
53 protected virtual void SetLifetimeStart(double start)
54 {
55 if (start != lifetimeStart)
56 SetLifetime(start, lifetimeEnd);
57 }
58
59 /// <summary>
60 /// Update <see cref="LifetimeEnd"/> of this <see cref="LifetimeEntry"/>.
61 /// </summary>
62 protected virtual void SetLifetimeEnd(double end)
63 {
64 if (end != lifetimeEnd)
65 SetLifetime(lifetimeStart, end);
66 }
67
68 /// <summary>
69 /// Updates the stored lifetimes of this <see cref="LifetimeEntry"/>.
70 /// </summary>
71 /// <param name="start">The new <see cref="LifetimeStart"/> value.</param>
72 /// <param name="end">The new <see cref="LifetimeEnd"/> value.</param>
73 protected void SetLifetime(double start, double end)
74 {
75 RequestLifetimeUpdate?.Invoke(this);
76
77 lifetimeStart = start;
78 lifetimeEnd = Math.Max(start, end); // Negative intervals are undesired.
79
80 LifetimeChanged?.Invoke(this);
81 }
82
83 /// <summary>
84 /// The current state of this <see cref="LifetimeEntry"/>.
85 /// </summary>
86 internal LifetimeEntryState State { get; set; }
87
88 /// <summary>
89 /// Uniquely identifies this <see cref="LifetimeEntry"/> in a <see cref="LifetimeEntryManager"/>.
90 /// </summary>
91 internal ulong ChildId { get; set; }
92 }
93}