Skip to content

Commit

Permalink
Merge pull request #33 from microdee/hotfix/1.1.3
Browse files Browse the repository at this point in the history
1.1.3
  • Loading branch information
microdee authored Mar 2, 2021
2 parents 1bffc58 + d3e9a7b commit ad9d9c6
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 217 deletions.
99 changes: 51 additions & 48 deletions Source/SpaceMouse/Private/SmEditorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// This source code is under MIT License https://github.com/microdee/UE4-SpaceMouse/blob/master/LICENSE

#include "SmEditorManager.h"
#include "SmViewportOverlay.h"

#include "CameraController.h"
#include "CommonBehaviors.h"
#include "SpaceMouse.h"
#include "Editor.h"
#include "SEditorViewport.h"
Expand All @@ -17,25 +19,12 @@ void FSmEditorManager::Tick(float DeltaSecs)
bPrintDebug = FSpaceMouseModule::Settings->DisplayDebugInformation;
for (auto sm : Devices)
{
sm->bPrintDebug = FSpaceMouseModule::Settings->DisplayDebugInformation;
sm->MaxReads = FSpaceMouseModule::Settings->MaxHidReadOperationsPerFrame;

sm->MovementTimeTolerance = FSpaceMouseModule::Settings->MovementSecondsTolerance;
sm->TranslationUnitsPerSec = FSpaceMouseModule::Settings->TranslationUnitsPerSec;
sm->XTranslationAxisMap = FSpaceMouseModule::Settings->XTranslationAxisMap;
sm->YTranslationAxisMap = FSpaceMouseModule::Settings->YTranslationAxisMap;
sm->ZTranslationAxisMap = FSpaceMouseModule::Settings->ZTranslationAxisMap;
sm->TranslationCurve = FSpaceMouseModule::Settings->TranslationCurve.GetRichCurveConst();

sm->RotationDegreesPerSec = FSpaceMouseModule::Settings->RotationDegreesPerSec;
sm->PitchAxisMap = FSpaceMouseModule::Settings->PitchAxisMap;
sm->YawAxisMap = FSpaceMouseModule::Settings->YawAxisMap;
sm->RollAxisMap = FSpaceMouseModule::Settings->RollAxisMap;
sm->RotationCurve = FSpaceMouseModule::Settings->RotationCurve.GetRichCurveConst();
sm->UserSettings = FSpaceMouseModule::Settings->GetUserSettings();
}
FSpaceMouseManager::Tick(DeltaSecs);

ManageActiveViewport();
ManageOrbitingOverlay();
MoveActiveViewport(Translation, Rotation);
LearnButtonMappings();

Expand All @@ -53,6 +42,19 @@ void FSmEditorManager::Start()
}
}

void FSmEditorManager::ManageOrbitingOverlay()
{

if(OnMovementStartedFrame && ActiveViewportClient)
{
OrbitingOverlay = MakeShared<FSmViewportOverlay>(ActiveViewportClient);
}
if(OnMovementEndedFrame)
{
OrbitingOverlay.Reset();
}
}

void FSmEditorManager::LearnButtonMappings()
{
if(FSpaceMouseModule::Settings->LearnDecreaseSpeed)
Expand Down Expand Up @@ -133,18 +135,7 @@ void FSmEditorManager::ManageActiveViewport()
}
}

bool FSmEditorManager::UseForceSetView(FEditorViewportClient* cvp)
{
static TSet<FName> ForceSetViewTable =
{
FName("SStaticMeshEditorViewport")
};

FName widgetType = cvp->GetEditorViewportWidget()->GetType();
return ForceSetViewTable.Contains(widgetType);
}

FVector FSmEditorManager::GetOrbitingPosDeltaOffset(FRotator rotDelta)
FVector FSmEditorManager::GetOrbitingPosDeltaOffset(FRotator rotDelta, float forwardDelta)
{
if(OnMovementStartedFrame)
{
Expand Down Expand Up @@ -175,27 +166,21 @@ FVector FSmEditorManager::GetOrbitingPosDeltaOffset(FRotator rotDelta)
ActiveViewportClient->GetViewRotation().RotateVector(LastOrbitPivotView);
}
}

