ToolTip.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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
  67. {
  68. hovering = target->IsHovering() && target->IsVisibleEffective();
  69. if (hovering)
  70. break;
  71. else
  72. ++it;
  73. }
  74. }
  75. }
  76. if (hovering)
  77. {
  78. float effectiveDelay = delay_ > 0.0f ? delay_ : GetSubsystem<UI>()->GetDefaultToolTipDelay();
  79. if (!hovered_)
  80. {
  81. hovered_ = true;
  82. displayAt_.Reset();
  83. }
  84. else if (displayAt_.GetMSec(false) >= (unsigned)(effectiveDelay * 1000.0f) && parent_ == target_)
  85. {
  86. originalPosition_ = GetPosition();
  87. IntVector2 screenPosition = GetScreenPosition();
  88. SetParent(root);
  89. SetPosition(screenPosition);
  90. SetVisible(true);
  91. // BringToFront() is unreliable in this case as it takes into account only input-enabled elements.
  92. // Rather just force priority to max
  93. SetPriority(M_MAX_INT);
  94. }
  95. }
  96. else
  97. {
  98. Reset();
  99. }
  100. }
  101. void ToolTip::Reset()
  102. {
  103. if (IsVisible() && parent_ == GetRoot())
  104. {
  105. SetParent(target_);
  106. SetPosition(originalPosition_);
  107. SetVisible(false);
  108. }
  109. hovered_ = false;
  110. displayAt_.Reset();
  111. }
  112. void ToolTip::AddAltTarget(UIElement* target)
  113. {
  114. altTargets_.Push(WeakPtr<UIElement>(target));
  115. }
  116. void ToolTip::SetDelay(float delay)
  117. {
  118. delay_ = delay;
  119. }
  120. }