Files
o2o-weather/mobile/src/components/foliage/parts.tsx
T

136 lines
6.2 KiB
TypeScript

import type { Kind, LabelSize, Pt, Spot, Stage } from '@nalssigak/shared/lib/foliage';
import { Image } from 'expo-image';
import { useCallback, useRef, useState, type ReactNode } from 'react';
import { Platform, Pressable, View, type LayoutChangeEvent, type StyleProp, type ViewStyle } from 'react-native';
import { Text } from '@/components/text';
import { Ic } from '@/components/ui';
import { MAP_IMAGE } from '@/lib/foliage';
// 지도는 밤에도 밝은 종이 그대로 (웹 b-keep-bg)
export const PAPER = '#fff6ea';
export const MAP_INK = '#28304a';
export const MAP_INK3 = '#6b7189';
const LABEL_INK: Record<Kind, string> = { maple: '#b5321f', ginkgo: '#8a6508' };
/**
* 지도 그림. 웹은 채도를 빼서(saturate .3) 잎 아이콘이 묻히지 않게 한다.
* 안드로이드는 같은 filter, iOS 는 filter 의 채도를 못 써서 종이색을 반쯤 덮어 옅게 한다
*/
export function MapImage({ style }: { style: StyleProp<ViewStyle> }) {
const android = Platform.OS === 'android';
return (
<View pointerEvents="none" style={[{ position: 'absolute' }, style, android ? { filter: [{ saturate: 0.3 }, { brightness: 1.08 }] } : null]}>
<Image source={MAP_IMAGE} style={{ width: '100%', height: '100%' }} contentFit="contain" accessibilityIgnoresInvertColors />
{!android && <View style={{ position: 'absolute', inset: 0, backgroundColor: PAPER, opacity: 0.5 }} />}
</View>
);
}
const DOT: Record<Exclude<Stage, 'peak'>, { size: number; color: string }> = {
first: { size: 7, color: 'rgba(217,119,46,0.85)' },
before: { size: 5, color: 'rgba(143,138,128,0.6)' },
gone: { size: 5, color: 'rgba(143,138,128,0.6)' },
};
/** 단계 표시: 절정은 잎, 첫 단풍은 주황 점, 단풍 전·지남은 회색 점(big 이면 점을 크게) */
export function StageMark({ m, big }: { m: Spot; big?: boolean }) {
if (m.stage === 'peak') return <Ic name={m.kind} size={big ? 32 : 20} />;
const dot = DOT[m.stage];
const size = big ? 14 : dot.size;
return <View style={{ width: size, height: size, borderRadius: size / 2, backgroundColor: dot.color }} />;
}
/** 지도 위 점·잎. 고른 명소는 속이 빈 고리만 둘러 옆 명소를 가리지 않게 */
export function Marker({ p, sel }: { p: Pt; sel: boolean }) {
return (
<View pointerEvents="none" className="absolute size-8 items-center justify-center" style={{ left: p.x - 16, top: p.y - 16, zIndex: sel ? 3 : 1 }}>
{sel ? <View className="absolute size-8 rounded-full border-[2.5px]" style={{ borderColor: MAP_INK, backgroundColor: 'rgba(255,255,255,0.3)' }} /> : null}
<StageMark m={p.m} big={sel && p.m.stage !== 'peak'} />
</View>
);
}
/** 시·도 이름: 지도에 옅게 깔리는 글자. 산 무늬 위에서도 읽히게 종이색 번짐을 두른다 */
export function Province({ name, x, y }: { name: string; x: number; y: number }) {
return (
<View pointerEvents="none" className="absolute w-10 items-center" style={{ left: x - 20, top: y - 8 }} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
<Text className="font-jua text-[13px] leading-4" style={{ color: 'rgba(92,74,54,0.85)', textShadowColor: PAPER, textShadowRadius: 4 }}>
{name}
</Text>
</View>
);
}
/**
* 명소 이름표. 자리를 못 받았으면(at 없음) 숨긴 채 왼쪽 위에 두고 크기만 잰다 (웹 useLabelSize 대신 onLayout).
* tone: 고름(진한 바탕) · 절정(잎 색 글자) · 그 밖(회색 글자)
*/
export function Label({ m, at, sel, onPress, onLayout, accessibilityLabel }: { m: Spot; at?: { left: number; top: number }; sel?: boolean; onPress: () => void; onLayout: (e: LayoutChangeEvent) => void; accessibilityLabel: string }) {
const color = sel ? '#ffffff' : m.stage === 'peak' ? LABEL_INK[m.kind] : '#5b6075';
return (
<Pressable
onPress={onPress}
onLayout={onLayout}
disabled={!at}
hitSlop={{ top: 8, bottom: 8, left: 4, right: 4 }}
accessibilityRole="button"
accessibilityLabel={accessibilityLabel}
accessibilityElementsHidden={!at}
importantForAccessibility={at ? 'auto' : 'no-hide-descendants'}
className="absolute rounded-[7px] px-[5px] py-px"
style={{
left: at?.left ?? 0,
top: at?.top ?? 0,
opacity: at ? 1 : 0,
zIndex: sel ? 3 : 2,
backgroundColor: sel ? MAP_INK : 'rgba(255,255,255,0.92)',
boxShadow: '0 1px 3px rgba(40,48,74,0.18)',
}}
>
<Text numberOfLines={1} className="text-[11.5px] leading-[15.5px] font-extrabold" style={{ color }}>
{m.name}
</Text>
</Pressable>
);
}
/** 이름표 크기 모으기: Label 의 onLayout 을 받아 이름별 폭·높이. 한 프레임에 한 번만 다시 그린다 */
export function useLabelSizes() {
const [size, setSize] = useState<LabelSize>({});
const acc = useRef<LabelSize>({});
const frame = useRef(0);
const measure = useCallback(
(name: string) => (e: LayoutChangeEvent) => {
const { width: w, height: h } = e.nativeEvent.layout;
const old = acc.current[name];
if (old && Math.abs(old.w - w) < 0.5 && Math.abs(old.h - h) < 0.5) return;
acc.current = { ...acc.current, [name]: { w, h } };
cancelAnimationFrame(frame.current);
frame.current = requestAnimationFrame(() => setSize(acc.current));
},
[],
);
return [size, measure] as const;
}
/** 범례: 절정 잎 둘, full 이면 첫 단풍·단풍 전 점까지 */
export function Legend({ full, color }: { full?: boolean; color: string }) {
const item = (mark: ReactNode, label: string) => (
<View key={label} className="flex-row items-center gap-1">
{mark}
<Text className="text-[12.5px] font-bold" style={{ color }}>
{label}
</Text>
</View>
);
return (
<View className="h-[52px] flex-row flex-wrap items-center justify-center gap-x-3 gap-y-1">
{item(<Ic name="maple" size={16} />, '단풍 절정')}
{item(<Ic name="ginkgo" size={16} />, '은행 절정')}
{full ? item(<View style={{ width: 7, height: 7, borderRadius: 4, backgroundColor: DOT.first.color }} />, '첫 단풍') : null}
{full ? item(<View style={{ width: 5, height: 5, borderRadius: 3, backgroundColor: DOT.before.color }} />, '단풍 전') : null}
</View>
);
}