ToolTip.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "../UI/ToolTip.h"
  25. #include "../UI/UI.h"
  26. namespace Urho3D
  27. {
  28. extern const char* UI_CATEGORY;
  29. ToolTip::ToolTip(Context* context) :
  30. UIElement(context),
  31. delay_(0.0f),
  32. hovered_(false)
  33. {
  34. SetVisible(false);
  35. }
  36. ToolTip::~ToolTip() = default;
  37. void ToolTip::RegisterObject(Context* context)
  38. {
  39. context->RegisterFactory<ToolTip>(UI_CATEGORY);
  40. URHO3D_COPY_BASE_ATTRIBUTES(UIElement);
  41. URHO3D_ACCESSOR_ATTRIBUTE("Delay", GetDelay, SetDelay, float, 0.0f, AM_FILE);
  42. }
  43. void ToolTip::Update(float timeStep)
  44. {
  45. // Track the element we are parented to for hovering. When we display, we move ourself to the root element
  46. // to ensure displaying on top
  47. UIElement* root = GetRoot();
  48. if (!root)
  49. return;
  50. if (parent_ != root)
  51. target_ = parent_;
  52. // If target is removed while we are displaying, we have no choice but to destroy ourself
  53. if (target_.Expired())
  54. {
  55. Remove();
  56. return;
  57. }
  58. bool hovering = target_->IsHovering() && target_->IsVisibleEffective();
  59. if (!hovering)
  60. {
  61. for (auto it = altTargets_.Begin(); it != altTargets_.End();)
  62. {
  63. SharedPtr<UIElement> target = it->Lock();
  64. if (!target)
  65. it = altTargets_.Erase(it);
  66. else if (hovering = target->IsHovering() && target->IsVisibleEffective())
  67. break;
  68. else
  69. ++it;
  70. }
  71. }
  72. if (hovering)
  73. {
  74. float effectiveDelay = delay_ > 0.0f ? delay_ : GetSubsystem<UI>()->GetDefaultToolTipDelay();
  75. if (!hovered_)
  76. {
  77. hovered_ = true;
  78. displayAt_.Reset();
  79. }
  80. else if (displayAt_.GetMSec(false) >= (unsigned)(effectiveDelay * 1000.0f) && parent_ == target_)
  81. {
  82. originalPosition_ = GetPosition();
  83. IntVector2 screenPosition = GetScreenPosition();
  84. SetParent(root);
  85. SetPosition(screenPosition);
  86. SetVisible(true);
  87. // BringToFront() is unreliable in this case as it takes into account only input-enabled elements.
  88. // Rather just force priority to max
  89. SetPriority(M_MAX_INT);
  90. }
  91. }
  92. else
  93. {
  94. Reset();
  95. }
  96. }
  97. void ToolTip::Reset()
  98. {
  99. if (IsVisible() && parent_ == GetRoot())
  100. {
  101. SetParent(target_);
  102. SetPosition(originalPosition_);
  103. SetVisible(false);
  104. }
  105. hovered_ = false;
  106. displayAt_.Reset();
  107. }
  108. void ToolTip::AddAltTarget(UIElement* target)
  109. {
  110. altTargets_.Push(WeakPtr<UIElement>(target));
  111. }
  112. void ToolTip::SetDelay(float delay)
  113. {
  114. delay_ = delay;
  115. }
  116. }