Skip to content

Commit

Permalink
Different terrain colors on editor/export
Browse files Browse the repository at this point in the history
Colors for export will remain the same for compatibility

Now we can change the colors on the editor without much issue

Ref #68
  • Loading branch information
ngyikp committed Apr 9, 2023
1 parent f981a73 commit 1ed2e18
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 32 deletions.
68 changes: 46 additions & 22 deletions src/levelEditor/GeoConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,56 @@ export const SCREEN_WIDTH = 1920;
export const SCREEN_HEIGHT: number = 1080 + 10; // I don't know why the geo has 10px extra at the bottom...

// Colors
const PIXEL_COLORS: Map<number, string> = new Map();
const EDITOR_UI_PIXEL_COLORS: Map<number, string> = new Map();
const EXPORT_TERRAIN_PIXEL_COLORS: Map<number, string> = new Map();

// higher ground layers
PIXEL_COLORS.set(255, '#eeeeee');
PIXEL_COLORS.set(254, '#e5e5e5');
PIXEL_COLORS.set(253, '#dddddd');
PIXEL_COLORS.set(252, '#d4d4d4');
PIXEL_COLORS.set(251, '#cccccc');
PIXEL_COLORS.set(250, '#c3c3c3');
PIXEL_COLORS.set(249, '#bbbbbb');
PIXEL_COLORS.set(248, '#b2b2b2');
PIXEL_COLORS.set(247, '#aaaaaa'); // [0, 3, 2]
PIXEL_COLORS.set(246, '#a1a1a1'); // [0, -2, -14]
PIXEL_COLORS.set(244, '#999999'); // [0, 5, 1]
PIXEL_COLORS.set(243, '#909090'); // [0, 6, 1]
PIXEL_COLORS.set(242, '#888888'); // [0, 6, 1]
EDITOR_UI_PIXEL_COLORS.set(255, '#eeeeee');
EDITOR_UI_PIXEL_COLORS.set(254, '#e5e5e5');
EDITOR_UI_PIXEL_COLORS.set(253, '#dddddd');
EDITOR_UI_PIXEL_COLORS.set(252, '#d4d4d4');
EDITOR_UI_PIXEL_COLORS.set(251, '#cccccc');
EDITOR_UI_PIXEL_COLORS.set(250, '#c3c3c3');
EDITOR_UI_PIXEL_COLORS.set(249, '#bbbbbb');
EDITOR_UI_PIXEL_COLORS.set(248, '#b2b2b2');
EDITOR_UI_PIXEL_COLORS.set(247, '#aaaaaa'); // [0, 3, 2]
EDITOR_UI_PIXEL_COLORS.set(246, '#a1a1a1'); // [0, -2, -14]
EDITOR_UI_PIXEL_COLORS.set(244, '#999999'); // [0, 5, 1]
EDITOR_UI_PIXEL_COLORS.set(243, '#909090'); // [0, 6, 1]
EDITOR_UI_PIXEL_COLORS.set(242, '#888888'); // [0, 6, 1]

PIXEL_COLORS.set(6, 'blue'); // water
PIXEL_COLORS.set(5, 'brown'); // unwalkable collision
PIXEL_COLORS.set(4, 'lightblue'); // sky
PIXEL_COLORS.set(3, 'pink'); // swimable wall
PIXEL_COLORS.set(2, '#555'); // unswimable wall
PIXEL_COLORS.set(1, '#e2e2e2'); // ramp
PIXEL_COLORS.set(0, '#fff'); // walkable
EDITOR_UI_PIXEL_COLORS.set(6, 'blue'); // water
EDITOR_UI_PIXEL_COLORS.set(5, 'brown'); // unwalkable collision
EDITOR_UI_PIXEL_COLORS.set(4, 'lightblue'); // sky
EDITOR_UI_PIXEL_COLORS.set(3, 'pink'); // swimable wall
EDITOR_UI_PIXEL_COLORS.set(2, '#555'); // unswimable wall
EDITOR_UI_PIXEL_COLORS.set(1, '#e2e2e2'); // ramp
EDITOR_UI_PIXEL_COLORS.set(0, '#fff'); // walkable

export {PIXEL_COLORS};
// higher ground layers
EXPORT_TERRAIN_PIXEL_COLORS.set(255, '#eeeeee');
EXPORT_TERRAIN_PIXEL_COLORS.set(254, '#e5e5e5');
EXPORT_TERRAIN_PIXEL_COLORS.set(253, '#dddddd');
EXPORT_TERRAIN_PIXEL_COLORS.set(252, '#d4d4d4');
EXPORT_TERRAIN_PIXEL_COLORS.set(251, '#cccccc');
EXPORT_TERRAIN_PIXEL_COLORS.set(250, '#c3c3c3');
EXPORT_TERRAIN_PIXEL_COLORS.set(249, '#bbbbbb');
EXPORT_TERRAIN_PIXEL_COLORS.set(248, '#b2b2b2');
EXPORT_TERRAIN_PIXEL_COLORS.set(247, '#aaaaaa'); // [0, 3, 2]
EXPORT_TERRAIN_PIXEL_COLORS.set(246, '#a1a1a1'); // [0, -2, -14]
EXPORT_TERRAIN_PIXEL_COLORS.set(244, '#999999'); // [0, 5, 1]
EXPORT_TERRAIN_PIXEL_COLORS.set(243, '#909090'); // [0, 6, 1]
EXPORT_TERRAIN_PIXEL_COLORS.set(242, '#888888'); // [0, 6, 1]

