import Roact, { createRef } from '@rbxts/roact'; import Hooks from '@rbxts/roact-hooks'; import * as UiUtility from '../../UiUtility'; import { TouchLayoutProps } from './TouchLayout'; export interface KeyboardLayoutProps extends DefaultProps, Pick { } interface DefaultProps { /** 键盘的按键 */ key?: string; } const Padding = [6, 3]; /** 键盘设备的UI布局 */ const KeyboardLayout: Hooks.FC> = (props, { useState, useValue, useMemo, useEffect }) => { const frameRef = createRef(); const labelRef = createRef(); const scaleRef = createRef(); const keySize = new UDim2(0, 18, 0, 18); const keyPosition = new UDim2(0, Padding[0], 0.5, -keySize.X.Offset / 2); const labelPosition = new UDim2(0, keyPosition.X.Offset + keySize.X.Offset + Padding[0] / 2, 0.5, 0); useEffect(() => { const frameRefCurrent = frameRef.getValue(); const labelRefCurrent = labelRef.getValue(); const scaleRefCurrent = scaleRef.getValue(); if (frameRefCurrent && labelRefCurrent) { frameRefCurrent.Size = new UDim2(0, labelPosition.X.Offset + labelRefCurrent.TextBounds.X + Padding[0] / 1.5, 0, keySize.Y.Offset + Padding[1] * 2); frameRefCurrent.Position = new UDim2(0.5, -frameRefCurrent.Size.X.Offset / 2, 0.2, -frameRefCurrent.Size.Y.Offset); } if (scaleRefCurrent) { scaleRefCurrent.Scale = UiUtility.getScale(); } }, [props.text]); return ( {/* KeyNode */} {/* LabelNode */} ); }; export default new Hooks(Roact)(KeyboardLayout, { defaultProps: { key: 'F', text: '', visible: false } });