Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
RadioData.cs
3using System;
4using System.Collections.Generic;
5using System.Linq;
6using UnityEngine;
7
8#pragma warning disable 0414 // we do this as the force clip sample rate variables are technically not used in code- but they are in the inspector
9
10namespace RyleRadio
11{
12 /// <summary>
13 /// The central data object defining the radio. Contains the tracks and information required to play the radio at runtime.
14 /// <br><br>Has a custom editor at \ref RadioDataEditor
15 /// </summary>
16 [CreateAssetMenu(fileName = "New Radio Data", menuName = "Ryle Radio/Radio Data")]
17 public class RadioData : ScriptableObject
18 {
19 // i wanted these to be editable in the inspector too, but for now it's internally hardcoded- looking into changing this at some point
20 /// The lower limit for tune on this radio. This may become non-const at some point
21 public const float LOW_TUNE = 0;
22 /// The upper limit for tune on this radio. This may become non-const at some point
23 public const float HIGH_TUNE = 1000;
24
25 /// The primary colour of gizmos relating to this radio
26 [SerializeField] private Color gizmoColor = new Color32(200, 180, 255, 255);
27 /// The secondary colour of gizmos relating to this radio
28 [SerializeField] private Color gizmoColorSecondary = new Color32(175, 105, 205, 255);
29
30 /// Whether or not this radio forces the sample rate on AudioClips it references
31 [SerializeField] private bool forceClipSampleRate = true;
32 /// The sample rate this radio can force on AudioClips it references. If left at 0, it chooses the project's default sample rate
33 [SerializeField] private int forcedSampleRate = 0;
34
35 /// <summary>
36 /// The tracks contained in this radio, editable in the inspector
37 /// </summary>
38 [SerializeField] private List<RadioTrackWrapper> trackWs = new() { new() };
39
40
41 /// Alias for \ref gizmoColor for safety
42 public Color GizmoColor => gizmoColor;
43 /// Alias for \ref gizmoColorSecondary for safety
45
46 /// Alias for \ref trackWs for safety- in documentation we usually call them the tracks, but for code clarity we explicitly call them wrappers in this object
47 public List<RadioTrackWrapper> TrackWrappers => trackWs;
48
49
50 /// Event invoked when \ref Init() is called
51 public Action<RadioData> OnInit { get; set; } = new(_ => { });
52 /// Event invoked when \ref Init() is called, but at the beginning before anything happens
53 public Action<RadioData> BeforeInit { get; set; } = new(_ => { });
54
55
56#if !SKIP_IN_DOXYGEN
57 // the names of all tracks contained in this radio
58 private List<string> trackNames;
59#endif
60 /// <summary>
61 /// The names of all tracks stored in this radio, used when selecting them in the inspector
62 /// </summary>
63 public List<string> TrackNames
64 {
65 get
66 {
67 // if the names haven't been generated, generate them
68 if (trackNames == null || trackNames.Count <= 0)
70
71 return trackNames;
72 }
73 }
74
75 /// <summary>
76 /// The IDs of all tracks stored in this radio
77 /// </summary>
78 private List<string> trackIDs;
79 public List<string> TrackIDs
80 {
81 get
82 {
83 // if the ids haven't been generated, generate them
84 if (trackIDs == null || trackIDs.Count <= 0)
86
87 return trackIDs;
88 }
89 }
90
91
92 /// <summary>
93 /// Converts a track's name to ID format
94 /// </summary>
95 /// <param name="_name">The name to convert</param>
96 /// <returns>The name transformed into ID format</returns>
97 public static string NameToID(string _name)
98 {
99 return _name.Split(", ")[0];
100 }
101
102 /// <summary>
103 /// Fills \ref TrackNames and \ref TrackIDs to match the current content of \ref trackWs
104 /// </summary>
105 private void PopulateTrackIDs()
106 {
107 // if there are no tracks, don't try to get the names
108 if (trackWs.Count <= 0)
109 return;
110
111 // reset the track info lists
112 trackNames = new List<string>();
113 trackIDs = new List<string>();
114
115 // for every track in the radio
116 for (int i = 0; i < TrackWrappers.Count; i++)
117 {
118 // cache the track
120
121 // find any other tracks with the same id as this one
122 var othersWithID = trackIDs.Where(t => t == track.id);
123
124 // if there are others, change the id of this track to match
125 if (othersWithID.Count() > 0)
126 track.id += othersWithID.Count(); // you can only add one new track at a time, so we just append the count to the end
127 // e.g a adding a track with the last one's id "music" will make the id of the new track "music1"
128
129 // combine some track info into a display id
130 string name = $"{track.id}, {track.range.x} - {track.range.y}";
131
132 // store the id and id
133 trackNames.Add(name);
134 trackIDs.Add(track.id);
135
136 // assign the id
137 track.name = name;
138 }
139 }
140
141 /// <summary>
142 /// Updates the track names and IDs when this object is changed
143 /// </summary>
144 private void OnValidate()
145 {
147 }
148
149 /// <summary>
150 /// Initialise this radio, its tracks, and referenced components
151 /// </summary>
152 public void Init()
153 {
154 BeforeInit(this);
155
156 // initialize all the tracks
157 foreach (RadioTrackWrapper trackW in TrackWrappers)
158 trackW.Init();
159
160 OnInit(this);
161 }
162
163 /// <summary>
164 /// Clears track names and IDs
165 /// </summary>
166 public void ClearCache()
167 {
168 TrackNames.Clear();
169 TrackIDs.Clear();
170 }
171
172 /// <summary>
173 /// Attempts to find a track in this radio using either an ID or a name
174 /// </summary>
175 /// <param name="_idOrName">The ID or name of the track to find</param>
176 /// <param name="_trackW">The track that has been found, or null if none was found</param>
177 /// <param name="_useID">If true, this method searches for a matching ID. If false, it searches for a matching name</param>
178 /// <returns>True if a track was found, false if not</returns>
179 public bool TryGetTrack(string _idOrName, out RadioTrackWrapper _trackW, bool _useID = true)
180 {
181 // either an id or id can be supplied here, but we always convert it to an id for this
182 string id = "";
183
184 if (_useID) // if an id was provided, use it
185 id = _idOrName;
186 else // if a id was provided, convert it to the id
187 id = NameToID(_idOrName);
188
189 // find any track with that id
190 var found = TrackWrappers.Find(t => t.id == id);
191
192 // if a track is found,
193 if (found != null)
194 {
195 // output it and return true
196 _trackW = found;
197 return true;
198 }
199 else // otherwise
200 {
201 // output it and return false
202 _trackW = null;
203 return false;
204 }
205 }
206 }
207
208}
The central data object defining the radio. Contains the tracks and information required to play the ...
Definition RadioData.cs:18
List< string > trackIDs
The IDs of all tracks stored in this radio.
Definition RadioData.cs:78
int forcedSampleRate
The sample rate this radio can force on AudioClips it references. If left at 0, it chooses the projec...
Definition RadioData.cs:33
List< string > TrackNames
The names of all tracks stored in this radio, used when selecting them in the inspector.
Definition RadioData.cs:64
Color gizmoColor
The primary colour of gizmos relating to this radio.
Definition RadioData.cs:26
void OnValidate()
Updates the track names and IDs when this object is changed.
Definition RadioData.cs:144
Action< RadioData > BeforeInit
Event invoked when Init() is called, but at the beginning before anything happens.
Definition RadioData.cs:53
static string NameToID(string _name)
Converts a track's name to ID format.
Definition RadioData.cs:97
void ClearCache()
Clears track names and IDs.
Definition RadioData.cs:166
void Init()
Initialise this radio, its tracks, and referenced components.
Definition RadioData.cs:152
Color GizmoColor
Alias for gizmoColor for safety.
Definition RadioData.cs:42
Color gizmoColorSecondary
The secondary colour of gizmos relating to this radio.
Definition RadioData.cs:28
const float LOW_TUNE
The lower limit for tune on this radio. This may become non-const at some point.
Definition RadioData.cs:21
bool TryGetTrack(string _idOrName, out RadioTrackWrapper _trackW, bool _useID=true)
Attempts to find a track in this radio using either an ID or a name.
Definition RadioData.cs:179
List< RadioTrackWrapper > trackWs
The tracks contained in this radio, editable in the inspector.
Definition RadioData.cs:38
Action< RadioData > OnInit
Event invoked when Init() is called.
Definition RadioData.cs:51
const float HIGH_TUNE
The upper limit for tune on this radio. This may become non-const at some point.
Definition RadioData.cs:23
bool forceClipSampleRate
Whether or not this radio forces the sample rate on AudioClips it references.
Definition RadioData.cs:31
List< RadioTrackWrapper > TrackWrappers
Alias for trackWs for safety- in documentation we usually call them the tracks, but for code clarity ...
Definition RadioData.cs:47
void PopulateTrackIDs()
Fills TrackNames and TrackIDs to match the current content of trackWs.
Definition RadioData.cs:105
Color GizmoColorSecondary
Alias for gizmoColorSecondary for safety.
Definition RadioData.cs:44
A wrapper class for RadioTrack so that track types can be switched between in the inspector!...
string id
The ID of this track- used to find and manipulate it in custom code.
void Init()
Initialize this wrapper and its track.
Base interfaces and classes for components, e.g: track accessors, output accessors.
Tracks to be used on a radio- includes base classes.
Definition RadioUtils.cs:20
The entire package.