Skip to content

Commit 78a8b2b

Browse files
rh101minggo
authored andcommitted
Only send the ON_PERCENTAGE_CHANGED if and only if the percentage value has actually changed. (#19556)
1 parent a3a536e commit 78a8b2b

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

cocos/ui/UISlider.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,24 @@ void Slider::setPercent(int percent)
455455
{
456456
percent = 0;
457457
}
458-
_percent = percent;
459-
float res = 1.0 * percent / _maxPercent;
458+
459+
// Only send event if value has changed
460+
if (_percent != percent)
461+
{
462+
_percent = percent;
463+
updateVisualSlider();
464+
percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
465+
}
466+
}
467+
468+
void Slider::updateVisualSlider()
469+
{
470+
float res = 1.0 * _percent / _maxPercent;
460471
float dis = _barLength * res;
461472
_slidBallRenderer->setPosition(dis, _contentSize.height / 2.0f);
462473
if (_scale9Enabled)
463474
{
464-
_progressBarRenderer->setPreferredSize(Size(dis,_contentSize.height));
475+
_progressBarRenderer->setPreferredSize(Size(dis, _contentSize.height));
465476
}
466477
else
467478
{
@@ -498,13 +509,11 @@ void Slider::onTouchMoved(Touch *touch, Event* /*unusedEvent*/)
498509
{
499510
_touchMovePosition = touch->getLocation();
500511
setPercent(getPercentWithBallPos(_touchMovePosition));
501-
percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
502512
}
503513

504514
void Slider::onTouchEnded(Touch *touch, Event *unusedEvent)
505515
{
506516
Widget::onTouchEnded(touch, unusedEvent);
507-
percentChangedEvent(EventType::ON_PERCENTAGE_CHANGED);
508517
percentChangedEvent(EventType::ON_SLIDEBALL_UP);
509518
}
510519

@@ -624,7 +633,7 @@ void Slider::barRendererScaleChangedWithSize()
624633
}
625634
}
626635
_barRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
627-
setPercent(_percent);
636+
updateVisualSlider();
628637
}
629638

630639
void Slider::progressBarRendererScaleChangedWithSize()
@@ -666,7 +675,7 @@ void Slider::progressBarRendererScaleChangedWithSize()
666675
}
667676
}
668677
_progressBarRenderer->setPosition(0.0f, _contentSize.height / 2.0f);
669-
setPercent(_percent);
678+
updateVisualSlider();
670679
}
671680

672681
void Slider::onPressStateChangedToNormal()

cocos/ui/UISlider.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ class CC_GUI_DLL Slider : public Widget
204204
*/
205205
void setPercent(int percent);
206206

207+
/**
208+
* Updates the visual elements of the slider.
209+
*/
210+
void updateVisualSlider();
211+
207212
/**
208213
* Gets the progress direction of slider.
209214
*

0 commit comments

Comments
 (0)