Skip to content

Commit eb537d5

Browse files
author
setchi
committed
Refactor
1 parent 3cbe6d4 commit eb537d5

File tree

9 files changed

+179
-150
lines changed

9 files changed

+179
-150
lines changed

Assets/Scripts/Model/EditData.cs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using LitJson;
2-
using NoteEditor.Model.JSON;
3-
using NoteEditor.Notes;
1+
using NoteEditor.Notes;
42
using NoteEditor.Utility;
53
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Linq;
84
using UniRx;
95

106
namespace NoteEditor.Model
@@ -24,59 +20,5 @@ public class EditData : SingletonMonoBehaviour<EditData>
2420
public static ReactiveProperty<int> BPM { get { return Instance.BPM_; } }
2521
public static ReactiveProperty<int> OffsetSamples { get { return Instance.offsetSamples_; } }
2622
public static Dictionary<NotePosition, NoteObject> Notes { get { return Instance.notes_; } }
27-
28-
public static string SerializeEditData()
29-
{
30-
var data = new SaveDataModel.EditData();
31-
data.BPM = BPM.Value;
32-
data.maxBlock = MaxBlock.Value;
33-
data.offset = OffsetSamples.Value;
34-
data.name = Path.GetFileNameWithoutExtension(Name.Value);
35-
36-
var sortedNoteObjects = Notes.Values
37-
.Where(note => !(note.note.type == NoteTypes.Long && Notes.ContainsKey(note.note.prev)))
38-
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, BPM.Value));
39-
40-
data.notes = new List<SaveDataModel.Note>();
41-
42-
foreach (var noteObject in sortedNoteObjects)
43-
{
44-
if (noteObject.note.type == NoteTypes.Single)
45-
{
46-
data.notes.Add(ToSaveData(noteObject));
47-
}
48-
else if (noteObject.note.type == NoteTypes.Long)
49-
{
50-
var current = noteObject;
51-
var note = ToSaveData(noteObject);
52-
53-
while (Notes.ContainsKey(current.note.next))
54-
{
55-
var nextObj = Notes[current.note.next];
56-
note.notes.Add(ToSaveData(nextObj));
57-
current = nextObj;
58-
}
59-
60-
data.notes.Add(note);
61-
}
62-
}
63-
64-
var jsonWriter = new JsonWriter();
65-
jsonWriter.PrettyPrint = true;
66-
jsonWriter.IndentValue = 4;
67-
JsonMapper.ToJson(data, jsonWriter);
68-
return jsonWriter.ToString();
69-
}
70-
71-
static SaveDataModel.Note ToSaveData(NoteObject noteObject)
72-
{
73-
var note = new SaveDataModel.Note();
74-
note.num = noteObject.note.position.num;
75-
note.block = noteObject.note.position.block;
76-
note.LPB = noteObject.note.position.LPB;
77-
note.type = noteObject.note.type == NoteTypes.Long ? 2 : 1;
78-
note.notes = new List<SaveDataModel.Note>();
79-
return note;
80-
}
8123
}
8224
}

Assets/Scripts/Model/Settings.cs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
using LitJson;
2-
using NoteEditor.Model.JSON;
3-
using NoteEditor.Utility;
1+
using NoteEditor.Utility;
42
using System.Collections.Generic;
5-
using System.Linq;
63
using UniRx;
74
using UnityEngine;
85

