Skip to content

Commit 11e6c8a

Browse files
author
setchi
committed
Refactor
1 parent 21126a9 commit 11e6c8a

File tree

10 files changed

+77
-77
lines changed

10 files changed

+77
-77
lines changed

Assets/Scenes/NoteEditor.unity

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ MonoBehaviour:
904904
fileItem: {fileID: 132690, guid: 1e268423609e86e4da1648375715dd32, type: 2}
905905
fileItemContainer: {fileID: 381058712}
906906
fileItemContainerTransform: {fileID: 381058713}
907-
LoadButton: {fileID: 1773141861}
907+
loadButton: {fileID: 1773141861}
908908
notesRegion: {fileID: 1852700502}
909909
noteObjectPrefab: {fileID: 145270, guid: 117a02bd3aa02974286fd278ec0611d1, type: 2}
910910
--- !u!222 &107512840

Assets/Scripts/Model/EditData.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
using NoteEditor.Notes;
1+
using LitJson;
2+
using NoteEditor.Model.JSON;
3+
using NoteEditor.Notes;
24
using NoteEditor.Utility;
35
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
48
using UniRx;
59

610
namespace NoteEditor.Model
@@ -20,5 +24,59 @@ public class EditData : SingletonMonoBehaviour<EditData>
2024
public static ReactiveProperty<int> BPM { get { return Instance.BPM_; } }
2125
public static ReactiveProperty<int> OffsetSamples { get { return Instance.offsetSamples_; } }
2226
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+
}
2381
}
2482
}

Assets/Scripts/Model/Settings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ public class Settings : SingletonMonoBehaviour<Settings>
1313
ReactiveProperty<string> workSpaceDirectoryPath_ = new ReactiveProperty<string>();
1414
ReactiveProperty<List<KeyCode>> noteInputKeyCodes_ = new ReactiveProperty<List<KeyCode>>();
1515
ReactiveProperty<int> selectedBlock_ = new ReactiveProperty<int>();
16-
ReactiveProperty<bool> isViewing_ = new ReactiveProperty<bool>(false);
16+
ReactiveProperty<bool> isOpen_ = new ReactiveProperty<bool>(false);
1717
Subject<Unit> requestForChangeInputNoteKeyCode_ = new Subject<Unit>();
1818

1919
public static ReactiveProperty<string> WorkSpaceDirectoryPath { get { return Instance.workSpaceDirectoryPath_; } }
2020
public static ReactiveProperty<List<KeyCode>> NoteInputKeyCodes { get { return Instance.noteInputKeyCodes_; } }
2121
public static ReactiveProperty<int> SelectedBlock { get { return Instance.selectedBlock_; } }
22-
public static ReactiveProperty<bool> IsViewing { get { return Instance.isViewing_; } }
22+
public static ReactiveProperty<bool> IsOpen { get { return Instance.isOpen_; } }
2323
public static Subject<Unit> RequestForChangeInputNoteKeyCode { get { return Instance.requestForChangeInputNoteKeyCode_; } }
2424
public static int MaxBlock = 0;
2525

Assets/Scripts/Presenter/MusicSelector/MusicSelectorPresenter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class MusicSelectorPresenter : MonoBehaviour
2525
[SerializeField]
2626
Transform fileItemContainerTransform;
2727
[SerializeField]
28-
Button LoadButton;
28+
Button loadButton;
2929
[SerializeField]
3030
GameObject notesRegion;
3131

@@ -72,7 +72,7 @@ void Start()
7272
.Subscribe(elm => elm.obj.GetComponent<FileListItem>().SetName(elm.fileName));
7373

7474

75-
LoadButton.OnClickAsObservable()
75+
loadButton.OnClickAsObservable()
7676
.Select(_ => MusicSelector.SelectedFileName.Value)
7777
.Where(fileName => !string.IsNullOrEmpty(fileName))
7878
.Subscribe(fileName => StartCoroutine(LoadMusic(fileName)));

Assets/Scripts/Presenter/NoteCanvas/InputNotesByKeyboardPresenter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ void Awake()
2020
void Init()
2121
{
2222
this.UpdateAsObservable()
23-
.Where(_ => !Settings.IsViewing.Value)
23+
.Where(_ => !Settings.IsOpen.Value)
2424
.Where(_ => !KeyInput.AltKey())
2525
.Where(_ => !KeyInput.CtrlKey())
2626
.Where(_ => !KeyInput.ShiftKey())
2727
.SelectMany(_ => Observable.Range(0, EditData.MaxBlock.Value))
28-
.Where(num => Input.GetKeyDown(Settings.NoteInputKeyCodes.Value[num]))
29-
.Subscribe(num => EnterNote(num));
28+
.Where(block => Input.GetKeyDown(Settings.NoteInputKeyCodes.Value[block]))
29+
.Subscribe(block => EnterNote(block));
3030
}
3131

3232
void EnterNote(int block)

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 = ConvertUtils.SerializeEditData();
111+
var json = EditData.SerializeEditData();
112112

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

Assets/Scripts/Presenter/Settings/InputNoteKeyCodeSettingsItem.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void Start()
3535
.AddTo(this);
3636

3737
this.UpdateAsObservable()
38-
.Where(_ => Settings.IsViewing.Value)
38+
.Where(_ => Settings.IsOpen.Value)
3939
.Where(_ => Settings.SelectedBlock.Value == block)
4040
.Where(_ => Input.anyKeyDown)
4141
.Select(_ => KeyInput.FetchKey())

