KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
WriteBoard.cpp
이 파일의 문서화 페이지로 가기
1// Copyright (c) 2025 Doppleddiggong. All rights reserved. Unauthorized copying, modification, or distribution of this file, via any medium is strictly prohibited. Proprietary and confidential.
2
3
4#include "WriteBoard.h"
5
6#include "IImageWrapper.h"
7#include "IImageWrapperModule.h"
8#include "Popup_WriteBoard.h"
9#include "Components/Image.h"
10#include "Engine/Canvas.h"
11#include "Engine/TextureRenderTarget2D.h"
12#include "Kismet/KismetRenderingLibrary.h"
13
14
19FVector2D UWriteBoard::GetLocalMousePos(UImage* Image_Canvas, UTextureRenderTarget2D* RT_Canvas, FVector2D mousePos)
20{
21 // Get Absolute Local Pos
22 const FGeometry& geometry = Image_Canvas->GetCachedGeometry();
23 FVector2D localPos = geometry.AbsoluteToLocal(mousePos);
24
25 // Get Canvas Size
26 const FVector2D canvasSize = geometry.GetLocalSize();
27 // Transform localPos(in Image_Canvas Coord) to RT_Canvas Coord && Clamp upto RT_Canvas' Border
28 localPos.X = FMath::Clamp((localPos.X / canvasSize.X * RT_Canvas->SizeX), 0.f, RT_Canvas->SizeX);
29 localPos.Y = FMath::Clamp((localPos.Y / canvasSize.Y * RT_Canvas->SizeY), 0.f, RT_Canvas->SizeY);
30 return localPos;
31}
32
33bool UWriteBoard::SaveRenderTargetToPNG(UTextureRenderTarget2D* RenderTarget, const FString& FullFilePath)
34{
35 FTextureRenderTargetResource* RTResource = RenderTarget->GameThread_GetRenderTargetResource();
36 if (!RTResource)
37 {
38 return false;
39 }
40
41 // Get Width & Height
42 const int32 Width = RenderTarget->SizeX;
43 const int32 Height = RenderTarget->SizeY;
44
45 // Set Bitmap array
46 TArray<FColor> Bitmap;
47 Bitmap.AddUninitialized(Width * Height);
48
49 // Read BGRA8 pixels in RenderTarget
50 RTResource->ReadPixels(Bitmap);
51
52 // PNG Encoder
53 IImageWrapperModule& ImageWrapperModule =
54 FModuleManager::LoadModuleChecked<IImageWrapperModule>("ImageWrapper");
55
56 TSharedPtr<IImageWrapper> ImageWrapper =
57 ImageWrapperModule.CreateImageWrapper(EImageFormat::PNG);
58
59 if (!ImageWrapper.IsValid())
60 {
61 return false;
62 }
63
64 ImageWrapper->SetRaw(
65 Bitmap.GetData(),
66 Bitmap.GetAllocatedSize(),
67 Width,
68 Height,
69 ERGBFormat::BGRA,
70 8
71 );
72
73 // Compress to PNG
74 const TArray64<uint8>& PNGData = ImageWrapper->GetCompressed(100);
75
76 // Make Directory
77 const FString Directory = FPaths::GetPath(FullFilePath);
78 IFileManager::Get().MakeDirectory(*Directory, true);
79
80 return FFileHelper::SaveArrayToFile(PNGData, *FullFilePath);
81}
82
83void UWriteBoard::SaveCanvas(int32 Qid, UTextureRenderTarget2D* RT_Canvas)
84{
85 // File Path
86 IFileManager::Get().MakeDirectory(*filePath, true);
87 // File Name
88 // FString fileName = FDateTime::Now().ToString(TEXT("%Y_%m_%d_%H_%M_%S.png"));
89 FString fileName = FString::Printf(TEXT("Answer%d.PNG"), Qid);
90
91 // Export Render Target to png
92 SaveRenderTargetToPNG(RT_Canvas, filePath / fileName);
93}
void SaveCanvas(int32 Qid, UTextureRenderTarget2D *RT_Canvas)
TObjectPtr< class UPopup_WriteBoard > parentWritePopup
Definition WriteBoard.h:30
FVector2D GetLocalMousePos(UImage *Image_Canvas, UTextureRenderTarget2D *RT_Canvas, FVector2D mousePos)
bool SaveRenderTargetToPNG(class UTextureRenderTarget2D *RenderTarget, const FString &FullFilePath)
const FString filePath
Definition WriteBoard.h:31