Advertisement
Guest User

SwitcherWidget (UE5)

a guest
Dec 26th, 2022
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | Source Code | 0 0
  1.  
  2. .h
  3.  
  4. #include "CoreMinimal.h"
  5. #include "Blueprint/UserWidget.h"
  6. #include "UW_SwitcherPanel.generated.h"
  7.  
  8. class UCanvasPanel;
  9. class UWidgetSwitcher;
  10. class UVerticalBox;
  11. class UHorizontalBox;
  12. class UButton;
  13. class UW_NavigationPanel;
  14. class UW_SwitcherPanel;
  15.  
  16. /**
  17.  *
  18.  */
  19. UCLASS()
  20. class SPORT_API UUW_SwitcherPanel : public UUserWidget
  21. {
  22.     GENERATED_BODY()
  23.  
  24. public:
  25.  
  26.     /* Constructor */
  27.  
  28.    
  29.  
  30. protected:
  31.    
  32.     virtual void NativeConstruct() override;
  33.  
  34.     /* Design Layout of the right hand menu panel (in game) for users to interact with  */
  35.     UPROPERTY(meta = (BindWidget))
  36.     TObjectPtr<UCanvasPanel> Switcher_CanvasPanel;
  37.  
  38.     UPROPERTY(meta = (BindWidget))
  39.     TObjectPtr<UWidgetSwitcher> Switcher_WidgetSwitcher;
  40.  
  41.     UPROPERTY(meta = (BindWidget))
  42.     TObjectPtr<UVerticalBox> Switcher_VerticalBox;
  43.  
  44.     UPROPERTY(meta = (BindWidget))
  45.     TObjectPtr<UHorizontalBox> Switcher_HorizontalBox1;
  46.  
  47.     UPROPERTY(meta = (BindWidget))
  48.     TObjectPtr<UHorizontalBox> Switcher_HorizontalBox2;
  49.  
  50.     UPROPERTY(meta = (BindWidget))
  51.     TObjectPtr<UButton> BtnNavigation;
  52.  
  53.     UPROPERTY(meta = (BindWidget))
  54.     TObjectPtr<UButton> BtnCrew;
  55.  
  56.     UPROPERTY(meta = (BindWidget))
  57.     TObjectPtr<UButton> BtnShipDetails;
  58.  
  59.  
  60. private:
  61.  
  62.     int32 SwitcherIndex = 0;
  63.  
  64.     /* Button Navigation */
  65.     UFUNCTION()
  66.     void BtnNavigationPressed();
  67.  
  68.     /* Button Crew */
  69.     UFUNCTION()
  70.     void BtnCrewPressed();
  71.  
  72.     /* Button Ship Details */
  73.     UFUNCTION()
  74.     void BtnShipDetailsPressed();
  75.  
  76.     /* Update the switcher panel */
  77.     void UpdateSwitcherPanel(int32 Index);
  78. };
  79.  
  80. ----------------------------------------------------------------------------------------------------
  81.  
  82. .cpp
  83.  
  84. #include "UI/UW_SwitcherPanel.h"
  85. #include "Components/CanvasPanel.h"
  86. #include "Components/WidgetSwitcher.h"
  87. #include "Components/VerticalBox.h"
  88. #include "Components/HorizontalBox.h"
  89. #include "Components/Button.h"
  90. #include "UI/UW_NavigationPanel.h"
  91.  
  92. //Native Constructor
  93. void UUW_SwitcherPanel::NativeConstruct()
  94. {
  95.     BtnNavigation->OnClicked.AddDynamic(this, &UUW_SwitcherPanel::BtnNavigationPressed);
  96.     BtnCrew->OnClicked.AddDynamic(this, &UUW_SwitcherPanel::BtnCrewPressed);
  97.     BtnShipDetails->OnClicked.AddDynamic(this, &UUW_SwitcherPanel::BtnShipDetailsPressed);
  98. }
  99.  
  100.  
  101. //Navigation Button
  102. void UUW_SwitcherPanel::BtnNavigationPressed()
  103. {
  104.     //check isCurrentWidget?
  105.     SwitcherIndex = 0;
  106.     UpdateSwitcherPanel(SwitcherIndex);
  107. }
  108.  
  109. //Crew Button
  110. void UUW_SwitcherPanel::BtnCrewPressed()
  111. {
  112.     SwitcherIndex = 1;
  113.     UpdateSwitcherPanel(SwitcherIndex);
  114. }
  115.  
  116. //Ship Details
  117. void UUW_SwitcherPanel::BtnShipDetailsPressed()
  118. {
  119.     SwitcherIndex = 2;
  120.     UpdateSwitcherPanel(SwitcherIndex);
  121. }
  122.  
  123. //Update Switcher Panel
  124. void UUW_SwitcherPanel::UpdateSwitcherPanel(int32 Index)
  125. {
  126.     UE_LOG(LogTemp, Warning, TEXT("UpdateSwitcherPanel Index %d"), Index);
  127.  
  128.     if (Switcher_WidgetSwitcher != nullptr)
  129.     {
  130.         int32 CurrentIndexPoint;
  131.         CurrentIndexPoint = Switcher_WidgetSwitcher->GetActiveWidgetIndex();
  132.         if (CurrentIndexPoint != Index)
  133.         {
  134.             Switcher_WidgetSwitcher->SetActiveWidgetIndex(Index);
  135.         }      
  136.     }
  137.     else
  138.     {
  139.         UE_LOG(LogTemp, Warning, TEXT("Not instantiated"));
  140.     }    
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement