AddWidgetToUserWidgetRoot

//H
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="UserWidget")) static void AddWidgetToUserWidgetRoot(UUserWidget* UserWidget, UWidget* Widget);
//Cpp
void UILbr::AddWidgetToUserWidgetRoot(UUserWidget* UserWidget, UWidget* Widget)
{
    if (UserWidget != nullptr && Widget != nullptr) {
        TScriptInterface<INamedSlotInterface> NamedSlotInterface = TScriptInterface<INamedSlotInterface>(UserWidget);
        TArray<FName> SlotNames;
        NamedSlotInterface->GetSlotNames(SlotNames);
        FName N;
        UE_LOG(IVS, Display, TEXT("AddWidgetToUserWidgetRoot UserWidget->WidgetTree->RootWidget==nullptr %s !SlotNames.IsValidIndex(0) %s"), *FString(UserWidget->WidgetTree->RootWidget == nullptr ? "True" : "False"), *FString(!SlotNames.IsValidIndex(0) ? "True" : "False"));
        if (!SlotNames.IsValidIndex(0)) {
            UserWidget->SetFlags(RF_Transactional);
            Widget->SetFlags(RF_Transactional);
            UPanelWidget* PanelWidget = UserWidget->WidgetTree->ConstructWidget<UPanelWidget>(UPanelWidget::StaticClass());
            PanelWidget->AddChild(Widget);
            UserWidget->WidgetTree->RootWidget = PanelWidget;
            UserWidget->WidgetTree->Modify();
            NamedSlotInterface->GetSlotNames(SlotNames);
            if (SlotNames.IsValidIndex(0)) {
                N = SlotNames[0];
            }
        }
        else {
            N = SlotNames[0];
        }
        UE_LOG(IVS, Display, TEXT("AddWidgetToUserWidgetRoot SlotName %s"), *N.ToString());
    }
}

//.h

