A Demo Project for the UnrealEngineSDK
Loading...
Searching...
No Matches
ButtonWidget.cpp
Go to the documentation of this file.
1// Copyright(c) Pixo Group. All Rights Reserved.
2
3#include "UI/ButtonWidget.h"
4
5#include "Blueprint/WidgetTree.h"
6#include "Components/TextBlock.h"
7#include "GameFramework/GameModeBase.h"
9#include "Kismet/GameplayStatics.h"
12
13#if WITH_EDITOR
14void UButtonWidget::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent)
15{
16 Super::PostEditChangeChainProperty(PropertyChangedEvent);
17
19}
20#endif // WITH_EDITOR
21
23{
24 Super::NativeOnInitialized();
25
26 bCurrentState = false;
27
28 ensure(ButtonHighlight);
29 ensure(ButtonBounds);
30 ensure(Button);
31 ensure(BtnName);
32
34
35 AGameModeBase* GameModeBase = GetWorld()->GetAuthGameMode();
36 if (GameModeBase->GetClass()->ImplementsInterface(UPixoCoreInterface::StaticClass()))
37 {
38 HintManager = Cast<UHintManager>(
39 IPixoCoreInterface::Execute_GetManagerRef(GameModeBase, UHintManager::StaticClass()));
40 if (HintManager)
41 {
43 }
44
45 StoryManager = Cast<UStoryManager>(
46 IPixoCoreInterface::Execute_GetManagerRef(GameModeBase, UStoryManager::StaticClass()));
47 if (StoryManager)
48 {
52
54 }
55 }
56 else
57 {
58 UE_LOG(LogTemp, Error, TEXT("GameModeBase doesn't implement IPixoCoreInterface"));
59 }
60
61 Button->OnReleased.AddDynamic(this, &UButtonWidget::ButtonReleased);
62 Button->OnHovered.AddDynamic(this, &UButtonWidget::ButtonHovered);
63 Button->OnUnhovered.AddDynamic(this, &UButtonWidget::OnUnHovered);
64 Button->OnPressed.AddDynamic(this, &UButtonWidget::OnPressed);
65
67}
68
70{
71 Super::NativePreConstruct();
72
74}
75
77{
78 this->ButtonName = Name;
79 BtnName->SetText(ButtonName);
80}
81
83{
84 return this->ButtonName;
85}
86
87void UButtonWidget::ChangeButtonState(const bool bState)
88{
89 bCurrentState = bState;
91}
92
93void UButtonWidget::SwitchButtonColor(const bool bState)
94{
95 Button->SetBackgroundColor(bIsSelectable && bState ? SelectedButtonColor : DefaultButtonColor);
96}
97
99{
100 ButtonHighlight->SetPadding(FMargin(ButtonHighlightWidth));
102
103 ButtonBounds->SetPadding(FMargin(ButtonBoundsWidth));
104 ButtonBounds->SetBrushColor(ButtonBoundsColor);
105
106 BtnName->SetText(ButtonName);
107 BtnName->SetColorAndOpacity(ButtonNameColor);
108
109 FSlateFontInfo FontInfo = BtnName->Font;
110 FontInfo.Size = ButtonNameFontSize;
111 BtnName->SetFont(FontInfo);
112
114}
115
117{
118 ButtonHighlight->BrushColor.A = 1.f;
119 ButtonHighlight->SetBrushColor(ButtonHighlight->BrushColor);
120}
121
123{
125 {
126 PlayAnimation(ButtonHighlightAnimation, 0.f, 0, EUMGSequencePlayMode::Forward, HighlightAnimationSpeed);
127 }
128}
129
131{
132 if (ButtonHighlightAnimation && IsPlayingAnimation())
133 {
134 StopAnimation(ButtonHighlightAnimation);
135 }
136
137 ButtonHighlight->BrushColor.A = 0.f;
138 ButtonHighlight->SetBrushColor(ButtonHighlight->BrushColor);
139}
140
141void UButtonWidget::OnHintActivated_Implementation(bool Activate)
142{
143 if (GEngine)
144 {
145 if (Activate)
146 {
148 }
149 else
150 {
152 }
153 }
154
155 if (Activate)
156 {
157 for (auto Item : HighlightActiveSteps)
158 {
159 if (Item.EqualTo(ActiveStep))
160 {
162 break;
163 }
164 }
165 }
166 else
167 {
169 }
170}
171
173{
176
178 OnClickEventPtrDelegate.Broadcast(ButtonName, GetUniqueID());
179 OnClickEventSelfPtrDelegate.Broadcast(this);
180
181 UWorld* ThisWorld = GetWorld();
182 if (SelectSound && ThisWorld && ThisWorld->bAllowAudioPlayback && !ThisWorld->IsNetMode(NM_DedicatedServer))
183 {
184 UGameplayStatics::PlaySound2D(ThisWorld, SelectSound);
185 }
186}
187
189{
191
192 UWorld* ThisWorld = GetWorld();
193 if (HoverSound && ThisWorld && ThisWorld->bAllowAudioPlayback && !ThisWorld->IsNetMode(NM_DedicatedServer))
194 {
195 UGameplayStatics::PlaySound2D(ThisWorld, HoverSound);
196 }
197}
198
203
205{
206 Button->SetBackgroundColor(PressedButtonColor);
207}
208
209void UButtonWidget::NewStep(FStoryStep NewStep, bool IsForward)
210{
212 {
213 return;
214 }
215
218
219 bool StepFound = false;
220 bool SubStepFound = false;
221 for (auto Step : HighlightActiveSteps)
222 {
223 if (ActiveStep.EqualTo(Step))
224 {
225 StepFound = true;
226 break;
227 }
228 }
229
230 for (auto SubStep : HighlightActiveSubSteps)
231 {
232 if (ActiveSubStep.EqualTo(SubStep))
233 {
234 SubStepFound = true;
235 break;
236 }
237 }
238
239 if (StepFound && (SubStepFound || HighlightActiveSubSteps.Num() == 0 || ActiveSubStep.IsEmpty()))
240 {
241 bWidgetActiveOnStep = true;
243 }
244 else
245 {
247 {
248 bWidgetActiveOnStep = false;
250 }
251 }
252}
253
255{
257 {
258 return;
259 }
260
262 ActiveSubStep = NewSubStep.StepName;
263
264 bool StepFound = false;
265 bool SubStepFound = false;
266
267 for (auto Step : HighlightActiveSteps)
268 {
269 if (ActiveStep.EqualTo(Step))
270 {
271 StepFound = true;
272 break;
273 }
274 }
275
276 for (auto SubStep : HighlightActiveSubSteps)
277 {
278 if (ActiveSubStep.EqualTo(SubStep))
279 {
280 SubStepFound = true;
281 break;
282 }
283 }
284
285 if (StepFound && (SubStepFound || HighlightActiveSubSteps.Num() == 0))
286 {
287 bWidgetActiveOnStep = true;
289 }
290 else
291 {
293 {
294 bWidgetActiveOnStep = false;
296 }
297 }
298}
299
UHintManager * HintManager
UPROPERTY(BlueprintReadWrite)
bool bWidgetActiveOnStep
UPROPERTY(BlueprintReadWrite)
void ActivateButtonPulsationHighlight()
virtual void ChangeButtonState(const bool bState)
Changes the state of the button .
float HighlightAnimationSpeed
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
void NewStep(FStoryStep NewStep, bool IsForward)
UFUNCTION()
FText ActiveStep
UPROPERTY(BlueprintReadWrite)
bool bIsPulsationHighlight
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
FLinearColor ButtonNameColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UBorder * ButtonHighlight
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
FLinearColor SelectedHoveredButtonColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void ButtonHovered()
UFUNCTION()
bool bIsSelectable
UPROPERTY(EditAnywhere, BlueprintReadWrite)
float ButtonHighlightWidth
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
virtual void NativePreConstruct() override
float ButtonBoundsWidth
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UStoryManager * StoryManager
UPROPERTY(BlueprintReadWrite)
FClickEventPtrDelegate OnClickEventPtrDelegate
UPROPERTY(BlueprintAssignable, BlueprintCallable, Category = "Delegate")
FLinearColor ButtonBoundsColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void StoryLoaded()
UFUNCTION()
FLinearColor DefaultButtonColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
virtual void NativeOnInitialized() override
TArray< FText > HighlightActiveSteps
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
void SetButtonName(const FText &Name)
UFUNCTION(BlueprintCallable)
TArray< FText > HighlightActiveSubSteps
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
bool ActivateHighlightOnEachActiveStep
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
FClickEventDelegate OnClickEventDelegate
UPROPERTY(BlueprintAssignable, BlueprintCallable, Category = "Delegate")
FLinearColor DefaultHoveredButtonColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void NewSubStep(FStoryStep NewSubStep)
UFUNCTION()
UWidgetAnimation * ButtonHighlightAnimation
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
void ButtonReleased()
UFUNCTION()
void OnPressed()
UFUNCTION()
USoundBase * HoverSound
UPROPERTY(EditAnywhere, BlueprintReadWrite)
FText ActiveSubStep
UPROPERTY(BlueprintReadWrite)
FLinearColor PressedButtonColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void ActivateButtonHighlight()
FClickEventSelfPtrDelegate OnClickEventSelfPtrDelegate
UPROPERTY(BlueprintAssignable, BlueprintCallable, Category = "Delegate")
UBorder * ButtonBounds
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
void OnHintActivated(bool Activate)
UFUNCTION(BlueprintNativeEvent, BlueprintCallable)
const FText & GetButtonName()
UFUNCTION(BlueprintCallable)
USoundBase * SelectSound
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UTextBlock * BtnName
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
float ButtonNameFontSize
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void DeactivateButtonHighlight()
void OnUnHovered()
UFUNCTION()
FLinearColor ButtonHighlightColor
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Highlight")
FText ButtonName
UPROPERTY(EditAnywhere, BlueprintReadWrite)
UButton * Button
UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
FLinearColor SelectedButtonColor
UPROPERTY(EditAnywhere, BlueprintReadWrite)
void SwitchButtonColor(const bool bState)
Switches the button color based on the state.
void RefreshWidgetStyle()
Refreshes the visual style of the button widget.
FOnHintActivated OnHintActivated
Event delegate for hint activation.
Definition HintManager.h:45
FOnNewStep OnNewStep
UPROPERTY(BlueprintAssignable, Category = "Story Delegate")
FStoryStep CurrentSubStep
UPROPERTY(BlueprintReadWrite, Replicated)
FOnStoryLoaded OnStoryLoaded
UPROPERTY(BlueprintAssignable, Category = "Story Delegate")
FOnNewSubStep OnNewSubStep
UPROPERTY(BlueprintAssignable, Category = "Story Delegate")
FStoryStep CurrentStep
UPROPERTY(BlueprintReadWrite, Replicated)
USTRUCT(BlueprintType)
FText StepName
UPROPERTY(BlueprintReadWrite, EditAnywhere)