if (FSpaceMouseModule::Settings->CameraBehavior == ESpaceMouseCameraBehavior::OrbittingNoRoll)
if(LastOrbitDistance > 0)
{
float yawcorr = FMath::Abs(FMath::Cos(FMath::DegreesToRadians(ActiveViewportClient->GetViewRotation().Pitch)));
rotDelta.Yaw *= yawcorr;
LastOrbitDistance -= forwardDelta;
LastOrbitPivotView.X -= forwardDelta;
}

GEngine->AddOnScreenDebugMessage(
13375,
FSpaceMouseModule::Settings->MovementSecondsTolerance,
FColor::Cyan,
FString::Printf(TEXT("Pivot distance: %f cm"), LastOrbitDistance)
);

FMatrix OrbitTr = FTransform(LastOrbitPivotView).ToMatrixWithScale()
* FTransform(rotDelta).ToMatrixWithScale()
* FTransform(FVector(-LastOrbitDistance, 0, 0)).ToMatrixWithScale();
if(OrbitingOverlay) OrbitingOverlay->Draw(LastOrbitPivot, LastOrbitDistance);

FVector ret = OrbitTr.TransformPosition(FVector::ZeroVector);
ret.X = 0;
return ret;
return UCommonBehaviors::GetOrbitingTranslationDelta(
LastOrbitPivotView,
ActiveViewportClient->GetViewRotation(),
rotDelta,
LastOrbitDistance,
FSpaceMouseModule::Settings->CameraBehavior == ESpaceMouseCameraBehavior::OrbittingWithRoll
);
}

FKeyEvent FSmEditorManager::GetKeyEventFromKey(const FInputActionKeyMapping& mapping)
Expand All @@ -216,6 +201,17 @@ FKeyEvent FSmEditorManager::GetKeyEventFromKey(const FInputActionKeyMapping& map
);
}

bool FSmEditorManager::AllowPerspectiveCameraMoveEvent(FEditorViewportClient* cvp)
{
static TSet<FName> IncompatibleViewports =
{
FName("SStaticMeshEditorViewport")
};

FName widgetType = cvp->GetEditorViewportWidget()->GetType();
return !IncompatibleViewports.Contains(widgetType);
}

void FSmEditorManager::MoveActiveViewport(FVector trans, FRotator rot)
{
if(!FSpaceMouseModule::Settings->ActiveInBackground)
Expand Down Expand Up @@ -294,7 +290,11 @@ void FSmEditorManager::MoveActiveViewport(FVector trans, FRotator rot)
{
OrbRot.Pitch *= currRot.Pitch > -80 && currRot.Pitch < 80;
}
trans = GetOrbitingPosDeltaOffset(orbitRotatesObject ? OrbRot : OrbRot.GetInverse()) / speedmul + (orbitMovesObject ? -trans : trans);
auto orbitTrans = orbitMovesObject ? -trans : trans;
trans = GetOrbitingPosDeltaOffset(
orbitRotatesObject ? OrbRot : OrbRot.GetInverse(),
orbitTrans.X * speedmul
) / speedmul + orbitTrans;
}

FVector posDelta = currRot.RotateVector(trans) * speedmul;
Expand Down Expand Up @@ -343,9 +343,12 @@ void FSmEditorManager::MoveActiveViewport(FVector trans, FRotator rot)
ActiveViewportClient->SetViewLocation(currPos);
ActiveViewportClient->SetViewRotation(currRot);

// This is important to trigger PerspectiveCameraMoved event from outside.
ActiveViewportClient->MoveViewportCamera(FVector::ZeroVector, FRotator::ZeroRotator);
ActiveViewportClient->Viewport->InvalidateHitProxy();
if(AllowPerspectiveCameraMoveEvent(ActiveViewportClient))
{
// This is important to trigger PerspectiveCameraMoved event from outside.
ActiveViewportClient->MoveViewportCamera(FVector::ZeroVector, FRotator::ZeroRotator);
ActiveViewportClient->Viewport->InvalidateHitProxy();
}
}
}
}
Expand Down
81 changes: 81 additions & 0 deletions Source/SpaceMouse/Private/SmViewportOverlay.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Fill out your copyright notice in the Description page of Project Settings.


