LoadProgress.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. if(!_maxSteps)
  22. return _progress;
  23. return static_cast<int>(_progress) + _step * static_cast<int>(_target - _progress) / _maxSteps;
  24. }
  25. void Progress::set(Type p)
  26. {
  27. _progress = p;
  28. }
  29. bool Progress::finished() const
  30. {
  31. return get() == std::numeric_limits<Type>::max();
  32. }
  33. void Progress::reset(int s)
  34. {
  35. _progress = std::numeric_limits<Type>::min();
  36. setupSteps(s);
  37. }
  38. void Progress::finish()
  39. {
  40. _progress = _target = std::numeric_limits<Type>::max();
  41. _step = std::numeric_limits<Type>::min();
  42. _maxSteps = std::numeric_limits<Type>::min();
  43. }
  44. void Progress::setupSteps(int s)
  45. {
  46. setupStepsTill(s, std::numeric_limits<Type>::max());
  47. }
  48. void Progress::setupStepsTill(int s, Type p)
  49. {
  50. if(finished())
  51. return;
  52. if(_step > std::numeric_limits<Type>::min())
  53. _progress = get();
  54. _step = std::numeric_limits<Type>::min();
  55. _maxSteps = s;
  56. _target = p;
  57. }
  58. void Progress::step(int count)
  59. {
  60. if(_step + count > _maxSteps)
  61. {
  62. _step = _maxSteps.load();
  63. }
  64. else
  65. {
  66. _step += count;
  67. }
  68. }