#include "CppManaPlayer.h"
#include "ManaPlayer.h"
#include "Materials/MaterialInstanceDynamic.h"
namespace
{
const FName MaterialParameterNames[] =
{
"TextureY",
"TextureU",
"TextureV",
"TextureA",
};
const int32 NumMaterialParameter = 4;
}
void UCppManaPlayer::Setup(UManaMovie* ManaMovie, UMaterial* Material)
{
ManaPlayer = NewObject<UManaPlayer>();
if (!ManaPlayer)
{
UE_LOG(LogCppManaPlayer, Error, TEXT("Failed to create a UManaPlayer object."));
return;
}
MaterialInstance = UMaterialInstanceDynamic::Create(Material, this);
if (!MaterialInstance)
{
UE_LOG(LogCppManaPlayer, Error, TEXT("Failed to create a UMaterialInstanceDynamic object."));
return;
}
ManaPlayer->SetLooping(true);
ManaPlayer->OpenMovieSource(ManaMovie);
UManaTexture* ManaTexture = ManaPlayer->GetTexture();
if (!ManaTexture)
{
UE_LOG(LogCppManaPlayer, Error, TEXT("Failed to get ManaTexture from ManaPlayer."));
return;
}
for (int Index = 0; Index < ManaTexture->GetNumComponentTextures(); Index++)
{
UManaComponentTexture* CompTex = ManaTexture->GetComponentTexture(Index);
int32 PlaneIndex = UManaComponentTexture::GetPlaneIndexFromType(CompTex->ComponentType);
if (PlaneIndex < 0 || PlaneIndex >= NumMaterialParameter)
{
UE_LOG(LogCppManaPlayer, Warning, TEXT("Unknown plane index type."));
continue;
}
MaterialInstance->SetTextureParameterValue(MaterialParameterNames[PlaneIndex], CompTex);
}
}
void UCppManaPlayer::PlayMovie() const
{
if (!ManaPlayer)
{
UE_LOG(LogCppManaPlayer, Warning, TEXT("UCppManaPlayer::Setup() function was not called."));
return;
}
if (ManaPlayer->IsPlaying())
{
return;
}
auto Result = ManaPlayer->Play();
if (!Result)
{
UE_LOG(LogCppManaPlayer, Error, TEXT("Failed to start movie playback."));
}
}
void UCppManaPlayer::PauseMovie() const
{
if (!ManaPlayer)
{
UE_LOG(LogCppManaPlayer, Warning, TEXT("UCppManaPlayer::Setup() function was not called."));
return;
}
if (ManaPlayer->IsPaused())
{
return;
}
auto Result = ManaPlayer->Pause();
if (!Result)
{
UE_LOG(LogCppManaPlayer, Error, TEXT("Failed to pause movie playback."));
}
}
void UCppManaPlayer::CloseMovie()
{
if (ManaPlayer)
{
ManaPlayer->Close();
}
}
class SAMPLE_API UCppManaPlayer : public UObject
{
:
UFUNCTION(BlueprintCallable, Category = "Sample")
void Setup(UManaMovie* ManaMovie, UMaterial* Material);
UFUNCTION(BlueprintCallable, Category = "Sample")
void PlayMovie() const;
UFUNCTION(BlueprintCallable, Category = "Sample")
void PauseMovie() const;
UFUNCTION(BlueprintCallable, Category = "Sample")
void CloseMovie();
UPROPERTY(EditAnywhere)
UManaPlayer* ManaPlayer{ nullptr };
:
};