Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
RadioComponentDataAccessor.cs
2using System;
3using System.Collections.Generic;
4using UnityEngine;
5
7{
8
9 /// <summary>
10 /// An extension of \ref RadioComponent that accesses specific tracks on the stored \ref RadioData
11 /// </summary>
13 {
14 /// <summary>
15 /// The tracks that this component affects. This is displayed with a \ref MultiselectAttribute
16 /// </summary>
17 [SerializeField, Multiselect("TrackNames")]
18 private int affectedTracks;
19
20 /// <summary>
21 /// The tracks that were affected previously- only matters if affected tracks are changed at runtime (which currently is not possible)
22 /// </summary>
23 private int lastAffectedTracks;
24
25 /// <summary>
26 /// The list of tracks on the \ref RadioData that this component can choose from
27 /// </summary>
28 protected List<string> TrackNames => data != null
29 ? data.TrackNames
30 : new() { "Data not assigned!" };
31
32 /// <summary>
33 /// Event called when the component is initialized
34 /// </summary>
35 public Action<RadioComponent> OnInit { get; set; } = new(_ => { });
36
37
38 /// <summary>
39 /// Initialises this component and links its affected tracks
40 /// </summary>
41 public override void Init()
42 {
43 // apply this component to selected tracks
45
46 // initialize the rest of this component
47 OnInit(this);
49 }
50
51 /// <summary>
52 /// Generally applicable method that converts \ref affectedTracks to a list of tracks, then calls \ref AssignToTrack() to link this component to each of them
53 /// </summary>
54 private void AssignToTracksGeneric()
55 {
56 // if the tracks have been changed since last initialize,
58 {
59 // find the different tracks as flags
60 int removedTracks = lastAffectedTracks ^ affectedTracks;
61
62 // convert those flags to indexes
63 int[] oldIndexes = MultiselectAttribute.ToInt(removedTracks);
64
65 // for each index,
66 for (int i = 0; i < oldIndexes.Length; i++)
67 {
68 // remove this component from the track
69 RadioTrackWrapper track = data.TrackWrappers[oldIndexes[i]];
70 RemoveFromTrack(track);
71 }
72 }
73
74 // convert the affected tracks from a flag int to indexes
75 int[] affectedIndexes = MultiselectAttribute.ToInt(affectedTracks);
77
78 // for each index,
79 for (int i = 0; i < affectedIndexes.Length; i++)
80 {
81 // assign this component to the indexed track
82 RadioTrackWrapper track = data.TrackWrappers[affectedIndexes[i]];
83 AssignToTrack(track);
84 }
85
86 // save the last affected tracks
88 }
89
90 /// Links this component to a track
91 protected abstract void AssignToTrack(RadioTrackWrapper _track);
92 /// Unlinks this component from a track
93 protected abstract void RemoveFromTrack(RadioTrackWrapper _track);
94
95 /// <summary>
96 /// Allows extra code for initialization so that \ref Init() can still be called
97 /// </summary>
98 protected virtual void AccessorInit() { }
99
100 }
101
102}
An extension of RadioComponent that accesses specific tracks on the stored RadioData.
List< string > TrackNames
The list of tracks on the RadioData that this component can choose from.
void AssignToTrack(RadioTrackWrapper _track)
Links this component to a track.
void AssignToTracksGeneric()
Generally applicable method that converts affectedTracks to a list of tracks, then calls AssignToTrac...
virtual void AccessorInit()
Allows extra code for initialization so that Init() can still be called.
int lastAffectedTracks
The tracks that were affected previously- only matters if affected tracks are changed at runtime (whi...
override void Init()
Initialises this component and links its affected tracks.
int affectedTracks
The tracks that this component affects. This is displayed with a MultiselectAttribute.
void RemoveFromTrack(RadioTrackWrapper _track)
Unlinks this component from a track.
Action< RadioComponent > OnInit
Event called when the component is initialized.
A scene component that holds a reference to a RadioData.
RadioData data
The RadioData (aka just radio) that this component is linked to.
A custom attribute that allows ints to display as a multiselect dropdown for a given collection,...
static int[] ToInt(int _flags)
Shorthand for MultiselectAttribute.To<int>(_flags, _options). Useful for converting a multiselect to ...
A wrapper class for RadioTrack so that track types can be switched between in the inspector!...
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