ProgressBar.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2008-2017 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 "ProgressBar.h"
  23. #include "../Core/Context.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../IO/Log.h"
  26. #include "UIEvents.h"
  27. namespace Urho3D
  28. {
  29. extern const char* orientations[];
  30. extern const char* UI_CATEGORY;
  31. ProgressBar::ProgressBar(Context * context) :
  32. BorderImage(context),
  33. orientation_(O_HORIZONTAL),
  34. loadingPercentStyle_("Text"),
  35. range_(1.0f),
  36. value_(0.0f),
  37. showPercentText_(true)
  38. {
  39. SetEnabled(false);
  40. SetEditable(false);
  41. SetFocus(false);
  42. knob_ = CreateChild<BorderImage>("S_Knob");
  43. knob_->SetInternal(true);
  44. loadingText_ = CreateChild<Text>("S_Text");
  45. loadingText_->SetInternal(true);
  46. UpdateProgressBar();
  47. }
  48. ProgressBar::~ProgressBar()
  49. {
  50. }
  51. void ProgressBar::RegisterObject(Context * context)
  52. {
  53. context->RegisterFactory<ProgressBar>(UI_CATEGORY);
  54. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  55. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  56. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  58. URHO3D_ACCESSOR_ATTRIBUTE("Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  59. URHO3D_ACCESSOR_ATTRIBUTE("Show Percent Text", GetShowPercentText, SetShowPercentText, bool, true, AM_FILE);
  60. }
  61. void ProgressBar::OnResize(const IntVector2& /*newSize*/, const IntVector2& /*delta*/)
  62. {
  63. UpdateProgressBar();
  64. }
  65. void ProgressBar::SetOrientation(Orientation type)
  66. {
  67. orientation_ = type;
  68. UpdateProgressBar();
  69. }
  70. void ProgressBar::SetRange(float range)
  71. {
  72. range = Max(range, 0.0f);
  73. if (range != range_)
  74. {
  75. range_ = range;
  76. UpdateProgressBar();
  77. }
  78. }
  79. void ProgressBar::SetValue(float value)
  80. {
  81. value = Clamp(value, 0.0f, range_);
  82. if (value != value_)
  83. {
  84. value_ = value;
  85. UpdateProgressBar();
  86. using namespace ProgressBarChanged;
  87. VariantMap& eventData = GetEventDataMap();
  88. eventData[P_ELEMENT] = this;
  89. eventData[P_VALUE] = value_;
  90. SendEvent(E_PROGRESSBARCHANGED, eventData);
  91. }
  92. }
  93. void ProgressBar::ChangeValue(float delta)
  94. {
  95. SetValue(value_ + delta);
  96. }
  97. void ProgressBar::SetShowPercentText(bool enable)
  98. {
  99. showPercentText_ = enable;
  100. loadingText_->SetVisible(showPercentText_);
  101. }
  102. bool ProgressBar::FilterImplicitAttributes(XMLElement &dest) const
  103. {
  104. if (!BorderImage::FilterImplicitAttributes(dest))
  105. return false;
  106. XMLElement childElem = dest.GetChild("element");
  107. if (!childElem)
  108. return false;
  109. if (!RemoveChildXML(childElem, "Name", "S_Knob"))
  110. return false;
  111. if (!RemoveChildXML(childElem, "Name", "S_Text"))
  112. return false;
  113. if (!RemoveChildXML(childElem, "Position"))
  114. return false;
  115. if (!RemoveChildXML(childElem, "Size"))
  116. return false;
  117. return true;
  118. }
  119. void ProgressBar::UpdateProgressBar()
  120. {
  121. const IntRect &border = knob_->GetBorder();
  122. if (range_ > 0.0f)
  123. {
  124. if (orientation_ == O_HORIZONTAL)
  125. {
  126. int loadingBarLength = (int) Max((float) GetWidth() * value_ / range_,
  127. (float) (border.left_ + border.right_));
  128. knob_->SetSize(loadingBarLength, GetHeight());
  129. knob_->SetPosition(Clamp(0, 0, GetWidth() - knob_->GetWidth()), 0);
  130. }
  131. else
  132. {
  133. int loadingBarLength = (int) Max((float) GetHeight() * value_ / range_,
  134. (float) (border.top_ + border.bottom_));
  135. knob_->SetSize(GetWidth(), loadingBarLength);
  136. knob_->SetPosition(0, Clamp(0, 0, GetHeight() - knob_->GetHeight()));
  137. }
  138. }
  139. else
  140. {
  141. knob_->SetSize(GetSize());
  142. knob_->SetPosition(0, 0);
  143. }
  144. // Update the text.
  145. loadingText_->SetStyle(loadingPercentStyle_);
  146. loadingText_->SetAlignment(HA_CENTER, VA_CENTER);
  147. loadingText_->SetText(Urho3D::ToString("%d %%", (int)((value_ / range_) * 100.0f + 0.5f)));
  148. }
  149. }