-
Notifications
You must be signed in to change notification settings - Fork 273
feat: piskel -> spritesheet/zip #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ConnorTippets
wants to merge
5
commits into
p2r3:master
Choose a base branch
from
ConnorTippets:piskel
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+182
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
52ad1be
feat: add piskel to json rename handler
ConnorTippets a4039c4
feat: piskel -> spritesheet conversion
ConnorTippets 8fe0a47
feat: piskel -> zip with frames
ConnorTippets fd31be9
fix: correctly mark input/output on supported piskel formats
ConnorTippets ed32e24
Merge branch 'master' into piskel
ConnorTippets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts"; | ||
| import CommonFormats from "src/CommonFormats.ts"; | ||
| import JSZip from "jszip"; | ||
|
|
||
| class piskelHandler implements FormatHandler { | ||
|
|
||
| public name: string = "piskel"; | ||
| public supportedFormats?: FileFormat[]; | ||
| public ready: boolean = false; | ||
|
|
||
| #canvas?: HTMLCanvasElement; | ||
| #ctx?: CanvasRenderingContext2D; | ||
|
|
||
| async init() { | ||
| this.supportedFormats = [ | ||
| CommonFormats.PNG.builder("png") | ||
| .markLossless() | ||
| .allowFrom(false) | ||
| .allowTo(true), | ||
| CommonFormats.ZIP.builder("zip") | ||
| .markLossless() | ||
| .allowFrom(false) | ||
| .allowTo(true), | ||
| { | ||
| name: "Piskel Sprite Save File", | ||
| format: "piskel", | ||
| extension: "piskel", | ||
| mime: "image/png+json", | ||
| from: true, | ||
| to: false, | ||
| category: "image", | ||
| internal: "piskel", | ||
| lossless: true | ||
| } | ||
| ]; | ||
|
|
||
| this.#canvas = document.createElement("canvas"); | ||
| const ctx = this.#canvas.getContext("2d"); | ||
| if (!ctx) { | ||
| throw new Error("Failed to create 2D rendering context."); | ||
| } | ||
| this.#ctx = ctx; | ||
|
|
||
| this.ready = true; | ||
| } | ||
|
|
||
| async doConvert( | ||
| inputFiles: FileData[], | ||
| inputFormat: FileFormat, | ||
| outputFormat: FileFormat | ||
| ): Promise<FileData[]> { | ||
| if (!this.ready || !this.#canvas || !this.#ctx) { | ||
| throw new Error("Handler not initialized!"); | ||
| } | ||
|
|
||
| if (!(inputFormat.internal === "piskel" && ["png", "zip"].includes(outputFormat.internal))) { | ||
| throw Error("Invalid input/output format."); | ||
| } | ||
|
|
||
| const outputFiles: FileData[] = []; | ||
|
|
||
| for (const inputFile of inputFiles) { | ||
|
|
||
| const fileRaw = new TextDecoder().decode(inputFile.bytes); | ||
| const contents = JSON.parse(fileRaw); | ||
|
|
||
| const version: number = contents.modelVersion; | ||
| if (version !== 2) { | ||
| throw Error("Only version 2 piskel files are supported."); | ||
| } | ||
|
|
||
| const layers: string[] = contents.piskel.layers; | ||
| if (layers.length === 0) { | ||
| throw Error("No layers to convert."); | ||
| } | ||
|
|
||
| const spriteWidth: number = contents.piskel.width; | ||
| const spriteHeight: number = contents.piskel.height; | ||
|
|
||
| // We're parsing the first layer, because they decided to | ||
| // duplicate the frame count for each layer instead of | ||
| // keeping it global, despite the fact that each layer | ||
| // has the same frame count. | ||
| const temp = JSON.parse(layers[0]); | ||
| const frameCount: number = temp.frameCount | ||
|
|
||
| this.#canvas.width = spriteWidth * frameCount; | ||
| this.#canvas.height = spriteHeight; | ||
|
|
||
| // We're clearing here because each layer needs to | ||
| // superimpose itself onto the previous. | ||
| this.#ctx.clearRect(0, 0, this.#canvas.width, this.#canvas.height); | ||
|
|
||
| for (const layerRaw of layers) { | ||
| const layer = JSON.parse(layerRaw); | ||
|
|
||
| const opacity: number = layer.opacity; | ||
|
|
||
| // I'm not entirely sure, but I think only the first chunk is used? | ||
| const layerB64: string = layer.chunks[0].base64PNG; | ||
|
|
||
| const image = new Image(); | ||
| await new Promise((resolve, reject) => { | ||
| image.addEventListener("load", resolve); | ||
| image.addEventListener("error", reject); | ||
| image.src = layerB64; | ||
| }); | ||
|
|
||
| this.#ctx.globalAlpha = opacity; | ||
| this.#ctx.drawImage(image, 0, 0); | ||
| } | ||
|
|
||
| if (outputFormat.internal === "png") { | ||
| const bytes: Uint8Array = await new Promise((resolve, reject) => { | ||
| this.#canvas!.toBlob(blob => { | ||
| if (!blob) { | ||
| return reject("Canvas output failed"); | ||
| } | ||
| blob.arrayBuffer().then(buffer => resolve(new Uint8Array(buffer))); | ||
| }, "image/png"); | ||
| }); | ||
|
|
||
| const name = inputFile.name.split(".").slice(0, -1).join(".") + "." + outputFormat.extension; | ||
| outputFiles.push({ bytes, name }); | ||
| } else if (outputFormat.internal === "zip") { | ||
| const zip = new JSZip(); | ||
|
|
||
| const fullUri = this.#canvas.toDataURL("image/png"); | ||
|
|
||
| const image = new Image(); | ||
| await new Promise((resolve, reject) => { | ||
| image.addEventListener("load", resolve); | ||
| image.addEventListener("error", reject); | ||
| image.src = fullUri; | ||
| }); | ||
|
|
||
| this.#canvas.width = spriteWidth; | ||
| this.#canvas.height = spriteHeight; | ||
|
|
||
| const baseName = inputFile.name.split(".").slice(0, -1).join("."); | ||
| for (let x = 0; x > -spriteWidth * frameCount; x -= spriteWidth) { | ||
| this.#ctx.clearRect(0, 0, this.#canvas.width, this.#canvas.height); | ||
| this.#ctx.drawImage(image, x, 0); | ||
|
|
||
| const bytes: Uint8Array = await new Promise((resolve, reject) => { | ||
| this.#canvas!.toBlob(blob => { | ||
| if (!blob) { | ||
| return reject("Canvas output failed"); | ||
| } | ||
| blob.arrayBuffer().then(buffer => resolve(new Uint8Array(buffer))); | ||
| }, "image/png"); | ||
| }); | ||
| const name = `${baseName}_Frame${-Number(x / spriteWidth)}.png`; | ||
| zip.file(name, bytes); | ||
| } | ||
|
|
||
| const bytes = await zip.generateAsync({ type: "uint8array" }); | ||
| const name = baseName + "." + outputFormat.extension; | ||
| outputFiles.push({ bytes, name }); | ||
| } | ||
| } | ||
|
|
||
| return outputFiles; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| export default piskelHandler; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.