DialogsManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 <OgreSingleton.h>
  17. #include "UiManager.h"
  18. #include "UiTextArea.h"
  19. /**
  20. * Possible states for messages.
  21. */
  22. enum MessageState{
  23. /**
  24. * The message is now closed.
  25. */
  26. MS_CLOSED,
  27. /**
  28. * The message window is shown, but there is no text.
  29. */
  30. MS_SHOW_WINDOW,
  31. /**
  32. * The message window and text are shown.
  33. */
  34. MS_SHOW_TEXT,
  35. /**
  36. * The message window is open.
  37. */
  38. MS_OPENED,
  39. /**
  40. * The message window has been hidden.
  41. */
  42. MS_HIDE_WINDOW
  43. };
  44. /**
  45. * Styles for message boxes.
  46. */
  47. enum MessageStyle{
  48. /**
  49. * Regular message box, solid background.
  50. */
  51. MSL_SOLID,
  52. /**
  53. * Translucent message box.
  54. */
  55. MSL_TRANSPARENT,
  56. /**
  57. * No message box.
  58. */
  59. MSL_NONE
  60. };
  61. /**
  62. * Message data.
  63. */
  64. struct MessageData{
  65. MessageData():
  66. widget(NULL),
  67. window(NULL),
  68. scissor(NULL),
  69. text_area(NULL),
  70. cursor(NULL),
  71. state(MS_CLOSED),
  72. clickable(true),
  73. show_window(true),
  74. show_cursor(false),
  75. auto_close(false),
  76. cursor_percent_y(0),
  77. cursor_y(0),
  78. cursor_row_selected(0),
  79. cursor_row_current(0),
  80. cursor_row_first(0),
  81. cursor_row_last(0),
  82. closeable(true),
  83. visible(true),
  84. translucent(false),
  85. timer(false),
  86. numeric(false)
  87. {}
  88. /**
  89. * The widget where the message is displayed.
  90. */
  91. UiWidget* widget;
  92. /**
  93. * The window where the message is displayed.
  94. */
  95. UiWidget* window;
  96. /**
  97. * @todo Understand and document.
  98. */
  99. UiWidget* scissor;
  100. /**
  101. * The message text area.
  102. */
  103. UiTextArea* text_area;
  104. /**
  105. * The message cursor.
  106. */
  107. UiWidget* cursor;
  108. /**
  109. * The state of the message.
  110. */
  111. MessageState state;
  112. /**
  113. * The message window X coordinate.
  114. */
  115. int x = 0;
  116. /**
  117. * The message window Y coordinate.
  118. */
  119. int y = 0;
  120. /**
  121. * The message width, in pixels.
  122. */
  123. int w = 0;
  124. /**
  125. * The message height, in pixels.
  126. */
  127. int h = 0;
  128. /**
  129. * @todo Understand and document.
  130. */
  131. std::vector<ScriptId> sync;
  132. /**
  133. * Indicates if the text is 'clickable', i.e., if it requieres a keypress
  134. * to advance.
  135. */
  136. bool clickable;
  137. /**
  138. * Indicates if the dialog window is shown.
  139. */
  140. bool show_window;
  141. /**
  142. * Indicates if the choice selection cursor must be shown.
  143. */
  144. bool show_cursor;
  145. /**
  146. * Indicates if the dialog must be closed automatically.
  147. */
  148. bool auto_close;
  149. /**
  150. * @todo Understand and document.
  151. */
  152. float cursor_percent_y;
  153. /**
  154. * Y position of the cursor in the message box.
  155. */
  156. float cursor_y;
  157. /**
  158. * Selected choice line.
  159. */
  160. int cursor_row_selected;
  161. /**
  162. * Currently selected choice line.
  163. */
  164. int cursor_row_current;
  165. /**
  166. * First chooseable line.
  167. */
  168. int cursor_row_first;
  169. /**
  170. * Last chooseable line.
  171. */
  172. int cursor_row_last;
  173. /**
  174. * Indicates if the window is closeable.
  175. */
  176. bool closeable;
  177. /**
  178. * Indicates if the window background is visible.
  179. */
  180. bool visible;
  181. /**
  182. * Indicates if the window background is translucent.
  183. */
  184. bool translucent;
  185. /**
  186. * Indicates if the window is for a timer.
  187. */
  188. bool timer;
  189. /**
  190. * Indicates if the window background is for a number.
  191. */
  192. bool numeric;
  193. };
  194. /**
  195. * The dialog manager.
  196. */
  197. class DialogsManager : public Ogre::Singleton<DialogsManager>{
  198. public:
  199. /**
  200. * Constructor.
  201. */
  202. DialogsManager();
  203. /**
  204. * Destructor.
  205. */
  206. virtual ~DialogsManager();
  207. /**
  208. * Initializes the dialog manager.
  209. */
  210. void Initialise();
  211. /**
  212. * Processes an input event.
  213. *
  214. * @param[in] event Event to process.
  215. */
  216. void Input(const VGears::Event& event);
  217. /**
  218. * Updates all the messages in the manager.
  219. */
  220. void Update();
  221. /**
  222. * Hides every dialog in the manager.
  223. */
  224. void Clear();
  225. /**
  226. * Opens a dialog.
  227. *
  228. * Equivalent to dialog_open in the field scripts.
  229. *
  230. * @param[in] d_name The dialog name. Can be used as ID.
  231. * @param[in] x X coordinate for the top left corner of the dialog.
  232. * @param[in] y Y coordinate for the top left corner of the dialog.
  233. * @param[in] w Width of the dialog, in pixels.
  234. * @param[in] h Height of the dialog, in pixels.
  235. */
  236. void OpenDialog(const char* d_name, int x, int y, int w, int h);
  237. /**
  238. * Sets the text of a dialog.
  239. *
  240. * Equivalent to dialog_set_text in the field scripts.
  241. *
  242. * @param[in] d_name Name of the dialog.
  243. * @param[in] text Text to set in the dialog.
  244. */
  245. void SetText(const char* d_name, const char* text);
  246. /**
  247. * Sets or unsets a dialog window as numeric
  248. *
  249. * @param[in] d_name Name of the dialog.
  250. * @param[in] numeric True to make it numeric, false otherwise.
  251. * @param[in] timer True to use the window for times, false for regular numbers. If used
  252. * for timer, the window will be used when calling {@see UpdateTimer}
  253. */
  254. void SetNumeric(const char* d_name, const bool numeric, const bool timer);
  255. /**
  256. * Updates the timer.
  257. *
  258. * Displays the formatted time in the timer window. To do so, a timer window must have been
  259. * set as timer with {@see SetNumeric}
  260. */
  261. void UpdateTimer(const unsigned int seconds);
  262. /**
  263. * Sets the window mode.
  264. *
  265. * @param[in] d_name Name of the dialog.
  266. * @param[in] bg Background mode. 1: No background/border. 2: Translucent background.
  267. * Anything else: Normal.
  268. * @param[in] closeable True if the window can be closed manually, false to lock it.
  269. */
  270. void SetMode(const char* d_name, const int bg, const bool closeable);
  271. /**
  272. * Syncs the dialog and makes the script wait until it is closed.
  273. *
  274. * Equivalent to dialog_wait_for_close in the field scripts.
  275. *
  276. * @param[in] d_name Name of the dialog.
  277. * @return 1 if the dialog doesn't exist, -1 otherwise.
  278. */
  279. int Sync(const char* d_name);
  280. /**
  281. * Closes and hides a dialog window.
  282. *
  283. * Equivalent to dialog_close in the field scripts.
  284. *
  285. * @param[in] d_name Name of the dialog to close.
  286. */
  287. void Hide(const char* d_name);
  288. /**
  289. * Sets a variable in a dialog
  290. *
  291. * @param[in] d_name Name of the dialog.
  292. * @param[in] name Name of the variable.
  293. * @param[in] value Value for the variable.
  294. */
  295. void SetVariable(
  296. const char* d_name, const char* name, const char* value
  297. );
  298. /**
  299. * Makes the dialog clickable or not.
  300. *
  301. * A clickable dialog presents a choice for the user that must be
  302. * selected with a cursor.
  303. *
  304. * @param[in] d_name Name of the dialog.
  305. * @param[in] clickable True to make the dialog clickable, false to
  306. * make it unclickable
  307. */
  308. void SetClickable(const char* d_name, const bool clickable);
  309. /**
  310. * Sets a choice sursor in the dialog.
  311. *
  312. * @param[in] d_name Name of the dialog.
  313. * @param[in] first_row First selectable line.
  314. * @param[in] last_row Last selectable line.
  315. */
  316. void SetCursor(
  317. const char* d_name, const int first_row, const int last_row
  318. );
  319. /**
  320. * Gets the cursor position in a dialog.
  321. *
  322. * @param[in] d_name Dialog name.
  323. * @return Position of the cursor in the dialog, including unselectable
  324. * lines (0-index). 0 if the dialog doesn't exist.
  325. */
  326. int GetCursor(const char* d_name) const;
  327. /**
  328. * Sets the map name from a text ID.
  329. *
  330. * @param[in] text_id The ID of the text to set as map name.
  331. */
  332. void ScriptSetMapName(const char* text_id);
  333. /**
  334. * Sets the map name from a text ID.
  335. *
  336. * @param[in] name The new map name.
  337. */
  338. void SetMapName(std::string name);
  339. /**
  340. * Retrieves the current map name.
  341. *
  342. * @return The map name.
  343. */
  344. std::string GetMapName();
  345. private:
  346. /**
  347. * Shows a message in a dialog.
  348. *
  349. * @param[in] id Message id.
  350. * @param[in] x X coordinate for the text in the dialog.
  351. * @param[in] y Y coordinate for the text in the dialog.
  352. * @param[in] width Width of the text, in pixels.
  353. * @param[in] height Height of the text, in pixels.
  354. * @todo Is this a window-limit-aware version of SetText?
  355. */
  356. void ShowMessage(
  357. const int id, const int x, const int y,
  358. const int width, const int height
  359. );
  360. /**
  361. * Closes and hides a message.
  362. *
  363. * @param[in] id ID of the message to close.
  364. */
  365. void HideMessage(const int id);
  366. /**
  367. * Retrieves a message Id from it's name.
  368. *
  369. * @param[in] d_name Dialog name.
  370. * @return The dialog ID. -1 if there is no dialog by that name.
  371. */
  372. int GetMessageId(const char* d_name) const;
  373. /**
  374. * Checks if a message is set to close automatically.
  375. *
  376. * @param[in] id Message ID.
  377. * @return True if the message is set to auto close, false otherwise.
  378. */
  379. bool AutoCloseCheck(const unsigned int id);
  380. /**
  381. * The limit area for message boxes.
  382. */
  383. UiWidget* limit_area_;
  384. /**
  385. * The list of messages.
  386. */
  387. std::vector<MessageData*> messages_;
  388. /**
  389. * Indicates if the 'next' button has been pressed.
  390. */
  391. bool next_pressed_;
  392. /**
  393. * Indicates if the 'next' button is being hold.
  394. */
  395. bool next_repeated_;
  396. /**
  397. * Indicates if the 'up' button has been pressed.
  398. */
  399. bool up_pressed_;
  400. /**
  401. * Indicates if the 'down' button has been pressed.
  402. */
  403. bool down_pressed_;
  404. /**
  405. * Current map name.
  406. */
  407. std::string map_name_;
  408. /**
  409. * The ID of the window that contains the timer, is any
  410. */
  411. int timer_window_id_;
  412. /**
  413. * The number of seconds in the timer.
  414. *
  415. * Stored solely to not re-set the text if the time is the same.
  416. */
  417. unsigned int timer_seconds_;
  418. };