console.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SuperTux - Console
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
  17. #define HEADER_SUPERTUX_SUPERTUX_CONSOLE_HPP
  18. #include <list>
  19. #include <squirrel.h>
  20. #include <sstream>
  21. #include <vector>
  22. #include "util/currenton.hpp"
  23. #include "video/font_ptr.hpp"
  24. #include "video/surface_ptr.hpp"
  25. class Console;
  26. class ConsoleStreamBuffer;
  27. class DrawingContext;
  28. class ConsoleBuffer final : public Currenton<ConsoleBuffer>
  29. {
  30. public:
  31. static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
  32. static ConsoleStreamBuffer s_outputBuffer; /**< stream buffer used by output stream */
  33. public:
  34. std::list<std::string> m_lines; /**< backbuffer of lines sent to the console. New lines get added to front. */
  35. Console* m_console;
  36. public:
  37. ConsoleBuffer();
  38. void addLines(const std::string& s); /**< display a string of (potentially) multiple lines in the console */
  39. void addLine(const std::string& s); /**< display a line in the console */
  40. void flush(ConsoleStreamBuffer& buffer); /**< act upon changes in a ConsoleStreamBuffer */
  41. void set_console(Console* console);
  42. private:
  43. ConsoleBuffer(const ConsoleBuffer&) = delete;
  44. ConsoleBuffer& operator=(const ConsoleBuffer&) = delete;
  45. };
  46. class Console final : public Currenton<Console>
  47. {
  48. public:
  49. Console(ConsoleBuffer& buffer);
  50. ~Console() override;
  51. void on_buffer_change(int line_count);
  52. void input(char c); /**< add character to inputBuffer */
  53. void backspace(); /**< delete character left of inputBufferPosition */
  54. void eraseChar(); /**< delete character at inputBufferPosition */
  55. void enter(); /**< process and clear input stream */
  56. void scroll(int offset); /**< scroll console text up or down by @c offset lines */
  57. void autocomplete(); /**< autocomplete current command */
  58. void show_history(int offset); /**< move @c offset lines forward through history; Negative offset moves backward */
  59. void move_cursor(int offset); /**< move the cursor @c offset chars to the right; Negative offset moves backward; 0xFFFF moves to the end */
  60. void draw(DrawingContext& context) const; /**< draw the console in a DrawingContext */
  61. void update(float dt_sec);
  62. void show(); /**< display the console */
  63. void open(); /**< open the console for viewing for 6 seconds */
  64. void hide(); /**< hide the console */
  65. void toggle(); /**< display the console if hidden, hide otherwise */
  66. bool hasFocus() const; /**< true if characters should be sent to the console instead of their normal target */
  67. private:
  68. ConsoleBuffer& m_buffer;
  69. std::string m_inputBuffer; /**< string used for keyboard input */
  70. int m_inputBufferPosition; /**< position in inputBuffer before which to append new characters */
  71. std::list<std::string> m_history; /**< command history. New lines get added to back. */
  72. std::list<std::string>::iterator m_history_position; /**< item of command history that is currently displayed */
  73. SurfacePtr m_background; /**< console background image */
  74. SurfacePtr m_background2; /**< second, moving console background image */
  75. HSQUIRRELVM m_vm; /**< squirrel thread for the console (with custom roottable) */
  76. HSQOBJECT m_vm_object;
  77. int m_backgroundOffset; /**< current offset of scrolling background image */
  78. float m_height; /**< height of the console in px */
  79. float m_alpha;
  80. int m_offset; /**< decrease to scroll text up */
  81. bool m_focused; /**< true if console has input focus */
  82. FontPtr m_font;
  83. float m_stayOpen;
  84. void parse(const std::string& s); /**< react to a given command */
  85. /** ready a virtual machine instance, creating a new thread and loading default .nut files if needed */
  86. void ready_vm();
  87. /** execute squirrel script and output result */
  88. void execute_script(const std::string& s);
  89. bool consoleCommand(const std::string& command, const std::vector<std::string>& arguments); /**< process internal command; return false if command was unknown, true otherwise */
  90. private:
  91. Console(const Console&) = delete;
  92. Console & operator=(const Console&) = delete;
  93. };
  94. class ConsoleStreamBuffer final : public std::stringbuf
  95. {
  96. public:
  97. virtual int sync() override
  98. {
  99. int result = std::stringbuf::sync();
  100. if (ConsoleBuffer::current())
  101. ConsoleBuffer::current()->flush(*this);
  102. return result;
  103. }
  104. };
  105. #endif
  106. /* EOF */