<repo_name>bianchengxiaosheng/Unreal4-Dynami-Umg-Test<file_sep>/AutoUI/Plugins/AutoGenerateUMGByXML/Source/AutoGenerateUMGByXML/Private/GenerateUMGFactory.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "GenerateUMGFactory.h"
#include "Classes/WidgetBlueprintFactory.h"
#include "Blueprint/UserWidget.h"
#include "Runtime/CoreUObject//Public/UObject/Package.h"
#include "Runtime/AssetRegistry/Public/AssetRegistryModule.h"
#include "Editor/UMGEditor/Public/WidgetBlueprint.h"
#include "Runtime/UMG/Public/UMG.h"
#include "Public/CoreUObject.h"

//class UMG;
//class Factory;
//class UCanvasPanel;
//class UImage;
//class UTextBlock;
//class UWidgetTree;

DEFINE_LOG_CATEGORY_STATIC(LogGenerateUMGFactory, Log, All);

GenerateUMGFactory::GenerateUMGFactory()
{
}

GenerateUMGFactory::~GenerateUMGFactory()
{
}

void GenerateUMGFactory::GenerateUMGByXML()
{
	UE_LOG(LogGenerateUMGFactory, Warning, TEXT("GenerateUMGByXML"));


	//NewWisget
	UFactory *factory = NewObject<UWidgetBlueprintFactory>();
	char * name = "/Game/W_MyWidgetBlueprint";


	UPackage *outer = CreatePackage(nullptr, UTF8_TO_TCHAR(name));
	UClass *u_class = factory->GetSupportedClass();

	char *obj_name = strrchr(name, '/') + 1;
	if (u_class->IsChildOf<UBlueprint>())
	{
		if (FindObject<UBlueprint>(outer, UTF8_TO_TCHAR(obj_name)))
		{
		}
	}
	UObject *u_object = factory->FactoryCreateNew(u_class, outer, FName(UTF8_TO_TCHAR(obj_name)), RF_Public | RF_Standalone, nullptr, GWarn);
	FAssetRegistryModule::AssetCreated(u_object);
	outer->MarkPackageDirty();

	UWidgetBlueprint* widget = Cast<UWidgetBlueprint>(u_object);
	widget->Modify();

	

	UCanvasPanel* slot1 = widget->WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), FName(TEXT("Slot1")));
	UImage* image1 = widget->WidgetTree->ConstructWidget<UImage>(UImage::StaticClass(),
		FName(TEXT("image1")));
	slot1->AddChild(image1);
	Cast<UPanelWidget>(slot1)->AddChild(image1);


	//UCanvasPanelSlot *slottemp = new UCanvasPanelSlot>("",widget->WidgetTree);
	//slottemp->Content = image1;


	//UCanvasPanelSlot* slot2 = widget->WidgetTree->ConstructWidget<UCanvasPanelSlot>(UCanvasPanelSlot::StaticClass(),
	//	FName(TEXT("Slot1")));
	UTextBlock* text_block = widget->WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(),
		FName(TEXT("text_block")));
	text_block->Text = FText::FromString(FString("Hellow"));
	//slot2->Content = text_block;


	UPanelWidget* rootWidgetTemp = Cast<UPanelWidget>(widget->WidgetTree->RootWidget);
	rootWidgetTemp->ClearChildren();
	rootWidgetTemp->AddChild(slot1);
	rootWidgetTemp->AddChild(image1);
	rootWidgetTemp->AddChild(text_block);

	widget->PostEditChange();






}
<file_sep>/README.md
# Unreal4-Dynami-Umg-Test
Unreal4 Dynami Umg Test
<file_sep>/AutoUI/Plugins/AutoGenerateUMGByXML/Source/AutoGenerateUMGByXML/Public/GenerateUMGFactory.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"


/**
 * 
 */
class AUTOGENERATEUMGBYXML_API GenerateUMGFactory
{
public:
	GenerateUMGFactory();

	~GenerateUMGFactory();

	void GenerateUMGByXML();

};
<file_sep>/AutoUI/Source/AutoUI/MyUserWidget.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "MyUserWidget.h"
//void UMyUserWidget::NativeConstruct()
//{
//	Super::NativeConstruct();
//
//}
void UMyUserWidget::OnCreationFromPalette()
{

}



