import { Clickable, ClickableOptions, createClickable, GenericClickable } from "@/features/clickable"; import { GenericConversion } from "@/features/conversion"; import { CoercableComponent, Replace, setDefault } from "@/features/feature"; import { displayResource } from "@/features/resource"; import { createTreeNode, GenericTree, GenericTreeNode, TreeNode, TreeNodeOptions } from "@/features/tree"; import player from "@/game/player"; import Decimal from "@/util/bignum"; import { Computable, GetComputableTypeWithDefault, processComputable, ProcessedComputable } from "@/util/computed"; import { computed, Ref, unref } from "vue"; export interface ResetButtonOptions extends ClickableOptions { conversion: GenericConversion; tree: GenericTree; treeNode: GenericTreeNode; resetDescription?: Computable; showNextAt?: Computable; display?: Computable; canClick?: Computable; } type ResetButton = Replace< Clickable, { resetDescription: GetComputableTypeWithDefault>; showNextAt: GetComputableTypeWithDefault; display: GetComputableTypeWithDefault>; canClick: GetComputableTypeWithDefault>; onClick: VoidFunction; } >; export type GenericResetButton = Replace< GenericClickable & ResetButton, { resetDescription: ProcessedComputable; showNextAt: ProcessedComputable; display: ProcessedComputable; canClick: ProcessedComputable; } >; export function createResetButton( options: T ): ResetButton { setDefault(options, "showNextAt", true); if (options.resetDescription == null) { options.resetDescription = computed(() => Decimal.lt(proxy.conversion.gainResource.value, 1e3) ? "Reset for " : "" ); } if (options.display == null) { options.display = computed(() => { const nextAt = unref(proxy.showNextAt) && ( ); return ( {proxy.resetDescription} {displayResource( proxy.conversion.gainResource, unref(proxy.conversion.currentGain) )} {proxy.conversion.gainResource.displayName} {nextAt} ); }); } if (options.canClick == null) { options.canClick = computed(() => Decimal.gt(unref(proxy.conversion.currentGain), 0)); } const onClick = options.onClick; options.onClick = function () { proxy.conversion.convert(); proxy.tree.reset(proxy.treeNode); onClick?.(); }; const proxy = createClickable(options) as unknown as ResetButton; return proxy; } export interface LayerTreeNodeOptions extends TreeNodeOptions { layerID: string; color: string; append?: boolean; } export type LayerTreeNode = Replace< TreeNode, { append: ProcessedComputable; } >; export type GenericLayerTreeNode = LayerTreeNode; export function createLayerTreeNode(options: T): LayerTreeNode { processComputable(options as T, "append"); return createTreeNode({ ...options, display: options.layerID, onClick: options.append != null && options.append ? function () { if (player.tabs.includes(options.layerID)) { const index = player.tabs.lastIndexOf(options.layerID); player.tabs = [ ...player.tabs.slice(0, index), ...player.tabs.slice(index + 1) ]; } else { player.tabs = [...player.tabs, options.layerID]; } } : function () { player.tabs.splice(1, 1, options.layerID); } }) as unknown as LayerTreeNode; }