ISSUE-069 - FL wipe working. Big refactor of how the PositionFlag components work.

I flipped the way the wipe effect works. Originally there was the complicated problem of revealing a flag instantly and then wiping it away, requiring some delicate work with mounting and unmounting components. Instead it now works by always having a colored PositionFlag component in place and temporarily disappearing the normal PositionFlag (that is on top of it) using a transition-free opacity switch that lets the WipeTransition work reliably. This makes for fewer states and conditional renders.
Added textColor and bgColor props to the PositionFlag component for flexibility.
This commit is contained in:
Kyle Geib committed 2022-03-14 08:49:14 -05:00
1 parent 0e8ef14dcd
commit 4bf471bd99
5 files changed
+49 -50

No files matched your search

@@ -7,17 +7,19 @@ type PositionFlagProps = {
size: Px,
number: number,
numberSizeFraction?: Fraction,
color?: string;
bgColor?: string,
numberColor?: string,
};
export function PositionFlag({
size,
number,
numberSizeFraction = 0.64,
color = theme.colors.posWhite,
bgColor = theme.colors.posWhite,
numberColor = theme.colors.black,
}: PositionFlagProps) {
return (
<PositionFlagBox size={size} color={color}>
<PositionNumber size={size * numberSizeFraction}>
<PositionFlagBox size={size} color={bgColor}>
<PositionNumber size={size * numberSizeFraction} color={numberColor}>
{number}
</PositionNumber>
</PositionFlagBox>
@@ -21,10 +21,12 @@ PositionFlagBox.displayName = "PositionFlagBox";
type PositionNumberProps = {
size: Px,
color: string,
};
export const PositionNumber = styled.span<PositionNumberProps>`
font-family: ${p => p.theme.fonts.f1Regular};
font-size: ${({ size }) => size}px;
color: ${({ color }) => color};
margin-top: 2px;
`;
PositionNumber.displayName = "PositionNumber";
@@ -62,31 +62,38 @@ export function TimingTowerRow({
const { design: { timingTower: ttTheme } } = theme;
const [pastPos, setPastPos] = useState(car.position);
const [posChange, setPosChange] = useState(0);
const [pastFLHolder, setPastFLHolder] = useState(false);
const [currentFL, setCurrentFL] = useState(false);
const [pastDisplayMode, setPastDisplayMode] = useState(displayMode);
const [hugRight, setHugRight] = useState(true);
// Using timeouts and boolean states I can manipulate visiblity of the position
// change effects. showPosWipe is true for only 1ms before being switched back off.
const [showPosOutline, setShowPosOutline] = useState(false);
// change effects. When a
const [showOutline, setShowOutline] = useState(false);
const [showPosWipe, setShowPosWipe] = useState(false);
useTimeoutWhen(() => setShowPosOutline(false), (
ttTheme.wipeDelayMs + ttTheme.wipeDurationMs
) * DDM, showPosOutline);
useTimeoutWhen(() => setShowOutline(false), ttTheme.rowTravelDurationMs * DDM, showOutline);
useTimeoutWhen(() => setShowPosWipe(false), 1, showPosWipe);
const [showChangeFlag, setShowChangeFlag] = useState(false);
useTimeoutWhen(() => setShowChangeFlag(false), ttTheme.wipeDelayMs * DDM, showChangeFlag);
const [changeColors, setChangeColors] = useState<[string, string]>(["white", "black"]);
useEffect(() => {
if (car.position !== pastPos) {
// Position change.
setShowChangeFlag(true);
setChangeColors([
car.position < pastPos ? theme.colors.posGainedGreen : theme.colors.posLostRed,
theme.colors.black,
]);
setPastPos(car.position);
setPosChange(pastPos - car.position);
setShowPosOutline(true);
setShowOutline(true);
setShowPosWipe(true);
} else if (car.notices.includes(CarNotice.FastestLap) && !currentFL) {
// Fastest Lap gained.
setShowChangeFlag(true);
setCurrentFL(true);
setChangeColors([theme.colors.posFastestLap, theme.colors.textWhite]);
} else if (currentFL && !car.notices.includes(CarNotice.FastestLap)) {
// Fastest Lap lost.
setCurrentFL(false);
}
}, [car.position]);
}, [car.position, car.notices]);
useEffect(() => {
if (pastDisplayMode !== displayMode) {
@@ -105,7 +112,7 @@ export function TimingTowerRow({
}, [displayMode]);
const { outlineClipPathProgress } = useSpring({
outlineClipPathProgress: showPosOutline ? 1 : 0,
outlineClipPathProgress: showOutline ? 1 : 0,
config: { duration: 333 * DDM },
delay: 0 * DDM,
});
@@ -201,36 +208,34 @@ export function TimingTowerRow({
>
<RowLeftHalfLayout>
{car.status !== CarStatus.Retired && (
/**
* The "Change" PositionFlag (the colored one) is always present but
* usually hidden under a second visible "Normal" PositionFlag. When
* a change is made the normal PositionFlag is hidden instantly and
* after a delay is revealed. With the reveal a WipeTransition also
* takes place. Before, the "Change" PositionFlag would wipe away.
* Now, instead, it is hidden under a revealed "Normal" PositionFlag.
*/
<RowLeftHalfPosFlagContainer size={ttTheme.posFlagSizePx}>
<PositionFlag
size={ttTheme.posFlagSizePx}
number={car.position}
numberSizeFraction={.6}
numberSizeFraction={0.6}
bgColor={changeColors[0]}
numberColor={changeColors[1]}
/>
<RowLeftHalfPosFlagChangeContainer
size={ttTheme.posFlagSizePx}
visible={showPosOutline}
transitionTime={167 * DDM}
>
{(showPosOutline) && (
<RowLeftHalfPosFlagChangeContainer open={!showChangeFlag}>
<WipeTransition
visible={showPosWipe}
delay={ttTheme.wipeDelayMs * DDM}
visible={!showChangeFlag}
angle={45}
duration={ttTheme.wipeDurationMs * DDM}
startingCorner="topLeft"
startingCorner="bottomRight"
>
<PositionFlag
size={ttTheme.posFlagSizePx}
number={car.position}
numberSizeFraction={.6}
color={posChange > 0
? theme.colors.posGainedGreen
: theme.colors.posLostRed}
/>
</WipeTransition>
)}
</RowLeftHalfPosFlagChangeContainer>
</RowLeftHalfPosFlagContainer>
)}
@@ -285,12 +290,8 @@ export function TimingTowerRow({
open={showOutline}
startThickness={1.5}
endThickness={1.5}
startColor={posChange > 0
? theme.colors.posGainedGreen
: theme.colors.posLostRed}
endColor={posChange > 0
? theme.colors.posGainedGreen
: theme.colors.posLostRed}
startColor={changeColors[0]}
endColor={changeColors[0]}
transitionProps={[{
property: "opacity",
duration: 333 * DDM,
@@ -6,6 +6,7 @@ import {
Px,
DirectionalTransitionProps,
TransitionProps,
OpenProps,
} from "../../../../types/style";
import { Milliseconds } from "../../../../types/util";
import { commonDirectionalTransition, commonTransition } from "../../../../utils/styling";
@@ -134,20 +135,12 @@ export const RowLeftHalfPosFlagContainer = styled.div<RowLeftHalfPosFlagContaine
`;
RowLeftHalfPosFlagContainer.displayName = "RowLeftHalfPosFlagContainer";
type RowLeftHalfPosFlagChangeContainerProps = {
size: Px,
visible: boolean,
transitionTime: Milliseconds,
};
export const RowLeftHalfPosFlagChangeContainer = styled.div<RowLeftHalfPosFlagChangeContainerProps>`
export const RowLeftHalfPosFlagChangeContainer = styled.div<OpenProps>`
position: absolute;
top: 0;
left: 0;
height: ${({ size }) => size}px;
width: ${({ size }) => size}px;
opacity: ${({ visible }) => visible ? 1 : 0};
transition: opacity ${({ transitionTime }) => transitionTime}ms;
opacity: ${({ open }) => open ? 1 : 0};
`;
RowLeftHalfPosFlagChangeContainer.displayName = "RowLeftHalfPosFlagChangeContainer";
+1
View File
@@ -21,6 +21,7 @@ export const theme = {
posLostRed: "#e40030",
posGainedGreen: "#10ff35",
posFastestLap: "#ae43ca",
black: "#000",
grey: "#aaa",
lightGrey: "#ddd",
faintGrey: "#eee",