heapcleaner

IntSpinBox (Implementation)

Apr 5th, 2017
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.91 KB | None | 0 0
  1. #include "UseYourModulePCH.h"
  2. #include "IntSpinBox.h"
  3.  
  4. #define LOCTEXT_NAMESPACE "UMG"
  5.  
  6. /////////////////////////////////////////////////////
  7. // UIntSpinBox
  8.  
  9. UIntSpinBox::UIntSpinBox(const FObjectInitializer& ObjectInitializer)
  10.     : Super(ObjectInitializer)
  11. {
  12.     if (!UE_SERVER)
  13.     {
  14.         static ConstructorHelpers::FObjectFinder<UFont> RobotoFontObj(TEXT("/Engine/EngineFonts/Roboto"));
  15.         Font = FSlateFontInfo(RobotoFontObj.Object, 12, FName("Bold"));
  16.     }
  17.  
  18.     // Grab other defaults from slate arguments.
  19.     SSpinBox<int32>::FArguments Defaults;
  20.  
  21.     Value = Defaults._Value.Get();
  22.     MinValue = Defaults._MinValue.Get().Get(0.0f);
  23.     MaxValue = Defaults._MaxValue.Get().Get(0.0f);
  24.     MinSliderValue = Defaults._MinSliderValue.Get().Get(0.0f);
  25.     MaxSliderValue = Defaults._MaxSliderValue.Get().Get(0.0f);
  26.     Delta = Defaults._Delta;
  27.     SliderExponent = Defaults._SliderExponent.Get();
  28.     MinDesiredWidth = Defaults._MinDesiredWidth.Get();
  29.     ClearKeyboardFocusOnCommit = Defaults._ClearKeyboardFocusOnCommit.Get();
  30.     SelectAllTextOnCommit = Defaults._SelectAllTextOnCommit.Get();
  31.  
  32.     WidgetStyle = *Defaults._Style;
  33.     ForegroundColor = FSlateColor(FLinearColor::Black);
  34. }
  35.  
  36. void UIntSpinBox::ReleaseSlateResources(bool bReleaseChildren)
  37. {
  38.     Super::ReleaseSlateResources(bReleaseChildren);
  39.  
  40.     MySpinBox.Reset();
  41. }
  42.  
  43. TSharedRef<SWidget> UIntSpinBox::RebuildWidget()
  44. {
  45.     MySpinBox = SNew(SSpinBox<int32>)
  46.     .Style(&WidgetStyle)
  47.     .Font(Font)
  48.     .ClearKeyboardFocusOnCommit(ClearKeyboardFocusOnCommit)
  49.     .SelectAllTextOnCommit(SelectAllTextOnCommit)
  50.     .OnValueChanged(BIND_UOBJECT_DELEGATE(FOnInt32ValueChanged, HandleOnValueChanged))
  51.     .OnValueCommitted(BIND_UOBJECT_DELEGATE(FOnInt32ValueCommitted, HandleOnValueCommitted))
  52.     .OnBeginSliderMovement(BIND_UOBJECT_DELEGATE(FSimpleDelegate, HandleOnBeginSliderMovement))
  53.     .OnEndSliderMovement(BIND_UOBJECT_DELEGATE(FOnInt32ValueChanged, HandleOnEndSliderMovement))
  54.     ;
  55.    
  56.     return BuildDesignTimeWidget( MySpinBox.ToSharedRef() );
  57. }
  58.  
  59. void UIntSpinBox::SynchronizeProperties()
  60. {
  61.     Super::SynchronizeProperties();
  62.  
  63.     MySpinBox->SetDelta(Delta);
  64.     MySpinBox->SetSliderExponent(SliderExponent);
  65.     MySpinBox->SetMinDesiredWidth(MinDesiredWidth);
  66.  
  67.     MySpinBox->SetForegroundColor(ForegroundColor);
  68.  
  69.     // Set optional values
  70.     bOverride_MinValue ? SetMinValue(MinValue) : ClearMinValue();
  71.     bOverride_MaxValue ? SetMaxValue(MaxValue) : ClearMaxValue();
  72.     bOverride_MinSliderValue ? SetMinSliderValue(MinSliderValue) : ClearMinSliderValue();
  73.     bOverride_MaxSliderValue ? SetMaxSliderValue(MaxSliderValue) : ClearMaxSliderValue();
  74.  
  75.     // Always set the value last so that the max/min values are taken into account.
  76.     TAttribute<int32> ValueBinding = OPTIONAL_BINDING(int32, Value);
  77.     MySpinBox->SetValue(ValueBinding);
  78. }
  79.  
  80. int32 UIntSpinBox::GetValue() const
  81. {
  82.     if (MySpinBox.IsValid())
  83.     {
  84.         return MySpinBox->GetValue();
  85.     }
  86.  
  87.     return Value;
  88. }
  89.  
  90. void UIntSpinBox::SetValue(int32 InValue)
  91. {
  92.     Value = InValue;
  93.     if (MySpinBox.IsValid())
  94.     {
  95.         MySpinBox->SetValue(InValue);
  96.     }
  97. }
  98.  
  99. // MIN VALUE
  100. int32 UIntSpinBox::GetMinValue() const
  101. {
  102.     int32 ReturnVal = TNumericLimits<int32>::Lowest();
  103.  
  104.     if (MySpinBox.IsValid())
  105.     {
  106.         ReturnVal = MySpinBox->GetMinValue();
  107.     }
  108.     else if (bOverride_MinValue)
  109.     {
  110.         ReturnVal = MinValue;
  111.     }
  112.  
  113.     return ReturnVal;
  114. }
  115.  
  116. void UIntSpinBox::SetMinValue(int32 InMinValue)
  117. {
  118.     bOverride_MinValue = true;
  119.     MinValue = InMinValue;
  120.     if (MySpinBox.IsValid())
  121.     {
  122.         MySpinBox->SetMinValue(InMinValue);
  123.     }
  124. }
  125.  
  126. void UIntSpinBox::ClearMinValue()
  127. {
  128.     bOverride_MinValue = false;
  129.     if (MySpinBox.IsValid())
  130.     {
  131.         MySpinBox->SetMinValue(TOptional<int32>());
  132.     }
  133. }
  134.  
  135. // MAX VALUE
  136. int32 UIntSpinBox::GetMaxValue() const
  137. {
  138.     int32 ReturnVal = TNumericLimits<int32>::Max();
  139.  
  140.     if (MySpinBox.IsValid())
  141.     {
  142.         ReturnVal = MySpinBox->GetMaxValue();
  143.     }
  144.     else if (bOverride_MaxValue)
  145.     {
  146.         ReturnVal = MaxValue;
  147.     }
  148.  
  149.     return ReturnVal;
  150. }
  151.  
  152. void UIntSpinBox::SetMaxValue(int32 InMaxValue)
  153. {
  154.     bOverride_MaxValue = true;
  155.     MaxValue = InMaxValue;
  156.     if (MySpinBox.IsValid())
  157.     {
  158.         MySpinBox->SetMaxValue(InMaxValue);
  159.     }
  160. }
  161. void UIntSpinBox::ClearMaxValue()
  162. {
  163.     bOverride_MaxValue = false;
  164.     if (MySpinBox.IsValid())
  165.     {
  166.         MySpinBox->SetMaxValue(TOptional<int32>());
  167.     }
  168. }
  169.  
  170. // MIN SLIDER VALUE
  171. int32 UIntSpinBox::GetMinSliderValue() const
  172. {
  173.     int32 ReturnVal = TNumericLimits<int32>::Min();
  174.  
  175.     if (MySpinBox.IsValid())
  176.     {
  177.         ReturnVal = MySpinBox->GetMinSliderValue();
  178.     }
  179.     else if (bOverride_MinSliderValue)
  180.     {
  181.         ReturnVal = MinSliderValue;
  182.     }
  183.  
  184.     return ReturnVal;
  185. }
  186.  
  187. void UIntSpinBox::SetMinSliderValue(int32 InMinSliderValue)
  188. {
  189.     bOverride_MinSliderValue = true;
  190.     MinSliderValue = InMinSliderValue;
  191.     if (MySpinBox.IsValid())
  192.     {
  193.         MySpinBox->SetMinSliderValue(InMinSliderValue);
  194.     }
  195. }
  196.  
  197. void UIntSpinBox::ClearMinSliderValue()
  198. {
  199.     bOverride_MinSliderValue = false;
  200.     if (MySpinBox.IsValid())
  201.     {
  202.         MySpinBox->SetMinSliderValue(TOptional<int32>());
  203.     }
  204. }
  205.  
  206. // MAX SLIDER VALUE
  207. int32 UIntSpinBox::GetMaxSliderValue() const
  208. {
  209.     int32 ReturnVal = TNumericLimits<int32>::Max();
  210.  
  211.     if (MySpinBox.IsValid())
  212.     {
  213.         ReturnVal = MySpinBox->GetMaxSliderValue();
  214.     }
  215.     else if (bOverride_MaxSliderValue)
  216.     {
  217.         ReturnVal = MaxSliderValue;
  218.     }
  219.  
  220.     return ReturnVal;
  221. }
  222.  
  223. void UIntSpinBox::SetMaxSliderValue(int32 InMaxSliderValue)
  224. {
  225.     bOverride_MaxSliderValue = true;
  226.     MaxSliderValue = InMaxSliderValue;
  227.     if (MySpinBox.IsValid())
  228.     {
  229.         MySpinBox->SetMaxSliderValue(InMaxSliderValue);
  230.     }
  231. }
  232.  
  233. void UIntSpinBox::ClearMaxSliderValue()
  234. {
  235.     bOverride_MaxSliderValue = false;
  236.     if (MySpinBox.IsValid())
  237.     {
  238.         MySpinBox->SetMaxSliderValue(TOptional<int32>());
  239.     }
  240. }
  241.  
  242. void UIntSpinBox::SetForegroundColor(FSlateColor InForegroundColor)
  243. {
  244.     ForegroundColor = InForegroundColor;
  245.     if ( MySpinBox.IsValid() )
  246.     {
  247.         MySpinBox->SetForegroundColor(ForegroundColor);
  248.     }
  249. }
  250.  
  251. // Event handlers
  252. void UIntSpinBox::HandleOnValueChanged(int32 InValue)
  253. {
  254.     if ( !IsDesignTime() )
  255.     {
  256.         OnValueChanged.Broadcast(InValue);
  257.     }
  258. }
  259.  
  260. void UIntSpinBox::HandleOnValueCommitted(int32 InValue, ETextCommit::Type CommitMethod)
  261. {
  262.     if ( !IsDesignTime() )
  263.     {
  264.         OnValueCommitted.Broadcast(InValue, CommitMethod);
  265.     }
  266. }
  267.  
  268. void UIntSpinBox::HandleOnBeginSliderMovement()
  269. {
  270.     if ( !IsDesignTime() )
  271.     {
  272.         OnBeginSliderMovement.Broadcast();
  273.     }
  274. }
  275.  
  276. void UIntSpinBox::HandleOnEndSliderMovement(int32 InValue)
  277. {
  278.     if ( !IsDesignTime() )
  279.     {
  280.         OnEndSliderMovement.Broadcast(InValue);
  281.     }
  282. }
  283.  
  284. void UIntSpinBox::PostLoad()
  285. {
  286.     Super::PostLoad();
  287.  
  288.     if ( GetLinkerUE4Version() < VER_UE4_DEPRECATE_UMG_STYLE_ASSETS )
  289.     {
  290.         if ( Style_DEPRECATED != nullptr )
  291.         {
  292.             const FSpinBoxStyle* StylePtr = Style_DEPRECATED->GetStyle<FSpinBoxStyle>();
  293.             if ( StylePtr != nullptr )
  294.             {
  295.                 WidgetStyle = *StylePtr;
  296.             }
  297.  
  298.             Style_DEPRECATED = nullptr;
  299.         }
  300.     }
  301. }
  302.  
  303.  
  304. #if WITH_EDITOR
  305. const FText UIntSpinBox::GetPaletteCategory()
  306. {
  307.     return LOCTEXT("Input", "Input");
  308. }
  309.  
  310. #endif
  311.  
  312. /////////////////////////////////////////////////////
  313.  
  314. #undef LOCTEXT_NAMESPACE
Advertisement
Add Comment
Please, Sign In to add comment