Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A try to create a default widget totally in c++.
// Fill out your copyright notice in the Description page of Project Settings.
#include "LectureWidget.h"
#include "Blueprint/WidgetTree.h"
#include "Components/Button.h"
#include "Components/Image.h"
#include "Components/CanvasPanel.h"
#include "Components/CanvasPanelSlot.h"
#include "Layout/Anchors.h"
#include "UObject/ConstructorHelpers.h"
ULectureWidget::ULectureWidget(const FObjectInitializer& ObjectInitializer): Super(ObjectInitializer){
// Set icons textures
static ConstructorHelpers::FObjectFinder<UTexture2D> HomeIconObj(TEXT("/Game/UI/Icons/home-outline"));
static ConstructorHelpers::FObjectFinder<UTexture2D> MenuIconObj(TEXT("/Game/UI/Icons/th-list-outline"));
static ConstructorHelpers::FObjectFinder<UTexture2D> ArrowLeftIconObj(TEXT("/Game/UI/Icons/arrow-left-outline"));
static ConstructorHelpers::FObjectFinder<UTexture2D> ArrowRightIconObj(TEXT("/Game/UI/Icons/arrow-right-outline"));
if (HomeIconObj.Object != NULL) HomeIconTexture = HomeIconObj.Object;
if (MenuIconObj.Object != NULL) MenuIconTexture = MenuIconObj.Object;
if (ArrowLeftIconObj.Object != NULL) ArrowLeftIconTexture = ArrowLeftIconObj.Object;
if (ArrowRightIconObj.Object != NULL) ArrowRightIconTexture = ArrowRightIconObj.Object;
}
void ULectureWidget::NativeConstruct()
{
Super::NativeConstruct();
UCanvasPanel* Root = WidgetTree->ConstructWidget<UCanvasPanel>(UCanvasPanel::StaticClass(), FName("Canvas"));
// UCanvasPanel* Root = Cast<UCanvasPanel>(GetRootWidget());
WidgetTree->RootWidget = Root;
UWidget* root = GetRootWidget();
UE_LOG(LogTemp, Warning, TEXT("Root widget class: %s"), *root->GetClass()->GetName());
// Create buttons and set them in a slot
HomeButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("Home_Button"));
HomeButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(HomeIconTexture));
HomeButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
HomeButton->WidgetStyle.Normal.DrawAs = ESlateBrushDrawType::Image;
HomeButton->WidgetStyle.Normal.TintColor = FLinearColor(1.f, 0.f, .3f, 1.f);
HomeButton->WidgetStyle.Normal.Margin = 0.f;
// HomeButton->SetColorAndOpacity(FLinearColor(1.f, 1.f, 1.f, 0));
UCanvasPanelSlot* HomeButtonSlot = Cast<UCanvasPanelSlot>(Root->AddChild(HomeButton));
HomeButtonSlot->SetAnchors(FAnchors(.0f, .0f, .0f, .0f));
HomeButtonSlot->SetSize(FVector2D(80.f, 80.f));
HomeButtonSlot->SetPosition(FVector2D(80.f, 80.f));
//HomeButtonSlot->SetAlignment(FVector2D(.5f, .5f));
MenuButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("Menu_Button"));
MenuButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(MenuIconTexture));
MenuButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
HomeButton->WidgetStyle.Normal.DrawAs= ESlateBrushDrawType::Image;
MenuButton->WidgetStyle.Normal.Margin = 0.f;
UCanvasPanelSlot* MenuButtonSlot = Cast<UCanvasPanelSlot>(Root->AddChild(MenuButton));
MenuButtonSlot->SetAnchors(FAnchors(1.f, 0, 1.f, 0));
MenuButtonSlot->SetSize(FVector2D(80.f, 80.f));
MenuButtonSlot->SetPosition(FVector2D(-160.f, 80.f));
ArrowLeftButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("ArrowLeft_Button"));
ArrowLeftButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(ArrowLeftIconTexture));
ArrowLeftButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
ArrowLeftButton->WidgetStyle.Normal.DrawAs = ESlateBrushDrawType::Image;
ArrowLeftButton->WidgetStyle.Normal.Margin = 0.f;
UCanvasPanelSlot* ArrowLeftButtonSlot = Cast<UCanvasPanelSlot>(Root->AddChild(ArrowLeftButton));
ArrowLeftButtonSlot->SetAnchors(FAnchors(0, 1.f, 0, 1.f));
ArrowLeftButtonSlot->SetSize(FVector2D(80.f, 80.f));
ArrowLeftButtonSlot->SetPosition(FVector2D(80.f, -200.f));
ArrowRightButton = WidgetTree->ConstructWidget<UButton>(UButton::StaticClass(), FName("ArrowRight_Button"));
ArrowRightButton->WidgetStyle.Normal.SetResourceObject(Cast<UObject>(ArrowRightIconTexture));
ArrowRightButton->WidgetStyle.Normal.SetImageSize(FVector2D(80.f, 80.f));
ArrowRightButton->WidgetStyle.Normal.DrawAs = ESlateBrushDrawType::Image;
ArrowRightButton->WidgetStyle.Normal.Margin = 0.f;
UCanvasPanelSlot* ArrowRightButtonSlot = Cast<UCanvasPanelSlot>(Root->AddChild(ArrowRightButton));
ArrowRightButtonSlot->SetAnchors(FAnchors(1.f, 1.f, 1.f, 1.f));
ArrowRightButtonSlot->SetSize(FVector2D(80.f, 80.f));
ArrowRightButtonSlot->SetPosition(FVector2D(-160.f, -200.f));
// Root->AddToRoot();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment