mirror of
https://github.com/sbrl/Nibriboard.git
synced 2018-01-10 21:33:49 +00:00
[client] Finish erasing. It _works_ :D :D :D
This commit is contained in:
parent
d5f6407cf5
commit
84b2c83fe6
2 changed files with 84 additions and 29 deletions
|
@ -76,12 +76,32 @@ class Chunk
|
||||||
getLineUnderPoint(point)
|
getLineUnderPoint(point)
|
||||||
{
|
{
|
||||||
// Prefer lines that have been drawn later (i.e. on top)
|
// 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.
|
// If our distance to the line is less than half the width (i.e.
|
||||||
// the radius), then we must be inside it
|
// 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 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)
|
update(dt)
|
||||||
|
|
|
@ -98,6 +98,12 @@ class Pencil
|
||||||
LineWidth: this.currentLineWidth
|
LineWidth: this.currentLineWidth
|
||||||
});
|
});
|
||||||
break;
|
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)
|
if(event.target != this.canvas)
|
||||||
return;
|
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)
|
switch(this.boardWindow.interface.currentTool)
|
||||||
{
|
{
|
||||||
case "brush":
|
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 :-(
|
// The server only supports ints atm, so we have to round here :-(
|
||||||
// TODO: Lift this limit
|
// TODO: Lift this limit
|
||||||
var nextPoint = new Vector(
|
var nextPoint = new Vector(
|
||||||
|
@ -131,18 +136,39 @@ class Pencil
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "eraser":
|
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(
|
let hoverChunkRef = new ChunkReference(
|
||||||
this.boardWindow.currentPlaneName,
|
this.boardWindow.currentPlaneName,
|
||||||
Math.floor((this.boardWindow.viewport.x + this.mouse.position.x) / this.boardWindow.gridSize),
|
Math.floor(locRef.x / this.boardWindow.gridSize),
|
||||||
Math.floor((this.boardWindow.viewport.y + this.mouse.position.y) / this.boardWindow.gridSize)
|
Math.floor(locRef.y / this.boardWindow.gridSize)
|
||||||
);
|
);
|
||||||
|
|
||||||
let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunk);
|
let hoverChunk = this.boardWindow.chunkCache.fetchChunk(hoverChunkRef);
|
||||||
if(hoverChunk == null)
|
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
|
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;
|
break;
|
||||||
|
|
||||||
case "pan":
|
case "pan":
|
||||||
|
@ -161,27 +187,36 @@ class Pencil
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMouseUp(event) {
|
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
|
// Ignore it if the ctrl key is held down - see above
|
||||||
if(this.boardWindow.keyboard.DownKeys.includes(17))
|
if(this.boardWindow.keyboard.DownKeys.includes(17))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
this.sendUnsent();
|
switch(this.boardWindow.interface.currentTool)
|
||||||
// Tell the server that the line is complete
|
{
|
||||||
this.rippleLink.send({
|
case "brush":
|
||||||
Event: "LineComplete",
|
|
||||||
LineId: this.currentLineId
|
|
||||||
});
|
|
||||||
|
|
||||||
this.pencilDown = false;
|
this.sendUnsent();
|
||||||
|
// Tell the server that the line is complete
|
||||||
|
this.rippleLink.send({
|
||||||
|
Event: "LineComplete",
|
||||||
|
LineId: this.currentLineId
|
||||||
|
});
|
||||||
|
|
||||||
// Reset the current line segments
|
this.pencilDown = false;
|
||||||
this.currentLineSegments = [];
|
|
||||||
this.currentSimplifiedLineSegments = [];
|
// Reset the current line segments
|
||||||
// Regenerate the line id
|
this.currentLineSegments = [];
|
||||||
this.currentLineId = cuid();
|
this.currentSimplifiedLineSegments = [];
|
||||||
|
// Regenerate the line id
|
||||||
|
this.currentLineId = cuid();
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "eraser":
|
||||||
|
this.pencilDown = false;
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue