|
| 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 | +} |
0 commit comments