From 65071760ec2ecec9129dfd5c5312141e09040169 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Fri, 23 Dec 2022 11:27:35 -0600 Subject: [PATCH] Make boards take state and link overrides --- src/features/boards/board.ts | 78 +++++++++++++++++++++++------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/features/boards/board.ts b/src/features/boards/board.ts index 4763993..689659d 100644 --- a/src/features/boards/board.ts +++ b/src/features/boards/board.ts @@ -9,7 +9,7 @@ import { Visibility } from "features/feature"; import { globalBus } from "game/events"; -import type { Persistent, State } from "game/persistence"; +import { DefaultValue, deletePersistent, Persistent, State } from "game/persistence"; import { persistent } from "game/persistence"; import type { Unsubscribe } from "nanoevents"; import { isFunction } from "util/common"; @@ -167,12 +167,12 @@ export interface BoardOptions { style?: Computable; startNodes: () => Omit[]; types: Record; + state?: Computable; + links?: Computable; } export interface BaseBoard { id: string; - state: Persistent; - links: Ref; nodes: Ref; selectedNode: Ref; selectedAction: Ref; @@ -191,6 +191,8 @@ export type Board = Replace< width: GetComputableType; classes: GetComputableType; style: GetComputableType; + state: GetComputableTypeWithDefault>; + links: GetComputableTypeWithDefault>; } >; @@ -198,31 +200,46 @@ export type GenericBoard = Replace< Board, { visibility: ProcessedComputable; + state: ProcessedComputable; + links: ProcessedComputable; } >; export function createBoard( optionsFunc: OptionsFunc ): Board { + const state = persistent({ + nodes: [], + selectedNode: null, + selectedAction: null + }); + return createLazyProxy(() => { const board = optionsFunc(); board.id = getUniqueID("board-"); board.type = BoardType; board[Component] = BoardComponent; - board.state = persistent({ - nodes: board.startNodes().map((n, i) => { - (n as BoardNode).id = i; - return n as BoardNode; - }), - selectedNode: null, - selectedAction: null - }); - board.nodes = computed(() => processedBoard.state.value.nodes); + if (board.state) { + deletePersistent(state); + processComputable(board as T, "state"); + } else { + state[DefaultValue] = { + nodes: board.startNodes().map((n, i) => { + (n as BoardNode).id = i; + return n as BoardNode; + }), + selectedNode: null, + selectedAction: null + }; + board.state = state; + } + + board.nodes = computed(() => unref(processedBoard.state).nodes); board.selectedNode = computed( () => processedBoard.nodes.value.find( - node => node.id === processedBoard.state.value.selectedNode + node => node.id === unref(processedBoard.state).selectedNode ) || null ); board.selectedAction = computed(() => { @@ -236,23 +253,30 @@ export function createBoard( } return ( type.actions.find( - action => action.id === processedBoard.state.value.selectedAction + action => action.id === unref(processedBoard.state).selectedAction ) || null ); }); board.mousePosition = ref(null); - board.links = computed(() => { - if (processedBoard.selectedAction.value == null) { - return null; - } - if (processedBoard.selectedAction.value.links && processedBoard.selectedNode.value) { - return getNodeProperty( - processedBoard.selectedAction.value.links, + if (board.links) { + processComputable(board as T, "links"); + } else { + board.links = computed(() => { + if (processedBoard.selectedAction.value == null) { + return null; + } + if ( + processedBoard.selectedAction.value.links && processedBoard.selectedNode.value - ); - } - return null; - }); + ) { + return getNodeProperty( + processedBoard.selectedAction.value.links, + processedBoard.selectedNode.value + ); + } + return null; + }); + } processComputable(board as T, "visibility"); setDefault(board, "visibility", Visibility.Visible); processComputable(board as T, "width"); @@ -286,10 +310,10 @@ export function createBoard( processComputable(nodeType as NodeTypeOptions, "actionDistance"); setDefault(nodeType, "actionDistance", Math.PI / 6); nodeType.nodes = computed(() => - processedBoard.state.value.nodes.filter(node => node.type === type) + unref(processedBoard.state).nodes.filter(node => node.type === type) ); setDefault(nodeType, "onClick", function (node: BoardNode) { - processedBoard.state.value.selectedNode = node.id; + unref(processedBoard.state).selectedNode = node.id; }); if (nodeType.actions) {