@@ -22,32 +19,5 @@ public class Settings : SingletonMonoBehaviour<Settings>
2219
public static ReactiveProperty<bool> IsOpen { get { return Instance.isOpen_; } }
2320
public static Subject<Unit> RequestForChangeInputNoteKeyCode { get { return Instance.requestForChangeInputNoteKeyCode_; } }
2421
public static int MaxBlock = 0;
25-
26-
public static void Apply(SettingsDataModel data)
27-
{
28-
NoteInputKeyCodes.Value = data.noteInputKeyCodes
29-
.Select(keyCodeNum => (KeyCode)keyCodeNum)
30-
.ToList();
31-
32-
MaxBlock = data.maxBlock;
33-
34-
WorkSpaceDirectoryPath.Value = string.IsNullOrEmpty(data.workSpaceDirectoryPath)
35-
? Application.persistentDataPath
36-
: data.workSpaceDirectoryPath;
37-
}
38-
39-
public static string SerializeSettings()
40-
{
41-
var data = new SettingsDataModel();
42-
43-
data.workSpaceDirectoryPath = WorkSpaceDirectoryPath.Value;
44-
data.maxBlock = EditData.MaxBlock.Value;
45-
data.noteInputKeyCodes = NoteInputKeyCodes.Value
46-
.Take(EditData.MaxBlock.Value)
47-
.Select(keyCode => (int)keyCode)
48-
.ToList();
49-
50-
return JsonMapper.ToJson(data);
51-
}
5222
}
5323
}

Assets/Scripts/Presenter/MusicSelector/MusicSelectorPresenter.cs

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using LitJson;
2-
using NoteEditor.Common;
3-
using NoteEditor.Model.JSON;
4-
using NoteEditor.Notes;
1+
using NoteEditor.Common;
52
using NoteEditor.Model;
3+
using NoteEditor.Notes;
64
using NoteEditor.Utility;
75
using System;
86
using System.Collections;
@@ -28,11 +26,6 @@ public class MusicSelectorPresenter : MonoBehaviour
2826
Button loadButton;
2927
[SerializeField]
3028
GameObject notesRegion;
31-
32-
/*
33-
[SerializeField]
34-
Text selectedFileNameText;
35-
*/
3629
[SerializeField]
3730
GameObject noteObjectPrefab;
3831

@@ -45,21 +38,18 @@ void Start()
4538
MusicSelector.DirectoryPath.Subscribe(path => directoryPathInputField.text = path);
4639
MusicSelector.DirectoryPath.Value = Settings.WorkSpaceDirectoryPath.Value + "/Musics/";
4740

48-
4941
if (!Directory.Exists(MusicSelector.DirectoryPath.Value))
5042
{
5143
Directory.CreateDirectory(MusicSelector.DirectoryPath.Value);
5244
}
5345

54-
5546
Observable.Timer(TimeSpan.FromMilliseconds(300), TimeSpan.Zero)
5647
.Where(_ => Directory.Exists(MusicSelector.DirectoryPath.Value))
5748
.Select(_ => new DirectoryInfo(MusicSelector.DirectoryPath.Value).GetFiles())
5849
.Select(fileInfo => fileInfo.Select(file => file.FullName).ToList())
5950
.Where(x => !x.SequenceEqual(MusicSelector.FilePathList.Value))
6051
.Subscribe(filePathList => MusicSelector.FilePathList.Value = filePathList);
6152

62-
6353
MusicSelector.FilePathList.AsObservable()
6454
.Select(filePathList => filePathList.Select(path => Path.GetFileName(path)))
6555
.Do(_ => Enumerable.Range(0, fileItemContainerTransform.childCount)
@@ -71,13 +61,10 @@ void Start()
7161
.Do(elm => elm.obj.transform.SetParent(fileItemContainer.transform))
7262
.Subscribe(elm => elm.obj.GetComponent<FileListItem>().SetName(elm.fileName));
7363

74-
7564
loadButton.OnClickAsObservable()
7665
.Select(_ => MusicSelector.SelectedFileName.Value)
7766
.Where(fileName => !string.IsNullOrEmpty(fileName))
7867
.Subscribe(fileName => StartCoroutine(LoadMusic(fileName)));
79-
80-
// MusicSelector.SelectedFileName.SubscribeToText(selectedFileNameText);
8168
}
8269

8370
IEnumerator LoadMusic(string fileName)
@@ -92,6 +79,7 @@ IEnumerator LoadMusic(string fileName)
9279

