Subtle animation of Console background /
[supertux.git] / src / console.hpp
1 //  $Id: worldmap.hpp 3209 2006-04-02 22:19:22Z sommer $
2 // 
3 //  SuperTux - Console
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #ifndef SUPERTUX_CONSOLE_H
21 #define SUPERTUX_CONSOLE_H
22
23 #include <list>
24 #include <map>
25 #include <string>
26 #include <sstream>
27 #include <iostream>
28
29 class Console;
30 class ConsoleStreamBuffer;
31 class ConsoleCommandReceiver;
32 class DrawingContext;
33 class Surface;
34
35 class Console 
36 {
37   public:
38     Console();
39     ~Console();
40
41     static std::ostream input; /**< stream of keyboard input to send to the console. Do not forget to send std::endl or to flush the stream. */
42     static std::ostream output; /**< stream of characters to output to the console. Do not forget to send std::endl or to flush the stream. */
43
44     static void backspace(); /**< delete last character sent to the input stream */
45     static void scroll(int offset); /**< scroll console text up or down by @c offset lines */
46     static void autocomplete(); /**< autocomplete current command */
47
48     void draw(DrawingContext& context); /**< draw the console in a DrawingContext */
49     static void show(); /**< display the console */
50     static void hide(); /**< hide the console */
51     static void toggle(); /**< display the console if hidden, hide otherwise */
52
53     static bool hasFocus(); /**< true if characters should be sent to the console instead of their normal target */
54     static void registerCommand(std::string command, ConsoleCommandReceiver* ccr); /**< associate command with the given CCR */
55     static void unregisterCommand(std::string command, ConsoleCommandReceiver* ccr); /**< dissociate command and CCR */
56
57   protected:
58     static std::list<std::string> lines; /**< backbuffer of lines sent to the console */
59     static std::map<std::string, std::list<ConsoleCommandReceiver*> > commands; /**< map of console commands and a list of associated ConsoleCommandReceivers */
60     Surface* background; /**< console background image */
61     Surface* background2; /**< second, moving console background image */
62     static int backgroundOffset; /**< current offset of scrolling background image */
63     static int height; /**< height of the console in px */
64     static int offset; /**< decrease to scroll text up */
65     static bool focused; /**< true if console has input focus */
66
67     static ConsoleStreamBuffer inputBuffer; /**< stream buffer used by input stream */
68     static ConsoleStreamBuffer outputBuffer; /**< stream buffer used by output stream */
69
70     static void addLine(std::string s); /**< display a line in the console */
71     static void parse(std::string s); /**< react to a given command */
72
73     friend class ConsoleStreamBuffer;
74     static void flush(ConsoleStreamBuffer* buffer); /**< act upon changes in a ConsoleStreamBuffer */
75 };
76
77 class ConsoleStreamBuffer : public std::stringbuf 
78 {
79   public:
80     int sync() 
81     {
82       Console::flush(this);
83       return std::stringbuf::sync();
84     }
85 };
86
87 class ConsoleCommandReceiver
88 {
89   public:
90     ConsoleCommandReceiver()
91     {
92       //Console::registerCommandReceiver(this);
93     }
94     virtual bool consoleCommand(std::string command) = 0; /**< callback from Console; return false if command was unknown, true otherwise */
95     virtual ~ConsoleCommandReceiver()
96     {
97       //Console::unregisterCommandReceiver(this);
98     }
99 };
100
101 #endif