ToolTip.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "../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. parentHovered_(false)
  33. {
  34. SetVisible(false);
  35. }
  36. ToolTip::~ToolTip()
  37. {
  38. }
  39. void ToolTip::RegisterObject(Context* context)
  40. {
  41. context->RegisterFactory<ToolTip>(UI_CATEGORY);
  42. URHO3D_COPY_BASE_ATTRIBUTES(UIElement);
  43. URHO3D_ACCESSOR_ATTRIBUTE("Delay", GetDelay, SetDelay, float, 0.0f, AM_FILE);
  44. }
  45. void ToolTip::Update(float timeStep)
  46. {
  47. // Track the element we are parented to for hovering. When we display, we move ourself to the root element
  48. // to ensure displaying on top
  49. UIElement* root = GetRoot();
  50. if (!root)
  51. return;
  52. if (parent_ != root)
  53. target_ = parent_;
  54. // If target is removed while we are displaying, we have no choice but to destroy ourself
  55. if (target_.Expired())
  56. {
  57. Remove();
  58. return;
  59. }
  60. if (target_->IsHovering() && target_->IsVisibleEffective())
  61. {
  62. float effectiveDelay = delay_ > 0.0f ? delay_ : GetSubsystem<UI>()->GetDefaultToolTipDelay();
  63. if (!parentHovered_)
  64. {
  65. parentHovered_ = true;
  66. displayAt_.Reset();
  67. }
  68. else if (displayAt_.GetMSec(false) >= (unsigned)(effectiveDelay * 1000.0f) && parent_ == target_)
  69. {
  70. originalPosition_ = GetPosition();
  71. IntVector2 screenPosition = GetScreenPosition();
  72. SetParent(root);
  73. SetPosition(screenPosition);
  74. SetVisible(true);
  75. // BringToFront() is unreliable in this case as it takes into account only input-enabled elements.
  76. // Rather just force priority to max
  77. SetPriority(M_MAX_INT);
  78. }
  79. }
  80. else
  81. {
  82. if (IsVisible() && parent_ == root)
  83. {
  84. SetParent(target_);
  85. SetPosition(originalPosition_);
  86. SetVisible(false);
  87. }
  88. parentHovered_ = false;
  89. displayAt_.Reset();
  90. }
  91. }
  92. void ToolTip::SetDelay(float delay)
  93. {
  94. delay_ = delay;
  95. }
  96. }