2
0

LoadProgress.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * LoadProgress.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "LoadProgress.h"
  12. using namespace Load;
  13. Progress::Progress(): _progress(std::numeric_limits<Type>::min())
  14. {
  15. setupSteps(100);
  16. }
  17. Type Progress::get() const
  18. {
  19. if(_step >= _maxSteps)
  20. return _target;
  21. return static_cast<int>(_progress) + _step * static_cast<int>(_target - _progress) / _maxSteps;
  22. }
  23. void Progress::set(Type p)
  24. {
  25. _progress = p;
  26. }
  27. bool Progress::finished() const
  28. {
  29. return get() == std::numeric_limits<Type>::max();
  30. }
  31. void Progress::reset(int s)
  32. {
  33. _progress = std::numeric_limits<Type>::min();
  34. setupSteps(s);
  35. }
  36. void Progress::finish()
  37. {
  38. _progress = _target = std::numeric_limits<Type>::max();
  39. _step = std::numeric_limits<Type>::min();
  40. _maxSteps = std::numeric_limits<Type>::min();
  41. }
  42. void Progress::setupSteps(int s)
  43. {
  44. setupStepsTill(s, std::numeric_limits<Type>::max());
  45. }
  46. void Progress::setupStepsTill(int s, Type p)
  47. {
  48. if(finished())
  49. return;
  50. if(_step > std::numeric_limits<Type>::min())
  51. _progress = get();
  52. _step = std::numeric_limits<Type>::min();
  53. _maxSteps = s;
  54. _target = p;
  55. }
  56. void Progress::step(int count)
  57. {
  58. if(_step + count > _maxSteps)
  59. {
  60. _step = _maxSteps.load();
  61. }
  62. else
  63. {
  64. _step += count;
  65. }
  66. }