LEAP Documentation 40220
Documentation for the LEAP project
InteractableComponent.h
Go to the documentation of this file.
1// Copyright 2017 Blue Isle Studios, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "Components/ActorComponent.h"
7#include "Runtime/CoreUObject/Public/UObject/TextProperty.h"
8#include "InteractableComponent.generated.h"
9
10DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorHighlightSignature, APlayerController*, Sender, UPrimitiveComponent*, Component);
11DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorHighlightExitSignature, APlayerController*, Sender, UPrimitiveComponent*, Component);
12DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorSelectedSignature, APlayerController*, Sender, UPrimitiveComponent*, Component);
13DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorHoldSelectedSignature, APlayerController*, Sender, UPrimitiveComponent*, Component);
14DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorValidInteractionSignature, APlayerController*, Sender, bool, bValidInteraction);
15
16UENUM(BlueprintType)
17enum class EInteractionType : uint8
18{
19 Select = 0,
20 Hold = 1,
21 Both = 2
22};
23
24USTRUCT(Blueprintable)
26{
27 GENERATED_USTRUCT_BODY()
28
29public:
30 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = HighlightableComponent)
31 bool bIsHighlightable = true;
32 UPROPERTY(BlueprintReadWrite, EditAnywhere, meta = (MultiLine = "true", EditCondition = "bIsHighlightable"))
33 FText HighlightText = NSLOCTEXT("ProjectX", "cmd_interact", "Interact");
34};
35
36UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
37class PROJECTX_API UInteractableComponent : public UActorComponent
38{
39 GENERATED_UCLASS_BODY()
40
41//~ Begin UActorComponent Interface
42public:
43 virtual void BeginPlay() override;
44 virtual void Deactivate() override;
45//~ End UActorComponent Interface
46
47public:
48 virtual bool CanSelect(APlayerController* Sender, UPrimitiveComponent* Component) const;
49 bool IsValidInteraction(APlayerController* Sender) const;
50
51 virtual void Select(APlayerController* Sender, UPrimitiveComponent* Component);
52 virtual void HoldSelect(APlayerController* Sender, UPrimitiveComponent* Component);
53 virtual void StartHold(APlayerController* Sender, UPrimitiveComponent* Component);
54 virtual void ExitHold(APlayerController* Sender, UPrimitiveComponent* Component);
55
56 UFUNCTION(BlueprintCallable, Category = "Interactable")
57 void SetHighlightText(FText NewHighlightText);
58 UFUNCTION(BlueprintCallable, Category = "Interactable")
59 void SetHighlightable(bool bHighlightable);
60 UFUNCTION(BlueprintCallable, Category = "Interactable")
61 void SetInteractionType(EInteractionType NewInteractionType);
62
63 virtual void Highlight(APlayerController* Sender, UPrimitiveComponent* Component);
64 virtual void ExitHighlight(APlayerController* Sender, UPrimitiveComponent* Component);
65
66 UFUNCTION(BlueprintPure, Category = "Interactable")
67 bool CanHighlight(const UPrimitiveComponent* Component) const;
68 UFUNCTION(BlueprintPure, Category = "Interactable")
69 FText GetHighlightText(const UPrimitiveComponent* Component, const APlayerController* EventSender = NULL) const;
70 UFUNCTION(BlueprintPure, Category = "Interactable")
71 FORCEINLINE bool RequiresHoldInteraction() const { return InteractionType == EInteractionType::Hold; }
72 UFUNCTION(BlueprintPure, Category = "Interactable")
73 FORCEINLINE bool HasHoldInteraction() const { return InteractionType == EInteractionType::Hold || InteractionType == EInteractionType::Both; }
74 UFUNCTION(BlueprintPure, Category = "Interactable")
75 FORCEINLINE float GetHoldTime() const { return HoldDuration; }
76 UFUNCTION(BlueprintPure, Category = "Interactable")
77 FORCEINLINE bool CanInteract() const { return bIsInteractable; }
78 void UpdateIsInteractable(bool bCanInteract) { bIsInteractable = bCanInteract; }
79
80
81public:
82 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
83 FActorHighlightSignature OnHighlight;
84 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
85 FActorHighlightExitSignature OnHighlightExit;
86 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
87 FActorSelectedSignature OnSelected;
88 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
89 FActorHoldSelectedSignature OnHoldSelected;
90 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
91 FActorHoldSelectedSignature OnHoldStarted;
92 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
93 FActorHoldSelectedSignature OnHoldExited;
94 UPROPERTY(BlueprintAssignable, Category = "Interactable Events")
95 FActorValidInteractionSignature OnValidInteractionUpdatedEvent;
96
97protected:
98 /* Can this interactable be highlighted? */
99 UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, Category = "Highlight Settings", meta = (AllowPrivateAccess = true))
100 bool bIsHighlightable = true;
101 /* Hover text to show when this interactable is highlighted */
102 UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, meta = (MultiLine = "true", EditCondition = "bIsHighlightable", AllowPrivateAccess = true), Category = "Highlight Settings")
103 FText HighlightText = NSLOCTEXT("ProjectX", "cmd_interact", "Interact");
104 /* Does the player need to hold the button to interact? */
105 UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Interactable", meta = (AllowPrivateAccess = true))
107 /* How long the player must hold to pick up items */
108 UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Interactable", meta = (EditCondition = "bAllowHoldInteraction", AllowPrivateAccess = true))
109 float HoldDuration = 1.f;
110
111 bool bIsInteractable = true;
112 /* If you want highlight text to change based on which component is highlighted, define overlays here */
113 UPROPERTY(BlueprintReadWrite, EditDefaultsOnly, meta = (MultiLine = "true", AllowPrivateAccess = true), Category = "Highlight Settings")
114 TMap<UPrimitiveComponent*, FHighlightableComponent> PerComponentHighlightText;
115
116 class UMeshComponent* Mesh;
117};
EInteractionType
Definition: InteractableComponent.h:18
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FActorHighlightSignature, APlayerController *, Sender, UPrimitiveComponent *, Component)
Definition: ProjectX.Build.cs:6
Definition: InteractableComponent.h:26