logo

libbulletml

Library of Bullet Markup Language (forked from https://shinh.skr.jp/libbulletml/index_en.html )git clone https://hacktivis.me/git/libbulletml.git

bulletmltree.cpp (3676B)


  1. #include "bulletmltree.h"
  2. #include "calc.h"
  3. #include "bulletmlerror.h"
  4. #include "auto_ptr_fix.h"
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <functional>
  8. std::string BulletMLNode::name2string[nameSize] = {
  9. "bullet", "action", "fire", "changeDirection", "changeSpeed", "accel",
  10. "wait", "repeat", "bulletRef", "actionRef", "fireRef", "vanish",
  11. "horizontal", "vertical", "term", "times", "direction", "speed", "param",
  12. "bulletml"
  13. };
  14. BulletMLNode::Type BulletMLNode::string2type(const std::string& str) {
  15. if (str == "aim") return aim;
  16. else if (str == "absolute") return absolute;
  17. else if (str == "relative") return relative;
  18. else if (str == "sequence") return sequence;
  19. else BulletMLError::doAssert(
  20. std::string("BulletML parser: unknown type ") + str + ".");
  21. return typeSize; // not reach
  22. }
  23. BulletMLNode::Name BulletMLNode::string2name(const std::string& str) {
  24. if (str == "bulletml") return bulletml;
  25. else if (str == "bullet") return bullet;
  26. else if (str == "action") return action;
  27. else if (str == "fire") return fire;
  28. else if (str == "changeDirection") return changeDirection;
  29. else if (str == "changeSpeed") return changeSpeed;
  30. else if (str == "accel") return accel;
  31. else if (str == "vanish") return vanish;
  32. else if (str == "wait") return wait;
  33. else if (str == "repeat") return repeat;
  34. else if (str == "direction") return direction;
  35. else if (str == "speed") return speed;
  36. else if (str == "horizontal") return horizontal;
  37. else if (str == "vertical") return vertical;
  38. else if (str == "term") return term;
  39. else if (str == "bulletRef") return bulletRef;
  40. else if (str == "actionRef") return actionRef;
  41. else if (str == "fireRef") return fireRef;
  42. else if (str == "param") return param;
  43. else if (str == "times") return times;
  44. else BulletMLError::doAssert(
  45. std::string("BulletML parser: unknown tag ") + str + ".");
  46. return nameSize; // not reach
  47. }
  48. BulletMLNode::BulletMLNode(const std::string& name)
  49. : name_(string2name(name)), type_(none) {
  50. setReleaseDuty(true);
  51. }
  52. BulletMLNode::~BulletMLNode() {}
  53. void BulletMLNode::setValue(const std::string& val) {
  54. auto_ptr_copy(val_, calc(val));
  55. }
  56. void BulletMLNode::dump() {
  57. #if 0
  58. std::cout << "<" << name2string[name_];
  59. /*
  60. AttributeMap::const_iterator ite;
  61. for (ite = attributes_.begin(); ite != attributes_.end(); ite++) {
  62. std::cout << " " << ite->first << "=" << ite->second;
  63. }
  64. */
  65. std::cout << ">" << std::endl;
  66. /*
  67. if (val_ != "") std::cout << val_ << std::endl;
  68. */
  69. std::for_each(childBegin(), childEnd(), std::mem_fun(&BulletMLNode::dump));
  70. std::cout << "</" << name2string[name_] << ">" << std::endl;
  71. #endif
  72. }
  73. BulletMLNode* BulletMLNode::getChild(Name name) {
  74. ChildIterator ite;
  75. for (ite = childBegin(); ite != childEnd(); ite++) {
  76. if ((*ite)->getName() == name) return *ite;
  77. }
  78. return 0;
  79. }
  80. bool BulletMLNode::findNode(Name name) const {
  81. if (getName() == name) return true;
  82. ConstChildIterator ite;
  83. for (ite = childBegin(); ite != childEnd(); ite++) {
  84. if ((*ite)->findNode(name)) return true;
  85. }
  86. return false;
  87. }
  88. BulletMLNode* BulletMLNode::next() {
  89. BulletMLNode* parent = getParent();
  90. if (parent == 0) return 0;
  91. ChildIterator ite =
  92. std::find(parent->childBegin(), parent->childEnd(), this);
  93. BulletMLError::doAssert(ite != parent->childEnd(), name_ + ": not found");
  94. ite++;
  95. if (ite == parent->childEnd()) return 0;
  96. else return *ite;
  97. }
  98. void BulletMLNode::getAllChildrenVec(Name name, std::vector<BulletMLNode*>& outvec) {
  99. ChildIterator ite;
  100. for (ite = childBegin(); ite != childEnd(); ite++) {
  101. if ((*ite)->getName() == name) outvec.push_back(*ite);
  102. }
  103. }