Skip to content

Instantly share code, notes, and snippets.

@johnfredcee
Last active October 26, 2023 10:31
Show Gist options
  • Save johnfredcee/29921c9f20ed74be5cf1c4c2df21b8d5 to your computer and use it in GitHub Desktop.
Save johnfredcee/29921c9f20ed74be5cf1c4c2df21b8d5 to your computer and use it in GitHub Desktop.
Slate Polyline Editor Control
// Copyright (C) John Connors 2023
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#pragma once
#include "CoreMinimal.h"
#include "Styling/SlateTypes.h"
#include "Widgets/SLeafWidget.h"
/**
*
*/
class SANDBOXTOOLS_API SPolylineEditor : public SLeafWidget
{
public:
SLATE_BEGIN_ARGS(SPolylineEditor)
{}
SLATE_ARGUMENT(TArray<FVector2f>, Points)
SLATE_ARGUMENT(FVector2f, LineScale)
SLATE_ARGUMENT(FSlateBrush*, Brush)
SLATE_ARGUMENT(float, LineThickness)
SLATE_ARGUMENT(bool, bClosedLine)
SLATE_ARGUMENT(bool, bMirror)
SLATE_ARGUMENT(FSimpleDelegate, OnPointAdded)
SLATE_ARGUMENT(FSimpleDelegate, OnPointRemoved)
SLATE_ARGUMENT(FSimpleDelegate, OnPointMoved)
SLATE_END_ARGS()
const static FVector2f PointRadius;
/** Constructs this widget with InArgs */
void Construct(const FArguments& InArgs);
virtual int32 OnPaint(const FPaintArgs& Args, const FGeometry& AllottedGeometry, const FSlateRect& MyCullingRect, FSlateWindowElementList& OutDrawElements, int32 LayerId, const FWidgetStyle& InWidgetStyle, bool bParentEnabled) const override;
virtual FReply OnMouseButtonDown(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
virtual FReply OnMouseButtonUp(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
virtual FReply OnMouseButtonDoubleClick(const FGeometry& InMyGeometry, const FPointerEvent& InMouseEvent) override;
virtual FReply OnMouseMove(const FGeometry& MyGeometry, const FPointerEvent& MouseEvent) override;
virtual FVector2D ComputeDesiredSize(float LayoutScaleMultiplier) const override;
void SetColorAndOpacity(FLinearColor InColorAndOpacity);
const FLinearColor& GetColorAndOpacity() const;
void SetBrush(FSlateBrush* InBrush);
void SetLineThickness(float InThickness);
void SetLineScale(FVector2f InScale);
void SetPoints(const TArray<FVector2f>& InPoints);
const TArray<FVector2f>& GetPoints();
void SetClosedLine(bool bInClosedLine);
void SetMirrored(bool bMirror);
bool IsMirrorVertex(int Index) const;
FVector2f MakePoint(const FGeometry& InGeometry, const FVector2f& InPoint);
void ComputeAllPoints(TArray<FVector2f>& AllPoints, bool bIncludeMirrorPoints) const;
void ComputeLinePoints(TArray<FVector2f>& LinePoints, const FGeometry& InGeometry, bool bIncludeMirrorPoints = true) const;
int32 PointIndexFromCursorPos(const FGeometry& Points, const FVector2f LocalCursorPos, const float RadiusSquared);
void AddClosestPointAndIndex(const FGeometry& MyGeometry, FVector2f LocalCursorPos);
protected:
TArray<FVector2f> Points;
FVector2f LineScale;
FInvalidatableBrushAttribute Brush;
float LineThickness;
int32 PointBeingDragged;
bool bClosedLine;
bool bMirror;
FLinearColor ColorAndOpacity;
FSimpleDelegate OnPointAdded;
FSimpleDelegate OnPointRemoved;
FSimpleDelegate OnPointMoved;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment