Documentation for the Unreal C++ Plugin
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"
10
11#if WITH_EDITOR
12void UButtonWidget::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChangedEvent)
13{
14 Super::PostEditChangeChainProperty(PropertyChangedEvent);
15
17}
18#endif // WITH_EDITOR
19
21{
22 Super::NativeOnInitialized();
23
24 bCurrentState = false;
25
26 ensure(ButtonHighlight);
27 ensure(ButtonBounds);
28 ensure(Button);
29 ensure(BtnName);
30
32
33 AGameModeBase* GameModeBase = GetWorld()->GetAuthGameMode();
34 if (GameModeBase->GetClass()->ImplementsInterface(UPixoCoreInterface::StaticClass()))
35 {
36 HintManager = Cast<UHintManager>(
37 IPixoCoreInterface::Execute_GetManagerRef(GameModeBase, UHintManager::StaticClass()));
38 if (HintManager)
39 {
41 }
42
43 StoryManager = Cast<UStoryManager>(
44 IPixoCoreInterface::Execute_GetManagerRef(GameModeBase, UStoryManager::StaticClass()));
45 if (StoryManager)
46 {
50
52 }
53 }
54 else
55 {
56 UE_LOG(LogTemp, Error, TEXT("GameModeBase doesn't implement IPixoCoreInterface"));
57 }
58
59 Button->OnReleased.AddDynamic(this, &UButtonWidget::ButtonReleased);
60 Button->OnHovered.AddDynamic(this, &UButtonWidget::ButtonHovered);
61 Button->OnUnhovered.AddDynamic(this, &UButtonWidget::OnUnHovered);
62 Button->OnPressed.AddDynamic(this, &UButtonWidget::OnPressed);
63
65}
66
68{
69 Super::NativePreConstruct();
70
72}
73
74void UButtonWidget::SetButtonName(const FText& Name)
75{
76 this->ButtonName = Name;
77 BtnName->SetText(ButtonName);
78}
79
81{
82 return this->ButtonName;
83}
84
85void UButtonWidget::ChangeButtonState(const bool bState)
86{
87 bCurrentState = bState;
89}
90
91void UButtonWidget::SwitchButtonColor(const bool bState)
92{
93 Button->SetBackgroundColor(bIsSelectable && bState ? SelectedButtonColor : DefaultButtonColor);
94}
95
97{
98 ButtonHighlight->SetPadding(FMargin(ButtonHighlightWidth));
100
101 ButtonBounds->SetPadding(FMargin(ButtonBoundsWidth));
102 ButtonBounds->SetBrushColor(ButtonBoundsColor);
103
104 BtnName->SetText(ButtonName);
105 BtnName->SetColorAndOpacity(ButtonNameColor);
106
107 FSlateFontInfo FontInfo = BtnName->Font;
108 FontInfo.Size = ButtonNameFontSize;
109 BtnName->SetFont(FontInfo);
110
112}
113
115{
116 ButtonHighlight->BrushColor.A = 1.f;
117 ButtonHighlight->SetBrushColor(ButtonHighlight->BrushColor);
118}
119
121{
123 {
124 PlayAnimation(ButtonHighlightAnimation, 0.f, 0, EUMGSequencePlayMode::Forward, HighlightAnimationSpeed);
125 }
126}
127
129{
130 if (ButtonHighlightAnimation && IsPlayingAnimation())
131 {
132 StopAnimation(ButtonHighlightAnimation);
133 }
134
135 ButtonHighlight->BrushColor.A = 0.f;
136 ButtonHighlight->SetBrushColor(ButtonHighlight->BrushColor);
137}
138
139void UButtonWidget::OnHintActivated_Implementation(bool Activate)
140{
141 if (GEngine)
142 {
143 if (Activate)
144 {
146 }
147 else
148 {
150 }
151 }
152
153 if (Activate)
154 {
155 for (auto Item : HighlightActiveSteps)
156 {
157 if (Item.EqualTo(ActiveStep))
158 {
160 break;
161 }
162 }
163 }
164 else
165 {
167 }
168}
169
171{
174
176 OnClickEventPtrDelegate.Broadcast(ButtonName, GetUniqueID());
177 OnClickEventSelfPtrDelegate.Broadcast(this);
178
179 UWorld* ThisWorld = GetWorld();
180 if (SelectSound && ThisWorld && ThisWorld->bAllowAudioPlayback && !ThisWorld->IsNetMode(NM_DedicatedServer))
181 {
182 UGameplayStatics::PlaySound2D(ThisWorld, SelectSound);
183 }
184}
185
187{
189
190 UWorld* ThisWorld = GetWorld();
191 if (HoverSound && ThisWorld && ThisWorld->bAllowAudioPlayback && !ThisWorld->IsNetMode(NM_DedicatedServer))
192 {
193 UGameplayStatics::PlaySound2D(ThisWorld, HoverSound);
194 }
195}
196
201
203{
204 Button->SetBackgroundColor(PressedButtonColor);
205}
206
207void UButtonWidget::NewStep(FStoryStep NewStep, bool IsForward)
208{
210 {
211 return;
212 }
213
216
217 bool StepFound = false;
218 bool SubStepFound = false;
219 for (auto Step : HighlightActiveSteps)
220 {
221 if (ActiveStep.EqualTo(Step))
222 {
223 StepFound = true;
224 break;
225 }
226 }
227
228 for (auto SubStep : HighlightActiveSubSteps)
229 {
230 if (ActiveSubStep.EqualTo(SubStep))
231 {
232 SubStepFound = true;
233 break;
234 }
235 }
236
237 if (StepFound && (SubStepFound || HighlightActiveSubSteps.Num() == 0 || ActiveSubStep.IsEmpty()))
238 {
239 bWidgetActiveOnStep = true;
241 }
242 else
243 {
245 {
246 bWidgetActiveOnStep = false;
248 }
249 }
250}
251
253{
255 {
256 return;
257 }
258
260 ActiveSubStep = NewSubStep.StepName;
261
262 bool StepFound = false;
263 bool SubStepFound = false;
264
265 for (auto Step : HighlightActiveSteps)
266 {
267 if (ActiveStep.EqualTo(Step))
268 {
269 StepFound = true;
270 break;
271 }
272 }
273
274 for (auto SubStep : HighlightActiveSubSteps)
275 {
276 if (ActiveSubStep.EqualTo(SubStep))
277 {
278 SubStepFound = true;
279 break;
280 }
281 }
282
283 if (StepFound && (SubStepFound || HighlightActiveSubSteps.Num() == 0))
284 {
285 bWidgetActiveOnStep = true;
287 }
288 else
289 {
291 {
292 bWidgetActiveOnStep = false;
294 }
295 }
296}
297
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)
ButtonType Type
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)