CCPUEventHandlerTranslator.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUEventHandlerTranslator.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPUEventHandlerManager.h"
  24. #include "extensions/Particle3D/PU/CCPUObserver.h"
  25. NS_CC_BEGIN
  26. PUEventHandlerTranslator::PUEventHandlerTranslator()
  27. :_handler(nullptr)
  28. {
  29. }
  30. //-------------------------------------------------------------------------
  31. void PUEventHandlerTranslator::translate(PUScriptCompiler* compiler, PUAbstractNode *node)
  32. {
  33. PUObjectAbstractNode* obj = reinterpret_cast<PUObjectAbstractNode*>(node);
  34. PUObjectAbstractNode* parent = obj->parent ? reinterpret_cast<PUObjectAbstractNode*>(obj->parent) : 0;
  35. // The name of the obj is the type of the ParticleEventHandler
  36. // Remark: This can be solved by using a listener, so that obj->values is filled with type + name. Something for later
  37. std::string type;
  38. if(!obj->name.empty())
  39. {
  40. type = obj->name;
  41. }
  42. else
  43. {
  44. return;
  45. }
  46. PUScriptTranslator *particleEventHandlerTranlator = PUEventHandlerManager::Instance()->getTranslator(type);
  47. if (!particleEventHandlerTranlator) return;
  48. // Create the ParticleEventHandler
  49. //mParticleEventHandler = ParticleSystemManager::getSingletonPtr()->createEventHandler(type);
  50. _handler = PUEventHandlerManager::Instance()->createEventHandler(type);
  51. if (!_handler)
  52. {
  53. return;
  54. }
  55. _handler->setEventHandlerType(type);
  56. if (parent && parent->context)
  57. {
  58. PUObserver* observer = static_cast<PUObserver *>(parent->context);
  59. observer->addEventHandler(_handler);
  60. }
  61. else
  62. {
  63. //// It is an alias
  64. //mParticleEventHandler->setAliasName(parent->name);
  65. //ParticleSystemManager::getSingletonPtr()->addAlias(mParticleEventHandler);
  66. }
  67. // The first value is the (optional) name
  68. std::string name;
  69. if(!obj->values.empty())
  70. {
  71. getString(*obj->values.front(), &name);
  72. _handler->setName(name);
  73. }
  74. // Set it in the context
  75. obj->context = _handler;
  76. // Run through properties
  77. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  78. {
  79. // No properties of its own
  80. if((*i)->type == ANT_PROPERTY)
  81. {
  82. PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  83. if (particleEventHandlerTranlator->translateChildProperty(compiler, *i))
  84. {
  85. // Parsed the property by another translator; do nothing
  86. }
  87. else
  88. {
  89. errorUnexpectedProperty(compiler, prop);
  90. }
  91. }
  92. else if((*i)->type == ANT_OBJECT)
  93. {
  94. if (particleEventHandlerTranlator->translateChildObject(compiler, *i))
  95. {
  96. // Parsed the object by another translator; do nothing
  97. }
  98. else
  99. {
  100. processNode(compiler, *i);
  101. }
  102. }
  103. else
  104. {
  105. errorUnexpectedToken(compiler, *i);
  106. }
  107. }
  108. }
  109. NS_CC_END