From 8cdb9591abce86d0fc18267c074af2a3dd61a2a7 Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Thu, 30 Nov 2017 23:03:55 +0000
Subject: [PATCH] Refactor to make it nicer

---
 MusicBoxConverter/MusicBoxScoreGenerator.cs | 52 ++++++++++++++-------
 MusicBoxConverter/SvgWriter.cs              |  7 +--
 MusicBoxConverter/Utilities/Vector2.cs      |  6 +--
 3 files changed, 43 insertions(+), 22 deletions(-)

diff --git a/MusicBoxConverter/MusicBoxScoreGenerator.cs b/MusicBoxConverter/MusicBoxScoreGenerator.cs
index a75c1b9..276d5e1 100644
--- a/MusicBoxConverter/MusicBoxScoreGenerator.cs
+++ b/MusicBoxConverter/MusicBoxScoreGenerator.cs
@@ -1,15 +1,21 @@
 ﻿using System;
+using System.Collections.Generic;
 using System.Linq;
-using System.Xml;
+
 using Melanchall.DryWetMidi.Smf;
 using Melanchall.DryWetMidi.Smf.Interaction;
 
+using SBRL.Utilities;
+
 namespace MusicBoxConverter
 {
 	public class MusicBoxScoreGenerator
 	{
-		public float horizontalScaleFactor = 0.1f;
-		public float verticalScaleFactor = 4f;
+		public Vector2 offset { get; set; } = new Vector2(10, 10);
+		public Vector2 scaleFactor { get; set; } = new Vector2(0.1f, 4f);
+
+		public int holeSize { get; set; } = 5;
+
 
 		MidiFile midiFile;
 		
@@ -22,24 +28,38 @@ namespace MusicBoxConverter
 		{
 			SvgWriter svg = new SvgWriter(destinationFilename);
 
-			foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())
+
+			foreach(Note note in AllNotes())
 			{
-				using(NotesManager notesManager = new NotesManager(chunk.Events))
-				{
-					foreach(Note note in notesManager.Notes)
-					{
-						Console.WriteLine("[{0}] {1}/{2}", note.Time, note.NoteName, note.NoteNumber);
-						svg.WriteCircle(
-							(int)(note.Time * horizontalScaleFactor),
-							(int)(note.NoteNumber * verticalScaleFactor),
-							5
-						);
-					}
-				}
+				Console.WriteLine("[{0}] {1}/{2}", note.Time, note.NoteName, note.NoteNumber);
+				svg.WriteCircle(
+					new Vector2(
+						offset.X + note.Time * scaleFactor.X,
+						offset.Y + note.NoteNumber * scaleFactor.Y
+					),
+					holeSize // radius
+				);
 			}
 
 			svg.Complete();
 		}
 
+		public IEnumerable<Note> AllNotes()
+		{
+			foreach(TrackChunk chunk in midiFile.Chunks.OfType<TrackChunk>())
+			{
+				using(NotesManager notesManager = new NotesManager(chunk.Events))
+				{
+					foreach(Note note in notesManager.Notes)
+						yield return note;
+				}
+			}
+		}
+
+		public int TrackLength()
+		{
+			//notesManager.Notes.OfType<TrackChunk>().Select((TrackChunk arg) => arg.Events.O;
+			return 1;
+		}
 	}
 }
diff --git a/MusicBoxConverter/SvgWriter.cs b/MusicBoxConverter/SvgWriter.cs
index 283cfdd..a7caf00 100644
--- a/MusicBoxConverter/SvgWriter.cs
+++ b/MusicBoxConverter/SvgWriter.cs
@@ -1,6 +1,7 @@
 ﻿using System;
 using System.IO;
 using System.Xml;
+using SBRL.Utilities;
 
 namespace MusicBoxConverter
 {
@@ -38,11 +39,11 @@ namespace MusicBoxConverter
 			xml.WriteEndElement();
 		}
 
-		public void WriteCircle(int cx, int cy, int radius, string fillStyle = "blue")
+		public void WriteCircle(Vector2 centre, int radius, string fillStyle = "blue")
 		{
 			xml.WriteStartElement("circle");
-			xml.WriteAttributeString("cx", cx.ToString());
-			xml.WriteAttributeString("cy", cy.ToString());
+			xml.WriteAttributeString("cx", centre.X.ToString());
+			xml.WriteAttributeString("cy", centre.Y.ToString());
 			xml.WriteAttributeString("r", radius.ToString());
 			xml.WriteAttributeString("fill", fillStyle);
 			xml.WriteEndElement();
diff --git a/MusicBoxConverter/Utilities/Vector2.cs b/MusicBoxConverter/Utilities/Vector2.cs
index ba18f68..4ccec14 100644
--- a/MusicBoxConverter/Utilities/Vector2.cs
+++ b/MusicBoxConverter/Utilities/Vector2.cs
@@ -21,14 +21,14 @@ namespace SBRL.Utilities
 		/// <summary>
 		/// The X coordinate.
 		/// </summary>
-		public int X { get; set; }
+		public float X { get; set; }
 		/// <summary>
 		/// The Y coordinate.
 		/// </summary>
-		public int Y { get; set; }
+		public float Y { get; set; }
 
 
-		public Vector2(int x, int y)
+		public Vector2(float x, float y)
 		{
 			X = x;
 			Y = y;
