restructure animation metadata

pull/2/head
thatcosmonaut 2019-12-12 13:17:32 -08:00
parent f72166f9b6
commit 99e74e2a2c
7 changed files with 68 additions and 47 deletions

View File

@ -1,5 +1,5 @@
import { IFrame } from './IFrame'; import { IFrame } from './IFrame';
import { IPin } from './IPin'; import { IPinDefinition } from './IPinDefinition';
export interface IAnimationData { export interface IAnimationData {
frameRate: number; frameRate: number;
@ -7,5 +7,5 @@ export interface IAnimationData {
originY: number | null; originY: number | null;
loop: boolean; loop: boolean;
frames: IFrame[]; frames: IFrame[];
pins: IPin[]; pinDefinitions: IPinDefinition[];
} }

View File

@ -1,4 +0,0 @@
export interface IPin {
id: number;
name: string;
}

View File

@ -0,0 +1,4 @@
export interface IPinDefinition {
id: number;
name: string;
}

View File

@ -56,7 +56,7 @@ export class CanvasHandler {
const pixelX: number = Math.floor(offsetX / ratioWidth); const pixelX: number = Math.floor(offsetX / ratioWidth);
const pixelY: number = Math.floor(offsetY / ratioHeight); const pixelY: number = Math.floor(offsetY / ratioHeight);
// console.log('CLICK X:' + pixelX + ' Y:' + pixelY); // console.log('CLICK X:' + pixelX + ' Y:' + pixelY);
if (this.projectData.currentlySelectedPin === 0) { if (this.projectData.currentlySelectedPin === -1) {
// update animation data // update animation data
this.animationData.originX = pixelX; this.animationData.originX = pixelX;
this.animationData.originY = pixelY; this.animationData.originY = pixelY;

View File

@ -137,10 +137,10 @@ export class FrameHandler {
for (let f = 0; f < this.animationData.frames.length; f++) { for (let f = 0; f < this.animationData.frames.length; f++) {
// this.frameViewer.children[f].classList.add('warning'); // this.frameViewer.children[f].classList.add('warning');
if (this.animationData.pins !== undefined) { if (this.animationData.pinDefinitions !== undefined) {
for (let p = 0; p < this.animationData.pins.length; p++) { for (let p = 0; p < this.animationData.pinDefinitions.length; p++) {
if (this.animationData.pins[p] !== undefined) { if (this.animationData.pinDefinitions[p] !== undefined) {
const pinIDtoCheck = this.animationData.pins[p].id; const pinIDtoCheck = this.animationData.pinDefinitions[p].id;
// console.log('checking frame ' + f + ' for pinID ' + this.animationData.pins[p].name); // console.log('checking frame ' + f + ' for pinID ' + this.animationData.pins[p].name);
if (this.frameViewer.children[f] !== undefined) { if (this.frameViewer.children[f] !== undefined) {
if (this.animationData.frames[f].pinData[pinIDtoCheck] === undefined) { if (this.animationData.frames[f].pinData[pinIDtoCheck] === undefined) {

View File

@ -51,12 +51,12 @@ export class Page {
loop: true, loop: true,
originX: -1, originX: -1,
originY: -1, originY: -1,
pins: [] pinDefinitions: []
}; };
// blank slate canvas data // blank slate canvas data
this.projectData = { this.projectData = {
currentFrame: 0, currentFrame: 0,
currentlySelectedPin: 0, currentlySelectedPin: -1,
height: 0, height: 0,
heightRatio: 0, heightRatio: 0,
width: 0, width: 0,
@ -206,7 +206,7 @@ export class Page {
} }
private ExportData() { private ExportData() {
this.pinHandler.UpdateAnimationPinNames(); this.pinHandler.UpdateAnimationPinDefinitions();
if (this.ProjectHasNeccesaryData()) { if (this.ProjectHasNeccesaryData()) {
const zip = new JSZip(); const zip = new JSZip();
@ -263,16 +263,16 @@ export class Page {
let passPinData: boolean = true; let passPinData: boolean = true;
for (let f = 0; f < this.animationData.frames.length; f++) { for (let f = 0; f < this.animationData.frames.length; f++) {
const errorOnFrame: boolean = false; const errorOnFrame: boolean = false;
if (this.animationData.pins !== undefined) { if (this.animationData.pinDefinitions !== undefined) {
for (let p = 0; p < this.animationData.pins.length; p++) { for (let p = 0; p < this.animationData.pinDefinitions.length; p++) {
if (this.animationData.pins[p] !== undefined) { if (this.animationData.pinDefinitions[p] !== undefined) {
const pinIDtoCheck = this.animationData.pins[p].id; const pinIDtoCheck = this.animationData.pinDefinitions[p].id;
// console.log('checking frame ' + f + ' for pinID ' + this.animationData.pins[p].name); // console.log('checking frame ' + f + ' for pinID ' + this.animationData.pins[p].name);
if (this.animationData.frames[f].pinData[pinIDtoCheck] === undefined) { if (this.animationData.frames[f].pinData[pinIDtoCheck] === undefined) {
if (!errorOnFrame) { if (!errorOnFrame) {
pinDataErrorString += f + ' :\n'; pinDataErrorString += f + ' :\n';
} }
pinDataErrorString += ' Pin: ' + this.animationData.pins[p].name + '\n'; pinDataErrorString += ' Pin: ' + this.animationData.pinDefinitions[p].name + '\n';
passPinData = false; passPinData = false;
} }
} }
@ -299,7 +299,6 @@ export class Page {
private AddPinButtonPressed = () => { private AddPinButtonPressed = () => {
this.pinHandler.AddNewPin(); this.pinHandler.AddNewPin();
this.pinHandler.pins += 1;
}; };
private handleFileSelect = async (event: DragEvent) => { private handleFileSelect = async (event: DragEvent) => {
@ -336,7 +335,7 @@ export class Page {
private ResetProgram = () => { private ResetProgram = () => {
// defining blank slate animation data // defining blank slate animation data
this.animationData.pins = []; this.animationData.pinDefinitions = [];
this.animationData.originX = null; this.animationData.originX = null;
this.animationData.originY = null; this.animationData.originY = null;
this.animationData.frameRate = 30; this.animationData.frameRate = 30;

View File

@ -1,9 +1,8 @@
import { IAnimationData } from './Interfaces/IAnimationData'; import { IAnimationData } from './Interfaces/IAnimationData';
import { IPin } from './Interfaces/IPin'; import { IPinDefinition } from './Interfaces/IPinDefinition';
import { IProjectData } from './Interfaces/IProjectData'; import { IProjectData } from './Interfaces/IProjectData';
export class PinHandler { export class PinHandler {
public pins: number = 1;
private pinSettingsDiv: HTMLElement; private pinSettingsDiv: HTMLElement;
private pinContainer: HTMLElement; private pinContainer: HTMLElement;
private allPinContainers: HTMLElement[]; private allPinContainers: HTMLElement[];
@ -26,13 +25,12 @@ export class PinHandler {
this.originPin = originPin; this.originPin = originPin;
this.originPin.classList.add('pinButtonContainer'); this.originPin.classList.add('pinButtonContainer');
// add origin click behaviour // add origin click behaviour
this.originPin.id = 'pinID_0';
this.originPin.addEventListener('click', () => { this.originPin.addEventListener('click', () => {
this.projectData.currentlySelectedPin = 0; this.projectData.currentlySelectedPin = -1;
this.UpdatePinBoxStatus(); this.UpdatePinBoxStatus();
}); });
// put origin into pincontainer array // put origin into pincontainer array
this.allPinContainers = [ originPin ]; this.allPinContainers = [];
} }
public UpdatePinBoxStatus = () => { public UpdatePinBoxStatus = () => {
@ -57,26 +55,26 @@ export class PinHandler {
if (this.animationData.originX === null || this.animationData.originY === null) { if (this.animationData.originX === null || this.animationData.originY === null) {
this.originPin.classList.add('warning'); this.originPin.classList.add('warning');
} }
if (this.projectData.currentlySelectedPin === 0) { if (this.projectData.currentlySelectedPin === -1) {
this.originPin.classList.add('selected'); this.originPin.classList.add('selected');
} }
}; };
public UpdateAnimationPinNames = () => { public UpdateAnimationPinDefinitions = () => {
const animationPinData: IPin[] = []; const animationPinData: IPinDefinition[] = [];
for (let i = 1; i < this.allPinContainers.length; i++) { for (let i = 0; i < this.allPinContainers.length; i++) {
const pinName: string = this.GetPinNameFromDiv(this.allPinContainers[i]); const pinName: string = this.GetPinNameFromDiv(this.allPinContainers[i]);
// console.log('new pin name = ' + pinName); // console.log('new pin name = ' + pinName);
if (pinName !== null && pinName !== undefined) { if (pinName !== null && pinName !== undefined) {
const newPinData: IPin = { const newPinData: IPinDefinition = {
id: this.GetPinNumberFromID(this.allPinContainers[i].id), id: this.GetPinNumberFromID(this.allPinContainers[i].id),
name: pinName name: pinName
}; };
animationPinData.push(newPinData); animationPinData.push(newPinData);
} }
} }
this.animationData.pins = animationPinData; this.animationData.pinDefinitions = animationPinData;
// console.log('updated animationPinData to ' + animationPinData); console.log(animationPinData);
}; };
public RemoveAllPins = () => { public RemoveAllPins = () => {
@ -87,7 +85,7 @@ export class PinHandler {
} }
this.allPinContainers.splice(1, this.allPinContainers.length - 1); this.allPinContainers.splice(1, this.allPinContainers.length - 1);
this.UpdatePinBoxStatus(); this.UpdatePinBoxStatus();
this.UpdateAnimationPinNames(); this.UpdateAnimationPinDefinitions();
}; };
public GetAvailablePins = (): number[] => { public GetAvailablePins = (): number[] => {
@ -116,19 +114,21 @@ export class PinHandler {
const newDiv = document.createElement('div'); const newDiv = document.createElement('div');
this.allPinContainers.push(newDiv); this.allPinContainers.push(newDiv);
let newPinIDString = this.animationData.pinDefinitions.length.toString();
this.pinContainer.appendChild(newDiv); this.pinContainer.appendChild(newDiv);
newDiv.id = 'pinID_' + this.pins.toString(); newDiv.id = 'pinID_' + newPinIDString;
newDiv.className = 'pinButtonContainer'; newDiv.className = 'pinButtonContainer';
// text input field for pin name // text input field for pin name
const newNameInput = document.createElement('input'); const newNameInput = document.createElement('input');
newNameInput.id = 'nameInput_' + this.pins.toString(); newNameInput.id = 'nameInput_' + newPinIDString;
newDiv.addEventListener('click', () => { newDiv.addEventListener('click', () => {
this.SelectPin(newDiv); this.SelectPin(newDiv);
}); });
newDiv.appendChild(newNameInput); newDiv.appendChild(newNameInput);
newNameInput.value = 'PinName_' + this.pins.toString(); newNameInput.value = 'PinName_' + newPinIDString;
newNameInput.addEventListener('focusout', () => { newNameInput.addEventListener('focusout', () => {
this.UpdateAnimationPinNames(); this.UpdateAnimationPinDefinitions();
}); });
// button to remove pin // button to remove pin
const removePinButton = document.createElement('div'); const removePinButton = document.createElement('div');
@ -146,21 +146,21 @@ export class PinHandler {
indexToDelete = i; indexToDelete = i;
} }
} }
if (indexToDelete !== 0) { if (indexToDelete !== -1) {
this.allPinContainers.splice(indexToDelete, 1); this.allPinContainers.splice(indexToDelete, 1);
} }
// remove data associated with that id from all frames // remove data associated with that id from all frames
this.RemovePinDataForID(idNumber); this.RemovePinDataForID(idNumber);
// remove the div itself // remove the div itself
newDiv.remove(); newDiv.remove();
this.UpdateAnimationPinNames(); //this.UpdateAnimationPinDefinitions();
//reset to origin //reset to origin
this.SelectOriginPin(); this.SelectOriginPin();
}); });
// break // break
this.UpdateAnimationPinNames(); this.UpdateAnimationPinDefinitions();
this.UpdatePinBoxStatus(); this.UpdatePinBoxStatus();
}; };
@ -176,27 +176,48 @@ export class PinHandler {
this.projectData.currentlySelectedPin = this.GetPinNumberFromID(pinDiv.id); this.projectData.currentlySelectedPin = this.GetPinNumberFromID(pinDiv.id);
// console.log('selected pin ' + this.projectData.currentlySelectedPin); // console.log('selected pin ' + this.projectData.currentlySelectedPin);
this.UpdatePinBoxStatus(); this.UpdatePinBoxStatus();
this.UpdateAnimationPinNames(); this.UpdateAnimationPinDefinitions();
}; };
private SelectOriginPin = () => { private SelectOriginPin = () => {
this.projectData.currentlySelectedPin = 0; this.projectData.currentlySelectedPin = -1;
this.UpdatePinBoxStatus(); this.UpdatePinBoxStatus();
}; };
private RemovePinDataForID = (pinID: number) => { private RemovePinDataForID = (pinID: number) => {
// check for matching id in pin list and remove // check for matching id in pin list and remove
let indexToDelete = -1;
let deleted: boolean = false; let deleted: boolean = false;
for (let i = 0; i < this.animationData.pins.length; i++) { for (let i = 0; i < this.animationData.pinDefinitions.length; i++) {
// console.log('checking if ' + this.animationData.pins[i].id.toString + ' === ' + pinID.toString()); // console.log('checking if ' + this.animationData.pins[i].id.toString + ' === ' + pinID.toString());
if (this.animationData.pins[i].id === pinID) { if (this.animationData.pinDefinitions[i].id === pinID) {
delete this.animationData.pins[i]; indexToDelete = i;
} }
// console.log('deleting pinID ' + pinID); // console.log('deleting pinID ' + pinID);
deleted = true; deleted = true;
} }
if (indexToDelete == -1) {
return;
}
console.log('deleting: ' + indexToDelete);
let removedPinDefinition = this.animationData.pinDefinitions[indexToDelete];
console.log('removing:');
console.log(removedPinDefinition);
this.animationData.pinDefinitions.splice(indexToDelete, 1);
for (let i = this.animationData.pinDefinitions.length - 1; i >= 0; i--) {
let pinDefinition = this.animationData.pinDefinitions[i];
if (pinDefinition.id > removedPinDefinition.id) {
let div = document.getElementById('pinID_' + pinDefinition.id);
div!.id = 'pinID_' + (pinDefinition.id - 1);
pinDefinition.id -= 1;
}
}
console.log(this.animationData.pinDefinitions);
if (!deleted) { if (!deleted) {
// console.log('failed to find pinID ' + pinID + ' in list of pins'); // console.log('failed to find pinID ' + pinID + ' in list of pins');
} }
@ -204,7 +225,8 @@ export class PinHandler {
// delete pin data from each frame // delete pin data from each frame
for (let f = 0; f < this.animationData.frames.length; f++) { for (let f = 0; f < this.animationData.frames.length; f++) {
if (this.animationData.frames[f].pinData[pinID] !== undefined) { if (this.animationData.frames[f].pinData[pinID] !== undefined) {
delete this.animationData.frames[f].pinData[pinID]; //delete this.animationData.frames[f].pinData[pinID];
this.animationData.frames[f].pinData.splice(pinID, 1);
// console.log('deleting pinID ' + pinID + ' data from frame ' + f); // console.log('deleting pinID ' + pinID + ' data from frame ' + f);
} else { } else {
// console.log('tried to delete pinID ' + pinID + ' data from frame ' + f + ' but it doesnt exist'); // console.log('tried to delete pinID ' + pinID + ' data from frame ' + f + ' but it doesnt exist');