AnimationTool/app/pin_handler.ts

226 lines
7.3 KiB
TypeScript
Raw Normal View History

import { IAnimationData } from './Interfaces/IAnimationData';
2019-12-12 21:17:32 +00:00
import { IPinDefinition } from './Interfaces/IPinDefinition';
2019-10-07 17:51:43 +00:00
import { IProjectData } from './Interfaces/IProjectData';
2019-09-28 17:59:50 +00:00
export class PinHandler {
private pinSettingsDiv: HTMLElement;
private pinContainer: HTMLElement;
private allPinContainers: HTMLElement[];
private projectData: IProjectData;
private animationData: IAnimationData;
2019-10-05 00:03:48 +00:00
private originPin: HTMLElement;
2019-09-28 17:59:50 +00:00
constructor(
pinSettingsDiv: HTMLElement,
pinContainer: HTMLElement,
originPin: HTMLElement,
projectData: IProjectData,
animationData: IAnimationData
) {
2019-09-28 17:59:50 +00:00
this.pinSettingsDiv = pinSettingsDiv;
this.pinContainer = pinContainer;
this.projectData = projectData;
this.animationData = animationData;
2019-10-05 00:03:48 +00:00
this.originPin = originPin;
this.originPin.classList.add('pinButtonContainer');
// add origin click behaviour
2019-10-05 00:03:48 +00:00
this.originPin.addEventListener('click', () => {
2019-12-12 21:17:32 +00:00
this.projectData.currentlySelectedPin = -1;
2019-10-05 00:38:18 +00:00
this.UpdatePinBoxStatus();
});
// put origin into pincontainer array
2019-12-12 21:17:32 +00:00
this.allPinContainers = [];
2019-09-28 17:59:50 +00:00
}
2019-10-05 00:38:18 +00:00
public UpdatePinBoxStatus = () => {
for (let i = 0; i < this.allPinContainers.length; i++) {
const pinDiv = this.allPinContainers[i];
pinDiv.classList.remove('selected', 'warning');
2019-10-08 22:15:49 +00:00
if (this.GetPinNumberFromID(this.allPinContainers[i].id) === this.projectData.currentlySelectedPin) {
this.allPinContainers[i].classList.add('selected');
}
// check frames for missing pin info
const pinNumber = this.GetPinNumberFromID(pinDiv.id);
for (let f = 0; f < this.animationData.frames.length; f++) {
if (this.animationData.frames[f] !== undefined) {
2019-10-09 01:50:03 +00:00
if (this.animationData.frames[f].pinData[pinNumber] === undefined) {
2019-10-08 22:15:49 +00:00
pinDiv.classList.add('warning');
break;
2019-10-05 00:38:18 +00:00
}
}
}
}
if (this.animationData.originX === null || this.animationData.originY === null) {
this.originPin.classList.add('warning');
}
2019-12-12 21:17:32 +00:00
if (this.projectData.currentlySelectedPin === -1) {
2019-10-05 00:38:18 +00:00
this.originPin.classList.add('selected');
}
};
2019-12-12 21:17:32 +00:00
public UpdateAnimationPinDefinitions = () => {
const animationPinData: IPinDefinition[] = [];
for (let i = 0; i < this.allPinContainers.length; i++) {
2019-10-03 23:33:32 +00:00
const pinName: string = this.GetPinNameFromDiv(this.allPinContainers[i]);
if (pinName !== null && pinName !== undefined) {
2019-12-12 21:17:32 +00:00
const newPinData: IPinDefinition = {
2019-10-03 23:33:32 +00:00
id: this.GetPinNumberFromID(this.allPinContainers[i].id),
name: pinName
};
animationPinData.push(newPinData);
}
2019-09-28 17:59:50 +00:00
}
2019-12-12 21:17:32 +00:00
this.animationData.pinDefinitions = animationPinData;
};
2019-09-28 17:59:50 +00:00
public RemoveAllPins = () => {
for (let i = 1; i < this.allPinContainers.length; i++) {
2019-10-03 23:33:32 +00:00
const pinID: number = this.GetPinNumberFromID(this.allPinContainers[i].id);
this.RemovePinDataForID(pinID);
this.allPinContainers[i].remove();
}
2019-10-05 00:38:18 +00:00
this.allPinContainers.splice(1, this.allPinContainers.length - 1);
this.UpdatePinBoxStatus();
2019-12-12 21:17:32 +00:00
this.UpdateAnimationPinDefinitions();
};
2019-10-03 23:33:32 +00:00
public GetAvailablePins = (): number[] => {
const availablePins: number[] = [];
for (let i = 1; i < this.allPinContainers.length; i++) {
const pinID: number = this.GetPinNumberFromID(this.allPinContainers[i].id);
availablePins.push(pinID);
}
return availablePins;
};
public GetPinName = (pinID: number): string => {
for (let p = 0; p < this.allPinContainers.length; p++) {
const pinContainer = this.allPinContainers[p];
if (this.GetPinNumberFromID(pinContainer.id) === pinID) {
const pinName: string = this.GetPinNameFromDiv(pinContainer);
return pinName;
}
}
return 'failed_to_return_pin_name_for_pin_' + pinID.toString();
};
2019-10-07 17:51:43 +00:00
public AddNewPin = () => {
// create info window div and append to pincontainer
const newDiv = document.createElement('div');
this.allPinContainers.push(newDiv);
2019-12-12 21:17:32 +00:00
let newPinIDString = this.animationData.pinDefinitions.length.toString();
this.pinContainer.appendChild(newDiv);
2019-12-12 21:17:32 +00:00
newDiv.id = 'pinID_' + newPinIDString;
newDiv.className = 'pinButtonContainer';
// text input field for pin name
const newNameInput = document.createElement('input');
2019-12-12 21:17:32 +00:00
newNameInput.id = 'nameInput_' + newPinIDString;
2019-10-08 19:53:58 +00:00
newDiv.addEventListener('click', () => {
this.SelectPin(newDiv);
});
newDiv.appendChild(newNameInput);
2019-12-12 21:17:32 +00:00
newNameInput.value = 'PinName_' + newPinIDString;
2019-10-04 01:02:08 +00:00
newNameInput.addEventListener('focusout', () => {
2019-12-12 21:17:32 +00:00
this.UpdateAnimationPinDefinitions();
2019-10-04 01:02:08 +00:00
});
// button to remove pin
2019-10-08 22:49:06 +00:00
const removePinButton = document.createElement('div');
newDiv.appendChild(removePinButton);
removePinButton.textContent = 'X';
removePinButton.className = 'removeButton';
2019-10-08 22:15:49 +00:00
removePinButton.addEventListener('click', (event: MouseEvent) => {
event.stopPropagation();
// get ID number for this div
2019-10-07 17:51:43 +00:00
const idNumber = this.GetPinNumberFromID(newDiv.id);
let indexToDelete: number = 0;
// that id from allPinContainers
for (let i = 0; i < this.allPinContainers.length; i++) {
2019-10-07 17:51:43 +00:00
if (this.GetPinNumberFromID(this.allPinContainers[i].id) === idNumber) {
indexToDelete = i;
}
}
2019-12-12 21:17:32 +00:00
if (indexToDelete !== -1) {
this.allPinContainers.splice(indexToDelete, 1);
}
// remove data associated with that id from all frames
this.RemovePinDataForID(idNumber);
// remove the div itself
newDiv.remove();
2019-12-12 21:17:32 +00:00
//this.UpdateAnimationPinDefinitions();
2019-10-08 22:15:49 +00:00
//reset to origin
this.SelectOriginPin();
});
// break
2019-10-08 19:53:58 +00:00
2019-12-12 21:17:32 +00:00
this.UpdateAnimationPinDefinitions();
2019-10-05 02:31:21 +00:00
this.UpdatePinBoxStatus();
};
2019-10-07 17:51:43 +00:00
private GetPinNameFromDiv = (pinElement: HTMLElement): string => {
return pinElement.getElementsByTagName('input')[0].value;
};
private GetPinNumberFromID = (id: string): number => {
return parseInt(id.split('_')[1], 10);
};
2019-10-04 23:38:11 +00:00
private SelectPin = (pinDiv: HTMLElement) => {
2019-10-07 17:51:43 +00:00
this.projectData.currentlySelectedPin = this.GetPinNumberFromID(pinDiv.id);
2019-10-05 00:38:18 +00:00
this.UpdatePinBoxStatus();
2019-12-12 21:17:32 +00:00
this.UpdateAnimationPinDefinitions();
2019-10-04 23:38:11 +00:00
};
2019-10-08 22:15:49 +00:00
private SelectOriginPin = () => {
2019-12-12 21:17:32 +00:00
this.projectData.currentlySelectedPin = -1;
2019-10-08 22:15:49 +00:00
this.UpdatePinBoxStatus();
};
private RemovePinDataForID = (pinID: number) => {
// check for matching id in pin list and remove
2019-12-12 21:17:32 +00:00
let indexToDelete = -1;
let deleted: boolean = false;
2019-12-12 21:17:32 +00:00
for (let i = 0; i < this.animationData.pinDefinitions.length; i++) {
if (this.animationData.pinDefinitions[i].id === pinID) {
indexToDelete = i;
}
deleted = true;
}
2019-12-12 21:17:32 +00:00
if (indexToDelete == -1) {
return;
}
let removedPinDefinition = this.animationData.pinDefinitions[indexToDelete];
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;
}
}
if (!deleted) {
2019-10-07 17:51:43 +00:00
// console.log('failed to find pinID ' + pinID + ' in list of pins');
}
// delete pin data from each frame
for (let f = 0; f < this.animationData.frames.length; f++) {
2019-10-09 01:50:03 +00:00
if (this.animationData.frames[f].pinData[pinID] !== undefined) {
2019-12-12 21:17:32 +00:00
//delete this.animationData.frames[f].pinData[pinID];
this.animationData.frames[f].pinData.splice(pinID, 1);
2019-10-07 17:51:43 +00:00
// console.log('deleting pinID ' + pinID + ' data from frame ' + f);
} else {
2019-10-07 17:51:43 +00:00
// console.log('tried to delete pinID ' + pinID + ' data from frame ' + f + ' but it doesnt exist');
}
}
};
2019-09-28 17:59:50 +00:00
}