<file_sep>/AutoUI/Plugins/AutoGenerateUMGByXML/Source/AutoGenerateUMGByXML/Private/AutoGenerateUMGByXMLCommands.cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.

#include "AutoGenerateUMGByXMLCommands.h"

#define LOCTEXT_NAMESPACE "FAutoGenerateUMGByXMLModule"

void FAutoGenerateUMGByXMLCommands::RegisterCommands()
{
	UI_COMMAND(PluginAction, "AutoGenerateUMGByXML", "Execute AutoGenerateUMGByXML action", EUserInterfaceActionType::Button, FInputGesture());
}

#undef LOCTEXT_NAMESPACE
<file_sep>/AutoUI/Plugins/AutoGenerateUMGByXML/Source/AutoGenerateUMGByXML/Private/AutoGenerateUMGByXML.cpp
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.

#include "AutoGenerateUMGByXML.h"
#include "AutoGenerateUMGByXMLStyle.h"
#include "AutoGenerateUMGByXMLCommands.h"
#include "Misc/MessageDialog.h"
#include "Framework/MultiBox/MultiBoxBuilder.h"

#include "LevelEditor.h"

static const FName AutoGenerateUMGByXMLTabName("×Ô¶¯Éú³ÉUMG");

#define LOCTEXT_NAMESPACE "FAutoGenerateUMGByXMLModule"
DEFINE_LOG_CATEGORY_STATIC(LogAutoGenerateUMG, Log, All);

void FAutoGenerateUMGByXMLModule::StartupModule()
{
	// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module

	umgFactory = new GenerateUMGFactory();

	FAutoGenerateUMGByXMLStyle::Initialize();
	FAutoGenerateUMGByXMLStyle::ReloadTextures();

	FAutoGenerateUMGByXMLCommands::Register();
	
	PluginCommands = MakeShareable(new FUICommandList);

	PluginCommands->MapAction(
		FAutoGenerateUMGByXMLCommands::Get().PluginAction,
		FExecuteAction::CreateRaw(this, &FAutoGenerateUMGByXMLModule::PluginButtonClicked),
		FCanExecuteAction());



		
	FLevelEditorModule& LevelEditorModule = FModuleManager::LoadModuleChecked<FLevelEditorModule>("LevelEditor");
	//
	//{
	//	TSharedPtr<FExtender> MenuExtender = MakeShareable(new FExtender());
	//	MenuExtender->AddMenuExtension("WindowLayout", EExtensionHook::After, PluginCommands, FMenuExtensionDelegate::CreateRaw(this, &FAutoGenerateUMGByXMLModule::AddMenuExtension));

	//	LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuExtender);
	//}
	//
	//{
	//	TSharedPtr<FExtender> ToolbarExtender = MakeShareable(new FExtender);
	//	ToolbarExtender->AddToolBarExtension("Settings", EExtensionHook::After, PluginCommands, FToolBarExtensionDelegate::CreateRaw(this, &FAutoGenerateUMGByXMLModule::AddToolbarExtension));
	//	
	//	LevelEditorModule.GetToolBarExtensibilityManager()->AddExtender(ToolbarExtender);
	//}

	{
		TSharedPtr<FExtender> MenuBarExtender = MakeShareable(new FExtender);
		MenuBarExtender->AddMenuBarExtension("Help", EExtensionHook::After, PluginCommands, FMenuBarExtensionDelegate::CreateRaw(this, &FAutoGenerateUMGByXMLModule::AddMenuBarExtension));
		LevelEditorModule.GetMenuExtensibilityManager()->AddExtender(MenuBarExtender);
	}
}

void FAutoGenerateUMGByXMLModule::ShutdownModule()
{
	// This function may be called during shutdown to clean up your module.  For modules that support dynamic reloading,
	// we call this function before unloading the module.
	FAutoGenerateUMGByXMLStyle::Shutdown();

	FAutoGenerateUMGByXMLCommands::Unregister();
}

void FAutoGenerateUMGByXMLModule::PluginButtonClicked()
{
	
	//UE_LOG(LogAutoGenerateUMG, Warning, TEXT("LogAutoGenerateUMG"));
	umgFactory->GenerateUMGByXML();
	// Put your "OnButtonClicked" stuff here
	//FText DialogText = FText::Format(
	//						LOCTEXT("PluginButtonDialogText", "Add code to {0} in {1} to override this button's actions"),
	//						FText::FromString(TEXT("FAutoGenerateUMGByXMLModule::PluginButtonClicked()")),
	//						FText::FromString(TEXT("AutoGenerateUMGByXML.cpp"))
	//				   );
	//FMessageDialog::Open(EAppMsgType::Ok, DialogText);
}