/** Get loaded class named.
* The given ClassName has to be a specific path to your class/object, for example: /Game/Blueprints/GameMode.GameMode
* To get the ClassName, right click on your class / object in the content browser and then 'Copy Reference' and remove the 'Blueprint' and quotes.
* 
* Получить загруженный класс именем.
* Данное ClassName должно быть конкретным путем к вашему class/object, например: /Game/Blueprints/GameMode.GameMode
* Для получения ClassName щёлкнете правой кнопкой мыши свой class/object в браузере содержимого, а затем 'Copy Reference' и удалите 'Blueprint' и кавычки.
* 
* Blueprint'/Game/Blueprints/PlayerController.PlayerController'
* /Game/Blueprints/PlayerController.PlayerController
*/
UFUNCTION(BlueprintCallable, Category="IVS|Get", Meta=(DisplayName="Get Loaded Class Named", Keywords="get loaded class named retrieve create object path find by value", ExpandEnumAsExecs="Branches")) static void GetLoadedClassNamed(ETF& Branches, bool& Success, const FString& ClassName, UClass*& Class_);
/** Get class named.
* The given ClassName has to be a specific path to your class/object, for example: /Game/Blueprints/GameMode.GameMode
* To get the ClassName, right click on your class / object in the content browser and then 'Copy Reference' and remove the 'Blueprint' and quotes.
* 
* Получить класс именем.
* Данное ClassName должно быть конкретным путем к вашему class/object, например: /Game/Blueprints/GameMode.GameMode
* Для получения ClassName щёлкнете правой кнопкой мыши свой class/object в браузере содержимого, а затем 'Copy Reference' и удалите 'Blueprint' и кавычки.
* 
* Blueprint'/Game/Blueprints/PlayerController.PlayerController'
* /Game/Blueprints/PlayerController.PlayerController
*/
UFUNCTION(BlueprintCallable, Category="IVS|Get", Meta=(DisplayName="Get Class Named", Keywords="get class named retrieve create object path find by value", ExpandEnumAsExecs="Branches")) static void GetClassNamed(ETF& Branches, bool& Success, const FString& ClassName, UClass*& Class_);
/** Get widget class named.
* The given ClassName has to be a specific path to your class/object, for example: /Game/Widget/Main.Main
* To get the ClassName, right click on your class / object in the content browser and then 'Copy Reference' and remove the 'Blueprint' and quotes.
* 
* Получить класс виджета именем.
* Данное ClassName должно быть конкретным путем к вашему class/object, например: /Game/Widget/Main.Main
* Для получения ClassName щёлкнете правой кнопкой мыши свой class/object в браузере содержимого, а затем 'Copy Reference' и удалите 'Blueprint' и кавычки.
* 
* Blueprint'/Game/Widget/Main.Main'
* /Game/Widget/Main.Main'
*/
UFUNCTION(BlueprintCallable, Category="IVS|UMG", Meta=(DisplayName="Get Widget Class Named", Keywords="get widget class named retrieve create object path find by value", ExpandEnumAsExecs="Branches")) static void GetWidgetClassNamed(ETF& Branches, bool& Success, const FString& ClassName, TSubclassOf<UWidget>& WidgetClass_);
/** Get named UWidget from ParentUserWidget.
* 
* Получить именем UWidget из UserWidget.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="UserWidget", ExpandEnumAsExecs="Branches")) static UWidget* GetWidgetNamedFromUserWidget(ETF& Branches, bool& Success, UUserWidget* UserWidget, const FString& Name);
/** Get named UWidget from ParentWidget.
* 
* Получить именем UWidget из PanelWidget.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="PanelWidget", ExpandEnumAsExecs="Branches")) static UWidget* GetWidgetNamedFromPanelWidget(ETF& Branches, bool& Success, UPanelWidget* PanelWidget, const FString& Name);
/** Get widgets by class. TopLevelOnly - only widgets that are added to the viewport.
* 
* Получить виджеты по классу. TopLevelOnly - только виджеты, которые добавлены в область просмотра.
*/
UFUNCTION(BlueprintCallable, Category="IVS|UMG", meta=(HidePin="WorldContextObject", DefaultToSelf="WorldContextObject", DeterminesOutputType="WidgetClass", DynamicOutputParam="FoundWidgets")) static void GetUserWidgetsOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass, TArray<UUserWidget*>& FoundUserWidgets, bool TopLevelOnly=true);
/** Remove widgets by class. TopLevelOnly - only widgets that are added to the viewport.
* 
* Удалить виджеты по классу. TopLevelOnly - только виджеты, которые добавлены в область просмотра.
*/
UFUNCTION(BlueprintCallable, Category="IVS|UMG", meta=( HidePin="WorldContextObject", DefaultToSelf="WorldContextObject")) static void RemoveUserWidgetsOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass);
/** Is UserWidget of Class in Viewport?
* 
* UserWidget класса в области просмотра?
*/
UFUNCTION(BlueprintPure, Category="IVS|UMG", meta=(HidePin="WorldContextObject", DefaultToSelf="WorldContextObject", ExpandEnumAsExecs="Branches")) static void IsUserWidgetOfClassInViewport(ETF& Branches, bool& Success, UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass);
/** Get the first UserWidget of the class in the viewport.
* 
* Получить первый UserWidget класса в области просмотра.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(WorldContext="WorldContextObject", DeterminesOutputType="UserWidgetClass", ExpandEnumAsExecs="Branches")) static UUserWidget* GetFirstUserWidgetOfClass(ETF& Branches, bool& Success, UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass, bool TopLevelOnly);
/** ChildWidget in PossibleParent?
* 
* ChildWidget в PossibleParent?
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="ChildWidget", ExpandEnumAsExecs="Branches")) static void WidgetIsChildOf(ETF& Branches, bool& Success, UWidget* ChildWidget, UWidget* PossibleParent);
/** Get the container of our ChildWidget by the UserWidgetClass class.
* 
* Получить контейнер по классу UserWidgetClass нашего ChildWidget.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="ChildWidget", DeterminesOutputType="UserWidgetClass", ExpandEnumAsExecs="Branches")) static UUserWidget* GetParentOfClassUserWidget(ETF& Branches, bool& Success, UWidget* ChildWidget, TSubclassOf<UUserWidget> UserWidgetClass);
/** Unlike normal caste, it does not select inheritance data.
* 
* В отличие от обычного каста не выбирает данные по наследованию.
* 
* Object->GetClass()==Class
*/
UFUNCTION(BlueprintCallable, Category="IVS", meta=(DefaultToSelf="Object", ExpandEnumAsExecs="Branches")) static UObject* CastToClass(ETF& Branches, bool& Succes, UObject* Object, TSubclassOf<UObject> Class);
/** Get the container of our ChildWidget by the WidgetClass class.
* 
* Получить контейнер по классу WidgetClass нашего ChildWidget.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="ChildWidget", DeterminesOutputType="WidgetClass", ExpandEnumAsExecs="Branches")) static UWidget* GetParentOfClassWidget(ETF& Branches, bool& Success, UWidget* ChildWidget, TSubclassOf<UWidget> WidgetClass);
/** Get the contents of the ParentWidget by the ChildUserWidgets class.
* 
* Получить содержимое ParentWidget по классу ChildUserWidgets.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="ParentWidget", DeterminesOutputType="UserWidgetClass", DynamicOutputParam="ChildUserWidgets")) static void GetChildrenOfClassUserWidget(UWidget* ParentWidget, TArray<UUserWidget*>& ChildUserWidgets, TSubclassOf<UUserWidget> UserWidgetClass, bool bImmediateOnly);
/** Get the contents of the ParentWidget by the ChildWidgets class.
* 
* Получить содержимое ParentWidget по классу ChildWidgets.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="ParentWidget", DeterminesOutputType="WidgetClass", DynamicOutputParam="ChildWidgets")) static void GetChildrenOfClassWidget(UWidget* ParentWidget, TArray<UWidget*>& ChildWidgets, TSubclassOf<UWidget> WidgetClass, bool bImmediateOnly);
/** Add Widget to UserWidget root.
* 
* Добавить Widget в корень UserWidget.
*/
UFUNCTION(Category="IVS|UMG", BlueprintCallable, BlueprintCosmetic, Meta=(DefaultToSelf="UserWidget")) static void AddWidgetToUserWidgetRoot(UUserWidget* UserWidget, UWidget* Widget);

