Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recorder/Sender の setting を ScriptableObject で保存する #20

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions Assets/ArtNet/Editor/DmxRecorder/DmxRecordWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
Expand Down Expand Up @@ -36,6 +35,11 @@ public void CreateGUI()
Initialize(visualElement);
}

private void OnDestroy()
{
SaveConfig();
}

[MenuItem("ArtNet/DmxRecorder")]
public static void ShowDmxRecorder()
{
Expand All @@ -45,6 +49,8 @@ public static void ShowDmxRecorder()

private void Initialize(VisualElement root)
{
_recorder.RecorderSettings = RecorderSettings.GetOrNewGlobalSettings();

var tabContent = new VisualElement { name = "tabContent" };
root.Add(tabContent);

Expand All @@ -55,21 +61,6 @@ private void Initialize(VisualElement root)
senderVisualElement.name = "senderPanel";
tabContent.Add(senderVisualElement);

var recordFormatStringType = EditorUserSettings.GetConfigValue(EditorSettingKey("OutputFormat")) ??
RecodeFormat.Binary.ToString();
var format = (RecodeFormat) Enum.Parse(typeof(RecodeFormat), recordFormatStringType);
var binaryRecordConfig = new BinaryRecordConfig
{
Directory = EditorUserSettings.GetConfigValue(EditorSettingKey("OutputDirectory")) ??
Application.dataPath,
FileName = EditorUserSettings.GetConfigValue(EditorSettingKey("OutputFileName")) ??
"dmx-record",
};

var animationClipRecordConfig = new AnimationClipRecordConfig();

_recorder.RecordConfigs = new RecordConfigs(format, binaryRecordConfig, animationClipRecordConfig);

InitializeHeaderTab(root);
InitializeRecorder(recorderVisualElement);
InitializeSender(senderVisualElement);
Expand Down Expand Up @@ -105,7 +96,5 @@ private void InitializeHeaderTab(VisualElement root)
recorderPanel.style.display = DisplayStyle.None;
});
}

private static string EditorSettingKey(string key) => $"{EditorSettingPrefix}{key}";
}
}
73 changes: 0 additions & 73 deletions Assets/ArtNet/Editor/DmxRecorder/RecordConfigs.cs

This file was deleted.

8 changes: 4 additions & 4 deletions Assets/ArtNet/Editor/DmxRecorder/Recorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public Recorder()
_receiver.OnReceivedPacket = OnReceivedPacket;
}
public RecordingStatus Status { get; private set; } = RecordingStatus.None;
public RecordConfigs RecordConfigs { get; set; }
public RecorderSettings RecorderSettings { get; set; }

public int GetRecordedCount() => _recordedDmx.Count;

Expand Down Expand Up @@ -124,7 +124,7 @@ private void StoreDmxPacket()
return;
}

switch (RecordConfigs.RecordFormat)
switch (RecorderSettings.RecordFormat)
{
case RecodeFormat.Binary:
StoreBinary();
Expand All @@ -139,7 +139,7 @@ private void StoreDmxPacket()

private void StoreBinary()
{
var binaryConfig = RecordConfigs.BinaryConfig;
var binaryConfig = RecorderSettings.BinarySetting;

if (!Directory.Exists(binaryConfig.Directory))
{
Expand All @@ -157,7 +157,7 @@ private void StoreBinary()

private void StoreAnimationClip()
{
var animationClipConfig = RecordConfigs.AnimationClipConfig;
var animationClipConfig = RecorderSettings.AnimationClipSetting;
var timelineConverter = new TimelineConverter(_recordedDmx);
timelineConverter.SaveDmxTimelineClips(animationClipConfig.OutputAnimationClipAssetPath);
}
Expand Down
136 changes: 136 additions & 0 deletions Assets/ArtNet/Editor/DmxRecorder/RecorderSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Object = UnityEngine.Object;

namespace ArtNet.Editor.DmxRecorder
{
public enum RecodeFormat
{
Binary = 0,
AnimationClip = 1,
}

public class RecorderSettings : ScriptableObject
{
[SerializeField] private RecodeFormat _recordFormat;

private string _savePath;

public RecodeFormat RecordFormat => _recordFormat;

public BinaryRecordSetting BinarySetting { get; } = new();
public AnimationClipRecordSetting AnimationClipSetting { get; } = new();

public static RecorderSettings GetOrNewGlobalSettings()
{
var globalPath = Path.Combine(Application.dataPath, "..", "Library", "ArtNet", "DmxRecorderSettings.asset");
return Load(globalPath);
}

private static RecorderSettings Load(string path)
{
RecorderSettings settings;
try
{
var objs = InternalEditorUtility.LoadSerializedFileAndForget(path);
settings = objs.FirstOrDefault(o => o is RecorderSettings) as RecorderSettings;
}
catch (Exception e)
{
Debug.LogError($"Failed to load RecorderSettings: {e.Message}");
settings = null;
}

if (settings == null)
{
settings = CreateInstance<RecorderSettings>();
// Settings.hideFlags = HideFlags.HideAndDontSave;
settings.name = "DmxRecorderSettings";
}

settings._savePath = path;
return settings;
}

public void Save()
{
if (string.IsNullOrEmpty(_savePath)) return;

try
{
var directory = Path.GetDirectoryName(_savePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Directory.CreateDirectory(directory);

var objs = new Object[] { this };
InternalEditorUtility.SaveToSerializedFileAndForget(objs, _savePath, true);
}
catch (Exception e)
{
Debug.LogError($"Failed to save RecorderSettings: {e.Message}");
}
}

public void ChangeRecordFormat(RecodeFormat format)
{
EditorUtility.SetDirty(this);
Undo.RecordObject(this, "Change Record Format");
if (_recordFormat == format) return;

_recordFormat = format;
Save();
}

private IRecordSetting Setting => RecordFormat switch
{
RecodeFormat.Binary => BinarySetting,
RecodeFormat.AnimationClip => AnimationClipSetting,
_ => throw new System.NotImplementedException()
};

public bool Validate() => ValidateErrors().Count == 0;
public List<string> ValidateErrors() => Setting.ValidateErrors();
}

public class BinaryRecordSetting : IRecordSetting
{
private const string Extension = ".dmx";

public string Directory { get; set; }
public string FileName { get; set; }

public string OutputPath => $"{Directory}/{FileName}{Extension}";

public List<string> ValidateErrors()
{
var errors = new List<string>();
if (!ValidateDirectory()) errors.Add("Directory is not set");
if (!ValidateFileName()) errors.Add("FileName is not set");

return errors;
}

private bool ValidateDirectory() => !string.IsNullOrEmpty(Directory);
private bool ValidateFileName() => !string.IsNullOrEmpty(FileName);
}

public class AnimationClipRecordSetting : IRecordSetting
{
public string OutputAnimationClipAssetPath { get; set; } = "Assets/Recording";

public List<string> ValidateErrors()
{
return new List<string>();
}
}

public interface IRecordSetting
{
public List<string> ValidateErrors();
}
}
Loading