Skip to content

Commit

Permalink
Remove game.resumed/paused in favor of status
Browse files Browse the repository at this point in the history
  • Loading branch information
4ian committed Dec 30, 2024
1 parent d0ef92d commit 1a8eee2
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 82 deletions.
24 changes: 0 additions & 24 deletions GDJS/Runtime/debugger-client/abstract-debugger-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -584,30 +584,6 @@ namespace gdjs {
);
}

/**
* Callback called when the game is paused.
*/
sendGamePaused(): void {
this._sendMessage(
circularSafeStringify({
command: 'game.paused',
payload: null,
})
);
}

/**
* Callback called when the game is resumed.
*/
sendGameResumed(): void {
this._sendMessage(
circularSafeStringify({
command: 'game.resumed',
payload: null,
})
);
}

sendInstancesUpdated(runtimeObjects: gdjs.RuntimeObject[]): void {
this._sendMessage(
circularSafeStringify({
Expand Down
3 changes: 1 addition & 2 deletions GDJS/Runtime/runtimegame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,7 @@ namespace gdjs {

this._paused = enable;
if (this._debuggerClient) {
if (this._paused) this._debuggerClient.sendGamePaused();
else this._debuggerClient.sendGameResumed();
this._debuggerClient.sendRuntimeGameStatus();
}
}

Expand Down
64 changes: 44 additions & 20 deletions newIDE/app/src/Debugger/DebuggerSelector.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,62 @@
// @flow
import { t } from '@lingui/macro';
import * as React from 'react';
import { I18n } from '@lingui/react';
import SelectField from '../UI/SelectField';
import SelectOption from '../UI/SelectOption';
import { type DebuggerId } from '../ExportAndShare/PreviewLauncher.flow';
import {
type DebuggerId,
type DebuggerStatus,
} from '../ExportAndShare/PreviewLauncher.flow';

type Props = {|
selectedId: DebuggerId,
debuggerIds: Array<DebuggerId>,
debuggerStatus: { [DebuggerId]: DebuggerStatus },
onChooseDebugger: DebuggerId => void,
|};

export default class DebuggerSelector extends React.Component<Props, void> {
render() {
const hasDebuggers = !!this.props.debuggerIds.length;
const debuggerIds = Object.keys(this.props.debuggerStatus);
const hasDebuggers = !!debuggerIds.length;
return (
<SelectField
fullWidth
value={hasDebuggers ? this.props.selectedId : 0}
onChange={(e, i, value) =>
this.props.onChooseDebugger(parseInt(value, 10) || 0)
}
disabled={!hasDebuggers}
>
{this.props.debuggerIds.map(id => (
<SelectOption value={id} key={id} label={t`Game preview #${id}`} />
))}
{!hasDebuggers && (
<SelectOption
value={0}
label={t`No preview running. Run a preview and you will be able to inspect it with the debugger`}
/>
<I18n>
{({ i18n }) => (
<SelectField
fullWidth
value={hasDebuggers ? this.props.selectedId : 0}
onChange={(e, i, value) =>
this.props.onChooseDebugger(parseInt(value, 10) || 0)
}
disabled={!hasDebuggers}
>
{debuggerIds.map(id => {
const status = this.props.debuggerStatus[+id];
const statusText = status.isPaused
? status.isInGameEdition
? t`Editing`
: t`Paused`
: status.isInGameEdition
? t`Playing (in-game edition)`
: t`Playing`;

return (
<SelectOption
value={+id}
key={id}
label={t`Game preview #${id} (${i18n._(statusText)})`}
/>
);
})}
{!hasDebuggers && (
<SelectOption
value={0}
label={t`No preview running. Run a preview and you will be able to inspect it with the debugger`}
/>
)}
</SelectField>
)}
</SelectField>
</I18n>
);
}
}
49 changes: 20 additions & 29 deletions newIDE/app/src/Debugger/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import EmptyMessage from '../UI/EmptyMessage';
import {
type PreviewDebuggerServer,
type DebuggerId,
type DebuggerStatus,
} from '../ExportAndShare/PreviewLauncher.flow';
import { type Log, LogsManager } from './DebuggerConsole';

Expand Down Expand Up @@ -53,7 +54,7 @@ type State = {|
debuggerGameData: { [DebuggerId]: any },
profilerOutputs: { [DebuggerId]: ProfilerOutput },
profilingInProgress: { [DebuggerId]: boolean },
gameIsPaused: { [DebuggerId]: boolean },
debuggerStatus: { [DebuggerId]: DebuggerStatus },
selectedId: DebuggerId,
logs: { [DebuggerId]: Array<Log> },
|};
Expand All @@ -70,7 +71,7 @@ export default class Debugger extends React.Component<Props, State> {
debuggerGameData: {},
profilerOutputs: {},
profilingInProgress: {},
gameIsPaused: {},
debuggerStatus: {},
selectedId: 0,
logs: {},
};
Expand All @@ -79,18 +80,22 @@ export default class Debugger extends React.Component<Props, State> {
_debuggerLogs: Map<number, LogsManager> = new Map();

updateToolbar = () => {
const { selectedId, gameIsPaused } = this.state;
const { selectedId, debuggerStatus } = this.state;

const selectedDebuggerContents = this._debuggerContents[
this.state.selectedId
];

const isSelectedDebuggerPaused = debuggerStatus[selectedId]
? debuggerStatus[selectedId].isPaused
: false;

this.props.setToolbar(
<Toolbar
onPlay={() => this._play(this.state.selectedId)}
onPause={() => this._pause(this.state.selectedId)}
canPlay={this._hasSelectedDebugger() && gameIsPaused[selectedId]}
canPause={this._hasSelectedDebugger() && !gameIsPaused[selectedId]}
canPlay={this._hasSelectedDebugger() && isSelectedDebuggerPaused}
canPause={this._hasSelectedDebugger() && !isSelectedDebuggerPaused}
canOpenProfiler={this._hasSelectedDebugger()}
isProfilerShown={
!!selectedDebuggerContents &&
Expand Down Expand Up @@ -161,14 +166,14 @@ export default class Debugger extends React.Component<Props, State> {
debuggerGameData,
profilerOutputs,
profilingInProgress,
gameIsPaused,
debuggerStatus,
}) => {
// Remove any data bound to the instance that might have been stored.
// Otherwise this would be a memory leak.
if (debuggerGameData[id]) delete debuggerGameData[id];
if (profilerOutputs[id]) delete profilerOutputs[id];
if (profilingInProgress[id]) delete profilingInProgress[id];
if (gameIsPaused[id]) delete gameIsPaused[id];
if (debuggerStatus[id]) delete debuggerStatus[id];

return {
debuggerIds,
Expand All @@ -181,7 +186,7 @@ export default class Debugger extends React.Component<Props, State> {
debuggerGameData,
profilerOutputs,
profilingInProgress,
gameIsPaused,
debuggerStatus,
};
},
() => this.updateToolbar()
Expand Down Expand Up @@ -237,9 +242,9 @@ export default class Debugger extends React.Component<Props, State> {
} else if (data.command === 'status') {
this.setState(
state => ({
gameIsPaused: {
...state.gameIsPaused,
[id]: !!data.payload.isPaused,
debuggerStatus: {
...state.debuggerStatus,
[id]: data.payload,
},
}),
() => this.updateToolbar()
Expand All @@ -259,20 +264,6 @@ export default class Debugger extends React.Component<Props, State> {
this.setState(state => ({
profilingInProgress: { ...state.profilingInProgress, [id]: false },
}));
} else if (data.command === 'game.resumed') {
this.setState(
state => ({
gameIsPaused: { ...state.gameIsPaused, [id]: false },
}),
() => this.updateToolbar()
);
} else if (data.command === 'game.paused') {
this.setState(
state => ({
gameIsPaused: { ...state.gameIsPaused, [id]: true },
}),
() => this.updateToolbar()
);
} else if (data.command === 'hotReloader.logs') {
// Nothing to do.
} else if (data.command === 'console.log') {
Expand All @@ -291,14 +282,14 @@ export default class Debugger extends React.Component<Props, State> {
const { previewDebuggerServer } = this.props;
previewDebuggerServer.sendMessage(id, { command: 'play' });

// Pause status is transmitted by the game (using `game.paused`, `game.resumed` or `status`).
// Pause status is transmitted by the game (using `status`).
};

_pause = (id: DebuggerId) => {
const { previewDebuggerServer } = this.props;
previewDebuggerServer.sendMessage(id, { command: 'pause' });

// Pause status is transmitted by the game (using `game.paused`, `game.resumed` or `status`).
// Pause status is transmitted by the game (using `status`).
};

_refresh = (id: DebuggerId) => {
Expand Down Expand Up @@ -350,7 +341,7 @@ export default class Debugger extends React.Component<Props, State> {
debuggerServerError,
debuggerServerState,
selectedId,
debuggerIds,
debuggerStatus,
debuggerGameData,
profilerOutputs,
profilingInProgress,
Expand Down Expand Up @@ -380,7 +371,7 @@ export default class Debugger extends React.Component<Props, State> {
<Column expand noMargin>
<DebuggerSelector
selectedId={selectedId}
debuggerIds={debuggerIds}
debuggerStatus={debuggerStatus}
onChooseDebugger={id =>
this.setState(
{
Expand Down
7 changes: 7 additions & 0 deletions newIDE/app/src/ExportAndShare/PreviewLauncher.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export type PreviewLauncherProps = {|
/** Each game connected to the debugger server is identified by a unique number. */
export type DebuggerId = number;

/** Each game connected to the debugger server can communicate its status. */
export type DebuggerStatus = {|
isPaused: boolean,
isInGameEdition: boolean,
currentSceneName: string | null,
|};

/** The callbacks for a debugger server used for previews. */
export type PreviewDebuggerServerCallbacks = {|
onErrorReceived: (err: Error) => void | Promise<void>,
Expand Down
7 changes: 1 addition & 6 deletions newIDE/app/src/MainFrame/PreviewState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type PreviewDebuggerServer,
type DebuggerId,
type HotReloaderLog,
type DebuggerStatus,
} from '../ExportAndShare/PreviewLauncher.flow';

/** Represents what should be run when a preview is launched */
Expand All @@ -21,12 +22,6 @@ export type PreviewState = {|
overridenPreviewExternalLayoutName: ?string,
|};

type DebuggerStatus = {|
isPaused: boolean,
isInGameEdition: boolean,
currentSceneName: string,
|};

type PreviewDebuggerServerWatcherResults = {|
getInGameEditionPreviewStatus: () => DebuggerStatus | null,
hasNonEditionPreviewsRunning: boolean,
Expand Down
3 changes: 2 additions & 1 deletion newIDE/app/src/MainFrame/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1814,7 +1814,8 @@ const MainFrame = (props: Props) => {
hotReload: false,
forceDiagnosticReport: false,
isForInGameEdition: {
forcedSceneName: runningInGameEditionPreviewStatus.currentSceneName,
forcedSceneName:
runningInGameEditionPreviewStatus.currentSceneName || '',
},
numberOfWindows: 0,
});
Expand Down

0 comments on commit 1a8eee2

Please sign in to comment.