Made createCollapsibleModifierSections utility function

This commit is contained in:
thepaperpilot 2022-04-30 16:42:03 -05:00
parent 1479c1d44d
commit c692578da7
2 changed files with 88 additions and 2 deletions

9
src/data/common.css Normal file
View file

@ -0,0 +1,9 @@
.modifier-toggle {
padding-right: 10px;
transform: translateY(-1px);
display: inline-block;
}
.modifier-toggle.collapsed {
transform: translate(-5px, -5px) rotate(-90deg);
}

View file

@ -5,7 +5,14 @@ import {
GenericClickable GenericClickable
} from "features/clickables/clickable"; } from "features/clickables/clickable";
import { GenericConversion } from "features/conversion"; import { GenericConversion } from "features/conversion";
import { CoercableComponent, jsx, OptionsFunc, Replace, setDefault } from "features/feature"; import {
CoercableComponent,
jsx,
JSXFunction,
OptionsFunc,
Replace,
setDefault
} from "features/feature";
import { displayResource } from "features/resources/resource"; import { displayResource } from "features/resources/resource";
import { import {
createTreeNode, createTreeNode,
@ -14,16 +21,21 @@ import {
TreeNode, TreeNode,
TreeNodeOptions TreeNodeOptions
} from "features/trees/tree"; } from "features/trees/tree";
import { Modifier } from "game/modifiers";
import { Persistent, persistent } from "game/persistence";
import player from "game/player"; import player from "game/player";
import Decimal, { DecimalSource } from "util/bignum"; import Decimal, { DecimalSource, format } from "util/bignum";
import { import {
Computable, Computable,
convertComputable,
GetComputableType, GetComputableType,
GetComputableTypeWithDefault, GetComputableTypeWithDefault,
processComputable, processComputable,
ProcessedComputable ProcessedComputable
} from "util/computed"; } from "util/computed";
import { renderJSX } from "util/vue";
import { computed, Ref, unref } from "vue"; import { computed, Ref, unref } from "vue";
import "./common.css";
export interface ResetButtonOptions extends ClickableOptions { export interface ResetButtonOptions extends ClickableOptions {
conversion: GenericConversion; conversion: GenericConversion;
@ -177,3 +189,68 @@ export function createLayerTreeNode<T extends LayerTreeNodeOptions>(
}; };
}) as unknown as LayerTreeNode<T>; }) as unknown as LayerTreeNode<T>;
} }
export function createCollapsibleModifierSections(
sections: {
title: string;
subtitle?: string;
modifier: Required<Modifier>;
base?: Computable<DecimalSource>;
unit?: string;
baseText?: Computable<CoercableComponent>;
visible?: Computable<boolean>;
}[]
): [JSXFunction, Persistent<boolean>[]] {
const processedBase = sections.map(s => convertComputable(s.base));
const processedBaseText = sections.map(s => convertComputable(s.baseText));
const processedVisible = sections.map(s => convertComputable(s.visible));
const collapsed = sections.map(() => persistent<boolean>(false));
const jsxFunc = jsx(() => {
const sectionJSX = sections.map((s, i) => {
if (unref(processedVisible[i]) === false) return null;
const header = (
<h3
onClick={() => (collapsed[i].value = !collapsed[i].value)}
style="cursor: pointer"
>
<span class={"modifier-toggle" + (unref(collapsed[i]) ? " collapsed" : "")}>
</span>
{s.title}
{s.subtitle ? <span class="subtitle"> ({s.subtitle})</span> : null}
</h3>
);
const modifiers = unref(collapsed[i]) ? null : (
<>
<div class="modifier-container">
<span class="modifier-amount">
{format(unref(processedBase[i]) ?? 1)}
{s.unit}
</span>
<span class="modifier-description">
{renderJSX(unref(processedBaseText[i]) ?? "Base")}
</span>
</div>
{renderJSX(unref(s.modifier.description))}
</>
);
return (
<>
{i === 0 ? null : <br />}
<div>
{header}
<br />
{modifiers}
<hr />
Total: {format(s.modifier.apply(unref(processedBase[i]) ?? 1))}
{s.unit}
</div>
</>
);
});
return <>{sectionJSX}</>;
});
return [jsxFunc, collapsed];
}