2
0

UISlider.cpp 4.6 KB

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