//.cpp

void UILbr::GetLoadedClassNamed(ETF& Branches, bool& Success, const FString& ClassName, UClass*& Class_){
{// get class >>
UClass* FoundClass=FindObject<UClass>(ANY_PACKAGE, *(ClassName+"_C"));
if(FoundClass!=nullptr){Branches=ETF::True_; Success=true; Class_=FoundClass; return;}
}// get class <<
{// get class through redirector >>
UObjectRedirector* RenamedClassRedirector=FindObject<UObjectRedirector>(ANY_PACKAGE, *(ClassName+"_C"));
if((RenamedClassRedirector!=nullptr) && (RenamedClassRedirector->DestinationObject!=nullptr)){
UClass* FoundClass=CastChecked<UClass>(RenamedClassRedirector->DestinationObject);
if(FoundClass!=nullptr){Branches=ETF::True_; Success=true; Class_=FoundClass; return;}
}}// get class through redirector <<
Branches=ETF::False_;
Success=false;
Class_=NULL;
}

void UILbr::GetClassNamed(ETF& Branches, bool& Success, const FString& ClassName, UClass*& Class_){
GetLoadedClassNamed(Branches, Success, ClassName, Class_);
if(Success){return;}
TArray<FAssetData> AssetData;
GetAssetRegistry().GetAssetsByClass("Object", AssetData, true);
for(FAssetData &A: AssetData){if(ClassName==A.AssetName.ToString()){Class_=A.GetAsset()->GetClass(); Success=Class_!=nullptr; if(Success){Branches=ETF::True_;} return;}}
}





void UILbr::GetWidgetClassNamed(ETF& Branches, bool& Success, const FString& ClassName, TSubclassOf<UWidget>& WidgetClass_){
UClass* FoundClass;
GetLoadedClassNamed(Branches, Success, ClassName, FoundClass);
if(Success){WidgetClass_=FoundClass; return;}
TArray<FAssetData> AssetData;
GetAssetRegistry().GetAssetsByClass("Object", AssetData, true);
for(FAssetData &A: AssetData){if(ClassName==A.AssetName.ToString()){WidgetClass_=A.GetAsset()->GetClass(); Success=WidgetClass_!=nullptr; if(Success){Branches=ETF::True_;} return;}}
}

UWidget* UILbr::GetWidgetNamedFromUserWidget(ETF& Branches, bool& Success, UUserWidget* UserWidget, const FString& Name){
UWidget* ResultWidget=nullptr;
if(UserWidget && Name!="None" && !Name.IsEmpty()){ResultWidget=UserWidget->GetWidgetFromName(*Name);}
Success=ResultWidget!=nullptr; Branches=Success? ETF::True_:  ETF::False_;
return ResultWidget;
}

UWidget* UILbr::GetWidgetNamedFromPanelWidget(ETF& Branches, bool& Success, UPanelWidget* PanelWidget, const FString& Name){
UWidget* ResultWidget=nullptr;
if(PanelWidget && !Name.IsEmpty()){
for(int32 M=PanelWidget->GetChildrenCount(), I=0; I<M; I++){
ResultWidget=PanelWidget->GetChildAt(I);
if(UKismetSystemLibrary::GetDisplayName(ResultWidget)==Name){Branches=ETF::True_; Success=true; return ResultWidget;}
UUserWidget* UW=Cast<UUserWidget>(ResultWidget);
if(UW!=nullptr){ResultWidget=UILbr::GetWidgetNamedFromUserWidget(Branches, Success, UW, Name); if(Success){return ResultWidget;}}
UPanelWidget* PW=Cast<UPanelWidget>(ResultWidget);
if(PW!=nullptr){ResultWidget=UILbr::GetWidgetNamedFromPanelWidget(Branches, Success, PW, Name); if(Success){return ResultWidget;}}
}} 
Branches=ETF::False_; Success=false; return nullptr;
}

