-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathAirBlueprintLib.h
More file actions
288 lines (240 loc) · 10.7 KB
/
Copy pathAirBlueprintLib.h
File metadata and controls
288 lines (240 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Misc/MessageDialog.h"
#include "Components/InputComponent.h"
#include "GameFramework/PlayerInput.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Components/MeshComponent.h"
#include "LandscapeProxy.h"
#include "Kismet/GameplayStatics.h"
#include "Kismet/KismetStringLibrary.h"
#include "Engine/World.h"
#include "Modules/ModuleManager.h"
#include "Runtime/Landscape/Classes/LandscapeComponent.h"
#include "common/AirSimSettings.hpp"
#include <string>
#include <regex>
#include "AirBlueprintLib.generated.h"
UENUM(BlueprintType)
enum class LogDebugLevel : uint8 {
Informational UMETA(DisplayName = "Informational"),
Success UMETA(DisplayName = "Success"),
Failure UMETA(DisplayName = "Failure"),
Unimportant UMETA(DisplayName = "Unimportant")
};
/**
*
*/
UCLASS()
class UAirBlueprintLib : public UBlueprintFunctionLibrary
{
GENERATED_BODY()
public:
static void OnBeginPlay();
static void OnEndPlay();
static void LogMessageString(const std::string &prefix, const std::string &suffix, LogDebugLevel level, float persist_sec = 60);
UFUNCTION(BlueprintCallable, Category = "Utils")
static void LogMessage(const FString &prefix, const FString &suffix, LogDebugLevel level, float persist_sec = 60);
static float GetWorldToMetersScale(const AActor* context);
UFUNCTION(BlueprintCallable, Category = "Utils")
static void showMessageAlertDialog(const FText& message, const FText& title)
{
FMessageDialog::Open(EAppMsgType::Ok, message, & title);
}
template<typename T>
static T* GetActorComponent(AActor* actor, FString name);
template<typename T>
static T* FindActor(const UObject* context, FString name)
{
TArray<AActor*> foundActors;
FindAllActor<T>(context, foundActors);
FName name_n = FName(*name);
for (AActor* actor : foundActors) {
if (actor->ActorHasTag(name_n) || actor->GetName().Compare(name) == 0) {
return static_cast<T*>(actor);
}
}
//UAirBlueprintLib::LogMessage(name + TEXT(" Actor not found!"), TEXT(""), LogDebugLevel::Failure);
return nullptr;
}
template<typename T>
static void FindAllActor(const UObject* context, TArray<AActor*>& foundActors)
{
UGameplayStatics::GetAllActorsOfClass(context, T::StaticClass(), foundActors);
}
static std::vector<std::string> ListMatchingActors(const UObject *context, const std::string& name_regex);
static bool HasObstacle(const AActor* actor, const FVector& start, const FVector& end,
const AActor* ignore_actor = nullptr, ECollisionChannel collision_channel = ECC_Visibility);
static bool GetObstacle(const AActor* actor, const FVector& start, const FVector& end,
FHitResult& hit, const AActor* ignore_actor = nullptr, ECollisionChannel collision_channel = ECC_Visibility);
static bool GetLastObstaclePosition(const AActor* actor, const FVector& start, const FVector& end,
FHitResult& hit, const AActor* ignore_actor = nullptr, ECollisionChannel collision_channel = ECC_Visibility);
static void FollowActor(AActor* follower, const AActor* followee, const FVector& offset, bool fixed_z = false, float fixed_z_val = 2.0f);
static bool SetMeshStencilID(const std::string& mesh_name, int object_id,
bool is_name_regex = false);
static int GetMeshStencilID(const std::string& mesh_name);
static void InitializeMeshStencilIDs(bool ignore_existing);
static bool IsInGameThread();
template<class T>
static std::string GetMeshName(T* mesh)
{
switch (mesh_naming_method_)
{
case msr::airlib::AirSimSettings::SegmentationSetting::MeshNamingMethodType::OwnerName:
if (mesh->GetOwner())
return std::string(TCHAR_TO_UTF8(*(mesh->GetOwner()->GetName())));
else
return ""; //std::string(TCHAR_TO_UTF8(*(UKismetSystemLibrary::GetDisplayName(mesh))));
case msr::airlib::AirSimSettings::SegmentationSetting::MeshNamingMethodType::StaticMeshName:
if (mesh->GetStaticMesh())
return std::string(TCHAR_TO_UTF8(*(mesh->GetStaticMesh()->GetName())));
else
return "";
default:
return "";
}
}
static std::string GetMeshName(ALandscapeProxy* mesh);
template<class UserClass>
static FInputActionBinding& BindActionToKey(const FName action_name, const FKey in_key, UserClass* actor,
FInputActionHandlerSignature::TMethodPtr<UserClass> func, bool on_press_or_release = false,
bool shift_key = false, bool control_key = false, bool alt_key = false, bool command_key = false)
{
FInputActionKeyMapping action(action_name, in_key, shift_key, control_key, alt_key, command_key);
APlayerController* controller = actor->GetWorld()->GetFirstPlayerController();
controller->PlayerInput->AddActionMapping(action);
return controller->InputComponent->
BindAction(action_name, on_press_or_release ? IE_Pressed : IE_Released, actor, func);
}
template<class UserClass>
static FInputAxisBinding& BindAxisToKey(const FName axis_name, const FKey in_key, AActor* actor, UserClass* obj,
FInputAxisHandlerSignature::TMethodPtr<UserClass> func)
{
FInputAxisKeyMapping axis(axis_name, in_key);
return UAirBlueprintLib::BindAxisToKey(axis, actor, obj, func);
}
template<class UserClass>
static FInputAxisBinding& BindAxisToKey(const FInputAxisKeyMapping& axis, AActor* actor, UserClass* obj,
typename FInputAxisHandlerSignature::TMethodPtr<UserClass> func)
{
APlayerController* controller = actor->GetWorld()->GetFirstPlayerController();
controller->PlayerInput->AddAxisMapping(axis);
return controller->InputComponent->
BindAxis(axis.AxisName, obj, func);
}
static int RemoveAxisBinding(const FInputAxisKeyMapping& axis, FInputAxisBinding* axis_binding, AActor* actor);
static void EnableInput(AActor* actor);
static void RunCommandOnGameThread(TFunction<void()> InFunction, bool wait = false, const TStatId InStatId = TStatId());
static float GetDisplayGamma();
static EAppReturnType::Type ShowMessage(EAppMsgType::Type MessageType, const std::string& message, const std::string& title);
static bool getLogMessagesHidden()
{
return log_messages_hidden_;
}
static void setLogMessagesHidden(bool is_hidden)
{
log_messages_hidden_ = is_hidden;
}
static void SetMeshNamingMethod(msr::airlib::AirSimSettings::SegmentationSetting::MeshNamingMethodType method)
{
mesh_naming_method_ = method;
}
static void enableWorldRendering(AActor* context, bool enable);
static void enableViewportRendering(AActor* context, bool enable);
static void setSimulatePhysics(AActor* actor, bool simulate_physics);
static void resetSimulatePhysics(AActor* actor);
static std::vector<UPrimitiveComponent*> getPhysicsComponents(AActor* actor);
static UObject* LoadObject(const std::string& name);
static UClass* LoadClass(const std::string& name);
static void setUnrealClockSpeed(const AActor* context, float clock_speed);
static IImageWrapperModule* getImageWrapperModule();
static void CompressImageArray(int32 width, int32 height, const TArray<FColor> &src, TArray<uint8> &dest);
static std::vector<msr::airlib::MeshPositionVertexBuffersResponse> GetStaticMeshComponents();
private:
template<typename T>
static void InitializeObjectStencilID(T* mesh, bool ignore_existing = true)
{
std::string mesh_name = common_utils::Utils::toLower(GetMeshName(mesh));
if (mesh_name == "" || common_utils::Utils::startsWith(mesh_name, "default_")) {
//common_utils::Utils::DebugBreak();
return;
}
FString name(mesh_name.c_str());
int hash = 5;
for (int idx = 0; idx < name.Len(); ++idx) {
auto char_num = UKismetStringLibrary::GetCharacterAsNumber(name, idx);
if (char_num < 97)
continue; //numerics and other punctuations
hash += char_num;
}
if (ignore_existing || mesh->CustomDepthStencilValue == 0) { //if value is already set then don't bother
SetObjectStencilID(mesh, hash % 256);
}
}
template<typename T>
static void SetObjectStencilIDIfMatch(T* mesh, int object_id,
const std::string& mesh_name, bool is_name_regex, const std::regex& name_regex, int& changes)
{
std::string comp_mesh_name = GetMeshName(mesh);
if (comp_mesh_name == "")
return;
bool is_match = (!is_name_regex && (comp_mesh_name == mesh_name))
|| (is_name_regex && std::regex_match(comp_mesh_name, name_regex));
if (is_match) {
++changes;
SetObjectStencilID(mesh, object_id);
}
}
template<typename T>
static void SetObjectStencilID(T* mesh, int object_id)
{
if (object_id < 0)
{
mesh->SetRenderCustomDepth(false);
}
else
{
mesh->SetCustomDepthStencilValue(object_id);
mesh->SetRenderCustomDepth(true);
}
//mesh->SetVisibility(false);
//mesh->SetVisibility(true);
}
static void SetObjectStencilID(ALandscapeProxy* mesh, int object_id)
{
if (object_id < 0)
{
mesh->bRenderCustomDepth = false;
}
else
{
mesh->CustomDepthStencilValue = object_id;
mesh->bRenderCustomDepth = true;
}
// Explicitly set the custom depth state on the components so the
// render state is marked dirty and the update actually takes effect
// immediately.
for (ULandscapeComponent* comp : mesh->LandscapeComponents)
{
if (object_id < 0)
{
comp->SetRenderCustomDepth(false);
}
else
{
comp->SetCustomDepthStencilValue(object_id);
comp->SetRenderCustomDepth(true);
}
}
}
static bool CompressUsingImageWrapper(const TArray<uint8>& uncompressed, const int32 width, const int32 height, TArray<uint8>& compressed);
private:
static bool log_messages_hidden_;
//FViewPort doesn't expose this field so we are doing dirty work around by maintaining count by ourselves
static uint32_t flush_on_draw_count_;
static msr::airlib::AirSimSettings::SegmentationSetting::MeshNamingMethodType mesh_naming_method_;
static IImageWrapperModule* image_wrapper_module_;
};