2017-04-25 18:34:03 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
window.EventEmitter = require("event-emitter-es6");
|
|
|
|
|
2017-04-27 17:36:45 +00:00
|
|
|
class Interface extends EventEmitter
|
2017-04-25 18:34:03 +00:00
|
|
|
{
|
|
|
|
constructor(inSidebar)
|
|
|
|
{
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.sidebar = inSidebar;
|
|
|
|
|
2017-04-28 11:42:34 +00:00
|
|
|
this.brushIndicator = this.sidebar.querySelector(".brush-indicator");
|
|
|
|
|
2017-04-27 20:53:07 +00:00
|
|
|
this.setupToolSelectors();
|
2017-04-28 11:42:34 +00:00
|
|
|
this.setupColourSelectors();
|
|
|
|
this.setupBrushWidthControls();
|
|
|
|
|
|
|
|
this.updateBrushIndicator();
|
2017-04-25 18:34:03 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 11:42:34 +00:00
|
|
|
/**
|
|
|
|
* Sets up the event listeners on the tool selectors.
|
|
|
|
*/
|
2017-04-25 18:34:03 +00:00
|
|
|
setupToolSelectors()
|
|
|
|
{
|
2017-04-28 11:42:34 +00:00
|
|
|
this.currentToolElement = this.sidebar.querySelector(".tools .tool-selector[data-selected]");
|
|
|
|
this.currentTool = "brush";
|
|
|
|
|
2017-04-25 18:34:03 +00:00
|
|
|
var toolSelectors = this.sidebar.querySelectorAll(".tools .tool-selector");
|
|
|
|
for(let i = 0; i < toolSelectors.length; i++)
|
|
|
|
{
|
|
|
|
toolSelectors[i].addEventListener("mouseup", this.handleSelectTool.bind(this));
|
|
|
|
toolSelectors[i].addEventListener("touchend", this.handleSelectTool.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:42:34 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:34:03 +00:00
|
|
|
/**
|
|
|
|
* Handles tool changes requested by the user.
|
|
|
|
*/
|
|
|
|
handleSelectTool(event)
|
|
|
|
{
|
|
|
|
delete this.currentToolElement.dataset.selected;
|
|
|
|
this.currentToolElement = event.target;
|
|
|
|
this.currentToolElement.dataset.selected = "yes";
|
|
|
|
this.currentTool = this.currentToolElement.dataset.toolName;
|
2017-04-27 20:53:07 +00:00
|
|
|
console.info("Selected tool", this.currentTool);
|
2017-04-25 18:34:03 +00:00
|
|
|
this.emit("toolchange", { newTool: this.currentTool });
|
|
|
|
}
|
|
|
|
|
2017-04-28 11:42:34 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:34:03 +00:00
|
|
|
/**
|
|
|
|
* Sets the displayed connection status.
|
|
|
|
* @param {bool} newStatus The new connection status to display. true = connected - false = disconnected.
|
|
|
|
*/
|
|
|
|
setConnectedStatus(newStatus)
|
|
|
|
{
|
|
|
|
if(newStatus)
|
|
|
|
this.sidebar.querySelector(".connection-indicator").dataset.connected = "yes";
|
|
|
|
else
|
|
|
|
delete this.sidebar.querySelector(".connection-indicator").dataset.connected;
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:36:45 +00:00
|
|
|
/**
|
|
|
|
* Fetches our colour from the ui.
|
|
|
|
*/
|
|
|
|
get OurColour()
|
|
|
|
{
|
|
|
|
return this.sidebar.querySelector(".name").style.borderTopColor;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Sets the colour displayed just above the name
|
|
|
|
* @param {HTMLColour} newColour The new colour to display jsut above the
|
|
|
|
* name in the sidebar.
|
|
|
|
*/
|
|
|
|
set OurColour(newColour)
|
|
|
|
{
|
|
|
|
this.sidebar.querySelector(".name").style.borderTopColor = newColour;
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:34:03 +00:00
|
|
|
/**
|
|
|
|
* Updates the username displayed.
|
|
|
|
* @param {string} newName The new name to display inthe sidebar.
|
|
|
|
*/
|
|
|
|
updateDisplayedName(newName)
|
|
|
|
{
|
|
|
|
this.sidebar.querySelector(".name").innerHTML = newName;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:36:45 +00:00
|
|
|
export default Interface;
|