asciibird/src/views/Editor.vue

386 lines
9.5 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; left: 220px"
2021-03-27 04:16:15 +00:00
:grid="[currentAsciibirdMeta.blockHeight, currentAsciibirdMeta.blockWidth]"
:w="canvas.width + (currentAsciibirdMeta.blockWidth/2)"
:h="canvas.height + (currentAsciibirdMeta.blockHeight/2)"
2021-03-20 03:45:10 +00:00
:draggable="currentTool === 'default'"
@resizing="onCanvasResize"
@dragging="onCanvasDrag"
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.2);
border: lightgrey 1px solid;
box-shadow: 0 2px 3px rgba(0, 0, 0, 0.2);
z-index: 0;
}
2021-01-02 01:52:44 +00:00
</style>
2020-12-19 01:27:50 +00:00
<script>
import Block from "../components/Block.vue";
2020-12-19 01:27:50 +00:00
export default {
name: "Editor",
2021-03-13 05:27:07 +00:00
props: ["tab", "refresh"],
components: { Block },
2021-01-02 01:52:44 +00:00
mounted() {
this.currentAsciibirdMeta = this.$store.getters.currentAscii;
this.mircColors = this.$store.getters.mircColors;
2021-03-27 02:07:33 +00:00
this.toolbarState = this.$store.getters.getToolbarState;
2021-03-13 03:30:58 +00:00
2021-03-13 05:27:07 +00:00
if (this.currentAsciibirdMeta.blocks) {
this.ctx = this.$refs.canvas.getContext("2d");
2021-03-27 02:07:33 +00:00
this.canvas.width =
this.currentAsciibirdMeta.width * this.currentAsciibirdMeta.blockWidth;
this.canvas.height =
this.currentAsciibirdMeta.height *
this.currentAsciibirdMeta.blockHeight;
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
this.$store.commit("changeTool", "default");
this.currentTool = "default";
2021-03-13 05:27:07 +00:00
}
},
created() {},
data: () => ({
text: "ASCII",
2021-01-23 02:50:32 +00:00
currentAsciibirdMeta: {
title: "Loading...",
width: 0,
height: 0,
},
mircColors: null,
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
},
2021-03-13 03:30:58 +00:00
x: 0,
2021-03-13 05:27:07 +00:00
y: 0,
refreshCanvas: 0,
2021-03-20 03:45:10 +00:00
currentTool: null,
redraw: true,
2021-03-27 02:07:33 +00:00
toolbarState: {
targetingFg: false,
targetingBg: false,
targetingText: false,
},
2021-03-27 04:16:15 +00:00
canTool: false,
}),
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-01-30 02:33:11 +00:00
return this.currentAsciibirdMeta.title ?? "";
},
2021-02-06 01:57:35 +00:00
watchToolChange() {
2021-03-20 03:45:10 +00:00
return this.$store.getters.getCurrentTool;
2021-03-13 05:27:07 +00:00
},
watchAsciiChange() {
return this.$store.getters.currentAscii;
2021-02-06 01:57:35 +00:00
},
2021-03-27 02:07:33 +00:00
watchTargetingFg() {
return this.$store.getters.getTargetingFg;
},
watchTargetingBg() {
return this.$store.getters.getTargetingBg;
},
watchTargetingText() {
return this.$store.getters.getTargetingText;
},
},
watch: {
2021-03-13 05:27:07 +00:00
watchAsciiChange(val, old) {
2021-03-20 03:45:10 +00:00
this.currentAsciibirdMeta = val;
2021-03-27 02:07:33 +00:00
this.canvas.width =
this.currentAsciibirdMeta.width * this.currentAsciibirdMeta.blockWidth;
this.canvas.height =
this.currentAsciibirdMeta.height *
this.currentAsciibirdMeta.blockHeight;
this.delayRedrawCanvas();
2021-03-27 04:16:15 +00:00
window.title = this.currentAsciibirdMeta.name
2021-02-06 01:57:35 +00:00
},
watchToolChange(val) {
2021-03-20 03:45:10 +00:00
this.currentTool = val;
2021-03-13 05:27:07 +00:00
},
2021-03-27 02:07:33 +00:00
watchTargetingFg(val) {
this.toolbarState.targetingFg = val;
},
watchTargetingBg(val) {
this.toolbarState.targetingBg = val;
},
watchTargetingText(val) {
this.toolbarState.targetingText = val;
},
},
methods: {
getMircColor(index) {
2021-03-13 05:27:07 +00:00
return this.$store.getters.mircColors[index];
2021-01-02 01:52:44 +00:00
},
2021-01-09 01:57:48 +00:00
redrawCanvas() {
2021-01-23 02:50:32 +00:00
// Clears the whole canvas
this.ctx.clearRect(0, 0, 10000, 10000);
2021-01-23 02:50:32 +00:00
2021-03-20 03:45:10 +00:00
if (this.currentAsciibirdMeta.blocks.length) {
const BLOCK_WIDTH = this.currentAsciibirdMeta.blockWidth;
const BLOCK_HEIGHT = this.currentAsciibirdMeta.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 = {};
2021-03-20 09:33:16 +00:00
this.ctx.font = "12.5px Hack";
2021-03-20 03:45:10 +00:00
for (y = 0; y < this.currentAsciibirdMeta.height + 1; y++) {
canvasY = BLOCK_HEIGHT * y;
2021-01-09 01:57:48 +00:00
2021-03-20 03:45:10 +00:00
for (x = 0; x < this.currentAsciibirdMeta.width + 1; x++) {
if (
this.currentAsciibirdMeta.blocks[y] &&
this.currentAsciibirdMeta.blocks[y][x]
) {
2021-03-27 02:07:33 +00:00
curBlock = Object.assign(curBlock,this.currentAsciibirdMeta.blocks[y][x])
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) {
this.ctx.fillStyle = this.mircColors[curBlock.bg];
this.ctx.fillRect(canvasX, canvasY, BLOCK_WIDTH, BLOCK_HEIGHT);
} else {
this.ctx.fillStyle = "rgba(0, 0, 200, 0)";
}
2021-03-13 05:27:07 +00:00
2021-03-20 03:45:10 +00:00
if (curBlock.char) {
if (curBlock.fg !== null) {
this.ctx.fillStyle = this.mircColors[curBlock.fg];
} else {
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,
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;
this.delayRedrawCanvas();
},
onCanvasDrag(left, top) {
// Update left and top in panel
},
// Mouse Up, Down and Move
canvasMouseUp() {
this.delayRedrawCanvas();
switch (this.currentTool) {
case "brush":
this.canTool = false;
break;
case "eraser":
this.canTool = false;
break;
case "fill":
this.canTool = false;
break;
}
},
canvasMouseDown() {
switch (this.currentTool) {
case "default":
break;
case "fill":
// this.canTool = true;
if (
this.currentAsciibirdMeta.blocks[this.y] &&
this.currentAsciibirdMeta.blocks[this.y][this.x]
) {
this.fillTool()
}
break;
case "brush":
this.canTool = true;
break;
case "eraser":
this.canTool = true;
break;
case "dropper":
if (
this.currentAsciibirdMeta.blocks[this.y] &&
this.currentAsciibirdMeta.blocks[this.y][this.x]
) {
let curBlock = this.currentAsciibirdMeta.blocks[this.y][this.x];
if (this.toolbarState.targetingFg) {
this.$store.commit("changeColorFg", curBlock.fg);
}
if (this.toolbarState.targetingBg) {
this.$store.commit("changeColorBg", curBlock.bg);
}
// if (this.toolbarState.targetingText) {
// }
}
this.currentTool = "default";
this.$store.commit("changeTool", this.currentTool);
break;
}
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;
}
2021-03-13 05:27:07 +00:00
this.x = Math.floor(this.x / this.currentAsciibirdMeta.blockWidth);
this.y = Math.floor(this.y / this.currentAsciibirdMeta.blockHeight);
2021-03-27 02:07:33 +00:00
switch (this.currentTool) {
case "brush":
2021-03-27 04:16:15 +00:00
if (this.canTool) {
2021-03-27 02:07:33 +00:00
if (
this.currentAsciibirdMeta.blocks[this.y] &&
this.currentAsciibirdMeta.blocks[this.y][this.x]
) {
if (this.toolbarState.targetingFg) {
this.currentAsciibirdMeta.blocks[this.y][
this.x
].fg = this.$store.getters.getFgColor;
}
if (this.toolbarState.targetingBg) {
this.currentAsciibirdMeta.blocks[this.y][
this.x
].bg = this.$store.getters.getBgColor;
}
// if (this.toolbarState.targetingText) {
// this.currentAsciibirdMeta.blocks[this.y][this.x].char = null;
// }
}
}
break;
case "eraser":
2021-03-27 04:16:15 +00:00
if (this.canTool) {
2021-03-27 02:07:33 +00:00
if (
this.currentAsciibirdMeta.blocks[this.y] &&
this.currentAsciibirdMeta.blocks[this.y][this.x]
) {
if (this.toolbarState.targetingFg) {
this.currentAsciibirdMeta.blocks[this.y][this.x].fg = null;
}
if (this.toolbarState.targetingBg) {
this.currentAsciibirdMeta.blocks[this.y][this.x].bg = null;
}
if (this.toolbarState.targetingText) {
this.currentAsciibirdMeta.blocks[this.y][this.x].char = null;
}
}
}
break;
}
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;
setTimeout(() => {
this.redraw = true
this.redrawCanvas();
2021-03-27 04:16:15 +00:00
}, 8);
2021-03-27 02:07:33 +00:00
}
},
2021-03-27 04:24:05 +00:00
// TOOLS
2021-03-27 04:16:15 +00:00
fillTool() {
let fillStartBlock = this.currentAsciibirdMeta.blocks[this.y][this.x];
if (this.toolbarState.targetingBg) {
let fillSameBg = fillStartBlock.bg
2021-03-20 03:45:10 +00:00
}
2021-03-27 02:07:33 +00:00
2021-03-27 04:16:15 +00:00
console.log(fillStartBlock)
},
2021-03-27 02:07:33 +00:00
},
2020-12-19 01:27:50 +00:00
};
</script>