Assets/Scripts/Presenter/Settings/SettingsWindowPresenter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void Awake()
7373
Settings.RequestForChangeInputNoteKeyCode.Select(_ => 0),
7474
EditData.MaxBlock,
7575
Settings.WorkSpaceDirectoryPath.Select(_ => 0))
76-
.Where(_ => Settings.IsViewing.Value)
76+
.Where(_ => Settings.IsOpen.Value)
7777
.DelayFrame(1)
7878
.Subscribe(_ => SaveSettings());
7979
}

Assets/Scripts/Presenter/Toolstrip/ToggleDisplaySettingsPresenter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,21 @@ public class ToggleDisplaySettingsPresenter : MonoBehaviour
1818
void Awake()
1919
{
2020
toggleDisplaySettingsButton.OnClickAsObservable()
21-
.Subscribe(_ => Settings.IsViewing.Value = !Settings.IsViewing.Value);
21+
.Subscribe(_ => Settings.IsOpen.Value = !Settings.IsOpen.Value);
2222

2323
Observable.Merge(
2424
this.UpdateAsObservable()
25-
.Where(_ => Settings.IsViewing.Value)
25+
.Where(_ => Settings.IsOpen.Value)
2626
.Where(_ => Input.GetKey(KeyCode.Escape)),
2727
this.UpdateAsObservable()
28-
.Where(_ => Settings.IsViewing.Value)
28+
.Where(_ => Settings.IsOpen.Value)
2929
.Where(_ => !isMouseOverOnSettingsWindow && Input.GetMouseButtonDown(0)))
30-
.Subscribe(_ => Settings.IsViewing.Value = false);
30+
.Subscribe(_ => Settings.IsOpen.Value = false);
3131

32-
Settings.IsViewing.Select(isViewing => isViewing ? Vector3.zero : Vector3.up * 100000)
32+
Settings.IsOpen.Select(isViewing => isViewing ? Vector3.zero : Vector3.up * 100000)
3333
.Subscribe(pos => settingsWindowTransform.localPosition = pos);
3434

35-
Settings.IsViewing.Subscribe(_ => Settings.SelectedBlock.Value = -1);
35+
Settings.IsOpen.Subscribe(_ => Settings.SelectedBlock.Value = -1);
3636
}
3737

3838
public void OnMouseEnterSettingsWindow()
Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
using LitJson;
1+
using NoteEditor.Model;
22
using NoteEditor.Model.JSON;
33
using NoteEditor.Notes;
4-
using NoteEditor.Model;
5-
using System.Collections.Generic;
6-
using System.IO;
7-
using System.Linq;
84
using UnityEngine;
95

106
namespace NoteEditor.Utility
@@ -58,59 +54,5 @@ public static Note ToNote(SaveDataModel.Note musicNote)
5854
new NotePosition(musicNote.LPB, musicNote.num, musicNote.block),
5955
musicNote.type == 1 ? NoteTypes.Single : NoteTypes.Long);
6056
}
61-
62-
public static string SerializeEditData()
63-
{
64-
var data = new SaveDataModel.EditData();
65-
data.BPM = EditData.BPM.Value;
66-
data.maxBlock = EditData.MaxBlock.Value;
67-
data.offset = EditData.OffsetSamples.Value;
68-
data.name = Path.GetFileNameWithoutExtension(EditData.Name.Value);
69-
70-
var sortedNoteObjects = EditData.Notes.Values
71-
.Where(note => !(note.note.type == NoteTypes.Long && EditData.Notes.ContainsKey(note.note.prev)))
72-
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, EditData.BPM.Value));
73-
74-
data.notes = new List<SaveDataModel.Note>();
75-
76-
foreach (var noteObject in sortedNoteObjects)
77-
{
78-
if (noteObject.note.type == NoteTypes.Single)
79-
{
80-
data.notes.Add(ToSaveData(noteObject));
81-
}
82-
else if (noteObject.note.type == NoteTypes.Long)
83-
{
84-
var current = noteObject;
85-
var note = ToSaveData(noteObject);
86-
87-
while (EditData.Notes.ContainsKey(current.note.next))
88-
{
89-
var nextObj = EditData.Notes[current.note.next];
90-
note.notes.Add(ToSaveData(nextObj));
91-
current = nextObj;
92-
}
93-
94-
data.notes.Add(note);
95-
}
96-
}
97-
98-
var jsonWriter = new JsonWriter();
99-
jsonWriter.PrettyPrint = true;
100-
jsonWriter.IndentValue = 4;
101-
JsonMapper.ToJson(data, jsonWriter);
102-
return jsonWriter.ToString();
103-
}
104-
105-
static SaveDataModel.Note ToSaveData(NoteObject noteObject)
106-
{
107-
var note = new SaveDataModel.Note();
108-
note.num = noteObject.note.position.num;
109-
note.block = noteObject.note.position.block;
110-
note.LPB = noteObject.note.position.LPB;
111-
note.type = noteObject.note.type == NoteTypes.Long ? 2 : 1;
112-
note.notes = new List<SaveDataModel.Note>();
113-
return note;
114-
}
11557
}
11658
}

0 commit comments

Comments
 (0)