KLingo Project Documentation 1.0.0
Unreal Engine 5.6 C++ Project Documentation
로딩중...
검색중...
일치하는것 없음
Popup_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 "Popup_WriteBoard.h"
5
6#include "GameLogging.h"
7#include "NetworkData.h"
8#include "NetworkMessage.h"
9#include "UImageButton.h"
10#include "UPopupManager.h"
11#include "UTextureButton.h"
12#include "Components/Image.h"
13#include "Engine/Canvas.h"
14#include "Kismet/KismetRenderingLibrary.h"
15#include "WriteBoard.h"
16#include "Components/HorizontalBox.h"
17#include "Components/TextBlock.h"
18
19UPopup_WriteBoard::UPopup_WriteBoard(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
20{
21 ConstructorHelpers::FObjectFinder<UTextureRenderTarget2D> rtCanvasRef(TEXT("/Script/Engine.TextureRenderTarget2D'/Game/CustomContents/UI/DrawingBoard/RT_Canvas.RT_Canvas'"));
22 if (rtCanvasRef.Succeeded())
23 {
24 RT_Canvas = rtCanvasRef.Object;
25 }
26
27 ConstructorHelpers::FObjectFinder<UTexture2D> dotLineImageRef(TEXT("/Script/Engine.Texture2D'/Game/CustomContents/UI/DrawingBoard/writepanel.writepanel'"));
28 if (dotLineImageRef.Succeeded())
29 {
30 dotLineTexture = dotLineImageRef.Object;
31 }
32
33 ConstructorHelpers::FObjectFinder<UFont> guideTextFontRef(TEXT("/Script/Engine.Font'/Engine/EngineFonts/Roboto.Roboto'"));
34 if (guideTextFontRef.Succeeded())
35 {
36 guideTextFont = guideTextFontRef.Object;
37 }
38}
39
41{
42 Super::NativeOnInitialized();
43}
44
46{
47 Super::NativeConstruct();
48 writeBoardObject = NewObject<UWriteBoard>();
49}
50
51void UPopup_WriteBoard::InitPopup(int32 InQid, const FWriteQuestionData& InQuestionData)
52{
53 // Button Event
54 Button_Save->OnButtonClickedEvent.RemoveDynamic(this, &UPopup_WriteBoard::SaveCanvas);
55 Button_Save->OnButtonClickedEvent.AddDynamic(this, &UPopup_WriteBoard::SaveCanvas);
56
57 if (Text_Guide != nullptr)
58 {
59 tempFontInfo = Text_Guide->GetFont();
60 Text_Guide->RemoveFromParent();
61 }
62
63 this->Qid = InQid;
64 this->AnswerKr = InQuestionData.answer_kor;
65 this->Text_Question->SetText(FText::FromString(InQuestionData.word_data.eng));
66
69}
70
72{
73 return prevMousePos;
74}
75
76FReply UPopup_WriteBoard::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
77{
78 // Check Mouse is in Canvas
79 const FGeometry CanvasGeometry = Image_Canvas->GetCachedGeometry();
80 if (!CanvasGeometry.IsUnderLocation(InMouseEvent.GetScreenSpacePosition()))
81 {
82 return FReply::Unhandled();
83 }
84
85 // Get Mouse Position in Local Image Coordinate System
86 // Save Current MousePos to prevMousePos
87 prevMousePos = GetLocalMousePos(InMouseEvent.GetScreenSpacePosition());
88 bIsDrawing = true;
89
90 // Draw Point Once
91 // PRINT_STRING(TEXT("%s"), *InMouseEvent.GetEffectingButton().GetFName().ToString());
92 if (InMouseEvent.IsMouseButtonDown(EKeys::LeftMouseButton)) // Draw
93 {
94 DrawPoint(GetLocalMousePos(InMouseEvent.GetScreenSpacePosition()), FLinearColor::Black);
95 }
96 else if (InMouseEvent.IsMouseButtonDown(EKeys::RightMouseButton)) // Erase
97 {
98 DrawPoint(GetLocalMousePos(InMouseEvent.GetScreenSpacePosition()), FLinearColor::White);
99 }
100
101 return FReply::Handled();
102}
103
104FReply UPopup_WriteBoard::NativeOnMouseButtonUp(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
105{
106 // Get Mouse Position in Local Image Coordinate System
107 // Save ZeroVector to prevMousePos
108 prevMousePos = GetLocalMousePos(FVector2D::ZeroVector);
109 bIsDrawing = false;
110
111 return FReply::Handled();
112}
113
114FReply UPopup_WriteBoard::NativeOnMouseMove(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
115{
116 // return When Mouse Not Pressed
117 if (!bIsDrawing) return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
118
119 if (InMouseEvent.IsMouseButtonDown(EKeys::LeftMouseButton)) // Draw
120 {
121 DrawLines(GetLocalMousePos(InMouseEvent.GetScreenSpacePosition()), FLinearColor::Black);
122 }
123 else if (InMouseEvent.IsMouseButtonDown(EKeys::RightMouseButton)) // Erase
124 {
125 DrawLines(GetLocalMousePos(InMouseEvent.GetScreenSpacePosition()), FLinearColor::White);
126 }
127
128 return FReply::Handled();
129}
130
132{
133 // 예상 답변에 따른 WriteBoard 길이 조절
134 float letterNum = AnswerKr.Len();
135
136 // 1. Render Target 길이 늘리기
137 RT_Canvas->ResizeTarget(stepLength * letterNum, stepLength);
138
139 // 2. Image의 길이 늘리기
140 Image_Canvas->SetDesiredSizeOverride(FVector2D(stepLength * letterNum, stepLength));
141
142 // 예상 답변 힌트 생성
143 for (const TCHAR letter : AnswerKr)
144 {
145 // 십자 점선 칸 이미지 추가
146 UImage* tempImage = NewObject<UImage>(this, UImage::StaticClass());
147 tempImage->SetBrushFromTexture(dotLineTexture);
148
149 // 이미지 크기 설정
150 FSlateBrush tempImageBrush = tempImage->GetBrush();
151 tempImageBrush.SetImageSize(FVector2D(360));
152 tempImage->SetBrush(tempImageBrush);
153
154 HorizontalBox_DotLine->AddChildToHorizontalBox(tempImage);
155 DotLineImages.Add(tempImage);
156
157 // 글자 힌트 텍스트 추가
158 UTextBlock* tempText = NewObject<UTextBlock>(this, UTextBlock::StaticClass());
159 tempText->SetText(FText::FromString(FString::Printf(TEXT("%c"), letter)));
160 tempText->SetColorAndOpacity(FLinearColor(0.4f, 0.4f, 0.4f, 0.5f));
161 tempText->SetMinDesiredWidth(360.f);
162 tempText->SetJustification(ETextJustify::Type::Center);
163
164 // 글자 폰트 크기 설정
165 tempText->SetFont(tempFontInfo);
166
167 HorizontalBox_Guide->AddChildToHorizontalBox(tempText);
168 GuideTexts.Add(tempText);
169 }
170}
171
173{
174 HorizontalBox_DotLine->ClearChildren();
175 HorizontalBox_Guide->ClearChildren();
176}
177
178void UPopup_WriteBoard::DrawPoint(FVector2D mousePos, FLinearColor drawColor)
179{
180 // Begin Draw Canvas To Render Target
181 UCanvas* canvas = nullptr;
182 FVector2D size;
183 FDrawToRenderTargetContext context;
184 UKismetRenderingLibrary::BeginDrawCanvasToRenderTarget(this, RT_Canvas, canvas, size, context);
185
186 // Set thickness Whether now in Draw or Erase
187 float thickness = (drawColor == FLinearColor::Black) ? 10 : 30;
188 // Draw Box
189 canvas->K2_DrawBox(mousePos, FVector2D(1, 1), thickness, drawColor);
190
191 UKismetRenderingLibrary::EndDrawCanvasToRenderTarget(this, context);
192}
193
194void UPopup_WriteBoard::DrawLines(FVector2D mousePos, FLinearColor drawColor)
195{
196 // Begin Draw Canvas To Render Target
197 UCanvas* canvas = nullptr;
198 FVector2D size;
199 FDrawToRenderTargetContext context;
200 UKismetRenderingLibrary::BeginDrawCanvasToRenderTarget(this, RT_Canvas, canvas, size, context);
201
202 // Calculate Draw Positions
203 FVector2D currPos = prevMousePos;
204 int32 div = 64;
205 FVector2D drawOffset = (mousePos - currPos) / div;
206
207 // Draw
208 for (int32 i = 1; i < div; ++i)
209 {
210 currPos = prevMousePos + drawOffset * i;
211
212 // Set thickness Whether now in Draw or Erase
213 float thickness = (drawColor == FLinearColor::Black) ? 10 : 25;
214 // Draw Line
215 canvas->K2_DrawLine(prevMousePos, currPos, thickness, drawColor);
216 }
217 prevMousePos = mousePos;
218
219 UKismetRenderingLibrary::EndDrawCanvasToRenderTarget(this, context);
220}
221
222FVector2D UPopup_WriteBoard::GetLocalMousePos(FVector2D mousePos)
223{
224 return writeBoardObject->GetLocalMousePos(Image_Canvas, RT_Canvas, mousePos);
225}
226
228{
229 if (const auto PopupMgr = UPopupManager::Get(GetWorld()))
230 {
231 if (writeBoardObject != nullptr)
232 writeBoardObject->SaveCanvas(Qid, RT_Canvas);
233
234 // 캔버스 저장 완료 델리게이트 브로드캐스트
235 OnCanvasSaved.Broadcast();
236
237 ClearCanvas();
238 PopupMgr->HideCurrentPopup(false);
239 }
240}
241
243{
244 UKismetRenderingLibrary::ClearRenderTarget2D(this, RT_Canvas, RT_Canvas->ClearColor);
245}
YiSan 전반에서 사용하는 공용 인터페이스를 선언합니다.
네트워크 요청과 응답에 사용되는 구조체 및 설정을 정의합니다.
TObjectPtr< class UImage > Image_Canvas
TArray< class UTextBlock * > GuideTexts
virtual void NativeOnInitialized() override
void InitPopup(int32 InQid, const FWriteQuestionData &InQuestionData)
TObjectPtr< class UTextureRenderTarget2D > RT_Canvas
TObjectPtr< class UWriteBoard > writeBoardObject
TObjectPtr< class UTextBlock > Text_Question
FSlateFontInfo tempFontInfo
TObjectPtr< class UTexture2D > dotLineTexture
TObjectPtr< class UTextBlock > Text_Guide
virtual FReply NativeOnMouseMove(const FGeometry &InGeometry, const FPointerEvent &InMouseEvent) override
TObjectPtr< class UImageButton > Button_Save
FOnCanvasSaved OnCanvasSaved
캔버스 저장 완료 시 호출되는 델리게이트
TArray< class UImage * > DotLineImages
TObjectPtr< class UHorizontalBox > HorizontalBox_Guide
FVector2D GetLocalMousePos(FVector2D mousePos)
FVector2D GetPrevMousePos()
virtual FReply NativeOnMouseButtonUp(const FGeometry &InGeometry, const FPointerEvent &InMouseEvent) override
virtual void NativeConstruct() override
UPopup_WriteBoard(const FObjectInitializer &ObjectInitializer)
void DrawLines(FVector2D mousePos, FLinearColor drawColor)
void DrawPoint(FVector2D mousePos, FLinearColor drawColor)
TObjectPtr< class UHorizontalBox > HorizontalBox_DotLine
TObjectPtr< class UFont > guideTextFont
virtual FReply NativeOnMouseButtonDown(const FGeometry &InGeometry, const FPointerEvent &InMouseEvent) override
Write 질문 및 정답 구조체입니다.
FWriteWordData word_data