CCPUObserverTranslator.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 "CCPUObserverTranslator.h"
  22. #include "extensions/Particle3D/PU/CCPUParticleSystem3D.h"
  23. #include "extensions/Particle3D/PU/CCPUObserverManager.h"
  24. #include "extensions/Particle3D/PU/CCPUObserver.h"
  25. NS_CC_BEGIN
  26. PUObserverTranslator::PUObserverTranslator()
  27. :_observer(nullptr)
  28. {
  29. }
  30. //-------------------------------------------------------------------------
  31. void PUObserverTranslator::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 Observer
  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. //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  45. return;
  46. }
  47. // Get the factory
  48. //ParticleObserverFactory* particleObserverFactory = ParticleSystemManager::getSingletonPtr()->getObserverFactory(type);
  49. //if (!particleObserverFactory)
  50. //{
  51. // //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  52. // return;
  53. //}
  54. PUScriptTranslator *particleObserverTranlator = PUObserverManager::Instance()->getTranslator(type);
  55. if (!particleObserverTranlator) return;
  56. // Create the Observer
  57. _observer = PUObserverManager::Instance()->createObserver(type);
  58. if (!_observer)
  59. {
  60. //compiler->addError(ScriptCompiler::CE_INVALIDPARAMETERS, obj->file, obj->line);
  61. return;
  62. }
  63. _observer->setObserverType(type);
  64. if (parent && parent->context)
  65. {
  66. PUParticleSystem3D* system = static_cast<PUParticleSystem3D *>(parent->context);
  67. system->addObserver(_observer);
  68. }
  69. else
  70. {
  71. //// It is an alias
  72. //mObserver->setAliasName(parent->name);
  73. //ParticleSystemManager::getSingletonPtr()->addAlias(mObserver);
  74. }
  75. // The first value is the (optional) name
  76. std::string name;
  77. if(!obj->values.empty())
  78. {
  79. getString(*obj->values.front(), &name);
  80. _observer->setName(name);
  81. }
  82. // Set it in the context
  83. obj->context = _observer;
  84. // Run through properties
  85. for(PUAbstractNodeList::iterator i = obj->children.begin(); i != obj->children.end(); ++i)
  86. {
  87. if((*i)->type == ANT_PROPERTY)
  88. {
  89. PUPropertyAbstractNode* prop = reinterpret_cast<PUPropertyAbstractNode*>((*i));
  90. if (prop->name == token[TOKEN_ENABLED])
  91. {
  92. // Property: enabled
  93. if (passValidateProperty(compiler, prop, token[TOKEN_ENABLED], VAL_BOOL))
  94. {
  95. bool val;
  96. if(getBoolean(*prop->values.front(), &val))
  97. {
  98. _observer->setEnabled(val);
  99. }
  100. }
  101. }
  102. else if (prop->name == token[TOKEN_OBSERVE_PARTICLE_TYPE])
  103. {
  104. // Property: observe_particle_type
  105. if (passValidateProperty(compiler, prop, token[TOKEN_OBSERVE_PARTICLE_TYPE], VAL_STRING))
  106. {
  107. std::string val;
  108. if(getString(*prop->values.front(), &val))
  109. {
  110. if (val == token[TOKEN_VISUAL_PARTICLE])
  111. {
  112. _observer->setParticleTypeToObserve(PUParticle3D::PT_VISUAL);
  113. }
  114. else if (val == token[TOKEN_EMITTER_PARTICLE])
  115. {
  116. _observer->setParticleTypeToObserve(PUParticle3D::PT_EMITTER);
  117. }
  118. else if (val == token[TOKEN_AFFECTOR_PARTICLE])
  119. {
  120. _observer->setParticleTypeToObserve(PUParticle3D::PT_AFFECTOR);
  121. }
  122. else if (val == token[TOKEN_TECHNIQUE_PARTICLE])
  123. {
  124. _observer->setParticleTypeToObserve(PUParticle3D::PT_TECHNIQUE);
  125. }
  126. else if (val == token[TOKEN_SYSTEM_PARTICLE])
  127. {
  128. _observer->setParticleTypeToObserve(PUParticle3D::PT_SYSTEM);
  129. }
  130. }
  131. }
  132. }
  133. else if (prop->name == token[TOKEN_OBSERVE_INTERVAL])
  134. {
  135. // Property: observe_interval
  136. if (passValidateProperty(compiler, prop, token[TOKEN_OBSERVE_INTERVAL], VAL_REAL))
  137. {
  138. float val;
  139. if(getFloat(*prop->values.front(), &val))
  140. {
  141. _observer->setObserverInterval(val);
  142. }
  143. }
  144. }
  145. else if (prop->name == token[TOKEN_OBSERVE_UNTIL_EVENT])
  146. {
  147. // Property: observe_until_event
  148. if (passValidateProperty(compiler, prop, token[TOKEN_OBSERVE_UNTIL_EVENT], VAL_BOOL))
  149. {
  150. bool val;
  151. if(getBoolean(*prop->values.front(), &val))
  152. {
  153. _observer->setObserveUntilEvent(val);
  154. }
  155. }
  156. }
  157. else if (particleObserverTranlator->translateChildProperty(compiler, *i))
  158. {
  159. // Parsed the property by another translator; do nothing
  160. }
  161. else
  162. {
  163. errorUnexpectedProperty(compiler, prop);
  164. }
  165. }
  166. else if((*i)->type == ANT_OBJECT)
  167. {
  168. if (particleObserverTranlator->translateChildObject(compiler, *i))
  169. {
  170. // Parsed the object by another translator; do nothing
  171. }
  172. else
  173. {
  174. processNode(compiler, *i);
  175. }
  176. }
  177. else
  178. {
  179. errorUnexpectedToken(compiler, *i);
  180. }
  181. }
  182. }
  183. NS_CC_END