ProgressBar.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../IO/Log.h"
  26. #include "../UI/ProgressBar.h"
  27. #include "../UI/UIEvents.h"
  28. namespace Urho3D
  29. {
  30. extern const char* orientations[];
  31. extern const char* UI_CATEGORY;
  32. ProgressBar::ProgressBar(Context * context) :
  33. BorderImage(context),
  34. orientation_(O_HORIZONTAL),
  35. loadingPercentStyle_("Text"),
  36. range_(1.0f),
  37. value_(0.0f),
  38. showPercentText_(true)
  39. {
  40. SetEnabled(false);
  41. SetEditable(false);
  42. SetFocus(false);
  43. knob_ = CreateChild<BorderImage>("S_Knob");
  44. knob_->SetInternal(true);
  45. loadingText_ = CreateChild<Text>("S_Text");
  46. loadingText_->SetInternal(true);
  47. UpdateProgressBar();
  48. }
  49. ProgressBar::~ProgressBar() = default;
  50. void ProgressBar::RegisterObject(Context * context)
  51. {
  52. context->RegisterFactory<ProgressBar>(UI_CATEGORY);
  53. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  54. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  55. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  56. URHO3D_ACCESSOR_ATTRIBUTE("Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  58. URHO3D_ACCESSOR_ATTRIBUTE("Show Percent Text", GetShowPercentText, SetShowPercentText, bool, true, AM_FILE);
  59. }
  60. void ProgressBar::OnResize(const IntVector2& /*newSize*/, const IntVector2& /*delta*/)
  61. {
  62. UpdateProgressBar();
  63. }
  64. void ProgressBar::SetOrientation(Orientation orientation)
  65. {
  66. orientation_ = orientation;
  67. UpdateProgressBar();
  68. }
  69. void ProgressBar::SetRange(float range)
  70. {
  71. range = Max(range, 0.0f);
  72. if (range != range_)
  73. {
  74. range_ = range;
  75. UpdateProgressBar();
  76. }
  77. }
  78. void ProgressBar::SetValue(float value)
  79. {
  80. value = Clamp(value, 0.0f, range_);
  81. if (value != value_)
  82. {
  83. value_ = value;
  84. UpdateProgressBar();
  85. using namespace ProgressBarChanged;
  86. VariantMap& eventData = GetEventDataMap();
  87. eventData[P_ELEMENT] = this;
  88. eventData[P_VALUE] = value_;
  89. SendEvent(E_PROGRESSBARCHANGED, eventData);
  90. }
  91. }
  92. void ProgressBar::ChangeValue(float delta)
  93. {
  94. SetValue(value_ + delta);
  95. }
  96. void ProgressBar::SetShowPercentText(bool enable)
  97. {
  98. showPercentText_ = enable;
  99. loadingText_->SetVisible(showPercentText_);
  100. }
  101. bool ProgressBar::FilterImplicitAttributes(XMLElement &dest) const
  102. {
  103. if (!BorderImage::FilterImplicitAttributes(dest))
  104. return false;
  105. XMLElement childElem = dest.GetChild("element");
  106. if (!childElem)
  107. return false;
  108. if (!RemoveChildXML(childElem, "Name", "S_Knob"))
  109. return false;
  110. if (!RemoveChildXML(childElem, "Name", "S_Text"))
  111. return false;
  112. if (!RemoveChildXML(childElem, "Position"))
  113. return false;
  114. if (!RemoveChildXML(childElem, "Size"))
  115. return false;
  116. return true;
  117. }
  118. void ProgressBar::UpdateProgressBar()
  119. {
  120. const IntRect &border = knob_->GetBorder();
  121. if (range_ > 0.0f)
  122. {
  123. if (orientation_ == O_HORIZONTAL)
  124. {
  125. auto loadingBarLength = (int) Max((float) GetWidth() * value_ / range_,
  126. (float) (border.left_ + border.right_));
  127. knob_->SetSize(loadingBarLength, GetHeight());
  128. knob_->SetPosition(Clamp(0, 0, GetWidth() - knob_->GetWidth()), 0);
  129. }
  130. else
  131. {
  132. auto loadingBarLength = (int) Max((float) GetHeight() * value_ / range_,
  133. (float) (border.top_ + border.bottom_));
  134. knob_->SetSize(GetWidth(), loadingBarLength);
  135. knob_->SetPosition(0, Clamp(GetHeight() - knob_->GetHeight(), 0, GetHeight() - knob_->GetHeight()));
  136. }
  137. }
  138. else
  139. {
  140. knob_->SetSize(GetSize());
  141. knob_->SetPosition(0, 0);
  142. }
  143. // Update the text.
  144. loadingText_->SetStyle(loadingPercentStyle_);
  145. loadingText_->SetAlignment(HA_CENTER, VA_CENTER);
  146. loadingText_->SetText(Urho3D::ToString("%d %%", RoundToInt((value_ / range_) * 100.0f)));
  147. }
  148. }