import * as React from "react"; import { Grid, Image, Header, Form, Checkbox, Input, Select, CheckboxProps, FormDropdownProps, InputProps, Segment, Button } from "semantic-ui-react"; import * as workspace from "../lib/workspace"; import * as palette from "../lib/palette"; import * as util from "../lib/util"; import { encodeSpriteSheet, encodeSprite } from "../lib/images"; interface FileInfoProps { file: SourceFile; } interface FileInfoState { output: string; } export class FileInfo extends React.Component { builder: AssetBuilder; setBuilderRef = (ref: AssetBuilder) => { this.builder = ref this.onBuilderChange(); }; onBuilderChange = () => this.setState({ output: this.getTextOutput() }); render() { let imageSource: string; const file = this.props.file; if (file.extension === ".png") { imageSource = "data:image/png;base64," + file.data; } const basename = file.name.substr(0, file.name.length - file.extension.length); return
{ imageSource && }
{file.name}
{ this.hasAsset() ? :
}
} getAsset(): Sprite | SpriteSheet { let selectedPalette = palette.defaultPalette; const paletteName = this.builder.state.palette; if (paletteName && paletteName != "__default__") { const id = parseInt(paletteName.substr(paletteName.lastIndexOf("_") + 1)); for (const f of workspace.allPalettes()) { if (f.id === id) selectedPalette = f; } } if (this.builder.state.isSpriteSheet) { const sheet: SpriteSheet = { identifier: value(this.builder.state.identifier), source: this.props.file as ImageFile, palette: selectedPalette, spriteWidth: parseInt(value(this.builder.state.spriteWidth)), spriteHeight: parseInt(value(this.builder.state.spriteHeight)), spriteCount: parseInt(value(this.builder.state.spriteCount)), }; return sheet; } else { const sprite: Sprite = { identifier: value(this.builder.state.identifier), source: this.props.file as ImageFile, palette: selectedPalette }; return sprite; } } getTextOutput() { const asset = this.getAsset(); if ((asset as SpriteSheet).spriteCount != undefined) { const encoded = encodeSpriteSheet(asset as SpriteSheet); return encoded.map((e, i) => `const ${asset.identifier}_${i} = ${e.img};`).join("\n\n") } else { const encoded = encodeSprite(asset as Sprite); return `const ${asset.identifier} = ${encoded.img};` } } hasAsset() { return !palette.isPaletteFile(this.props.file.extension); } } interface AssetBuilderProps { info: ImageInfo; basename: string; onChange: () => void; } interface AssetBuilderState { isSpriteSheet: boolean; palette: string; identifier: FieldValue; spriteWidth?: FieldValue; spriteHeight?: FieldValue; spriteCount?: FieldValue; } interface FieldValue { text: string; err?: string; } type EventHandler = (event: React.FormEvent, data: U) => void; export class AssetBuilder extends React.Component { private handlers: { [index: string]: EventHandler} = {}; private checkboxes: { [index: string]: EventHandler} = {}; private dropdowns: { [index: string]: EventHandler} = {}; constructor(props: AssetBuilderProps) { super(props); const count = Math.floor(props.info.width / 16) * Math.floor(props.info.height / 16) this.state = { isSpriteSheet: false, palette: undefined, identifier: { text: util.escapeIdentifier(props.basename) }, spriteWidth: { text: "16" }, spriteHeight: { text: "16" }, spriteCount: { text: "" + count } }; } handleTextChange(fieldName: string, isNumber = false) { if (!this.handlers[fieldName]) { this.handlers[fieldName] = ev => { const val: FieldValue = { text: (ev.target as HTMLInputElement).value }; if (isNumber) { let parsed = parseInt(val.text); if (isNaN(parsed)) { val.err = "Please enter a number"; } } let stateTransaction: any = {}; stateTransaction[fieldName] = val; this.setState(stateTransaction, this.props.onChange); }; } return this.handlers[fieldName]; } handleCheckboxChange(fieldName: string) { if (!this.checkboxes[fieldName]) { this.checkboxes[fieldName] = (ev, props) => { let stateTransaction: any = {}; stateTransaction[fieldName] = props.checked; this.setState(stateTransaction, this.props.onChange); }; } return this.checkboxes[fieldName]; } handleDropdownChange(fieldName: string) { if (!this.dropdowns[fieldName]) { this.dropdowns[fieldName] = (ev, props) => { let stateTransaction: any = {}; stateTransaction[fieldName] = props.value; this.setState(stateTransaction, this.props.onChange); }; } return this.dropdowns[fieldName]; } render() { return
{ this.state.isSpriteSheet ? : undefined }
} } interface SelectOption { key: string; text: string; value: string; } function getPaletteOptions() { const options: SelectOption[] = []; options.push({ key: "__default__", text: "default", value: "__default__" }); workspace.allPalettes().forEach(p => { options.push({ key: "palette_" + p.id, text: palette.getName(p), value: "palette_" + p.id }); }) return options; } function value(val: FieldValue) { return val ? val.text : ""; } function mkGradient(colors: string[]) { const colorStrings: string[] = []; for (let i = 0; i < 16; i++) { const color = colors[i] || "#000000"; colorStrings.push(`${color} ${i * 6.25}%, ${color} ${(i + 1) * 6.25}%`); } return `linear-gradient(to right, ${colorStrings.join(", ")})`; } interface CodePreviewProps { text: string; } class CodePreview extends React.Component { input: HTMLTextAreaElement; handleTextRef = (i: HTMLTextAreaElement) => this.input = i; handleCopy = () => { if (!this.input) return; this.input.focus(); this.input.setSelectionRange(0, 9999); try { const success = document.execCommand("copy"); } catch (e) { } }; render() { return } }