Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
StationRadioTrackEditor.cs
1using UnityEngine;
2
3namespace RyleRadio.Editor
4{
5 using RyleRadio.Tracks;
6
7#if UNITY_EDITOR
8 using UnityEditor;
9
10 /// <summary>
11 /// The editor for a \ref StationRadioTrack
12 /// <br><br>This mainly exists so that we can reset any newly added tracks in the station- much the same as \ref RadioDataEditor
13 /// <br><b>See: </b>\ref StationRadioTrack
14 /// </summary>
15 [CustomPropertyDrawer(typeof(StationRadioTrack))]
16 public class StationRadioTrackEditor : PropertyDrawer
17 {
18 /// The last recorded number of tracks in this station
19 private int lastTrackWSize = -1;
20
21 /// Whether or not the random threshold is currently visible in the inspector
22 private bool isShowingThreshold = false;
23
24 /// Whether or not this track is showing its child tracks- optional variable for cleanliness
25 private bool showTrackInParent = true;
26
27 /// <summary>
28 /// Draws the track, its child tracks, and other variables, e.g: randomisation settings
29 /// </summary>
30 public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
31 {
32 SerializedProperty trackWs = _property.FindPropertyRelative("stationTrackWs"); // the tracks
33
34 SerializedProperty isRandom = _property.FindPropertyRelative("randomSequence"); // whether or not the station is random
35 SerializedProperty threshold = _property.FindPropertyRelative("thresholdBeforeRepeats"); // the random threshold if it is
36
37 _position.position += new Vector2(24, 0);
38 _position.width -= 24;
39
40 // visuals for randomization settings
41 Rect dropdownRect = new(_position.position - new Vector2(24, 0), new Vector2(0, 20));
42 Rect isRandomRect = dropdownRect;
43
44 // put the threshold slider under the bool
45 isRandomRect.x += 12;
46
47 Rect thresholdSliderRect = isRandomRect;
48
49 isRandomRect.width = _position.width;
50 isRandomRect.y += 20;
51
52 // put the threshold slider under the bool
53 thresholdSliderRect.y += 20 + isRandomRect.height;
54 thresholdSliderRect.width = _position.width;
55
56 // if the station editor was just loaded, assign the default track count
57 if (lastTrackWSize < 0)
58 lastTrackWSize = trackWs.arraySize;
59
60 showTrackInParent = EditorGUI.Foldout(dropdownRect, showTrackInParent, _label);
61
62 if (showTrackInParent)
63 {
64 // if the station is in a random order,
65 if (EditorGUI.Toggle(isRandomRect, new GUIContent("Random Sequence"), isRandom.boolValue))
66 {
67 isRandom.boolValue = true;
68 isShowingThreshold = true;
69
70 // show the threshold for that order
71 EditorGUI.Slider(thresholdSliderRect, threshold, 0, 1, new GUIContent("Threshold Before Repeats"));
72 }
73 else
74 {
75 isRandom.boolValue = false;
76 isShowingThreshold = false;
77 }
78
79 // add the height of the random settings to the property height
80 _position.position = new Vector2(
81 _position.position.x,
82 _position.position.y + dropdownRect.height + isRandomRect.height + (
83 isShowingThreshold
84 ? thresholdSliderRect.height + 4
85 : 0
86 )
87 );
88
89 // show the list of tracks
90 EditorGUI.PropertyField(_position, trackWs, new GUIContent("Station Tracks"), true);
91
92 // if the station was changed
93 if (GUI.changed)
94 {
95 // and a new track was added
96 if (trackWs.arraySize > lastTrackWSize)
97 {
98 // get the new element
99 SerializedProperty newElement = trackWs.GetArrayElementAtIndex(trackWs.arraySize - 1);
100
101 // get the stored track and track eventType
102 SerializedProperty track = newElement.FindPropertyRelative("track");
103 SerializedProperty trackType = newElement.FindPropertyRelative("trackType");
104
105 // get the stored gain
106 SerializedProperty gain = newElement.FindPropertyRelative("gain");
107
108 // create an empty track for the station
109 track.managedReferenceValue = StationRadioTrackWrapper.CreateTrackEditor(trackType.stringValue);
110
111 // reset the track's gain
112 gain.floatValue = 100;
113
114 // store the new track list size
115 lastTrackWSize++;
116 }
117 // if a track was removed
118 else if (trackWs.arraySize < lastTrackWSize)
119 {
120 // store the new track list size
121 lastTrackWSize--;
122 }
123 }
124
125 }
126 else
127 {
128 // add the height of the random settings to the property height
129 _position.position = new Vector2(
130 _position.position.x,
131 _position.position.y + dropdownRect.height
132 );
133 }
134
135 // apply any inspector changes
136 _property.serializedObject.ApplyModifiedProperties();
137
138 }
139
140 /// <summary>
141 /// Gets the total height of this track in the inspector, including child tracks
142 /// </summary>
143 public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
144 {
145 if (showTrackInParent)
146 {
147 // if the random options are shown, add height accordingly
148 float randomOptionsHeight = 48 + (isShowingThreshold ? 20 : 0);
149
150 // get the height of the contained track list
151 float listHeight = EditorGUI.GetPropertyHeight(property.FindPropertyRelative("stationTrackWs"));
152
153 // combine the heights
154 return randomOptionsHeight + listHeight;
155 }
156 else
157 {
158 return 18;
159 }
160 }
161 }
162#endif
163
164}
A eventType of RadioTrack that contains other tracks. Has a custom editor in StationRadioTrackEditor ...
Editor scripts for classes and attributes, notably excluding MultiselectAttribute.