Support floating point values for the progress bar.

This commit is contained in:
hiker 2018-12-10 22:13:13 +11:00
parent 386dc279fd
commit 89cc549ebc
10 changed files with 37 additions and 32 deletions

View File

@ -817,20 +817,20 @@ void Skin::drawProgress(Widget* w, const core::recti &rect,
{
ProgressBarWidget * progress = (ProgressBarWidget*)w;
drawProgressBarInScreen(w, rect, progress->getValue(),
w->m_deactivated);
w->m_deactivated);
}
} // drawProgress
// ----------------------------------------------------------------------------
void Skin::drawProgressBarInScreen(SkinWidgetContainer* swc,
const core::rect< s32 > &rect, int progress,
bool deactivated)
const core::rect< s32 > &rect,
float progress, bool deactivated)
{
drawBoxFromStretchableTexture(swc, rect,
SkinConfig::m_render_params["progress::neutral"], deactivated);
core::recti rect2 = rect;
rect2.LowerRightCorner.X -= (rect.getWidth())
- progress * rect.getWidth() / 100;
- int(progress * rect.getWidth());
drawBoxFromStretchableTexture(swc, rect2,
SkinConfig::m_render_params["progress::fill"], deactivated);
} // drawProgress

View File

@ -338,7 +338,7 @@ namespace GUIEngine
void drawBadgeOn(const Widget* widget, const core::rect<s32>& rect);
void drawProgressBarInScreen(SkinWidgetContainer* swc,
const core::rect< s32 > &rect,
int progress, bool deactivated = false);
float progress, bool deactivated = false);
// irrlicht's callbacks
virtual void draw2DRectangle (gui::IGUIElement *element,

View File

@ -69,23 +69,29 @@ void ProgressBarWidget::add()
// -----------------------------------------------------------------------------
void ProgressBarWidget::setValue(int value)
void ProgressBarWidget::setValue(float value)
{
m_value = value;
m_target_value = value;
m_previous_value = value;
m_value = value/100.0f;
m_target_value = m_value;
m_previous_value = m_value;
if (m_show_label)
setLabel(std::string(StringUtils::toString(value) + "%").c_str());
{
int percent = int(value);
setLabel(std::string(StringUtils::toString(percent) + "%").c_str());
}
} // setValue
// -----------------------------------------------------------------------------
void ProgressBarWidget::moveValue(int value)
void ProgressBarWidget::moveValue(float value)
{
m_previous_value = m_value;
m_target_value = value;
m_target_value = value/100.0f;
if (m_show_label)
setLabel(std::string(StringUtils::toString(value) + "%").c_str());
{
int percent = int(value);
setLabel(std::string(StringUtils::toString(percent) + "%").c_str());
}
} // moveValue
// -----------------------------------------------------------------------------
@ -95,14 +101,14 @@ void ProgressBarWidget::update(float delta)
if (m_target_value != m_value)
{
// Compute current progress in the animation
float cur = (static_cast<float>(m_value) - m_previous_value)
float cur = (m_value - m_previous_value)
/ (m_target_value - m_previous_value);
// Animation time: 1.0 seconds
cur += delta * 10;
if (cur > 1)
cur = 1;
m_value = int(m_previous_value +
cur * (m_target_value - m_previous_value) );
m_value = m_previous_value
+ cur * (m_target_value - m_previous_value);
}
} // update
@ -115,4 +121,3 @@ void ProgressBarWidget::setLabel(irr::core::stringw label)
} // setLabel
// -----------------------------------------------------------------------------

View File

@ -44,12 +44,12 @@ namespace GUIEngine
/** Change the label on the widget */
void setLabel(const irr::core::stringw label);
int m_value;
float m_value;
bool m_show_label;
/** Values for animation */
int m_target_value;
int m_previous_value;
float m_target_value;
float m_previous_value;
public:
@ -58,10 +58,10 @@ namespace GUIEngine
virtual ~ProgressBarWidget();
/** Change the value of the widget, it must be a percent. */
void setValue(int value);
void setValue(float value);
/** Change the value of the widget smooth, it must be a percent. */
void moveValue(int value);
void moveValue(float value);
virtual void update(float delta);
@ -69,7 +69,7 @@ namespace GUIEngine
// --------------------------------------------------------------------
/** Get the current value of the widget. */
int getValue() {return m_value; };
float getValue() {return m_value; };
// --------------------------------------------------------------------
/** Sets if the current fraction is shown as a percentage value
* in the progress bar. */

View File

@ -40,7 +40,7 @@ using namespace irr;
SkillLevelWidget::SkillLevelWidget(core::recti area, const int player_id,
bool multiplayer, bool display_icon,
const int value)
const float value)
: Widget(WTYPE_DIV)
{
m_player_id = player_id;
@ -147,7 +147,7 @@ void SkillLevelWidget::setSize(const int x, const int y, const int w, const int
// -----------------------------------------------------------------------------
void SkillLevelWidget::setValue(const int value)
void SkillLevelWidget::setValue(const float value)
{
m_bar->moveValue(value);
}

6
src/guiengine/widgets/skill_level_widget.hpp Normal file → Executable file
View File

@ -63,7 +63,7 @@ namespace GUIEngine
SkillLevelWidget(core::recti area, const int player_id,
bool multiplayer, bool display_text,
const int value = 0);
const float value = 0);
virtual ~SkillLevelWidget() {};
@ -82,10 +82,10 @@ namespace GUIEngine
void setSize(const int x, const int y, const int w, const int h);
/** Change the value of the widget, it must be a percent. */
void setValue(const int value);
void setValue(const float value);
/** Get the current values of the widget. */
int getValue() {return m_bar->getValue(); };
float getValue() {return m_bar->getValue(); };
/** Change the label of the widget */
void setLabel(const irr::core::stringw& label);

View File

@ -283,7 +283,7 @@ void AddonsLoading::onUpdate(float delta)
if(m_progress->isVisible())
{
float progress = m_download_request->getProgress();
m_progress->setValue((int)(progress*100.0f));
m_progress->setValue(progress*100.0f);
if(progress<0)
{
// Avoid displaying '-100%' in case of an error.

View File

@ -74,7 +74,7 @@ void NetworkKartSelectionScreen::onUpdate(float dt)
auto lp = LobbyProtocol::get<LobbyProtocol>();
float new_value = lp->getRemainingVotingTime() / lp->getMaxVotingTime();
if(new_value < 0) new_value = 0;
m_timer->moveValue(int(new_value*100));
m_timer->setValue(new_value*100.0f);
} // onUpdate
// ----------------------------------------------------------------------------

View File

@ -509,6 +509,6 @@ void TracksScreen::onUpdate(float dt)
auto lp = LobbyProtocol::get<LobbyProtocol>();
float new_value = lp->getRemainingVotingTime() / lp->getMaxVotingTime();
if (new_value < 0) new_value = 0;
m_timer->moveValue(int(new_value * 100));
m_timer->setValue(new_value * 100.0f);
} // onUpdate

View File

@ -198,7 +198,7 @@ void VoteOverview::onUpdate(float dt)
auto lp = LobbyProtocol::get<LobbyProtocol>();
float new_value = lp->getRemainingVotingTime() / lp->getMaxVotingTime();
if (new_value < 0) new_value = 0;
m_timer->moveValue(int(new_value * 100));
m_timer->setValue(new_value * 100);
if(m_index_to_hostid.size()==0) return;