Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
setchi committed Oct 6, 2015
1 parent 3cbe6d4 commit eb537d5
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 150 deletions.
60 changes: 1 addition & 59 deletions Assets/Scripts/Model/EditData.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using LitJson;
using NoteEditor.Model.JSON;
using NoteEditor.Notes;
using NoteEditor.Notes;
using NoteEditor.Utility;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UniRx;

namespace NoteEditor.Model
Expand All @@ -24,59 +20,5 @@ public class EditData : SingletonMonoBehaviour<EditData>
public static ReactiveProperty<int> BPM { get { return Instance.BPM_; } }
public static ReactiveProperty<int> OffsetSamples { get { return Instance.offsetSamples_; } }
public static Dictionary<NotePosition, NoteObject> Notes { get { return Instance.notes_; } }

public static string SerializeEditData()
{
var data = new SaveDataModel.EditData();
data.BPM = BPM.Value;
data.maxBlock = MaxBlock.Value;
data.offset = OffsetSamples.Value;
data.name = Path.GetFileNameWithoutExtension(Name.Value);

var sortedNoteObjects = Notes.Values
.Where(note => !(note.note.type == NoteTypes.Long && Notes.ContainsKey(note.note.prev)))
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, BPM.Value));

data.notes = new List<SaveDataModel.Note>();

foreach (var noteObject in sortedNoteObjects)
{
if (noteObject.note.type == NoteTypes.Single)
{
data.notes.Add(ToSaveData(noteObject));
}
else if (noteObject.note.type == NoteTypes.Long)
{
var current = noteObject;
var note = ToSaveData(noteObject);

while (Notes.ContainsKey(current.note.next))
{
var nextObj = Notes[current.note.next];
note.notes.Add(ToSaveData(nextObj));
current = nextObj;
}

data.notes.Add(note);
}
}

var jsonWriter = new JsonWriter();
jsonWriter.PrettyPrint = true;
jsonWriter.IndentValue = 4;
JsonMapper.ToJson(data, jsonWriter);
return jsonWriter.ToString();
}

static SaveDataModel.Note ToSaveData(NoteObject noteObject)
{
var note = new SaveDataModel.Note();
note.num = noteObject.note.position.num;
note.block = noteObject.note.position.block;
note.LPB = noteObject.note.position.LPB;
note.type = noteObject.note.type == NoteTypes.Long ? 2 : 1;
note.notes = new List<SaveDataModel.Note>();
return note;
}
}
}
32 changes: 1 addition & 31 deletions Assets/Scripts/Model/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using LitJson;
using NoteEditor.Model.JSON;
using NoteEditor.Utility;
using NoteEditor.Utility;
using System.Collections.Generic;
using System.Linq;
using UniRx;
using UnityEngine;

Expand All @@ -22,32 +19,5 @@ public class Settings : SingletonMonoBehaviour<Settings>
public static ReactiveProperty<bool> IsOpen { get { return Instance.isOpen_; } }
public static Subject<Unit> RequestForChangeInputNoteKeyCode { get { return Instance.requestForChangeInputNoteKeyCode_; } }
public static int MaxBlock = 0;

public static void Apply(SettingsDataModel data)
{
NoteInputKeyCodes.Value = data.noteInputKeyCodes
.Select(keyCodeNum => (KeyCode)keyCodeNum)
.ToList();

MaxBlock = data.maxBlock;

WorkSpaceDirectoryPath.Value = string.IsNullOrEmpty(data.workSpaceDirectoryPath)
? Application.persistentDataPath
: data.workSpaceDirectoryPath;
}

public static string SerializeSettings()
{
var data = new SettingsDataModel();

data.workSpaceDirectoryPath = WorkSpaceDirectoryPath.Value;
data.maxBlock = EditData.MaxBlock.Value;
data.noteInputKeyCodes = NoteInputKeyCodes.Value
.Take(EditData.MaxBlock.Value)
.Select(keyCode => (int)keyCode)
.ToList();

return JsonMapper.ToJson(data);
}
}
}
55 changes: 4 additions & 51 deletions Assets/Scripts/Presenter/MusicSelector/MusicSelectorPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using LitJson;
using NoteEditor.Common;
using NoteEditor.Model.JSON;
using NoteEditor.Notes;
using NoteEditor.Common;
using NoteEditor.Model;
using NoteEditor.Notes;
using NoteEditor.Utility;
using System;
using System.Collections;
Expand All @@ -28,11 +26,6 @@ public class MusicSelectorPresenter : MonoBehaviour
Button loadButton;
[SerializeField]
GameObject notesRegion;

