DialogsManager.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. {}
  83. /**
  84. * The widget where the message is displayed.
  85. */
  86. UiWidget* widget;
  87. /**
  88. * The window where the message is displayed.
  89. */
  90. UiWidget* window;
  91. /**
  92. * @todo Understand and document.
  93. */
  94. UiWidget* scissor;
  95. /**
  96. * The message text area.
  97. */
  98. UiTextArea* text_area;
  99. /**
  100. * The message cursor.
  101. */
  102. UiWidget* cursor;
  103. /**
  104. * The state of the message.
  105. */
  106. MessageState state;
  107. /**
  108. * The message window X coordinate.
  109. */
  110. int x = 0;
  111. /**
  112. * The message window Y coordinate.
  113. */
  114. int y = 0;
  115. /**
  116. * The message width, in pixels.
  117. */
  118. int w = 0;
  119. /**
  120. * The message height, in pixels.
  121. */
  122. int h = 0;
  123. /**
  124. * @todo Understand and document.
  125. */
  126. std::vector<ScriptId> sync;
  127. /**
  128. * Indicates if the text is 'clickable', i.e., if it requieres a keypress
  129. * to advance.
  130. */
  131. bool clickable;
  132. /**
  133. * Indicates if the dialog window is shown.
  134. */
  135. bool show_window;
  136. /**
  137. * Indicates if the choice selection cursor must be shown.
  138. */
  139. bool show_cursor;
  140. /**
  141. * Indicates if the dialog must be closed automatically.
  142. */
  143. bool auto_close;
  144. /**
  145. * @todo Understand and document.
  146. */
  147. float cursor_percent_y;
  148. /**
  149. * Y position of the cursor in the message box.
  150. */
  151. float cursor_y;
  152. /**
  153. * Selected choice line.
  154. */
  155. int cursor_row_selected;
  156. /**
  157. * Currently selected choice line.
  158. */
  159. int cursor_row_current;
  160. /**
  161. * First chooseable line.
  162. */
  163. int cursor_row_first;
  164. /**
  165. * Last chooseable line.
  166. */
  167. int cursor_row_last;
  168. };
  169. /**
  170. * The dialog manager.
  171. */
  172. class DialogsManager : public Ogre::Singleton<DialogsManager>{
  173. public:
  174. /**
  175. * Constructor.
  176. */
  177. DialogsManager();
  178. /**
  179. * Destructor.
  180. */
  181. virtual ~DialogsManager();
  182. /**
  183. * Initializes the dialog manager.
  184. */
  185. void Initialise();
  186. /**
  187. * Processes an input event.
  188. *
  189. * @param event[in] Event to process.
  190. */
  191. void Input(const QGears::Event& event);
  192. /**
  193. * Updates all the messages in the manager.
  194. */
  195. void Update();
  196. /**
  197. * Hides every dialog in the manager.
  198. */
  199. void Clear();
  200. /**
  201. * Opens a dialog.
  202. *
  203. * Equivalent to dialog_open in the field scripts.
  204. *
  205. * @param d_name[in] The dialog name. Can be used as ID.
  206. * @param x[in] X coordinate for the top left corner of the dialog.
  207. * @param y[in] Y coordinate for the top left corner of the dialog.
  208. * @param w[in] Width of the dialog, in pixels.
  209. * @param h[in] Height of the dialog, in pixels.
  210. */
  211. void OpenDialog(const char* d_name, int x, int y, int w, int h);
  212. /**
  213. * Sets the text of a dialog.
  214. *
  215. * Equivalent to dialog_set_text in the field scripts.
  216. *
  217. * @param d_name[in] Name of the dialog.
  218. * @param text[in] Text to set in the dialog.
  219. */
  220. void SetText(const char* d_name, const char* text);
  221. /**
  222. * Syncs the dialog and makes the script wait until it is closed.
  223. *
  224. * Equivalent to dialog_wait_for_close in the field scripts.
  225. *
  226. * @param d_name[in] Name of the dialog.
  227. * @return 1 if the dialog doesn't exist, -1 otherwise.
  228. */
  229. int Sync(const char* d_name);
  230. /**
  231. * Closes and hides a dialog window.
  232. *
  233. * Equivalent to dialog_close in the field scripts.
  234. *
  235. * @param dialog_name Name of the dialog to close.
  236. */
  237. void Hide(const char* d_name);
  238. /**
  239. * Sets a variable in a dialog
  240. *
  241. * @param d_name[in] Name of the dialog.
  242. * @param name[in] Name of the variable.
  243. * @param value[in] Value for the variable.
  244. */
  245. void SetVariable(
  246. const char* d_name, const char* name, const char* value
  247. );
  248. /**
  249. * Makes the dialog clickable or not.
  250. *
  251. * A clickable dialog presents a choice for the user that must be
  252. * selected with a cursor.
  253. *
  254. * @param d_name[in] Name of the dialog.
  255. * @param clickable[in] True to make the dialog clickable, false to
  256. * make it unclickable
  257. */
  258. void SetClickable(const char* d_name, const bool clickable);
  259. /**
  260. * Sets a choice sursor in the dialog.
  261. *
  262. * @param d_name[in] Name of the dialog.
  263. * @param first_row[in] First selectable line.
  264. * @param last_row[in] Last selectable line.
  265. */
  266. void SetCursor(
  267. const char* d_name, const int first_row, const int last_row
  268. );
  269. /**
  270. * Gets the cursor position in a dialog.
  271. *
  272. * @param d_name[in] Dialog name.
  273. * @return Position of the cursor in the dialog, including unselectable
  274. * lines (0-index). 0 if the dialog doesn't exist.
  275. */
  276. int GetCursor(const char* d_name) const;
  277. private:
  278. /**
  279. * Shows a message in a dialog.
  280. *
  281. * @param id[in] Message id.
  282. * @param x[in] X coordinate for the text in the dialog.
  283. * @param y[in] Y coordinate for the text in the dialog.
  284. * @param width[in] Width of the text, in pixels.
  285. * @param height[in] Height of the text, in pixels.
  286. * @todo Is this a window-limit-aware version of SetText?
  287. */
  288. void ShowMessage(
  289. const int id, const int x, const int y,
  290. const int width, const int height
  291. );
  292. /**
  293. * Closes and hides a message.
  294. *
  295. * @param id[in] ID of the message to close.
  296. */
  297. void HideMessage(const int id);
  298. /**
  299. * Retrieves a message Id from it's name.
  300. *
  301. * @param d_name[in] Dialog name.
  302. * @return The dialog ID. -1 if there is no dialog by that name.
  303. */
  304. int GetMessageId(const char* d_name) const;
  305. /**
  306. * Checks if a message is set to close automatically.
  307. *
  308. * @param id[in] Message ID.
  309. * @return True if the message is set to auto close, false otherwise.
  310. */
  311. bool AutoCloseCheck(const unsigned int id);
  312. /**
  313. * The limit area for message boxes.
  314. */
  315. UiWidget* limit_area_;
  316. /**
  317. * The list of messages.
  318. */
  319. std::vector<MessageData*> messages_;
  320. /**
  321. * Indicates if the 'next' button has been pressed.
  322. */
  323. bool next_pressed_;
  324. /**
  325. * Indicates if the 'next' button is being hold.
  326. */
  327. bool next_repeated_;
  328. /**
  329. * Indicates if the 'up' button has been pressed.
  330. */
  331. bool up_pressed_;
  332. /**
  333. * Indicates if the 'down' button has been pressed.
  334. */
  335. bool down_pressed_;
  336. };