2
0

UISlider.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <UI/UISlider.h>
  5. #include <UI/UIManager.h>
  6. JPH_IMPLEMENT_RTTI_VIRTUAL(UISlider)
  7. {
  8. JPH_ADD_BASE_CLASS(UISlider, UIElement)
  9. }
  10. void UISlider::CopyTo(UIElement *ioElement) const
  11. {
  12. UIElement::CopyTo(ioElement);
  13. UISlider *element = StaticCast<UISlider>(ioElement);
  14. element->mCurrentValue = mCurrentValue;
  15. element->mMinValue = mMinValue;
  16. element->mMaxValue = mMaxValue;
  17. element->mStepValue = mStepValue;
  18. element->mDecreaseButton = mDecreaseButton;
  19. element->mIncreaseButton = mIncreaseButton;
  20. element->mSpaceBetweenButtonAndSlider = mSpaceBetweenButtonAndSlider;
  21. element->mSlider = mSlider;
  22. element->mThumb = mThumb;
  23. element->mValueChangedAction = mValueChangedAction;
  24. }
  25. void UISlider::GetSliderRange(int &outSliderStart, int &outSliderEnd) const
  26. {
  27. outSliderStart = GetX() + mDecreaseButton->GetWidth() + mSpaceBetweenButtonAndSlider;
  28. outSliderEnd = GetX() + GetWidth() - mIncreaseButton->GetWidth() - mSpaceBetweenButtonAndSlider;
  29. }
  30. int UISlider::GetThumbStart(int inSliderStart, int inSliderEnd) const
  31. {
  32. return inSliderStart + int(float(inSliderEnd - inSliderStart - mThumb.mWidth) * (mCurrentValue - mMinValue) / (mMaxValue - mMinValue));
  33. }
  34. bool UISlider::HandleUIEvent(EUIEvent inEvent, UIElement *inSender)
  35. {
  36. if (inEvent == EVENT_BUTTON_DOWN)
  37. {
  38. if (inSender == mDecreaseButton)
  39. {
  40. SetValueInternal(mCurrentValue - mStepValue);
  41. return true;
  42. }
  43. else if (inSender == mIncreaseButton)
  44. {
  45. SetValueInternal(mCurrentValue + mStepValue);
  46. return true;
  47. }
  48. }
  49. return UIElement::HandleUIEvent(inEvent, inSender);
  50. }
  51. bool UISlider::MouseDown(int inX, int inY)
  52. {
  53. if (Contains(inX, inY))
  54. {
  55. int slider_start, slider_end;
  56. GetSliderRange(slider_start, slider_end);
  57. int tx = GetThumbStart(slider_start, slider_end);
  58. if (inX >= tx && inX < tx + mThumb.mWidth)
  59. {
  60. mThumbDragPoint = inX - tx;
  61. return true;
  62. }
  63. }
  64. return UIElement::MouseDown(inX, inY);
  65. }
  66. bool UISlider::MouseUp(int inX, int inY)
  67. {
  68. if (mThumbDragPoint != -1)
  69. {
  70. mThumbDragPoint = -1;
  71. return true;
  72. }
  73. return UIElement::MouseUp(inX, inY);
  74. }
  75. bool UISlider::MouseMove(int inX, int inY)
  76. {
  77. if (mThumbDragPoint != -1)
  78. {
  79. // Calculate new value
  80. int slider_start, slider_end;
  81. GetSliderRange(slider_start, slider_end);
  82. SetValueInternal(mMinValue + float(inX - mThumbDragPoint - slider_start) * (mMaxValue - mMinValue) / float(slider_end - slider_start - mThumb.mWidth));
  83. return true;
  84. }
  85. return UIElement::MouseMove(inX, inY);
  86. }
  87. void UISlider::MouseCancel()
  88. {
  89. UIElement::MouseCancel();
  90. mThumbDragPoint = -1;
  91. }
  92. void UISlider::Draw() const
  93. {
  94. UIElement::Draw();
  95. int slider_start, slider_end;
  96. GetSliderRange(slider_start, slider_end);
  97. // Draw slider
  98. int sy = (GetHeight() - mSlider.mHeight) / 2;
  99. GetManager()->DrawQuad(slider_start, GetY() + sy, slider_end - slider_start, mSlider.mHeight, mSlider, Color::sWhite);
  100. // Draw thumb
  101. int tx = GetThumbStart(slider_start, slider_end);
  102. int ty = (GetHeight() - mThumb.mHeight) / 2;
  103. GetManager()->DrawQuad(tx, GetY() + ty, mThumb.mWidth, mThumb.mHeight, mThumb, Color::sWhite);
  104. }
  105. void UISlider::AutoLayout()
  106. {
  107. UIElement::AutoLayout();
  108. // Position decrease button
  109. mDecreaseButton->SetRelativeX(0);
  110. mDecreaseButton->SetRelativeY((GetHeight() - mDecreaseButton->GetHeight()) / 2);
  111. // Position increase button
  112. mIncreaseButton->SetRelativeX(GetWidth() - mIncreaseButton->GetWidth());
  113. mIncreaseButton->SetRelativeY((GetHeight() - mIncreaseButton->GetHeight()) / 2);
  114. }
  115. void UISlider::SetValueInternal(float inValue)
  116. {
  117. float old_value = mCurrentValue;
  118. float step = round((inValue - mMinValue) / mStepValue);
  119. mCurrentValue = Clamp(mMinValue + step * mStepValue, mMinValue, mMaxValue);
  120. if (mCurrentValue != old_value)
  121. {
  122. if (mValueChangedAction)
  123. mValueChangedAction(mCurrentValue);
  124. UpdateStaticText();
  125. }
  126. }
  127. void UISlider::UpdateStaticText()
  128. {
  129. if (mStaticText != nullptr)
  130. {
  131. float step_frac = mStepValue - trunc(mStepValue);
  132. float min_frac = mMinValue - trunc(mMinValue);
  133. float max_frac = mMaxValue - trunc(mMaxValue);
  134. float smallest = step_frac;
  135. if (min_frac < smallest && abs(min_frac) > 1.0e-6f)
  136. smallest = min_frac;
  137. if (max_frac < smallest && abs(max_frac) > 1.0e-6f)
  138. smallest = max_frac;
  139. if (smallest == 0.0f)
  140. {
  141. // Integer values
  142. mStaticText->SetText(ConvertToString(int(round(mCurrentValue))));
  143. }
  144. else
  145. {
  146. int num_digits = -int(floor(log10(smallest)));
  147. stringstream ss;
  148. ss.precision(num_digits);
  149. ss << fixed << mCurrentValue;
  150. mStaticText->SetText(ss.str());
  151. }
  152. }
  153. }