Merge pull request #151 from huantianad/random-code-refactoring

Random code refactoring
This commit is contained in:
minenice55 2022-08-19 19:17:59 -04:00 committed by GitHub
commit c6e4f477fb
2 changed files with 147 additions and 156 deletions

View file

@ -380,46 +380,34 @@ namespace HeavenStudio.Editor
{ {
var path = Path.Combine(paths); var path = Path.Combine(paths);
if (path != String.Empty) if (path == string.Empty) return;
{
loadedMusic = false; loadedMusic = false;
using (FileStream zipFile = File.Open(path, FileMode.Open)) using var zipFile = File.Open(path, FileMode.Open);
using var archive = new ZipArchive(zipFile, ZipArchiveMode.Read);
foreach (var entry in archive.Entries)
switch (entry.Name)
{ {
using (var archive = new ZipArchive(zipFile, ZipArchiveMode.Read)) case "remix.json":
{ {
foreach (ZipArchiveEntry entry in archive.Entries) using var stream = entry.Open();
{ using var reader = new StreamReader(stream);
if (entry.Name == "remix.json") LoadRemix(reader.ReadToEnd());
{
using (var stream = entry.Open()) break;
{
byte[] bytes;
using (var ms = new MemoryStream())
{
stream.CopyTo(ms);
bytes = ms.ToArray();
string json = Encoding.UTF8.GetString(bytes);
LoadRemix(json);
} }
} case "song.ogg":
}
else if (entry.Name == "song.ogg")
{ {
using (var stream = entry.Open()) using var stream = entry.Open();
{ using var memoryStream = new MemoryStream();
byte[] bytes; stream.CopyTo(memoryStream);
using (var ms = new MemoryStream()) var bytes = memoryStream.ToArray();
{
stream.CopyTo(ms);
bytes = ms.ToArray();
Conductor.instance.musicSource.clip = OggVorbis.VorbisPlugin.ToAudioClip(bytes, "music"); Conductor.instance.musicSource.clip = OggVorbis.VorbisPlugin.ToAudioClip(bytes, "music");
loadedMusic = true; loadedMusic = true;
Timeline.FitToSong(); Timeline.FitToSong();
}
} break;
}
}
} }
} }
@ -431,7 +419,6 @@ namespace HeavenStudio.Editor
UpdateEditorStatus(false); UpdateEditorStatus(false);
CommandManager.instance.Clear(); CommandManager.instance.Clear();
Timeline.FitToSong(); Timeline.FitToSong();
}
}); });
} }

View file

