Skip to content

Commit

Permalink
fix: handling of enums with value 0 (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
timkurvers committed Apr 10, 2024
1 parent fd7b1ac commit 3cf2e4d
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
8 changes: 2 additions & 6 deletions src/ui/components/abstract/FramePointType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ enum FramePointType {
BOTTOMRIGHT = 8,
}

export const stringToPointType = (string?: string) => {
if (!string) return undefined;
return FramePointType[string.toUpperCase() as keyof typeof FramePointType];
};

export const FramePointTypeSide = {
const FramePointTypeSide = {
CENTERX: [
FramePointType.TOP,
FramePointType.CENTER,
Expand Down Expand Up @@ -49,3 +44,4 @@ export const FramePointTypeSide = {
};

export default FramePointType;
export { FramePointTypeSide };
14 changes: 6 additions & 8 deletions src/ui/components/abstract/LayoutFrame.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ScriptRegion from './ScriptRegion';
import XMLNode from '../../XMLNode';
import { stringToFramePointType } from '../../utils';
import {
EPSILON1,
EPSILON2,
Expand All @@ -18,10 +19,7 @@ import {
} from '../../../utils';

import FramePoint from './FramePoint';
import FramePointType, {
FramePointTypeSide,
stringToPointType,
} from './FramePointType';
import FramePointType, { FramePointTypeSide } from './FramePointType';

class FrameNode extends LinkedListNode {
frame: LayoutFrame;
Expand Down Expand Up @@ -397,16 +395,16 @@ class LayoutFrame {
const relativePointValue = child.attributes.get('relativePoint');
const relativeValue = child.attributes.get('relativeTo');

const pointType = stringToPointType(pointValue);
const pointType = stringToFramePointType(pointValue);
let relativePointType = pointType;
if (!pointType) {
if (pointType === undefined) {
// TODO: Error handling
continue;
}

if (relativePointValue) {
relativePointType = stringToPointType(relativePointValue);
if (!relativePointType) {
relativePointType = stringToFramePointType(relativePointValue);
if (relativePointType === undefined) {
// TODO: Error handling
continue;
}
Expand Down
9 changes: 4 additions & 5 deletions src/ui/components/simple/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { Rect } from '../../../math';
import {
stringToDrawLayerType,
stringToStrataType,
stringToFrameStrataType,
} from '../../utils';

import FrameFlag from './FrameFlag';
Expand Down Expand Up @@ -308,8 +308,8 @@ class Frame extends ScriptRegion {
}

if (frameStrata) {
const strataType = stringToStrataType(frameStrata);
if (strataType) {
const strataType = stringToFrameStrataType(frameStrata);
if (strataType !== undefined) {
this.setFrameStrataType(strataType);
} else {
// TODO: Error handling
Expand Down Expand Up @@ -371,8 +371,7 @@ class Frame extends ScriptRegion {

const level = layer.attributes.get('level');

// TODO: Case sensitivity
const drawLayerType = stringToDrawLayerType(level) || DrawLayerType.ARTWORK;
const drawLayerType = stringToDrawLayerType(level) ?? DrawLayerType.ARTWORK;

for (const layerChild of layer.children) {
const iname = layerChild.name.toLowerCase();
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/simple/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ class Texture extends Region {

if (alphaMode) {
const blendMode = stringToBlendMode(alphaMode);
if (blendMode) {
if (blendMode !== undefined) {
this.setBlendMode(blendMode);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/ui/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BlendMode } from '../gfx/types';

import DrawLayerType from './DrawLayerType';
import FramePointType from './components/abstract/FramePointType';
import FrameStrataType from './components/abstract/FrameStrataType';

export const stringToBlendMode = (string?: string) => {
Expand All @@ -13,7 +14,12 @@ export const stringToDrawLayerType = (string?: string) => {
return DrawLayerType[string?.toUpperCase() as keyof typeof DrawLayerType];
};

export const stringToStrataType = (string?: string) => {
export const stringToFramePointType = (string?: string) => {
if (!string) return undefined;
return FramePointType[string.toUpperCase() as keyof typeof FramePointType];
};

export const stringToFrameStrataType = (string?: string) => {
if (!string) return undefined;
return FrameStrataType[string?.toUpperCase() as keyof typeof FrameStrataType];
};

0 comments on commit 3cf2e4d

Please sign in to comment.