src/icon.mjs
-
import { VYI } from './vyi.mjs';
-import { Frame } from './frame.mjs';
+
+
+
icon.mjs
+
+
+
+
+
+
+
+
+
+ import { VYI } from './vyi.mjs';
+import { Frame } from './frame.mjs';
+/**
+ * @public
+ */
export class Icon {
/**
- * An array of Icon's that are state of this icon.
+ * An array of Icon's that are state of this icon.
* @private
* @type {Array}
*/
states = [];
/**
- * An arary of Frame's that are the frames of this icon.
+ * An arary of Frame's that are the frames of this icon.
* @private
* @type {Array}
*/
@@ -82,7 +87,7 @@
* @private
* @type {string}
*/
- name = '';
+ name = '';
/**
* The icon that owns this icon. This means this icon is state.
* @private
@@ -111,8 +116,8 @@
* @returns {string} The generated UUID.
*/
static generateID(pVYI) {
- const genID = () => {
- return Math.floor(Math.random() * 0xFFFFFFFF).toString(16).padStart(8, '0');
+ const genID = () => {
+ return Math.floor(Math.random() * 0xFFFFFFFF).toString(16).padStart(8, '0');
}
// Generate a random number in the range of 0 to 0xFFFFFFFF (8 hex digits) and convert to hex
let id = genID();
@@ -150,6 +155,22 @@
getID() {
return this.id;
}
+ /**
+ * Gets the vyi this icon belongs to.
+ *
+ * @returns {VYI} The vyi this icon belongs to.
+ */
+ getVyi() {
+ return this.vyi;
+ }
+ /**
+ * Gets the icon this state belongs to. If this icon is not a state, it will return undefined.
+ *
+ * @returns {Icon|undefined} The icon this state belongs to.
+ */
+ getParent() {
+ return this.parent;
+ }
/**
* parses through the icon data and adds data to this icon.
* @param {Object} pIconData - The icon data that is used to build this icon.
@@ -177,7 +198,7 @@
if (Array.isArray(frameArray)) {
// If the frame array has data then we need to store it.
if (frameArray.length) {
- frameArray.forEach((pFrame) => {
+ frameArray.forEach((pFrame) => {
// pFrame is an array holding the datalURL and frameDelay of the frame
this.addFrame(pFrame);
});
@@ -187,9 +208,9 @@
if (Array.isArray(stateArray)) {
// If the state array has data then we need to store it.
if (stateArray.length) {
- stateArray.forEach((pState) => {
+ stateArray.forEach((pState) => {
// Here we create a icon with aggregated data because the state data is not enough to make it an icon.
- // We do this because a state is basically an icon, but it just "inherits" alot of the data. But this makes it easier to manage if we treat it internally as an icon.
+ // We do this because a state is basically an icon, but it just "inherits" alot of the data. But this makes it easier to manage if we treat it internally as an icon.
const aggregatedIconData = [];
// iconName
aggregatedIconData[0] = pState[0];
@@ -215,10 +236,10 @@
* @returns {self} This icon instance.
*/
setSize(pWidth, pHeight) {
- if (typeof(pWidth) === 'number') {
+ if (typeof(pWidth) === 'number') {
this.width = pWidth;
}
- if (typeof(pHeight) === 'number') {
+ if (typeof(pHeight) === 'number') {
this.height = pHeight;
}
}
@@ -236,10 +257,10 @@
*/
setDataURL(pDataURL) {
if (pDataURL) {
- if (typeof(pDataURL) === 'string') {
+ if (typeof(pDataURL) === 'string') {
this.dataURL = pDataURL;
} else {
- VYI.logger.prefix('VYI-module').error('Invalid data url type!');
+ VYI.logger.prefix('VYI-module').error('Invalid data url type!');
}
}
return this;
@@ -258,10 +279,10 @@
*/
setDelay(pDelay) {
if (pDelay) {
- if (typeof(pDelay) === 'number') {
+ if (typeof(pDelay) === 'number') {
this.delay = pDelay;
} else {
- VYI.logger.prefix('VYI-module').error('Invalid delay type!');
+ VYI.logger.prefix('VYI-module').error('Invalid delay type!');
}
}
return this;
@@ -279,11 +300,11 @@
* @returns {self} This icon instance.
*/
rename(pName) {
- if (pName || pName === '') {
- if (typeof(pName) === 'string') {
+ if (pName || pName === '') {
+ if (typeof(pName) === 'string') {
this.name = pName;
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid type for pName!');
+ VYI.logger.prefix('VYI-Module').error('Invalid type for pName!');
}
}
return this;
@@ -302,13 +323,13 @@
*/
setAllFrameDelays(pDelay) {
if (pDelay) {
- if (typeof(pDelay) === 'number') {
+ if (typeof(pDelay) === 'number') {
this.setDelay(pDelay);
- this.frames.forEach((pFrame) => {
+ this.frames.forEach((pFrame) => {
pFrame.setDelay(pDelay);
});
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid type for pDelay!');
+ VYI.logger.prefix('VYI-Module').error('Invalid type for pDelay!');
}
}
return this;
@@ -339,16 +360,16 @@
if (this.states.includes(pState)) {
index = this.states.indexOf(pState);
// Remove via reference to name
- } else if (typeof(pName) === 'string') {
+ } else if (typeof(pName) === 'string') {
const state = this.getState(pName);
if (state) {
index = this.states.indexOf(state);
}
} else {
- VYI.logger.prefix('VYI-Module').error('Failed to remove state!');
+ VYI.logger.prefix('VYI-Module').error('Failed to remove state!');
return this;
}
- if (typeof(index) === 'number') {
+ if (typeof(index) === 'number') {
// Remove the state
this.states.splice(index, 1);
}
@@ -369,10 +390,10 @@
this.indexFrames();
return frame;
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid frame data passed!');
+ VYI.logger.prefix('VYI-Module').error('Invalid frame data passed!');
}
} else {
- VYI.logger.prefix('VYI-Module').error('No frame data passed!');
+ VYI.logger.prefix('VYI-Module').error('No frame data passed!');
}
}
/**
@@ -389,18 +410,18 @@
index = this.frames.indexOf(pFrame);
// Remove via index passed.
} else if (pIndex || pIndex === 0) {
- if (typeof(pIndex) === 'number') {
+ if (typeof(pIndex) === 'number') {
const frame = this.getFrame(pIndex);
if (frame) {
index = pIndex;
}
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid pIndex type!');
+ VYI.logger.prefix('VYI-Module').error('Invalid pIndex type!');
}
} else {
- VYI.logger.prefix('VYI-Module').error('Failed to remove frame!');
+ VYI.logger.prefix('VYI-Module').error('Failed to remove frame!');
}
- if (typeof(index) === 'number') {
+ if (typeof(index) === 'number') {
// Remove the frame
this.frames.splice(index, 1);
// Re-index frames after a change
@@ -414,20 +435,20 @@
*/
indexFrames() {
// Reorder the frames after removing.
- this.frames.forEach((pFrame, pIndex) => {
+ this.frames.forEach((pFrame, pIndex) => {
pFrame.index = pIndex;
});
}
/**
* Reorders the frame in the animation. The index of the passed frame will be swapped with the frame at pIndex.
- * The "first" frame of the animation is technically this icon's dataURL. So if you are aiming to change the order of this icon and convert it into a frame.
+ * The "first" frame of the animation is technically this icon's dataURL. So if you are aiming to change the order of this icon and convert it into a frame.
* pCurrentIndex must be set to -1 to match this icon.
* @param {number} pCurrentIndex - The current index of the frame.
* @param {number} pIndex - The index the frame will be moving to.
* @returns {self} This icon instance.
*/
reorderFrame(pCurrentIndex, pIndex) {
- if (typeof(pCurrentIndex) === 'number' && typeof(pIndex) === 'number') {
+ if (typeof(pCurrentIndex) === 'number' && typeof(pIndex) === 'number') {
let frameAtIndex;
let currentFrame;
// We check if the current index is -1, if it is then it means we want to treat this icon as a frame. As the icon data and delay of this icon serves
@@ -460,25 +481,25 @@
frameAtIndex.setDataURL(currentFrameDataURL);
frameAtIndex.setDelay(currentFrameDelay);
} else {
- VYI.logger.prefix('VYI-Module').error('There was no frame found at pCurrentIndex, or there was no frame found at pIndex!');
+ VYI.logger.prefix('VYI-Module').error('There was no frame found at pCurrentIndex, or there was no frame found at pIndex!');
}
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid type used!');
+ VYI.logger.prefix('VYI-Module').error('Invalid type used!');
}
return this;
}
/**
* Gets the frame existing at pIndex.
- * Frame 0 will actually be frame "1" in the animation. As this icon will actually be frame 0.
- * If you are trying to get "frame" 1. Then you will need to use the icon's delay and data url. As that is frame 0.
+ * Frame 0 will actually be frame "1" in the animation. As this icon will actually be frame 0.
+ * If you are trying to get "frame" 1. Then you will need to use the icon's delay and data url. As that is frame 0.
* @param {number} pIndex - The index of the frame to get.
* @returns {Frame|undefined} The frame found at pIndex.
*/
getFrame(pIndex) {
- if (typeof(pIndex) === 'number') {
+ if (typeof(pIndex) === 'number') {
return this.frames[pIndex];
} else {
- VYI.logger.prefix('VYI-Module').error('Invalid type used!');
+ VYI.logger.prefix('VYI-Module').error('Invalid type used!');
}
}
/**
@@ -495,7 +516,7 @@
*/
getFramesData() {
const frameDataArray = [];
- this.frames.forEach((pFrame) => {
+ this.frames.forEach((pFrame) => {
frameDataArray.push(pFrame.export());
});
return frameDataArray;
@@ -506,8 +527,8 @@
* @returns {Icon} The state that has the name of pName.
*/
getState(pName) {
- if (typeof(pName) === 'string') {
- for (let i = this.states.length - 1; i >= 0; i--) {
+ if (typeof(pName) === 'string') {
+ for (let i = this.states.length - 1; i >= 0; i--) {
const icon = this.states[i];
// If the icon has the same name, return that icon
if (icon.getName() === pName) {
@@ -515,7 +536,7 @@
}
}
} else {
- VYI.logger.prefix('VYI-module').error('Invalid name type used!');
+ VYI.logger.prefix('VYI-module').error('Invalid name type used!');
}
}
/**
@@ -533,7 +554,7 @@
getStatesData() {
const stateDataArray = [];
// Loop state array to export relevant information.
- this.states.forEach((pState) => {
+ this.states.forEach((pState) => {
stateDataArray.push(pState.exportAsState());
});
return stateDataArray;
@@ -555,13 +576,13 @@
stateData[3] = [];
// Loop frame array to export relevant information.
- this.frames.forEach((pFrame) => {
+ this.frames.forEach((pFrame) => {
stateData[3].push(pFrame.export());
});
return stateData;
}
/**
- * Exports this icon's data into proper vyi format.
+ * Exports this icon's data into proper vyi format.
* @private
* @returns {Array} An array of data related to this icon in the proper vyi format.
*/
@@ -591,19 +612,21 @@
return iconData;
}
}
+
+
+
+
+
-