Skip to content

Commit eb8fe6f

Browse files
authored
Merge pull request #1 from glowingsteam/master
Add generic X/Y coordinate delegate so selector can be used as a virtual joystick
2 parents 195d4cc + bfd0934 commit eb8fe6f

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

Source/ColorWheelPlugin/Private/ColorWidget.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ TSharedRef<SWidget> UColorWidget::RebuildWidget()
1212
.HueCircle(&HueCircle)
1313
.OnMouseCaptureBegin_UObject(this, &UColorWidget::MouseDown)
1414
.OnMouseCaptureEnd_UObject(this, &UColorWidget::MouseUp)
15-
.OnValueChanged(FOnLinearColorValueChanged::CreateUObject(this, &UColorWidget::OnValueChanged));
15+
.OnValueChanged(FOnLinearColorValueChanged::CreateUObject(this, &UColorWidget::OnValueChanged))
16+
.OnPositionChanged(FOnPositionChanged::CreateUObject(this, &UColorWidget::OnPositionUpdated));
1617

1718
return ColorWheel.ToSharedRef();
1819
}
@@ -30,6 +31,13 @@ void UColorWidget::OnValueChanged(FLinearColor NewValue)
3031
OnColorChanged.Broadcast(Color);
3132
}
3233

34+
35+
void UColorWidget::OnPositionUpdated(FVector2D NewPosition)
36+
{
37+
//UE_LOG(LogTemp, Warning, TEXT("D - X: %f - Y: %f"), NewPosition.X, NewPosition.Y)
38+
OnPositionChanged.Broadcast(NewPosition);
39+
}
40+
3341
/// Main Functions ///
3442

3543
void UColorWidget::SetColor(const FLinearColor NewColor)

Source/ColorWheelPlugin/Private/SWColorWheel.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void SWColorWheel::Construct(const FArguments& InArgs)
1616
OnMouseCaptureBegin = InArgs._OnMouseCaptureBegin;
1717
OnMouseCaptureEnd = InArgs._OnMouseCaptureEnd;
1818
OnValueChanged = InArgs._OnValueChanged;
19+
OnPositionChanged = InArgs._OnPositionChanged;
1920
}
2021

2122
void SWColorWheel::SetColorAndOpacity(FLinearColor InColorAndOpacity, TEnumAsByte<EWheelBrushTarget> TargetBrush)
@@ -138,6 +139,10 @@ bool SWColorWheel::ProcessMouseAction(const FGeometry& MyGeometry, const FPointe
138139
{
139140
const FVector2D LocalMouseCoordinate = MyGeometry.AbsoluteToLocal(MouseEvent.GetScreenSpacePosition());
140141
const FVector2D RelativePositionFromCenter = (2.0f * LocalMouseCoordinate - MyGeometry.GetLocalSize()) / (MyGeometry.GetLocalSize() - SelectorImage->ImageSize);
142+
143+
OnPositionChanged.ExecuteIfBound(FVector2D(FMath::Clamp(RelativePositionFromCenter.X, -1.0, 1.0), -1.0 * FMath::Clamp(RelativePositionFromCenter.Y, -1.0, 1.0)));
144+
//UE_LOG(LogTemp, Warning, TEXT("X: %f - Y: %f"), RelativePositionFromCenter.X, RelativePositionFromCenter.Y)
145+
141146
const float RelativeRadius = RelativePositionFromCenter.Size();
142147

143148
if (RelativeRadius <= 1.0f || bProcessWhenOutsideColorWheel)

Source/ColorWheelPlugin/Public/ColorWidget.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class SWColorWheel;
2222
*/
2323
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FColorChangedEvent, const FLinearColor&, NewColor);
2424

25+
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPositionUpdatedEvent, const FVector2D&, NewPosition);
26+
2527
/** Delegate: Broadcasted when the mouse is down on the Wheel. */
2628
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMouseDownEvent);
2729

@@ -56,10 +58,14 @@ class COLORWHEELPLUGIN_API UColorWidget final : public UWidget
5658
| Events |
5759
\============================================================================*/
5860

59-
/** Event called when value is changed */
61+
/** Event called when color value is changed */
6062
UPROPERTY(BlueprintAssignable, Category="Color Wheel|Event")
6163
FColorChangedEvent OnColorChanged;
6264

65+
/** Event called when value is changed */
66+
UPROPERTY(BlueprintAssignable, Category = "Color Wheel|Event")
67+
FPositionUpdatedEvent OnPositionChanged;
68+
6369
/** Event called when the mouse is being pressed on the wheel */
6470
UPROPERTY(BlueprintAssignable, Category="Color Wheel|Event")
6571
FMouseDownEvent OnMouseDown;
@@ -135,6 +141,8 @@ class COLORWHEELPLUGIN_API UColorWidget final : public UWidget
135141
// Callback Function: Handles the broadcasting of the delegate once a color changes.
136142
void OnValueChanged(FLinearColor NewValue);
137143

144+
void OnPositionUpdated(FVector2D NewPosition);
145+
138146
// Callback Function: Handles the broadcasting for when the Mouse is lifted from the wheel.
139147
inline void MouseUp() { OnMouseUp.Broadcast(); };
140148

Source/ColorWheelPlugin/Public/SWColorWheel.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class FPaintArgs;
2020
class FSlateWindowElementList;
2121
struct FSlateBrush;
2222

23+
DECLARE_DELEGATE_OneParam(FOnPositionChanged, FVector2D)
24+
2325
// Used for some of the Color wheels UFUNCTIONS to specify the target brush.
2426
UENUM()
2527
enum EWheelBrushTarget
@@ -50,6 +52,7 @@ class COLORWHEELPLUGIN_API SWColorWheel : public SLeafWidget
5052
, _OnMouseCaptureBegin()
5153
, _OnMouseCaptureEnd()
5254
, _OnValueChanged()
55+
, _OnPositionChanged()
5356
{ }
5457

5558
/// Attributes ///
@@ -76,6 +79,9 @@ class COLORWHEELPLUGIN_API SWColorWheel : public SLeafWidget
7679
/** Invoked when a new value is selected on the color wheel. */
7780
SLATE_EVENT(FOnLinearColorValueChanged, OnValueChanged)
7881

82+
/** Invoked when a new value is selected on the widget wheel. */
83+
SLATE_EVENT(FOnPositionChanged, OnPositionChanged);
84+
7985
SLATE_END_ARGS()
8086

8187
public:
@@ -153,5 +159,8 @@ class COLORWHEELPLUGIN_API SWColorWheel : public SLeafWidget
153159

154160
/** Invoked when a new value is selected on the color wheel. */
155161
FOnLinearColorValueChanged OnValueChanged;
162+
163+
/** Invoked when a new value is selected on the color wheel. */
164+
FOnPositionChanged OnPositionChanged;
156165

157166
};

0 commit comments

Comments
 (0)