void SDrawHouseTypeTools::OnButtonClickLoadImage()
{
// 当前如果是在硬装模块,则可进行导入户型操作
FHardEdMode* HardEdMode = (FHardEdMode*)GLevelEditorModeTools().GetActiveMode(FBuiltinEditorModes::EM_HardEdModeId);
if (HardEdMode)
{
//打开文件夹
TArray<FString> OpenFilenames;
TSharedPtr<SWindow> ParentWindow = FSlateApplication::Get().GetActiveTopLevelWindow();
void* ParentWindowHandle = (ParentWindow.IsValid() && ParentWindow->GetNativeWindow().IsValid()) ? ParentWindow->GetNativeWindow()->GetOSWindowHandle() : nullptr;
if (ExtendFunc::OpenFileDialog(
ParentWindowHandle,
NSLOCTEXT("UIHome", "OpenReferencePictureTitle", "打开户型图片文件...").ToString(),
FPaths::GameDir(),
TEXT(""),
NSLOCTEXT("UIHome", "OpenReferencePictureFilter", "All Files (*.*)|*.*|户型图片文件 (*.png)|*.png|户型图片文件 (*.jpg)|*.jpg").ToString(),
ExtendFunc::EFileDialogFlags::None,
OpenFilenames))
{
if (OpenFilenames.Num() > 0)
{
FString OpenFilename = OpenFilenames[0];
FName BrushName(*OpenFilename);
//传了图片路径,返回可以用来显示在runtime中的图片纹理
if (SpriteTexture.IsValid() && SpriteTexture->IsRooted())
{
SpriteTexture->RemoveFromRoot();
}
// 判断图片得格式是否正确
SpriteTexture = TWeakObjectPtr<UTexture2D>(GetTexture2DFromDiskFile(OpenFilename));
if (SpriteTexture.IsValid())
{
SpriteTexture->AddToRoot();
//将数据存储并导入到画图方法中
FIntPoint ImageSize = FIntPoint(SpriteTexture->GetSizeX(), SpriteTexture->GetSizeY());
IHardModeModule::Get().GetImageSize() = ImageSize;
TextureAlpha = 0.5;
HardEdMode->OnHouseImgAlphaChanged(TextureAlpha);
HardEdMode->SetHouseImg(SpriteTexture.Get(), ImageSize);
}
else
{
FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT("请导入正确格式的图片!")));
OnButtonClickLoadImage();
}
}
}
}
else
{
FMessageDialog::Open(EAppMsgType::Ok, FText::FromString(TEXT(" ")));
}
}
//获取图片纹理
UTexture2D* SDrawHouseTypeTools::GetTexture2DFromDiskFile(const FString& FilePath)
{
// 如果找到该文件,则返回true,否则为false
if (!FPaths::FileExists(FilePath))
{
UE_LOG(LogTemp, Error, TEXT("File not found: %s"), *FilePath);
return nullptr;
}
// 从文件中加载压缩的字节数据
TArray<uint8> FileData;
if (!FFileHelper::LoadFileToArray(FileData, *FilePath))
{
UE_LOG(LogTemp, Error, TEXT("Failed to load file: %s"), *FilePath);
return nullptr;
}
// 使用ImageWrapper模块检测图像类型
IImageWrapperModule& ImageWrapperModule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(TEXT("ImageWrapper"));
EImageFormat::Type ImageFormat = ImageWrapperModule.DetectImageFormat(FileData.GetData(), FileData.Num());
if (ImageFormat == EImageFormat::Invalid)
{
UE_LOG(LogTemp, Error, TEXT("Unrecognized image file format: %s"), *FilePath);
return nullptr;
}
// 为检测到的图像格式创建一个图像包装
IImageWrapperPtr ImageWrapper = ImageWrapperModule.CreateImageWrapper(ImageFormat);
if (!ImageWrapper.IsValid())
{
UE_LOG(LogTemp, Error, TEXT("Failed to create image wrapper for file: %s"), *FilePath);
return nullptr;
}
// 解压图像数据
const TArray<uint8>* RawData = nullptr;
ImageWrapper->SetCompressed(FileData.GetData(), FileData.Num());
ImageWrapper->GetRaw(ERGBFormat::BGRA, 8, RawData);
if (RawData == nullptr)
{
UE_LOG(LogTemp, Error, TEXT("Failed to decompress image file: %s"), *FilePath);
return nullptr;
}
// 创建纹理并上传未压缩的图像数据
FString TextureBaseName = TEXT("Texture_") + FPaths::GetBaseFilename(FilePath);
// UObject* Outer = nullptr;
UTexture2D* mytexture = MyCreateTexture(*RawData, ImageWrapper->GetWidth(), ImageWrapper->GetHeight(), EPixelFormat::PF_B8G8R8A8, FName(*TextureBaseName));
return mytexture;
}
UTexture2D* SDrawHouseTypeTools::MyCreateTexture(const TArray<uint8>& PixelData, int32 InSizeX, int32 InSizeY, EPixelFormat InFormat, FName BaseName)
{
// 无耻地抄袭UTexture2D:CreateTransient一些修改
if (InSizeX <= 0 || InSizeY <= 0 || (InSizeX % GPixelFormats[InFormat].BlockSizeX) != 0 || (InSizeY % GPixelFormats[InFormat].BlockSizeY) != 0)
{
return nullptr;
}
// 最重要的不同之处在于UTexture2D::CreateTransient:我们为新结构提供一个名称和一个所有者
UTexture2D* NewTexture = NewObject<UTexture2D>(GetTransientPackage(), NAME_None, RF_Transient);
NewTexture->PlatformData = new FTexturePlatformData();
NewTexture->PlatformData->SizeX = InSizeX;
NewTexture->PlatformData->SizeY = InSizeY;
NewTexture->PlatformData->PixelFormat = InFormat;
// 分配第一个纹理映射并上传像素数据
int32 NumBlocksX = InSizeX / GPixelFormats[InFormat].BlockSizeX;
int32 NumBlocksY = InSizeY / GPixelFormats[InFormat].BlockSizeY;
FTexture2DMipMap* Mip = new(NewTexture->PlatformData->Mips) FTexture2DMipMap();
Mip->SizeX = InSizeX;
Mip->SizeY = InSizeY;
Mip->BulkData.Lock(LOCK_READ_WRITE);
void* TextureData = Mip->BulkData.Realloc(NumBlocksX * NumBlocksY * GPixelFormats[InFormat].BlockBytes);
FMemory::Memcpy(TextureData, PixelData.GetData(), PixelData.Num());
Mip->BulkData.Unlock();
NewTexture->UpdateResource();
return NewTexture;
}