parent
52f05d96fd
commit
04d373b6b4
@ -0,0 +1,6 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<project version="4"> |
||||
<component name="VcsDirectoryMappings"> |
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> |
||||
</component> |
||||
</project> |
@ -1,46 +1,4 @@ |
||||
# Getting Started with Create React App |
||||
# Image Sources |
||||
|
||||
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). |
||||
|
||||
## Available Scripts |
||||
|
||||
In the project directory, you can run: |
||||
|
||||
### `npm start` |
||||
|
||||
Runs the app in the development mode.\ |
||||
Open [http://localhost:3000](http://localhost:3000) to view it in the browser. |
||||
|
||||
The page will reload if you make edits.\ |
||||
You will also see any lint errors in the console. |
||||
|
||||
### `npm test` |
||||
|
||||
Launches the test runner in the interactive watch mode.\ |
||||
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. |
||||
|
||||
### `npm run build` |
||||
|
||||
Builds the app for production to the `build` folder.\ |
||||
It correctly bundles React in production mode and optimizes the build for the best performance. |
||||
|
||||
The build is minified and the filenames include the hashes.\ |
||||
Your app is ready to be deployed! |
||||
|
||||
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. |
||||
|
||||
### `npm run eject` |
||||
|
||||
**Note: this is a one-way operation. Once you `eject`, you can’t go back!** |
||||
|
||||
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. |
||||
|
||||
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. |
||||
|
||||
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. |
||||
|
||||
## Learn More |
||||
|
||||
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). |
||||
|
||||
To learn React, check out the [React documentation](https://reactjs.org/). |
||||
* src/fabula-points.svg: https://game-icons.net/1x1/lorc/star-swirl.html |
||||
* src/ultima-points.svg: https://game-icons.net/1x1/lorc/evil-moon.html |
@ -0,0 +1,181 @@ |
||||
.characterStatus { |
||||
height: 150px; |
||||
width: 500px; |
||||
position: relative; |
||||
background-color: #454; |
||||
} |
||||
|
||||
.characterHeader { |
||||
position: absolute; |
||||
left: 160px; |
||||
bottom: 60px; |
||||
z-index: 4; |
||||
} |
||||
|
||||
.characterLevel { |
||||
display: inline; |
||||
color: white; |
||||
-webkit-text-stroke: 1px rgba(0, 0, 0, 0.2); |
||||
text-shadow: 0 0 2px black; |
||||
margin-right: 0.25em; |
||||
} |
||||
|
||||
.characterLevelLabel { |
||||
font-size: smaller; |
||||
font-variant: small-caps; |
||||
} |
||||
|
||||
.characterName { |
||||
display: inline; |
||||
color: white; |
||||
font-family: sans-serif; |
||||
font-weight: bold; |
||||
font-size: 30px; |
||||
text-align: left; |
||||
-webkit-text-stroke: 1px rgba(0, 0, 0, 0.5); |
||||
text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5); |
||||
} |
||||
|
||||
.characterHpBar, .characterMpBar, .characterIpBar { |
||||
box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5); |
||||
border: 2px solid black; |
||||
transform: skewX(-30deg) translateX(15px); |
||||
border-radius: 5px; |
||||
position: absolute; |
||||
width: calc(100% - 30px); |
||||
margin: 2px 2px 8px 5px; |
||||
bottom: 0; |
||||
} |
||||
|
||||
.characterHpValue, .characterMpValue, .characterIpValue { |
||||
color: white; |
||||
font-family: sans-serif; |
||||
font-weight: bold; |
||||
font-style: italic; |
||||
font-size: 60px; |
||||
text-align: right; |
||||
-webkit-text-stroke: 1px black; |
||||
text-shadow: 2px 2px rgba(0, 0, 0, 0.5); |
||||
position: absolute; |
||||
bottom: 5px; |
||||
} |
||||
|
||||
.characterHp { |
||||
position: absolute; |
||||
height: 60px; |
||||
left: 112px; |
||||
right: 0; |
||||
bottom: 28px; |
||||
overflow: visible; |
||||
z-index: 1; |
||||
} |
||||
|
||||
.characterHpBar { |
||||
height: 25px; |
||||
} |
||||
|
||||
.characterHpValue { |
||||
font-size: 60px; |
||||
right: 5px; |
||||
} |
||||
|
||||
.characterMp, .characterIp { |
||||
position: absolute; |
||||
bottom: 0; |
||||
height: 40px; |
||||
} |
||||
|
||||
.characterMp { |
||||
left: 95px; |
||||
right: 155px; |
||||
z-index: 3; |
||||
} |
||||
|
||||
.characterIp { |
||||
width: 150px; |
||||
right: 20px; |
||||
z-index: 2; |
||||
} |
||||
|
||||
.characterMpBar, .characterIpBar { |
||||
height: 20px; |
||||
} |
||||
|
||||
.characterMpValue, .characterIpValue { |
||||
font-size: 40px; |
||||
right: 10px; |
||||
} |
||||
|
||||
.characterMpBar { |
||||
height: 20px; |
||||
} |
||||
|
||||
.characterMpValue { |
||||
font-size: 40px; |
||||
} |
||||
|
||||
.characterPortrait { |
||||
position: absolute; |
||||
top: 10px; |
||||
bottom: 15px; |
||||
left: 50px; |
||||
width: 125px; |
||||
background: no-repeat center / cover; |
||||
z-index: 0; |
||||
} |
||||
|
||||
.characterTurns { |
||||
position: absolute; |
||||
top: 5px; |
||||
left: 5px; |
||||
width: 40px; |
||||
height: 40px; |
||||
font-size: 40px; |
||||
font-weight: bold; |
||||
-webkit-text-stroke: 2px black; |
||||
text-shadow: 2px 2px 2px black ; |
||||
line-height: 40px; |
||||
text-align: center; |
||||
box-sizing: border-box; |
||||
border: 2px solid black; |
||||
border-radius: 9px 0; |
||||
color: white; |
||||
background-color: deepskyblue; |
||||
} |
||||
|
||||
.characterTurnsDone { |
||||
background-color: darkblue; |
||||
} |
||||
|
||||
.characterSp { |
||||
position: absolute; |
||||
color: white; |
||||
line-height: 40px; |
||||
-webkit-text-stroke: 2px black; |
||||
text-shadow: 2px 2px 2px black ; |
||||
font-size: 30px; |
||||
letter-spacing: -3px; |
||||
text-align: center; |
||||
font-weight: bold; |
||||
bottom: 5px; |
||||
left: 5px; |
||||
width: 40px; |
||||
height: 40px; |
||||
} |
||||
|
||||
.characterSpFabula { |
||||
background: url("fabula-points.svg"); |
||||
} |
||||
|
||||
.characterSpUltima { |
||||
background: url("ultima-points.svg"); |
||||
} |
||||
|
||||
.characterSp { |
||||
opacity: 50%; |
||||
transition: opacity 0.3s ease-out; |
||||
} |
||||
|
||||
.characterSp:hover { |
||||
opacity: 100%; |
||||
} |
@ -0,0 +1,232 @@ |
||||
import { animated } from "@react-spring/web"; |
||||
import {ReactElement, useMemo} from "react"; |
||||
import { |
||||
evaluateResourceBarStyles, |
||||
ResourceBarColors, |
||||
ResourceBarStyles |
||||
} from "./resource_bar"; |
||||
import {isDefined} from "./type_check"; |
||||
import { |
||||
SpringyValueInterpolatables, |
||||
useSpringyValue |
||||
} from "./SpringyValueHook"; |
||||
import "./CharacterStatus.css"; |
||||
|
||||
export enum CharacterHealth { |
||||
Full = "Full", |
||||
Healthy = "Healthy", |
||||
Wounded = "Wounded", |
||||
Crisis = "Crisis", |
||||
Peril = "Peril", |
||||
KO = "KO", |
||||
} |
||||
|
||||
export enum CharacterTurnState { |
||||
None = "None", |
||||
Ready = "Ready", |
||||
HighTurns = "HighTurns", |
||||
Done = "Done", |
||||
CantAct = "CantAct", |
||||
KO = "KO", |
||||
} |
||||
|
||||
export enum SPType { |
||||
UltimaPoints = "Ultima", |
||||
FabulaPoints = "Fabula", |
||||
} |
||||
|
||||
export interface StatusEffect { |
||||
name: string |
||||
iconUrl: string |
||||
} |
||||
|
||||
export interface Character { |
||||
portraitUrl?: string |
||||
name?: string |
||||
level?: number |
||||
hp?: number |
||||
maxHp?: number |
||||
health?: CharacterHealth |
||||
mp?: number |
||||
maxMp?: number |
||||
ip?: number |
||||
maxIp?: number |
||||
sp?: number |
||||
spType?: SPType |
||||
turnsLeft?: number |
||||
turnsTotal?: number |
||||
canAct?: boolean |
||||
statuses?: StatusEffect[] |
||||
} |
||||
|
||||
const hpBarStyle: SpringyValueInterpolatables<ResourceBarStyles> = { |
||||
foreground: 'linear-gradient(to bottom, rgb(255, 255, 255, 0.1) 0%, rgb(0, 0, 0, 0) 30% 50%, rgb(0, 0, 0, 0.1) 80%, rgb(0, 0, 0, 0.2) 95%, rgb(0, 0, 0, 0.3) 100%)', |
||||
barDirection: "to right", |
||||
barColors: ({flashValue}: {flashValue: number}): ResourceBarColors => { |
||||
return { |
||||
emptiedColor: `rgb(${Math.min(1, Math.max(flashValue, 0)) * 55 + 55}, 55, 55)`, |
||||
toEmptyColor: 'rgb(200, 0, 0)', |
||||
toFillColor: 'rgb(150, 250, 250)', |
||||
filledColor: 'rgb(0, 200, 0)', |
||||
} |
||||
}, |
||||
} |
||||
|
||||
const mpBarStyle: SpringyValueInterpolatables<ResourceBarStyles> = { |
||||
foreground: 'linear-gradient(to bottom, rgb(255, 255, 255, 0.1) 0%, rgb(0, 0, 0, 0) 30% 50%, rgb(0, 0, 0, 0.1) 80%, rgb(0, 0, 0, 0.2) 95%, rgb(0, 0, 0, 0.3) 100%)', |
||||
barColors: ({flashValue}: {flashValue: number}): ResourceBarColors => { |
||||
return { |
||||
emptiedColor: `rgb(${Math.min(1, Math.max(flashValue, 0)) * 55 + 55}, 55, 55)`, |
||||
toEmptyColor: 'rgb(250, 250, 60)', |
||||
toFillColor: 'rgb(150, 250, 250)', |
||||
filledColor: 'rgb(0, 150, 255)', |
||||
} |
||||
}, |
||||
} |
||||
|
||||
const ipBarStyle: SpringyValueInterpolatables<ResourceBarStyles> = { |
||||
foreground: 'linear-gradient(to bottom, rgb(255, 255, 255, 0.1) 0%, rgb(0, 0, 0, 0) 30% 50%, rgb(0, 0, 0, 0.1) 80%, rgb(0, 0, 0, 0.2) 95%, rgb(0, 0, 0, 0.3) 100%)', |
||||
barColors: ({flashValue}: {flashValue: number}): ResourceBarColors => { |
||||
return { |
||||
emptiedColor: `rgb(${Math.min(1, Math.max(flashValue, 0)) * 55 + 55}, 55, 55)`, |
||||
toEmptyColor: 'rgb(160, 55, 195)', |
||||
toFillColor: 'rgb(250, 250, 60)', |
||||
filledColor: 'rgb(255, 150, 55)', |
||||
} |
||||
}, |
||||
} |
||||
|
||||
export function CharacterStatus({character}: {character: Character}): ReactElement { |
||||
const {name, level, health} = character |
||||
|
||||
const {hp, maxHp} = character |
||||
const {interpolate: hpInterpolate} = useSpringyValue({ |
||||
current: hp, |
||||
max: maxHp, |
||||
flash: isDefined(maxHp) && isDefined(hp) && hp * 2 < maxHp && hp > 0, |
||||
}) |
||||
const {hpText, hpBarStyleInterpolated} = useMemo(() => { |
||||
if (isDefined(hp) && isDefined(maxHp) && maxHp > 0) { |
||||
return { |
||||
hpText: hpInterpolate(({recentValue}) => `${Math.round(recentValue)}`), |
||||
hpBarStyleInterpolated: evaluateResourceBarStyles(hpBarStyle, hpInterpolate), |
||||
} |
||||
} else { |
||||
return {} |
||||
} |
||||
}, [hp, maxHp, hpInterpolate]) |
||||
|
||||
const {mp, maxMp} = character |
||||
const {interpolate: mpInterpolate} = useSpringyValue({ |
||||
current: mp, |
||||
max: maxMp, |
||||
flash: false, |
||||
}) |
||||
const {mpText, mpBarStyleInterpolated} = useMemo(() => { |
||||
if (isDefined(mp) && isDefined(maxMp) && maxMp > 0) { |
||||
return { |
||||
mpText: mpInterpolate(({recentValue}) => `${Math.round(recentValue)}`), |
||||
mpBarStyleInterpolated: evaluateResourceBarStyles(mpBarStyle, mpInterpolate), |
||||
} |
||||
} else { |
||||
return {} |
||||
} |
||||
}, [mp, maxMp, mpInterpolate]) |
||||
|
||||
const {ip, maxIp} = character |
||||
const {interpolate: ipInterpolate} = useSpringyValue({ |
||||
current: ip, |
||||
max: maxIp, |
||||
flash: false, |
||||
}) |
||||
const {ipText, ipBarStyleInterpolated} = useMemo(() => { |
||||
if (isDefined(ip) && isDefined(maxIp) && maxIp > 0) { |
||||
return { |
||||
ipText: ipInterpolate(({recentValue}) => `${Math.round(recentValue)}`), |
||||
ipBarStyleInterpolated: evaluateResourceBarStyles(ipBarStyle, ipInterpolate), |
||||
} |
||||
} else { |
||||
return {} |
||||
} |
||||
}, [ip, maxIp, ipInterpolate]) |
||||
|
||||
const {sp, spType} = character |
||||
const {interpolate: spInterpolate} = useSpringyValue({ |
||||
current: sp, |
||||
flash: isDefined(spType) && isDefined(sp) && sp > 0, |
||||
}) |
||||
const {spText} = useMemo(() => { |
||||
if (isDefined(sp) && isDefined(spType)) { |
||||
return { |
||||
spText: spInterpolate(({recentValue}) => recentValue.toFixed(0)) |
||||
} |
||||
} else { |
||||
return {} |
||||
} |
||||
}, [sp, spType, spInterpolate]) |
||||
|
||||
const {turnsLeft, turnsTotal, canAct} = character |
||||
const {turnsState, turnsText} = useMemo(() => { |
||||
if (isDefined(turnsTotal) && hp === 0 && isDefined(maxHp) && maxHp > 0) { |
||||
return { |
||||
turnsState: CharacterTurnState.KO, |
||||
} |
||||
} else if (isDefined(turnsTotal) && (canAct === false || turnsTotal === 0)) { |
||||
return { |
||||
turnsState: CharacterTurnState.CantAct, |
||||
} |
||||
} else if (isDefined(turnsTotal) && turnsLeft === 0) { |
||||
return { |
||||
turnsState: CharacterTurnState.Done, |
||||
} |
||||
} else if (turnsTotal === 1 && turnsLeft === 1) { |
||||
return { |
||||
turnsState: CharacterTurnState.Ready, |
||||
} |
||||
} else if (isDefined(turnsTotal) && turnsTotal > 1 && isDefined(turnsLeft)) { |
||||
return { |
||||
turnsState: CharacterTurnState.HighTurns, |
||||
turnsText: `${turnsLeft}` |
||||
} |
||||
} else { |
||||
return { |
||||
turnsState: CharacterTurnState.None |
||||
} |
||||
} |
||||
}, [hp, maxHp, canAct, turnsLeft, turnsTotal]) |
||||
|
||||
return <div className="characterStatus"> |
||||
<div className={"characterPortrait"} /> |
||||
{isDefined(turnsState) && |
||||
<div className={"characterTurns characterTurns" + turnsState}>{turnsText}</div>} |
||||
<div className={"characterHeader"}> |
||||
{isDefined(level) && |
||||
<div className="characterLevel"> |
||||
<span className="characterLevelLabel">Lv</span> |
||||
<span className="characterLevelValue">{level}</span> |
||||
</div>} |
||||
{isDefined(name) && |
||||
<div className={"characterName characterName" + (health ?? "Unknown")}>{name}</div>} |
||||
</div> |
||||
{isDefined(hpText) && |
||||
<div className={"characterHp"}> |
||||
<animated.div className={"characterHpBar"} style={hpBarStyleInterpolated} /> |
||||
<animated.div className={"characterHpValue"}>{hpText}</animated.div> |
||||
</div>} |
||||
{isDefined(mpText) && |
||||
<div className={"characterMp"}> |
||||
<animated.div className={"characterMpBar"} style={mpBarStyleInterpolated} /> |
||||
<animated.div className={"characterMpValue"}>{mpText}</animated.div> |
||||
</div>} |
||||
{isDefined(ipText) && |
||||
<div className={"characterIp"}> |
||||
<animated.div className={"characterIpBar"} style={ipBarStyleInterpolated} /> |
||||
<animated.div className={"characterIpValue"}>{ipText}</animated.div> |
||||
</div>} |
||||
{isDefined(spText) && |
||||
<animated.div className={"characterSp characterSp" + spType}> |
||||
<animated.span className={"characterSpValue characterSpValue" + spType}> |
||||
{spText}</animated.span> |
||||
</animated.div>} |
||||
</div> |
||||
} |
@ -0,0 +1,131 @@ |
||||
import {useCallback, useMemo, useState} from "react"; |
||||
import {Interpolation, SpringConfig, SpringValue, to, useSpring, useTrail} from "@react-spring/web"; |
||||
|
||||
export interface UseSpringyValueProps { |
||||
current?: number |
||||
max?: number |
||||
flash?: boolean |
||||
springDelays?: readonly [number, number, number] |
||||
springConfigs?: readonly [SpringConfig, SpringConfig, SpringConfig] |
||||
flashConfig?: SpringConfig |
||||
} |
||||
|
||||
export interface UseSpringyValueOutput { |
||||
springs: {v: SpringValue}[] |
||||
flashSpring: {v: SpringValue} |
||||
interpolate: SpringyValueInterpolate |
||||
} |
||||
|
||||
export interface SpringyValues { |
||||
currentValue?: number // The true current value of the resource, after the recent delta.
|
||||
displayedValue: number // The displayed current value of the resource, after the recent delta.
|
||||
newValue: number // The displayed value of the resource as of ~2 seconds ago.
|
||||
recentValue: number // The end of the delta that's being applied.
|
||||
maxValue?: number // The maximum value of the resource.
|
||||
previousValue?: number // The original value before the most recent delta.
|
||||
flashValue: number // The position in the oscillation of the flashing resource.
|
||||
flashing?: boolean // Whether the resource is actively flashing.
|
||||
} |
||||
|
||||
const literalSymbol: unique symbol = Symbol("SpringyValueLiteralSymbol") |
||||
export type SpringyValueLiteral<T> = {[literalSymbol]: T} |
||||
export function markSpringyValueLiteral<T>(value: T): SpringyValueLiteral<T> { |
||||
return {[literalSymbol]: value} |
||||
} |
||||
export function isSpringyValueLiteral<T>(value: SpringyValueInterpolatable<T>): value is SpringyValueLiteral<T> { |
||||
return typeof value === "object" && value !== null && Object.hasOwn(value, literalSymbol) |
||||
} |
||||
export type SpringyValueInterpolator<T> = (v: SpringyValues) => T |
||||
export type SpringyValueInterpolatable<T> = SpringyValueLiteral<T> | Exclude<T, Function> | SpringyValueInterpolator<T> |
||||
export type SpringyValueInterpolate = <T>(v: SpringyValueInterpolatable<T>) => SpringyValueInterpolated<T> |
||||
export type SpringyValueInterpolated<T> = T | Interpolation<T> |
||||
export type SpringyValueInterpolatables<TargetType extends object> = |
||||
{[Property in keyof TargetType]: SpringyValueInterpolatable<TargetType[Property]>} |
||||
export type SpringyValueInterpolateds<TargetType extends object> = |
||||
{[Property in keyof TargetType]: SpringyValueInterpolated<TargetType[Property]>} |
||||
|
||||
export function evaluateSpringyValueInterpolator<T>(f: SpringyValueInterpolatable<T>, v: SpringyValues): T { |
||||
if (f instanceof Function) { |
||||
return f(v) |
||||
} else if (isSpringyValueLiteral(f)) { |
||||
return f[literalSymbol] |
||||
} else { |
||||
return f |
||||
} |
||||
} |
||||
export function interpolateSpringyValueInterpolatables<T extends object>(values: SpringyValueInterpolatables<T>, interpolator: SpringyValueInterpolate): SpringyValueInterpolateds<T> { |
||||
const result: {[key: string]: SpringyValueInterpolated<any>} = {} |
||||
for (const [key, value] of Object.entries(values) as [string, SpringyValueInterpolatable<any>][]) { |
||||
result[key] = interpolator(value) |
||||
} |
||||
return result as SpringyValueInterpolateds<T> |
||||
} |
||||
|
||||
const DEFAULT_SPRING_DELAYS = [0, 500, 50] as const |
||||
const DEFAULT_SPRING_CONFIGS: readonly [SpringConfig, SpringConfig, SpringConfig] = [{tension: 1200, friction: 40, precision: 0.1, round: 0.001, clamp: true}, {tension: 200, friction: 20, precision: 0.1, round: 0.001}, {mass: 2, tension: 200, friction: 90, precision: 0.1, round: 0.001}] |
||||
const DEFAULT_FLASH_CONFIG: SpringConfig = { |
||||
tension: 170, |
||||
friction: 26, |
||||
clamp: true, |
||||
} |
||||
|
||||
export function useSpringyValue({current, max, flash, springConfigs=DEFAULT_SPRING_CONFIGS, springDelays=DEFAULT_SPRING_DELAYS, flashConfig=DEFAULT_FLASH_CONFIG}: UseSpringyValueProps): UseSpringyValueOutput { |
||||
const [lastCurrent, setLastCurrent] = useState(current) |
||||
const [wasFlashing, setWasFlashing] = useState(flash) |
||||
|
||||
const [springs, barApi] = useTrail(3, () => ({ |
||||
v: current ?? 0 |
||||
}), []) |
||||
const [flashSpring, flashApi] = useSpring({ |
||||
from: {v: 0}, |
||||
to: flash ? [{v: 1}, {v: 0}] : [{v: 0}], |
||||
loop: flash, |
||||
config: flashConfig, |
||||
}, []) |
||||
|
||||
const interpolate = useCallback(function <T>(v: SpringyValueInterpolatable<T>): SpringyValueInterpolated<T> { |
||||
if (v instanceof Function) { |
||||
return to([...springs.map(s => s.v), flashSpring.v], |
||||
(displayedValue: number, newValue: number, recentValue: number, flashValue: number) => v({ |
||||
currentValue: current, |
||||
displayedValue, |
||||
newValue, |
||||
recentValue, |
||||
flashValue, |
||||
maxValue: max, |
||||
previousValue: lastCurrent, |
||||
flashing: flash ?? false |
||||
})) |
||||
} else if (isSpringyValueLiteral(v)) { |
||||
return v[literalSymbol] |
||||
} else { |
||||
return v |
||||
} |
||||
}, [current, lastCurrent, max, flash, springs, flashSpring.v]) |
||||
|
||||
if (flash !== wasFlashing) { |
||||
if (flash && !wasFlashing) { |
||||
flashApi.start({to: [{v: 1}, {v: 0}], loop: true}) |
||||
} else if (!flash && wasFlashing) { |
||||
flashApi.start({to: [{v: 0}], loop: false}) |
||||
} |
||||
setWasFlashing(flash) |
||||
} |
||||
|
||||
if (current !== lastCurrent) { |
||||
barApi.stop(true) |
||||
if ((lastCurrent ?? 0) < (current ?? 0)) { |
||||
barApi.set((idx, ctrl) => ({ v: Math.max(lastCurrent ?? 0, ctrl.get().v) })) |
||||
} else { |
||||
barApi.set((idx, ctrl) => ({ v: Math.min(lastCurrent ?? 0, ctrl.get().v) })) |
||||
} |
||||
barApi.start((i) => ({to: [{v: current ?? 0}], immediate: false, delay: springDelays[i], config: springConfigs[i]})) |
||||
setLastCurrent(current) |
||||
} |
||||
|
||||
return useMemo(() => ({ |
||||
springs, |
||||
flashSpring, |
||||
interpolate, |
||||
}), [springs, flashSpring, interpolate]) |
||||
} |
After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 2.6 KiB |
@ -0,0 +1,81 @@ |
||||
import CSS from "csstype"; |
||||
import { |
||||
evaluateSpringyValueInterpolator, interpolateSpringyValueInterpolatables, |
||||
SpringyValueInterpolatables, |
||||
SpringyValueInterpolate, |
||||
SpringyValueInterpolateds, |
||||
SpringyValues |
||||
} from "./SpringyValueHook"; |
||||
import {isDefined} from "./type_check"; |
||||
|
||||
export interface ResourceBarColors { |
||||
filledColor: CSS.Property.Color |
||||
toFillColor: CSS.Property.Color |
||||
toEmptyColor: CSS.Property.Color |
||||
emptiedColor: CSS.Property.Color |
||||
} |
||||
|
||||
export function resourceGradient( |
||||
{filledColor, toFillColor, toEmptyColor, emptiedColor}: ResourceBarColors, |
||||
{displayedValue, recentValue, newValue, maxValue}: SpringyValues, |
||||
direction: string): string { |
||||
const effectiveMax = isDefined(maxValue) && maxValue !== 0 ? maxValue : Number.POSITIVE_INFINITY |
||||
const currentStop = Math.min(Math.max(100 * displayedValue / effectiveMax, 0), 100) |
||||
const currentSoftEdgeLeft = Math.min(Math.max(currentStop - 0.2, 0), 100) |
||||
const currentSoftEdgeRight = Math.min(Math.max(currentStop + 0.2, 0), 100) |
||||
|
||||
const endFillingStop = Math.min(Math.max(100 * recentValue / effectiveMax, 0), currentStop) |
||||
const midFillingStop = Math.min(Math.max(100 * newValue / effectiveMax, endFillingStop), currentStop) |
||||
|
||||
const endFadingStop = Math.min(Math.max(100 * recentValue / effectiveMax, currentStop), 100) |
||||
const midFadingStop = |
||||
Math.min(Math.max(100 * newValue / effectiveMax, currentStop), endFadingStop) |
||||
|
||||
const layers = [] |
||||
if (currentStop === 100) { |
||||
layers.unshift(filledColor) |
||||
} else { |
||||
layers.unshift(emptiedColor) |
||||
if (currentStop > 0) { |
||||
layers.unshift(`linear-gradient(${direction}, ${filledColor} 0% ${currentSoftEdgeLeft.toFixed(4)}%, transparent ${currentSoftEdgeRight.toFixed(4)}% 100%)`) |
||||
} |
||||
} |
||||
if (endFillingStop !== currentStop || midFillingStop !== currentStop) { |
||||
layers.unshift(`linear-gradient(${direction}, transparent 0% ${endFillingStop.toFixed(4)}%, ${toFillColor} ${midFillingStop.toFixed(4)}% ${currentSoftEdgeLeft.toFixed(4)}%, transparent ${currentSoftEdgeRight.toFixed(4)}% 100%)`) |
||||
} |
||||
if (endFadingStop !== currentStop || midFadingStop !== currentStop) { |
||||
layers.unshift(`linear-gradient(${direction}, transparent 0% ${currentSoftEdgeLeft.toFixed(4)}%, ${toEmptyColor} ${currentSoftEdgeRight.toFixed(4)}% ${midFadingStop.toFixed(4)}%, transparent ${endFadingStop.toFixed(4)}% 100%)`) |
||||
} |
||||
return layers.join(", ") |
||||
} |
||||
|
||||
export interface ResourceBarStyles extends CSS.Properties { |
||||
foreground?: CSS.Properties["background"] |
||||
barDirection?: string |
||||
barColors?: ResourceBarColors |
||||
} |
||||
|
||||
export const DEFAULT_BAR_COLORS: ResourceBarColors = { |
||||
emptiedColor: 'black', |
||||
filledColor: 'limegreen', |
||||
toEmptyColor: 'red', |
||||
toFillColor: 'cyan', |
||||
} as const |
||||
|
||||
export const DEFAULT_BAR_DIRECTION: string = "to right" |
||||
|
||||
export function evaluateResourceBarStyles(barStyle: SpringyValueInterpolatables<ResourceBarStyles>, interpolate: SpringyValueInterpolate) { |
||||
const intermediate: SpringyValueInterpolatables<ResourceBarStyles> = Object.assign({}, barStyle) |
||||
delete intermediate.background |
||||
delete intermediate.foreground |
||||
delete intermediate.barColors |
||||
delete intermediate.barDirection |
||||
intermediate.background = (v: SpringyValues) => [ |
||||
evaluateSpringyValueInterpolator(barStyle["foreground"], v) as string | undefined, |
||||
resourceGradient( |
||||
evaluateSpringyValueInterpolator(barStyle["barColors"], v) ?? DEFAULT_BAR_COLORS, v, |
||||
evaluateSpringyValueInterpolator(barStyle["barDirection"], v) ?? DEFAULT_BAR_DIRECTION), |
||||
evaluateSpringyValueInterpolator(barStyle["background"], v) as string | undefined |
||||
].filter((s) => !!s).join(', ') |
||||
return interpolateSpringyValueInterpolatables(intermediate as SpringyValueInterpolateds<CSS.Properties>, interpolate) |
||||
} |
@ -0,0 +1,3 @@ |
||||
export function isDefined<T>(i: T | undefined): i is T { |
||||
return typeof i !== "undefined" |
||||
} |
After Width: | Height: | Size: 2.2 KiB |
Loading…
Reference in new issue