/*
[SerializeField]
Text selectedFileNameText;
*/
[SerializeField]
GameObject noteObjectPrefab;

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


if (!Directory.Exists(MusicSelector.DirectoryPath.Value))
{
Directory.CreateDirectory(MusicSelector.DirectoryPath.Value);
}


Observable.Timer(TimeSpan.FromMilliseconds(300), TimeSpan.Zero)
.Where(_ => Directory.Exists(MusicSelector.DirectoryPath.Value))
.Select(_ => new DirectoryInfo(MusicSelector.DirectoryPath.Value).GetFiles())
.Select(fileInfo => fileInfo.Select(file => file.FullName).ToList())
.Where(x => !x.SequenceEqual(MusicSelector.FilePathList.Value))
.Subscribe(filePathList => MusicSelector.FilePathList.Value = filePathList);


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


loadButton.OnClickAsObservable()
.Select(_ => MusicSelector.SelectedFileName.Value)
.Where(fileName => !string.IsNullOrEmpty(fileName))
.Subscribe(fileName => StartCoroutine(LoadMusic(fileName)));

// MusicSelector.SelectedFileName.SubscribeToText(selectedFileNameText);
}

IEnumerator LoadMusic(string fileName)
Expand All @@ -92,6 +79,7 @@ IEnumerator LoadMusic(string fileName)

if (Audio.Source.clip == null)
{
// TODO: 読み込み失敗時の処理
}
else
{
Expand All @@ -111,42 +99,7 @@ void LoadEditData()
if (File.Exists(filePath))
{
var json = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
var editData = JsonMapper.ToObject<SaveDataModel.EditData>(json);
InstantiateEditData(editData);
}
}

