Skip to content

Commit a985af3

Browse files
committed
Added file picker code
1 parent 7546615 commit a985af3

File tree

6 files changed

+269
-0
lines changed

6 files changed

+269
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is the first line of DummyFile1.txt
2+
This is a nice line!

FilePicker/DummyTextFiles/DummyFile2.txt

Whitespace-only changes.

FilePicker/FilePicker.Build.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
2+
3+
using UnrealBuildTool;
4+
5+
public class FilePicker : ModuleRules
6+
{
7+
public FilePicker(ReadOnlyTargetRules Target) : base(Target)
8+
{
9+
PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
10+
11+
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay","SlateCore" });
12+
}
13+
}

FilePicker/FilePickerCharacter.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
2+
3+
#include "FilePickerCharacter.h"
4+
#include "HeadMountedDisplayFunctionLibrary.h"
5+
#include "Camera/CameraComponent.h"
6+
#include "Components/CapsuleComponent.h"
7+
#include "Components/InputComponent.h"
8+
#include "GameFramework/CharacterMovementComponent.h"
9+
#include "GameFramework/Controller.h"
10+
#include "GameFramework/SpringArmComponent.h"
11+
12+
//File picker includes
13+
#include "Developer/DesktopPlatform/Public/IDesktopPlatform.h"
14+
#include "Developer/DesktopPlatform/Public/DesktopPlatformModule.h"
15+
16+
//////////////////////////////////////////////////////////////////////////
17+
// AFilePickerCharacter
18+
19+
AFilePickerCharacter::AFilePickerCharacter()
20+
{
21+
// Set size for collision capsule
22+
GetCapsuleComponent()->InitCapsuleSize(42.f, 96.0f);
23+
24+
// set our turn rates for input
25+
BaseTurnRate = 45.f;
26+
BaseLookUpRate = 45.f;
27+
28+
// Don't rotate when the controller rotates. Let that just affect the camera.
29+
bUseControllerRotationPitch = false;
30+
bUseControllerRotationYaw = false;
31+
bUseControllerRotationRoll = false;
32+
33+
// Configure character movement
34+
GetCharacterMovement()->bOrientRotationToMovement = true; // Character moves in the direction of input...
35+
GetCharacterMovement()->RotationRate = FRotator(0.0f, 540.0f, 0.0f); // ...at this rotation rate
36+
GetCharacterMovement()->JumpZVelocity = 600.f;
37+
GetCharacterMovement()->AirControl = 0.2f;
38+
39+
// Create a camera boom (pulls in towards the player if there is a collision)
40+
CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));
41+
CameraBoom->SetupAttachment(RootComponent);
42+
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
43+
CameraBoom->bUsePawnControlRotation = true; // Rotate the arm based on the controller
44+
45+
// Create a follow camera
46+
FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));
47+
FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
48+
FollowCamera->bUsePawnControlRotation = false; // Camera does not rotate relative to arm
49+
50+
// Note: The skeletal mesh and anim blueprint references on the Mesh component (inherited from Character)
51+
// are set in the derived blueprint asset named MyCharacter (to avoid direct content references in C++)
52+
}
53+
54+
//////////////////////////////////////////////////////////////////////////
55+
// Input
56+
57+
void AFilePickerCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
58+
{
59+
// Set up gameplay key bindings
60+
check(PlayerInputComponent);
61+
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
62+
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
63+
64+
PlayerInputComponent->BindAxis("MoveForward", this, &AFilePickerCharacter::MoveForward);
65+
PlayerInputComponent->BindAxis("MoveRight", this, &AFilePickerCharacter::MoveRight);
66+
67+
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
68+
// "turn" handles devices that provide an absolute delta, such as a mouse.
69+
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
70+
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
71+
PlayerInputComponent->BindAxis("TurnRate", this, &AFilePickerCharacter::TurnAtRate);
72+
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
73+
PlayerInputComponent->BindAxis("LookUpRate", this, &AFilePickerCharacter::LookUpAtRate);
74+
75+
// handle touch devices
76+
PlayerInputComponent->BindTouch(IE_Pressed, this, &AFilePickerCharacter::TouchStarted);
77+
PlayerInputComponent->BindTouch(IE_Released, this, &AFilePickerCharacter::TouchStopped);
78+
79+
// VR headset functionality
80+
PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AFilePickerCharacter::OnResetVR);
81+
}
82+
83+
84+
void AFilePickerCharacter::OpenFileDialog(const FString& DialogTitle, const FString& DefaultPath, const FString& FileTypes, TArray<FString>& OutFileNames)
85+
{
86+
if (GEngine)
87+
{
88+
if (GEngine->GameViewport)
89+
{
90+
void* ParentWindowHandle = GEngine->GameViewport->GetWindow()->GetNativeWindow()->GetOSWindowHandle();
91+
IDesktopPlatform* DesktopPlatform = FDesktopPlatformModule::Get();
92+
if (DesktopPlatform)
93+
{
94+
//Opening the file picker!
95+
uint32 SelectionFlag = 0; //A value of 0 represents single file selection while a value of 1 represents multiple file selection
96+
DesktopPlatform->OpenFileDialog(ParentWindowHandle, DialogTitle, DefaultPath, FString(""), FileTypes, SelectionFlag, OutFileNames);
97+
}
98+
}
99+
}
100+
}
101+
102+
void AFilePickerCharacter::PrintData(const FString& File)
103+
{
104+
TArray<FString> LoadedText;
105+
FFileHelper::LoadFileToStringArray(LoadedText, *File);
106+
for (int32 i = 0; i < LoadedText.Num(); i++)
107+
{
108+
GLog->Log(LoadedText[i]);
109+
}
110+
}
111+
112+
void AFilePickerCharacter::OnResetVR()
113+
{
114+
UHeadMountedDisplayFunctionLibrary::ResetOrientationAndPosition();
115+
}
116+
117+
void AFilePickerCharacter::TouchStarted(ETouchIndex::Type FingerIndex, FVector Location)
118+
{
119+
Jump();
120+
}
121+
122+
void AFilePickerCharacter::TouchStopped(ETouchIndex::Type FingerIndex, FVector Location)
123+
{
124+
StopJumping();
125+
}
126+
127+
void AFilePickerCharacter::TurnAtRate(float Rate)
128+
{
129+
// calculate delta for this frame from the rate information
130+
AddControllerYawInput(Rate * BaseTurnRate * GetWorld()->GetDeltaSeconds());
131+
}
132+
133+
void AFilePickerCharacter::LookUpAtRate(float Rate)
134+
{
135+
// calculate delta for this frame from the rate information
136+
AddControllerPitchInput(Rate * BaseLookUpRate * GetWorld()->GetDeltaSeconds());
137+
}
138+
139+
void AFilePickerCharacter::MoveForward(float Value)
140+
{
141+
if ((Controller != NULL) && (Value != 0.0f))
142+
{
143+
// find out which way is forward
144+
const FRotator Rotation = Controller->GetControlRotation();
145+
const FRotator YawRotation(0, Rotation.Yaw, 0);
146+
147+
// get forward vector
148+
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::X);
149+
AddMovementInput(Direction, Value);
150+
}
151+
}
152+
153+
void AFilePickerCharacter::MoveRight(float Value)
154+
{
155+
if ( (Controller != NULL) && (Value != 0.0f) )
156+
{
157+
// find out which way is right
158+
const FRotator Rotation = Controller->GetControlRotation();
159+
const FRotator YawRotation(0, Rotation.Yaw, 0);
160+
161+
// get right vector
162+
const FVector Direction = FRotationMatrix(YawRotation).GetUnitAxis(EAxis::Y);
163+
// add movement in that direction
164+
AddMovementInput(Direction, Value);
165+
}
166+
}