void UILbr::GetUserWidgetsOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass, TArray<UUserWidget*>& FoundUserWidgets, bool TopLevelOnly){
//Prevent possibility of an ever-growing array if user uses this in a loop
FoundUserWidgets.Empty();
if(!UserWidgetClass || !WorldContextObject){return;}
UWorld*const World=GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull);
if(!World){return;}
for(TObjectIterator<UUserWidget> Itr; Itr; ++Itr){if(Itr->GetWorld()!=World || !Itr->IsA(UserWidgetClass)){continue;} if(TopLevelOnly){if(Itr->IsInViewport()){FoundUserWidgets.Add(*Itr);}}else{FoundUserWidgets.Add(*Itr);}}
}

void UILbr::RemoveUserWidgetsOfClass(UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass){
if(!UserWidgetClass || !WorldContextObject){return;}
UWorld*const World=GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull);
if(!World){return;}
for(TObjectIterator<UUserWidget> Itr; Itr; ++Itr){if(Itr->GetWorld()!=World || !Itr->IsA(UserWidgetClass)){continue;} if(Itr->IsInViewport()){Itr->RemoveFromViewport();}}
}

void UILbr::IsUserWidgetOfClassInViewport(ETF& Branches, bool& Success, UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass){ 
if(!UserWidgetClass || !WorldContextObject){Branches=ETF::False_; Success=false; return;}
UWorld*const World=GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull);
if(!World){Branches=ETF::False_; Success=false; return;}
for(TObjectIterator<UUserWidget> Itr; Itr; ++Itr){if(Itr->GetWorld()!=World || !Itr->IsA(UserWidgetClass)){continue;} if(Itr->GetIsVisible()){Branches=ETF::True_; Success=true; return;}}
Branches=ETF::False_; Success=false; 
}

UUserWidget* UILbr::GetFirstUserWidgetOfClass(ETF& Branches, bool& Success, UObject* WorldContextObject, TSubclassOf<UUserWidget> UserWidgetClass, bool TopLevelOnly){
if(!UserWidgetClass || !WorldContextObject){Branches=ETF::False_; Success=false; return nullptr;}
const UWorld* World=GEngine->GetWorldFromContextObject(WorldContextObject, EGetWorldErrorMode::ReturnNull);
if(!World){Branches=ETF::False_; Success=false; return nullptr;}
UUserWidget* ResultWidget=nullptr;
for(TObjectIterator<UUserWidget> Itr; Itr; ++Itr){
UUserWidget* LiveWidget=*Itr;
// Skip any widget that's not in the current world context.
if(LiveWidget->GetWorld()!=World){continue;}
// Skip any widget that is not a child of the class specified.
if(!LiveWidget->GetClass()->IsChildOf(UserWidgetClass)){continue;}
if(!TopLevelOnly || LiveWidget->IsInViewport()){ResultWidget=LiveWidget; break;}
}
if(ResultWidget!=nullptr){Branches=ETF::True_; Success=true;}else{Branches=ETF::False_; Success=false;}
return ResultWidget;
}

void UILbr::WidgetIsChildOf(ETF& Branches, bool& Success, UWidget* ChildWidget, UWidget* PossibleParent){
Success=(ChildWidget && PossibleParent)? ChildWidget->IsChildOf(PossibleParent): false;
Branches=Success? ETF::True_: ETF::False_;
}

UUserWidget* UILbr::GetParentOfClassUserWidget(ETF& Branches, bool& Success, UWidget* ChildWidget /*=this*/, TSubclassOf<UUserWidget> UserWidgetClass){
UUserWidget* ResultParent=nullptr;
if(ChildWidget && UserWidgetClass){
UWidget* PossibleParent=ChildWidget->GetParent();
UWidget* NextPossibleParent=nullptr;
while(PossibleParent!=nullptr){
// Вернемся, когда найдем родительский элемент желаемого класса.
// Return once we find a parent of the desired class.
if(PossibleParent->GetClass()->IsChildOf(UserWidgetClass)){ResultParent=Cast<UUserWidget>(PossibleParent); break;}
NextPossibleParent=PossibleParent->GetParent();
// Если у нас нет родителя, следуем по внешней цепочке, пока не найдем другой виджет, если он вообще есть.
// If we don't have a parent, follow the outer chain until we find another widget, if at all.
if(NextPossibleParent==nullptr){UWidgetTree* WidgetTree=Cast<UWidgetTree>(PossibleParent->GetOuter()); if(WidgetTree){NextPossibleParent=Cast<UWidget>(WidgetTree->GetOuter());}}
PossibleParent=NextPossibleParent;
}
}
Success=ResultParent!=nullptr; Branches=Success? ETF::True_:  ETF::False_;
return ResultParent;
}