#include "SmViewportOverlay.h"
#include "Editor.h"
#include "SEditorViewport.h"
#include "EditorViewportClient.h"
#include "Kismet/GameplayStatics.h"


FSmViewportOverlay::FSmViewportOverlay(FEditorViewportClient* VpClient)
: ViewportClient(VpClient)
{
auto VpRootWidget = VpClient->GetEditorViewportWidget();
Viewport = StaticCastSharedPtr<SViewport>(GetChildWidgetOfType(VpRootWidget, TEXT("SViewport")));
if(!Viewport) return;
Overlay = StaticCastSharedRef<SOverlay>(Viewport->GetChildren()->GetChildAt(0));

Overlay->AddSlot()
. HAlign(HAlign_Left)
. VAlign(VAlign_Top)
[
SAssignNew(DistanceText, STextBlock)
. SimpleTextMode(true)
. ColorAndOpacity(FLinearColor::Yellow)
. ShadowOffset(FVector2D(2, 2))
. ShadowColorAndOpacity(FLinearColor::Black)
//. RenderTransformPivot(FVector2D(0.5, 0.5))
];
}

FSmViewportOverlay::~FSmViewportOverlay()
{
if(Overlay)
Overlay->RemoveSlot(DistanceText.ToSharedRef());
}

void FSmViewportOverlay::Draw(FVector Pivot, float PivotDistance)
{
FSceneViewFamilyContext ViewFam({
ViewportClient->Viewport,
ViewportClient->GetScene(),
ViewportClient->EngineShowFlags
});
auto DpiScale = ViewportClient->ShouldDPIScaleSceneCanvas() ? ViewportClient->GetDPIScale() : 1.0f;
auto SceneView = ViewportClient->CalcSceneView(&ViewFam);
FVector2D Pos2;
auto VpSize = ViewportClient->Viewport->GetSizeXY();
SceneView->ProjectWorldToScreen(
Pivot,
FIntRect({0, 0}, VpSize),
SceneView->ViewMatrices.GetViewProjectionMatrix(),
Pos2
);


DistanceText->SetText(
FText::FromString(FString::Printf(TEXT("%.2f cm"), PivotDistance))
);

Pos2 /= DpiScale;
Pos2 -= DistanceText->GetDesiredSize() / 2.0f;
DistanceText->SetRenderTransform(FSlateRenderTransform(Pos2));
}

TSharedPtr<SWidget> FSmViewportOverlay::GetChildWidgetOfType(TSharedPtr<SWidget> InWidget, FName InType)
{
if(InWidget->GetType() == InType) return InWidget;
auto Children = InWidget->GetChildren();

for(int i=0; i<Children->Num(); i++)
{
auto CurrChild = GetChildWidgetOfType(
Children->GetChildAt(i),
InType
);
if(CurrChild.IsValid()) return CurrChild;
}
return {};
}

24 changes: 24 additions & 0 deletions Source/SpaceMouse/Private/SpaceMouseConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,27 @@ USpaceMouseConfig::USpaceMouseConfig(const FObjectInitializer& ObjectInitializer
RotationCurve.GetRichCurve()->AddKey(0.0, 0.0);
RotationCurve.GetRichCurve()->AddKey(1.0, 1.0);
}

FUserSettings USpaceMouseConfig::GetUserSettings()
{
return
{
DisplayDebugInformation,
MaxHidReadOperationsPerFrame,
MovementSecondsTolerance,
{
XTranslationAxisMap,
YTranslationAxisMap,
ZTranslationAxisMap,
TranslationUnitsPerSec,
TranslationCurve.GetRichCurve()
},
{
PitchAxisMap,
YawAxisMap,
RollAxisMap,
RotationDegreesPerSec,
RotationCurve.GetRichCurve()
}
};
}
8 changes: 6 additions & 2 deletions Source/SpaceMouse/Public/SmEditorManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

struct FInputActionKeyMapping;
class FEditorViewportClient;
class FSmViewportOverlay;

