From 84b2c83fe65168afe36df054aa0d7b4550276d49 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 8 Dec 2017 20:37:42 +0000 Subject: [PATCH] [client] Finish erasing. It _works_ :D :D :D --- Nibriboard/ClientFiles/Chunk.js | 24 ++++++++- Nibriboard/ClientFiles/Pencil.js | 89 ++++++++++++++++++++++---------- 2 files changed, 84 insertions(+), 29 deletions(-) diff --git a/Nibriboard/ClientFiles/Chunk.js b/Nibriboard/ClientFiles/Chunk.js index 88e669a..6213b36 100644 --- a/Nibriboard/ClientFiles/Chunk.js +++ b/Nibriboard/ClientFiles/Chunk.js @@ -76,12 +76,32 @@ class Chunk getLineUnderPoint(point) { // Prefer lines that have been drawn later (i.e. on top) - for (let i = this.lines.length - 1; i > 0; i--) { + for (let i = this.lines.length - 1; i >= 0; i--) { // If our distance to the line is less than half the width (i.e. // the radius), then we must be inside it - if(point_line_distance_multi(point, this.lines[i].Points) <= this.lines[i].Width / 2) + let thisLineDistanceData = point_line_distance_multi(point, this.lines[i].Points); + if(thisLineDistanceData[1] <= this.lines[i].Width / 2) return this.lines[i]; } + return null; + } + + /** + * Removes the line with the specified unique id from this chunk. + * Warning: This is client-side only! The server won't get told about this, + * and changes will be overwritten by the next chunk update! + * @param {string} targetUniqueId The target unique id of the line to remove. + */ + removeByUniqueId(targetUniqueId) + { + // Search for the line with the target unique id + for(let i in this.lines) { + if(this.lines[i].UniqueId == targetUniqueId) { + // Found it! Remove it and return. + this.lines.splice(i, 1); + break; + } + } } update(dt) diff --git a/Nibriboard/ClientFiles/Pencil.js b/Nibriboard/ClientFiles/Pencil.js index d590b71..53569cf 100644 --- a/Nibriboard/ClientFiles/Pencil.js +++ b/Nibriboard/ClientFiles/Pencil.js @@ -98,6 +98,12 @@ class Pencil LineWidth: this.currentLineWidth }); break; + + case "eraser": + // Put the pencil down, but don't tell the server since we're + // not drawing a line + this.pencilDown = true; + break; } } @@ -107,13 +113,12 @@ class Pencil if(event.target != this.canvas) return; - // Don't draw anything if the left mouse button isn't down - if(/*!this.mouse.leftDown || (is this really needed?)*/ !this.pencilDown) - return; - switch(this.boardWindow.interface.currentTool) { case "brush": + if(!this.pencilDown) + return; // Don't draw anything if the left mouse button isn't down + // The server only supports ints atm, so we have to round here :-( // TODO: Lift this limit var nextPoint = new Vector( @@ -131,18 +136,39 @@ class Pencil break; case "eraser": - let locRef = this.boardWin.cursorSyncer.absCursorPosition; + if(!this.pencilDown) + return; // Only erase when the pencil is down + + let locRef = this.boardWindow.cursorSyncer.absCursorPosition; let hoverChunkRef = new ChunkReference( this.boardWindow.currentPlaneName, - Math.floor((this.boardWindow.viewport.x + this.mouse.position.x) / this.boardWindow.gridSize), - Math.floor((this.boardWindow.viewport.y + this.mouse.position.y) / this.boardWindow.gridSize) + Math.floor(locRef.x / this.boardWindow.gridSize), + Math.floor(locRef.y / this.boardWindow.gridSize) ); - let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunk); - if(hoverChunk == null) + let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunkRef); + if(hoverChunk == null) { + console.warn(`Can't erase on chunk ${hoverChunkRef} as it's currently not loaded.`); break; // If it's null, then we haven't received it yet from the server + } - let lineToErase = hoverChunk.getLineUnderPoint() + let lineToErase = hoverChunk.getLineUnderPoint(locRef); + if(lineToErase == null) { + console.debug(`No line found at abs ${locRef}.`); + break; // There's no line underneath the cursor atm + } + + console.info(`[pencil] Erasing line with unique id ${lineToErase.UniqueId} from ${hoverChunkRef}.`); + + // Ask the server politely to remove the line + this.rippleLink.send({ + Event: "LineRemove", + ContainingChunk: hoverChunkRef, + UniqueId: lineToErase.UniqueId + }); + + // Remove the line ourselves too to make the interface update faster + hoverChunk.removeByUniqueId(lineToErase.UniqueId); break; case "pan": @@ -161,27 +187,36 @@ class Pencil } handleMouseUp(event) { - // Don't do anything at all if the brush tool isn't selected - if(this.boardWindow.interface.currentTool !== "brush") - return; // Ignore it if the ctrl key is held down - see above if(this.boardWindow.keyboard.DownKeys.includes(17)) return; - this.sendUnsent(); - // Tell the server that the line is complete - this.rippleLink.send({ - Event: "LineComplete", - LineId: this.currentLineId - }); - - this.pencilDown = false; - - // Reset the current line segments - this.currentLineSegments = []; - this.currentSimplifiedLineSegments = []; - // Regenerate the line id - this.currentLineId = cuid(); + switch(this.boardWindow.interface.currentTool) + { + case "brush": + + this.sendUnsent(); + // Tell the server that the line is complete + this.rippleLink.send({ + Event: "LineComplete", + LineId: this.currentLineId + }); + + this.pencilDown = false; + + // Reset the current line segments + this.currentLineSegments = []; + this.currentSimplifiedLineSegments = []; + // Regenerate the line id + this.currentLineId = cuid(); + + break; + + case "eraser": + this.pencilDown = false; + + break; + } } /**