Console.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 <OgreColourValue.h>
  17. #include <OgreLog.h>
  18. #include <OgreSingleton.h>
  19. #include <OgreStringVector.h>
  20. #include <OIS/OIS.h>
  21. #include <list>
  22. #include <vector>
  23. #include "Event.h"
  24. /**
  25. * The game console.
  26. */
  27. class Console : public Ogre::Singleton<Console>, public Ogre::LogListener{
  28. public:
  29. /**
  30. * Constructor.
  31. */
  32. Console();
  33. /**
  34. * Destructor.
  35. */
  36. ~Console();
  37. /**
  38. * Processes an input event.
  39. *
  40. * @param event[in] Event to process.
  41. */
  42. void Input(const QGears::Event& event);
  43. /**
  44. * Updates the console.
  45. */
  46. void Update();
  47. /**
  48. * Redraws the console.
  49. */
  50. void UpdateDraw();
  51. /**
  52. * Notifies the console to update itself.
  53. */
  54. void UpdateNotification();
  55. /**
  56. * Processes a console resizing event.
  57. */
  58. void OnResize();
  59. /**
  60. * Shows the console.
  61. */
  62. void SetToVisible();
  63. /**
  64. * Hides the console.
  65. */
  66. void SetToHide();
  67. /**
  68. * Checks if the console is currently visible.
  69. *
  70. * @return TRue if the console is visible, false otherwise.
  71. */
  72. bool IsVisible() const;
  73. /**
  74. * Writes text to the console.
  75. *
  76. * @param text[in] Text to write.
  77. * @param color[in] Color for the text. Default is white.
  78. */
  79. void AddTextToOutput(
  80. const Ogre::String& text,
  81. const Ogre::ColourValue& colour = Ogre::ColourValue::White
  82. );
  83. /**
  84. * Executes a command in the console
  85. *
  86. * @param command[in] Command to execute.
  87. */
  88. void ExecuteCommand(const Ogre::String& command);
  89. /**
  90. * @todo Understand and document.
  91. */
  92. void ExecuteScript();
  93. /**
  94. * Autocompletes the current input.
  95. */
  96. void CompleteInput();
  97. /**
  98. * Clears the autocompletion list.
  99. */
  100. void ResetAutoCompletion();
  101. /**
  102. * Adds a console input to the console history.
  103. */
  104. void AddInputToHistory();
  105. /**
  106. * Sets a line from the history as the current input.
  107. */
  108. void SetInputLineFromHistory();
  109. /**
  110. * Logs a message to the console.
  111. *
  112. * @param message[in] The message to log.
  113. * @param lml[in] Log level for the message.
  114. * @param maskDebug Indicates if the mesage is beinng printed to the
  115. * console or not.
  116. * @param logName[in] Name of the log.
  117. * @param skipThisMessage[in] If true, the message will not be logged.
  118. */
  119. virtual void messageLogged(
  120. const Ogre::String& message, Ogre::LogMessageLevel lml,
  121. bool maskDebug, const Ogre::String &logName, bool& skipThisMessage
  122. );
  123. private:
  124. /**
  125. * Loads the console input history.
  126. */
  127. void LoadHistory();
  128. /**
  129. * Saves the console input history.
  130. */
  131. void SaveHistory();
  132. /**
  133. * Adds a string to the console input history.
  134. */
  135. void AddToHistory(const Ogre::String& history);
  136. /**
  137. * Translates numpad key events to regular number events.
  138. *
  139. * @param event[in] Event to translate.
  140. */
  141. char TranslateNumpad(const QGears::Event& event);
  142. /**
  143. * The console width.
  144. */
  145. int console_width_;
  146. /**
  147. * The console height.
  148. */
  149. int console_height_;
  150. /**
  151. * The width of each line in the console.
  152. */
  153. unsigned int line_width_;
  154. /**
  155. * The width of each letter in the console.
  156. */
  157. int letter_width_;
  158. /**
  159. * Indicates if the console is being made visible.
  160. */
  161. bool to_visible_;
  162. /**
  163. * Indicates if the console is currently visible.
  164. */
  165. bool visible_;
  166. /**
  167. * Line height of the console.
  168. */
  169. float height_;
  170. /**
  171. * An ouptut line of the console.
  172. */
  173. struct OutputLine{
  174. /**
  175. * Text in the line.
  176. */
  177. Ogre::String text;
  178. /**
  179. * Color for the text in the line.
  180. */
  181. Ogre::ColourValue colour;
  182. /**
  183. * Time at which it was printed.
  184. */
  185. float time;
  186. };
  187. /**
  188. * Listof lines in the console.
  189. */
  190. std::list<OutputLine> output_line_;
  191. /**
  192. * Max number of lines in output list.
  193. */
  194. unsigned int max_output_line_;
  195. /**
  196. * The bottom line of the console, the active one.
  197. */
  198. unsigned int display_line_;
  199. /**
  200. * Currently typed text.
  201. */
  202. Ogre::String input_line_;
  203. /**
  204. * Position of the cursor in the active line.
  205. */
  206. unsigned int cursor_position_;
  207. /**
  208. * Cursor blink frequency, in seconds.
  209. */
  210. float cursor_blink_time_;
  211. /**
  212. * The console input history.
  213. */
  214. std::list<Ogre::String> history_;
  215. /**
  216. * Currently selected line of the input history.
  217. */
  218. int history_line_cycle_index_;
  219. /**
  220. * Maximum number of inputs to save to the history.
  221. */
  222. unsigned int max_history_size_;
  223. /**
  224. * List of strings available for autocompletion.
  225. */
  226. Ogre::StringVector auto_completition_;
  227. /**
  228. * Index of the currently displayed autocompletion option.
  229. */
  230. unsigned int auto_completition_line_;
  231. };