@ -3,7 +3,7 @@ using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
using System; using System;
using System.Linq;
using TMPro; using TMPro;
using Starpelly; using Starpelly;
@ -47,135 +47,139 @@ namespace HeavenStudio.Editor
this.propertyName = propertyName; this.propertyName = propertyName;
this.caption.text = caption; this.caption.text = caption;
var objType = type.GetType(); switch (type)
if (objType == typeof(EntityTypes.Integer))
{ {
var integer = ((EntityTypes.Integer)type); case EntityTypes.Integer integer:
slider.minValue = integer.min; slider.minValue = integer.min;
slider.maxValue = integer.max; slider.maxValue = integer.max;
slider.value = Mathf.RoundToInt(System.Convert.ToSingle(parameterManager.entity[propertyName])); slider.wholeNumbers = true;
slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString(); inputField.text = slider.value.ToString();
slider.onValueChanged.AddListener(delegate slider.onValueChanged.AddListener(
_ =>
{ {
inputField.text = slider.value.ToString(); inputField.text = slider.value.ToString();
parameterManager.entity[propertyName] = (int) slider.value; parameterManager.entity[propertyName] = (int) slider.value;
}); }
);
inputField.onSelect.AddListener(delegate inputField.onSelect.AddListener(
{ _ =>
Editor.instance.editingInputField = true; Editor.instance.editingInputField = true
}); );
inputField.onEndEdit.AddListener(delegate inputField.onEndEdit.AddListener(
_ =>
{ {
slider.value = Mathf.RoundToInt(System.Convert.ToSingle(System.Convert.ToSingle(inputField.text))); slider.value = Convert.ToSingle(inputField.text);
parameterManager.entity[propertyName] = (int) slider.value; parameterManager.entity[propertyName] = (int) slider.value;
Editor.instance.editingInputField = false; Editor.instance.editingInputField = false;
});
} }
else if (objType == typeof(EntityTypes.Float)) );
{ break;
var fl = ((EntityTypes.Float)type);
case EntityTypes.Float fl:
slider.minValue = fl.min; slider.minValue = fl.min;
slider.maxValue = fl.max; slider.maxValue = fl.max;
slider.value = System.Convert.ToSingle(parameterManager.entity[propertyName]); slider.value = Convert.ToSingle(parameterManager.entity[propertyName]);
inputField.text = slider.value.ToString("G"); inputField.text = slider.value.ToString("G");
slider.onValueChanged.AddListener(delegate slider.onValueChanged.AddListener(
_ =>
{ {
var newValue = (float)System.Math.Round(slider.value, 4); var newValue = (float) Math.Round(slider.value, 4);
inputField.text = newValue.ToString("G"); inputField.text = newValue.ToString("G");
parameterManager.entity[propertyName] = newValue; parameterManager.entity[propertyName] = newValue;
}); }
);
inputField.onSelect.AddListener(delegate inputField.onSelect.AddListener(
{ _ =>
Editor.instance.editingInputField = true; Editor.instance.editingInputField = true
}); );
inputField.onEndEdit.AddListener(delegate inputField.onEndEdit.AddListener(
_ =>
{ {
slider.value = (float)System.Math.Round(System.Convert.ToSingle(inputField.text), 4); slider.value = (float) Math.Round(Convert.ToSingle(inputField.text), 4);
parameterManager.entity[propertyName] = slider.value; parameterManager.entity[propertyName] = slider.value;
Editor.instance.editingInputField = false; Editor.instance.editingInputField = false;
});
} }
else if(type is bool) );
{ break;
toggle.isOn = System.Convert.ToBoolean(parameterManager.entity[propertyName]); // ' (bool)type ' always results in false
toggle.onValueChanged.AddListener(delegate case bool _:
{ // ' (bool)type ' always results in false
parameterManager.entity[propertyName] = toggle.isOn; toggle.isOn = Convert.ToBoolean(parameterManager.entity[propertyName]);
});
}
else if (objType.IsEnum)
{
List<TMP_Dropdown.OptionData> dropDownData = new List<TMP_Dropdown.OptionData>();
var vals = Enum.GetValues(objType);
var selected = 0;
for (int i = 0; i < vals.Length; i++)
{
string name = Enum.GetNames(objType)[i];
TMP_Dropdown.OptionData optionData = new TMP_Dropdown.OptionData();
optionData.text = name; toggle.onValueChanged.AddListener(
_ => parameterManager.entity[propertyName] = toggle.isOn
);
break;
dropDownData.Add(optionData); case Color _:
colorPreview.colorPicker.onColorChanged += _ =>
parameterManager.entity[propertyName] = colorPreview.colorPicker.color;
if ((int)vals.GetValue(i) == (int)parameterManager.entity[propertyName]) var paramCol = (Color) parameterManager.entity[propertyName];
selected = i;
}
dropdown.AddOptions(dropDownData);
dropdown.value = selected;
dropdown.onValueChanged.AddListener(delegate ColorBTN.onClick.AddListener(
{ () =>
parameterManager.entity[propertyName] = (int)Enum.GetValues(objType).GetValue(dropdown.value);
});
}
else if (objType == typeof(Color))
{
colorPreview.colorPicker.onColorChanged += delegate
{
parameterManager.entity[propertyName] = (Color)colorPreview.colorPicker.color;
};
Color paramCol = (Color)parameterManager.entity[propertyName];
ColorBTN.onClick.AddListener(delegate
{ {
ColorTable.gameObject.SetActive(true); ColorTable.gameObject.SetActive(true);
colorTableActive = true; colorTableActive = true;
colorPreview.ChangeColor(paramCol); colorPreview.ChangeColor(paramCol);
}); }
);
colorPreview.ChangeColor(paramCol); colorPreview.ChangeColor(paramCol);
ColorTable.gameObject.SetActive(false); ColorTable.gameObject.SetActive(false);
} break;
//why the FUCK wasn't this a thing before lmao
else if(objType == typeof(string))
{
// Debug.Log("entity " + propertyName + " is: " + (string)(parameterManager.entity[propertyName]));
inputFieldString.text = (string)(parameterManager.entity[propertyName]);
inputFieldString.onSelect.AddListener(delegate case string _:
{ inputFieldString.text = (string) parameterManager.entity[propertyName];
Editor.instance.editingInputField = true;
});
inputFieldString.onEndEdit.AddListener(delegate inputFieldString.onSelect.AddListener(
{ _ =>
// Debug.Log("setting " + propertyName + " to: " + inputFieldString.text); Editor.instance.editingInputField = true
);
inputFieldString.onEndEdit.AddListener(
_ =>
{;
parameterManager.entity[propertyName] = inputFieldString.text; parameterManager.entity[propertyName] = inputFieldString.text;
Editor.instance.editingInputField = false; Editor.instance.editingInputField = false;
}); }
);
break;
case Enum enumKind:
var enumType = enumKind.GetType();
var enumVals = Enum.GetValues(enumType);
var enumNames = Enum.GetNames(enumType).ToList();
// Can we assume non-holey enum?
// If we can we can simplify to dropdown.value = (int) parameterManager.entity[propertyName]
var currentlySelected = (int) parameterManager.entity[propertyName];
var selected = enumVals
.Cast<object>()
.ToList()
.FindIndex(val => (int) val == currentlySelected);
dropdown.AddOptions(enumNames);
dropdown.value = selected;
dropdown.onValueChanged.AddListener(_ =>
parameterManager.entity[propertyName] = Enum.ToObject(enumType, dropdown.value)
);
break;
default:
throw new ArgumentOutOfRangeException(
nameof(type), type, "I don't know how to make a property of this type!"
);
} }
} }