mirror of
https://github.com/RHeavenStudio/HeavenStudio.git
synced 2025-06-12 11:17:39 +02:00
Note param stuff + some other fixes
This commit is contained in:
@ -32,6 +32,8 @@ namespace HeavenStudio.Editor
|
||||
public bool active;
|
||||
|
||||
private int childCountAtStart;
|
||||
|
||||
public Dictionary<string, EventPropertyPrefab> currentProperties = new();
|
||||
|
||||
public bool canDisable = true;
|
||||
|
||||
@ -47,6 +49,7 @@ namespace HeavenStudio.Editor
|
||||
{ typeof(Float), FloatP },
|
||||
{ typeof(Note), NoteP },
|
||||
{ typeof(Dropdown), DropdownP },
|
||||
{ typeof(NoteSampleDropdown), DropdownP },
|
||||
{ typeof(Button), ButtonP },
|
||||
{ typeof(Color), ColorP },
|
||||
{ typeof(bool), BooleanP },
|
||||
@ -111,25 +114,30 @@ namespace HeavenStudio.Editor
|
||||
|
||||
DestroyParams();
|
||||
|
||||
Dictionary<string, GameObject> ePrefabs = new();
|
||||
|
||||
for (int i = 0; i < action.parameters.Count; i++)
|
||||
{
|
||||
var p = action.parameters[i];
|
||||
ePrefabs.Add(p.propertyName, AddParam(p.propertyName, p.parameter, p.caption, p.tooltip));
|
||||
currentProperties.Add(p.propertyName, AddParam(p.propertyName, p.parameter, p.caption, p.tooltip));
|
||||
}
|
||||
|
||||
foreach (var p in action.parameters)
|
||||
{
|
||||
if (p.collapseParams == null || p.collapseParams.Count == 0) continue;
|
||||
EventPropertyPrefab input = ePrefabs[p.propertyName].GetComponent<EventPropertyPrefab>();
|
||||
EventPropertyPrefab input = currentProperties[p.propertyName];
|
||||
foreach (var c in p.collapseParams)
|
||||
{
|
||||
List<GameObject> collapseables = c.collapseables.Select(x => ePrefabs[x]).ToList();
|
||||
List<GameObject> collapseables = c.collapseables.Select(x => currentProperties[x].gameObject).ToList();
|
||||
input.propertyCollapses.Add(new EventPropertyPrefab.PropertyCollapse(collapseables, c.CollapseOn, entity));
|
||||
}
|
||||
input.SetCollapses(p.parameter);
|
||||
}
|
||||
|
||||
foreach (var p in action.parameters)
|
||||
{
|
||||
EventPropertyPrefab prop = currentProperties[p.propertyName];
|
||||
|
||||
prop.PostLoadProperties(p.parameter);
|
||||
}
|
||||
|
||||
active = true;
|
||||
}
|
||||
@ -139,7 +147,7 @@ namespace HeavenStudio.Editor
|
||||
}
|
||||
}
|
||||
|
||||
private GameObject AddParam(string propertyName, object type, string caption, string tooltip = "")
|
||||
private EventPropertyPrefab AddParam(string propertyName, object type, string caption, string tooltip = "")
|
||||
{
|
||||
Type typeType = type.GetType();
|
||||
GameObject propertyPrefab = DropdownP; // enum check is hardcoded because enums are awesome (lying)
|
||||
@ -163,7 +171,7 @@ namespace HeavenStudio.Editor
|
||||
EventPropertyPrefab property = input.GetComponent<EventPropertyPrefab>();
|
||||
property.SetProperties(propertyName, type, caption);
|
||||
|
||||
return input;
|
||||
return property;
|
||||
}
|
||||
|
||||
private void DestroyParams()
|
||||
@ -174,6 +182,8 @@ namespace HeavenStudio.Editor
|
||||
{
|
||||
Destroy(transform.GetChild(i).gameObject);
|
||||
}
|
||||
|
||||
currentProperties.Clear();
|
||||
}
|
||||
}
|
||||
}
|
@ -28,6 +28,7 @@ namespace HeavenStudio.Editor
|
||||
this.caption.text = _captionText = caption;
|
||||
}
|
||||
public virtual void SetCollapses(object type) { }
|
||||
public virtual void PostLoadProperties(object type) { }
|
||||
|
||||
public void UpdateCollapse(object type)
|
||||
{
|
||||
|
@ -20,13 +20,19 @@ namespace HeavenStudio.Editor
|
||||
public Scrollbar scrollbar;
|
||||
|
||||
public int[] values;
|
||||
private int _defaultValue;
|
||||
|
||||
private int defaultValue;
|
||||
private int lastValue = -1;
|
||||
private Array enumValues;
|
||||
private object type;
|
||||
|
||||
private bool openedDropdown = false;
|
||||
|
||||
private bool setup = false;
|
||||
|
||||
public override void SetProperties(string propertyName, object type, string caption)
|
||||
{
|
||||
base.SetProperties(propertyName, type, caption);
|
||||
|
||||
this.type = type;
|
||||
|
||||
int selected = 0;
|
||||
|
||||
@ -34,13 +40,14 @@ namespace HeavenStudio.Editor
|
||||
{
|
||||
case EntityTypes.Dropdown dropdownEntity:
|
||||
// entity[propertyName].ChangeValues(dropdownEntity.Values);
|
||||
_defaultValue = dropdownEntity.defaultValue;
|
||||
defaultValue = dropdownEntity.defaultValue;
|
||||
EntityTypes.DropdownObj dropdownObj = entity[propertyName];
|
||||
|
||||
int size = dropdownObj.Values.Count;
|
||||
values = new int[size];
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
values[i] = i;
|
||||
}
|
||||
|
||||
@ -53,32 +60,51 @@ namespace HeavenStudio.Editor
|
||||
dropdown.ClearOptions();
|
||||
dropdown.AddOptions(newValues);
|
||||
dropdown.enabled = newValues.Count > 0;
|
||||
dropdownObj.value = _defaultValue;
|
||||
dropdownObj.value = defaultValue;
|
||||
});
|
||||
break;
|
||||
case Enum enumEntity:
|
||||
Type enumType = enumEntity.GetType();
|
||||
_defaultValue = (int)type;
|
||||
values = Enum.GetValues(enumType).Cast<int>().ToArray();
|
||||
defaultValue = (int)type;
|
||||
enumValues = Enum.GetValues(enumType);
|
||||
values = enumValues.Cast<int>().ToArray();
|
||||
selected = Array.FindIndex(values, val => val == (int)entity[propertyName]);
|
||||
|
||||
dropdown.AddOptions(Enum.GetNames(enumType).ToList());
|
||||
dropdown.onValueChanged.AddListener(val => entity[propertyName] = values[val]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
case EntityTypes.NoteSampleDropdown noteDropdown:
|
||||
Type noteEnumType = noteDropdown.defaultValue.GetType();
|
||||
enumValues = Enum.GetValues(noteEnumType);
|
||||
values = enumValues.Cast<int>().ToArray();
|
||||
selected = Array.FindIndex(values, val => val == (int)entity[propertyName]);
|
||||
defaultValue = selected;
|
||||
lastValue = selected;
|
||||
|
||||
dropdown.AddOptions(Enum.GetNames(noteEnumType).ToList());
|
||||
dropdown.onValueChanged.AddListener(val =>
|
||||
{
|
||||
entity[propertyName] = values[val];
|
||||
UpdateNoteProperty(noteDropdown, enumValues.GetValue(values[val]));
|
||||
|
||||
lastValue = values[val];
|
||||
});
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
dropdown.value = selected;
|
||||
dropdown.enabled = dropdown.options.Count > 0;
|
||||
|
||||
dropdown.onValueChanged.AddListener(newValue => {
|
||||
this.caption.text = (newValue != _defaultValue) ? (_captionText + "*") : _captionText;
|
||||
dropdown.onValueChanged.AddListener(newValue =>
|
||||
{
|
||||
this.caption.text = (newValue != defaultValue) ? (_captionText + "*") : _captionText;
|
||||
});
|
||||
}
|
||||
|
||||
public void ResetValue()
|
||||
{
|
||||
dropdown.value = _defaultValue;
|
||||
dropdown.value = defaultValue;
|
||||
}
|
||||
|
||||
public override void SetCollapses(object type)
|
||||
@ -105,5 +131,44 @@ namespace HeavenStudio.Editor
|
||||
openedDropdown = false;
|
||||
}
|
||||
}
|
||||
|
||||
#region Note Sample Dropdown
|
||||
private void OnEnable() { // Used for when the dropdown is uncollapsed
|
||||
if (setup && type is EntityTypes.NoteSampleDropdown sampleDropdown)
|
||||
{
|
||||
UpdateNoteProperty(sampleDropdown, enumValues.GetValue(values[entity[propertyName]]), true);
|
||||
}
|
||||
}
|
||||
|
||||
public override void PostLoadProperties(object type)
|
||||
{
|
||||
base.PostLoadProperties(type);
|
||||
|
||||
setup = true;
|
||||
|
||||
if (type is EntityTypes.NoteSampleDropdown sampleDropdown && gameObject.activeSelf)
|
||||
{
|
||||
UpdateNoteProperty(sampleDropdown, enumValues.GetValue(values[entity[propertyName]]));
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateNoteProperty(EntityTypes.NoteSampleDropdown noteDropdown, object newSampleEnum, bool forceSwitchCheck = false)
|
||||
{
|
||||
EventParameterManager.instance.currentProperties.TryGetValue(noteDropdown.semisProp, out var property);
|
||||
|
||||
if (!property) return;
|
||||
|
||||
NotePropertyPrefab noteProperty = (NotePropertyPrefab)property;
|
||||
NoteSample sample = noteDropdown.getNoteSample(newSampleEnum);
|
||||
|
||||
bool switched = false;
|
||||
if ((int)newSampleEnum != lastValue || forceSwitchCheck) {
|
||||
// Keep the semitones value if the note is the same, otherwise reset it
|
||||
if(sample.note != noteProperty.note.sampleNote) parameterManager.entity[noteDropdown.semisProp] = 0;
|
||||
switched = true;
|
||||
}
|
||||
noteProperty.SetNote(new EntityTypes.Note(0, sample.note, sample.octave, sample.sample, offsetToC: false), switched);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
@ -10,30 +10,17 @@ using UnityEngine;
|
||||
|
||||
public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
{
|
||||
public TMP_Text noteLabel;
|
||||
|
||||
public TMP_Text noteLabel, flatLabel;
|
||||
|
||||
private Sound previewAudioSource;
|
||||
private EntityTypes.Note note;
|
||||
private int offsetFromC;
|
||||
public EntityTypes.Note note;
|
||||
|
||||
public override void SetProperties(string propertyName, object type, string caption)
|
||||
{
|
||||
base.SetProperties(propertyName, type, caption);
|
||||
|
||||
note = (EntityTypes.Note)type;
|
||||
|
||||
slider.minValue = note.min;
|
||||
slider.maxValue = note.max;
|
||||
|
||||
slider.wholeNumbers = true;
|
||||
|
||||
offsetFromC = 3 - note.sampleNote;
|
||||
|
||||
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]) - offsetFromC;
|
||||
_defaultValue = slider.value;
|
||||
|
||||
inputField.text = slider.value.ToString();
|
||||
noteLabel.text = GetNoteText(note, (int)slider.value + offsetFromC);
|
||||
SetNote((EntityTypes.Note)type);
|
||||
|
||||
slider.onValueChanged.AddListener(
|
||||
_ =>
|
||||
@ -50,7 +37,7 @@ public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
this.caption.text = _captionText;
|
||||
}
|
||||
|
||||
noteLabel.text = GetNoteText(note, trueSemitones);
|
||||
UpdateNoteText(trueSemitones);
|
||||
|
||||
PlayPreview(note, trueSemitones);
|
||||
}
|
||||
@ -64,9 +51,10 @@ public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
inputField.onEndEdit.AddListener(
|
||||
_ =>
|
||||
{
|
||||
int trueSemitones = (int)slider.value + offsetFromC;
|
||||
|
||||
slider.value = Convert.ToSingle(inputField.text);
|
||||
|
||||
int trueSemitones = (int)slider.value + offsetFromC;
|
||||
|
||||
parameterManager.entity[propertyName] = trueSemitones;
|
||||
Editor.instance.editingInputField = false;
|
||||
if (slider.value != _defaultValue)
|
||||
@ -78,12 +66,43 @@ public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
this.caption.text = _captionText;
|
||||
}
|
||||
|
||||
noteLabel.text = GetNoteText(note, trueSemitones);
|
||||
UpdateNoteText(trueSemitones);
|
||||
|
||||
PlayPreview(note, trueSemitones);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public void SetNote(EntityTypes.Note note, bool playPreview = false)
|
||||
{
|
||||
this.note = note;
|
||||
|
||||
slider.minValue = note.min;
|
||||
slider.maxValue = note.max;
|
||||
|
||||
slider.wholeNumbers = true;
|
||||
|
||||
offsetFromC = 0;
|
||||
if(note.offsetToC)
|
||||
offsetFromC = 3 - note.sampleNote;
|
||||
|
||||
int lastValue = (int)slider.value;
|
||||
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]) - offsetFromC;
|
||||
_defaultValue = slider.value;
|
||||
|
||||
inputField.text = slider.value.ToString();
|
||||
UpdateNoteText((int)slider.value + offsetFromC);
|
||||
|
||||
if((int)slider.value == lastValue && playPreview)
|
||||
PlayPreview(note, (int)slider.value + offsetFromC);
|
||||
}
|
||||
|
||||
private void UpdateNoteText(int semiTones)
|
||||
{
|
||||
GetNoteText(note, semiTones, out var sharp, out var flat);
|
||||
noteLabel.text = sharp;
|
||||
flatLabel.text = flat;
|
||||
}
|
||||
|
||||
public void OnSelectSliderHandle()
|
||||
{
|
||||
@ -92,21 +111,24 @@ public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
|
||||
private void PlayPreview(EntityTypes.Note note, int currentSemitones)
|
||||
{
|
||||
if (note.sampleName.Equals("") || !PersistentDataManager.gameSettings.previewNoteSounds) return;
|
||||
if (note.sampleName == null || !PersistentDataManager.gameSettings.previewNoteSounds) return;
|
||||
|
||||
if (previewAudioSource != null)
|
||||
{
|
||||
previewAudioSource.Stop(true);
|
||||
previewAudioSource.KillLoop();
|
||||
previewAudioSource = null;
|
||||
}
|
||||
|
||||
float pitch = SoundByte.GetPitchFromSemiTones(currentSemitones, true);
|
||||
if(pitch == 1f) pitch = 1.0001f; // man writes worst workaround ever, banned from Heaven Studio source code
|
||||
previewAudioSource = SoundByte.PlayOneShotGame(note.sampleName, pitch: pitch, volume: 0.75f, forcePlay: true, ignoreConductorPause: true);
|
||||
previewAudioSource.KillLoop(.5f);
|
||||
}
|
||||
|
||||
private static readonly string[] notes = { "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#" };
|
||||
private static readonly string[] notesFlat = { "A", "Bb", "B", "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab" };
|
||||
|
||||
private static string GetNoteText(EntityTypes.Note note, int currentSemitones)
|
||||
private static string GetNoteText(EntityTypes.Note note, int currentSemitones, out string sharp, out string flat)
|
||||
{
|
||||
int noteIndex = (note.sampleNote + currentSemitones) % 12;
|
||||
if (noteIndex < 0)
|
||||
@ -121,7 +143,9 @@ public class NotePropertyPrefab : NumberPropertyPrefab
|
||||
{
|
||||
octave--;
|
||||
}
|
||||
|
||||
return notes[noteIndex] + octave;
|
||||
|
||||
sharp = notes[noteIndex] + octave;
|
||||
flat = notesFlat[noteIndex] + octave;
|
||||
return sharp;
|
||||
}
|
||||
}
|
||||
|
@ -158,28 +158,6 @@ namespace HeavenStudio.Editor
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly string[] notes = {
|
||||
"A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"
|
||||
};
|
||||
|
||||
private static string GetNoteText(EntityTypes.Note note, int newSemitones)
|
||||
{
|
||||
int noteIndex = (note.sampleNote + newSemitones) % 12;
|
||||
if (noteIndex < 0) {
|
||||
noteIndex += 12;
|
||||
}
|
||||
|
||||
int octaveOffset = (note.sampleNote + newSemitones) / 12;
|
||||
int octave = note.sampleOctave + octaveOffset;
|
||||
|
||||
if ((note.sampleNote + newSemitones) % 12 < 0)
|
||||
{
|
||||
octave--;
|
||||
}
|
||||
|
||||
return notes[noteIndex] + octave;
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
|
Reference in New Issue
Block a user