From 381d435fd72955218405ef10689a8c0d4f4747b8 Mon Sep 17 00:00:00 2001 From: kts of kettek Date: Fri, 16 Feb 2024 22:53:15 -0800 Subject: [PATCH] Add dubious move tool --- frontend/src/App.svelte | 7 +++- frontend/src/sections/Editor2D.svelte | 2 +- frontend/src/types/tools.ts | 55 +++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 1419bb0..1d1982e 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -14,10 +14,10 @@ import { OverflowMenu, OverflowMenuItem } from "carbon-components-svelte" - import { Close, Erase, PaintBrushAlt, RainDrop, Redo, Select_01, Undo, Scale, Eyedropper } from "carbon-icons-svelte" + import { Close, Erase, PaintBrushAlt, RainDrop, Redo, Select_01, Undo, Scale, Eyedropper, Move } from "carbon-icons-svelte" import StackPreview from './sections/StackPreview.svelte' import type { Canvas } from './types/canvas' - import { BrushTool, EraserTool, FillTool, PickerTool, SelectionTool, type BrushType, type Tool } from './types/tools' + import { BrushTool, EraserTool, FillTool, PickerTool, SelectionTool, type BrushType, type Tool, MoveTool } from './types/tools' import BrushSize from './components/BrushSize.svelte' import Shortcut from './components/Shortcut.svelte' import Shortcuts from './components/Shortcuts.svelte' @@ -41,6 +41,7 @@ let toolErase = new EraserTool() let toolBrush = new BrushTool() let toolPicker = new PickerTool() + let toolMove = new MoveTool() let currentTool: Tool = toolBrush let previousTool: Tool = null let brushSize: number = 1 @@ -110,6 +111,7 @@ + @@ -117,6 +119,7 @@ swapTool(toolSelection)} /> + swapTool(toolMove)} /> swapTool(toolBrush)} /> currentTool===toolBrush?swapTool(toolPicker):null} on:release={()=>previousTool===toolBrush&¤tTool===toolPicker?swapTool(toolBrush):null} /> swapTool(toolFill)} /> diff --git a/frontend/src/sections/Editor2D.svelte b/frontend/src/sections/Editor2D.svelte index cdf2b5b..252c94f 100644 --- a/frontend/src/sections/Editor2D.svelte +++ b/frontend/src/sections/Editor2D.svelte @@ -4,7 +4,7 @@ import type { data } from '../../wailsjs/go/models.ts' import type { LoadedFile } from '../types/file' import { FilledCircle, FilledSquare, type PixelPosition } from '../types/shapes' - import { BrushTool, EraserTool, FillTool, PickerTool, type BrushType, type Tool, SelectionTool } from '../types/tools' + import { BrushTool, EraserTool, FillTool, PickerTool, MoveTool, type BrushType, type Tool, SelectionTool } from '../types/tools' import { Button, NumberInput, OverflowMenu, OverflowMenuItem, Slider } from 'carbon-components-svelte'; import { ZoomIn, ZoomOut } from 'carbon-icons-svelte'; diff --git a/frontend/src/types/tools.ts b/frontend/src/types/tools.ts index 4c3d062..aa59a3d 100644 --- a/frontend/src/types/tools.ts +++ b/frontend/src/types/tools.ts @@ -274,6 +274,61 @@ export class SelectionTool implements Tool { } } + this.active = false + } +} + +export class MoveTool implements Tool { + private active: boolean + private startX: number + private startY: number + private endX: number + private endY: number + + isActive(): boolean { + return this.active + } + + pointerDown(ctx: ToolContext, ptr: Pointer) { + this.startX = this.endX = ptr.x + this.startY = this.endY = ptr.y + this.active = true + } + pointerMove(ctx: ToolContext, ptr: Pointer) { + this.endX = ptr.x + this.endY = ptr.y + } + pointerUp(ctx: ToolContext, ptr: Pointer) { + if (this.startX === this.endX && this.startY === this.endY) { + this.active = false + return + } + + let dx = this.endX - this.startX + let dy = this.endY - this.startY + + let pixels: { x: number, y: number, index: number }[] = [] + let clearPixels: { x: number, y: number, index: number }[] = [] + for (let x = 0; x < ctx.file.canvas.width; x++) { + for (let y = 0; y < ctx.file.canvas.height; y++) { + if (ctx.file.selection.isPixelMarked(x, y)) { + let p = ctx.file.canvas.getPixel(x, y) + let { a } = ctx.file.canvas.getPaletteAsRGBA(p) + // FIXME: Do we really want to treat 0 as transparent index? Additionally, for RGBA, we probably want to merge the colors and create a new entry... maybe this should be handled in PixelsPlaceUndoable within the file class... + if (a !== 0) { + if (x+dx >= 0 && x+dx < ctx.file.canvas.width && y+dy >= 0 && y+dy < ctx.file.canvas.height) { + pixels.push({x: x+dx, y: y+dy, index: p}) + } + clearPixels.push({x, y, index: 0}) + } + } + } + } + + ctx.file.capture() + ctx.file.push(new PixelsPlaceUndoable(clearPixels)) + ctx.file.push(new PixelsPlaceUndoable(pixels)) + ctx.file.release() this.active = false } } \ No newline at end of file