import { InputBlockBase, type InputBlock } from "./inputBlock.js"; import { type BaseBlock } from "./baseBlock.js"; import { ConnectionPointType } from "../connection/connectionPointType.js"; import { type BooleanInputBlockData, type Color3InputBlockData, type Color4InputBlockData, type FloatInputBlockData, type SerializedInputBlockData, type TextureInputBlockData, type Vector2InputBlockData, } from "./inputBlock.serialization.types.js"; import { type IBlockSerializerV1 } from "../serialization/v1/smartFilterSerialization.types.js"; /** * Determines which generic type of InputBlock we are trying to serialize and calls the appropriate function * to serialize the specifics for that type of InputBlock * @param inputBlock - The InputBlock to serialize * @returns Serialized data for the InputBlock */ function SerializeInputBlockData(inputBlock: InputBlockBase): SerializedInputBlockData { switch (inputBlock.type) { case ConnectionPointType.Texture: return SerializeTextureInputBlock(inputBlock as InputBlock); case ConnectionPointType.Boolean: return SerializeBooleanInputBlock(inputBlock as InputBlock); case ConnectionPointType.Float: return SerializeFloatInputBlock(inputBlock as InputBlock); case ConnectionPointType.Color3: return SerializeColor3InputBlock(inputBlock as InputBlock); case ConnectionPointType.Color4: return SerializeColor4InputBlock(inputBlock as InputBlock); case ConnectionPointType.Vector2: return SerializeVector2InputBlock(inputBlock as InputBlock); } } /** * Generates the serialized data for a Texture InputBlock * @param inputBlock - The Texture InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeTextureInputBlock(inputBlock: InputBlock): TextureInputBlockData { const internalTexture = inputBlock.runtimeValue.value?.getInternalTexture(); const forcedExtension = internalTexture?._extension ?? null; let url = internalTexture?.url ?? null; if (url === "" || !url) { url = inputBlock.editorData?.url ?? null; } return { inputType: ConnectionPointType.Texture, url, urlTypeHint: inputBlock.editorData?.urlTypeHint ?? null, flipY: internalTexture?.invertY ?? null, anisotropicFilteringLevel: internalTexture?.anisotropicFilteringLevel ?? null, forcedExtension: forcedExtension !== "" ? forcedExtension : null, appMetadata: inputBlock.appMetadata, }; } /** * Generates the serialized data for a Boolean InputBlock * @param inputBlock - The Boolean InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeBooleanInputBlock(inputBlock: InputBlock): BooleanInputBlockData { return { inputType: ConnectionPointType.Boolean, value: inputBlock.runtimeValue.value, appMetadata: inputBlock.appMetadata, }; } /** * Generates the serialized data for a Float InputBlock * @param inputBlock - The Float InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeFloatInputBlock(inputBlock: InputBlock): FloatInputBlockData { return { inputType: ConnectionPointType.Float, value: inputBlock.runtimeValue.value, animationType: inputBlock.editorData?.animationType ?? null, valueDeltaPerMs: inputBlock.editorData?.valueDeltaPerMs ?? null, min: inputBlock.editorData?.min ?? null, max: inputBlock.editorData?.max ?? null, appMetadata: inputBlock.appMetadata, }; } /** * Generates the serialized data for a Color3 InputBlock * @param inputBlock - The Color3 InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeColor3InputBlock(inputBlock: InputBlock): Color3InputBlockData { return { inputType: ConnectionPointType.Color3, value: inputBlock.runtimeValue.value, appMetadata: inputBlock.appMetadata, }; } /** * Generates the serialized data for a Color4 InputBlock * @param inputBlock - The Color4 InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeColor4InputBlock(inputBlock: InputBlock): Color4InputBlockData { return { inputType: ConnectionPointType.Color4, value: inputBlock.runtimeValue.value, appMetadata: inputBlock.appMetadata, }; } /** * Generates the serialized data for a Vector2 InputBlock * @param inputBlock - The Vector2 InputBlock to serialize * @returns The serialized data for the InputBlock */ function SerializeVector2InputBlock(inputBlock: InputBlock): Vector2InputBlockData { return { inputType: ConnectionPointType.Vector2, value: inputBlock.runtimeValue.value, appMetadata: inputBlock.appMetadata, }; } /** * The V1 serializer for an InputBlock */ export const InputBlockSerializer: IBlockSerializerV1 = { blockType: InputBlockBase.ClassName, serialize: (block: BaseBlock) => { if (block.blockType !== InputBlockBase.ClassName) { throw new Error("Was asked to serialize an unrecognized block type"); } return { name: block.name, uniqueId: block.uniqueId, blockType: InputBlockBase.ClassName, namespace: null, comments: block.comments, data: SerializeInputBlockData(block as unknown as InputBlockBase), }; }, };