asciibird/src/views/Editor.vue

336 lines
9.2 KiB
Vue
Raw Normal View History

2020-12-19 01:27:50 +00:00
<template>
<div>
2021-03-27 04:16:15 +00:00
<div id="canvas-area">
2021-03-20 03:45:10 +00:00
<vue-draggable-resizable
style="z-index: 5;"
:grid="[
$store.getters.currentAscii.blockHeight,
$store.getters.currentAscii.blockWidth,
]"
:w="canvas.width + $store.getters.currentAscii.blockWidth"
:h="canvas.height + $store.getters.currentAscii.blockHeight"
:draggable="$store.getters.getCurrentTool === 'default'"
@resizing="onCanvasResize"
@dragstop="onCavasDragStop"
:x="$store.getters.currentAscii.x"
:y="$store.getters.currentAscii.y"
2021-03-20 03:45:10 +00:00
>
2021-03-31 23:29:55 +00:00
<canvas
ref="canvastools"
id="canvastools"
class="canvas"
:width="canvas.width"
:height="canvas.height"
@mousemove="canvasMouseMove"
@mousedown="canvasMouseDown"
@mouseup="canvasMouseUp"
></canvas>
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;
}
.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>
2021-03-31 23:29:55 +00:00
import Block from '../components/Block.vue';
2020-12-19 01:27:50 +00:00
export default {
2021-03-31 23:29:55 +00:00
name: 'Editor',
components: { Block },
2021-01-02 01:52:44 +00:00
mounted() {
if (this.$store.getters.currentAscii.blocks) {
2021-03-31 23:29:55 +00:00
this.ctx = this.$refs.canvas.getContext('2d');
this.canvas.width = this.$store.getters.currentAscii.width
* this.$store.getters.currentAscii.blockWidth;
this.canvas.height = this.$store.getters.currentAscii.height
* this.$store.getters.currentAscii.blockHeight;
2021-03-27 02:07:33 +00:00
this.delayRedrawCanvas();
2021-03-31 23:29:55 +00:00
this.$store.commit('changeTool', 'default');
2021-03-13 05:27:07 +00:00
}
},
created() {},
data: () => ({
2021-01-02 01:52:44 +00:00
ctx: 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,
throttle: true,
}),
computed: {
2021-03-13 05:27:07 +00:00
canvasStyle() {
2021-03-20 03:45:10 +00:00
return `width:${this.canvas.width};height:${this.canvas.height};`;
2021-01-23 02:50:32 +00:00
},
generateTitle() {
2021-03-31 23:29:55 +00:00
return this.$store.getters.currentAscii.title ?? '';
2021-03-13 05:27:07 +00:00
},
watchAsciiChange() {
return this.$store.getters.currentAscii;
2021-02-06 01:57:35 +00:00
},
},
watch: {
2021-03-13 05:27:07 +00:00
watchAsciiChange(val, old) {
2021-03-31 23:29:55 +00:00
this.canvas.width = this.$store.getters.currentAscii.width
* this.$store.getters.currentAscii.blockWidth;
this.canvas.height = this.$store.getters.currentAscii.height
* this.$store.getters.currentAscii.blockHeight;
2021-03-27 02:07:33 +00:00
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
document.title = `asciibird - ${this.$store.getters.currentAscii.title}`;
2021-03-27 02:07:33 +00:00
},
},
methods: {
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.$store.getters.currentAscii.blocks.length) {
const BLOCK_WIDTH = this.$store.getters.currentAscii.blockWidth;
const BLOCK_HEIGHT = this.$store.getters.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
2021-03-31 23:29:55 +00:00
this.ctx.font = '12.5px Hack';
for (y = 0; y < this.$store.getters.currentAscii.height + 1; y++) {
2021-03-20 03:45:10 +00:00
canvasY = BLOCK_HEIGHT * y;
2021-01-09 01:57:48 +00:00
for (x = 0; x < this.$store.getters.currentAscii.width + 1; x++) {
2021-03-20 03:45:10 +00:00
if (
2021-03-31 23:29:55 +00:00
this.$store.getters.currentAscii.blocks[y]
&& this.$store.getters.currentAscii.blocks[y][x]
2021-03-20 03:45:10 +00:00
) {
curBlock = Object.assign(
curBlock,
2021-03-31 23:29:55 +00:00
this.$store.getters.currentAscii.blocks[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-03-29 05:10:31 +00:00
this.ctx.fillStyle = this.$store.getters.mircColours[
curBlock.bg
];
2021-03-20 03:45:10 +00:00
this.ctx.fillRect(canvasX, canvasY, BLOCK_WIDTH, BLOCK_HEIGHT);
} else {
2021-03-31 23:29:55 +00:00
this.ctx.fillStyle = 'rgba(0, 0, 200, 0)';
2021-03-20 03:45:10 +00:00
}
2021-03-13 05:27:07 +00:00
2021-03-20 03:45:10 +00:00
if (curBlock.char) {
if (curBlock.fg !== null) {
2021-03-29 05:10:31 +00:00
this.ctx.fillStyle = this.$store.getters.mircColours[
curBlock.fg
];
2021-03-20 03:45:10 +00:00
} else {
2021-03-31 23:29:55 +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,
2021-03-20 09:33:16 +00:00
canvasX - 1,
2021-03-31 23:29:55 +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) {
this.canvas.width = width;
this.canvas.height = height;
// 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
this.$store.commit("changeAsciiCanvasState", {x: x, y: y})
2021-03-27 04:16:15 +00:00
},
// Mouse Up, Down and Move
canvasMouseUp() {
switch (this.$store.getters.getCurrentTool) {
2021-03-31 23:29:55 +00:00
case 'brush':
2021-03-27 04:16:15 +00:00
this.canTool = false;
break;
2021-03-31 23:29:55 +00:00
case 'eraser':
2021-03-27 04:16:15 +00:00
this.canTool = false;
break;
2021-03-31 23:29:55 +00:00
case 'fill':
2021-03-27 04:16:15 +00:00
this.canTool = false;
break;
}
2021-03-31 23:29:55 +00:00
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
},
canvasMouseDown() {
2021-03-31 23:29:55 +00:00
if (this.$store.getters.currentAscii.blocks[this.y] && this.$store.getters.currentAscii.blocks[this.y][this.x]) {
const targetBlock = this.$store.getters.currentAscii.blocks[this.y][this.x];
switch (this.$store.getters.getCurrentTool) {
2021-03-31 23:29:55 +00:00
case 'default':
break;
2021-03-31 23:29:55 +00:00
case 'fill':
this.fillTool(targetBlock);
break;
2021-03-31 23:29:55 +00:00
case 'brush':
this.canTool = true;
break;
2021-03-31 23:29:55 +00:00
case 'eraser':
this.canTool = true;
break;
2021-03-31 23:29:55 +00:00
case 'dropper':
const curBlock = this.$store.getters.currentAscii.blocks[this.y][this.x];
if (this.$store.getters.getTargetingFg) {
2021-03-31 23:29:55 +00:00
this.$store.commit('changeColourFg', curBlock.fg);
}
2021-03-27 04:16:15 +00:00
if (this.$store.getters.getTargetingBg) {
2021-03-31 23:29:55 +00:00
this.$store.commit('changeColourBg', curBlock.bg);
}
2021-03-27 04:16:15 +00:00
2021-03-29 08:21:23 +00:00
if (this.$store.getters.getTargetingChar) {
2021-03-31 23:29:55 +00:00
this.$store.commit('changeChar', curBlock.char);
2021-03-29 08:21:23 +00:00
}
2021-03-31 23:29:55 +00:00
this.$store.commit('changeTool', this.$store.getters.getCurrentTool);
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-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;
}
this.x = Math.floor(this.x / this.$store.getters.currentAscii.blockWidth);
this.y = Math.floor(
2021-03-31 23:29:55 +00:00
this.y / this.$store.getters.currentAscii.blockHeight,
);
2021-03-31 23:29:55 +00:00
this.$emit('coordsupdate', { x: this.x, y: this.y });
2021-03-29 05:10:31 +00:00
if (
2021-03-31 23:29:55 +00:00
this.$store.getters.currentAscii.blocks[this.y]
&& this.$store.getters.currentAscii.blocks[this.y][this.x]
) {
switch (this.$store.getters.getCurrentTool) {
2021-03-31 23:29:55 +00:00
case 'brush':
if (this.canTool) {
if (this.$store.getters.getTargetingFg) {
2021-03-29 08:21:23 +00:00
this.$store.getters.currentAscii.blocks[this.y][this.x].fg = this.$store.getters.getFgColour;
2021-03-27 02:07:33 +00:00
}
if (this.$store.getters.getTargetingBg) {
2021-03-29 08:21:23 +00:00
this.$store.getters.currentAscii.blocks[this.y][this.x].bg = this.$store.getters.getBgColour;
}
if (this.$store.getters.getTargetingChar) {
this.$store.getters.currentAscii.blocks[this.y][this.x].char = this.$store.getters.getSelectedChar;
2021-03-27 02:07:33 +00:00
}
}
break;
2021-03-27 02:07:33 +00:00
2021-03-31 23:29:55 +00:00
case 'eraser':
if (this.canTool) {
if (this.$store.getters.getTargetingFg) {
this.$store.getters.currentAscii.blocks[this.y][
this.x
].fg = null;
2021-03-27 02:07:33 +00:00
}
if (this.$store.getters.getTargetingBg) {
this.$store.getters.currentAscii.blocks[this.y][
this.x
].bg = null;
2021-03-27 02:07:33 +00:00
}
2021-03-29 08:21:23 +00:00
if (this.$store.getters.getTargetingChar) {
this.$store.getters.currentAscii.blocks[this.y][
this.x
].char = null;
2021-03-27 02:07:33 +00:00
}
}
break;
}
2021-03-27 02:07:33 +00:00
}
this.delayRedrawCanvas();
2021-03-13 03:30:58 +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-03-31 23:29:55 +00:00
}, this.$store.state.options.canvasRedrawSpeed);
2021-03-27 02:07:33 +00:00
}
},
2021-03-27 04:24:05 +00:00
// TOOLS
2021-03-31 23:29:55 +00:00
fillTool(block) {
if (this.$store.getters.getTargetingBg) {
2021-03-31 23:29:55 +00:00
const fillSameBg = block.bg;
2021-03-20 03:45:10 +00:00
}
2021-03-27 02:07:33 +00:00
2021-03-31 23:29:55 +00:00
console.log(block);
},
},
2020-12-19 01:27:50 +00:00
};
</script>