TextSample.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. "arial18",
  10. "dynamic",
  11. "pirulen",
  12. "squarehead",
  13. "baroque",
  14. "custom",
  15. };
  16. TextSample::TextSample()
  17. : _form(NULL), _stateBlock(NULL), _scale(1.0f), _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[1]->start();
  84. _fonts[1]->drawText(fps, 245, 5, Vector4(0, 0.5f, 1, 1), _fonts[0]->getSize());
  85. _form->draw();
  86. unsigned int size = (float)_font->getSize() * _scale;
  87. if (_font != _fonts[1])
  88. _font->start();
  89. if (_simple)
  90. {
  91. // Sample simple versions of measureText, drawText.
  92. unsigned int w, h;
  93. _font->measureText(_sampleString.c_str(), size, &w, &h);
  94. _font->drawText(_sampleString.c_str(), _viewport.x, _viewport.y, Vector4::fromColor(0xff0000ff), size, _rightToLeft);
  95. _font->drawText("'", _viewport.x, _viewport.y, Vector4::fromColor(0x00ff00ff), size);
  96. _font->drawText(".", _viewport.x, _viewport.y + h, Vector4::fromColor(0x00ff00ff), size);
  97. _font->drawText("'", _viewport.x + w, _viewport.y, Vector4::fromColor(0x00ff00ff), size);
  98. _font->drawText(".", _viewport.x + w, _viewport.y + h, Vector4::fromColor(0x00ff00ff), size);
  99. }
  100. else
  101. {
  102. // Sample viewport versions.
  103. gameplay::Rectangle area;
  104. _font->measureText(_sampleString.c_str(), _viewport, size, &area, _alignment, _wrap, _ignoreClip);
  105. _font->drawText(_sampleString.c_str(), _useViewport? _viewport : area, Vector4::fromColor(0xffffffff), size, _alignment, _wrap, _rightToLeft);
  106. _font->drawText("'", _viewport.x, _viewport.y, Vector4::fromColor(0x00ff00ff), size);
  107. _font->drawText(".", _viewport.x, _viewport.y + _viewport.height, Vector4::fromColor(0x00ff00ff), size);
  108. _font->drawText("'", _viewport.x + _viewport.width, _viewport.y, Vector4::fromColor(0x00ff00ff), size);
  109. _font->drawText(".", _viewport.x + _viewport.width, _viewport.y + _viewport.height, Vector4::fromColor(0x00ff00ff), size);
  110. _font->drawText("'", area.x, area.y, Vector4::fromColor(0x0000ffff), size);
  111. _font->drawText(".", area.x, area.y + area.height, Vector4::fromColor(0x0000ffff), size);
  112. _font->drawText("'", area.x + area.width, area.y, Vector4::fromColor(0x0000ffff), size);
  113. _font->drawText(".", area.x + area.width, area.y + area.height, Vector4::fromColor(0x0000ffff), size);
  114. }
  115. if (_font != _fonts[1])
  116. {
  117. _font->finish();
  118. }
  119. _fonts[1]->finish();
  120. }
  121. void TextSample::touchEvent(Touch::TouchEvent event, int x, int y, unsigned int contactIndex)
  122. {
  123. _viewport.width = x - _viewport.x;
  124. _viewport.height = y - _viewport.y;
  125. }
  126. void TextSample::controlEvent(Control* control, EventType evt)
  127. {
  128. const char* id = control->getId();
  129. if (strcmp(id, "fontButton") == 0)
  130. {
  131. _fontIndex++;
  132. if (_fontIndex >= _fontsCount)
  133. {
  134. _fontIndex = 0;
  135. }
  136. _font = _fonts[_fontIndex];
  137. std::string s = "Font (" + _fontNames[_fontIndex] + ")";
  138. static_cast<Button*>(control)->setText(s.c_str());
  139. }
  140. else if (strcmp(id, "wrapButton") == 0)
  141. {
  142. _wrap = !_wrap;
  143. Button* wrapButton = static_cast<Button*>(control);
  144. if (_wrap)
  145. wrapButton->setText("Word Wrap (On)");
  146. else
  147. wrapButton->setText("Word Wrap (Off)");
  148. }
  149. else if (strcmp(id, "clipRectButton") == 0)
  150. {
  151. _ignoreClip = !_ignoreClip;
  152. Button* clipRectButton = static_cast<Button*>(control);
  153. if (_ignoreClip)
  154. clipRectButton->setText("Clipping (Off)");
  155. else
  156. clipRectButton->setText("Clipping (On)");
  157. }
  158. else if (strcmp(id, "reverseButton") == 0)
  159. {
  160. _rightToLeft = !_rightToLeft;
  161. Button* reverseButton = static_cast<Button*>(control);
  162. if (_rightToLeft)
  163. reverseButton->setText("Reverse Text (On)");
  164. else
  165. reverseButton->setText("Reverse Text (Off)");
  166. }
  167. else if (strcmp(id, "switchClipRegionButton") == 0)
  168. {
  169. _useViewport = !_useViewport;
  170. Button* switchClipButton = static_cast<Button*>(control);
  171. if (_useViewport)
  172. switchClipButton->setText("Clip Regions (Viewport)");
  173. else
  174. switchClipButton->setText("Clip Regions (Text Area)");
  175. }
  176. else if (strcmp(id, "simpleAdvancedButton") == 0)
  177. {
  178. _simple = !_simple;
  179. Button* simpleAdvancedButton = static_cast<Button*>(control);
  180. if (_simple)
  181. simpleAdvancedButton->setText("Font API (Simple)");
  182. else
  183. simpleAdvancedButton->setText("Font API (Advanced)");
  184. }
  185. else if (strcmp(id, "smallerButton") == 0)
  186. {
  187. if (_scale > 0.11f)
  188. {
  189. _scale -= 0.1f;
  190. Label* scaleLabel = static_cast<Label*>(_form->getControl("scaleLabel"));
  191. char s[20];
  192. sprintf(s, "Font Scale (%.1f)", _scale);
  193. scaleLabel->setText(s);
  194. }
  195. }
  196. else if (strcmp(id, "biggerButton") == 0)
  197. {
  198. _scale += 0.1f;
  199. Label* scaleLabel = static_cast<Label*>(_form->getControl("scaleLabel"));
  200. char s[20];
  201. sprintf(s, "Scale (%.1f)", _scale);
  202. scaleLabel->setText(s);
  203. }
  204. else if (strcmp(id, "topLeftButton") == 0)
  205. {
  206. _alignment = Font::ALIGN_TOP_LEFT;
  207. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  208. alignmentLabel->setText("Align (Top-Left)");
  209. }
  210. else if (strcmp(id, "topCenterButton") == 0)
  211. {
  212. _alignment = Font::ALIGN_TOP_HCENTER;
  213. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  214. alignmentLabel->setText("Align (Top-Center)");
  215. }
  216. else if (strcmp(id, "topRightButton") == 0)
  217. {
  218. _alignment = Font::ALIGN_TOP_RIGHT;
  219. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  220. alignmentLabel->setText("Align (Top-Right)");
  221. }
  222. else if (strcmp(id, "centerLeftButton") == 0)
  223. {
  224. _alignment = Font::ALIGN_VCENTER_LEFT;
  225. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  226. alignmentLabel->setText("Align (Center-Left)");
  227. }
  228. else if (strcmp(id, "centerButton") == 0)
  229. {
  230. _alignment = Font::ALIGN_VCENTER_HCENTER;
  231. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  232. alignmentLabel->setText("Align (Center)");
  233. }
  234. else if (strcmp(id, "centerRightButton") == 0)
  235. {
  236. _alignment = Font::ALIGN_VCENTER_RIGHT;
  237. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  238. alignmentLabel->setText("Align (Center-Right)");
  239. }
  240. else if (strcmp(id, "bottomLeftButton") == 0)
  241. {
  242. _alignment = Font::ALIGN_BOTTOM_LEFT;
  243. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  244. alignmentLabel->setText("Align (Bottom-Left)");
  245. }
  246. else if (strcmp(id, "bottomCenterButton") == 0)
  247. {
  248. _alignment = Font::ALIGN_BOTTOM_HCENTER;
  249. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  250. alignmentLabel->setText("Align (Bottom-Center)");
  251. }
  252. else if (strcmp(id, "bottomRightButton") == 0)
  253. {
  254. _alignment = Font::ALIGN_BOTTOM_RIGHT;
  255. Label* alignmentLabel = static_cast<Label*>(_form->getControl("alignmentLabel"));
  256. alignmentLabel->setText("Align (Bottom-Right)");
  257. }
  258. }