Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
ObserverEvent.cs
1using NaughtyAttributes;
2using UnityEngine;
3using UnityEngine.Events;
4
6{
7
8 // doing this in a partial class as this is effectively an extension of RadioObserver in a different script for cleanliness
9 public partial class RadioObserver
10 {
11 /// <summary>
12 /// A singular event inside a \ref RadioObserver, tracking a value or trigger and invoking methods accordingly
13 /// </summary>
14 [System.Serializable]
15 public class ObserverEvent
16 {
17 /// <summary>
18 /// The type of event this is looking for
19 /// </summary>
21
22 /// <summary>
23 /// The method of comparisonType used in this event- not shown if a Trigger event is chosen
24 /// </summary>
25 [AllowNesting, ShowIf("NeedComparison")]
27
28 /// <summary>
29 /// A value clamped between 0 and 1 to use in the comparison.
30 /// <br><br>Shows when a comparison is needed, it does not use a range, and the eventType is not set to Tune.
31 /// </summary>
32 [AllowNesting, ShowIf(EConditionOperator.And, "NeedComparison", "NotNeedVector", "NotIsTune"), Range(0, 1)]
33 public float clampedValue;
34
35 /// <summary>
36 /// A value clamped between 0 and 1000 to use in the comparison.
37 /// <br><br>Shows when a comparison is needed, it does not use a range, and the eventType is set to Tune.
38 /// </summary>
39 [AllowNesting, ShowIf(EConditionOperator.And, "NeedComparison", "NotNeedVector", "IsTune"), Range(0.0f, 1000.0f)]
40 public float tuneValue;
41
42 /// <summary>
43 /// A range between 0 and 1 to use in the comparisonType.
44 /// <br><br>Shows when a comparison is needed, it needs a range, and the eventType is not set to Tune.
45 /// </summary>
46 [AllowNesting, ShowIf(EConditionOperator.And, "NeedComparison", "NeedVector", "NotIsTune"), MinMaxSlider(0, 1)]
47 public Vector2 clampedRange;
48
49 /// <summary>
50 /// A range between 0 and 1000 to use in the comparison.
51 /// <br><br>Shows when a comparison is needed, it needs a range, and the eventType is set to Tune.
52 /// </summary>
53 [AllowNesting, ShowIf(EConditionOperator.And, "NeedComparison", "NeedVector", "IsTune"), MinMaxSlider(0, 1000)]
54 public Vector2 tuneRange;
55
56 /// <summary>
57 /// Show/hide events- useful when there's a lot of them
58 /// </summary>
59 public bool showEvents;
60
61 /// <summary>
62 /// Called when \ref eventType is fulfilled for the first time. Only called once until \ref onEnd is called
63 /// </summary>
64 [ShowIf("showEvents"), AllowNesting]
65 public UnityEvent<float> onTrigger;
66
67 /// <summary>
68 /// Called every frame while \ref eventType is fulfilled, including the first. Cannot be called if \ref eventType is a Trigger event
69 /// </summary>
70 [ShowIf(EConditionOperator.And, "showEvents", "NeedComparison"), AllowNesting]
71 public UnityEvent<float> onStay;
72
73 /// <summary>
74 /// Called when \ref eventType is no longer fulfilled. Only called once until the event is fulfilled again. Cannot be called if \ref eventType is a Trigger event
75 /// </summary>
76 [ShowIf(EConditionOperator.And, "showEvents", "NeedComparison"), AllowNesting]
77 public UnityEvent<float> onEnd;
78
79 /// <summary>
80 /// Tracks if this event has been called in this or the previous frame
81 /// </summary>
82 [HideInInspector] public bool staying = false;
83
84 /// Does this event not need a value or range to compare to?
86 /// Does this event need a value or range to compare to?
87 public bool NeedComparison =>
93 || eventType == EventType.OutputTune;
94
95 /// Does this event not need a range to compare to, and uses a single value instead?
96 public bool NotNeedVector => !NeedVector;
97 /// Does this event need a range to compare to, not a single value?
98 public bool NeedVector =>
100 || comparisonType == ComparisonType.BetweenExclusive;
101
102 /// Does this event not use Tune?
103 public bool NotIsTune => !IsTune;
104 /// Does this event use Tune?
105 public bool IsTune =>
106 eventType == EventType.OutputTune;
107
108
109 /// <summary>
110 /// If this event uses a comparison, check if its fulfilled
111 /// </summary>
112 /// <param name="_cValue">The value to compare against</param>
113 /// <returns>True if the comparison is fulfilled, false if not</returns>
114 public bool EvaluateComparison(float _cValue)
115 {
116 // if the event is not a comparison you should not be calling this method (unless it's being used on many events at once)
118 return false;
119
120 float value;
121 Vector2 range;
122
123 // if this event uses OutputTune, then make sure we're comparing to the tune values
124 if (IsTune)
125 {
126 range = tuneRange;
127 value = tuneValue;
128 }
129 else
130 {
131 value = clampedValue;
132 range = clampedRange;
133 }
134
135 // evaluate the specific comparison- just logical operators
136 return comparisonType switch
137 {
138 ComparisonType.Equal => _cValue == value,
139 ComparisonType.GreaterThan => _cValue > value,
140 ComparisonType.GreaterThanOrEqual => _cValue >= value,
141 ComparisonType.LessThan => _cValue < value,
142 ComparisonType.LessThanOrEqual => _cValue <= value,
143
144 ComparisonType.BetweenExclusive => range.x < _cValue && _cValue < range.y, // i want (x < value < y) notation >:)
145 ComparisonType.BetweenInclusive => range.x <= _cValue && _cValue <= range.y,
146
147 _ => false,
148 };
149 }
150
151 /// <summary>
152 /// Makes the event more readable when printed, in the form "{eventType}/{comparisonType}"
153 /// </summary>
154 public override string ToString()
155 {
156 return eventType + "/" + comparisonType;
157 }
158 }
159 }
160
161}
A singular event inside a RadioObserver, tracking a value or trigger and invoking methods accordingly...
bool showEvents
Show/hide events- useful when there's a lot of them.
UnityEvent< float > onTrigger
Called when eventType is fulfilled for the first time. Only called once until onEnd is called.
bool NotNeedComparison
Does this event not need a value or range to compare to?
bool NotNeedVector
Does this event not need a range to compare to, and uses a single value instead?
float tuneValue
A value clamped between 0 and 1000 to use in the comparison. Shows when a comparison is needed,...
UnityEvent< float > onStay
Called every frame while eventType is fulfilled, including the first. Cannot be called if eventType i...
bool NeedComparison
Does this event need a value or range to compare to?
EventType eventType
The type of event this is looking for.
bool EvaluateComparison(float _cValue)
If this event uses a comparison, check if its fulfilled.
override string ToString()
Makes the event more readable when printed, in the form "{eventType}/{comparisonType}...
bool staying
Tracks if this event has been called in this or the previous frame.
UnityEvent< float > onEnd
Called when eventType is no longer fulfilled. Only called once until the event is fulfilled again....
Vector2 tuneRange
A range between 0 and 1000 to use in the comparison. Shows when a comparison is needed,...
bool NeedVector
Does this event need a range to compare to, not a single value?
ComparisonType comparisonType
The method of comparisonType used in this event- not shown if a Trigger event is chosen.
bool NotIsTune
Does this event not use Tune?
float clampedValue
A value clamped between 0 and 1 to use in the comparison. Shows when a comparison is needed,...
Vector2 clampedRange
A range between 0 and 1 to use in the comparisonType. Shows when a comparison is needed,...
A component used to watch for specific happenings on a RadioOutput, e.g: a clip being a certain volum...
EventType
The event an observer is looking for.
@ TunePower
The tune power of the track: how close the RadioOutput.Tune value is to the range of the track.
@ Gain
The gain of the track: this is currently defined exclusively in the track's gain variable.
@ OutputVolume
The volume of the track: tune power * broadcast power * insulation.
@ BroadcastPower
The broadcast power of the track: how close to any active RadioBroadcasters the output's transform is...
@ Insulation
The insulation of the track: the higher the value the less insulation- the power of any RadioInsulato...
ComparisonType
The method of comparisonType used for an event. For example, checking if the volume is greater than a...
@ GreaterThanOrEqual
The value is greater than a number.
@ BetweenExclusive
The value is between numbers x and y, including if it's equal to x or y.
@ LessThanOrEqual
The value is less than a number.
@ LessThan
The value is greater than or equal to a number.
@ BetweenInclusive
The value is less than or equal to a number.
@ GreaterThan
The value is equal to a number.
Components to be placed on scene objects, e.g: Outputs, Broadcasters, Observers.