UObject* UILbr::CastToClass(ETF& Branches, bool& Succes, UObject* Object, TSubclassOf<UObject> Class){Succes=Object->GetClass()==Class; Branches=Succes? ETF::True_: ETF::False_; return Object;}

UWidget* UILbr::GetParentOfClassWidget(ETF& Branches, bool& Success, UWidget* ChildWidget, TSubclassOf<UWidget> WidgetClass){
if(ChildWidget && WidgetClass){
UWidget* PossibleParent=ChildWidget->GetParent();
while(PossibleParent!=nullptr){Success=PossibleParent->GetClass()==WidgetClass; if(Success){Success=true; Branches=ETF::True_; return PossibleParent;}else{PossibleParent=PossibleParent->GetParent();}}
}
Success=false; Branches=ETF::False_;
return nullptr;
}
 
void UILbr::GetChildrenOfClassUserWidget(UWidget* ParentWidget, TArray<UUserWidget*>& ChildUserWidgets, TSubclassOf<UUserWidget> UserWidgetClass, bool bImmediateOnly){
ChildUserWidgets.Empty();
if(ParentWidget && UserWidgetClass){
// Current set of widgets to check
TInlineComponentArray<UWidget*> WidgetsToCheck;
// Set of all widgets we have checked
TInlineComponentArray<UWidget*> CheckedWidgets;
WidgetsToCheck.Push(ParentWidget);
// While still work left to do
while(WidgetsToCheck.Num()>0){
// Get the next widgets off the queue
const bool bAllowShrinking=false;
UWidget* PossibleParent=WidgetsToCheck.Pop(bAllowShrinking);
// Add it to the 'checked' set, should not already be there!
if(!CheckedWidgets.Contains(PossibleParent)){
CheckedWidgets.Add(PossibleParent);
TArray<UWidget*> Widgets;
UWidgetTree::GetChildWidgets(PossibleParent, Widgets);
// Add any widget that is a child of the class specified.
// If we're not just looking for our immediate children,
// add this widget to list of widgets to check next.
for(UWidget* Widget:Widgets){if(!CheckedWidgets.Contains(Widget)){if(Widget->GetClass()->IsChildOf(UserWidgetClass)){ChildUserWidgets.Add(Cast<UUserWidget>(Widget));} if(!bImmediateOnly){WidgetsToCheck.Push(Widget);}}}
if(bImmediateOnly){break;}
}}}}

void UILbr::GetChildrenOfClassWidget(UWidget* ParentWidget, TArray<UWidget*>& ChildWidgets, TSubclassOf<UWidget> WidgetClass, bool bImmediateOnly){
ChildWidgets.Empty();
if(ParentWidget && WidgetClass){
// Current set of widgets to check
TInlineComponentArray<UWidget*> WidgetsToCheck;
// Set of all widgets we have checked
TInlineComponentArray<UWidget*> CheckedWidgets;
WidgetsToCheck.Push(ParentWidget);
// While still work left to do
while(WidgetsToCheck.Num()>0){
// Get the next widgets off the queue
const bool bAllowShrinking=false;
UWidget* PossibleParent=WidgetsToCheck.Pop(bAllowShrinking);
// Add it to the 'checked' set, should not already be there!
if(!CheckedWidgets.Contains(PossibleParent)){
CheckedWidgets.Add(PossibleParent);
TArray<UWidget*> Widgets;
UWidgetTree::GetChildWidgets(PossibleParent, Widgets);
// Add any widget that is a child of the class specified.
// If we're not just looking for our immediate children,
// add this widget to list of widgets to check next.
for(UWidget* Widget:Widgets){if(!CheckedWidgets.Contains(Widget)){if(Widget->GetClass()->IsChildOf(WidgetClass)){ChildWidgets.Add(Cast<UWidget>(Widget));} if(!bImmediateOnly){WidgetsToCheck.Push(Widget);}}}
if(bImmediateOnly){break;}
}}}}

http://format.krzaq.cc/