void FAutoGenerateUMGByXMLModule::AddMenuExtension(FMenuBuilder& Builder)
{
	Builder.AddMenuEntry(FAutoGenerateUMGByXMLCommands::Get().PluginAction);
	
}

void FAutoGenerateUMGByXMLModule::AddMenuBarExtension(FMenuBarBuilder &Builder)
{


	Builder.AddPullDownMenu(
		LOCTEXT("Custom_Menu_bar", "GenerateUMGByXML"),
		LOCTEXT("Custom tip", "GenerateUMGByXML"),
		FNewMenuDelegate::CreateRaw(this, &FAutoGenerateUMGByXMLModule::NewMenuBar),
		"GenerateUMGByXML"
	);
}

void FAutoGenerateUMGByXMLModule::NewMenuBar(FMenuBuilder &Builder) {
	Builder.AddMenuEntry(FAutoGenerateUMGByXMLCommands::Get().PluginAction);

}

void FAutoGenerateUMGByXMLModule::AddToolbarExtension(FToolBarBuilder& Builder)
{
	Builder.AddToolBarButton(FAutoGenerateUMGByXMLCommands::Get().PluginAction);

	
}


#undef LOCTEXT_NAMESPACE
	
IMPLEMENT_MODULE(FAutoGenerateUMGByXMLModule, AutoGenerateUMGByXML)<file_sep>/AutoUI/Source/AutoUI/AutoUIWidget.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Runtime/UMG/Public/UMG.h"

#include "Runtime/CoreUObject//Public/UObject/Package.h"
#include "Runtime/AssetRegistry/Public/AssetRegistryModule.h"
#include "AutoUIWidget.generated.h"

/**
 * 
 */
UCLASS()
class AUTOUI_API UAutoUIWidget : public UUserWidget
{
	GENERATED_BODY()
public:

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "UI")
		UButton* StartButton;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "UI")
		UButton* RegisterButton;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "UI")
		UButton* ExitButton;
	virtual void NativeConstruct() override;
	//UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidgetOptional))
	//UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (BindWidget))
		UTextBlock * testTxtBlock;
	////UButton* exampleButtton;
	//

	virtual TSharedRef<SWidget> RebuildWidget();
	
	UComboBoxString* ComboServer;

	UEditableTextBox* TxtBoxAccount;
	UEditableTextBox* TxtBoxPwd;
	bool Initialize() override;
	UAutoUIWidget(const FObjectInitializer& ObjectInitializer);
	//UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "UI")
	UTextBlock* TxtDebugMsg;

	//UFUNCTION()
	//	void ExitButClickEvent();

	//UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
		UEditableText* editTxt;
	//UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (BindWidget))
		UCanvasPanel* RootPanel;
	void InitTest();
#if WITH_EDITOR
	virtual void OnCreationFromPalette() override;
#endif
private:
	
		

	
	
};
<file_sep>/AutoUI/Source/AutoUI/AutoUI.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "AutoUI.h"
#include "Modules/ModuleManager.h"

IMPLEMENT_PRIMARY_GAME_MODULE( FDefaultGameModuleImpl, AutoUI, "AutoUI" );
<file_sep>/AutoUI/Source/AutoUI/AutoUIWidget.cpp
// Fill out your copyright notice in the Description page of Project Settings.