9380
if (Audio.Source.clip == null)
9481
{
82+
// TODO: 読み込み失敗時の処理
9583
}
9684
else
9785
{
@@ -111,42 +99,7 @@ void LoadEditData()
11199
if (File.Exists(filePath))
112100
{
113101
var json = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
114-
var editData = JsonMapper.ToObject<SaveDataModel.EditData>(json);
115-
InstantiateEditData(editData);
116-
}
117-
}
118-
119-
void InstantiateEditData(SaveDataModel.EditData editData)
120-
{
121-
var notePresenter = EditNotesPresenter.Instance;
122-
123-
EditData.BPM.Value = editData.BPM;
124-
EditData.MaxBlock.Value = editData.maxBlock;
125-
EditData.OffsetSamples.Value = editData.offset;
126-
127-
foreach (var note in editData.notes)
128-
{
129-
if (note.type == 1)
130-
{
131-
notePresenter.AddNote(ConvertUtils.ToNote(note));
132-
continue;
133-
}
134-
135-
var longNoteObjects = new[] { note }.Concat(note.notes)
136-
.Select(note_ =>
137-
{
138-
notePresenter.AddNote(ConvertUtils.ToNote(note_));
139-
return EditData.Notes[ConvertUtils.ToNote(note_).position];
140-
})
141-
.ToList();
142-
143-
for (int i = 1; i < longNoteObjects.Count; i++)
144-
{
145-
longNoteObjects[i].note.prev = longNoteObjects[i - 1].note.position;
146-
longNoteObjects[i - 1].note.next = longNoteObjects[i].note.position;
147-
}
148-
149-
EditState.LongNoteTailPosition.Value = NotePosition.None;
102+
EditDataSerializer.Deserialize(json);
150103
}
151104
}
152105

Assets/Scripts/Presenter/Save/SavePresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public void Save()
108108
var fileName = Path.GetFileNameWithoutExtension(EditData.Name.Value) + ".json";
109109
var directoryPath = Settings.WorkSpaceDirectoryPath.Value + "/Notes/";
110110
var filePath = directoryPath + fileName;
111-
var json = EditData.SerializeEditData();
111+
var json = EditDataSerializer.Serialize();
112112

