From ce7c4b22c490fbae8cb4d2c0f7f695ca159a524c Mon Sep 17 00:00:00 2001
From: Starbeamrainbowlabs <sbrl@starbeamrainbowlabs.com>
Date: Fri, 28 Apr 2017 12:42:34 +0100
Subject: [PATCH] Create brush indicator

---
 Nibriboard/ClientFiles/Interface.js | 77 ++++++++++++++++++++++++++++-
 Nibriboard/ClientFiles/Nibri.css    | 10 ++++
 Nibriboard/ClientFiles/Pencil.js    |  5 ++
 Nibriboard/ClientFiles/index.html   |  6 +--
 4 files changed, 93 insertions(+), 5 deletions(-)

diff --git a/Nibriboard/ClientFiles/Interface.js b/Nibriboard/ClientFiles/Interface.js
index d987d4c..431a918 100644
--- a/Nibriboard/ClientFiles/Interface.js
+++ b/Nibriboard/ClientFiles/Interface.js
@@ -10,13 +10,23 @@ class Interface extends EventEmitter
 		
 		this.sidebar = inSidebar;
 		
-		this.currentToolElement = this.sidebar.querySelector(".tools .tool-selector[data-selected]");
-		this.currentTool = "brush";
+		this.brushIndicator = this.sidebar.querySelector(".brush-indicator");
+		
 		this.setupToolSelectors();
+		this.setupColourSelectors();
+		this.setupBrushWidthControls();
+		
+		this.updateBrushIndicator();
 	}
 	
+	/**
+	 * Sets up the event listeners on the tool selectors.
+	 */
 	setupToolSelectors()
 	{
+		this.currentToolElement = this.sidebar.querySelector(".tools .tool-selector[data-selected]");
+		this.currentTool = "brush";
+		
 		var toolSelectors = this.sidebar.querySelectorAll(".tools .tool-selector");
 		for(let i = 0; i < toolSelectors.length; i++)
 		{
@@ -25,6 +35,32 @@ class Interface extends EventEmitter
 		}
 	}
 	
+	/**
+	 * Sets up the event listeners on the colour selectors.
+	 */
+	setupColourSelectors()
+	{
+		this.currentColourElement = this.sidebar.querySelector(".palette .palette-colour[data-selected]");
+		this.currentColour = this.currentColourElement.style.backgroundColor;
+		
+		var colours = this.sidebar.querySelectorAll(".palette .palette-colour");
+		for (let i = 0; i < colours.length; i++) {
+			colours[i].addEventListener("mouseup", this.handleSelectColour.bind(this));
+			colours[i].addEventListener("touchend", this.handleSelectColour.bind(this));
+		}
+	}
+	
+	/**
+	 * Sets up the brush width controls
+	 */
+	setupBrushWidthControls()
+	{
+		this.brushWidthElement = this.sidebar.querySelector(".brush-width-controls");
+		this.currentBrushWidth = parseInt(this.brushWidthElement.value);
+		
+		this.brushWidthElement.addEventListener("input", this.handleBrushWidthChange.bind(this));
+	}
+	
 	/**
 	 * Handles tool changes requested by the user.
 	 */
@@ -38,6 +74,43 @@ class Interface extends EventEmitter
 		this.emit("toolchange", { newTool: this.currentTool });
 	}
 	
+	/**
+	 * Handles colour selection changes requested by the user.
+	 */
+	handleSelectColour(event)
+	{
+		delete this.currentColourElement.datasest.selected;
+		this.currentColourElement = event.target;
+		this.currentColourElement.dataset.selected = "yes";
+		this.currentColour = this.currentColourElement.style.backgroundColor;
+		
+		this.updateBrushIndicator();
+		
+		console.info("Selected colour", this.currentColour);
+		this.emit("colourchange", { newColour: this.currentColour });
+	}
+	
+	/**
+	 * Handles brush widdth changes requested by the user
+	 */
+	handleBrushWidthChange(event)
+	{
+		this.currentBrushWidth = parseInt(event.target.value);
+		
+		this.updateBrushIndicator();
+		
+		this.emit("brushwidthchange", { newWidth: this.currentLineWidth });
+	}
+	
+	updateBrushIndicator()
+	{
+		// The brush indicator is zoom-agnostic (for the moment, at least)
+		this.brushIndicator.style.width = `${this.currentBrushWidth}px`;
+		this.brushIndicator.style.height = this.brushIndicator.style.width;
+		
+		this.brushIndicator.style.backgroundColor = this.currentColour;
+	}
+	
 	/**
 	 * Sets the displayed connection status.
 	 * @param {bool} newStatus The new connection status to display. true = connected - false = disconnected.
diff --git a/Nibriboard/ClientFiles/Nibri.css b/Nibriboard/ClientFiles/Nibri.css
index 93ec9cd..681e7fa 100644
--- a/Nibriboard/ClientFiles/Nibri.css
+++ b/Nibriboard/ClientFiles/Nibri.css
@@ -58,6 +58,11 @@ body
     border-top: 0.2em solid rgba(255, 255, 255, 0.3);
 }
 
+.brush-indicator
+{
+    border-radius: 50%;
+}
+
 .tool-selector
 {
     display: inline-block;
@@ -81,6 +86,11 @@ body
     cursor: pointer;
 }
 
+.palette-colour[data-selected]
+{
+    border: 0.2em dashed hsl(219, 87%, 54%);
+}
+
 .fps
 {
     top: 0.2em !important; right: 0.2em !important;
diff --git a/Nibriboard/ClientFiles/Pencil.js b/Nibriboard/ClientFiles/Pencil.js
index fade59e..3c75aa6 100644
--- a/Nibriboard/ClientFiles/Pencil.js
+++ b/Nibriboard/ClientFiles/Pencil.js
@@ -47,8 +47,13 @@ class Pencil
 		// The time of the last push of the line to the server.
 		this.lastServerPush = 0;
 		
+		// Event Listeners
 		canvas.addEventListener("mousemove", this.handleMouseMove.bind(this));
 		canvas.addEventListener("mouseup", this.handleMouseUp.bind(this));
+		
+		this.boardWindow.interface.on("colourchange", (function(event) {
+		    this.currentColour = event.newColour;
+		}).bind(this))
 	}
 	
 	handleMouseMove(event) {
diff --git a/Nibriboard/ClientFiles/index.html b/Nibriboard/ClientFiles/index.html
index ad5f241..48ba1b7 100644
--- a/Nibriboard/ClientFiles/index.html
+++ b/Nibriboard/ClientFiles/index.html
@@ -15,9 +15,9 @@
 			<hr />
 			
 			<section class="brush-settings">
-				<div class="current-brush"></div>
+				<div class="brush-indicator"></div>
 				
-				<input type="range" min="1" max="30" step="1" id="brush-width" />
+				<input type="range" min="1" max="30" step="0.1" class="brush-width-controls" />
 			</section>
 			
 			<section class="tools">
@@ -33,7 +33,7 @@
 				<span class="palette-colour" style="background-color: white;"></span>
 				<span class="palette-colour" style="background-color: black;"></span>
 				<span class="palette-colour" style="background-color: red;"></span>
-				<span class="palette-colour" style="background-color: orange;"></span>
+				<span class="palette-colour" style="background-color: orange;" data-selected></span>
 				<span class="palette-colour" style="background-color: darkgreen;"></span>
 				<span class="palette-colour" style="background-color: blue;"></span>
 				<span class="palette-colour" style="background-color: purple;"></span>