#include "AutoUIWidget.h"
#if WITH_EDITOR
#include "Editor/UMGEditor/Classes/WidgetBlueprintFactory.h"
#include "Editor/UnrealEd/Classes/Factories/Factory.h"
#include "Editor/UMGEditor/Public/WidgetBlueprint.h"
#endif
UAutoUIWidget::UAutoUIWidget(const FObjectInitializer& ObjectInitializer):Super(ObjectInitializer)
{
	ComboServer = NULL;
	TxtBoxAccount = nullptr;
	TxtBoxPwd = nullptr;
}
bool UAutoUIWidget::Initialize()
{
	
	if (!Super::Initialize())
	{
		return false;
	}


	if (UComboBoxString* combo = Cast<UComboBoxString>(GetWidgetFromName("ComboServer")))
	{
		ComboServer = combo;
	}
	if (UEditableTextBox* txtbox = Cast<UEditableTextBox>(GetWidgetFromName("TxtBoxAccount")))
	{
		TxtBoxAccount = txtbox;
		TxtBoxAccount->SetKeyboardFocus();
	}
	if (UEditableTextBox* txtbox = Cast<UEditableTextBox>(GetWidgetFromName("TxtBoxPwd")))
	{
		TxtBoxPwd = txtbox;
	}

	/** 初始化游戏开始按钮  GetWidgetFromName 从实例化的蓝图中获取对应名字的控件*/
	StartButton = Cast<UButton>(GetWidgetFromName(TEXT("Button_Start")));
	/** 初始化游戏注册按钮 */
	RegisterButton = Cast<UButton>(GetWidgetFromName(TEXT("Button_Register")));
	/** 初始化游戏退出按钮 */
	ExitButton = Cast<UButton>(GetWidgetFromName(TEXT("Button_Exit")));
	/** 设置退出游戏按钮的点击事件 */

	//if (ExitButton)
	//{
	//	ExitButton->OnClicked.AddDynamic(this, &UAutoUIWidget::ExitButClickEvent);
	//}
	
	return true;


}
#if WITH_EDITOR
 void UAutoUIWidget::OnCreationFromPalette()
{

}
#endif
void UAutoUIWidget::NativeConstruct()
{
	Super::NativeConstruct();
	//TxtDebugMsg = Cast<UTextBlock>(GetWidgetFromName(TEXT("TxtDebugMsg")));
}
void UAutoUIWidget::InitTest()
{
	UPanelWidget* RootWidget = Cast<UPanelWidget>(GetRootWidget());
	if (!RootWidget)
	{
		RootPanel = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), FName(TEXT("RootPanel")));
		RootWidget = RootPanel;

		//UImage* image = WidgetTree->ConstructWidget<UImage>(UImage::StaticClass());
		////image->SetBrushFromTexture(IndicatorTexture);
		//image->SetColorAndOpacity(FLinearColor(0, 0, 0, 0));
		//UPanelSlot* slot = RootWidget->AddChild(image);
		//UCanvasPanelSlot* canvasSlot = Cast<UCanvasPanelSlot>(slot);

		//canvasSlot->SetAnchors(FAnchors(0.5f, 0.5f));
		//canvasSlot->SetAlignment(FVector2D(0.5f, 0.5f));
		//canvasSlot->SetOffsets(FMargin(0, 0, 10, 10));

		//RootWidget->Slot = canvasSlot;
		UCanvasPanelSlot* RootWidgetSlot = Cast<UCanvasPanelSlot>(RootWidget->Slot);
		if (RootWidgetSlot)
		{
			
			RootWidgetSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
			RootWidgetSlot->SetOffsets(FMargin(0.f, 0.f));
		}
		else
		{
			
			//RootWidgetSlot->SetAnchors(FAnchors(0.f, 0.f, 1.f, 1.f));
			//RootWidgetSlot->SetOffsets(FMargin(0.f, 0.f));
			//RootWidget->Slot = RootWidgetSlot;
		}



		WidgetTree->RootWidget = RootWidget;
	
	
	}

	WidgetTree->Modify();
	if (RootWidget && WidgetTree)
	{
	
		
		testTxtBlock = WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(), FName(TEXT("testTxtBlock")));
	
		RootWidget->AddChild(testTxtBlock);
		
		uint64 MyValue = UINT64_MAX;
		testTxtBlock->SetText(FText::FromString(FString::Printf(TEXT("%llu"),MyValue)));
		UCanvasPanelSlot * textBoxSlot = Cast<UCanvasPanelSlot>(testTxtBlock->Slot);
		if (textBoxSlot)
		{
			textBoxSlot->SetAutoSize(true);
		}
		editTxt = WidgetTree->ConstructWidget<UEditableText>(UEditableText::StaticClass(), FName(TEXT("editTxt")));
		RootWidget->AddChild(editTxt);
		//if (editTxt)
		//{
		//	editTxt->SetText(FText("sss"));
		//}
		auto MyButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass());
		RootWidget->AddChild(MyButton);

		auto MyButton2 = NewObject<UButton>(this);
		RootWidget->AddChild(MyButton2);
		
	}

	PostEditChange();

