import type { DragEndEvent, DragStartEvent } from "@dnd-kit/core"; import { DndContext, DragOverlay, PointerSensor, useDraggable, useSensor, useSensors, } from "@dnd-kit/core"; import { snapCenterToCursor } from "@dnd-kit/modifiers"; import type { Editor, TLAssetId, TLComponents, TLImageAsset, TLShapeId, TLUiStylePanelProps, } from "@tldraw/tldraw"; import { AssetRecordType, createShapeId, DefaultQuickActions, DefaultStylePanel, DefaultZoomMenu, Tldraw, } from "@tldraw/tldraw"; import clsx from "clsx"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { useTheme } from "~/features/theme/core/provider"; import type { LanguageCode } from "~/modules/i18n/config"; import { modesShort } from "~/modules/in-game-lists/modes"; import { stageIds } from "~/modules/in-game-lists/stage-ids"; import type { ModeShort, StageId } from "~/modules/in-game-lists/types"; import { specialWeaponIds, subWeaponIds, weaponCategories, } from "~/modules/in-game-lists/weapon-ids"; import { logger } from "~/utils/logger"; import { mainWeaponImageUrl, modeImageUrl, outlinedMainWeaponImageUrl, specialWeaponImageUrl, stageMinimapImageUrlWithEnding, subWeaponImageUrl, weaponCategoryUrl, } from "~/utils/urls"; import { SendouButton } from "../../../components/elements/Button"; import { Image } from "../../../components/Image"; import type { StageBackgroundStyle } from "../plans-types"; const DROPPED_IMAGE_SIZE_PX = 45; const BACKGROUND_WIDTH = 1127; const BACKGROUND_HEIGHT = 634; export default function Planner() { const { i18n } = useTranslation(); const { htmlThemeClass } = useTheme(); const [editor, setEditor] = React.useState(null); const [imgOutlined, setImgOutlined] = React.useState(false); const [activeDragItem, setActiveDragItem] = React.useState<{ src: string; previewPath: string; } | null>(null); const sensors = useSensors(useSensor(PointerSensor)); const handleMount = React.useCallback( (mountedEditor: Editor) => { setEditor(mountedEditor); mountedEditor.user.updateUserPreferences({ locale: ourLanguageToTldrawLanguage(i18n.language), colorScheme: htmlThemeClass === "dark" ? "dark" : "light", }); }, [i18n, htmlThemeClass], ); const handleAddImage = React.useCallback( ({ src, size, isLocked, point, cb, }: { src: string; size: number[]; isLocked: boolean; point: number[]; cb?: () => void; }) => { if (!editor) return; // tldraw creator: // "So image shapes in tldraw work like this: we add an asset to the app.assets table, then we reference that asset in the shape object itself. // This lets us have multiple copies of an image on the canvas without having all of those take up memory individually" const assetId: TLAssetId = AssetRecordType.createId(); const srcWithOutline = imgOutlined ? `${src}?outline=red` : src; // idk if this is the best solution, but it was the example given and it seems to cope well with lots of shapes at once const imageAsset: TLImageAsset = { id: assetId, type: "image", typeName: "asset", props: { name: "img", src: srcWithOutline, w: size[0], h: size[1], mimeType: null, isAnimated: false, }, meta: {}, }; editor.createAssets([imageAsset]); const shapeId: TLShapeId = createShapeId(); const shape = { type: "image", x: point[0], y: point[1], isLocked: isLocked, id: shapeId, props: { assetId: assetId, w: size[0], h: size[1], }, }; editor.createShape(shape); cb?.(); }, [editor, imgOutlined], ); const handleAddWeaponAtPosition = React.useCallback( (src: string, point: [number, number]) => { const centeredPoint: [number, number] = [ point[0] - DROPPED_IMAGE_SIZE_PX / 2, point[1] - DROPPED_IMAGE_SIZE_PX / 2, ]; handleAddImage({ src, size: [DROPPED_IMAGE_SIZE_PX, DROPPED_IMAGE_SIZE_PX], isLocked: false, point: centeredPoint, cb: () => editor?.setCurrentTool("select"), }); }, [editor, handleAddImage], ); const handleDragStart = (event: DragStartEvent) => { const { src, previewPath } = event.active.data.current as { src: string; previewPath: string; }; setActiveDragItem({ src, previewPath }); }; const handleDragEnd = (event: DragEndEvent) => { setActiveDragItem(null); if (!editor) return; const { active } = event; const { src } = active.data.current as { src: string }; const pointerPosition = event.activatorEvent as PointerEvent; const dropX = pointerPosition.clientX + (event.delta?.x ?? 0); const dropY = pointerPosition.clientY + (event.delta?.y ?? 0); const pagePoint = editor.screenToPage({ x: dropX, y: dropY }); handleAddWeaponAtPosition(src, [pagePoint.x, pagePoint.y]); }; const handleAddBackgroundImage = React.useCallback( (urlArgs: { stageId: StageId; mode: ModeShort; style: StageBackgroundStyle; }) => { if (!editor) return; editor.mark("pre-background-change"); const shapes = editor.getCurrentPageShapes(); // i dont think locked shapes can be deleted for (const value of shapes) { editor.updateShape({ id: value.id, type: value.type, isLocked: false }); } editor.deleteShapes(shapes); handleAddImage({ src: stageMinimapImageUrlWithEnding(urlArgs), size: [BACKGROUND_WIDTH, BACKGROUND_HEIGHT], isLocked: true, point: [0, 0], }); editor.zoomToFit(); }, [editor, handleAddImage], ); // removes all tldraw ui that isnt needed const tldrawComponents: TLComponents = { ActionsMenu: null, ContextMenu: null, DebugMenu: null, DebugPanel: null, HelperButtons: null, HelpMenu: null, KeyboardShortcutsDialog: null, MainMenu: null, MenuPanel: null, Minimap: null, NavigationPanel: null, PageMenu: null, QuickActions: null, SharePanel: null, StylePanel: CustomStylePanel, TopPanel: null, ZoomMenu: null, }; return (
{activeDragItem ? ( ) : null}
); } // Formats the style panel so it can have classnames, this is needed so it can be moved below the header bar which blocks clicks (idk why this is different to the old version), also needed to format the quick actions bar and zoom menu nicely function CustomStylePanel(props: TLUiStylePanelProps) { return (
); } function OutlineToggle({ outlined, setImgOutlined, }: { outlined?: boolean; setImgOutlined: (outline: boolean) => void; }) { const { t } = useTranslation(["common"]); const handleClick = () => { setImgOutlined(!outlined); }; return (
{outlined ? t("common:actions.outlined") : t("common:actions.noOutline")}
); } function DraggableWeaponButton({ id, src, imgPath, previewPath, alt, title, size, }: { id: string; src: string; imgPath: string; previewPath: string; alt: string; title: string; size: number; }) { const { attributes, listeners, setNodeRef, isDragging } = useDraggable({ id, data: { src, previewPath }, }); return ( ); } function WeaponImageSelector() { const { t, i18n } = useTranslation(["weapons", "common", "game-misc"]); const isWide = i18n.language === "fr"; return (
{weaponCategories.map((category) => { return (
{t(`common:weapon.category.${category.name}`)} {t(`common:weapon.category.${category.name}`)}
{category.weaponIds.map((weaponId) => { return ( ); })}
); })}
{t("common:weapon.category.subs")}
{subWeaponIds.map((subWeaponId) => { return ( ); })}
{t("common:weapon.category.specials")}
{specialWeaponIds.map((specialWeaponId) => { return ( ); })}
{t("common:plans.adder.objective")}
{(["TC", "RM", "CB"] as const).map((mode) => { return ( ); })}
); } const LAST_STAGE_ID_WITH_IMAGES = 24; function StageBackgroundSelector({ onAddBackground, }: { onAddBackground: (args: { stageId: StageId; mode: ModeShort; style: StageBackgroundStyle; }) => void; }) { const { t } = useTranslation(["game-misc", "common"]); const [stageId, setStageId] = React.useState(stageIds[0]); const [mode, setMode] = React.useState("SZ"); const [backgroundStyle, setBackgroundStyle] = React.useState("MINI"); const handleStageIdChange = (stageId: StageId) => { setStageId(stageId); }; return (
onAddBackground({ style: backgroundStyle, stageId, mode }) } className="w-max" > {t("common:actions.setBg")}
); } // when adding new language check from Tldraw codebase what is the matching // language in TRANSLATIONS constant, or default to english if none found const ourLanguageToTldrawLanguageMap: Record = { "es-US": "es", "es-ES": "es", ko: "ko-kr", nl: "en", zh: "zh-ch", he: "he", // map to itself da: "da", de: "de", en: "en", "fr-CA": "fr-CA", "fr-EU": "fr-EU", it: "it", ja: "ja", ru: "ru", pl: "pl", "pt-BR": "pt-br", }; function ourLanguageToTldrawLanguage(ourLanguageUserSelected: string) { for (const [ourLanguage, tldrawLanguage] of Object.entries( ourLanguageToTldrawLanguageMap, )) { if (ourLanguage === ourLanguageUserSelected) { return tldrawLanguage; } } logger.error(`No tldraw language found for: ${ourLanguageUserSelected}`); return "en"; }