Add utility function for showing previews of how formulas will change

This commit is contained in:
thepaperpilot 2023-02-15 18:21:38 -06:00
parent adb35191ea
commit be5c3435c6

View file

@ -33,9 +33,10 @@ import type {
} from "util/computed";
import { convertComputable, processComputable } from "util/computed";
import { getFirstFeature, render, renderColJSX, renderJSX, VueFeature } from "util/vue";
import { computed, Ref, unref, watchEffect } from "vue";
import { computed, ComputedRef, Ref, unref, watchEffect } from "vue";
import "./common.css";
import { main } from "./projEntry";
import Formula, { GenericFormula } from "game/formulas";
/** An object that configures a {@link ResetButton} */
export interface ResetButtonOptions extends ClickableOptions {
@ -457,6 +458,41 @@ export function estimateTime(
});
}
export function createFormulaPreview(
formula: GenericFormula,
showPreview: Computable<boolean>,
previewAmount: Computable<DecimalSource> = 1
): ComputedRef<CoercableComponent> {
const processedShowPreview = convertComputable(showPreview);
const processedPreviewAmount = convertComputable(previewAmount);
if (!formula.hasVariable()) {
throw "Cannot create formula preview if the formula does not have a variable";
}
return computed(() => {
if (unref(processedShowPreview)) {
const curr = formatSmall(formula.evaluate());
const preview = formatSmall(
formula.evaluate(
Decimal.add(
unref(formula.innermostVariable ?? 0),
unref(processedPreviewAmount)
)
)
);
return jsx(() => (
<>
<b>
<i>
{curr}{preview}
</i>
</b>
</>
));
}
return formatSmall(formula.evaluate());
});
}
export function setUpDailyProgressTracker(options: {
resource: Resource;
goal: DecimalSource;