Node.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "Node.h"
  2. #include "Font.h"
  3. #include "core/Renderer.h"
  4. #include "pugixml/pugixml.hpp"
  5. #include "utils/stringUtils.h"
  6. #include "RenderState.h"
  7. #include "AnimationFrame.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. void Node::draw(DrawContext &dc)
  63. {
  64. drawChildren(dc);
  65. }
  66. /*
  67. bool test()
  68. {
  69. string utf = ws2utf8(L"A");
  70. wstring ws = utf8tows(utf.c_str());
  71. const char *bf = (char*)ws.c_str();
  72. const char *utfstr = utf.c_str();
  73. int code = 0;
  74. while (getNextSingleCode(code, utfstr))
  75. {
  76. int q = 0;
  77. }
  78. return true;
  79. }
  80. bool b = test();
  81. */
  82. TextNode::TextNode(const char *v)
  83. {
  84. #ifdef OX_DEBUG
  85. _text = v;
  86. #endif
  87. const char *utfstr = v;
  88. int code = 0;
  89. utfstr = getNextCode(code, utfstr);
  90. while (code)
  91. {
  92. Symbol s;
  93. s.code = code;
  94. _data.push_back(s);
  95. utfstr = getNextCode(code, utfstr);
  96. }
  97. }
  98. void TextNode::draw(DrawContext &dc)
  99. {
  100. for (size_t i = 0; i < _data.size(); ++i)
  101. {
  102. const Symbol &s = _data[i];
  103. if (!s.gl)
  104. continue;
  105. Diffuse df;
  106. df.base = s.gl->texture;
  107. dc.renderer->setDiffuse(df);
  108. dc.renderer->draw(s.gl->src, s.destRect);
  109. }
  110. drawChildren(dc);
  111. }
  112. int _defMissing = '?';
  113. void TextNode::setDefaultMissingSymbol(int v)
  114. {
  115. _defMissing = v;
  116. }
  117. void TextNode::_resize(Aligner &rd)
  118. {
  119. if (!_data.empty())
  120. {
  121. int i = 0;
  122. Font *font = rd.getStyle().font;
  123. while (i != (int)_data.size())
  124. {
  125. Symbol &s = _data[i];
  126. //wchar_t c = s.c;
  127. s.gl = font->getGlyph(s.code);
  128. if (s.gl)
  129. i += rd.putSymbol(s);
  130. else
  131. {
  132. if (s.code == '\n')
  133. rd.nextLine();
  134. else
  135. {
  136. s.gl = font->getGlyph(_defMissing);
  137. i += rd.putSymbol(s);
  138. }
  139. }
  140. ++i;
  141. if (i < 0)
  142. i = 0;
  143. }
  144. }
  145. }
  146. float mlt(int x, float sf)
  147. {
  148. //return (x + 0.01f) / sf;
  149. return x / sf;
  150. }
  151. void TextNode::_finalPass(Aligner &rd)
  152. {
  153. float scaleFactor = rd.getScaleFactor();
  154. int offsetY = rd.bounds.pos.y;
  155. for (size_t i = 0; i < _data.size(); ++i)
  156. {
  157. Symbol &s = _data[i];
  158. s.y += offsetY;
  159. if (s.gl)
  160. s.destRect = RectF(mlt(s.x, scaleFactor), mlt(s.y, scaleFactor), mlt(s.gl->sw, scaleFactor), mlt(s.gl->sh, scaleFactor));
  161. else
  162. s.destRect = RectF(0, 0, 0, 0);
  163. //s.destRect.size = Vector2(4.26666666f, 7.46666666f);
  164. }
  165. }
  166. void DivNode::_resize(Aligner &rd)
  167. {
  168. }
  169. void DivNode::draw(DrawContext &dc)
  170. {
  171. Color prevColor = dc.renderer->getPrimaryColor();
  172. Color blendedColor = color;
  173. blendedColor.a = (blendedColor.a * dc.rs->alpha) / 255;
  174. dc.renderer->setPrimaryColor(blendedColor);
  175. drawChildren(dc);
  176. dc.renderer->setPrimaryColor(prevColor);
  177. }
  178. DivNode::DivNode(const pugi::xml_node &node)
  179. {
  180. pugi::xml_attribute attr = node.first_attribute();
  181. while(attr)
  182. {
  183. if (!strcmp(attr.name(), "c") ||
  184. !strcmp(attr.name(), "color"))
  185. {
  186. color = hex2color(attr.value());
  187. }
  188. attr = attr.next_attribute();
  189. }
  190. }
  191. }
  192. }