2025-12-06 14:08:48 +08:00
|
|
|
|
import React, { useState, useRef, useCallback, useEffect, useMemo } from 'react';
|
2025-11-29 23:00:48 +08:00
|
|
|
|
import { Image, X, Navigation, ChevronDown, Copy } from 'lucide-react';
|
|
|
|
|
|
import { convertFileSrc } from '@tauri-apps/api/core';
|
2025-12-08 21:26:35 +08:00
|
|
|
|
import { Core } from '@esengine/ecs-framework';
|
2025-12-06 14:08:48 +08:00
|
|
|
|
import { ProjectService, AssetRegistryService } from '@esengine/editor-core';
|
2025-11-23 14:49:37 +08:00
|
|
|
|
import { AssetPickerDialog } from '../../../components/dialogs/AssetPickerDialog';
|
2025-11-19 14:54:03 +08:00
|
|
|
|
import './AssetField.css';
|
|
|
|
|
|
|
|
|
|
|
|
interface AssetFieldProps {
|
2025-11-23 14:49:37 +08:00
|
|
|
|
label?: string;
|
2025-12-06 14:08:48 +08:00
|
|
|
|
/** Value can be GUID or path (for backward compatibility) */
|
2025-11-19 14:54:03 +08:00
|
|
|
|
value: string | null;
|
|
|
|
|
|
onChange: (value: string | null) => void;
|
2025-11-29 23:00:48 +08:00
|
|
|
|
fileExtension?: string;
|
2025-11-19 14:54:03 +08:00
|
|
|
|
placeholder?: string;
|
|
|
|
|
|
readonly?: boolean;
|
2025-11-29 23:00:48 +08:00
|
|
|
|
onNavigate?: (path: string) => void;
|
|
|
|
|
|
onCreate?: () => void;
|
2025-11-19 14:54:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* Check if a string is a valid UUID v4 (GUID format)
|
|
|
|
|
|
*/
|
|
|
|
|
|
function isGUID(str: string): boolean {
|
|
|
|
|
|
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
|
|
|
|
return uuidRegex.test(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-11-19 14:54:03 +08:00
|
|
|
|
export function AssetField({
|
|
|
|
|
|
label,
|
|
|
|
|
|
value,
|
|
|
|
|
|
onChange,
|
|
|
|
|
|
fileExtension = '',
|
|
|
|
|
|
placeholder = 'None',
|
|
|
|
|
|
readonly = false,
|
2025-11-25 22:23:19 +08:00
|
|
|
|
onNavigate,
|
|
|
|
|
|
onCreate
|
2025-11-19 14:54:03 +08:00
|
|
|
|
}: AssetFieldProps) {
|
|
|
|
|
|
const [isDragging, setIsDragging] = useState(false);
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const [showPicker, setShowPicker] = useState(false);
|
2025-11-29 23:00:48 +08:00
|
|
|
|
const [thumbnailUrl, setThumbnailUrl] = useState<string | null>(null);
|
2025-11-19 14:54:03 +08:00
|
|
|
|
const inputRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// Get AssetRegistryService for GUID ↔ Path conversion
|
|
|
|
|
|
const assetRegistry = useMemo(() => {
|
|
|
|
|
|
return Core.services.tryResolve(AssetRegistryService) as AssetRegistryService | null;
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve value to path (value can be GUID or path)
|
|
|
|
|
|
const resolvedPath = useMemo(() => {
|
|
|
|
|
|
if (!value) return null;
|
|
|
|
|
|
|
|
|
|
|
|
// If value is a GUID, resolve to path
|
|
|
|
|
|
if (isGUID(value) && assetRegistry) {
|
|
|
|
|
|
return assetRegistry.getPathByGuid(value) || null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise treat as path (backward compatibility)
|
|
|
|
|
|
return value;
|
|
|
|
|
|
}, [value, assetRegistry]);
|
|
|
|
|
|
|
2025-11-29 23:00:48 +08:00
|
|
|
|
// 检测是否是图片资源
|
|
|
|
|
|
const isImageAsset = useCallback((path: string | null) => {
|
|
|
|
|
|
if (!path) return false;
|
|
|
|
|
|
return ['.png', '.jpg', '.jpeg', '.gif', '.webp', '.bmp'].some(ext =>
|
|
|
|
|
|
path.toLowerCase().endsWith(ext)
|
|
|
|
|
|
);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// 加载缩略图(使用 resolvedPath)
|
2025-11-29 23:00:48 +08:00
|
|
|
|
useEffect(() => {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
if (resolvedPath && isImageAsset(resolvedPath)) {
|
2025-11-29 23:00:48 +08:00
|
|
|
|
// 获取项目路径并构建完整路径
|
|
|
|
|
|
const projectService = Core.services.tryResolve(ProjectService);
|
|
|
|
|
|
const projectPath = projectService?.getCurrentProject()?.path;
|
|
|
|
|
|
|
|
|
|
|
|
if (projectPath) {
|
|
|
|
|
|
// 构建完整的文件路径
|
2025-12-06 14:08:48 +08:00
|
|
|
|
const fullPath = resolvedPath.startsWith('/') || resolvedPath.includes(':')
|
|
|
|
|
|
? resolvedPath
|
|
|
|
|
|
: `${projectPath}/${resolvedPath}`;
|
2025-11-29 23:00:48 +08:00
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
const url = convertFileSrc(fullPath);
|
|
|
|
|
|
setThumbnailUrl(url);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
setThumbnailUrl(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// 没有项目路径时,尝试直接使用 resolvedPath
|
2025-11-29 23:00:48 +08:00
|
|
|
|
try {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
const url = convertFileSrc(resolvedPath);
|
2025-11-29 23:00:48 +08:00
|
|
|
|
setThumbnailUrl(url);
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
setThumbnailUrl(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setThumbnailUrl(null);
|
|
|
|
|
|
}
|
2025-12-06 14:08:48 +08:00
|
|
|
|
}, [resolvedPath, isImageAsset]);
|
2025-11-29 23:00:48 +08:00
|
|
|
|
|
2025-11-19 14:54:03 +08:00
|
|
|
|
const handleDragEnter = useCallback((e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
if (!readonly) {
|
|
|
|
|
|
setIsDragging(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [readonly]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleDragLeave = useCallback((e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
setIsDragging(false);
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const handleDragOver = useCallback((e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
|
|
const handleDrop = useCallback((e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
setIsDragging(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (readonly) return;
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// Try to get GUID from drag data first
|
|
|
|
|
|
const assetGuid = e.dataTransfer.getData('asset-guid');
|
|
|
|
|
|
if (assetGuid && isGUID(assetGuid)) {
|
|
|
|
|
|
// Validate extension if needed
|
|
|
|
|
|
if (fileExtension && assetRegistry) {
|
|
|
|
|
|
const path = assetRegistry.getPathByGuid(assetGuid);
|
|
|
|
|
|
if (path && !path.endsWith(fileExtension)) {
|
|
|
|
|
|
return; // Extension mismatch
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
onChange(assetGuid);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Fallback: handle asset-path and convert to GUID
|
|
|
|
|
|
const assetPath = e.dataTransfer.getData('asset-path');
|
|
|
|
|
|
if (assetPath && (!fileExtension || assetPath.endsWith(fileExtension))) {
|
|
|
|
|
|
// Try to get GUID from path
|
|
|
|
|
|
if (assetRegistry) {
|
|
|
|
|
|
// Path might be absolute, convert to relative first
|
|
|
|
|
|
let relativePath = assetPath;
|
|
|
|
|
|
if (assetPath.includes(':') || assetPath.startsWith('/')) {
|
|
|
|
|
|
relativePath = assetRegistry.absoluteToRelative(assetPath) || assetPath;
|
|
|
|
|
|
}
|
|
|
|
|
|
const guid = assetRegistry.getGuidByPath(relativePath);
|
|
|
|
|
|
if (guid) {
|
|
|
|
|
|
onChange(guid);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Fallback to path if GUID not found (backward compatibility)
|
|
|
|
|
|
onChange(assetPath);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Handle file drops
|
2025-11-19 14:54:03 +08:00
|
|
|
|
const files = Array.from(e.dataTransfer.files);
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const file = files.find((f) =>
|
2025-11-19 14:54:03 +08:00
|
|
|
|
!fileExtension || f.name.endsWith(fileExtension)
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
if (file) {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// For file drops, we still use filename (need to register first)
|
2025-11-19 14:54:03 +08:00
|
|
|
|
onChange(file.name);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const text = e.dataTransfer.getData('text/plain');
|
|
|
|
|
|
if (text && (!fileExtension || text.endsWith(fileExtension))) {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// Try to convert to GUID if it's a path
|
|
|
|
|
|
if (assetRegistry && !isGUID(text)) {
|
|
|
|
|
|
const guid = assetRegistry.getGuidByPath(text);
|
|
|
|
|
|
if (guid) {
|
|
|
|
|
|
onChange(guid);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-19 14:54:03 +08:00
|
|
|
|
onChange(text);
|
|
|
|
|
|
}
|
2025-12-06 14:08:48 +08:00
|
|
|
|
}, [onChange, fileExtension, readonly, assetRegistry]);
|
2025-11-19 14:54:03 +08:00
|
|
|
|
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const handleBrowse = useCallback(() => {
|
2025-11-19 14:54:03 +08:00
|
|
|
|
if (readonly) return;
|
2025-11-23 14:49:37 +08:00
|
|
|
|
setShowPicker(true);
|
|
|
|
|
|
}, [readonly]);
|
2025-11-19 14:54:03 +08:00
|
|
|
|
|
2025-11-23 14:49:37 +08:00
|
|
|
|
const handlePickerSelect = useCallback((path: string) => {
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// Convert path to GUID if possible
|
|
|
|
|
|
if (assetRegistry) {
|
|
|
|
|
|
// Path might be absolute, convert to relative first
|
|
|
|
|
|
let relativePath = path;
|
|
|
|
|
|
if (path.includes(':') || path.startsWith('/')) {
|
|
|
|
|
|
relativePath = assetRegistry.absoluteToRelative(path) || path;
|
|
|
|
|
|
}
|
|
|
|
|
|
const guid = assetRegistry.getGuidByPath(relativePath);
|
|
|
|
|
|
if (guid) {
|
|
|
|
|
|
onChange(guid);
|
|
|
|
|
|
setShowPicker(false);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Fallback to path if GUID not found
|
2025-11-23 14:49:37 +08:00
|
|
|
|
onChange(path);
|
|
|
|
|
|
setShowPicker(false);
|
2025-12-06 14:08:48 +08:00
|
|
|
|
}, [onChange, assetRegistry]);
|
2025-11-19 14:54:03 +08:00
|
|
|
|
|
|
|
|
|
|
const handleClear = useCallback(() => {
|
|
|
|
|
|
if (!readonly) {
|
|
|
|
|
|
onChange(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [onChange, readonly]);
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
const getFileName = (path: string | null) => {
|
|
|
|
|
|
if (!path) return placeholder;
|
2025-11-19 14:54:03 +08:00
|
|
|
|
const parts = path.split(/[\\/]/);
|
|
|
|
|
|
return parts[parts.length - 1];
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
// Display name uses resolvedPath
|
|
|
|
|
|
const displayName = resolvedPath ? getFileName(resolvedPath) : placeholder;
|
|
|
|
|
|
|
2025-11-19 14:54:03 +08:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="asset-field">
|
2025-11-23 14:49:37 +08:00
|
|
|
|
{label && <label className="asset-field__label">{label}</label>}
|
2025-11-29 23:00:48 +08:00
|
|
|
|
<div className="asset-field__content">
|
|
|
|
|
|
{/* 缩略图预览 */}
|
2025-11-19 14:54:03 +08:00
|
|
|
|
<div
|
2025-11-29 23:00:48 +08:00
|
|
|
|
className={`asset-field__thumbnail ${isDragging ? 'dragging' : ''}`}
|
2025-11-19 14:54:03 +08:00
|
|
|
|
onDragEnter={handleDragEnter}
|
|
|
|
|
|
onDragLeave={handleDragLeave}
|
|
|
|
|
|
onDragOver={handleDragOver}
|
|
|
|
|
|
onDrop={handleDrop}
|
|
|
|
|
|
>
|
2025-11-29 23:00:48 +08:00
|
|
|
|
{thumbnailUrl ? (
|
|
|
|
|
|
<img src={thumbnailUrl} alt="" />
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Image size={18} className="asset-field__thumbnail-icon" />
|
2025-11-19 14:54:03 +08:00
|
|
|
|
)}
|
2025-11-29 23:00:48 +08:00
|
|
|
|
</div>
|
2025-11-19 14:54:03 +08:00
|
|
|
|
|
2025-11-29 23:00:48 +08:00
|
|
|
|
{/* 右侧区域 */}
|
|
|
|
|
|
<div className="asset-field__right">
|
|
|
|
|
|
{/* 下拉选择框 */}
|
|
|
|
|
|
<div
|
|
|
|
|
|
ref={inputRef}
|
2025-12-06 14:08:48 +08:00
|
|
|
|
className={`asset-field__dropdown ${resolvedPath ? 'has-value' : ''} ${isDragging ? 'dragging' : ''}`}
|
2025-11-29 23:00:48 +08:00
|
|
|
|
onClick={!readonly ? handleBrowse : undefined}
|
|
|
|
|
|
onDragEnter={handleDragEnter}
|
|
|
|
|
|
onDragLeave={handleDragLeave}
|
|
|
|
|
|
onDragOver={handleDragOver}
|
|
|
|
|
|
onDrop={handleDrop}
|
2025-12-06 14:08:48 +08:00
|
|
|
|
title={resolvedPath || placeholder}
|
2025-11-29 23:00:48 +08:00
|
|
|
|
>
|
|
|
|
|
|
<span className="asset-field__value">
|
2025-12-06 14:08:48 +08:00
|
|
|
|
{displayName}
|
2025-11-29 23:00:48 +08:00
|
|
|
|
</span>
|
|
|
|
|
|
<ChevronDown size={12} className="asset-field__dropdown-arrow" />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 操作按钮行 */}
|
|
|
|
|
|
<div className="asset-field__actions">
|
|
|
|
|
|
{/* 定位按钮 */}
|
2025-12-06 14:08:48 +08:00
|
|
|
|
{resolvedPath && onNavigate && (
|
2025-11-29 23:00:48 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className="asset-field__btn"
|
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
|
e.stopPropagation();
|
2025-12-06 14:08:48 +08:00
|
|
|
|
onNavigate(resolvedPath);
|
2025-11-29 23:00:48 +08:00
|
|
|
|
}}
|
|
|
|
|
|
title="Locate in Asset Browser"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Navigation size={12} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
2025-12-06 14:08:48 +08:00
|
|
|
|
{/* 复制路径按钮 - copy path, not GUID */}
|
|
|
|
|
|
{resolvedPath && (
|
2025-11-29 23:00:48 +08:00
|
|
|
|
<button
|
|
|
|
|
|
className="asset-field__btn"
|
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
|
e.stopPropagation();
|
2025-12-06 14:08:48 +08:00
|
|
|
|
navigator.clipboard.writeText(resolvedPath);
|
2025-11-29 23:00:48 +08:00
|
|
|
|
}}
|
|
|
|
|
|
title="Copy Path"
|
|
|
|
|
|
>
|
|
|
|
|
|
<Copy size={12} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
|
|
{/* 清除按钮 */}
|
|
|
|
|
|
{value && !readonly && (
|
|
|
|
|
|
<button
|
|
|
|
|
|
className="asset-field__btn asset-field__btn--clear"
|
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
handleClear();
|
|
|
|
|
|
}}
|
|
|
|
|
|
title="Clear"
|
|
|
|
|
|
>
|
|
|
|
|
|
<X size={12} />
|
|
|
|
|
|
</button>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
2025-11-19 14:54:03 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-11-23 14:49:37 +08:00
|
|
|
|
|
|
|
|
|
|
<AssetPickerDialog
|
|
|
|
|
|
isOpen={showPicker}
|
|
|
|
|
|
onClose={() => setShowPicker(false)}
|
|
|
|
|
|
onSelect={handlePickerSelect}
|
|
|
|
|
|
title="Select Asset"
|
|
|
|
|
|
fileExtensions={fileExtension ? [fileExtension] : []}
|
|
|
|
|
|
/>
|
2025-11-19 14:54:03 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2025-11-23 14:49:37 +08:00
|
|
|
|
}
|