TextSample.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "TextSample.h"
  2. #include "SamplesGame.h"
  3. #if defined(ADD_SAMPLE)
  4. ADD_SAMPLE("Graphics", "Text", TextSample, 9);
  5. #endif
  6. #define FONT_COUNT 6
  7. std::string _fontNames[] =
  8. {
  9. "arial",
  10. "arial-distance",
  11. "badaboom",
  12. "fishfingers",
  13. "neuropol",
  14. "custom",
  15. };
  16. TextSample::TextSample()
  17. : _form(NULL), _stateBlock(NULL), _size(18), _wrap(true), _ignoreClip(false), _useViewport(true), _rightToLeft(false), _simple(false), _alignment(Font::ALIGN_LEFT),
  18. _fontsCount(FONT_COUNT), _fontIndex(0), _font(NULL), _viewport(250, 100, 512, 200)
  19. {
  20. }
  21. void TextSample::finalize()
  22. {
  23. SAFE_RELEASE(_stateBlock);
  24. for (unsigned int i = 0; i < _fonts.size(); i++)
  25. {
  26. SAFE_RELEASE(_fonts[i]);
  27. }
  28. SAFE_RELEASE(_form);
  29. }
  30. void TextSample::initialize()
  31. {
  32. // Create our render state block that will be reused across all materials
  33. _stateBlock = RenderState::StateBlock::create();
  34. _stateBlock->setCullFace(true);
  35. _stateBlock->setDepthTest(true);
  36. _stateBlock->setBlend(true);
  37. _stateBlock->setBlendSrc(RenderState::BLEND_SRC_ALPHA);
  38. _stateBlock->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA);
  39. for (unsigned int i = 0; i < _fontsCount; i++)
  40. {
  41. std::string s = "res/common/";
  42. s += _fontNames[i].c_str();
  43. s += ".gpb";
  44. Font* f = Font::create(s.c_str());
  45. _fonts.push_back(f);
  46. }
  47. _font = _fonts[0];
  48. _sampleString = std::string("Lorem ipsum dolor sit amet, \n" \
  49. "consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n" \
  50. "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.\n" \
  51. "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.\n" \
  52. "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
  53. // Create and listen to form.
  54. _form = Form::create("res/common/text.form");
  55. static_cast<Button*>(_form->getControl("fontButton"))->addListener(this, Control::Listener::CLICK);
  56. static_cast<Button*>(_form->getControl("wrapButton"))->addListener(this, Control::Listener::CLICK);
  57. static_cast<Button*>(_form->getControl("clipRectButton"))->addListener(this, Control::Listener::CLICK);
  58. static_cast<Button*>(_form->getControl("reverseButton"))->addListener(this, Control::Listener::CLICK);
  59. static_cast<Button*>(_form->getControl("switchClipRegionButton"))->addListener(this, Control::Listener::CLICK);
  60. static_cast<Button*>(_form->getControl("simpleAdvancedButton"))->addListener(this, Control::Listener::CLICK);
  61. static_cast<Button*>(_form->getControl("smallerButton"))->addListener(this, Control::Listener::CLICK);
  62. static_cast<Button*>(_form->getControl("biggerButton"))->addListener(this, Control::Listener::CLICK);
  63. static_cast<Button*>(_form->getControl("topLeftButton"))->addListener(this, Control::Listener::CLICK);
  64. static_cast<Button*>(_form->getControl("topCenterButton"))->addListener(this, Control::Listener::CLICK);
  65. static_cast<Button*>(_form->getControl("topRightButton"))->addListener(this, Control::Listener::CLICK);
  66. static_cast<Button*>(_form->getControl("centerLeftButton"))->addListener(this, Control::Listener::CLICK);
  67. static_cast<Button*>(_form->getControl("centerButton"))->addListener(this, Control::Listener::CLICK);
  68. static_cast<Button*>(_form->getControl("centerRightButton"))->addListener(this, Control::Listener::CLICK);
  69. static_cast<Button*>(_form->getControl("bottomLeftButton"))->addListener(this, Control::Listener::CLICK);
  70. static_cast<Button*>(_form->getControl("bottomCenterButton"))->addListener(this, Control::Listener::CLICK);
  71. static_cast<Button*>(_form->getControl("bottomRightButton"))->addListener(this, Control::Listener::CLICK);
  72. }
  73. void TextSample::update(float elapsedTime)
  74. {
  75. }
  76. void TextSample::render(float elapsedTime)
  77. {
  78. // Clear the screen.
  79. clear(CLEAR_COLOR_DEPTH, Vector4(0, 0, 0, 1), 1.0f, 0);
  80. // Draw the frame rate.
  81. char fps[5];
  82. sprintf(fps, "%u", getFrameRate());
  83. _fonts[0]->start();
  84. _fonts[0]->drawText(fps, 245, 5, Vector4(0, 0.5f, 1, 1), _size);
  85. if (_font != _fonts[0])
  86. _font->start();
  87. if (_simple)
  88. {
  89. // Sample simple versions of measureText, drawText.
  90. unsigned int w, h;
  91. _font->measureText(_sampleString.c_str(), _size, &w, &h);
  92. _font->drawText(_sampleString.c_str(), _viewport.x, _viewport.y, Vector4::fromColor(0xff0000ff), _size, _rightToLeft);
  93. _font->drawText("'", _viewport.x, _viewport.y, Vector4::fromColor(0x00ff00ff), _size);
  94. _font->drawText(".", _viewport.x, _viewport.y + h, Vector4::fromColor(0x00ff00ff), _size);
  95. _font->drawText("'", _viewport.x + w, _viewport.y, Vector4::fromColor(0x00ff00ff), _size);
  96. _font->drawText(".", _viewport.x + w, _viewport.y + h, Vector4::fromColor(0x00ff00ff), _size);
  97. }
  98. else
  99. {
  100. // Sample viewport versions.
  101. gameplay::Rectangle area;
  102. _font->measureText(_sampleString.c_str(), _viewport, _size, &area, _alignment, _wrap, _ignoreClip);
  103. _font->drawText(_sampleString.c_str(), _useViewport? _viewport : area, Vector4::fromColor(0xffffffff), _size, _alignment, _wrap, _rightToLeft);
  104. _font->drawText("'", _viewport.x, _viewport.y, Vector4::fromColor(0x00ff00ff), _size);
  105. _font->drawText(".", _viewport.x, _viewport.y + _viewport.height, Vector4::fromColor(0x00ff00ff), _size);
  106. _font->drawText("'", _viewport.x + _viewport.width, _viewport.y, Vector4::fromColor(0x00ff00ff), _size);
  107. _font->drawText(".", _viewport.x + _viewport.width, _viewport.y + _viewport.height, Vector4::fromColor(0x00ff00ff), _size);
  108. _font->drawText("'", area.x, area.y, Vector4::fromColor(0x0000ffff), _size);
  109. _font->drawText(".", area.x, area.y + area.height, Vector4::fromColor(0x0000ffff), _size);
  110. _font->drawText("'", area.x + area.width, area.y, Vector4::fromColor(0x0000ffff), _size);
  111. _font->drawText(".", area.x + area.width, area.y + area.height, Vector4::fromColor(0x0000ffff), _size);
  112. }
  113. if (_font != _fonts[0])
  114. {
  115. _font->finish();
  116. }
  117. _fonts[0]->finish();
  118. _form->draw();
  119. }
  120. void TextSample::touchEvent(Touch::TouchEvent event, int x, int y, unsigned int contactIndex)
  121. {
  122. _viewport.width = x - _viewport.x;
  123. _viewport.height = y - _viewport.y;
  124. }
  125. void TextSample::controlEvent(Control* control, EventType evt)
  126. {
  127. const char* id = control->getId();
  128. if (strcmp(id, "fontButton") == 0)
  129. {
  130. _fontIndex++;
  131. if (_fontIndex >= _fontsCount)
  132. {
  133. _fontIndex = 0;
  134. }
  135. _font = _fonts[_fontIndex];
  136. std::string s = "Font (" + _fontNames[_fontIndex] + ")";
  137. static_cast<Button*>(control)->setText(s.c_str());
  138. }
  139. else if (strcmp(id, "wrapButton") == 0)
  140. {
  141. _wrap = !_wrap;
  142. Button* wrapButton = static_cast<Button*>(control);
  143. if (_wrap)
  144. wrapButton->setText("Word Wrap (On)");
  145. else
  146. wrapButton->setText("Word Wrap (Off)");
  147. }
  148. else if (strcmp(id, "clipRectButton") == 0)
  149. {
  150. _ignoreClip = !_ignoreClip;
  151. Button* clipRectButton = static_cast<Button*>(control);
  152. if (_ignoreClip)
  153. clipRectButton->setText("Clipping (Off)");
  154. else
  155. clipRectButton->setText("Clipping (On)");
  156. }
  157. else if (strcmp(id, "reverseButton") == 0)
  158. {
  159. _rightToLeft = !_rightToLeft;
  160. Button* reverseButton = static_cast<Button*>(control);
  161. if (_rightToLeft)
  162. reverseButton->setText("Reverse Text (On)");
  163. else
  164. reverseButton->setText("Reverse Text (Off)");
  165. }
  166. else if (strcmp(id, "switchClipRegionButton") == 0)
  167. {
  168. _useViewport = !_useViewport;
  169. Button* switchClipButton = static_cast<Button*>(control);
  170. if (_useViewport)
  171. switchClipButton->setText("Clip Regions (Viewport)");
  172. else
  173. switchClipButton->setText("Clip Regions (Text Area)");
  174. }
  175. else if (strcmp(id, "simpleAdvancedButton") == 0)
  176. {
  177. _simple = !_simple;
  178. Button* simpleAdvancedButton = static_cast<Button*>(control);
  179. if (_simple)
  180. simpleAdvancedButton->setText("Font API (Simple)");
  181. else
  182. simpleAdvancedButton->setText("Font API (Advanced)");
  183. }
  184. else if (strcmp(id, "smallerButton") == 0)
  185. {
  186. if (_size > 12)
  187. {
  188. _size -= 2;
  189. Label* sizeLabel = static_cast<Label*>(_form->getControl("sizeLabel"));
  190. char s[20];
  191. sprintf(s, "Size (%u)", _size);
  192. sizeLabel->setText(s);
  193. }
  194. }
  195. else if (strcmp(id, "biggerButton") == 0)
  196. {
  197. _size += 2;
  198. Label* sizeLabel = static_cast<Label*>(_form->getControl("sizeLabel"));
  199. char s[20];
  200. sprintf(s, "Size (%u)", _size);
  201. sizeLabel->setText(s);
  202. }
  203. else if (strcmp(id, "topLeftButton") == 0)
  204. {
  205. _alignment = Font::ALIGN_TOP_LEFT;
  206. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  207. alignmentLabel->setText("Align (Top-Left)");
  208. }
  209. else if (strcmp(id, "topCenterButton") == 0)
  210. {
  211. _alignment = Font::ALIGN_TOP_HCENTER;
  212. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  213. alignmentLabel->setText("Align (Top-Center)");
  214. }
  215. else if (strcmp(id, "topRightButton") == 0)
  216. {
  217. _alignment = Font::ALIGN_TOP_RIGHT;
  218. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  219. alignmentLabel->setText("Align (Top-Right)");
  220. }
  221. else if (strcmp(id, "centerLeftButton") == 0)
  222. {
  223. _alignment = Font::ALIGN_VCENTER_LEFT;
  224. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  225. alignmentLabel->setText("Align (Center-Left)");
  226. }
  227. else if (strcmp(id, "centerButton") == 0)
  228. {
  229. _alignment = Font::ALIGN_VCENTER_HCENTER;
  230. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  231. alignmentLabel->setText("Align (Center)");
  232. }
  233. else if (strcmp(id, "centerRightButton") == 0)
  234. {
  235. _alignment = Font::ALIGN_VCENTER_RIGHT;
  236. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  237. alignmentLabel->setText("Align (Center-Right)");
  238. }
  239. else if (strcmp(id, "bottomLeftButton") == 0)
  240. {
  241. _alignment = Font::ALIGN_BOTTOM_LEFT;
  242. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  243. alignmentLabel->setText("Align (Bottom-Left)");
  244. }
  245. else if (strcmp(id, "bottomCenterButton") == 0)
  246. {
  247. _alignment = Font::ALIGN_BOTTOM_HCENTER;
  248. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  249. alignmentLabel->setText("Align (Bottom-Center)");
  250. }
  251. else if (strcmp(id, "bottomRightButton") == 0)
  252. {
  253. _alignment = Font::ALIGN_BOTTOM_RIGHT;
  254. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  255. alignmentLabel->setText("Align (Bottom-Right)");
  256. }
  257. }