2017-03-28 19:25:54 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
import Mouse from './Utilities/Mouse';
|
|
|
|
import Vector from './Utilities/Vector';
|
|
|
|
|
|
|
|
var cuid = require("cuid");
|
2017-03-28 19:25:54 +00:00
|
|
|
|
|
|
|
class Pencil
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates a new Pencil class instance.
|
|
|
|
* @param {RippleLink} inRippleLink The connection to the nibri server.
|
|
|
|
* @return {Pencil} A new Pencil class instance.
|
|
|
|
*/
|
2017-04-14 19:22:01 +00:00
|
|
|
constructor(inRippleLink, inBoardWindow)
|
2017-03-28 19:25:54 +00:00
|
|
|
{
|
2017-04-15 12:13:07 +00:00
|
|
|
this.boardWindow = inBoardWindow;
|
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
// The time, in milliseconds, between pushes of the line to the server.
|
|
|
|
this.pushDelay = 200;
|
|
|
|
|
2017-04-15 12:13:07 +00:00
|
|
|
// The current line width
|
|
|
|
this.currentLineWidth = 3;
|
|
|
|
// The current line colour
|
|
|
|
this.currentColour = "black";
|
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
/**
|
|
|
|
* The ripple link connection to the server.
|
|
|
|
* @type {RippleLink}
|
|
|
|
*/
|
2017-03-28 19:25:54 +00:00
|
|
|
this.rippleLink = inRippleLink;
|
|
|
|
/**
|
|
|
|
* The mouse information.
|
|
|
|
* @type {Mouse}
|
|
|
|
*/
|
|
|
|
this.mouse = new Mouse();
|
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
// The id of the current line-in-progress.
|
|
|
|
this.currentLineId = cuid();
|
|
|
|
// Holds the (unsimplified) line segments before the pencil is lifted.
|
|
|
|
this.currentLineSegments = [];
|
|
|
|
// The segments of the (unsimplified) line that haven't yet been sent
|
|
|
|
// to the server.
|
|
|
|
this.unsentSegments = [];
|
2017-03-28 19:59:06 +00:00
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
// The time of the last push of the line to the server.
|
|
|
|
this.lastServerPush = 0;
|
2017-03-28 19:59:06 +00:00
|
|
|
|
2017-04-14 19:22:01 +00:00
|
|
|
document.addEventListener("mouseDown", this.handleMouseDown.bind(this));
|
2017-03-28 19:25:54 +00:00
|
|
|
document.addEventListener("mouseMove", this.handleMouseMove.bind(this));
|
2017-04-14 19:22:01 +00:00
|
|
|
document.addEventListener("mouseUp", this.handleMouseUp.bind(this));
|
|
|
|
}
|
|
|
|
|
2017-03-28 19:25:54 +00:00
|
|
|
handleMouseMove(event) {
|
2017-04-15 12:13:07 +00:00
|
|
|
// todo add zoom support here
|
|
|
|
var nextPoint = new Vector(
|
|
|
|
event.clientX + this.boardWindow.viewport.x,
|
|
|
|
event.clientY + this.boardWindow.viewport.y
|
|
|
|
);
|
2017-04-14 19:22:01 +00:00
|
|
|
this.unsentSegments.push(nextPoint);
|
|
|
|
this.currentLineSegments.push(nextPoint);
|
|
|
|
|
|
|
|
if(new Date() - this.lastServerPush > this.pushDelay)
|
|
|
|
sendUnsent();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleMouseUp(event) {
|
|
|
|
sendUnsent();
|
2017-04-15 12:13:07 +00:00
|
|
|
// Reset the current line segments
|
2017-04-14 19:22:01 +00:00
|
|
|
this.currentLineSegments = [];
|
|
|
|
// Regenerate the line id
|
|
|
|
this.currentLineId = cuid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the unsent segments of the line to the server and reset the line
|
|
|
|
* unsent segments buffer.
|
|
|
|
*/
|
|
|
|
sendUnsent() {
|
|
|
|
// It's time for another push of the line to the server
|
|
|
|
this.rippleLink.send({
|
|
|
|
Event: "LinePart",
|
|
|
|
Points: this.unsentSegments,
|
|
|
|
LineId: this.currentLineId
|
|
|
|
});
|
|
|
|
// Reset the unsent segments buffer
|
|
|
|
this.unsentSegments = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
render(canvas, context) {
|
2017-04-15 12:13:07 +00:00
|
|
|
if(this.currentLineSegments.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
context.save();
|
2017-03-28 19:25:54 +00:00
|
|
|
|
2017-04-15 12:13:07 +00:00
|
|
|
context.beginPath();
|
|
|
|
context.lineTo(this.currentLineSegments[0].x, this.currentLineSegments[0].y);
|
|
|
|
for(let point of this.currentLineSegments) {
|
|
|
|
context.lineTo(point.x, point.y);
|
|
|
|
}
|
|
|
|
context.lineWidth = this.currentColour;
|
|
|
|
context.strokeStyle = this.currentColour;
|
|
|
|
|
|
|
|
context.restore();
|
2017-03-28 19:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-15 12:13:07 +00:00
|
|
|
|
|
|
|
export default Pencil;
|