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

71 lines
1.8 KiB
C++

// Fill out your copyright notice in the Description page of Project Settings.
#include "SVolumeMeterBar.h"
#include "Dsp.h"
#include "UI/Style/AronaStyle.h"
#include "SlateOptMacros.h"
BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SVolumeMeterBar::Construct(const FArguments& InArgs)
{
Width = InArgs._Width;
MeterValue = InArgs._MeterValue;
Mode = InArgs._Mode;
MinValue = InArgs._MinValue;
MaxValue = InArgs._MaxValue;
}
int32 SVolumeMeterBar::OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect,
FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle,
bool bParentEnabled) const
{
const float BarSpace = FAronaStyle::GetValue<float>(VolumeMeterBarSpace);
const TArray<float>& CurrentValue = MeterValue.Get();
if (CurrentValue.Num() == 0)
{
return LayerId;
}
float CursorX = 0;
for (int i = 0; i < CurrentValue.Num(); ++i)
{
float Value;
switch (Mode)
{
case EVolumeMeterMode::Linear:
Value = CurrentValue[i];
break;
case EVolumeMeterMode::Decibel:
Value = Audio::ConvertToDecibels(CurrentValue[i]);
break;
default:
Value = 0;
}
Value = FMath::Clamp(Value, MinValue, MaxValue);
const float Height = (Value - MinValue) / (MaxValue - MinValue) * AllottedGeometry.Size.Y;
FSlateDrawElement::MakeBox(
OutDrawElements,
++LayerId,
AllottedGeometry.ToPaintGeometry(FVector2D(Width, Height), FSlateLayoutTransform(1.f, TransformPoint(1.f, FVector2f(CursorX, AllottedGeometry.Size.Y - Height)))),
FAronaStyle::GetSlateBrush(VolumeMeterBar),
ESlateDrawEffect::None,
FLinearColor::Red
);
CursorX += Width + BarSpace;
}
return LayerId;
}
FVector2D SVolumeMeterBar::ComputeDesiredSize(float LayoutScaleMultiplier) const
{
return FVector2D(Width, 0);
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION