Skip to content

Commit

Permalink
implement module flipping
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoDJ committed Feb 4, 2022
1 parent 765425a commit 865bc79
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/common/MappingGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,34 @@ export interface MappingParams {
mapStart: MapStart;
}

export interface MappingEntry {
position: number;
flipped: boolean;
}

export class MappingGenerator {

constructor(protected mappingParams: MappingParams) { }

// TODO: implement rotation of frames
async generateMapping(frameWidth: number, frameHeight: number): Promise<number[]> {
async generateMapping(frameWidth: number, frameHeight: number): Promise<MappingEntry[]> {
const snake = this.mappingParams.mapType == 'snake';
const vertical = this.mappingParams.mapOrientation == 'vert';
const startingLeft = this.mappingParams.mapStart == 'tl' || this.mappingParams.mapStart == 'bl';
const startingTop = this.mappingParams.mapStart == 'tl' || this.mappingParams.mapStart == 'tr';

// ‧͙⁺˚* Magic, do not touch! *˚⁺‧͙

// for vertical orientation, invert the meaning of X and Y
// for vertical orientation, the meaning of X and Y is swapped
const width = vertical ? frameHeight : frameWidth;
const height = vertical ? frameWidth : frameHeight;

let mapping = [];
let mapping: MappingEntry[] = [];
for (let i = 0; i < width * height; i++) {
let x: number, y: number;

// for vertical orientation, invert the meaning of X and Y
// for vertical orientation, the meaning of X and Y is swapped
// booleans related if the
let flipX = vertical ? !startingTop : !startingLeft;
let flipY = vertical ? !startingLeft : !startingTop;

Expand All @@ -56,13 +62,18 @@ export class MappingGenerator {
x = width - (i % width) - 1;

let newPos: number;
// for vertical orientation, invert the meaning of X and Y
// for vertical orientation, the meaning of X and Y is swapped
if (!vertical)
newPos = y * width + x;
else
newPos = x * height + y;

mapping[newPos] = i;
let flipModule = false;
if (this.mappingParams.mapFlip && this.mappingParams.mapFlip != 'none') {
flipModule = (y % 2 == 0) != (this.mappingParams.mapFlip == 'even');
}

mapping[newPos] = { position: i, flipped: flipModule };
}
return mapping;
}
Expand Down
19 changes: 17 additions & 2 deletions src/converters/ModuleMappingConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,23 @@ export class ModuleMappingConverter {

// reorder the frames
let tempFrames = [...frameArr.frames]; // clone array, but leave object references intact
mapping.forEach((mapPos, i) => {
frameArr.frames[i] = tempFrames[mapPos];
mapping.forEach((mapping, i) => {
frameArr.frames[i] = tempFrames[mapping.position];

// rotate frame by 180° if needed
if (mapping.flipped) {
let frame = frameArr.frames[i];
let tempBuf = Buffer.from(frame.buffer);
for (let y = 0; y < frame.height; y++) {
for (let x = 0; x < frame.width; x++) {
let flippedX = frame.width - x - 1;
let flippedY = frame.height - y - 1;
let toPos = flippedY * frame.width + flippedX;
let fromPos = y * frame.width + x;
tempBuf.copy(frame.buffer, toPos * 3, fromPos * 3, (fromPos + 1) * 3);
}
}
}
});

// debug mapping
Expand Down
2 changes: 1 addition & 1 deletion src/converters/PixelMappingConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class PixelMappingConverter {
frameArr.frames.forEach(frame => {
let tempBuf = Buffer.from(frame.buffer);
for (let i = 0; i < frame.width * frame.height; i++) {
tempBuf.copy(frame.buffer, mapping[i] * 3, i * 3, (i * 3) + 3);
tempBuf.copy(frame.buffer, mapping[i].position * 3, i * 3, (i * 3) + 3);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export class FrameMapComponent extends Rete.Component {

const mapFlipOptions: SelectionOption[] = [
{ value: 'none', name: 'No flipping' },
{ value: 'even', name: 'Even Lines Flipped (Not implemented yet)' },
{ value: 'odd', name: 'Odd Lines Flipped (Not implemented yet)' },
{ value: 'even', name: 'Even Lines Flipped' },
{ value: 'odd', name: 'Odd Lines Flipped' },
];
const ctrlMapFlip = new SelectionControl(this.editor, 'mapFlipCtrl', "Mapping Flip", mapFlipOptions);

Expand Down

0 comments on commit 865bc79

Please sign in to comment.