Console.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 "core/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[in] event Event to process.
  41. */
  42. void Input(const VGears::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[in] text Text to write.
  77. * @param[in] colour Color for the text. Default is white.
  78. */
  79. void AddTextToOutput(
  80. const Ogre::String& text, const Ogre::ColourValue& colour = Ogre::ColourValue::White
  81. );
  82. /**
  83. * Executes a command in the console
  84. *
  85. * @param[in] command Command to execute.
  86. */
  87. void ExecuteCommand(const Ogre::String& command);
  88. /**
  89. * @todo Understand and document.
  90. */
  91. void ExecuteScript();
  92. /**
  93. * Autocompletes the current input.
  94. */
  95. void CompleteInput();
  96. /**
  97. * Clears the autocompletion list.
  98. */
  99. void ResetAutoCompletion();
  100. /**
  101. * Adds a console input to the console history.
  102. */
  103. void AddInputToHistory();
  104. /**
  105. * Sets a line from the history as the current input.
  106. */
  107. void SetInputLineFromHistory();
  108. /**
  109. * Logs a message to the console.
  110. *
  111. * @param[in] message The message to log.
  112. * @param[in] lml Log level for the message.
  113. * @param[in] maskDebug Indicates if the mesage is being printed to
  114. * the console or not.
  115. * @param[in] logName Name of the log.
  116. * @param[in] skipThisMessage If true, the message will not be logged.
  117. */
  118. virtual void messageLogged(
  119. const Ogre::String& message, Ogre::LogMessageLevel lml,
  120. bool maskDebug, const Ogre::String &logName, bool& skipThisMessage
  121. );
  122. private:
  123. /**
  124. * Loads the console input history.
  125. */
  126. void LoadHistory();
  127. /**
  128. * Saves the console input history.
  129. */
  130. void SaveHistory();
  131. /**
  132. * Adds a string to the console input history.
  133. */
  134. void AddToHistory(const Ogre::String& history);
  135. /**
  136. * Translates numpad key events to regular number events.
  137. *
  138. * @param[in] event Event to translate.
  139. */
  140. char TranslateNumpad(const VGears::Event& event);
  141. /**
  142. * The console width.
  143. */
  144. int console_width_;
  145. /**
  146. * The console height.
  147. */
  148. int console_height_;
  149. /**
  150. * The width of each line in the console.
  151. */
  152. unsigned int line_width_;
  153. /**
  154. * The width of each letter in the console.
  155. */
  156. int letter_width_;
  157. /**
  158. * Indicates if the console is being made visible.
  159. */
  160. bool to_visible_;
  161. /**
  162. * Indicates if the console is currently visible.
  163. */
  164. bool visible_;
  165. /**
  166. * Line height of the console.
  167. */
  168. float height_;
  169. /**
  170. * An ouptut line of the console.
  171. */
  172. struct OutputLine{
  173. /**
  174. * Text in the line.
  175. */
  176. Ogre::String text;
  177. /**
  178. * Color for the text in the line.
  179. */
  180. Ogre::ColourValue colour;
  181. /**
  182. * Time at which it was printed.
  183. */
  184. float time;
  185. };
  186. /**
  187. * Listof lines in the console.
  188. */
  189. std::list<OutputLine> output_line_;
  190. /**
  191. * Max number of lines in output list.
  192. */
  193. unsigned int max_output_line_;
  194. /**
  195. * The bottom line of the console, the active one.
  196. */
  197. unsigned int display_line_;
  198. /**
  199. * Currently typed text.
  200. */
  201. Ogre::String input_line_;
  202. /**
  203. * Position of the cursor in the active line.
  204. */
  205. unsigned int cursor_position_;
  206. /**
  207. * Cursor blink frequency, in seconds.
  208. */
  209. float cursor_blink_time_;
  210. /**
  211. * The console input history.
  212. */
  213. std::list<Ogre::String> history_;
  214. /**
  215. * Currently selected line of the input history.
  216. */
  217. int history_line_cycle_index_;
  218. /**
  219. * Maximum number of inputs to save to the history.
  220. */
  221. unsigned int max_history_size_;
  222. /**
  223. * List of strings available for autocompletion.
  224. */
  225. Ogre::StringVector auto_completition_;
  226. /**
  227. * Index of the currently displayed autocompletion option.
  228. */
  229. unsigned int auto_completition_line_;
  230. };