OpenDialogInteractionConfig.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "OpenDialogInteractionConfig.h"
  2. #include <XML.h>
  3. #include "Entity.h"
  4. #include "UIController.h"
  5. #include "UIDialog.h"
  6. #include "UIDialogElement.h"
  7. OpenDialogInteractionConfig::OpenDialogInteractionConfig()
  8. : InteractionConfig(),
  9. dialogElement(0)
  10. {}
  11. OpenDialogInteractionConfig::~OpenDialogInteractionConfig()
  12. {
  13. if (dialogElement)
  14. {
  15. dialogElement->release();
  16. }
  17. }
  18. void OpenDialogInteractionConfig::setDialogElement(
  19. UIDialogElement* dialogElement)
  20. {
  21. if (this->dialogElement)
  22. {
  23. this->dialogElement->release();
  24. }
  25. this->dialogElement = dialogElement;
  26. }
  27. UIDialogElement* OpenDialogInteractionConfig::zDialogElement() const
  28. {
  29. return dialogElement;
  30. }
  31. bool OpenDialogInteractionConfig::onInteraction(
  32. Framework::Either<Block*, Entity*> target,
  33. Item* zItem,
  34. Entity* actor,
  35. bool& itemChanged)
  36. {
  37. Framework::XML::Element* uiml = dialogElement->toUIML(target, actor);
  38. Game::INSTANCE->zUIController()->addDialog(
  39. new UIDialog(uiml->getAttributeValue("id"), actor->getId(), uiml));
  40. return true;
  41. }
  42. OpenDialogInteractionConfigFactory::OpenDialogInteractionConfigFactory()
  43. : InteractionConfigFactory<OpenDialogInteractionConfig>()
  44. {}
  45. JSONObjectValidationBuilder* OpenDialogInteractionConfigFactory::addToValidator(
  46. JSONObjectValidationBuilder* builder) const
  47. {
  48. return InteractionConfigFactory<
  49. OpenDialogInteractionConfig>::addToValidator(builder)
  50. ->withRequiredAttribute("dialogElement",
  51. Game::INSTANCE->zTypeRegistry()->getValidator<UIDialogElement>());
  52. }
  53. OpenDialogInteractionConfig* OpenDialogInteractionConfigFactory::fromJson(
  54. Framework::JSON::JSONObject* zJson) const
  55. {
  56. OpenDialogInteractionConfig* result
  57. = InteractionConfigFactory<OpenDialogInteractionConfig>::fromJson(
  58. zJson);
  59. result->setDialogElement(
  60. Game::INSTANCE->zTypeRegistry()->fromJson<UIDialogElement>(
  61. zJson->zValue("dialogElement")));
  62. return result;
  63. }
  64. Framework::JSON::JSONObject* OpenDialogInteractionConfigFactory::toJsonObject(
  65. OpenDialogInteractionConfig* zObject) const
  66. {
  67. Framework::JSON::JSONObject* result
  68. = InteractionConfigFactory<OpenDialogInteractionConfig>::toJsonObject(
  69. zObject);
  70. result->addValue("dialogElement",
  71. Game::INSTANCE->zTypeRegistry()->toJson(zObject->zDialogElement()));
  72. return result;
  73. }
  74. const char* OpenDialogInteractionConfigFactory::getTypeToken() const
  75. {
  76. return "OpenDialogInteractionConfig";
  77. }
  78. OpenDialogInteractionConfig* OpenDialogInteractionConfigFactory::createValue(
  79. Framework::JSON::JSONObject* zJson) const
  80. {
  81. return new OpenDialogInteractionConfig();
  82. }