ArticyXImporter
ArticyXImporter for Unreal Engine
Loading...
Searching...
No Matches
ArticyRef.h
1//
2// Copyright (c) 2023 articy Software GmbH & Co. KG. All rights reserved.
3//
4
5#pragma once
6
7#include "ArticyBaseTypes.h"
8#include "ArticyObject.h"
9#include "ArticyRef.generated.h"
10
11USTRUCT(BlueprintType)
12struct ARTICYRUNTIME_API FArticyRef
13{
14 GENERATED_BODY()
15
16public:
17 UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Setup")
18 bool bReferenceBaseObject = true;
19
21 UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (EditCondition="!bReferenceBaseObject"), Category="Setup")
22 mutable int32 CloneId = 0;
23
24 void SetId(FArticyId NewId);
25 void SetReference(UArticyObject* Object);
26
27 const FArticyId& GetId() const;
28
30 template<typename T = UArticyObject>
31 T* GetObject(const UObject* WorldContext) const;
32
33 uint32 GetEffectiveCloneId() const
34 {
35 return bReferenceBaseObject ? 0ul : *reinterpret_cast<uint32*>(&CloneId);
36 }
37
38 enum EStringInitResult
39 {
40 NoneSet,
41 IdSet,
42 RefSet,
43 AllSet
44 };
45
46 // reference: color.h
47 // used in details customization
48 EStringInitResult InitFromString(const FString& SourceString)
49 {
50 Id.High = Id.Low = 0;
51
52 const bool IdSet =
53 FParse::Value(*SourceString, TEXT("Low="), Id.Low) &&
54 FParse::Value(*SourceString, TEXT("High="), Id.High);
55 const bool RefSet =
56 FParse::Bool(*SourceString, TEXT("bReferenceBaseObject="), bReferenceBaseObject) &&
57 FParse::Value(*SourceString, TEXT("CloneId="), CloneId);
58
59 return IdSet && RefSet ? EStringInitResult::AllSet : IdSet ? EStringInitResult::IdSet : RefSet ? EStringInitResult::RefSet : EStringInitResult::NoneSet;
60 }
61
74 FString ToString() const
75 {
76 return FString::Printf(TEXT("(bReferenceBaseObject=%s, CloneId=%d, Id=(Low=%d, High=%d))"), bReferenceBaseObject ? TEXT("True") : TEXT("False"), CloneId, Id.Low, Id.High);
77 }
78
82 bool operator==(const FArticyRef& Other) const
83 {
84 return MatchesEffective(Other);
85 }
86
88 bool MatchesRaw(const FArticyRef& Other) const
89 {
90 return Other.Id.Low == Id.Low && Other.Id.High == Id.High && Other.bReferenceBaseObject == bReferenceBaseObject && Other.CloneId == CloneId;
91 }
92
94 bool MatchesEffective(const FArticyRef& Other) const
95 {
96 return Other.Id.Low == Id.Low && Other.Id.High == Id.High && Other.bReferenceBaseObject == bReferenceBaseObject && Other.GetEffectiveCloneId() == GetEffectiveCloneId();
97 }
98
99 bool operator!=(const FArticyRef& Other) const
100 {
101 return !this->operator==(Other);
102 }
103
104 friend uint32 GetTypeHash(const FArticyRef& Ref)
105 {
106 return HashCombine(GetTypeHash(Ref.GetId()), Ref.GetEffectiveCloneId());
107 }
108
109
110private:
115 UPROPERTY(EditAnywhere, Category="Setup")
116 mutable FArticyId Id = 0;
117
119 mutable TWeakObjectPtr<UArticyObject> CachedObject = nullptr;
120 mutable FArticyId CachedId = 0;
121 mutable int32 CachedCloneId = 0;
122
123 UArticyObject* GetObjectInternal(const UObject* WorldContext) const;
124
125private:
126
128 //template<class CPPSTRUCT>
129 //friend typename TEnableIf<TStructOpsTypeTraits<CPPSTRUCT>::WithSerializer, bool>::Type SerializeOrNot(FArchive& Ar, CPPSTRUCT *Data);
130
131 //bool Serialize(FArchive& Ar);
132
134 //template<class CPPSTRUCT>
135 //friend typename TEnableIf<TStructOpsTypeTraits<CPPSTRUCT>::WithPostSerialize>::Type PostSerializeOrNot(const FArchive& Ar, CPPSTRUCT *Data);
136
137 //void PostSerialize(const FArchive& Ar);
138};
139
140template<typename T>
141T* FArticyRef::GetObject(const UObject* WorldContext) const
142{
143 if (!CachedObject.IsValid() || CachedId != Id || CloneId != CachedCloneId)
144 {
145 CachedId = Id;
146 CachedCloneId = CloneId;
147 CachedObject = GetObjectInternal(WorldContext);
148 }
149
150 return Cast<T>(CachedObject.Get());
151}
152
153//template<>
154//struct TStructOpsTypeTraits<FArticyRef> : public TStructOpsTypeTraitsBase2<FArticyRef>
155//{
156// enum
157// {
158// WithSerializer = true,
159// WithPostSerialize = true
160// };
161//};
Definition ArticyObject.h:19
Definition ArticyBaseTypes.h:18
Definition ArticyRef.h:13
bool MatchesRaw(const FArticyRef &Other) const
Definition ArticyRef.h:88
FString ToString() const
Definition ArticyRef.h:74
T * GetObject(const UObject *WorldContext) const
Definition ArticyRef.h:141
int32 CloneId
Definition ArticyRef.h:22
bool operator==(const FArticyRef &Other) const
Definition ArticyRef.h:82
bool MatchesEffective(const FArticyRef &Other) const
Definition ArticyRef.h:94