AronaSlate/Source/Arona/UI/Style/AronaStyle.cpp
2024-01-25 11:21:15 +08:00

154 lines
5.3 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "AronaStyle.h"
#include "Styling/SlateStyleRegistry.h"
#include "Framework/Application/SlateApplication.h"
#include "Misc/AronaConfig.h"
TSharedPtr< FAronaStyle > FAronaStyle::StyleInstance = NULL;
void FAronaStyle::Initialize()
{
if (!StyleInstance.IsValid())
{
StyleInstance = Create();
StyleInstance->LoadConfig();
FSlateStyleRegistry::RegisterSlateStyle(*StyleInstance);
}
}
void FAronaStyle::Shutdown()
{
FSlateStyleRegistry::UnRegisterSlateStyle(*StyleInstance);
ensure(StyleInstance.IsUnique());
StyleInstance.Reset();
}
const FButtonStyle* FAronaStyle::GetButtonStyle(const FName& PropertyName, const ANSICHAR* Specifier)
{
return &StyleInstance->GetWidgetStyle<FButtonStyle>(PropertyName, Specifier, nullptr);
}
const FSlateBrush* FAronaStyle::GetSlateBrush(const FName& PropertyName, const ANSICHAR* Specifier)
{
return StyleInstance->GetBrush(PropertyName, Specifier, nullptr);
}
FSlateFontInfo FAronaStyle::GetFontInfo(const FName& PropertyName, const ANSICHAR* Specifier)
{
return StyleInstance->GetFontStyle(PropertyName, Specifier);
}
#define IMAGE_BRUSH( RelativePath, ... ) FSlateImageBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define BOX_BRUSH( RelativePath, ... ) FSlateBoxBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define BORDER_BRUSH( RelativePath, ... ) FSlateBorderBrush( Style->RootToContentDir( RelativePath, TEXT(".png") ), __VA_ARGS__ )
#define TTF_FONT( RelativePath, ... ) FSlateFontInfo( Style->RootToContentDir( RelativePath, TEXT(".ttf") ), __VA_ARGS__ )
#define OTF_FONT( RelativePath, ... ) FSlateFontInfo( Style->RootToContentDir( RelativePath, TEXT(".otf") ), __VA_ARGS__ )
#define MakeImageBrush(Name, Path, Size) \
FSlateImageBrush Name = IMAGE_BRUSH(TEXT(Path), Size); \
Name.Tiling = ESlateBrushTileType::NoTile; \
Name.ImageSize = Size; \
Name.DrawAs = ESlateBrushDrawType::Image; \
Name.ImageType = ESlateBrushImageType::FullColor; \
void FAronaStyle::LoadConfig()
{
TSharedRef<FAronaStyle> Style = StyleInstance.ToSharedRef();
AddButtonStyle(CloseButtonStyleName);
AddFontInfo(DefaultFontName);
AddFloatValue(VolumeMeterBarSpace);
AddImageBrush(PianoRollBackground);
AddImageBrush(VolumeMeterBar);
AddImageBrush(WhiteBrush);
AddImageBrush(BlackKey);
AddImageBrush(WhiteKey);
}
void FAronaStyle::AddImageBrush(FName Key)
{
TOptional<FString> ImageFileName = GetConfigValue<FString>(Key);
if (!ImageFileName)
return;
TOptional<FVector2f> ImageSize = GetConfigValue<FVector2f>(FName(Key.ToString() + "Size"));
const FString ImageFilePathName = StyleInstance->RootToContentDir(ImageFileName.GetValue(), TEXT(".png"));
StyleInstance->Set(Key, new FSlateImageBrush(ImageFilePathName, ImageSize.Get(FVector2f(16))));
}
void FAronaStyle::AddFloatValue(FName Key)
{
TOptional<float> Value = GetConfigValue<float>(Key);
if (!Value)
return;
StyleInstance->Set(Key, Value.GetValue());
}
void FAronaStyle::AddFontInfo(FName Key)
{
const TOptional<FString> FontName = GetConfigValue<FString>(Key);
if (!FontName)
return;
const TOptional<float> FontSize = GetConfigValue<float>(FName(Key.ToString() + "Size"));
StyleInstance->Set(Key, FSlateFontInfo(StyleInstance->RootToContentDir(FontName.GetValue(), TEXT(".ttf")), FontSize.Get(12)));
}
void FAronaStyle::AddButtonStyle(FName Key)
{
const FString ButtonNormalName = Key.ToString() + "Normal";
const FString ButtonHoveredName = Key.ToString() + "Hovered";
const FString ButtonPressedName = Key.ToString() + "Pressed";
const TOptional<FString> ButtonNormal = GetConfigValue<FString>(FName(ButtonNormalName));
const TOptional<FString> ButtonHovered = GetConfigValue<FString>(FName(ButtonHoveredName));
const TOptional<FString> ButtonPressed = GetConfigValue<FString>(FName(ButtonPressedName));
const TOptional<FVector2f> Size = GetConfigValue<FVector2f>(FName(Key.ToString() + "Size"));
FButtonStyle ButtonStyle;
const FSlateImageBrush ButtonNormalBrush = FSlateImageBrush(StyleInstance->RootToContentDir(ButtonNormal.Get(""), TEXT(".png")), Size.Get(FVector2f(16)));
const FSlateImageBrush ButtonHoveredBrush = FSlateImageBrush(StyleInstance->RootToContentDir(ButtonHovered.Get(""), TEXT(".png")), Size.Get(FVector2f(16)));
const FSlateImageBrush ButtonPressedBrush = FSlateImageBrush(StyleInstance->RootToContentDir(ButtonPressed.Get(""), TEXT(".png")), Size.Get(FVector2f(16)));
ButtonStyle.SetNormal(ButtonNormalBrush).SetHovered(ButtonHoveredBrush).SetPressed(ButtonPressedBrush);
StyleInstance->Set(CloseButtonStyleName, ButtonStyle);
}
TSharedRef< FAronaStyle > FAronaStyle::Create()
{
TSharedRef<FAronaStyle> Style = MakeShareable(new FAronaStyle("AronaStyle"));
FString SkinName;
FAronaConfig::GetValue("Skin", "Skin", SkinName);
Style->SkinDir = FPaths::ProjectContentDir() / TEXT("Skin") / SkinName;
Style->Config.Read(Style->SkinDir / TEXT("Skin.ini"));
Style->SetContentRoot(Style->SkinDir);
return Style;
}
#undef IMAGE_BRUSH
#undef BOX_BRUSH
#undef BORDER_BRUSH
#undef TTF_FONT
#undef OTF_FONT
void FAronaStyle::ReloadTextures()
{
if (FSlateApplication::IsInitialized())
{
FSlateApplication::Get().GetRenderer()->ReloadTextureResources();
}
}
const ISlateStyle& FAronaStyle::Get()
{
return *StyleInstance;
}
const FName& FAronaStyle::GetStyleSetName() const
{
static FName Name(TEXT("AronaModuleStyle"));
return Name;
}