Skip to content

Commit

Permalink
feat: Translate layer flags into a map
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCyberRonin committed Oct 9, 2023
1 parent 2d58041 commit 0fa1f1c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
40 changes: 39 additions & 1 deletion Aseprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ class Aseprite {
this.tilesets = [];
}

//#region Static Constants

/**
* Map of the possible flag values for a Layer
*/
static LAYER_FLAG_MAP = {
visible: 0b1,
editable: 0b10,
lockMovement: 0b100,
background: 0b1000,
preferLinkedCels: 0b10000,
collapsedGroup: 0b100000,
reference: 0b1000000
};

//#endregion Static Constants

/**
* Reads the next byte (8-bit unsigned) value in the buffer
*
Expand Down Expand Up @@ -212,6 +229,27 @@ class Aseprite {
this._offset += numBytes;
}

/**
* Translates the listed flags for a layer into true/false values
* dictating whether or not that flag is "on"(true) or "off"(false)
*
* @private
* @param {object} flagMap Object of flags and their binary value
* @param {number} flagValue Value of the layer flags
* @returns {object} Map of the flags and if they are true or false
*/
#translateFlags(flagValue) {
// Create an object to put the flags and their "toggle" (true/false)
const translatedFlagMap = {};
// Iterate through the flags and their binary value, use bitwise op
// to see if the flag is present in the "layer flags" and add the
// flag to the map with the accompanying "toggle"
for( const flag in Aseprite.LAYER_FLAG_MAP) {
translatedFlagMap[flag] = (flagValue & Aseprite.LAYER_FLAG_MAP[flag]) == Aseprite.LAYER_FLAG_MAP[flag];
}
return translatedFlagMap;
}

/**
* Reads the 128-byte header of an Aseprite file and stores the information
*
Expand Down Expand Up @@ -501,7 +539,7 @@ class Aseprite {
*/
readLayerChunk() {
const layer = {}
layer.flags = this.readNextWord();
layer.flags = this.#translateFlags(this.readNextWord());
layer.type = this.readNextWord();
layer.layerChildLevel = this.readNextWord();
this.skipBytes(4);
Expand Down
11 changes: 10 additions & 1 deletion typings/Aseprite.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ declare namespace Aseprite {
color: string;
}
export interface Layer {
flags: number;
flags: Aseprite.LayerFlags;
type: number;
layerChildLevel: number;
blendMode: number;
Expand Down Expand Up @@ -125,4 +125,13 @@ declare namespace Aseprite {
fGamma: number;
icc?: Buffer;
}
export interface LayerFlags {
visible: boolean;
editable: boolean;
lockMovement: boolean;
background: boolean;
preferLinkedCels: boolean;
collapsedGroup: boolean;
reference: boolean;
}
}

0 comments on commit 0fa1f1c

Please sign in to comment.