void InstantiateEditData(SaveDataModel.EditData editData)
{
var notePresenter = EditNotesPresenter.Instance;

EditData.BPM.Value = editData.BPM;
EditData.MaxBlock.Value = editData.maxBlock;
EditData.OffsetSamples.Value = editData.offset;

foreach (var note in editData.notes)
{
if (note.type == 1)
{
notePresenter.AddNote(ConvertUtils.ToNote(note));
continue;
}

var longNoteObjects = new[] { note }.Concat(note.notes)
.Select(note_ =>
{
notePresenter.AddNote(ConvertUtils.ToNote(note_));
return EditData.Notes[ConvertUtils.ToNote(note_).position];
})
.ToList();

for (int i = 1; i < longNoteObjects.Count; i++)
{
longNoteObjects[i].note.prev = longNoteObjects[i - 1].note.position;
longNoteObjects[i - 1].note.next = longNoteObjects[i].note.position;
}

EditState.LongNoteTailPosition.Value = NotePosition.None;
EditDataSerializer.Deserialize(json);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Presenter/Save/SavePresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void Save()
var fileName = Path.GetFileNameWithoutExtension(EditData.Name.Value) + ".json";
var directoryPath = Settings.WorkSpaceDirectoryPath.Value + "/Notes/";
var filePath = directoryPath + fileName;
var json = EditData.SerializeEditData();
var json = EditDataSerializer.Serialize();

if (!Directory.Exists(directoryPath))
{
Expand Down
14 changes: 6 additions & 8 deletions Assets/Scripts/Presenter/Settings/SettingsWindowPresenter.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LitJson;
using NoteEditor.Model.JSON;
using NoteEditor.Model;
using NoteEditor.Model;
using NoteEditor.Utility;
using System.IO;
using System.Linq;
using UniRx;
Expand All @@ -19,7 +18,7 @@ public class SettingsWindowPresenter : MonoBehaviour
static string fileName = "settings.json";
static string filePath = directoryPath + fileName;

SettingsDataModel LoadSettings()
string LoadSettingsJson()
{
if (!Directory.Exists(directoryPath))
{
Expand All @@ -32,18 +31,17 @@ SettingsDataModel LoadSettings()
File.WriteAllText(filePath, defaultSettings.text, System.Text.Encoding.UTF8);
}

var json = File.ReadAllText(filePath, System.Text.Encoding.UTF8);
return JsonMapper.ToObject<SettingsDataModel>(json);
return File.ReadAllText(filePath, System.Text.Encoding.UTF8);
}

void SaveSettings()
{
File.WriteAllText(filePath, Settings.SerializeSettings(), System.Text.Encoding.UTF8);
File.WriteAllText(filePath, SettingsSerializer.Serialize(), System.Text.Encoding.UTF8);
}

void Awake()
{
Settings.Apply(LoadSettings());
SettingsSerializer.Deserialize(LoadSettingsJson());

EditData.MaxBlock.Do(_ => Enumerable.Range(0, itemContentTransform.childCount)
.Select(i => itemContentTransform.GetChild(i))
Expand Down
103 changes: 103 additions & 0 deletions Assets/Scripts/Utility/EditDataSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using LitJson;
using NoteEditor.Model;
using NoteEditor.Model.JSON;
using NoteEditor.Notes;
using NoteEditor.Presenter;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace NoteEditor.Utility
{
public class EditDataSerializer
{
public static string Serialize()
{
var data = new SaveDataModel.EditData();
data.BPM = EditData.BPM.Value;
data.maxBlock = EditData.MaxBlock.Value;
data.offset = EditData.OffsetSamples.Value;
data.name = Path.GetFileNameWithoutExtension(EditData.Name.Value);

var sortedNoteObjects = EditData.Notes.Values
.Where(note => !(note.note.type == NoteTypes.Long && EditData.Notes.ContainsKey(note.note.prev)))
.OrderBy(note => note.note.position.ToSamples(Audio.Source.clip.frequency, EditData.BPM.Value));

data.notes = new List<SaveDataModel.Note>();

foreach (var noteObject in sortedNoteObjects)
{
if (noteObject.note.type == NoteTypes.Single)
{
data.notes.Add(ToSaveData(noteObject));
}
else if (noteObject.note.type == NoteTypes.Long)
{
var current = noteObject;
var note = ToSaveData(noteObject);

while (EditData.Notes.ContainsKey(current.note.next))
{
var nextObj = EditData.Notes[current.note.next];
note.notes.Add(ToSaveData(nextObj));
current = nextObj;
}

data.notes.Add(note);
}
}

var jsonWriter = new JsonWriter();
jsonWriter.PrettyPrint = true;
jsonWriter.IndentValue = 4;
JsonMapper.ToJson(data, jsonWriter);
return jsonWriter.ToString();
}

public static void Deserialize(string json)
{
var editData = JsonMapper.ToObject<SaveDataModel.EditData>(json);
var notePresenter = EditNotesPresenter.Instance;

EditData.BPM.Value = editData.BPM;
EditData.MaxBlock.Value = editData.maxBlock;
EditData.OffsetSamples.Value = editData.offset;

foreach (var note in editData.notes)
{
if (note.type == 1)
{
notePresenter.AddNote(ConvertUtils.ToNote(note));
continue;
}

var longNoteObjects = new[] { note }.Concat(note.notes)
.Select(note_ =>
{
notePresenter.AddNote(ConvertUtils.ToNote(note_));
return EditData.Notes[ConvertUtils.ToNote(note_).position];
})
.ToList();

for (int i = 1; i < longNoteObjects.Count; i++)
{
longNoteObjects[i].note.prev = longNoteObjects[i - 1].note.position;
longNoteObjects[i - 1].note.next = longNoteObjects[i].note.position;
}

EditState.LongNoteTailPosition.Value = NotePosition.None;
}
}

static SaveDataModel.Note ToSaveData(NoteObject noteObject)
{
var note = new SaveDataModel.Note();
note.num = noteObject.note.position.num;
note.block = noteObject.note.position.block;
note.LPB = noteObject.note.position.LPB;
note.type = noteObject.note.type == NoteTypes.Long ? 2 : 1;
note.notes = new List<SaveDataModel.Note>();
return note;
}
}
}
12 changes: 12 additions & 0 deletions Assets/Scripts/Utility/EditDataSerializer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eb537d5

Please sign in to comment.