NebulaGames

C++ to Blueprints Floating Fill Bar

Mar 29th, 2019
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 22.89 KB | None | 0 0
  1. // Fill out your copyright notice in the Description page of Project Settings.
  2.  
  3. #pragma once
  4.  
  5. #include "CoreMinimal.h"
  6. #include "Components/ActorComponent.h"
  7. #include "Runtime/Engine/Classes/GameFramework/Actor.h"
  8. #include "Runtime/UMG/Public/Components/ProgressBar.h"
  9. #include "Runtime/Engine/Public/TimerManager.h"
  10. #include "Runtime/UMG/Public/Blueprint/UserWidget.h"
  11. #include "Floating_Fill_Bar.generated.h"
  12.  
  13.  
  14. // Enum of Times
  15. UENUM(BlueprintType)
  16. enum class ETime : uint8
  17. {
  18.     Seconds, Minutes, Hours, Days, Months, Years
  19. };
  20.  
  21. // Input struct for fill duration
  22. USTRUCT(BlueprintType)
  23. struct FCustom_Signals
  24. {
  25.     GENERATED_USTRUCT_BODY()
  26.  
  27. public:
  28.  
  29.     // How often we will call this event
  30.     UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ClampMin = "0", UIMin = "0"))
  31.         int32 Cycle_Length;
  32.     // Enum selection of time denominations
  33.     UPROPERTY(EditAnywhere, BlueprintReadWrite, meta = (ClampMin = "0", UIMin = "0"))
  34.         ETime Time_Category;
  35. };
  36.  
  37. // Input struct for clock suffixes
  38. USTRUCT(BlueprintType)
  39. struct FClock_Suffix
  40. {
  41.     GENERATED_USTRUCT_BODY()
  42.  
  43. public:
  44.  
  45.     // The displayed suffix for the "year" length of time
  46.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  47.         FString Year_Suffix;
  48.     // The displayed suffix for the "month" length of time
  49.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  50.         FString Month_Suffix;
  51.     // The displayed suffix for the "day" length of time
  52.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  53.         FString Day_Suffix;
  54.     // The displayed suffix for the "hour" length of time
  55.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  56.         FString Hour_Suffix;
  57.     // The displayed suffix for the "minute" length of time
  58.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  59.         FString Minute_Suffix;
  60.     // The displayed suffix for the "second" length of time
  61.     UPROPERTY(EditAnywhere, BlueprintReadWrite)
  62.         FString Second_Suffix;
  63. };
  64.  
  65. // Input struct containing all data necessary to update the given progress bar
  66. USTRUCT(BlueprintType)
  67. struct FWait_Time
  68. {
  69.     GENERATED_USTRUCT_BODY()
  70.  
  71. public:
  72.  
  73.     // The fill percent between 0 and 100 to start out progress bar at
  74.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "100", UIMin = "0", UIMax = "100"))
  75.         int32 Start_Percent;
  76.     // The number of seconds on the clock
  77.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  78.         int32 Seconds;
  79.     // The number of minutes on the clock
  80.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "59", UIMin = "0", UIMax = "59"))
  81.         int32 Minutes;
  82.     // The number of hours on the clock
  83.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "23", UIMin = "0", UIMax = "23"))
  84.         int32 Hours;
  85.     // The number of days on the clock
  86.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "31", UIMin = "0", UIMax = "31"))
  87.         int32 Days;
  88.     // The number of months on the clock
  89.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", ClampMax = "12", UIMin = "0", UIMax = "12"))
  90.         int32 Months;
  91.     // The number of years on the clock
  92.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time", meta = (ClampMin = "0", UIMin = "0"))
  93.         int32 Years;
  94.     // The name of the progress bar variable inside the widget
  95.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time")
  96.         FName Progress_Bar_Name;
  97.     // Input struct of all time denomination suffixes
  98.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time")
  99.         FClock_Suffix Custom_Suffix;
  100.     // Input array of all custom blueprint events to fire off with their cycle lengths
  101.     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Wait Time")
  102.         TArray<FCustom_Signals> Signals;
  103.     // Save the Signal Counter value in case we load from a save file we will still fire off events at the given cycle instead of starting over
  104.         int32 SaveCount;
  105. };
  106.  
  107. UCLASS( ClassGroup=(Custom), Blueprintable, meta=(BlueprintSpawnableComponent) )
  108. class CODE_TEST_API UFloating_Fill_Bar : public UActorComponent
  109. {
  110.     GENERATED_BODY()
  111.  
  112. public:
  113.  
  114.     // Sets default values for this component's properties
  115.     UFloating_Fill_Bar();
  116.  
  117.     // Called every frame
  118.     virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
  119.  
  120.     // Read the value representing the remaining time on the clock
  121.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  122.         FString Clock;
  123.     // Read the seconds left on the clock
  124.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  125.         int32 Seconds_Left;
  126.     // Read the minutes left on the clock
  127.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  128.         int32 Minutes_Left;
  129.     // Read the hours left on the clock
  130.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  131.         int32 Hours_Left;
  132.     // Read the days left on the clock
  133.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  134.         int32 Days_Left;
  135.     // Read the months left on the clock
  136.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  137.         int32 Months_Left;
  138.     // Read the years left on the clock
  139.     UPROPERTY(BlueprintReadOnly, Category = "Floating Fill Bar")
  140.         int32 Years_Left;
  141.  
  142.     // Function to modify an input widget's progress bar by name
  143.     UFUNCTION(BlueprintCallable)
  144.         void UpdateBar(FWait_Time Duration, float GameSeconds_Per_Realtime, bool Fill, bool Hide_When_Complete, UUserWidget* Floating_Widget, bool Custom_Signaling);
  145.     // Function to change the rate of time passage
  146.     UFUNCTION(BlueprintCallable)
  147.         void Alter_Rate_Of_Time_Passage(float New_Speed);
  148.     // Function to pause our timer
  149.     UFUNCTION(BlueprintCallable)
  150.         void Pause();
  151.     // Function to unpause our timer
  152.     UFUNCTION(BlueprintCallable)
  153.         void Unpause();
  154.     // Function to clear our timer
  155.     UFUNCTION(BlueprintCallable)
  156.         void ClearTimer();
  157.     // Function to modify the remaining time on the clock and/or speed changes to other variables will not be carried through and can be left blank
  158.     UFUNCTION(BlueprintCallable)
  159.         void Modify_Clock(FWait_Time Modify_Time, float Modify_Speed);
  160.     // Function to output a single struct with the last known remaining time, can be directly fed back into a call to "Update Bar"
  161.     UFUNCTION(BlueprintCallable)
  162.         void SaveClock(FWait_Time &Save_Time);
  163.  
  164.     // Function called when the clock has finished counting down
  165.     UFUNCTION(BlueprintImplementableEvent)
  166.         void FillBar_Completed();
  167.     // Custom event to fire off at the given interval
  168.     UFUNCTION(BlueprintImplementableEvent)
  169.         void Custom_Signal_1();
  170.     // Custom event to fire off at the given interval
  171.     UFUNCTION(BlueprintImplementableEvent)
  172.         void Custom_Signal_2();
  173.     // Custom event to fire off at the given interval
  174.     UFUNCTION(BlueprintImplementableEvent)
  175.         void Custom_Signal_3();
  176.     // Custom event to fire off at the given interval
  177.     UFUNCTION(BlueprintImplementableEvent)
  178.         void Custom_Signal_4();
  179.     // Custom event to fire off at the given interval
  180.     UFUNCTION(BlueprintImplementableEvent)
  181.         void Custom_Signal_5();
  182.  
  183.     // Global variables for internal use only
  184.     bool IsFilling;
  185.     bool Error;
  186.     bool UseCustomSignals;
  187.     bool Hide;
  188.    
  189.     int32 SecondsResetValue;
  190.     int32 MinutesResetValue;
  191.     int32 HoursResetValue;
  192.     int32 DaysResetValue;
  193.     int32 MonthsResetValue;
  194.     int32 DefaultReset;
  195.     int32 SignalCounter;
  196.  
  197.     float TotalWaitTime;
  198.     float DeltaPercent;
  199.     float CurrentPercent;
  200.  
  201.     FString SecondsOutput;
  202.     FString MinutesOutput;
  203.     FString HoursOutput;
  204.     FString DaysOutput;
  205.     FString MonthsOutput;
  206.     FString YearsOutput;
  207.     FString InputWarning;
  208.  
  209.     FWait_Time InternalWaitTime;
  210.     FTimerHandle FillBarHandle;
  211.     UUserWidget* InternalWidget;
  212.     UProgressBar* InternalProgressBar;
  213.     AActor* Parent_Actor;
  214.  
  215.     // Internal use functions
  216.     void CountTime();
  217.     void Fill_Unfill();
  218.     void GenerateClock();
  219.     void Check_Custom_Signals();
  220.     void Revert_Signal_Cycle();
  221.  
  222. protected:
  223.  
  224.     // Called when the game starts
  225.     virtual void BeginPlay() override; 
  226. };
  227.  
  228. -------------------------------------------------------------------------------------------------
  229.  
  230. // Fill out your copyright notice in the Description page of Project Settings.
  231.  
  232. #include "Floating_Fill_Bar.h"
  233.  
  234. // Sets default values for this component's properties
  235. UFloating_Fill_Bar::UFloating_Fill_Bar()
  236. {
  237.     // Disable tick
  238.     PrimaryComponentTick.bCanEverTick = false;
  239.  
  240.     // Retrieve actor component's Parent Actor
  241.     Parent_Actor = GetOwner();
  242.  
  243.     // Assume a proper time was entered
  244.     Error = false;
  245.  
  246.     // Many variables need to be reset at "-1", we shall declare this as an easy to read variable
  247.     DefaultReset = -1;
  248.  
  249.     // Set the clock's reset values
  250.     SecondsResetValue = 59;
  251.     MinutesResetValue = 59;
  252.     HoursResetValue = 23;
  253.     DaysResetValue = 29;
  254.     MonthsResetValue = 11;
  255.  
  256.     // Always start our signal counter at 0
  257.     SignalCounter = 0;
  258.  
  259.     // Assume we should hide when complete
  260.     Hide = true;
  261. }
  262.  
  263. // Called when the game starts
  264. void UFloating_Fill_Bar::BeginPlay()
  265. {
  266.     Super::BeginPlay();
  267.  
  268.     // ...
  269.    
  270. }
  271.  
  272. // Called every frame
  273. void UFloating_Fill_Bar::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
  274. {
  275.     Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
  276.  
  277.     // ...
  278. }
  279.  
  280. void UFloating_Fill_Bar::UpdateBar(FWait_Time Duration, float GameSeconds_Per_Realtime, bool Fill, bool Hide_When_Complete, UUserWidget* Floating_Widget, bool Custom_Signaling)
  281. {
  282.     // Copy our input struct for use in other functions
  283.     InternalWaitTime = Duration;
  284.  
  285.     // Determine if we are filling or emptying the progress bar
  286.     IsFilling = Fill;
  287.  
  288.     // Should we hide the fill bar once complete?
  289.     Hide = Hide_When_Complete;
  290.  
  291.     // Determine if we have custom signals to handle
  292.     UseCustomSignals = Custom_Signaling;
  293.  
  294.     // Set the current percent and wait time to desired values
  295.     CurrentPercent = float(InternalWaitTime.Start_Percent) / 100;
  296.     Seconds_Left = InternalWaitTime.Seconds;
  297.     Minutes_Left = InternalWaitTime.Minutes;
  298.     Hours_Left = InternalWaitTime.Hours;
  299.     Days_Left = InternalWaitTime.Days;
  300.     Months_Left = InternalWaitTime.Months;
  301.     Years_Left = InternalWaitTime.Years;
  302.  
  303.     // If this is saved data our Signal Count should be set to the last value it had, otherwise it will remain at the default value of 0
  304.     if (InternalWaitTime.SaveCount > 0)
  305.     {
  306.         SignalCounter = InternalWaitTime.SaveCount;
  307.     }
  308.  
  309.     // Set our global widget variable to the widget reference supplied by the user
  310.     InternalWidget = Floating_Widget;
  311.  
  312.     // Set our progress bar pointer
  313.     InternalProgressBar = dynamic_cast<UProgressBar*>(InternalWidget->GetWidgetFromName(FName(InternalWaitTime.Progress_Bar_Name)));
  314.  
  315.     // If we are using the custom signal feature calculate the cycle length in seconds
  316.     if (UseCustomSignals == true && InternalWaitTime.Signals.Num() > 0)
  317.     {
  318.         // Local variables for making time conversions
  319.         int32 ConversionRate = 1;
  320.  
  321.         // Loop through input array and convert all signal durations into seconds
  322.         for (int x = 0; x < InternalWaitTime.Signals.Num(); ++x)
  323.         {
  324.             if (InternalWaitTime.Signals[x].Cycle_Length > 0)
  325.             {
  326.                 switch (InternalWaitTime.Signals[x].Time_Category)
  327.                 {
  328.                 case ETime::Seconds:
  329.                     ConversionRate = 1;
  330.                     break;
  331.                 case ETime::Minutes:
  332.                     ConversionRate = 60;
  333.                     break;
  334.                 case ETime::Hours:
  335.                     ConversionRate = 3600;
  336.                     break;
  337.                 case ETime::Days:
  338.                     ConversionRate = 86400;
  339.                     break;
  340.                 case ETime::Months:
  341.                     ConversionRate = 2592000;
  342.                     break;
  343.                 case ETime::Years:
  344.                     ConversionRate = 31536000;
  345.                     break;
  346.                 default:
  347.                     break;
  348.                 }
  349.                 InternalWaitTime.Signals[x].Cycle_Length *= ConversionRate;
  350.             }
  351.         }
  352.     }
  353.     else
  354.     {
  355.         UseCustomSignals = false;
  356.     }
  357.  
  358.     // Set the progress bar to the desired percentage
  359.     InternalProgressBar->SetPercent(CurrentPercent);
  360.  
  361.     // Ensure input percentage is between 0 and 1 and makes sense based on whether we are filling or emptying the bar
  362.     if (CurrentPercent < 0.f && IsFilling == true)
  363.     {
  364.         CurrentPercent = 0.f;
  365.  
  366.         // We will repeat this function since we assume the input is correct and simply out of range ** we are filling an empty bar **
  367.         InternalProgressBar->SetPercent(CurrentPercent);
  368.     }
  369.     else if (CurrentPercent <= 0 && IsFilling == false)
  370.     {
  371.         // We will not attempt to guess the user's intent here, the inputs are contradictory as the warning text states so we will error out
  372.         Error = true;
  373.         InputWarning = "Attempting to drain an empty bar!!";
  374.     }
  375.     else if (CurrentPercent > 1.f && IsFilling == false)
  376.     {
  377.         CurrentPercent = 1.f;
  378.  
  379.         // We will repeat this function since we assume the input is correct and simply out of range ** we are emptying a full bar **
  380.         InternalProgressBar->SetPercent(CurrentPercent);
  381.     }
  382.     else if (CurrentPercent >= 1.f && IsFilling == true)
  383.     {
  384.         // We will not attempt to guess the user's intent here, the inputs are contradictory as the warning text states so we will error out
  385.         Error = true;
  386.         InputWarning = "Attempting to fill a full bar!!";
  387.     }
  388.  
  389.     // Perform initial subtraction to ensure time counts down properly in cases where we start at 0 seconds
  390.     if (Seconds_Left == 0)                         
  391.     {
  392.         if (Minutes_Left > 0)
  393.         {
  394.             Minutes_Left--;
  395.             Seconds_Left = 60;
  396.         }
  397.         else if (Hours_Left > 0)
  398.         {
  399.             Hours_Left--;
  400.             Minutes_Left = MinutesResetValue;
  401.             Seconds_Left = SecondsResetValue;
  402.         }
  403.         else if (Days_Left > 0)
  404.         {
  405.             Days_Left--;
  406.             Hours_Left = HoursResetValue;
  407.             Minutes_Left = MinutesResetValue;
  408.             Seconds_Left = SecondsResetValue;
  409.         }
  410.         else if (Months_Left > 0)
  411.         {
  412.             Months_Left--;
  413.             Days_Left = DaysResetValue;
  414.             Hours_Left = HoursResetValue;
  415.             Minutes_Left = MinutesResetValue;
  416.             Seconds_Left = SecondsResetValue;
  417.         }
  418.         else if (Years_Left > 0)
  419.         {
  420.             Years_Left--;
  421.             Months_Left = MonthsResetValue;
  422.             Days_Left = DaysResetValue;
  423.             Hours_Left = HoursResetValue;
  424.             Minutes_Left = MinutesResetValue;
  425.             Seconds_Left = SecondsResetValue;
  426.         }
  427.         else
  428.         {
  429.             Error = true;
  430.             InputWarning = "Attempting to count down but no time was entered!!";
  431.         }
  432.     }
  433.  
  434.     // If no input errors occured ** a valid time was entered ** we may continue
  435.     if (Error == false)
  436.     {
  437.         // For delta time calculation purposes create a temporary percent in case we are starting at 0% otherwise we will have no "delta" value
  438.         float TempPercent;
  439.         if (CurrentPercent == 0.f || CurrentPercent == 1.f)
  440.         {
  441.             // If we fill from empty or drain from full we must traverse the entire fill bar thus the "percent" is set to 1
  442.             TempPercent = 1.f;
  443.         }
  444.         else
  445.         {
  446.             // Get the remaining percent to fill
  447.             TempPercent = (1 - CurrentPercent);
  448.         }
  449.  
  450.         // Prevent accidental division by "0"
  451.         if (GameSeconds_Per_Realtime == 0.f)
  452.         {
  453.             GameSeconds_Per_Realtime = 1.f;
  454.         }
  455.  
  456.         // Determine how long in seconds it will take to fill the progress bar
  457.         TotalWaitTime = float((InternalWaitTime.Years * 31536000) + (InternalWaitTime.Months * 2592000) + (InternalWaitTime.Days * 86400) + (InternalWaitTime.Hours * 3600) + (InternalWaitTime.Minutes * 60) + (InternalWaitTime.Seconds));
  458.  
  459.         // Determine how much the progress bar should change each cycle ** this is why TempPercent is needed because starting at 0% will give a 0 value here
  460.         DeltaPercent = (TempPercent / TotalWaitTime);
  461.  
  462.         // If we are emptying the progress bar make DeltaPercent negative
  463.         if (IsFilling == false)
  464.         {
  465.             DeltaPercent *= -1;
  466.         }
  467.  
  468.         // Create a timer
  469.         Parent_Actor->GetWorldTimerManager().SetTimer(FillBarHandle, this, &UFloating_Fill_Bar::CountTime, (1 / GameSeconds_Per_Realtime), true);
  470.     }
  471.     else
  472.     {
  473.         // Output an error message where we expect to see the time remaining
  474.         Clock = InputWarning;
  475.     }
  476. }
  477.  
  478. // Function that actually counts time down
  479. void UFloating_Fill_Bar::CountTime()
  480. {
  481.     Seconds_Left--;
  482.  
  483.     if (Seconds_Left == DefaultReset)
  484.     {
  485.         Seconds_Left = SecondsResetValue;
  486.         Minutes_Left--;
  487.  
  488.         if (Minutes_Left == DefaultReset && Hours_Left > 0)
  489.         {
  490.             Minutes_Left = MinutesResetValue;
  491.             Hours_Left--;
  492.         }
  493.         else if (Minutes_Left == DefaultReset && Hours_Left == 0)
  494.         {
  495.             Hours_Left--;
  496.         }
  497.  
  498.         if (Hours_Left == DefaultReset && Days_Left > 0)
  499.         {
  500.             Minutes_Left = MinutesResetValue;
  501.             Hours_Left = HoursResetValue;
  502.             Days_Left--;
  503.         }
  504.         else if(Hours_Left == DefaultReset && Days_Left == 0)
  505.         {
  506.             Days_Left--;
  507.         }
  508.  
  509.         if (Days_Left == DefaultReset && Months_Left > 0)
  510.         {
  511.             Minutes_Left = MinutesResetValue;
  512.             Hours_Left = HoursResetValue;
  513.             Days_Left = DaysResetValue;
  514.             Months_Left--;
  515.         }
  516.         else if (Days_Left == DefaultReset && Months_Left == 0)
  517.         {
  518.             Months_Left--;
  519.         }
  520.  
  521.         if (Months_Left == DefaultReset && Years_Left > 0)
  522.         {
  523.             Minutes_Left = MinutesResetValue;
  524.             Hours_Left = HoursResetValue;
  525.             Days_Left = DaysResetValue;
  526.             Months_Left = MonthsResetValue;
  527.             Years_Left--;
  528.         }
  529.     }
  530.  
  531.     if (UseCustomSignals == true)
  532.     {
  533.         SignalCounter++;
  534.         Check_Custom_Signals();
  535.     }
  536.  
  537.     // Check if we have completed filling or emptying the progress bar
  538.     if (Seconds_Left == 0 && Minutes_Left == 0 && Hours_Left == 0 && Days_Left == 0 && Months_Left == 0 && Years_Left == 0)
  539.     {
  540.         if (IsFilling == true)
  541.         {
  542.             InternalProgressBar->SetPercent(0.f);
  543.         }
  544.         else
  545.         {
  546.             InternalProgressBar->SetPercent(1.f);
  547.         }
  548.  
  549.         // If we are finished pause the timer and clear it
  550.         Pause();
  551.         ClearTimer();
  552.  
  553.         // Fire off blueprint event when fill bar is done filling or emptying
  554.         FillBar_Completed();
  555.  
  556.         // Should we hide our fill bar now that is is complete?
  557.         if (Hide == true)
  558.         {
  559.             InternalProgressBar->SetVisibility(ESlateVisibility::Hidden);
  560.         }
  561.     }
  562.  
  563.     // Function call to create Clock string values
  564.     GenerateClock();
  565.  
  566.     // Function call to fill or empty our progress bar by the specified amount
  567.     Fill_Unfill();
  568. }
  569.  
  570. // Function that creates the output string "Clock"
  571. void UFloating_Fill_Bar::GenerateClock()
  572. {
  573.     if (Years_Left == 0)
  574.     {
  575.         YearsOutput = "";
  576.     }
  577.     else
  578.     {
  579.         YearsOutput = FString::FromInt(Years_Left) + " " + InternalWaitTime.Custom_Suffix.Year_Suffix + " ";
  580.     }
  581.     if (Months_Left == 0 && Years_Left == 0)
  582.     {
  583.         MonthsOutput = "";
  584.     }
  585.     else
  586.     {
  587.         MonthsOutput = FString::FromInt(Months_Left) + " " + InternalWaitTime.Custom_Suffix.Month_Suffix + " ";
  588.     }
  589.     if (Days_Left == 0 && Months_Left == 0 && Years_Left == 0)
  590.     {
  591.         DaysOutput = "";
  592.     }
  593.     else
  594.     {
  595.         DaysOutput = FString::FromInt(Days_Left) + " " + InternalWaitTime.Custom_Suffix.Day_Suffix + " ";
  596.     }  
  597.     if (Hours_Left == 0 && Days_Left == 0 && Months_Left == 0 && Years_Left == 0)
  598.     {
  599.         HoursOutput = "";
  600.     }
  601.     else
  602.     {
  603.         HoursOutput = FString::FromInt(Hours_Left) + " " + InternalWaitTime.Custom_Suffix.Hour_Suffix + " ";
  604.     }
  605.     if (Minutes_Left == 0 && Hours_Left == 0 && Days_Left == 0 && Months_Left == 0 && Years_Left == 0)
  606.     {
  607.         MinutesOutput = "";
  608.     }
  609.     else
  610.     {
  611.         MinutesOutput = FString::FromInt(Minutes_Left) + " " + InternalWaitTime.Custom_Suffix.Minute_Suffix + " ";
  612.     }
  613.    
  614.     SecondsOutput = FString::FromInt(Seconds_Left) + " " + InternalWaitTime.Custom_Suffix.Second_Suffix + " ";
  615.  
  616.     Clock = YearsOutput + MonthsOutput + DaysOutput + HoursOutput + MinutesOutput + SecondsOutput;
  617.  
  618.     if (Error == true)
  619.     {
  620.         Clock = "PLEASE ENTER A VALID TIME";
  621.     }
  622. }
  623.  
  624. // Update our progress bar, either fill or empty it incrementally
  625. void UFloating_Fill_Bar::Fill_Unfill()
  626. {
  627.     CurrentPercent += DeltaPercent;
  628.     InternalProgressBar->SetPercent(CurrentPercent);
  629. }
  630.  
  631. // Allows for modification of the rate of time passage through a blueprint function call
  632. void UFloating_Fill_Bar::Alter_Rate_Of_Time_Passage(float New_Speed)
  633. {
  634.     Revert_Signal_Cycle();
  635.  
  636.     InternalWaitTime.Seconds = Seconds_Left;
  637.     InternalWaitTime.Minutes = Minutes_Left;
  638.     InternalWaitTime.Hours = Hours_Left;
  639.     InternalWaitTime.Days = Days_Left;
  640.     InternalWaitTime.Months = Months_Left;
  641.     InternalWaitTime.Years = Years_Left;
  642.     InternalWaitTime.Start_Percent = (CurrentPercent * 100);
  643.  
  644.     UpdateBar(InternalWaitTime, New_Speed, IsFilling, Hide, InternalWidget, UseCustomSignals);
  645. }
  646.  
  647. // Function to pause our timer
  648. void UFloating_Fill_Bar::Pause()
  649. {
  650.     Parent_Actor->GetWorldTimerManager().PauseTimer(FillBarHandle);
  651. }
  652.  
  653. // Function to unpause our timer
  654. void UFloating_Fill_Bar::Unpause()
  655. {
  656.     Parent_Actor->GetWorldTimerManager().UnPauseTimer(FillBarHandle);
  657. }
  658.  
  659. // Function to clear our timer
  660. void UFloating_Fill_Bar::ClearTimer()
  661. {
  662.     Parent_Actor->GetWorldTimerManager().ClearTimer(FillBarHandle);
  663. }
  664.  
  665. // Function to modify our count down in real time
  666. void UFloating_Fill_Bar::Modify_Clock(FWait_Time Modify_Time, float Modify_Speed)
  667. {
  668.     Revert_Signal_Cycle();
  669.  
  670.     InternalWaitTime.Seconds = Modify_Time.Seconds;
  671.     InternalWaitTime.Minutes = Modify_Time.Minutes;
  672.     InternalWaitTime.Hours = Modify_Time.Hours;
  673.     InternalWaitTime.Days = Modify_Time.Days;
  674.     InternalWaitTime.Months = Modify_Time.Months;
  675.     InternalWaitTime.Years = Modify_Time.Years;
  676.     InternalWaitTime.Start_Percent = Modify_Time.Start_Percent;
  677.  
  678.     UpdateBar(InternalWaitTime, Modify_Speed, IsFilling, Hide, InternalWidget, UseCustomSignals);
  679. }
  680.  
  681. // Function to save the status of our fill bar current count down
  682. void UFloating_Fill_Bar::SaveClock(FWait_Time &Save_Time)
  683. {
  684.     Revert_Signal_Cycle();
  685.  
  686.     Save_Time = InternalWaitTime;
  687.     Save_Time.Seconds = Seconds_Left;
  688.     Save_Time.Minutes = Minutes_Left;
  689.     Save_Time.Hours = Hours_Left;
  690.     Save_Time.Days = Days_Left;
  691.     Save_Time.Months = Months_Left;
  692.     Save_Time.Years = Years_Left;
  693.     Save_Time.Start_Percent = (CurrentPercent * 100);
  694.     Save_Time.SaveCount = SignalCounter;
  695. }
  696.  
  697. // Fire off blueprint events at specified rates
  698. void UFloating_Fill_Bar::Check_Custom_Signals()
  699. {
  700.     for (int x = 0; x < InternalWaitTime.Signals.Num(); ++x)
  701.     {
  702.         if (InternalWaitTime.Signals[x].Cycle_Length > 0 && (SignalCounter % InternalWaitTime.Signals[x].Cycle_Length) == 0)
  703.         {
  704.             switch (x)
  705.             {
  706.             case 0:
  707.                 Custom_Signal_1();
  708.                 break;
  709.             case 1:
  710.                 Custom_Signal_2();
  711.                 break;
  712.             case 2:
  713.                 Custom_Signal_3();
  714.                 break;
  715.             case 3:
  716.                 Custom_Signal_4();
  717.                 break;
  718.             case 4:
  719.                 Custom_Signal_5();
  720.                 break;
  721.             default:
  722.                 break;
  723.             }
  724.         }
  725.     }
  726.  
  727.     // If we have reached the max value for an int32 reset the count to 0 ** cylcles greater than 68 years won't work, they exceed this value **
  728.     if (SignalCounter == 2147483647)
  729.     {
  730.         SignalCounter = 0;
  731.     }
  732. }
  733.  
  734. // Function to convert the custom blueprint event cycle length back to the enum compatible value
  735. void UFloating_Fill_Bar::Revert_Signal_Cycle()
  736. {
  737.     // Local variables for making time conversions
  738.     int32 ConversionRate = 1;
  739.  
  740.     for (int x = 0; x < InternalWaitTime.Signals.Num(); ++x)
  741.     {
  742.         if (InternalWaitTime.Signals[x].Cycle_Length > 0)
  743.         {
  744.             switch (InternalWaitTime.Signals[x].Time_Category)
  745.             {
  746.             case ETime::Seconds:
  747.                 ConversionRate = 1;
  748.                 break;
  749.             case ETime::Minutes:
  750.                 ConversionRate = 60;
  751.                 break;
  752.             case ETime::Hours:
  753.                 ConversionRate = 3600;
  754.                 break;
  755.             case ETime::Days:
  756.                 ConversionRate = 86400;
  757.                 break;
  758.             case ETime::Months:
  759.                 ConversionRate = 2592000;
  760.                 break;
  761.             case ETime::Years:
  762.                 ConversionRate = 31536000;
  763.                 break;
  764.             default:
  765.                 break;
  766.             }
  767.             InternalWaitTime.Signals[x].Cycle_Length /= ConversionRate;
  768.         }
  769.     }
  770. }
Advertisement
Add Comment
Please, Sign In to add comment