MyGUI_ProgressBar.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_ProgressBar.h"
#include "MyGUI_ResourceSkin.h"
#include "MyGUI_Widget.h"
#include "MyGUI_Gui.h"
#include "MyGUI_SkinManager.h"
namespace MyGUI
{
const size_t PROGRESS_AUTO_WIDTH = 200;
const size_t PROGRESS_AUTO_RANGE = 1000;
const float PROGRESS_AUTO_COEF = 400;
void ProgressBar::initialiseOverride()
{
Base::initialiseOverride();
assignWidget(mTrackPlace, "TrackPlace");
if (nullptr == mTrackPlace)
{
//OBSOLETE
assignWidget(mTrackPlace, "Client");
if (nullptr == mTrackPlace)
mTrackPlace = this;
}
if (isUserString("TrackSkin"))
mTrackSkin = getUserString("TrackSkin");
if (isUserString("TrackWidth"))
mTrackWidth = utility::parseValue<int>(getUserString("TrackWidth"));
//OBSOLETE
if (isUserString("TrackMin"))
mTrackMin = utility::parseValue<int>(getUserString("TrackMin"));
else
mTrackMin = mTrackWidth;
if (isUserString("TrackStep"))
mTrackStep = utility::parseValue<int>(getUserString("TrackStep"));
if (isUserString("TrackFill"))
mFillTrack = utility::parseValue<bool>(getUserString("TrackFill"));
if (!isUserString("TrackStep"))
mTrackStep = mTrackWidth;
if (1 > mTrackWidth)
mTrackWidth = 1;
}
void ProgressBar::shutdownOverride()
{
mTrackPlace = nullptr;
Base::shutdownOverride();
}
void ProgressBar::setProgressRange(size_t _range)
{
if (mAutoTrack)
return;
mRange = _range;
if (mEndPosition > mRange)
mEndPosition = mRange;
if (mStartPosition > mRange)
mStartPosition = mRange;
updateTrack();
}
void ProgressBar::setProgressPosition(size_t _pos)
{
if (mAutoTrack)
return;
mEndPosition = _pos;
if (mEndPosition > mRange)
mEndPosition = mRange;
updateTrack();
}
void ProgressBar::setProgressAutoTrack(bool _auto)
{
if (mAutoTrack == _auto)
return;
mAutoTrack = _auto;
if (mAutoTrack)
{
Gui::getInstance().eventFrameStart += newDelegate(this, &ProgressBar::frameEntered);
mRange = PROGRESS_AUTO_RANGE;
mEndPosition = mStartPosition = 0;
mAutoPosition = 0.0f;
}
else
{
Gui::getInstance().eventFrameStart -= newDelegate(this, &ProgressBar::frameEntered);
mRange = mEndPosition = mStartPosition = 0;
}
updateTrack();
}
void ProgressBar::frameEntered(float _time)
{
if (!mAutoTrack)
return;
mAutoPosition += (PROGRESS_AUTO_COEF * _time);
size_t pos = (size_t)mAutoPosition;
if (pos > (mRange + PROGRESS_AUTO_WIDTH))
mAutoPosition = 0.0f;
if (pos > mRange)
mEndPosition = mRange;
else
mEndPosition = size_t(mAutoPosition);
if (pos < PROGRESS_AUTO_WIDTH)
mStartPosition = 0;
else
mStartPosition = pos - PROGRESS_AUTO_WIDTH;
updateTrack();
}
void ProgressBar::setPosition(const IntPoint& _point)
{
Base::setPosition(_point);
}
void ProgressBar::setSize(const IntSize& _size)
{
Base::setSize(_size);
updateTrack();
}
void ProgressBar::setCoord(const IntCoord& _coord)
{
Base::setCoord(_coord);
updateTrack();
}
void ProgressBar::updateTrack()
{
// все скрыто
if ((0 == mRange) || (0 == mEndPosition))
{
for (auto& iter : mVectorTrack)
{
iter->setVisible(false);
}
return;
}
// тут попроще расчеты
if (mFillTrack)
{
if (mVectorTrack.empty())
{
Widget* widget =
mTrackPlace->createWidget<Widget>(mTrackSkin, IntCoord(), Align::Left | Align::VStretch);
mVectorTrack.push_back(widget);
}
else
{
// первый показываем и ставим норм альфу
VectorWidgetPtr::iterator iter = mVectorTrack.begin();
(*iter)->setVisible(true);
(*iter)->setAlpha(ALPHA_MAX);
// все начиная со второго скрываем
++iter;
for (; iter != mVectorTrack.end(); ++iter)
{
(*iter)->setVisible(false);
}
}
Widget* wid = mVectorTrack.front();
// полностью виден
if ((0 == mStartPosition) && (mRange == mEndPosition))
{
setTrackPosition(wid, 0, 0, getClientWidth(), getClientHeight());
}
// эх
else
{
int pos = (int)mStartPosition * (getClientWidth() - mTrackMin) / (int)mRange;
setTrackPosition(
wid,
pos,
0,
((int)mEndPosition * (getClientWidth() - mTrackMin) / (int)mRange) - pos + mTrackMin,
getClientHeight());
}
return;
}
// сначала проверяем виджеты для трека
int width = getClientWidth() - mTrackWidth + mTrackStep;
int count = width / mTrackStep;
int ost = (width % mTrackStep);
if (ost > 0)
{
width += mTrackStep - ost;
count++;
}
while ((int)mVectorTrack.size() < count)
{
Widget* widget = mTrackPlace->createWidget<Widget>(
mTrackSkin,
IntCoord(/*(int)mVectorTrack.size() * mTrackStep, 0, mTrackWidth, getClientHeight()*/),
Align::Left | Align::VStretch);
widget->setVisible(false);
mVectorTrack.push_back(widget);
}
// все видно
if ((0 == mStartPosition) && (mRange == mEndPosition))
{
int pos = 0;
for (auto& iter : mVectorTrack)
{
iter->setAlpha(ALPHA_MAX);
iter->setVisible(true);
setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
pos++;
}
}
// эх, придется считать
else
{
// сколько не видно
int hide_pix = (width * (int)mStartPosition / (int)mRange);
int hide_count = hide_pix / mTrackStep;
// сколько видно
int show_pix = (width * (int)mEndPosition / (int)mRange);
int show_count = show_pix / mTrackStep;
int pos = 0;
for (auto& iter : mVectorTrack)
{
if (0 > show_count)
{
iter->setVisible(false);
}
else if (0 == show_count)
{
iter->setAlpha((float)(show_pix % mTrackStep) / (float)mTrackStep);
iter->setVisible(true);
setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
}
else
{
if (0 < hide_count)
{
iter->setVisible(false);
}
else if (0 == hide_count)
{
iter->setAlpha(1.0f - ((float)(hide_pix % mTrackStep) / (float)mTrackStep));
iter->setVisible(true);
setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
}
else
{
iter->setAlpha(ALPHA_MAX);
iter->setVisible(true);
setTrackPosition(iter, pos * mTrackStep, 0, mTrackWidth, getClientHeight());
}
}
hide_count--;
show_count--;
pos++;
}
}
}
void ProgressBar::setTrackPosition(Widget* _widget, int _left, int _top, int _width, int _height)
{
if (mFlowDirection == FlowDirection::LeftToRight)
_widget->setCoord(_left, _top, _width, _height);
else if (mFlowDirection == FlowDirection::RightToLeft)
_widget->setCoord(mTrackPlace->getWidth() - _left - _width, _top, _width, _height);
else if (mFlowDirection == FlowDirection::TopToBottom)
_widget->setCoord(_top, _left, _height, _width);
else if (mFlowDirection == FlowDirection::BottomToTop)
_widget->setCoord(_top, mTrackPlace->getHeight() - _left - _width, _height, _width);
}
int ProgressBar::getClientWidth() const
{
return mFlowDirection.isHorizontal() ? mTrackPlace->getWidth() : mTrackPlace->getHeight();
}
int ProgressBar::getClientHeight() const
{
return mFlowDirection.isHorizontal() ? mTrackPlace->getHeight() : mTrackPlace->getWidth();
}
void ProgressBar::setFlowDirection(FlowDirection _value)
{
mFlowDirection = _value;
updateTrack();
}
void ProgressBar::setPropertyOverride(std::string_view _key, std::string_view _value)
{
if (_key == "Range")
setProgressRange(utility::parseValue<size_t>(_value));
else if (_key == "RangePosition")
setProgressPosition(utility::parseValue<size_t>(_value));
else if (_key == "AutoTrack")
setProgressAutoTrack(utility::parseValue<bool>(_value));
else if (_key == "FlowDirection")
setFlowDirection(utility::parseValue<FlowDirection>(_value));
else
{
Base::setPropertyOverride(_key, _value);
return;
}
eventChangeProperty(this, _key, _value);
}
size_t ProgressBar::getProgressRange() const
{
return mRange;
}
size_t ProgressBar::getProgressPosition() const
{
return mEndPosition;
}
bool ProgressBar::getProgressAutoTrack() const
{
return mAutoTrack;
}
FlowDirection ProgressBar::getFlowDirection() const
{
return mFlowDirection;
}
} // namespace MyGUI
MyGUI_Gui.h
MyGUI_Precompiled.h
MyGUI_ProgressBar.h
MyGUI_ResourceSkin.h
MyGUI_SkinManager.h
MyGUI_Widget.h
MyGUI::Gui::getInstance
static Gui & getInstance()
Definition MyGUI_Gui.cpp:34
MyGUI::Gui::eventFrameStart
EventHandle_FrameEventDelegate eventFrameStart
Definition MyGUI_Gui.h:215
MyGUI::ProgressBar::shutdownOverride
void shutdownOverride() override
Definition MyGUI_ProgressBar.cpp:63
MyGUI::ProgressBar::setSize
void setSize(const IntSize &_size) override
Definition MyGUI_ProgressBar.cpp:141
MyGUI::ProgressBar::getProgressRange
size_t getProgressRange() const
Definition MyGUI_ProgressBar.cpp:349
MyGUI::ProgressBar::getFlowDirection
FlowDirection getFlowDirection() const
Definition MyGUI_ProgressBar.cpp:364
MyGUI::ProgressBar::setFlowDirection
void setFlowDirection(FlowDirection _value)
Definition MyGUI_ProgressBar.cpp:316
MyGUI::ProgressBar::setCoord
void setCoord(const IntCoord &_coord) override
Definition MyGUI_ProgressBar.cpp:148
MyGUI::ProgressBar::setProgressRange
void setProgressRange(size_t _range)
Definition MyGUI_ProgressBar.cpp:70
MyGUI::ProgressBar::initialiseOverride
void initialiseOverride() override
Definition MyGUI_ProgressBar.cpp:21
MyGUI::ProgressBar::getProgressAutoTrack
bool getProgressAutoTrack() const
Definition MyGUI_ProgressBar.cpp:359
MyGUI::ProgressBar::setPropertyOverride
void setPropertyOverride(std::string_view _key, std::string_view _value) override
Definition MyGUI_ProgressBar.cpp:322
MyGUI::ProgressBar::setPosition
void setPosition(const IntPoint &_point) override
Definition MyGUI_ProgressBar.cpp:136
MyGUI::ProgressBar::setProgressPosition
void setProgressPosition(size_t _pos)
Definition MyGUI_ProgressBar.cpp:82
MyGUI::ProgressBar::setProgressAutoTrack
void setProgressAutoTrack(bool _auto)
Definition MyGUI_ProgressBar.cpp:92
MyGUI::ProgressBar::getProgressPosition
size_t getProgressPosition() const
Definition MyGUI_ProgressBar.cpp:354
MyGUI::UserData::isUserString
bool isUserString(std::string_view _key) const
Definition MyGUI_WidgetUserData.cpp:44
MyGUI::UserData::getUserString
std::string_view getUserString(std::string_view _key) const
Definition MyGUI_WidgetUserData.cpp:20
MyGUI::Widget
widget description should be here.
Definition MyGUI_Widget.h:40
MyGUI::Widget::eventChangeProperty
EventHandle_WidgetStringString eventChangeProperty
Definition MyGUI_Widget.h:324
MyGUI::Widget::assignWidget
void assignWidget(T *&_widget, std::string_view _name)
Definition MyGUI_Widget.h:413
MyGUI::utility::parseValue
T parseValue(std::string_view _value)
Definition MyGUI_StringUtility.h:60
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::ALPHA_MAX
constexpr float ALPHA_MAX
Definition MyGUI_Macros.h:20
MyGUI::IntPoint
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:27
MyGUI::PROGRESS_AUTO_WIDTH
const size_t PROGRESS_AUTO_WIDTH
Definition MyGUI_ProgressBar.cpp:17
MyGUI::PROGRESS_AUTO_COEF
const float PROGRESS_AUTO_COEF
Definition MyGUI_ProgressBar.cpp:19
MyGUI::IntCoord
types::TCoord< int > IntCoord
Definition MyGUI_Types.h:36
MyGUI::IntSize
types::TSize< int > IntSize
Definition MyGUI_Types.h:30
MyGUI::PROGRESS_AUTO_RANGE
const size_t PROGRESS_AUTO_RANGE
Definition MyGUI_ProgressBar.cpp:18
MyGUI::newDelegate
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
Definition MyGUI_Delegate.h:100
MyGUI::Align::Left
@ Left
Definition MyGUI_Align.h:27
MyGUI::Align::VStretch
@ VStretch
Definition MyGUI_Align.h:33
MyGUI::FlowDirection
Definition MyGUI_FlowDirection.h:19
MyGUI::FlowDirection::TopToBottom
@ TopToBottom
Definition MyGUI_FlowDirection.h:24
MyGUI::FlowDirection::RightToLeft
@ RightToLeft
Definition MyGUI_FlowDirection.h:23
MyGUI::FlowDirection::BottomToTop
@ BottomToTop
Definition MyGUI_FlowDirection.h:25
MyGUI::FlowDirection::LeftToRight
@ LeftToRight
Definition MyGUI_FlowDirection.h:22