MessageBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Copyright (c) 2017 the Atomic project.
  3. // Copyright (c) 2008-2015 the Urho3D project.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "../../Core/Context.h"
  24. #include "../../Graphics/Graphics.h"
  25. #include "../../IO/Log.h"
  26. #include "../../UI/SystemUI/SystemUIEvents.h"
  27. #include "../../UI/SystemUI/SystemUI.h"
  28. #include "../../UI/SystemUI/MessageBox.h"
  29. namespace Atomic
  30. {
  31. MessageBox::MessageBox(Context* context, const String& messageString, const String& titleString) :
  32. Object(context),
  33. titleText_(0),
  34. messageText_(messageString),
  35. isOpen_(true)
  36. {
  37. SetTitle(titleString);
  38. Graphics* graphics = GetSubsystem<Graphics>();
  39. windowSize_ = ImVec2(300, 150);
  40. windowPosition_ = ImVec2(graphics->GetWidth() / 2 - windowSize_.x / 2, graphics->GetHeight() / 2 - windowSize_.y / 2);
  41. SubscribeToEvent(E_SYSTEMUIFRAME, ATOMIC_HANDLER(MessageBox, RenderFrame));
  42. }
  43. MessageBox::~MessageBox()
  44. {
  45. UnsubscribeFromAllEvents();
  46. }
  47. void MessageBox::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<MessageBox>();
  50. }
  51. void MessageBox::SetTitle(const String& text)
  52. {
  53. titleText_ = ToString("%s##%p", text.CString(), this);
  54. }
  55. void MessageBox::SetMessage(const String& text)
  56. {
  57. messageText_ = text;
  58. }
  59. const String& MessageBox::GetTitle() const
  60. {
  61. return titleText_;
  62. }
  63. const String& MessageBox::GetMessage() const
  64. {
  65. return messageText_;
  66. }
  67. void MessageBox::RenderFrame(StringHash eventType, VariantMap& eventData)
  68. {
  69. using namespace MessageACK;
  70. ImGui::SetNextWindowPos(windowPosition_, ImGuiSetCond_FirstUseEver);
  71. if (ImGui::Begin(titleText_.CString(), &isOpen_, windowSize_, -1, ImGuiWindowFlags_NoCollapse|
  72. ImGuiWindowFlags_NoSavedSettings))
  73. {
  74. ImGui::TextUnformatted(messageText_.CString());
  75. auto region = ImGui::GetContentRegionAvail();
  76. ImGui::SetCursorPos(ImVec2(region.x - 100 + 20, region.y + 20));
  77. bool closeWindow = false;
  78. bool status = false;
  79. if (ImGui::Button("Ok"))
  80. {
  81. closeWindow = true;
  82. status = true;
  83. }
  84. ImGui::SameLine();
  85. if (ImGui::Button("Cancel") || !isOpen_)
  86. {
  87. closeWindow = true;
  88. status = false;
  89. }
  90. if (closeWindow)
  91. {
  92. SendEvent(E_MESSAGEACK, P_OK, status);
  93. UnsubscribeFromAllEvents();
  94. isOpen_ = false;
  95. }
  96. }
  97. ImGui::End();
  98. }
  99. }