Skip to content

Commit

Permalink
Add circuit connection drawing. (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanjon2040 authored Sep 28, 2021
1 parent 3b88e66 commit c634c33
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ void FFlowGraphConnectionDrawingPolicy::BuildPaths()
}
}

void FFlowGraphConnectionDrawingPolicy::DrawConnection(int32 LayerId, const FVector2D& Start, const FVector2D& End,
const FConnectionParams& Params)
{
if (UFlowGraphSettings::Get()->ConnectionDrawType == EFlowConnectionDrawType::Default)
{
FConnectionDrawingPolicy::DrawConnection(LayerId, Start, End, Params);
return;
}

Internal_DrawCircuitSpline(LayerId, Start, End, Params);
}

void FFlowGraphConnectionDrawingPolicy::Draw(TMap<TSharedRef<SWidget>, FArrangedWidget>& InPinGeometries, FArrangedChildren& ArrangedNodes)
{
BuildPaths();
Expand Down Expand Up @@ -162,3 +174,49 @@ void FFlowGraphConnectionDrawingPolicy::DetermineWiringStyle(UEdGraphPin* Output
}
}
}

void FFlowGraphConnectionDrawingPolicy::Internal_DrawCircuitSpline(const int32& LayerId, const FVector2D& Start, const FVector2D& End, const FConnectionParams& Params) const
{
const FVector2D StartingPoint = FVector2D(Start.X + UFlowGraphSettings::Get()->ConnectionSpacing.X, Start.Y);
const FVector2D EndPoint = FVector2D(End.X - UFlowGraphSettings::Get()->ConnectionSpacing.Y, End.Y);
const FVector2D ControlPoint = Internal_GetControlPoint(StartingPoint, EndPoint);

const FVector2D StartDirection = (Params.StartDirection == EGPD_Output) ? FVector2D(1.0f, 0.0f) : FVector2D(-1.0f, 0.0f);
const FVector2D EndDirection = (Params.EndDirection == EGPD_Input) ? FVector2D(1.0f, 0.0f) : FVector2D(-1.0f, 0.0f);

FSlateDrawElement::MakeDrawSpaceSpline(DrawElementsList, LayerId, Start, StartDirection, StartingPoint, EndDirection, Params.WireThickness, ESlateDrawEffect::None, Params.WireColor);
FSlateDrawElement::MakeDrawSpaceSpline(DrawElementsList, LayerId, StartingPoint, StartDirection, ControlPoint, EndDirection, Params.WireThickness, ESlateDrawEffect::None, Params.WireColor);
FSlateDrawElement::MakeDrawSpaceSpline(DrawElementsList, LayerId, ControlPoint, StartDirection, EndPoint, EndDirection, Params.WireThickness, ESlateDrawEffect::None, Params.WireColor);
FSlateDrawElement::MakeDrawSpaceSpline(DrawElementsList, LayerId, EndPoint, StartDirection, End, EndDirection, Params.WireThickness, ESlateDrawEffect::None, Params.WireColor);
}

FVector2D FFlowGraphConnectionDrawingPolicy::Internal_GetControlPoint(const FVector2D& Source, const FVector2D& Target) const
{
const FVector2D Delta = Target - Source;
const float Tangent = FMath::Tan(UFlowGraphSettings::Get()->ConnectionAngle * (PI / 180.f));

const float DX = FMath::Abs(Delta.X);
const float DY = FMath::Abs(Delta.Y);

const float SlopeWidth = DY / Tangent;
if (DX > SlopeWidth)
{
return Delta.X > 0.f ? FVector2D(Target.X - SlopeWidth, Source.Y) : FVector2D(Source.X - SlopeWidth, Target.Y);
}

const float SlopeHeight = DX * Tangent;
if (DY > SlopeHeight)
{
if (Delta.Y > 0.f)
{
return Delta.X < 0.f ? FVector2D(Source.X, Target.Y - SlopeHeight) : FVector2D(Target.X, Source.Y + SlopeHeight);
}

if (Delta.X < 0.f)
{
return FVector2D(Source.X, Target.Y + SlopeHeight);
}
}

return FVector2D(Target.X, Source.Y - SlopeHeight);
}
3 changes: 3 additions & 0 deletions Source/FlowEditor/Private/Graph/FlowGraphSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

UFlowGraphSettings::UFlowGraphSettings(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
, ConnectionDrawType(EFlowConnectionDrawType::Circuit)
, ConnectionAngle(45.f)
, ConnectionSpacing(FVector2D(30.f))
, bExposeFlowAssetCreation(true)
, bExposeFlowNodeCreation(true)
, bShowAssetToolbarAboveLevelEditor(true)
Expand Down
13 changes: 13 additions & 0 deletions Source/FlowEditor/Public/Graph/FlowGraphConnectionDrawingPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#include "ConnectionDrawingPolicy.h"
#include "EdGraphUtilities.h"

UENUM()
enum class EFlowConnectionDrawType : uint8
{
Default,
Circuit
};

struct FFlowGraphConnectionDrawingPolicyFactory : public FGraphPanelPinConnectionFactory
{
virtual ~FFlowGraphConnectionDrawingPolicyFactory()
Expand Down Expand Up @@ -42,7 +49,13 @@ class FFlowGraphConnectionDrawingPolicy : public FConnectionDrawingPolicy
void BuildPaths();

// FConnectionDrawingPolicy interface
virtual void DrawConnection(int32 LayerId, const FVector2D& Start, const FVector2D& End, const FConnectionParams& Params) override;
virtual void Draw(TMap<TSharedRef<SWidget>, FArrangedWidget>& PinGeometries, FArrangedChildren& ArrangedNodes) override;
virtual void DetermineWiringStyle(UEdGraphPin* OutputPin, UEdGraphPin* InputPin, FConnectionParams& Params) override;
// End of FConnectionDrawingPolicy interface

private:

void Internal_DrawCircuitSpline(const int32& LayerId, const FVector2D& Start, const FVector2D& End, const FConnectionParams& Params) const;
FVector2D Internal_GetControlPoint(const FVector2D& Source, const FVector2D& Target) const;
};
10 changes: 10 additions & 0 deletions Source/FlowEditor/Public/Graph/FlowGraphSettings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "FlowGraphConnectionDrawingPolicy.h"
#include "Engine/DeveloperSettings.h"

#include "FlowTypes.h"
Expand All @@ -14,6 +15,15 @@ class UFlowGraphSettings final : public UDeveloperSettings
GENERATED_UCLASS_BODY()
static UFlowGraphSettings* Get() { return CastChecked<UFlowGraphSettings>(UFlowGraphSettings::StaticClass()->GetDefaultObject()); }

UPROPERTY(config, EditAnywhere, Category = "Flow Graph")
EFlowConnectionDrawType ConnectionDrawType;

UPROPERTY(config, EditAnywhere, Category = "Flow Graph", meta = (EditCondition = "ConnectionDrawType == EFlowConnectionDrawType::Circuit"))
float ConnectionAngle;

UPROPERTY(config, EditAnywhere, Category = "Flow Graph", meta = (EditCondition = "ConnectionDrawType == EFlowConnectionDrawType::Circuit"))
FVector2D ConnectionSpacing;

/** Show Flow Asset in Flow category of "Create Asset" menu?
* Requires restart after making a change. */
UPROPERTY(EditAnywhere, config, Category = "Default UI")
Expand Down

0 comments on commit c634c33

Please sign in to comment.