|
| 1 | +// Copyright (c) W2.Wizard 2020-2021. All Rights Reserved. |
| 2 | + |
| 3 | +#include "ColorWidget.h" |
| 4 | + |
| 5 | +/// Slate Overrides /// |
| 6 | + |
| 7 | +TSharedRef<SWidget> UColorWidget::RebuildWidget() |
| 8 | +{ |
| 9 | + ColorWheel = SNew(SWColorWheel) |
| 10 | + .SelectedColor_UObject(this, &UColorWidget::GetColor) |
| 11 | + .SelectorPin(&SelectorPin) |
| 12 | + .HueCircle(&HueCircle) |
| 13 | + .OnMouseCaptureBegin_UObject(this, &UColorWidget::MouseDown) |
| 14 | + .OnMouseCaptureEnd_UObject(this, &UColorWidget::MouseUp) |
| 15 | + .OnValueChanged(FOnLinearColorValueChanged::CreateUObject(this, &UColorWidget::OnValueChanged)); |
| 16 | + |
| 17 | + return ColorWheel.ToSharedRef(); |
| 18 | +} |
| 19 | + |
| 20 | +void UColorWidget::ReleaseSlateResources(bool bReleaseChildren) |
| 21 | +{ |
| 22 | + Super::ReleaseSlateResources(bReleaseChildren); |
| 23 | + |
| 24 | + ColorWheel.Reset(); |
| 25 | +} |
| 26 | + |
| 27 | +void UColorWidget::OnValueChanged(FLinearColor NewValue) |
| 28 | +{ |
| 29 | + Color = NewValue.HSVToLinearRGB(); |
| 30 | + OnColorChanged.Broadcast(Color); |
| 31 | +} |
| 32 | + |
| 33 | +/// Main Functions /// |
| 34 | + |
| 35 | +void UColorWidget::SetColor(const FLinearColor NewColor) |
| 36 | +{ |
| 37 | + // Incase of Black, the wheel gets fucked ヽ(`Д´)ノ︵ ┻━┻. |
| 38 | + if (NewColor.IsAlmostBlack()) |
| 39 | + { |
| 40 | + // We pass in a false value |
| 41 | + Color = FLinearColor::White; |
| 42 | + |
| 43 | + // Preserve alpha. |
| 44 | + Color.A = NewColor.A; |
| 45 | + IsNull = true; |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + Color = NewColor; |
| 51 | + IsNull = false; |
| 52 | +} |
| 53 | + |
| 54 | +FLinearColor UColorWidget::GetCurrentColor() |
| 55 | +{ |
| 56 | + // Was input black? |
| 57 | + if (IsNull) |
| 58 | + { |
| 59 | + // Return black color |
| 60 | + auto FallbackVal = FLinearColor::Black; |
| 61 | + FallbackVal.A = Color.A; |
| 62 | + |
| 63 | + return FallbackVal; |
| 64 | + } |
| 65 | + |
| 66 | + return Color; |
| 67 | +} |
| 68 | + |
| 69 | +/// Generics /// |
| 70 | + |
| 71 | +void UColorWidget::SetColorAndOpacity(FLinearColor InColorAndOpacity, TEnumAsByte<EWheelBrushTarget> Target) |
| 72 | +{ |
| 73 | + // Update the values themselves, again this doesnt look good so change it later perhaps. |
| 74 | + auto ColorVal = FSlateColor(InColorAndOpacity); |
| 75 | + switch (Target) |
| 76 | + { |
| 77 | + case EWheelBrushTarget::All: |
| 78 | + SelectorPin.TintColor = ColorVal; |
| 79 | + HueCircle.TintColor = ColorVal; |
| 80 | + break; |
| 81 | + |
| 82 | + case EWheelBrushTarget::SelectorPin: |
| 83 | + SelectorPin.TintColor = ColorVal; |
| 84 | + break; |
| 85 | + |
| 86 | + case EWheelBrushTarget::HueCircle: |
| 87 | + HueCircle.TintColor = ColorVal; |
| 88 | + break; |
| 89 | + } |
| 90 | + |
| 91 | + // Update the visual widget itself |
| 92 | + if (ColorWheel.IsValid()) |
| 93 | + { |
| 94 | + ColorWheel->SetColorAndOpacity(InColorAndOpacity, Target); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | + |
0 commit comments