MessageBox.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../IO/Log.h"
  6. #include "../Resource/ResourceCache.h"
  7. #include "../Resource/XMLFile.h"
  8. #include "../UI/Button.h"
  9. #include "../UI/MessageBox.h"
  10. #include "../UI/Text.h"
  11. #include "../UI/UI.h"
  12. #include "../UI/UIEvents.h"
  13. #include "../UI/Window.h"
  14. namespace Urho3D
  15. {
  16. MessageBox::MessageBox(Context* context, const String& messageString, const String& titleString, XMLFile* layoutFile,
  17. XMLFile* styleFile) :
  18. Object(context),
  19. window_(nullptr),
  20. titleText_(nullptr),
  21. messageText_(nullptr),
  22. okButton_(nullptr)
  23. {
  24. // If layout file is not given, use the default message box layout
  25. if (!layoutFile)
  26. {
  27. auto* cache = GetSubsystem<ResourceCache>();
  28. layoutFile = cache->GetResource<XMLFile>("UI/MessageBox.xml");
  29. if (!layoutFile) // Error is already logged
  30. return; // Note: windowless MessageBox should not be used!
  31. }
  32. auto* ui = GetSubsystem<UI>();
  33. UIElement* root = ui->GetRoot();
  34. {
  35. SharedPtr<UIElement> holder = ui->LoadLayout(layoutFile, styleFile);
  36. if (!holder) // Error is already logged
  37. return;
  38. window_ = holder;
  39. root->AddChild(window_); // Take ownership of the object before SharedPtr goes out of scope
  40. }
  41. // Set the title and message strings if they are given
  42. titleText_ = window_->GetChildDynamicCast<Text>("TitleText", true);
  43. if (titleText_ && !titleString.Empty())
  44. titleText_->SetText(titleString);
  45. messageText_ = window_->GetChildDynamicCast<Text>("MessageText", true);
  46. if (messageText_ && !messageString.Empty())
  47. messageText_->SetText(messageString);
  48. // Center window after the message is set
  49. auto* window = dynamic_cast<Window*>(window_);
  50. if (window)
  51. {
  52. const IntVector2& size = window->GetSize();
  53. window->SetPosition((root->GetWidth() - size.x_) / 2, (root->GetHeight() - size.y_) / 2);
  54. window->SetModal(true);
  55. SubscribeToEvent(window, E_MODALCHANGED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  56. }
  57. // Bind the buttons (if any in the loaded UI layout) to event handlers
  58. okButton_ = window_->GetChildDynamicCast<Button>("OkButton", true);
  59. if (okButton_)
  60. {
  61. ui->SetFocusElement(okButton_);
  62. SubscribeToEvent(okButton_, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  63. }
  64. auto* cancelButton = window_->GetChildDynamicCast<Button>("CancelButton", true);
  65. if (cancelButton)
  66. SubscribeToEvent(cancelButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  67. auto* closeButton = window_->GetChildDynamicCast<Button>("CloseButton", true);
  68. if (closeButton)
  69. SubscribeToEvent(closeButton, E_RELEASED, URHO3D_HANDLER(MessageBox, HandleMessageAcknowledged));
  70. // Increase reference count to keep Self alive
  71. AddRef();
  72. }
  73. MessageBox::~MessageBox()
  74. {
  75. // This would remove the UI-element regardless of whether it is parented to UI's root or UI's modal-root
  76. if (window_)
  77. window_->Remove();
  78. }
  79. void MessageBox::RegisterObject(Context* context)
  80. {
  81. context->RegisterFactory<MessageBox>();
  82. }
  83. void MessageBox::SetTitle(const String& text)
  84. {
  85. if (titleText_)
  86. titleText_->SetText(text);
  87. }
  88. void MessageBox::SetMessage(const String& text)
  89. {
  90. if (messageText_)
  91. messageText_->SetText(text);
  92. }
  93. const String& MessageBox::GetTitle() const
  94. {
  95. return titleText_ ? titleText_->GetText() : String::EMPTY;
  96. }
  97. const String& MessageBox::GetMessage() const
  98. {
  99. return messageText_ ? messageText_->GetText() : String::EMPTY;
  100. }
  101. void MessageBox::HandleMessageAcknowledged(StringHash eventType, VariantMap& eventData)
  102. {
  103. using namespace MessageACK;
  104. VariantMap& newEventData = GetEventDataMap();
  105. newEventData[P_OK] = eventData[Released::P_ELEMENT] == okButton_;
  106. SendEvent(E_MESSAGEACK, newEventData);
  107. // Self destruct
  108. ReleaseRef();
  109. }
  110. }