FilePicker/FilePickerCharacter.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 1998-2018 Epic Games, Inc. All Rights Reserved.
2+
3+
#pragma once
4+
5+
#include "CoreMinimal.h"
6+
#include "GameFramework/Character.h"
7+
#include "FilePickerCharacter.generated.h"
8+
9+
UCLASS(config=Game)
10+
class AFilePickerCharacter : public ACharacter
11+
{
12+
GENERATED_BODY()
13+
14+
/** Camera boom positioning the camera behind the character */
15+
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
16+
class USpringArmComponent* CameraBoom;
17+
18+
/** Follow camera */
19+
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Camera, meta = (AllowPrivateAccess = "true"))
20+
class UCameraComponent* FollowCamera;
21+
public:
22+
AFilePickerCharacter();
23+
24+
/** Base turn rate, in deg/sec. Other scaling may affect final turn rate. */
25+
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
26+
float BaseTurnRate;
27+
28+
/** Base look up/down rate, in deg/sec. Other scaling may affect final rate. */
29+
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Camera)
30+
float BaseLookUpRate;
31+
32+
protected:
33+
34+
/** Resets HMD orientation in VR. */
35+
void OnResetVR();
36+
37+
/** Called for forwards/backward input */
38+
void MoveForward(float Value);
39+
40+
/** Called for side to side input */
41+
void MoveRight(float Value);
42+
43+
/**
44+
* Called via input to turn at a given rate.
45+
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
46+
*/
47+
void TurnAtRate(float Rate);
48+
49+
/**
50+
* Called via input to turn look up/down at a given rate.
51+
* @param Rate This is a normalized rate, i.e. 1.0 means 100% of desired turn rate
52+
*/
53+
void LookUpAtRate(float Rate);
54+
55+
/** Handler for when a touch input begins. */
56+
void TouchStarted(ETouchIndex::Type FingerIndex, FVector Location);
57+
58+
/** Handler for when a touch input stops. */
59+
void TouchStopped(ETouchIndex::Type FingerIndex, FVector Location);
60+
61+
protected:
62+
// APawn interface
63+
virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;
64+
// End of APawn interface
65+
66+
public:
67+
/** Returns CameraBoom subobject **/
68+
FORCEINLINE class USpringArmComponent* GetCameraBoom() const { return CameraBoom; }
69+
/** Returns FollowCamera subobject **/
70+
FORCEINLINE class UCameraComponent* GetFollowCamera() const { return FollowCamera; }
71+
72+
/*
73+
* Opens a file dialog for the specified data
74+
* Filetypes must be in the format of: <File type Description>|*.<actual extension>
75+
* You can combine multiple extensions by placing ";" between them
76+
* For example: Text Files|*.txt|Excel files|*.csv|Image Files|*.png;*.jpg;*.bmp will display 3 lines for 3 different type of files.
77+
*/
78+
UFUNCTION(BlueprintCallable, Category = "FilePicker")
79+
void OpenFileDialog(const FString& DialogTitle, const FString& DefaultPath, const FString& FileTypes, TArray<FString>& OutFileNames);
80+
81+
/*Prints the data of the given file*/
82+
UFUNCTION(BlueprintCallable, Category = "FilePicker")
83+
void PrintData(const FString& File);
84+
};
85+

FilePicker/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# File picker tutorial
2+
3+
Read the full tutorial here: https://wp.me/p6hvtS-ry

0 commit comments

Comments
 (0)