Skip to content

Commit

Permalink
Auto-commit: 2024-03-03 15:59:21
Browse files Browse the repository at this point in the history
Files:
src/demo/DemoGame.ts
src/index.tsx
src/ui/GameContextType.tsx
src/ui/Hud.tsx
src/ui/HudContent.tsx
src/ui/UserInterface.ts
src/ui/popup/PopupManager.ts
src/ui/popup/usePopup.ts
src/ui/popup/usePopupManager.ts

https://github.com/jacklehamster/autocommit
  • Loading branch information
jacklehamster committed Mar 3, 2024
1 parent 55fcaab commit fd7f95c
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 25 deletions.
9 changes: 5 additions & 4 deletions src/demo/DemoGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ export class DemoGame extends AuxiliaryHolder {
engine.setMaxSpriteCount(0);
}
});

this.addAuxiliary(new SpriteUpdater({ engine, motor, sprites: spritesAccumulator }));

const exitBlock = {
Expand Down Expand Up @@ -410,22 +411,22 @@ export class DemoGame extends AuxiliaryHolder {
listener: {
onEnter() {
displayBox.setImageId(Assets.WIREFRAME_RED);
ui.showDialog({
ui.openDialog({
zIndex: 1,
conversation: {
messages: [
{ text: "Hello there." },
{
text: "How are you?",
action: () => ui.showMenu({
action: () => ui.openMenu({
position: [200, 390],
size: [undefined, 100],
positionFromRight: true,
items: [
{ label: "good", action: closePopupAction() },
{ label: "bad", action: closePopupAction() },
],
})
}),
},
{ text: "Bye bye." },
{
Expand Down Expand Up @@ -635,7 +636,7 @@ export class DemoGame extends AuxiliaryHolder {
listener: {
onEnter() {
displayBox.setImageId(Assets.WIREFRAME_RED);
ui.showDialog({
ui.openDialog({
conversation: {
messages: [
{ text: "Going down..." },
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function testCanvas(canvas: HTMLCanvasElement) {
const menuControls = new KeyboardControls(keyboard);

const webGlCanvas = new WebGlCanvas(canvas);
const ui: UserInterface = new Hud({ controls: menuControls, webGlCanvas });
const ui: Hud = new Hud({ controls: menuControls, webGlCanvas });
ui.addDialogListener({
onPopup(count) {
gameControls.enabled = count === 0;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/GameContextType.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface GameContextType {
removeControlsLock(uid: string): void;
openMenu(value: MenuData): void;
openDialog(value: DialogData): void;
popBack(): void;
closePopup(): void;
controls?: IControls;
topPopupUid: string;
}
Expand All @@ -25,7 +25,7 @@ export const DEFAULT_GAME_CONTEXT: GameContextType = {
openDialog: function (_value: DialogData | undefined): void {
throw new Error('Function not implemented.');
},
popBack(): void {
closePopup(): void {
throw new Error('Function not implemented.');
},
topPopupUid: '',
Expand Down
8 changes: 6 additions & 2 deletions src/ui/Hud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,22 @@ export class Hud extends AuxiliaryHolder implements UserInterface {
this.#rootElem.style.pointerEvents = 'none';
}

showDialog(dialog: DialogData): void {
openDialog(dialog: DialogData): void {
const type = 'dialog';
const uid = type + '-' + uuidv4();
this.#popupManager.openDialog?.({ uid, type, ...dialog });
}

showMenu(menuData: MenuData): void {
openMenu(menuData: MenuData): void {
const type = 'menu';
const uid = type + '-' + uuidv4();
this.#popupManager.openMenu?.({ uid, type, ...menuData });
}

closePopup(): void {
this.#popupManager.closePopup?.();
}

activate(): void {
super.activate();
document.body.appendChild(this.#rootElem);
Expand Down
7 changes: 4 additions & 3 deletions src/ui/HudContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,25 @@ interface Props {
}

export function HudContent({ dialogManager, controls }: Props) {
const { popups, addPopup, popBack, topPopupUid } = usePopupManager();
const { popups, addPopup, closePopup, topPopupUid } = usePopupManager();

const gameContext: GameContextType = useMemo<GameContextType>(
() => ({
addControlsLock: dialogManager.addControlsLock,
removeControlsLock: dialogManager.removeControlsLock,
openMenu: addPopup,
openDialog: addPopup,
popBack,
closePopup,
controls,
topPopupUid,
}),
[dialogManager, controls, addPopup, popBack, topPopupUid],
[dialogManager, controls, addPopup, closePopup, topPopupUid],
);

useEffect(() => {
dialogManager.openMenu = gameContext.openMenu;
dialogManager.openDialog = gameContext.openDialog;
dialogManager.closePopup = gameContext.closePopup;
}, [dialogManager, gameContext]);

return (
Expand Down
8 changes: 3 additions & 5 deletions src/ui/UserInterface.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Auxiliary } from "world/aux/Auxiliary";
import { Listener } from "./Listener";
import { MenuData } from "./menu/model/MenuData";
import { DialogData } from "./dialog/model/DialogData";

export interface UserInterface extends Auxiliary {
addDialogListener(listener: Listener): void;
removeDialogListener(listener: Listener): void;
showMenu(menu: MenuData): void;
showDialog(dialog: DialogData): void;
openMenu(menu: MenuData): void;
openDialog(dialog: DialogData): void;
closePopup(): void;
}
11 changes: 8 additions & 3 deletions src/ui/popup/PopupManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { UserInterface } from 'ui/UserInterface';
import { Listener } from '../Listener';
import { DialogData } from '../dialog/model/DialogData';
import { MenuData } from '../menu/model/MenuData';

export class PopupManager {
export class PopupManager implements UserInterface {
#popups: string[] = [];

constructor(private listeners: Set<Listener>) {
Expand All @@ -20,8 +21,12 @@ export class PopupManager {
this.listeners.forEach(listener => listener.onPopup(this.#popups.length));
}

openDialog?(dialog: DialogData): void;
openMenu?(menu: MenuData): void;
openDialog(dialog: DialogData): void {
}
openMenu(menu: MenuData): void {
}
closePopup(): void {
}

get lockUid() {
return this.#popups[this.#popups.length - 1];
Expand Down
6 changes: 3 additions & 3 deletions src/ui/popup/usePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ interface Props {
}

export function usePopup({ popupData }: Props) {
const { popBack } = useGameContext();
const { closePopup } = useGameContext();
const popupInterface = useMemo<PopupInterface>(
() => ({
close() {
popBack();
closePopup();
},
}),
[popBack],
[closePopup],
);

return { popupInterface };
Expand Down
4 changes: 2 additions & 2 deletions src/ui/popup/usePopupManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export function usePopupManager() {
[setPopups],
);

const popBack = useCallback(
const closePopup = useCallback(
() => setPopups((popups) => popups.slice(0, popups.length - 1)),
[setPopups],
);

return {
popups,
addPopup,
popBack,
closePopup,
topPopupUid,
}
}

0 comments on commit fd7f95c

Please sign in to comment.