Node.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "Node.h"
  2. #include "Font.h"
  3. #include "pugixml/pugixml.hpp"
  4. #include "utils/stringUtils.h"
  5. #include "RenderState.h"
  6. #include "AnimationFrame.h"
  7. #include "res/ResFont.h"
  8. namespace oxygine
  9. {
  10. namespace text
  11. {
  12. Node::Node(): _firstChild(0), _lastChild(0), _nextSibling(0)
  13. {
  14. }
  15. Node::~Node()
  16. {
  17. Node* child = _firstChild;
  18. while (child)
  19. {
  20. Node* next = child->_nextSibling;
  21. delete child;
  22. child = next;
  23. }
  24. }
  25. void Node::appendNode(Node* tn)
  26. {
  27. if (!_firstChild)
  28. _firstChild = tn;
  29. else
  30. _lastChild->_nextSibling = tn;
  31. _lastChild = tn;
  32. }
  33. void Node::resize(Aligner& rd)
  34. {
  35. _resize(rd);
  36. Node* child = _firstChild;
  37. while (child)
  38. {
  39. child->resize(rd);
  40. child = child->_nextSibling;
  41. }
  42. }
  43. void Node::finalPass(Aligner& rd)
  44. {
  45. _finalPass(rd);
  46. Node* child = _firstChild;
  47. while (child)
  48. {
  49. child->finalPass(rd);
  50. child = child->_nextSibling;
  51. }
  52. }
  53. void Node::drawChildren(DrawContext& dc)
  54. {
  55. Node* child = _firstChild;
  56. while (child)
  57. {
  58. child->draw(dc);
  59. child = child->_nextSibling;
  60. }
  61. }
  62. Symbol* Node::getSymbol(int& pos)
  63. {
  64. Node* node = _firstChild;
  65. while (node)
  66. {
  67. int num = 0;
  68. Symbol* res = node->getSymbol(pos);
  69. if (res)
  70. return res;
  71. node = node->_nextSibling;
  72. }
  73. return 0;
  74. }
  75. void Node::draw(DrawContext& dc)
  76. {
  77. drawChildren(dc);
  78. }
  79. /*
  80. bool test()
  81. {
  82. string utf = ws2utf8(L"A");
  83. wstring ws = utf8tows(utf.c_str());
  84. const char *bf = (char*)ws.c_str();
  85. const char *utfstr = utf.c_str();
  86. int code = 0;
  87. while (getNextSingleCode(code, utfstr))
  88. {
  89. int q = 0;
  90. }
  91. return true;
  92. }
  93. bool b = test();
  94. */
  95. TextNode::TextNode(const char* v)
  96. {
  97. #ifdef OX_DEBUG
  98. _text = v;
  99. #endif
  100. const char* utfstr = v;
  101. int code = 0;
  102. utfstr = getNextCode(code, utfstr);
  103. while (code)
  104. {
  105. Symbol s;
  106. s.code = code;
  107. _data.push_back(s);
  108. utfstr = getNextCode(code, utfstr);
  109. }
  110. }
  111. Symbol* TextNode::getSymbol(int& pos)
  112. {
  113. if ((int)_data.size() > pos)
  114. return &_data[pos];
  115. pos -= _data.size();
  116. return Node::getSymbol(pos);
  117. }
  118. void TextNode::draw(DrawContext& dc)
  119. {
  120. for (size_t i = 0; i < _data.size(); ++i)
  121. {
  122. const Symbol& s = _data[i];
  123. if (!s.gl.texture)
  124. continue;
  125. dc.renderer->drawElement(s.gl.texture, dc.color.rgba(), s.gl.src, s.destRect);
  126. }
  127. drawChildren(dc);
  128. }
  129. int _defMissing = '?';
  130. void TextNode::setDefaultMissingSymbol(int v)
  131. {
  132. _defMissing = v;
  133. }
  134. void TextNode::_resize(Aligner& rd)
  135. {
  136. if (!_data.empty())
  137. {
  138. int i = 0;
  139. const Font* font = rd.getStyle().font->getFont(0, rd.getStyle().fontSize);
  140. glyphOptions opt = rd.getStyle().options;
  141. while (i != (int)_data.size())
  142. {
  143. Symbol& s = _data[i];
  144. if (s.code == '\n')
  145. rd.nextLine();
  146. else
  147. {
  148. const glyph* gl = font->getGlyph(s.code, opt);
  149. if (gl)
  150. {
  151. s.gl = *gl;
  152. i += rd.putSymbol(s);
  153. }
  154. else
  155. {
  156. gl = font->getGlyph(_defMissing, opt);
  157. if (gl)//even 'missing' symbol could be missing
  158. {
  159. s.gl = *gl;
  160. i += rd.putSymbol(s);
  161. }
  162. }
  163. }
  164. ++i;
  165. if (i < 0)
  166. i = 0;
  167. }
  168. }
  169. }
  170. float mlt(int x, float sf)
  171. {
  172. //return (x + 0.01f) / sf;
  173. return x / sf;
  174. }
  175. void TextNode::_finalPass(Aligner& rd)
  176. {
  177. float scaleFactor = rd.getScale();
  178. int offsetY = rd.bounds.pos.y;
  179. for (size_t i = 0; i < _data.size(); ++i)
  180. {
  181. Symbol& s = _data[i];
  182. s.y += offsetY;
  183. if (s.gl.texture)
  184. s.destRect = RectF(mlt(s.x, scaleFactor), mlt(s.y, scaleFactor), mlt(s.gl.sw, scaleFactor), mlt(s.gl.sh, scaleFactor));
  185. else
  186. s.destRect = RectF(0, 0, 0, 0);
  187. //s.destRect.size = Vector2(4.26666666f, 7.46666666f);
  188. }
  189. }
  190. void DivNode::_resize(Aligner& rd)
  191. {
  192. }
  193. void DivNode::draw(DrawContext& dc)
  194. {
  195. /*
  196. Color prevColor = dc.renderer->getPrimaryColor();
  197. Color blendedColor = color;
  198. blendedColor.a = (blendedColor.a * dc.rs->alpha) / 255;
  199. dc.renderer->setPrimaryColor(blendedColor);
  200. drawChildren(dc);
  201. dc.renderer->setPrimaryColor(prevColor);
  202. */
  203. Color prev = dc.color;
  204. dc.color = color * dc.primary;
  205. drawChildren(dc);
  206. dc.color = prev;
  207. }
  208. DivNode::DivNode(const pugi::xml_node& node)
  209. {
  210. pugi::xml_attribute attr = node.first_attribute();
  211. while (attr)
  212. {
  213. if (!strcmp(attr.name(), "c") ||
  214. !strcmp(attr.name(), "color"))
  215. {
  216. color = hex2color(attr.value());
  217. }
  218. attr = attr.next_attribute();
  219. }
  220. }
  221. }
  222. }