113113
if (!Directory.Exists(directoryPath))
114114
{

Assets/Scripts/Presenter/Settings/SettingsWindowPresenter.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using LitJson;
2-
using NoteEditor.Model.JSON;
3-
using NoteEditor.Model;
1+
using NoteEditor.Model;
2+
using NoteEditor.Utility;
43
using System.IO;
54
using System.Linq;
65
using UniRx;
@@ -19,7 +18,7 @@ public class SettingsWindowPresenter : MonoBehaviour
1918
static string fileName = "settings.json";
2019
static string filePath = directoryPath + fileName;
2120

22-
SettingsDataModel LoadSettings()
21+
string LoadSettingsJson()
2322
{
2423
if (!Directory.Exists(directoryPath))
2524
{
@@ -32,18 +31,17 @@ SettingsDataModel LoadSettings()
3231
File.WriteAllText(filePath, defaultSettings.text, System.Text.Encoding.UTF8);
3332
}
3433

35-
var json = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
36-
return JsonMapper.ToObject<SettingsDataModel>(json);
34+
return File.ReadAllText(filePath, System.Text.Encoding.UTF8);
3735
}
3836

3937
void SaveSettings()
4038
{
41-
File.WriteAllText(filePath, Settings.SerializeSettings(), System.Text.Encoding.UTF8);
39+
File.WriteAllText(filePath, SettingsSerializer.Serialize(), System.Text.Encoding.UTF8);
4240
}
4341

4442
void Awake()
4543
{
46-
Settings.Apply(LoadSettings());
44+
SettingsSerializer.Deserialize(LoadSettingsJson());
4745

4846
EditData.MaxBlock.Do(_ => Enumerable.Range(0, itemContentTransform.childCount)
4947
.Select(i => itemContentTransform.GetChild(i))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using LitJson;
2+
using NoteEditor.Model;
3+
using NoteEditor.Model.JSON;
4+
using NoteEditor.Notes;
5+
using NoteEditor.Presenter;
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
10+
namespace NoteEditor.Utility
11+
{
12+
public class EditDataSerializer
13+
{
14+
public static string Serialize()
15+
{
16+
var data = new SaveDataModel.EditData();
17+
data.BPM = EditData.BPM.Value;
18+
data.maxBlock = EditData.MaxBlock.Value;
19+
data.offset = EditData.OffsetSamples.Value;
20+
data.name = Path.GetFileNameWithoutExtension(EditData.Name.Value);
21+
22+
var sortedNoteObjects = EditData.Notes.Values
23+
.Where(note => !(note.note.type == NoteTypes.Long && EditData.Notes.ContainsKey(note.note.prev)))
24+
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, EditData.BPM.Value));
25+
26+
data.notes = new List<SaveDataModel.Note>();
27+
28+
foreach (var noteObject in sortedNoteObjects)
29+
{
30+
if (noteObject.note.type == NoteTypes.Single)
31+
{
32+
data.notes.Add(ToSaveData(noteObject));
33+
}
34+
else if (noteObject.note.type == NoteTypes.Long)
35+
{
36+
var current = noteObject;
37+
var note = ToSaveData(noteObject);
38+
39+
while (EditData.Notes.ContainsKey(current.note.next))
40+
{
41+
var nextObj = EditData.Notes[current.note.next];
42+
note.notes.Add(ToSaveData(nextObj));
43+
current = nextObj;
44+
}
45+
46+
data.notes.Add(note);
47+
}
48+
}
49+
50+
var jsonWriter = new JsonWriter();
51+
jsonWriter.PrettyPrint = true;
52+
jsonWriter.IndentValue = 4;
53+
JsonMapper.ToJson(data, jsonWriter);
54+
return jsonWriter.ToString();
55+
}
56+
57+
public static void Deserialize(string json)
58+
{
59+
var editData = JsonMapper.ToObject<SaveDataModel.EditData>(json);
60+
var notePresenter = EditNotesPresenter.Instance;
61+
62+
EditData.BPM.Value = editData.BPM;
63+
EditData.MaxBlock.Value = editData.maxBlock;
64+
EditData.OffsetSamples.Value = editData.offset;
65+
66+
foreach (var note in editData.notes)
67+
{
68+
if (note.type == 1)
69+
{
70+
notePresenter.AddNote(ConvertUtils.ToNote(note));
71+
continue;
72+
}
73+
74+
var longNoteObjects = new[] { note }.Concat(note.notes)
75+
.Select(note_ =>
76+
{
77+
notePresenter.AddNote(ConvertUtils.ToNote(note_));
78+
return EditData.Notes[ConvertUtils.ToNote(note_).position];
79+
})
80+
.ToList();
81+
82+
for (int i = 1; i < longNoteObjects.Count; i++)
83+
{
84+
longNoteObjects[i].note.prev = longNoteObjects[i - 1].note.position;
85+
longNoteObjects[i - 1].note.next = longNoteObjects[i].note.position;
86+
}
87+
88+
EditState.LongNoteTailPosition.Value = NotePosition.None;
89+
}
90+
}
91+
92+
static SaveDataModel.Note ToSaveData(NoteObject noteObject)
93+
{
94+
var note = new SaveDataModel.Note();
95+
note.num = noteObject.note.position.num;
96+
note.block = noteObject.note.position.block;
97+
note.LPB = noteObject.note.position.LPB;
98+
note.type = noteObject.note.type == NoteTypes.Long ? 2 : 1;
99+
note.notes = new List<SaveDataModel.Note>();
100+
return note;
101+
}
102+
}
103+
}

Assets/Scripts/Utility/EditDataSerializer.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)