背包控件以及四个子控件
背包子控件的基类
#pragma once
#include "CoreMinimal.h"
#include"SlAiTypes.h"
#include "SCompoundWidget.h"
class SLAICOURSE_API SSlAiContainerBaseWidget : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiContainerBaseWidget)
{}
SLATE_ATTRIBUTE(int,WorkIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
static TSharedPtr<SSlAiContainerBaseWidget> CreateContainer(EContainerType::Type NeedType, int WorkId);
protected:
TAttribute<int> WorkIndex;
TSharedPtr<class SBorder> ContainerBorder;
TSharedPtr<SBorder> ObjectImage;
TSharedPtr<class STextBlock> ObjectNumText;
};
#include "SSlAiContainerBaseWidget.h"
#include "SlateOptMacros.h"
#include"SlAiStyle.h"
#include"SlAiGameWidgetStyle.h"
#include"SBorder.h"
#include"STextBlock.h"
#include"SSlAiContainerNormalWidget.h"
#include"SSlAiContainerInputWidget.h"
#include"SSlAiContainerShortcutWidget.h"
#include"SSlAiContainerOutputWidget.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiContainerBaseWidget::Construct(const FArguments& InArgs)
{
WorkIndex = InArgs._WorkIndex;
ChildSlot
[
SAssignNew(ContainerBorder,SBorder)
.BorderImage(&SlAiStyle::GetGameStyle()->NormalContainerBrush)
.Padding(FMargin(8.f))
[
SAssignNew(ObjectImage,SBorder)
.BorderImage(&SlAiStyle::GetGameStyle()->EmptyBrush)
.HAlign(HAlign_Right)
.VAlign(VAlign_Bottom)
.Padding(FMargin(0.f,0.f,4.f,0.f))
[
SAssignNew(ObjectNumText,STextBlock)
.Font(SlAiStyle::GetGameStyle()->Font_Outline_16)
.ColorAndOpacity(SlAiStyle::GetGameStyle()->FontColor_Black)
]
]
];
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
TSharedPtr<SSlAiContainerBaseWidget> SSlAiContainerBaseWidget::CreateContainer(EContainerType::Type NeedType, int WorkId)
{
TSharedPtr<SSlAiContainerBaseWidget> ResultContainer;
switch (NeedType)
{
case EContainerType::Output:
SAssignNew(ResultContainer, SSlAiContainerOutputWidget).WorkIndex(WorkId);
break;
case EContainerType::Input:
SAssignNew(ResultContainer, SSlAiContainerInputWidget).WorkIndex(WorkId);
break;
case EContainerType::Normal:
SAssignNew(ResultContainer, SSlAiContainerNormalWidget).WorkIndex(WorkId);
break;
case EContainerType::Shortcut:
SAssignNew(ResultContainer, SSlAiContainerShortcutWidget).WorkIndex(WorkId);
break;
}
return ResultContainer;
}
背包子快捷栏类
#pragma once
#include "CoreMinimal.h"
#include "SSlAiContainerBaseWidget.h"
class SLAICOURSE_API SSlAiContainerShortcutWidget : public SSlAiContainerBaseWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiContainerShortcutWidget)
{}
SLATE_ATTRIBUTE(int,WorkIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
};
#include "SSlAiContainerShortcutWidget.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiContainerShortcutWidget::Construct(const FArguments& InArgs)
{
SSlAiContainerBaseWidget::Construct(SSlAiContainerBaseWidget::FArguments().WorkIndex(InArgs._WorkIndex));
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
背包输入台
#pragma once
#include "CoreMinimal.h"
#include "SSlAiContainerBaseWidget.h"
class SLAICOURSE_API SSlAiContainerInputWidget : public SSlAiContainerBaseWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiContainerInputWidget)
{}
SLATE_ATTRIBUTE(int,WorkIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
};
#include "SSlAiContainerInputWidget.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiContainerInputWidget::Construct(const FArguments& InArgs)
{
SSlAiContainerBaseWidget::Construct(SSlAiContainerBaseWidget::FArguments().WorkIndex(InArgs._WorkIndex));
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
背包主物品类
#pragma once
#include "CoreMinimal.h"
#include "SSlAiContainerBaseWidget.h"
class SLAICOURSE_API SSlAiContainerNormalWidget : public SSlAiContainerBaseWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiContainerNormalWidget)
{}
SLATE_ATTRIBUTE(int,WorkIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
};
#include "SSlAiContainerNormalWidget.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiContainerNormalWidget::Construct(const FArguments& InArgs)
{
SSlAiContainerBaseWidget::Construct(SSlAiContainerBaseWidget::FArguments().WorkIndex(InArgs._WorkIndex));
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
背包输出台类
#pragma once
#include "CoreMinimal.h"
#include "SSlAiContainerBaseWidget.h"
class SLAICOURSE_API SSlAiContainerOutputWidget : public SSlAiContainerBaseWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiContainerOutputWidget)
{}
SLATE_ATTRIBUTE(int,WorkIndex)
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
};
#include "SSlAiContainerOutputWidget.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiContainerOutputWidget::Construct(const FArguments& InArgs)
{
SSlAiContainerBaseWidget::Construct(SSlAiContainerBaseWidget::FArguments().WorkIndex(InArgs._WorkIndex));
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
在GameMode声明 初始化背包管理类委托
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "SlAiGameMode.generated.h"
DECLARE_DELEGATE(FInitPackageManager)
UCLASS()
class SLAICOURSE_API ASlAiGameMode : public AGameModeBase
{
GENERATED_BODY()
public:
ASlAiGameMode();
virtual void Tick(float DeltaSeconds) override;
void InitGamePlayModule();
public:
class ASlAiPlayerController* SPController;
class ASlAiPlayerCharacter* SPCharacter;
class ASlAiPlayerState* SPState;
FInitPackageManager InitPackageManager;
protected:
virtual void BeginPlay() override;
void InitializePackage();
private:
bool IsInitPackage;
};
#include "SlAiGameMode.h"
#include"SlAiDataHandle.h"
#include"SlAiGameInstance.h"
#include"SlAiHelper.h"
#include"Kismet/GameplayStatics.h"
#include"SlAiGameHUD.h"
#include"SlAiPlayerController.h"
#include"SlAiPlayerState.h"
#include"SlAiPlayerCharacter.h"
ASlAiGameMode::ASlAiGameMode()
{
PrimaryActorTick.bCanEverTick = true;
HUDClass = ASlAiGameHUD::StaticClass();
PlayerControllerClass = ASlAiPlayerController::StaticClass();
PlayerStateClass = ASlAiPlayerState::StaticClass();
DefaultPawnClass = ASlAiPlayerCharacter::StaticClass();
IsInitPackage = false;
}
void ASlAiGameMode::Tick(float DeltaSeconds)
{
InitializePackage();
}
void ASlAiGameMode::InitGamePlayModule()
{
SPController = Cast<ASlAiPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
SPCharacter = Cast<ASlAiPlayerCharacter>(UGameplayStatics::GetPlayerCharacter(GetWorld(), 0));
SPState = Cast<ASlAiPlayerState>(SPController->PlayerState);
}
void ASlAiGameMode::BeginPlay()
{
SlAiDataHandle::Get()->InitializeGameData();
if (!SPController) InitGamePlayModule();
}
void ASlAiGameMode::InitializePackage()
{
if (IsInitPackage) return;
InitPackageManager.ExecuteIfBound();
IsInitPackage = true;
}
在背包控件初始化背包
#pragma once
#include "CoreMinimal.h"
#include"SlAiTypes.h"
#include "Widgets/SCompoundWidget.h"
class SLAICOURSE_API SSlAiPackageWidget : public SCompoundWidget
{
public:
SLATE_BEGIN_ARGS(SSlAiPackageWidget)
{}
SLATE_END_ARGS()
void Construct(const FArguments& InArgs);
void InitPackageManager();
private:
TSharedPtr<class SUniformGridPanel> ShortcutGrid;
TSharedPtr<SUniformGridPanel> PackageGrid;
TSharedPtr<SUniformGridPanel> CompoundGrid;
TSharedPtr<class SBorder> OutputBorder;
};
#include "SSlAiPackageWidget.h"
#include "SlateOptMacros.h"
#include"SlAiStyle.h"
#include"SlAiGameWidgetStyle.h"
#include"SBox.h"
#include"SImage.h"
#include"SOverlay.h"
#include"SUniformGridPanel.h"
#include"SSlAiContainerBaseWidget.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiPackageWidget::Construct(const FArguments& InArgs)
{
ChildSlot
[
SNew(SOverlay)
+SOverlay::Slot()
.HAlign(HAlign_Right)
.VAlign(VAlign_Center)
.Padding(FMargin(0.f,0.f,50.f,0.f))
[
SNew(SBox)
.WidthOverride(800.f)
.HeightOverride(800.f)
[
SNew(SOverlay)
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
[
SNew(SImage)
.Image(&SlAiStyle::GetGameStyle()->PackageBGBrush)
]
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Padding(FMargin(40.f,680.f,40.f,40.f))
[
SAssignNew(ShortcutGrid,SUniformGridPanel)
]
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Padding(FMargin(40.f,320.f,40.f,160.f))
[
SAssignNew(PackageGrid,SUniformGridPanel)
]
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Padding(FMargin(80.f,40.f,480.f,520.f))
[
SAssignNew(CompoundGrid,SUniformGridPanel)
]
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Padding(FMargin(560.f,120.f,160.f,600.f))
[
SAssignNew(OutputBorder,SBorder)
]
+SOverlay::Slot()
.HAlign(HAlign_Fill)
.VAlign(VAlign_Fill)
.Padding(FMargin(400.f,120.f,320.f,600.f))
[
SNew(SImage)
.Image(&SlAiStyle::GetGameStyle()->CompoundArrowBrush)
]
]
]
];
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SSlAiPackageWidget::InitPackageManager()
{
for (int i = 0; i < 9; ++i)
{
TSharedPtr<SSlAiContainerBaseWidget> NewContainer = SSlAiContainerBaseWidget::CreateContainer(EContainerType::Shortcut, i);
ShortcutGrid->AddSlot(i, 0)[NewContainer->AsShared()];
}
for (int i = 0; i < 36; ++i)
{
TSharedPtr<SSlAiContainerBaseWidget> NewContainer = SSlAiContainerBaseWidget::CreateContainer(EContainerType::Normal, i);
PackageGrid->AddSlot(i % 9, i / 9)[NewContainer->AsShared()];
}
for (int i = 0; i < 9; ++i)
{
TSharedPtr<SSlAiContainerBaseWidget> NewContainer = SSlAiContainerBaseWidget::CreateContainer(EContainerType::Input, i);
CompoundGrid->AddSlot(i % 3, i / 3)[NewContainer->AsShared()];
}
TSharedPtr<SSlAiContainerBaseWidget> NewContainer = SSlAiContainerBaseWidget::CreateContainer(EContainerType::Output, 1);
OutputBorder->SetContent(NewContainer->AsShared());
}
在GameHUD绑定委托
GM->InitPackageManager.BindRaw(GameHUDWidget->PackageWidget.Get(), &SSlAiPackageWidget::InitPackageManager);
运行
|