#if WITH_EDITOR
	//NewWisget
	UFactory *factory = NewObject<UWidgetBlueprintFactory>();
	char * name = "/Game/W_MyWidgetBlueprint";
	

	UPackage *outer = CreatePackage(nullptr, UTF8_TO_TCHAR(name));
	UClass *u_class = factory->GetSupportedClass();

	char *obj_name = strrchr(name, '/') + 1;
	if (u_class->IsChildOf<UBlueprint>())
	{
		if (FindObject<UBlueprint>(outer, UTF8_TO_TCHAR(obj_name)))
		{
		}
	}
	UObject *u_object = factory->FactoryCreateNew(u_class, outer, FName(UTF8_TO_TCHAR(obj_name)), RF_Public | RF_Standalone, nullptr, GWarn);
	FAssetRegistryModule::AssetCreated(u_object);
	outer->MarkPackageDirty();

	UWidgetBlueprint* widget = Cast<UWidgetBlueprint>(u_object);
	widget->Modify();

	
	UCanvasPanel* slot1 = widget->WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(),FName(TEXT("Slot1")));
	UImage* image1 = widget->WidgetTree->ConstructWidget<UImage>(UImage::StaticClass(),
		FName(TEXT("image1")));
	slot1->AddChild(image1);
	//UCanvasPanelSlot* slot2 = widget->WidgetTree->ConstructWidget<UCanvasPanelSlot>(UCanvasPanelSlot::StaticClass(),
	//	FName(TEXT("Slot1")));
	UTextBlock* text_block = widget->WidgetTree->ConstructWidget<UTextBlock>(UTextBlock::StaticClass(),
		FName(TEXT("text_block")));
	text_block->Text = FText::FromString(FString("Hellow"));
	//slot2->Content = text_block;
	UPanelWidget* rootWidgetTemp = Cast<UPanelWidget>(widget->WidgetTree->RootWidget);
	rootWidgetTemp->ClearChildren();
	rootWidgetTemp->AddChild(slot1);
	rootWidgetTemp->AddChild(image1);

	widget->PostEditChange();



#endif
	
}

TSharedRef<SWidget> UAutoUIWidget::RebuildWidget()
{
	
	
	InitTest();
	TSharedRef<SWidget> widget = Super::RebuildWidget();
	
	
	return widget;
}

<file_sep>/AutoUI/Source/AutoUI/MyUserWidget.h
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "Runtime/UMG/Public/UMG.h"
#include "MyUserWidget.generated.h"

/**
 * 
 */
UCLASS()
class AUTOUI_API UMyUserWidget : public UUserWidget
{
	GENERATED_BODY()
		

//public:
//	UPROPERTY(meta = (BindWidgetOptional))
//		class UEditableTextBox* HealthProgressBar;
//	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "State",meta=(BindWidget))
//		float Energy;
//	
//	void NativeConstruct() override;
//	UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "State")
//
//		float Energy2;
//
//private:
//	UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Power", meta = (AllowPrivateAccess = true))
//		float PowerLevel;
//	
virtual void OnCreationFromPalette() override;
};
<file_sep>/AutoUI/Plugins/AutoGenerateUMGByXML/Source/AutoGenerateUMGByXML/Public/AutoGenerateUMGByXMLCommands.h
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.

#pragma once

#include "CoreMinimal.h"
#include "Framework/Commands/Commands.h"
#include "AutoGenerateUMGByXMLStyle.h"

class FAutoGenerateUMGByXMLCommands : public TCommands<FAutoGenerateUMGByXMLCommands>
{
public:

	FAutoGenerateUMGByXMLCommands()
		: TCommands<FAutoGenerateUMGByXMLCommands>(TEXT("AutoGenerateUMGByXML"), NSLOCTEXT("Contexts", "AutoGenerateUMGByXML", "AutoGenerateUMGByXML Plugin"), NAME_None, FAutoGenerateUMGByXMLStyle::GetStyleSetName())
	{
	}

	// TCommands<> interface
	virtual void RegisterCommands() override;

public:
	TSharedPtr< FUICommandInfo > PluginAction;

};
