Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit dbb46ec

Browse files
committed
Refactor CalculateKnob() and use it
1 parent 5965006 commit dbb46ec

File tree

1 file changed

+11
-35
lines changed

1 file changed

+11
-35
lines changed

GUI/GUISlider.cpp

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,7 @@ void GUISlider::OnMouseWheelChange(int x, int y, int modifier, int mouseWheelCha
473473
}
474474

475475
if (m_Value != m_OldValue) {
476-
const int size = (m_Orientation == Horizontal) ? m_Width : m_Height;
477-
const int posRange = size - m_KnobSize - m_EndThickness;
478-
const float ratio = static_cast<float>(m_Value - m_Minimum) / static_cast<float>(m_Maximum - m_Minimum);
479-
m_KnobPosition = std::max(m_EndThickness, static_cast<int>(std::round(static_cast<float>(posRange) * ratio)));
480-
476+
CalculateKnob();
481477
AddEvent(GUIEvent::Notification, Changed, 0);
482478
}
483479
}
@@ -537,37 +533,17 @@ GUIPanel *GUISlider::GetPanel(void)
537533
//////////////////////////////////////////////////////////////////////////////////////////
538534
// Description: Calculates the knob position and size.
539535

540-
void GUISlider::CalculateKnob(void)
541-
{
542-
m_KnobPosition = 0;
543-
m_KnobSize = 0;
544-
545-
// Knob image not loaded yet
546-
if (!m_KnobImage)
547-
return;
536+
void GUISlider::CalculateKnob(void) {
537+
if (!m_KnobImage)
538+
return;
548539

549-
if (m_Orientation == Horizontal) {
550-
// Horizontal
551-
m_KnobSize = m_KnobImage->GetWidth();
552-
553-
if (m_Maximum-m_Minimum > 0) {
554-
float V = (float)(m_Value-m_Minimum) / (float)(m_Maximum-m_Minimum);
555-
m_KnobPosition = (float)(m_Width-m_KnobSize)*V;
556-
}
557-
558-
} else {
559-
// Vertical
560-
m_KnobSize = m_KnobImage->GetHeight();
561-
562-
if (m_Maximum-m_Minimum > 0) {
563-
float V = (float)(m_Value-m_Minimum) / (float)(m_Maximum-m_Minimum);
564-
m_KnobPosition = (float)(m_Height-m_KnobSize)*V;
565-
}
566-
}
567-
568-
// Clamp the knob position again for the graphics
569-
m_KnobPosition = MAX(m_KnobPosition, m_EndThickness);
570-
m_KnobPosition = MIN(m_KnobPosition, (m_Orientation == Horizontal ? m_Width : m_Height) - m_KnobSize - m_EndThickness);
540+
if (m_Maximum > m_Minimum) {
541+
const bool orientation = (m_Orientation == Horizontal);
542+
m_KnobSize = (orientation) ? m_KnobImage->GetWidth() : m_KnobImage->GetHeight();
543+
const int size = (orientation) ? m_Width : m_Height;
544+
const float valueRatio = static_cast<float>(m_Value - m_Minimum) / static_cast<float>(m_Maximum - m_Minimum);
545+
m_KnobPosition = m_EndThickness + static_cast<int>(static_cast<float>(size - m_KnobSize - 2 * m_EndThickness) * valueRatio);
546+
}
571547
}
572548

573549

0 commit comments

Comments
 (0)