TextSample.cpp 11 KB

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