class SPACEMOUSE_API FSmEditorManager : public FSpaceMouseManager
{
Expand All @@ -26,9 +27,11 @@ class SPACEMOUSE_API FSmEditorManager : public FSpaceMouseManager
FEditorViewportClient* ActiveViewportClient = nullptr;
FString focusedVpType = "";

TSharedPtr<FSmViewportOverlay> OrbitingOverlay;

static FKeyEvent GetKeyEventFromKey(const FInputActionKeyMapping& mapping);
static bool UseForceSetView(FEditorViewportClient* cvp);
FVector GetOrbitingPosDeltaOffset(FRotator rotDelta);
static bool AllowPerspectiveCameraMoveEvent(FEditorViewportClient* cvp);
FVector GetOrbitingPosDeltaOffset(FRotator rotDelta, float forwardDelta);

public:

Expand All @@ -52,6 +55,7 @@ class SPACEMOUSE_API FSmEditorManager : public FSpaceMouseManager

virtual void Tick(float DeltaSecs) override;
void Start();
void ManageOrbitingOverlay();
void LearnButtonMappings();
void LearnButtonMapping(int& target);
void ManageActiveViewport();
Expand Down
25 changes: 25 additions & 0 deletions Source/SpaceMouse/Public/SmViewportOverlay.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"

/**
* Object managing the lifespan of orbiting information over the active viewport
*/
class SPACEMOUSE_API FSmViewportOverlay
{
public:
FSmViewportOverlay(FEditorViewportClient* VpClient);
~FSmViewportOverlay();

void Draw(FVector Pivot, float PivotDistance);

private:
FEditorViewportClient* ViewportClient = nullptr;
TSharedPtr<STextBlock> DistanceText;
TSharedPtr<SViewport> Viewport;
TSharedPtr<SOverlay> Overlay;

static TSharedPtr<SWidget> GetChildWidgetOfType(TSharedPtr<SWidget> InWidget, FName InType);
};
3 changes: 3 additions & 0 deletions Source/SpaceMouse/Public/SpaceMouseConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "CoreMinimal.h"
#include "UObject/Interface.h"
#include "UserSettings.h"
#include "Curves/CurveFloat.h"
#include "GameFramework/PlayerInput.h"

Expand Down Expand Up @@ -53,6 +54,8 @@ class USpaceMouseConfig : public UObject
public:
USpaceMouseConfig(const FObjectInitializer& ObjectInitializer);

FUserSettings GetUserSettings();

UPROPERTY(EditAnywhere, Config, Category = "Behavior")
bool ActiveInBackground = false;

Expand Down
5 changes: 3 additions & 2 deletions Source/SpaceMouse/SpaceMouse.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public SpaceMouse(ReadOnlyTargetRules Target) : base(Target)
PrivateDependencyModuleNames.AddRange(
new string[]
{
// ... add private dependencies that you statically link with here ...
// ... add private dependencies that you statically link with here ...
}
);

Expand All @@ -55,7 +55,8 @@ public SpaceMouse(ReadOnlyTargetRules Target) : base(Target)
"SlateCore",
"UnrealEd",
"LevelEditor",
"ApplicationCore"
"ApplicationCore",
"RHI"
// ... add private dependencies that you statically link with here ...
}
);
Expand Down
26 changes: 26 additions & 0 deletions Source/SpaceMouseReader/Private/CommonBehaviors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Fill out your copyright notice in the Description page of Project Settings.


#include "SpaceMouseReader/Public/CommonBehaviors.h"

FVector UCommonBehaviors::GetOrbitingTranslationDelta(
FVector Pivot,
FRotator CurrentRotation,
FRotator RotationDelta,
float Distance,
bool bWithRoll
) {
if (!bWithRoll)
{
const float YawCorr = FMath::Abs(FMath::Cos(FMath::DegreesToRadians(CurrentRotation.Pitch)));
RotationDelta.Yaw *= YawCorr;
}

const FMatrix OrbitTr = FTransform(Pivot).ToMatrixWithScale()
* FTransform(RotationDelta).ToMatrixWithScale()
* FTransform(FVector(-Distance, 0, 0)).ToMatrixWithScale();

FVector Ret = OrbitTr.TransformPosition(FVector::ZeroVector);
Ret.X = 0;
return Ret;
}
Loading

0 comments on commit ad9d9c6

Please sign in to comment.