asciibird/src/views/Editor.vue

1290 lines
35 KiB
Vue
Raw Normal View History

2020-12-19 01:27:50 +00:00
<template>
<div>
<div
id="canvas-area"
@mouseleave="isMouseOnCanvas = false"
@mouseenter="isMouseOnCanvas = true"
>
2021-03-20 03:45:10 +00:00
<vue-draggable-resizable
2021-05-01 02:17:49 +00:00
style="z-index: 5"
2021-04-24 00:20:11 +00:00
:grid="[currentAscii.blockWidth, currentAscii.blockHeight]"
:w="canvas.width"
:h="canvas.height"
:draggable="$store.getters.currentTool === 0"
2021-04-03 03:13:03 +00:00
@resizestop="onCanvasResize"
@dragstop="onCavasDragStop"
2021-04-03 03:13:03 +00:00
:x="currentAscii.x"
:y="currentAscii.y"
2021-03-20 03:45:10 +00:00
>
2021-04-03 03:13:03 +00:00
<canvas
2021-03-31 23:29:55 +00:00
ref="canvastools"
id="canvastools"
2021-05-01 02:17:49 +00:00
class="canvastools"
2021-03-31 23:29:55 +00:00
:width="canvas.width"
:height="canvas.height"
@mousemove="canvasMouseMove"
@mousedown="canvasMouseDown"
@mouseup="canvasMouseUp"
2021-04-03 03:13:03 +00:00
></canvas>
2021-03-31 23:29:55 +00:00
2021-03-20 03:45:10 +00:00
<canvas
ref="canvas"
id="canvas"
class="canvas"
:width="canvas.width"
:height="canvas.height"
2021-03-27 02:07:33 +00:00
@mousemove="canvasMouseMove"
@mousedown="canvasMouseDown"
@mouseup="canvasMouseUp"
2021-03-20 03:45:10 +00:00
></canvas>
</vue-draggable-resizable>
</div>
2020-12-19 01:27:50 +00:00
</div>
</template>
2021-01-02 01:52:44 +00:00
<style>
body {
background: #eee;
}
2021-05-01 02:17:49 +00:00
.canvastools {
2021-04-03 03:13:03 +00:00
position: absolute;
z-index: 100;
opacity: 0.5;
2021-05-29 01:09:09 +00:00
cursor: crosshair;
2021-04-03 03:13:03 +00:00
}
.canvas {
position: absolute;
background: rgba(0, 0, 0, 0.8);
border: lightgrey 1px solid;
z-index: 0;
}
2021-01-02 01:52:44 +00:00
</style>
2020-12-19 01:27:50 +00:00
<script>
import { emptyBlock, toolbarIcons, mircColours99 } from "./../ascii.js";
2020-12-19 01:27:50 +00:00
export default {
2021-04-17 00:12:02 +00:00
name: "Editor",
2021-01-02 01:52:44 +00:00
mounted() {
if (this.currentAsciiBlocks) {
2021-04-17 00:12:02 +00:00
this.ctx = this.$refs.canvas.getContext("2d");
2021-05-01 02:17:49 +00:00
this.toolCtx = this.$refs.canvastools.getContext("2d");
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
this.canvas.width =
2021-08-03 04:59:12 +00:00
this.currentAscii.width * this.currentAscii.blockWidth;
2021-04-17 00:12:02 +00:00
this.canvas.height =
2021-08-03 04:59:12 +00:00
this.currentAscii.height * this.currentAscii.blockHeight;
2021-04-03 03:13:03 +00:00
2021-03-27 02:07:33 +00:00
this.delayRedrawCanvas();
2021-04-17 00:12:02 +00:00
this.$store.commit("changeTool", 0);
2021-04-03 03:13:03 +00:00
const _this = this;
this._keyListener = function (e) {
2021-08-03 04:59:12 +00:00
e.preventDefault();
if (this.isTextEditing) {
2021-05-15 01:52:20 +00:00
_this.canvasKeyDown(e.key);
return;
2021-05-15 01:52:20 +00:00
}
2021-08-03 04:59:12 +00:00
let ctrlKey = e.ctrlKey || e.metaKey;
2021-05-15 01:52:20 +00:00
// Ctrl Z here
// skg - thanks for mac key suggestion, bro
if (e.key === "z" && ctrlKey) {
this.undo();
2021-05-15 01:52:20 +00:00
}
2021-05-15 01:52:20 +00:00
// Ctrl Y here
if (e.key === "y" && ctrlKey) {
2021-06-29 01:49:39 +00:00
// Fk it works :\
this.redo();
2021-05-15 01:52:20 +00:00
}
// Ctrl C - copy blocks
if (e.key === "c" && ctrlKey) {
2021-08-03 04:59:12 +00:00
if (this.selectedBlocks.length) {
this.$store.commit("selectBlocks", this.selectedBlocks);
this.selectedBlocks = [];
}
}
// Ctrl V - paste blocks
if (e.key === "v" && ctrlKey) {
2021-08-03 04:59:12 +00:00
if (this.haveSelectBlocks) {
this.$store.commit("brushBlocks", this.selectBlocks);
this.$store.commit("changeTool", 4);
}
}
if (e.key === "d" && ctrlKey) {
2021-08-03 04:59:12 +00:00
this.$store.commit("toggleDebugPanel", !this.debugPanelState.visible);
}
2021-04-03 03:13:03 +00:00
};
2021-04-17 00:12:02 +00:00
document.addEventListener("keydown", this._keyListener.bind(this));
2021-03-13 05:27:07 +00:00
}
},
data: () => ({
2021-01-02 01:52:44 +00:00
ctx: null,
2021-04-03 03:13:03 +00:00
toolCtx: null,
2021-01-09 01:57:48 +00:00
canvas: {
width: 512,
height: 512,
2021-01-09 01:57:48 +00:00
},
x: 0, // Ascii X and Y
y: 0, // Ascii X and Y
redraw: true, // Used to limit canvas redraw
2021-03-27 04:16:15 +00:00
canTool: false,
2021-04-03 03:13:03 +00:00
textEditing: {
startX: null,
startY: null,
},
selecting: {
startX: null,
startY: null,
endX: null,
endY: null,
canSelect: false,
},
isMouseOnCanvas: false,
2021-08-03 04:59:12 +00:00
selectedBlocks: [],
}),
computed: {
2021-04-03 03:13:03 +00:00
currentAscii() {
2021-03-13 05:27:07 +00:00
return this.$store.getters.currentAscii;
2021-02-06 01:57:35 +00:00
},
currentAsciiBlocks() {
return this.$store.getters.currentAsciiBlocks;
},
2021-04-03 03:13:03 +00:00
currentTool() {
return toolbarIcons[this.$store.getters.currentTool];
2021-04-03 03:13:03 +00:00
},
mircColours() {
return this.$store.getters.mircColours;
2021-05-15 01:52:20 +00:00
},
2021-05-08 01:51:30 +00:00
canFg() {
return this.$store.getters.isTargettingFg;
2021-05-08 01:51:30 +00:00
},
canBg() {
return this.$store.getters.isTargettingBg;
2021-05-08 01:51:30 +00:00
},
canText() {
return this.$store.getters.isTargettingChar;
2021-05-08 01:51:30 +00:00
},
currentFg() {
return this.$store.getters.currentFg;
},
currentBg() {
return this.$store.getters.currentBg;
},
currentChar() {
return this.$store.getters.getChar;
},
isTextEditing() {
2021-08-03 04:59:12 +00:00
return this.currentTool.name === "text";
2021-04-03 03:13:03 +00:00
},
isSelecting() {
2021-08-03 04:59:12 +00:00
return this.currentTool.name === "select";
2021-04-03 03:13:03 +00:00
},
isSelected() {
return (
this.selecting.startX &&
this.selecting.startY &&
this.selecting.endX &&
this.selecting.endY
);
},
2021-06-29 01:49:39 +00:00
brushBlocks() {
return this.$store.getters.brushBlocks;
},
canvasX() {
return this.x * this.currentAscii.blockWidth;
},
canvasY() {
return this.y * this.currentAscii.blockHeight;
},
2021-07-31 02:12:11 +00:00
toolbarState() {
return this.$store.getters.toolbarState;
2021-07-31 02:12:11 +00:00
},
mirrorX() {
return this.toolbarState.mirrorX;
},
mirrorY() {
return this.toolbarState.mirrorY;
},
debugPanelState() {
2021-08-03 04:59:12 +00:00
return this.$store.getters.debugPanel;
},
brushBlocks() {
return this.$store.getters.brushBlocks;
},
selectBlocks() {
return this.$store.getters.selectBlocks;
},
options() {
return this.$store.getters.options;
},
2021-08-03 04:59:12 +00:00
haveSelectBlocks() {
return this.selectBlocks.length ? true : false;
},
mircColours() {
return mircColours99;
2021-08-03 04:59:12 +00:00
}
},
watch: {
2021-04-03 03:13:03 +00:00
currentAscii(val, old) {
if (val !== old) {
this.onCanvasResize(
100,
100,
this.currentAscii.width * this.currentAscii.blockWidth,
this.currentAscii.height * this.currentAscii.blockHeight
);
this.canvas.width =
this.currentAscii.width * this.currentAscii.blockWidth;
this.canvas.height =
this.currentAscii.height * this.currentAscii.blockHeight;
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
document.title = `asciibird - ${this.currentAscii.title}`;
}
2021-04-03 03:13:03 +00:00
},
currentTool() {
2021-08-03 04:59:12 +00:00
switch (this.currentTool.name) {
2021-04-17 00:12:02 +00:00
case "default":
// Reset default values for tools
2021-08-03 04:59:12 +00:00
this.textEditing = {
2021-04-03 03:13:03 +00:00
startX: null,
startY: null,
2021-08-03 04:59:12 +00:00
};
2021-04-03 03:13:03 +00:00
2021-08-03 04:59:12 +00:00
this.selecting = {
2021-04-03 03:13:03 +00:00
startX: null,
startY: null,
endX: null,
endY: null,
canSelect: false,
2021-08-03 04:59:12 +00:00
};
2021-04-03 03:13:03 +00:00
break;
}
2021-03-27 02:07:33 +00:00
},
isMouseOnCanvas() {
if (!this.isSelecting) {
this.clearToolCanvas();
this.canTool = false;
}
},
},
methods: {
2021-05-15 01:52:20 +00:00
undo() {
this.$store.commit("undoBlocks");
this.delayRedrawCanvas();
},
redo() {
this.$store.commit("redoBlocks");
this.delayRedrawCanvas();
2021-05-15 01:52:20 +00:00
},
redrawSelect() {
if (this.currentAsciiBlocks.length) {
2021-04-03 03:13:03 +00:00
this.clearToolCanvas();
this.toolCtx.fillStyle = this.mircColours[0];
2021-04-03 03:13:03 +00:00
2021-05-01 02:17:49 +00:00
this.toolCtx.fillRect(
this.selecting.startX,
this.selecting.startY,
this.selecting.endX - this.selecting.startX,
2021-07-31 02:12:11 +00:00
this.selecting.endY - this.selecting.startY
2021-04-17 00:12:02 +00:00
);
2021-05-01 02:17:49 +00:00
this.toolCtx.stroke();
2021-04-03 03:13:03 +00:00
}
},
2021-01-09 01:57:48 +00:00
redrawCanvas() {
// Clears the whole canvas, we can maybe get a better way to check how far
// we need to clear the canvas
this.ctx.clearRect(0, 0, 10000, 10000);
2021-01-23 02:50:32 +00:00
if (this.currentAsciiBlocks.length) {
2021-04-03 03:13:03 +00:00
const BLOCK_WIDTH = this.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.currentAscii.blockHeight;
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
// Position of the meta array
let x = 0;
let y = 0;
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
// Draws the actual rectangle
let canvasX = 0;
let canvasY = 0;
let curBlock = {};
// hack font for ascii shout outs 2 beenz
this.ctx.font = "13px Hack";
2021-04-03 03:13:03 +00:00
for (y = 0; y < this.currentAscii.height + 1; y++) {
2021-03-20 03:45:10 +00:00
canvasY = BLOCK_HEIGHT * y;
2021-01-09 01:57:48 +00:00
2021-04-03 03:13:03 +00:00
for (x = 0; x < this.currentAscii.width + 1; x++) {
if (this.currentAsciiBlocks[y] && this.currentAsciiBlocks[y][x]) {
2021-08-03 04:59:12 +00:00
curBlock = { ...this.currentAsciiBlocks[y][x] };
2021-03-27 02:07:33 +00:00
2021-03-20 03:45:10 +00:00
canvasX = BLOCK_WIDTH * x;
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
// Background block
if (curBlock.bg !== null) {
2021-08-03 04:59:12 +00:00
this.ctx.fillStyle = this.mircColours[curBlock.bg];
2021-03-20 03:45:10 +00:00
this.ctx.fillRect(canvasX, canvasY, BLOCK_WIDTH, BLOCK_HEIGHT);
}
2021-03-13 05:27:07 +00:00
2021-03-20 03:45:10 +00:00
if (curBlock.char) {
if (curBlock.fg !== null) {
2021-08-03 04:59:12 +00:00
this.ctx.fillStyle = this.mircColours[curBlock.fg];
2021-03-20 03:45:10 +00:00
} else {
2021-04-17 00:12:02 +00:00
this.ctx.fillStyle = "#000000";
2021-03-13 05:27:07 +00:00
}
2021-03-20 03:45:10 +00:00
this.ctx.fillText(
curBlock.char,
canvasX + 0.5,
2021-04-17 00:12:02 +00:00
canvasY + BLOCK_HEIGHT - 3
2021-03-20 03:45:10 +00:00
);
}
2021-01-23 02:50:32 +00:00
}
2021-01-30 02:33:11 +00:00
}
2021-01-09 01:57:48 +00:00
}
2021-03-20 03:45:10 +00:00
}
2021-01-09 01:57:48 +00:00
2021-03-20 03:45:10 +00:00
this.ctx.stroke();
2021-03-27 04:16:15 +00:00
},
onCanvasResize(left, top, width, height) {
const blocks = this.currentAsciiBlocks;
2021-04-03 03:13:03 +00:00
const oldWidth = blocks[0].length;
const oldHeight = blocks.length;
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
const canvasBlockHeight = Math.floor(
height / this.currentAscii.blockHeight
);
2021-04-03 03:13:03 +00:00
const canvasBlockWidth = Math.floor(width / this.currentAscii.blockWidth);
2021-05-15 01:57:44 +00:00
if (canvasBlockHeight > oldHeight || canvasBlockWidth > oldWidth) {
// console.log({ canvasBlockHeight, oldHeight });
2021-04-03 03:13:03 +00:00
for (let y = 0; y < canvasBlockHeight; y++) {
// New row
if (!blocks[y]) {
blocks[y] = [];
for (let x = 0; x < canvasBlockWidth; x++) {
2021-08-03 04:59:12 +00:00
blocks[y][x] = { ...emptyBlock };
2021-04-03 03:13:03 +00:00
}
} else {
// blocks[y]
// no new rows but new cols
for (let x = 0; x < canvasBlockWidth; x++) {
if (blocks[y] && !blocks[y][x]) {
2021-08-03 04:59:12 +00:00
blocks[y][x] = { ...emptyBlock };
2021-04-03 03:13:03 +00:00
}
}
}
}
}
if (canvasBlockWidth > oldWidth) {
// console.log({ canvasBlockWidth, oldWidth });
2021-04-03 03:13:03 +00:00
}
2021-03-27 04:16:15 +00:00
this.canvas.width = width;
this.canvas.height = height;
2021-04-17 00:12:02 +00:00
this.$store.commit("changeAsciiWidthHeight", {
2021-04-03 03:13:03 +00:00
width: canvasBlockWidth,
height: canvasBlockHeight,
});
2021-04-17 00:12:02 +00:00
this.$store.commit("updateAsciiBlocks", blocks);
2021-04-03 03:13:03 +00:00
// Restructure blocks code here
2021-03-27 04:16:15 +00:00
this.delayRedrawCanvas();
},
onCavasDragStop(x, y) {
2021-03-27 04:16:15 +00:00
// Update left and top in panel
2021-04-17 00:12:02 +00:00
this.$store.commit("changeAsciiCanvasState", { x, y });
2021-03-27 04:16:15 +00:00
},
2021-04-03 03:13:03 +00:00
canvasKeyDown(char) {
2021-08-03 04:59:12 +00:00
if (this.isTextEditing) {
2021-04-17 00:12:02 +00:00
if (
this.currentAsciiBlocks[this.textEditing.startY] &&
this.currentAsciiBlocks[this.textEditing.startY][
2021-04-17 00:12:02 +00:00
this.textEditing.startX
]
) {
2021-07-31 02:12:11 +00:00
var targetBlock =
this.currentAsciiBlocks[this.textEditing.startY][
this.textEditing.startX
];
2021-03-27 04:16:15 +00:00
2021-04-17 00:12:02 +00:00
switch (char) {
case "Backspace":
2021-05-01 02:17:49 +00:00
if (
this.currentAsciiBlocks[this.textEditing.startY][
2021-05-01 02:17:49 +00:00
this.textEditing.startX - 1
]
) {
2021-04-17 00:12:02 +00:00
this.textEditing.startX--;
targetBlock.char = null;
}
break;
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
default:
if (char.length === 1) {
2021-07-31 02:12:11 +00:00
if (this.canFg) {
targetBlock.fg = this.currentFg;
2021-04-17 00:12:02 +00:00
}
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
targetBlock.char = char;
2021-08-03 04:59:12 +00:00
if (this.mirrorX) {
targetBlock =
2021-07-31 02:12:11 +00:00
this.currentAsciiBlocks[this.textEditing.startY][
2021-08-03 04:59:12 +00:00
this.currentAscii.width - this.textEditing.startX
2021-07-31 02:12:11 +00:00
];
2021-08-03 04:59:12 +00:00
if (this.canFg) {
targetBlock.fg = this.currentFg;
2021-07-31 02:12:11 +00:00
}
2021-08-03 04:59:12 +00:00
targetBlock.char = char;
}
2021-07-31 02:12:11 +00:00
2021-08-03 04:59:12 +00:00
if (this.mirrorY) {
targetBlock =
this.currentAsciiBlocks[
this.currentAscii.height - this.textEditing.startY
][this.textEditing.startX];
2021-07-31 02:12:11 +00:00
2021-08-03 04:59:12 +00:00
if (this.canFg) {
targetBlock.fg = this.currentFg;
2021-07-31 02:12:11 +00:00
}
2021-08-03 04:59:12 +00:00
targetBlock.char = char;
}
2021-07-31 02:12:11 +00:00
2021-08-03 04:59:12 +00:00
if (this.mirrorY && this.mirrorX) {
targetBlock =
this.currentAsciiBlocks[
this.currentAscii.height - this.textEditing.startY
][this.currentAscii.width - this.textEditing.startX];
2021-07-31 02:12:11 +00:00
2021-08-03 04:59:12 +00:00
if (this.canFg) {
targetBlock.fg = this.currentFg;
2021-07-31 02:12:11 +00:00
}
2021-08-03 04:59:12 +00:00
targetBlock.char = char;
}
2021-04-17 00:12:02 +00:00
if (
this.currentAsciiBlocks[this.textEditing.startY][
2021-04-17 00:12:02 +00:00
this.textEditing.startX + 1
]
) {
this.textEditing.startX++;
} else {
this.textEditing.startX = 0;
if (this.textEditing.startY < this.currentAscii.height) {
this.textEditing.startY++;
}
}
}
break;
2021-04-03 03:13:03 +00:00
}
this.drawTextIndicator();
2021-04-03 03:13:03 +00:00
}
this.delayRedrawCanvas();
2021-07-31 02:12:11 +00:00
this.$store.commit("updateAsciiBlocks", this.currentAsciiBlocks);
2021-04-03 03:13:03 +00:00
}
},
2021-03-27 04:16:15 +00:00
// Mouse Up, Down and Move
canvasMouseUp() {
2021-08-03 06:33:06 +00:00
if (this.currentTool.name === "default") return;
2021-04-17 00:12:02 +00:00
switch (this.currentTool.name) {
case "brush":
2021-03-27 04:16:15 +00:00
this.canTool = false;
2021-05-15 01:52:20 +00:00
this.$store.commit("updateAsciiBlocks", this.currentAsciiBlocks);
2021-03-27 04:16:15 +00:00
break;
2021-04-17 00:12:02 +00:00
case "eraser":
2021-03-27 04:16:15 +00:00
this.canTool = false;
2021-05-15 01:52:20 +00:00
this.$store.commit("updateAsciiBlocks", this.currentAsciiBlocks);
2021-03-27 04:16:15 +00:00
break;
2021-04-17 00:12:02 +00:00
case "fill":
2021-03-27 04:16:15 +00:00
this.canTool = false;
this.$store.commit("updateAsciiBlocks", this.currentAsciiBlocks);
2021-03-27 04:16:15 +00:00
break;
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
case "select":
2021-04-03 03:13:03 +00:00
this.selecting.canSelect = false;
this.clearToolCanvas();
this.processSelect();
this.redrawSelect();
2021-04-03 03:13:03 +00:00
break;
2021-04-17 00:12:02 +00:00
case "text":
this.textEditing.startX = this.x;
this.textEditing.startY = this.y;
2021-04-03 03:13:03 +00:00
break;
2021-03-27 04:16:15 +00:00
}
2021-03-31 23:29:55 +00:00
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
},
canvasMouseDown() {
2021-08-03 06:33:06 +00:00
if (this.currentTool.name === "default") return;
2021-05-01 02:17:49 +00:00
this.toolCtx.clearRect(0, 0, 10000, 10000);
2021-04-03 03:13:03 +00:00
2021-04-02 04:43:11 +00:00
if (
this.currentAsciiBlocks[this.y] &&
this.currentAsciiBlocks[this.y][this.x] &&
2021-04-17 00:12:02 +00:00
this.currentTool
2021-04-02 04:43:11 +00:00
) {
const targetBlock = this.currentAsciiBlocks[this.y][this.x];
2021-04-02 04:43:11 +00:00
2021-04-17 00:12:02 +00:00
switch (this.currentTool.name) {
case "default":
break;
2021-04-17 00:12:02 +00:00
case "select":
2021-07-31 02:12:11 +00:00
this.selecting.startX = this.canvasX;
this.selecting.startY = this.canvasY;
this.selecting.canSelect = true;
2021-04-03 03:13:03 +00:00
break;
2021-04-17 00:12:02 +00:00
case "fill":
2021-04-02 04:43:11 +00:00
this.fill();
break;
2021-04-17 00:12:02 +00:00
case "brush":
this.canTool = true;
this.drawBrush();
break;
2021-04-17 00:12:02 +00:00
case "eraser":
this.canTool = true;
this.eraser();
break;
2021-04-17 00:12:02 +00:00
case "dropper":
2021-07-24 04:34:49 +00:00
if (this.canFg) {
2021-04-17 00:12:02 +00:00
this.$store.commit("changeColourFg", targetBlock.fg);
}
2021-03-27 04:16:15 +00:00
2021-07-24 04:34:49 +00:00
if (this.canBg) {
2021-04-17 00:12:02 +00:00
this.$store.commit("changeColourBg", targetBlock.bg);
}
2021-03-27 04:16:15 +00:00
2021-07-24 04:34:49 +00:00
if (this.canText) {
2021-04-17 00:12:02 +00:00
this.$store.commit("changeChar", targetBlock.char);
2021-03-29 08:21:23 +00:00
}
2021-04-17 00:12:02 +00:00
this.$store.commit("changeTool", 0);
break;
}
2021-03-27 04:16:15 +00:00
}
2021-01-09 01:57:48 +00:00
},
2021-03-27 02:07:33 +00:00
canvasMouseMove(e) {
2021-08-03 06:33:06 +00:00
if (this.currentTool.name === "default") return;
2021-03-13 03:30:58 +00:00
if (e.offsetX >= 0) {
this.x = e.offsetX;
}
2021-03-13 05:27:07 +00:00
2021-03-13 03:30:58 +00:00
if (e.offsetY >= 0) {
this.y = e.offsetY;
}
2021-05-29 01:09:09 +00:00
this.x = Math.floor(this.x / this.currentAscii.blockWidth);
this.y = Math.floor(this.y / this.currentAscii.blockHeight);
2021-04-17 00:12:02 +00:00
this.$emit("coordsupdate", { x: this.x, y: this.y });
2021-03-29 05:10:31 +00:00
if (
this.currentAsciiBlocks[this.y] &&
this.currentAsciiBlocks[this.y][this.x]
) {
2021-08-03 04:59:12 +00:00
switch (this.currentTool.name) {
2021-04-17 00:12:02 +00:00
case "brush":
if (this.isMouseOnCanvas) {
this.drawBrush();
}
break;
2021-03-27 02:07:33 +00:00
2021-04-17 00:12:02 +00:00
case "eraser":
if (this.isMouseOnCanvas) {
this.drawBrush(true);
2021-03-27 02:07:33 +00:00
}
this.eraser();
break;
2021-04-03 03:13:03 +00:00
2021-04-17 00:12:02 +00:00
case "select":
// this.drawIndicator();
2021-08-03 06:33:06 +00:00
2021-04-03 03:13:03 +00:00
if (this.selecting.canSelect) {
this.selecting.endX = this.canvasX;
this.selecting.endY = this.canvasY;
this.redrawSelect();
}
if (!this.isSelected) {
this.redrawSelect();
}
2021-04-03 03:13:03 +00:00
break;
case "text":
this.drawIndicator();
break;
case "dropper":
this.drawIndicator();
break;
case "fill":
this.drawIndicator();
break;
}
2021-03-27 02:07:33 +00:00
}
this.delayRedrawCanvas();
2021-03-13 03:30:58 +00:00
},
2021-04-03 03:13:03 +00:00
clearToolCanvas() {
2021-05-01 02:17:49 +00:00
if (this.toolCtx) {
this.toolCtx.clearRect(0, 0, 10000, 10000);
this.toolCtx.stroke();
2021-04-03 03:13:03 +00:00
}
},
2021-03-27 02:07:33 +00:00
delayRedrawCanvas() {
if (this.redraw) {
this.redraw = false;
2021-03-27 02:07:33 +00:00
setTimeout(() => {
this.redraw = true;
2021-03-27 02:07:33 +00:00
this.redrawCanvas();
2021-08-03 04:59:12 +00:00
}, this.options.canvasRedrawSpeed);
2021-03-27 02:07:33 +00:00
}
},
2021-04-03 03:13:03 +00:00
//
// TOOLS
//
processSelect() {
2021-08-03 04:59:12 +00:00
//
let x = 0;
let y = 0;
2021-08-03 04:59:12 +00:00
let curBlock = {};
this.selectedBlocks = [];
2021-08-03 04:59:12 +00:00
for (y = 0; y < this.currentAscii.height; y++) {
if (
y >=
Math.floor(this.selecting.startY / this.currentAscii.blockHeight) &&
y <= Math.floor(this.selecting.endY / this.currentAscii.blockHeight)
) {
if (!this.selectedBlocks[y]) {
this.selectedBlocks[y] = [];
}
for (x = 0; x < this.currentAscii.width; x++) {
if (
2021-08-03 04:59:12 +00:00
x >=
Math.floor(
2021-08-03 04:59:12 +00:00
this.selecting.startX / this.currentAscii.blockWidth
) &&
2021-08-03 04:59:12 +00:00
x <=
Math.floor(this.selecting.endX / this.currentAscii.blockWidth)
) {
2021-08-03 04:59:12 +00:00
if (this.currentAsciiBlocks[y] && this.currentAsciiBlocks[y][x]) {
curBlock = { ...this.currentAsciiBlocks[y][x] };
2021-08-03 04:59:12 +00:00
if (!this.selectedBlocks[y][x]) {
this.selectedBlocks[y][x] = { ...curBlock };
}
}
}
}
2021-08-03 04:59:12 +00:00
}
}
},
drawIndicator() {
this.clearToolCanvas();
2021-05-29 00:45:16 +00:00
let targetBlock = this.currentAsciiBlocks[this.y][this.x];
let indicatorColour = targetBlock.bg === 0 ? 1 : 0;
2021-05-29 00:45:16 +00:00
if (targetBlock.bg === 8) {
indicatorColour = 1;
}
this.toolCtx.fillStyle = this.mircColours[indicatorColour];
const BLOCK_WIDTH = this.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.currentAscii.blockHeight;
this.toolCtx.fillRect(
this.x * BLOCK_WIDTH,
this.y * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
2021-07-31 02:12:11 +00:00
if (this.isTextEditing) {
if (this.mirrorX) {
this.toolCtx.fillRect(
(this.currentAscii.width - this.x) * BLOCK_WIDTH,
this.y * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY) {
this.toolCtx.fillRect(
this.x * BLOCK_WIDTH,
2021-08-03 04:59:12 +00:00
(this.currentAscii.height - this.y) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY && this.mirrorX) {
this.toolCtx.fillRect(
(this.currentAscii.width - this.x) * BLOCK_WIDTH,
2021-08-03 04:59:12 +00:00
(this.currentAscii.height - this.y) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
}
this.toolCtx.stroke();
},
drawTextIndicator() {
this.clearToolCanvas();
2021-05-29 01:09:09 +00:00
let targetBlock =
this.currentAsciiBlocks[this.textEditing.startY][
this.textEditing.startX
];
2021-05-29 01:09:09 +00:00
let indicatorColour = targetBlock.bg === 0 ? 1 : 0;
2021-05-29 01:09:09 +00:00
if (targetBlock.bg === 8) {
indicatorColour = 1;
}
this.toolCtx.fillStyle = this.mircColours[indicatorColour];
const BLOCK_WIDTH = this.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.currentAscii.blockHeight;
2021-07-31 02:12:11 +00:00
this.toolCtx.fillRect(
this.textEditing.startX * BLOCK_WIDTH,
this.textEditing.startY * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
2021-07-31 02:12:11 +00:00
if (this.mirrorX) {
this.toolCtx.fillRect(
(this.currentAscii.width - this.textEditing.startX) * BLOCK_WIDTH,
this.textEditing.startY * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY) {
this.toolCtx.fillRect(
this.textEditing.startX * BLOCK_WIDTH,
2021-08-03 04:59:12 +00:00
(this.currentAscii.height - this.textEditing.startY) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY && this.mirrorX) {
this.toolCtx.fillRect(
(this.currentAscii.width - this.textEditing.startX) * BLOCK_WIDTH,
2021-08-03 04:59:12 +00:00
(this.currentAscii.height - this.textEditing.startY) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
this.toolCtx.stroke();
},
2021-07-31 02:12:11 +00:00
//
// drawBrush
// - draws brush
// - draws preview for all toolbar things that need it
// - also works with the copy / paste
drawBrush(plain = false) {
this.clearToolCanvas();
const BLOCK_WIDTH = this.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.currentAscii.blockHeight;
let targetBlock = this.currentAsciiBlocks[this.y][this.x];
2021-07-24 04:34:49 +00:00
let brushDiffX = 0;
let xLength = 0;
2021-07-31 02:12:11 +00:00
let asciiWidth = this.currentAscii.width;
let asciiHeight = this.currentAscii.height;
2021-07-24 04:34:49 +00:00
// If the first row isn't selected then we cannot get the width
// with the 0 index
for (let i = 0; i <= this.brushBlocks.length; i++) {
if (this.brushBlocks[i]) {
brushDiffX = Math.floor(this.brushBlocks[i].length / 2) * BLOCK_WIDTH;
xLength = this.brushBlocks[i].length;
break;
}
}
2021-05-08 01:51:30 +00:00
2021-07-24 04:34:49 +00:00
// We always have a Y array
let brushDiffY = Math.floor(this.brushBlocks.length / 2) * BLOCK_HEIGHT;
2021-05-29 00:28:30 +00:00
2021-06-29 01:49:39 +00:00
for (let y = 0; y < this.brushBlocks.length; y++) {
if (!this.brushBlocks[y]) {
continue;
}
2021-07-24 04:34:49 +00:00
for (let x = 0; x < xLength; x++) {
if (!this.brushBlocks[y][x]) {
continue;
}
2021-06-29 01:49:39 +00:00
let brushBlock = this.brushBlocks[y][x];
2021-05-08 01:51:30 +00:00
let brushX = this.x * BLOCK_WIDTH + x * BLOCK_WIDTH - brushDiffX;
let brushY = this.y * BLOCK_HEIGHT + y * BLOCK_HEIGHT - brushDiffY;
2021-07-31 02:12:11 +00:00
let arrayY = brushY / BLOCK_HEIGHT;
let arrayX = brushX / BLOCK_WIDTH;
2021-05-29 01:09:09 +00:00
if (
2021-07-31 02:12:11 +00:00
this.currentAsciiBlocks[arrayY] &&
this.currentAsciiBlocks[arrayY][arrayX]
) {
2021-07-31 02:12:11 +00:00
targetBlock = this.currentAsciiBlocks[arrayY][arrayX];
if (!plain) {
if (this.canBg) {
2021-07-31 02:12:11 +00:00
this.toolCtx.fillStyle =
brushBlock.bg !== null
? this.mircColours[brushBlock.bg]
2021-07-31 02:12:11 +00:00
: "#FFFFFF";
this.toolCtx.fillRect(
brushX,
brushY,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
2021-07-31 02:12:11 +00:00
if (this.mirrorX) {
this.toolCtx.fillRect(
(asciiWidth - arrayX) * BLOCK_WIDTH,
brushY,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY) {
this.toolCtx.fillRect(
brushX,
(asciiHeight - arrayY) * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY && this.mirrorX) {
this.toolCtx.fillRect(
(asciiWidth - arrayX) * BLOCK_WIDTH,
(asciiHeight - arrayY) * BLOCK_HEIGHT,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
2021-07-24 04:34:49 +00:00
if (this.canTool && brushBlock.bg !== null) {
2021-08-03 04:59:12 +00:00
targetBlock.bg = !this.haveSelectBlocks
? this.currentBg
2021-07-31 02:12:11 +00:00
: brushBlock.bg;
if (this.mirrorX) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[arrayY][asciiWidth - arrayX].bg =
!this.haveSelectBlocks
? this.currentBg
: brushBlock.bg;
2021-07-31 02:12:11 +00:00
}
if (this.mirrorY) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[asciiHeight - arrayY][arrayX].bg =
!this.haveSelectBlocks
? this.currentBg
: brushBlock.bg;
2021-07-31 02:12:11 +00:00
}
if (this.mirrorY && this.mirrorX) {
this.currentAsciiBlocks[asciiHeight - arrayY][
asciiWidth - arrayX
2021-08-03 04:59:12 +00:00
].bg = !this.haveSelectBlocks
? this.currentBg
2021-07-31 02:12:11 +00:00
: brushBlock.bg;
}
}
}
if (this.canFg) {
2021-07-31 02:12:11 +00:00
this.toolCtx.fillStyle =
brushBlock.fg !== null
? this.mircColours[brushBlock.fg]
2021-07-31 02:12:11 +00:00
: "#000000";
2021-07-24 04:34:49 +00:00
if (this.canTool && brushBlock.fg !== null) {
2021-08-03 04:59:12 +00:00
targetBlock.fg = !this.haveSelectBlocks
2021-07-31 02:12:11 +00:00
? this.currentFg
: brushBlock.fg;
if (this.mirrorX) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[arrayY][asciiWidth - arrayX].fg =
!this.haveSelectBlocks
? this.currentFg
: brushBlock.fg;
2021-07-31 02:12:11 +00:00
}
if (this.mirrorY) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[asciiHeight - arrayY][arrayX].fg =
!this.haveSelectBlocks
? this.currentFg
: brushBlock.fg;
2021-07-31 02:12:11 +00:00
}
if (this.mirrorY && this.mirrorX) {
this.currentAsciiBlocks[asciiHeight - arrayY][
asciiWidth - arrayX
2021-08-03 04:59:12 +00:00
].fg = !this.haveSelectBlocks
2021-07-31 02:12:11 +00:00
? this.currentFg
: brushBlock.fg;
}
}
}
if (this.canText && brushBlock.char !== null) {
2021-08-03 04:59:12 +00:00
this.toolCtx.fillStyle = this.mircColours[brushBlock.fg];
2021-05-29 00:28:30 +00:00
this.toolCtx.fillText(
brushBlock.char,
brushX - 1,
2021-08-03 04:59:12 +00:00
brushY + BLOCK_HEIGHT - 2
);
2021-08-03 04:59:12 +00:00
if (this.mirrorX) {
this.toolCtx.fillText(
brushBlock.char,
(asciiWidth - arrayX) * BLOCK_WIDTH,
brushY + BLOCK_HEIGHT - 2
);
}
if (this.mirrorY) {
this.toolCtx.fillText(
brushBlock.char,
brushX - 1,
(asciiHeight - arrayY) * BLOCK_HEIGHT + 10
);
}
if (this.mirrorY && this.mirrorX) {
this.toolCtx.fillText(
brushBlock.char,
(asciiWidth - arrayX) * BLOCK_WIDTH,
(asciiHeight - arrayY) * BLOCK_HEIGHT + 10
);
}
2021-07-24 04:34:49 +00:00
if (this.canTool && brushBlock.char !== null) {
2021-08-03 04:59:12 +00:00
targetBlock.char = !this.haveSelectBlocks
? this.currentChar
2021-07-31 02:12:11 +00:00
: brushBlock.char;
if (this.mirrorX) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[arrayY][asciiWidth - arrayX].char =
!this.haveSelectBlocks
? this.currentChar
: brushBlock.char;
2021-07-31 02:12:11 +00:00
}
2021-07-31 02:12:11 +00:00
if (this.mirrorY) {
2021-08-03 04:59:12 +00:00
this.currentAsciiBlocks[asciiHeight - arrayY][arrayX].char =
!this.haveSelectBlocks
? this.currentChar
: brushBlock.char;
2021-07-31 02:12:11 +00:00
}
if (this.mirrorY && this.mirrorX) {
this.currentAsciiBlocks[asciiHeight - arrayY][
asciiWidth - arrayX
2021-08-03 04:59:12 +00:00
].char = !this.haveSelectBlocks
? this.currentChar
2021-07-31 02:12:11 +00:00
: brushBlock.char;
}
}
}
} else {
2021-08-03 04:59:12 +00:00
let indicatorColour = targetBlock.bg === 0 ? 1 : 0;
if (targetBlock.bg === 8) {
indicatorColour = 1;
}
this.toolCtx.fillStyle = this.mircColours[indicatorColour];
this.toolCtx.fillRect(brushX, brushY, BLOCK_WIDTH, BLOCK_HEIGHT);
2021-07-31 02:12:11 +00:00
if (this.mirrorX) {
this.toolCtx.fillRect(
2021-08-03 04:59:12 +00:00
(asciiWidth - arrayX) * BLOCK_WIDTH,
2021-07-31 02:12:11 +00:00
brushY,
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY) {
this.toolCtx.fillRect(
brushX,
2021-08-03 04:59:12 +00:00
(asciiHeight - arrayY) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
if (this.mirrorY && this.mirrorX) {
this.toolCtx.fillRect(
2021-08-03 04:59:12 +00:00
(asciiWidth - arrayX) * BLOCK_WIDTH,
(asciiHeight - arrayY) * BLOCK_HEIGHT,
2021-07-31 02:12:11 +00:00
BLOCK_WIDTH,
BLOCK_HEIGHT
);
}
}
}
}
}
this.toolCtx.stroke();
},
eraser() {
if (this.canTool) {
const BLOCK_WIDTH = this.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.currentAscii.blockHeight;
let targetBlock = this.currentAsciiBlocks[this.y][this.x];
let brushDiffX =
2021-08-03 04:59:12 +00:00
Math.floor(this.brushBlocks[0].length / 2) * BLOCK_WIDTH;
let brushDiffY = Math.floor(this.brushBlocks.length / 2) * BLOCK_HEIGHT;
2021-05-29 01:09:09 +00:00
2021-08-03 04:59:12 +00:00
for (let y = 0; y < this.brushBlocks.length; y++) {
for (let x = 0; x < this.brushBlocks[0].length; x++) {
let brushX = this.x * BLOCK_WIDTH + x * BLOCK_WIDTH - brushDiffX;
let brushY = this.y * BLOCK_HEIGHT + y * BLOCK_HEIGHT - brushDiffY;
2021-07-31 02:12:11 +00:00
let arrayY = brushY / BLOCK_HEIGHT;
let arrayX = brushX / BLOCK_WIDTH;
if (
2021-07-31 02:12:11 +00:00
this.currentAsciiBlocks[arrayY] &&
this.currentAsciiBlocks[arrayY][arrayX]
) {
2021-07-31 02:12:11 +00:00
targetBlock = this.currentAsciiBlocks[arrayY][arrayX];
2021-07-31 02:12:11 +00:00
if (this.canFg) {
targetBlock.fg = null;
2021-05-08 01:51:30 +00:00
}
2021-07-31 02:12:11 +00:00
if (this.canBg) {
targetBlock.bg = null;
}
2021-07-31 02:12:11 +00:00
if (this.canText) {
targetBlock.char = null;
}
}
2021-07-31 02:12:11 +00:00
if (this.mirrorX) {
if (
this.currentAsciiBlocks[arrayY] &&
this.currentAsciiBlocks[arrayY][
this.currentAscii.width - arrayX
]
) {
targetBlock =
this.currentAsciiBlocks[arrayY][
this.currentAscii.width - arrayX
];
if (this.canFg) {
targetBlock.fg = null;
}
if (this.canBg) {
targetBlock.bg = null;
}
if (this.canText) {
targetBlock.char = null;
}
}
}
if (this.mirrorY) {
if (
this.currentAsciiBlocks[this.currentAscii.height - arrayY] &&
this.currentAsciiBlocks[this.currentAscii.height - arrayY][
arrayX
]
) {
targetBlock =
this.currentAsciiBlocks[this.currentAscii.height - arrayY][
arrayX
];
if (this.canFg) {
targetBlock.fg = null;
}
if (this.canBg) {
targetBlock.bg = null;
}
if (this.canText) {
targetBlock.char = null;
}
}
}
if (this.mirrorY && this.mirrorX) {
if (
this.currentAsciiBlocks[this.currentAscii.height - arrayY] &&
this.currentAsciiBlocks[this.currentAscii.height - arrayY][
this.currentAscii.width - arrayX
]
) {
targetBlock =
this.currentAsciiBlocks[this.currentAscii.height - arrayY][
this.currentAscii.width - arrayX
];
if (this.canFg) {
targetBlock.fg = null;
}
if (this.canBg) {
targetBlock.bg = null;
}
if (this.canText) {
targetBlock.char = null;
}
}
}
}
}
}
2021-05-08 01:51:30 +00:00
},
// Fill tool
2021-04-02 04:43:11 +00:00
fill() {
const newColor = this.currentBg;
2021-08-04 01:30:00 +00:00
const current = this.currentAsciiBlocks[this.y][this.x].bg;
2021-04-02 04:43:11 +00:00
2021-04-03 03:13:03 +00:00
// If the newColor is same as the existing
// Then return the original image.
2021-04-02 04:43:11 +00:00
if (current === newColor) {
return this.currentAsciiBlocks;
2021-04-02 04:43:11 +00:00
}
2021-03-27 04:24:05 +00:00
2021-08-04 01:30:00 +00:00
this.fillTool(this.currentAsciiBlocks, this.y, this.x, current);
2021-04-02 04:43:11 +00:00
},
fillTool(fillBlocks, y, x, current) {
2021-04-03 03:13:03 +00:00
// If row is less than 0
2021-04-02 04:43:11 +00:00
if (x < 0) {
return;
}
2021-04-02 00:19:33 +00:00
2021-04-03 03:13:03 +00:00
// If column is less than 0
2021-04-02 04:43:11 +00:00
if (y < 0) {
return;
}
2021-04-02 00:19:33 +00:00
2021-04-03 03:13:03 +00:00
// If row is greater than image length
2021-05-01 02:17:49 +00:00
if (x >= this.currentAscii.width) {
2021-04-02 04:43:11 +00:00
return;
}
2021-04-02 03:44:08 +00:00
2021-04-03 03:13:03 +00:00
// If column is greater than image length
2021-05-01 02:17:49 +00:00
if (y >= this.currentAscii.height) {
2021-04-02 04:43:11 +00:00
return;
}
2021-04-02 04:43:11 +00:00
if (!fillBlocks[y] && !fillBlocks[y][x]) {
return;
}
2021-04-02 00:19:33 +00:00
2021-04-03 03:13:03 +00:00
// If the current pixel is not which needs to be replaced
2021-04-02 04:43:11 +00:00
if (fillBlocks[y][x].bg !== current) {
return;
}
2021-04-02 03:44:08 +00:00
if (this.canBg) {
fillBlocks[y][x].bg = this.currentBg;
}
if (this.canFg) {
fillBlocks[y][x].fg = this.currentFg;
}
if (this.canText) {
fillBlocks[y][x].char = this.currentChar;
}
2021-04-02 03:44:08 +00:00
2021-04-03 03:13:03 +00:00
// Fill in all four directions
// Fill Prev row
this.fillTool(fillBlocks, y, x - 1, current);
2021-04-02 03:44:08 +00:00
2021-04-03 03:13:03 +00:00
// Fill Next row
this.fillTool(fillBlocks, y, x + 1, current);
2021-04-02 03:44:08 +00:00
2021-04-03 03:13:03 +00:00
// Fill Prev col
this.fillTool(fillBlocks, y - 1, x, current);
2021-04-03 03:13:03 +00:00
// Fill next col
this.fillTool(fillBlocks, y + 1, x, current);
},
},
2020-12-19 01:27:50 +00:00
};
</script>