UIDialogElement.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "UIDialogElement.h"
  2. #include "Block.h"
  3. #include "Entity.h"
  4. #include "UIReference.h"
  5. UIDialogElement::UIDialogElement()
  6. : UIContainerElement(),
  7. notifyOnClose(0)
  8. {}
  9. UIDialogElement::~UIDialogElement()
  10. {
  11. if (notifyOnClose)
  12. {
  13. notifyOnClose->release();
  14. }
  15. }
  16. void UIDialogElement::setTitle(const Framework::Text& title)
  17. {
  18. this->title = title;
  19. }
  20. const Framework::Text& UIDialogElement::getTitle() const
  21. {
  22. return title;
  23. }
  24. void UIDialogElement::setNotifyOnClose(UIReference* notifyOnClose)
  25. {
  26. if (this->notifyOnClose)
  27. {
  28. this->notifyOnClose->release();
  29. }
  30. this->notifyOnClose = notifyOnClose;
  31. }
  32. UIReference* UIDialogElement::zNotifyOnClose() const
  33. {
  34. return notifyOnClose;
  35. }
  36. Framework::XML::Element* UIDialogElement::toUIML(
  37. Framework::Either<Block*, Entity*> zTarget, Entity* zActor) const
  38. {
  39. Framework::XML::Element* result
  40. = UIContainerElement::toUIML(zTarget, zActor);
  41. result->setName("dialog");
  42. result->setAttribute("title", title);
  43. Framework::Text id = getId();
  44. if (zTarget.isA())
  45. {
  46. id.append() << "_" + zTarget.getA()->getDimensionId() << ","
  47. << zTarget.getA()->getPos().x << ","
  48. << zTarget.getA()->getPos().y << ","
  49. << zTarget.getA()->getPos().z << "_" << zActor->getId();
  50. }
  51. else
  52. {
  53. id.append() << "_" << zTarget.getB()->getId() << "_" << zActor->getId();
  54. }
  55. result->setAttribute("id", id);
  56. if (notifyOnClose)
  57. {
  58. result->setAttribute(
  59. "notifyOnClose", notifyOnClose->getReferenceId(zTarget, zActor));
  60. }
  61. return result;
  62. }
  63. UIDialogElementFactory::UIDialogElementFactory()
  64. : UIContainerElementFactory<UIDialogElement>()
  65. {}
  66. JSONObjectValidationBuilder* UIDialogElementFactory::addToValidator(
  67. JSONObjectValidationBuilder* builder) const
  68. {
  69. return UIContainerElementFactory::addToValidator(builder)
  70. ->withRequiredString("title")
  71. ->finishString()
  72. ->withRequiredAttribute("notifyOnClose",
  73. Game::INSTANCE->zTypeRegistry()->getValidator<UIReference>(),
  74. false,
  75. true);
  76. }
  77. UIDialogElement* UIDialogElementFactory::fromJson(
  78. Framework::JSON::JSONObject* zJson) const
  79. {
  80. UIDialogElement* result = UIContainerElementFactory::fromJson(zJson);
  81. result->setTitle(zJson->zValue("title")->asString()->getString());
  82. if (zJson->hasValue("notifyOnClose"))
  83. {
  84. result->setNotifyOnClose(
  85. Game::INSTANCE->zTypeRegistry()->fromJson<UIReference>(
  86. zJson->zValue("notifyOnClose")));
  87. }
  88. return result;
  89. }
  90. Framework::JSON::JSONObject* UIDialogElementFactory::toJsonObject(
  91. UIDialogElement* zObject) const
  92. {
  93. Framework::JSON::JSONObject* result
  94. = UIContainerElementFactory::toJsonObject(zObject);
  95. result->addValue(
  96. "title", new Framework::JSON::JSONString(zObject->getTitle()));
  97. if (zObject->zNotifyOnClose())
  98. {
  99. result->addValue("notifyOnClose",
  100. Game::INSTANCE->zTypeRegistry()->toJson<UIReference>(
  101. zObject->zNotifyOnClose()));
  102. }
  103. return result;
  104. }
  105. UIDialogElement* UIDialogElementFactory::createElement(
  106. Framework::JSON::JSONObject* zJson) const
  107. {
  108. return new UIDialogElement();
  109. }
  110. const char* UIDialogElementFactory::getTypeToken() const
  111. {
  112. return "dialog";
  113. }