DialogsManager.h 12 KB

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