EXPORT_TERRAIN_PIXEL_COLORS.set(6, 'blue'); // water
EXPORT_TERRAIN_PIXEL_COLORS.set(5, 'brown'); // unwalkable collision
EXPORT_TERRAIN_PIXEL_COLORS.set(4, 'lightblue'); // sky
EXPORT_TERRAIN_PIXEL_COLORS.set(3, 'pink'); // swimable wall
EXPORT_TERRAIN_PIXEL_COLORS.set(2, '#555'); // unswimable wall
EXPORT_TERRAIN_PIXEL_COLORS.set(1, '#e2e2e2'); // ramp
EXPORT_TERRAIN_PIXEL_COLORS.set(0, '#fff'); // walkable

export {EDITOR_UI_PIXEL_COLORS, EXPORT_TERRAIN_PIXEL_COLORS};

// Explanations
export const PIXEL_COLORS_EXPLANATIONS = [
Expand Down
3 changes: 3 additions & 0 deletions src/levelEditor/common/GeoPreview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import drawGeoToCanvas from '../util/drawGeoToCanvas';
import styles from './GeoPreview.module.css';

type Props = $ReadOnly<{
colors: Map<number, string>,
level: LevelType,
mapMouseMoveCoordinates: ?[number, number],
scale: number,
Expand Down Expand Up @@ -50,6 +51,7 @@ export default function GeoPreview(props: Props): React$Node {

drawGeoToCanvas({
canvas,
colors: props.colors,
ctx,
geo: decodedGeo,
scale: props.scale * dpr,
Expand Down Expand Up @@ -82,6 +84,7 @@ export default function GeoPreview(props: Props): React$Node {
}, [
decodedGeo,
dpr,
props.colors,
props.mapMouseMoveCoordinates,
props.scale,
props.paintBufferUpdate,
Expand Down
3 changes: 2 additions & 1 deletion src/levelEditor/preview/LevelPreview.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow strict

import GeoPreview from '../common/GeoPreview';
import {GEO_WIDTH, SCREEN_WIDTH} from '../GeoConstants';
import {EDITOR_UI_PIXEL_COLORS, GEO_WIDTH, SCREEN_WIDTH} from '../GeoConstants';
// $FlowFixMe[untyped-import]
import spriteData from '../spriteData.json';
import type {EditorToolType} from '../types/EditorToolType';
Expand Down Expand Up @@ -164,6 +164,7 @@ export default function LevelPreview(props: Props): React$Node {
{props.activeUiViews.includes('GEO') ? (
<div className={styles.geoCanvas}>
<GeoPreview
colors={EDITOR_UI_PIXEL_COLORS}
geoPaintBuffer={props.geoPaintBuffer}
level={props.level}
mapMouseMoveCoordinates={null}
Expand Down
2 changes: 2 additions & 0 deletions src/levelEditor/sidebar/LevelSidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ErrorBoundary from '../../common/ErrorBoundary';
import GeoPreview from '../common/GeoPreview';
import {EDITOR_UI_PIXEL_COLORS} from '../GeoConstants';
import type {GameEntityType} from '../types/GameEntityType';
import type {LevelInspectorUiView} from '../types/LevelInspectorUiView';
import type {LevelType} from '../types/LevelType';
Expand Down Expand Up @@ -55,6 +56,7 @@ export default function LevelSidebar(props: Props): React$Node {
<div className={styles.sidebar}>
<ErrorBoundary>
<GeoPreview
colors={EDITOR_UI_PIXEL_COLORS}
geoPaintBuffer={null}
level={props.level}
mapMouseMoveCoordinates={props.mapMouseMoveCoordinates}
Expand Down
10 changes: 7 additions & 3 deletions src/levelEditor/terrainEditor/LevelTerrainEditorModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import getCanvasRenderingContext from '../../util/getCanvasRenderingContext';
import GeoPreview from '../common/GeoPreview';
import {useCurrentCoordinatesNonNullable} from '../CurrentCoordinatesContext';
import {
EXPORT_TERRAIN_PIXEL_COLORS,
GEO_HEIGHT,
GEO_WIDTH,
PIXEL_COLORS,
PIXEL_COLORS_EXPLANATIONS,
} from '../GeoConstants';
import type {LevelType} from '../types/LevelType';
Expand All @@ -38,7 +38,7 @@ function prepareCanvas(): HTMLCanvasElement {
function generateColorsToGameBytes() {
const bytes = [];

PIXEL_COLORS.forEach((color, byte) => {
EXPORT_TERRAIN_PIXEL_COLORS.forEach((color, byte) => {
const colorObj = tinycolor(color);
const rgb: {r: number, g: number, b: number} = colorObj.toRgb();

Expand Down Expand Up @@ -79,7 +79,9 @@ export default function LevelTerrainEditorModal(props: Props): React$Node {
return {
description: explanation.description,
colors: explanation.colors.map((colorIndex) => {
const colorObj = tinycolor(PIXEL_COLORS.get(colorIndex));
const colorObj = tinycolor(
EXPORT_TERRAIN_PIXEL_COLORS.get(colorIndex)
);
const rgb = colorObj.toRgb();

return [rgb.r, rgb.g, rgb.b];
Expand Down Expand Up @@ -184,6 +186,7 @@ export default function LevelTerrainEditorModal(props: Props): React$Node {
const ctx = getCanvasRenderingContext(canvas, false);
drawGeoToCanvas({
canvas,
colors: EXPORT_TERRAIN_PIXEL_COLORS,
ctx,
geo: decodedGeo,
scale: 1,
Expand Down Expand Up @@ -219,6 +222,7 @@ export default function LevelTerrainEditorModal(props: Props): React$Node {

<div className={styles.geoPreview}>
<GeoPreview
colors={EXPORT_TERRAIN_PIXEL_COLORS}
geoPaintBuffer={null}
level={props.level}
mapMouseMoveCoordinates={null}
Expand Down
8 changes: 5 additions & 3 deletions src/levelEditor/toolbar/LevelToolbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {useHotkeys} from 'react-hotkeys-hook';

import SelectableButton from '../../common/SelectableButton';
import {
PIXEL_COLORS,
EDITOR_UI_PIXEL_COLORS,
PIXEL_COLORS_EXPLANATIONS,
TOOLBAR_COLOR_LOOKUP,
} from '../GeoConstants';
Expand Down Expand Up @@ -74,7 +74,9 @@ export default function LevelToolbar(props: Props): React$Node {
<div className={styles.colorContainer}>
<span
className={styles.colorDisplay}
style={{background: PIXEL_COLORS.get(props.currentPaintColor)}}
style={{
background: EDITOR_UI_PIXEL_COLORS.get(props.currentPaintColor),
}}
/>
{colorDescription != null ? colorDescription.description : 'N/A'}
</div>
Expand Down Expand Up @@ -113,7 +115,7 @@ export default function LevelToolbar(props: Props): React$Node {
>
<span
className={styles.colorBox}
style={{background: PIXEL_COLORS.get(colorIndex)}}
style={{background: EDITOR_UI_PIXEL_COLORS.get(colorIndex)}}
/>
{description}
</SelectableButton>
Expand Down
6 changes: 4 additions & 2 deletions src/levelEditor/util/drawGeoToCanvas.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// @flow strict

import {GEO_WIDTH, PIXEL_COLORS} from '../GeoConstants';
import {GEO_WIDTH} from '../GeoConstants';

export default function drawGeoToCanvas({
canvas,
colors,
ctx,
geo,
scale,
geoPaintBuffer,
}: {
canvas: HTMLCanvasElement,
colors: Map<number, string>,
ctx: CanvasRenderingContext2D,
geo: Uint8Array,
scale: number,
Expand All @@ -27,7 +29,7 @@ export default function drawGeoToCanvas({
pixelToUse = geoPaintBuffer[index];
}

const fill = PIXEL_COLORS.get(pixelToUse);
const fill = colors.get(pixelToUse);
if (fill == null) {
console.warn('unknown pixel color ' + pixelToUse);
return null;
Expand Down
3 changes: 2 additions & 1 deletion src/levelEditor/worldMap/getWorldMapGeoPreviewCache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow strict

import getCanvasRenderingContext from '../../util/getCanvasRenderingContext';
import {GEO_HEIGHT, GEO_WIDTH} from '../GeoConstants';
import {EDITOR_UI_PIXEL_COLORS, GEO_HEIGHT, GEO_WIDTH} from '../GeoConstants';
import decodeGeoString from '../util/decodeGeoString';
import drawGeoToCanvas from '../util/drawGeoToCanvas';

Expand All @@ -23,6 +23,7 @@ export default function getWorldMapGeoPreviewCache(geo: string): string {
const ctx = getCanvasRenderingContext(canvas, false);
drawGeoToCanvas({
canvas,
colors: EDITOR_UI_PIXEL_COLORS,
ctx,
geo: decodedGeo,
scale: dpr,
Expand Down

0 comments on commit 1ed2e18

Please sign in to comment.