Skip to content

Commit

Permalink
Merge pull request #185 from HerrZatacke/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
HerrZatacke authored Sep 8, 2024
2 parents 10145e3 + 967bf56 commit 81e953e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gb-printer-web",
"version": "1.42.6",
"version": "1.42.7",
"description": "gb-printer-web",
"scripts": {
"start": "webpack serve --config ./scripts/webpack.dev.js",
Expand Down
13 changes: 13 additions & 0 deletions src/javascript/app/components/Frames/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ function Frames() {
exportJson,
setActiveFrameGroupName,
activeFrameGroup,
convertFormat,
enableDebug,
} = useFrames();

return (
Expand Down Expand Up @@ -84,6 +86,17 @@ function Frames() {
>
{`Export current framegroup (${selectedFrameGroup})`}
</button>
{
enableDebug ? (
<button
type="button"
className="button"
onClick={convertFormat}
>
Convert frames to new format
</button>
) : null
}
</div>
</div>
);
Expand Down
50 changes: 49 additions & 1 deletion src/javascript/app/components/Frames/useFrames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { State } from '../../store/State';
import type { Frame } from '../../../../types/Frame';
import type { ExportJSONAction } from '../../../../types/actions/StorageActions';
import type { ExportTypes } from '../../../consts/exportTypes';
import type { GlobalUpdateAction } from '../../../../types/GlobalUpdateAction';
import { compressAndHashFrame, loadFrameData, saveFrameData } from '../../../tools/applyFrame/frameData';
import { padFrameData } from '../../../tools/saveLocalStorageItems';

const getValidFrameGroupId = (groups: FrameGroup[], byId: string): string => {
const group = groups.find(({ id }) => id === byId);
Expand All @@ -27,16 +30,25 @@ interface UseFrames {
palette: string[],
setActiveFrameGroupName: (name: string) => void,
activeFrameGroup: FrameGroup,
convertFormat: () => void,
enableDebug: boolean,
}

const useFrames = (): UseFrames => {
const dispatch = useDispatch();

const { savFrameTypes, frames, frameGroupNames, palette } = useSelector((state: State) => ({
const {
savFrameTypes,
frames,
frameGroupNames,
palette,
enableDebug,
} = useSelector((state: State) => ({
savFrameTypes: state.savFrameTypes,
frames: state.frames,
frameGroupNames: state.frameGroupNames,
palette: state.palettes.find(({ shortName }) => shortName === state.activePalette) || state.palettes[0],
enableDebug: state.enableDebug,
}));

const frameGroups = getFrameGroups(frames, frameGroupNames);
Expand Down Expand Up @@ -75,6 +87,40 @@ const useFrames = (): UseFrames => {
});
};

const convertFormat = async () => {
const updatedFrames = await Promise.all(frames.map(async (frame): Promise<Frame> => {
const stateData = await loadFrameData(frame.hash);

if (!stateData) {
return frame;
}

const imageStartLine = stateData.upper.length / 20;
const tileData = padFrameData(stateData);

const { dataHash: newHash } = await compressAndHashFrame(tileData, imageStartLine);


if (frame.hash === newHash) {
return frame;
}

const saveHash = await saveFrameData(tileData, imageStartLine);

return {
...frame,
hash: saveHash,
};
}));

dispatch<GlobalUpdateAction>({
type: Actions.GLOBAL_UPDATE,
payload: {
frames: updatedFrames,
},
});
};

return {
selectedFrameGroup,
groupFrames,
Expand All @@ -84,6 +130,8 @@ const useFrames = (): UseFrames => {
palette: palette?.palette,
setActiveFrameGroupName,
activeFrameGroup,
convertFormat,
enableDebug,
};
};

Expand Down
24 changes: 15 additions & 9 deletions src/javascript/tools/saveLocalStorageItems/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,35 @@ export const saveImageFileContent = async (fileContent: string): Promise<string>
return save(lines);
};

export const saveFrameFileContent = async (fileContent: string): Promise<string> => {
const tiles = JSON.parse(fileContent) as FrameData;
const imageStartLine = tiles.upper.length / 20;

export const padFrameData = (frameData: FrameData): string[] => {
const black = Array(32)
.fill('f')
.join('');
const pad = Array(16)
.fill(black);

// tiles need to be padded with some lines that get stripped again when saving frame data
const paddedFrameData = [
...tiles.upper,
...frameData.upper,
...Array(14)
.fill('')
.map((_, index) => ([
...tiles.left[index],
...frameData.left[index],
...pad,
...tiles.right[index],
...frameData.right[index],
]))
.flat(),
...tiles.lower,
...frameData.lower,
];

return paddedFrameData;
};

export const saveFrameFileContent = async (fileContent: string): Promise<string> => {
const tiles = JSON.parse(fileContent) as FrameData;
const imageStartLine = tiles.upper.length / 20;

// tiles need to be padded with some lines that get stripped again when saving frame data
const paddedFrameData = padFrameData(tiles);
return saveFrameData(paddedFrameData, imageStartLine);
};

Expand Down

0 comments on commit 81e953e

Please sign in to comment.