UiTextArea.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreHardwareVertexBuffer.h>
  17. #include <OgreRenderQueueListener.h>
  18. #include <OgreRoot.h>
  19. #include <Overlay/OgreUTFString.h>
  20. #include <tinyxml.h>
  21. #include "UiFont.h"
  22. #include "UiWidget.h"
  23. class UiSprite;
  24. /**
  25. * The textarea state.
  26. */
  27. enum TextState{
  28. /**
  29. * The text is displayed.
  30. */
  31. TS_SHOW_TEXT,
  32. /**
  33. * The text is scrolling.
  34. */
  35. TS_SCROLL_TEXT,
  36. /**
  37. * The text is paused waiting for input.
  38. */
  39. TS_PAUSE_OK,
  40. /**
  41. * The text is paused waiting for time to pass.
  42. */
  43. TS_PAUSE_TIME,
  44. /**
  45. * The textarea is done with.
  46. */
  47. TS_DONE,
  48. /**
  49. * The text is overflowing.
  50. */
  51. TS_OVERFLOW,
  52. /**
  53. * The text is changing pages.
  54. */
  55. TS_NEXT_PAGE,
  56. };
  57. struct TextChar{
  58. TextChar():
  59. char_code(0),
  60. colour(Ogre::ColourValue::White),
  61. skip(false),
  62. variable(""),
  63. variable_len(0),
  64. pause_ok(false),
  65. pause_time(0.0f),
  66. next_page(false),
  67. sprite(NULL),
  68. sprite_y(0)
  69. {}
  70. int char_code;
  71. Ogre::ColourValue colour;
  72. bool skip;
  73. Ogre::String variable;
  74. unsigned int variable_len;
  75. bool pause_ok;
  76. float pause_time;
  77. bool next_page;
  78. UiSprite* sprite;
  79. float sprite_y;
  80. };
  81. struct TextVariable
  82. {
  83. Ogre::String name;
  84. Ogre::UTFString value;
  85. };
  86. /**
  87. * An ingame textarea.
  88. *
  89. * It may be any window used to represent text: a diaog window, a menu panel,
  90. * a choice selection...
  91. */
  92. class UiTextArea : public UiWidget{
  93. public:
  94. /**
  95. * Creates a UiTextArea.
  96. *
  97. * @param name[in] Name for the textarea.
  98. */
  99. UiTextArea(const Ogre::String& name);
  100. /**
  101. * Creates a UiTextArea.
  102. *
  103. * @param name[in] Name for the textarea.
  104. * @param path_name[in] Path for the widget.
  105. * @param parent[in] Pointer to the widget that will parent the
  106. * UiTextArea.
  107. */
  108. UiTextArea(
  109. const Ogre::String& name, const Ogre::String& path_name,
  110. UiWidget* parent
  111. );
  112. /**
  113. * Destroys the UiTextArea.
  114. */
  115. virtual ~UiTextArea();
  116. /**
  117. * Sets default values for the UiTextArea.
  118. *
  119. * It's automatically called when the UiTextArea is created.
  120. */
  121. void Initialise();
  122. /**
  123. * Updates the UiTextArea.
  124. */
  125. virtual void Update();
  126. /**
  127. * Renders the UiTextArea on the screen.
  128. */
  129. virtual void Render();
  130. /**
  131. * Updates the UiTextArea.
  132. */
  133. virtual void UpdateTransformation();
  134. /**
  135. * Updates the UiTextArea geometry.
  136. */
  137. void UpdateGeometry();
  138. /**
  139. * Handles a keypress.
  140. */
  141. void InputPressed();
  142. /**
  143. * Handles a key being hold.
  144. */
  145. void InputRepeated();
  146. /**
  147. * Text alignment in the text area.
  148. */
  149. enum TextAlign{
  150. /**
  151. * Left aligned text.
  152. */
  153. LEFT,
  154. /**
  155. * Right aligned text.
  156. */
  157. RIGHT,
  158. /**
  159. * Horizontally centered text.
  160. */
  161. CENTER
  162. };
  163. /**
  164. * Sets the text alignment in the text area.
  165. *
  166. * @param align[in] The text alignment.
  167. */
  168. void SetTextAlign(const TextAlign align);
  169. /**
  170. * Sets the paddings in the text area.
  171. *
  172. * @param top[in] Padding from the top, in pixels.
  173. * @param right[in] Padding from the right, in pixels.
  174. * @param bottom[in] Padding from the bottom, in pixels.
  175. * @param left[in] Padding from the left, in pixels.
  176. */
  177. void SetPadding(
  178. const float top, const float right,
  179. const float bottom, const float left
  180. );
  181. /**
  182. * Sets the text from a string.
  183. *
  184. * @param text[in] Text to set.
  185. */
  186. void SetText(const Ogre::UTFString& text);
  187. /**
  188. * Sets the text from an XML node.
  189. *
  190. * @param text[in] XML node with the text to set.
  191. */
  192. void SetText(TiXmlNode* text);
  193. /**
  194. * Removes the text from the text area.
  195. */
  196. void TextClear();
  197. /**
  198. * @todo Understand and document.
  199. *
  200. * @param end[in] @todo Understand and document.
  201. */
  202. void RemoveSpritesFromText(const unsigned int end);
  203. /**
  204. * Set the font for the text.
  205. *
  206. * @param font[in] The font to set.
  207. */
  208. void SetFont(const Ogre::String& font);
  209. /**
  210. * Retrieves the font.
  211. *
  212. * @return The font used for the text in the textarea.
  213. */
  214. const UiFont* GetFont() const;
  215. /**
  216. * Sets the printing speed.
  217. *
  218. * @param speed[in] The text speed. -1 for instant text.
  219. * @todo Explain units or references.
  220. */
  221. void SetTextPrintSpeed(const float speed);
  222. /**
  223. * Sets the scroll duration.
  224. *
  225. * @param time[in] Time to scroll a line.
  226. * @todo Units or references.
  227. */
  228. void SetTextScrollTime(const float time);
  229. /**
  230. * Sets a variable in the text.
  231. *
  232. * @param name[in] Variable name.
  233. * @param value[in] Variable value.
  234. */
  235. void SetVariable(
  236. const Ogre::String& name, const Ogre::UTFString& value
  237. );
  238. /**
  239. * Gets the value of a variable in the text.
  240. *
  241. * @param name[in] The variable name.
  242. * @return The variable value, in string format, or an empty string if
  243. * there is no such variable.
  244. */
  245. Ogre::UTFString GetVariable(const Ogre::String& name) const;
  246. /**
  247. * Checks the text state.
  248. *
  249. * @return The text state.
  250. */
  251. TextState GetTextState() const;
  252. /**
  253. * Gets the text limit.
  254. *
  255. * @return The text limit.
  256. * @todo The limit is the max number of letters per text area? Does it
  257. * include multiple pages?
  258. */
  259. float GetTextLimit() const;
  260. /**
  261. * Gets the text size.
  262. *
  263. * @return The number of characters in the text.
  264. */
  265. unsigned int GetTextSize() const;
  266. /**
  267. * Retrieves the pause time of the text.
  268. *
  269. * @return The time the text must still remain paused, in second.
  270. */
  271. float GetPauseTime() const;
  272. private:
  273. /**
  274. * Retrieves the text width.
  275. *
  276. * @return The text width, in pixels.
  277. */
  278. float GetTextWidth() const;
  279. /**
  280. * Prepares text from a XML node.
  281. *
  282. * @param node[in] The XML node to get the text from.
  283. * @param colour[in] The text colour.
  284. * @todo Does this call setText?
  285. */
  286. void PrepareTextFromNode(
  287. TiXmlNode* node, const Ogre::ColourValue& colour
  288. );
  289. /**
  290. * Prepares text from a string.
  291. *
  292. * @param text[in] The text to prepare.
  293. * @param colour[in] The text colour.
  294. * @todo Does this call setText?
  295. */
  296. void PrepareTextFromText(
  297. const Ogre::UTFString& text, const Ogre::ColourValue& colour
  298. );
  299. /**
  300. * Constructor.
  301. */
  302. UiTextArea();
  303. /**
  304. * Creates a vertex buffer for the textarea.
  305. */
  306. void CreateVertexBuffer();
  307. /**
  308. * Destroys a vertex buffer for the textarea.
  309. */
  310. void DestroyVertexBuffer();
  311. /**
  312. * Material for the text area.
  313. */
  314. Ogre::MaterialPtr material_;
  315. /**
  316. * The scene manager.
  317. */
  318. Ogre::SceneManager* scene_manager_;
  319. /**
  320. * The render system.
  321. */
  322. Ogre::RenderSystem* render_system_;
  323. /**
  324. * Max letter per textarea.
  325. */
  326. unsigned int max_letters_;
  327. /**
  328. * The render operation.
  329. */
  330. Ogre::RenderOperation render_operation_;
  331. /**
  332. * The text area vertext buffer.
  333. */
  334. Ogre::HardwareVertexBufferSharedPtr vertex_buffer_;
  335. /**
  336. * The font for the text.
  337. */
  338. UiFont* font_;
  339. /**
  340. * The text alignment.
  341. */
  342. TextAlign text_align_;
  343. /**
  344. * The text.
  345. */
  346. std::vector<TextChar> text_;
  347. /**
  348. * The text limit.
  349. */
  350. float text_limit_;
  351. /**
  352. * The text printing speed.
  353. */
  354. float text_print_speed_;
  355. /**
  356. * @todo Understand and document.
  357. */
  358. float text_print_speed_mod_;
  359. /**
  360. * The state of the text.
  361. */
  362. TextState text_state_;
  363. /**
  364. * Variables in the text.
  365. */
  366. std::vector<TextVariable> text_variable_;
  367. /**
  368. * Time to scroll the text.
  369. */
  370. float text_scroll_time_;
  371. /**
  372. * @todo Understand and document.
  373. */
  374. float text_y_offset_;
  375. /**
  376. * @todo Understand and document.
  377. */
  378. float text_y_offset_target_;
  379. /**
  380. * The time to pause the text.
  381. */
  382. float pause_time_;
  383. /**
  384. * @todo Understand and document.
  385. */
  386. unsigned int next_page_start_;
  387. /**
  388. * Indicates if the 'next' button has been pressed.
  389. */
  390. bool next_pressed_;
  391. /**
  392. * Indicates if the 'next' button is being held down.
  393. */
  394. bool next_repeated_;
  395. /**
  396. * The top padding.
  397. */
  398. float padding_top_;
  399. /**
  400. * The right padding.
  401. */
  402. float padding_right_;
  403. /**
  404. * The bottom padding.
  405. */
  406. float padding_bottom_;
  407. /**
  408. * The left padding.
  409. */
  410. float padding_left_;
  411. /**
  412. * @todo Understand and document.
  413. */
  414. bool timer_;
  415. /**
  416. * @todo Understand and document.
  417. */
  418. int timer_time_;
  419. };