Skip to content

Commit 5139b05

Browse files
Add DPI scaling & config
1 parent d7055cb commit 5139b05

4 files changed

Lines changed: 49 additions & 15 deletions

File tree

client/src/client-config.tsx

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { useEffect } from 'react'
22
import { useAppContext } from './app-context'
33
import { GameRenderer } from './playback/GameRenderer'
4+
import { NumInput } from './components/forms'
45

56
export type ClientConfig = typeof DEFAULT_CONFIG
67

@@ -15,7 +16,8 @@ const DEFAULT_CONFIG = {
1516
showMapXY: true,
1617
streamRunnerGames: true,
1718
profileGames: false,
18-
validateMaps: false
19+
validateMaps: false,
20+
resolutionScale: 100
1921
}
2022

2123
const configDescription: Record<keyof ClientConfig, string> = {
@@ -25,14 +27,17 @@ const configDescription: Record<keyof ClientConfig, string> = {
2527
showMapXY: 'Show X,Y when hovering a tile',
2628
streamRunnerGames: 'Stream each round from the runner live as the game is being played',
2729
profileGames: 'Enable saving profiling data when running games',
28-
validateMaps: 'Validate maps before running a game'
30+
validateMaps: 'Validate maps before running a game',
31+
resolutionScale: 'Resolution scale for the game area. Decrease to help performance.'
2932
}
3033

3134
export function getDefaultConfig(): ClientConfig {
3235
const config: ClientConfig = { ...DEFAULT_CONFIG }
3336
for (const key in config) {
3437
const value = localStorage.getItem('config' + key)
35-
if (value) config[key as keyof ClientConfig] = JSON.parse(value)
38+
if (value) {
39+
;(config[key as keyof ClientConfig] as any) = JSON.parse(value)
40+
}
3641
}
3742
return config
3843
}
@@ -55,7 +60,7 @@ export const ConfigPage: React.FC<Props> = (props) => {
5560

5661
const ConfigBooleanElement: React.FC<{ configKey: keyof ClientConfig }> = ({ configKey }) => {
5762
const context = useAppContext()
58-
const value = context.state.config[configKey]
63+
const value = context.state.config[configKey] as boolean
5964
return (
6065
<div className={'flex flex-row items-center mb-2'}>
6166
<input
@@ -68,7 +73,7 @@ const ConfigBooleanElement: React.FC<{ configKey: keyof ClientConfig }> = ({ con
6873
}))
6974
localStorage.setItem('config' + configKey, JSON.stringify(e.target.checked))
7075
// hopefully after the setState is done
71-
setTimeout(() => GameRenderer.render(), 10)
76+
setTimeout(() => GameRenderer.fullRender(), 10)
7277
}}
7378
/>
7479
<div className={'ml-2 text-xs'}>{configDescription[configKey] ?? configKey}</div>
@@ -82,8 +87,29 @@ const ConfigStringElement: React.FC<{ configKey: string }> = ({ configKey }) =>
8287
return <div className={'flex flex-row items-center'}>Todo</div>
8388
}
8489

85-
const ConfigNumberElement: React.FC<{ configKey: string }> = ({ configKey }) => {
90+
const ConfigNumberElement: React.FC<{ configKey: keyof ClientConfig }> = ({ configKey }) => {
8691
const context = useAppContext()
87-
const value = context.state.config[configKey as keyof ClientConfig]
88-
return <div className={'flex flex-row items-center'}>Todo</div>
92+
const value = context.state.config[configKey as keyof ClientConfig] as number
93+
return (
94+
<div className={'flex flex-row items-center mb-2'}>
95+
<NumInput
96+
value={value}
97+
changeValue={(newVal) => {
98+
context.setState((prevState) => ({
99+
...prevState,
100+
config: { ...context.state.config, [configKey]: newVal }
101+
}))
102+
localStorage.setItem('config' + configKey, JSON.stringify(newVal))
103+
// hopefully after the setState is done
104+
setTimeout(() => {
105+
// Trigger canvas dimension update to ensure resolution is updated
106+
GameRenderer.onMatchChange()
107+
}, 10)
108+
}}
109+
min={10}
110+
max={100}
111+
/>
112+
<div className={'ml-2 text-xs'}>{configDescription[configKey] ?? configKey}</div>
113+
</div>
114+
)
89115
}

client/src/components/forms.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const NumInput: React.FC<NumInputProps> = (props) => {
8888
return (
8989
<input
9090
className={
91-
'border border-white bg-light py-0.5 pl-1 rounded-md w-12 ' +
91+
'border border-white bg-light py-0.5 pl-1 rounded-md w-14 ' +
9292
(props.disabled ? 'opacity-50 ' : '') +
9393
(props.className ?? '')
9494
}

client/src/components/sidebar/game/quick-line-chart.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ interface LineChartProps {
2121
resolution?: number
2222
}
2323

24-
export const QuickLineChart: React.FC<LineChartProps> = ({ data, width, height, margin, resolution = 1 }) => {
24+
export const QuickLineChart: React.FC<LineChartProps> = ({
25+
data,
26+
width,
27+
height,
28+
margin,
29+
resolution = window.devicePixelRatio ?? 1
30+
}) => {
2531
const canvasRef = useRef<HTMLCanvasElement | null>(null)
2632

2733
useEffect(() => {
@@ -58,11 +64,11 @@ export const QuickLineChart: React.FC<LineChartProps> = ({ data, width, height,
5864
margin,
5965
{
6066
range: { min: 0, max: data.length },
61-
options: {}
67+
options: { textColor: 'white', lineColor: 'white' }
6268
},
6369
{
6470
range: { min: 0, max: max },
65-
options: {}
71+
options: { textColor: 'white', lineColor: 'white' }
6672
}
6773
)
6874
}, [data.length, height, margin, width])

client/src/playback/GameRenderer.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,11 @@ class GameRendererClass {
115115

116116
private updateCanvasDimensions(dims: Vector) {
117117
for (const canvas of Object.values(this.canvases)) {
118-
canvas.width = dims.x * TILE_RESOLUTION
119-
canvas.height = dims.y * TILE_RESOLUTION
120-
canvas.getContext('2d')?.scale(TILE_RESOLUTION, TILE_RESOLUTION)
118+
const dpi = window.devicePixelRatio ?? 1
119+
const resolution = TILE_RESOLUTION * dpi * (GameConfig.config.resolutionScale / 100)
120+
canvas.width = dims.x * resolution
121+
canvas.height = dims.y * resolution
122+
canvas.getContext('2d')?.scale(resolution, resolution)
121123
}
122124
}
123125

0 commit comments

Comments
 (0)