Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/unity_build/src/libraries/core/command/Shell.h @ 8524

Last change on this file since 8524 was 8524, checked in by rgrieder, 13 years ago

Fixed a serious problem with the debug levels for the Shells by introducing DevModeListener and moving the debug levels to the Shell again..

  • Property svn:eol-style set to native
File size: 9.1 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Reto Grieder
26 *
27 */
28
29/**
30    @defgroup ShellConsole Shell and console
31    @ingroup Command
32*/
33
34/**
35    @file
36    @ingroup Command ShellConsole
37    @brief Declaration of the Shell and ShellListener classes.
38*/
39
40#ifndef _Shell_H__
41#define _Shell_H__
42
43#include "core/CorePrereqs.h"
44
45#include <list>
46#include <sstream>
47#include <string>
48#include <vector>
49
50#include "util/OutputHandler.h"
51#include "core/Core.h"
52#include "core/OrxonoxClass.h"
53#include "core/input/InputBuffer.h"
54
55namespace orxonox
56{
57    /**
58        @brief An interface, used to get a notification if the state of the Shell changes.
59    */
60    class _CoreExport ShellListener
61    {
62        friend class Shell;
63
64        public:
65            virtual ~ShellListener() {}
66
67        private:
68            virtual void linesChanged() {}          ///< Called if all output-lines have changed
69            virtual void onlyLastLineChanged() {}   ///< Called if only the last output-line has changed
70            virtual void lineAdded() {}             ///< Called if a new line was added to the output
71            virtual void inputChanged() {}          ///< Called if the input has changed
72            virtual void cursorChanged() {}         ///< Called if the cursor in the input line has changed
73            virtual void executed() {}              ///< Called if a command from the input line was executed
74            virtual void exit() {}                  ///< Called if the console should be closed
75    };
76
77
78    /**
79        @brief The Shell is the logical component of the console that displays output to the user and allows him to enter commands.
80
81        The Shell gathers output sent from OutputHandler by inheriting from OutputListener.
82        The output-lines are stored in the shell, so they can be displayed in a graphical
83        console. Additionally the Shell has an InputBuffer which is needed by the user to
84        enter commands.
85
86        Different graphical consoles build upon a Shell, for example InGameConsole and IOConsole.
87    */
88    class _CoreExport Shell : virtual public OrxonoxClass, public OutputListener, public DevModeListener
89    {
90        public:
91            /// Defines the type of a line of text in the Shell - some types depend on the output level, others are of internal use.
92            enum LineType
93            {
94                TDebug  = OutputLevel::TDebug,
95                None    = OutputLevel::None,
96                Warning = OutputLevel::Warning,
97                Error   = OutputLevel::Error,
98                Info    = OutputLevel::Info,
99                Debug   = OutputLevel::Debug,
100                Verbose = OutputLevel::Verbose,
101                Ultra   = OutputLevel::Ultra,
102                Input,
103                Command,
104                Hint
105            };
106
107            Shell(const std::string& consoleName, bool bScrollable);
108            ~Shell();
109
110            void setConfigValues();
111            void commandHistoryOffsetChanged();
112            void commandHistoryLengthChanged();
113
114            void registerListener(ShellListener* listener);
115            void unregisterListener(ShellListener* listener);
116
117            /// Returns the input buffer which is needed by the user to enter text into the shell.
118            inline InputBuffer* getInputBuffer()
119                { return this->inputBuffer_; }
120
121            void setCursorPosition(unsigned int cursor);
122            /// Returns the current position of the cursor in the input buffer.
123            inline unsigned int getCursorPosition() const
124                { return this->inputBuffer_->getCursorPosition(); }
125
126            /// Returns the current content of the input buffer (the text which was entered by the user)
127            inline const std::string& getInput() const
128                { return this->inputBuffer_->get(); }
129
130            typedef std::list<std::pair<std::string, LineType> > LineList;
131            LineList::const_iterator getNewestLineIterator() const;
132            LineList::const_iterator getEndIterator() const;
133
134            void addOutput(const std::string& text, LineType type = None);
135            void clearOutput();
136
137            /// Returns the number of output-lines that are displayed in the shell.
138            inline unsigned int getNumLines() const
139                { return this->outputLines_.size(); }
140            /// Returns the line which is currently viewed if the user scrolls through the older output-lines in the shell.
141            inline unsigned int getScrollPosition() const
142                { return this->scrollPosition_; }
143
144            /// Returns the cache size that is actually used in CommandExecutor, but placed here for better readability of the config file.
145            static inline unsigned int getCacheSize()
146                { return Shell::cacheSize_s; }
147
148        private:
149            Shell(const Shell& other);
150
151            // DevModeListener
152            void devModeChanged(bool value);
153
154            void addToHistory(const std::string& command);
155            const std::string& getFromHistory() const;
156            void clearInput();
157            // OutputListener
158            void outputChanged(int level);
159
160            void configureInputBuffer();
161
162            // InputBuffer callbacks
163            void inputChanged();
164            void execute();
165            void hintAndComplete();
166            void backspace();
167            void deleteChar();
168            void cursorRight();
169            void cursorLeft();
170            void cursorEnd();
171            void cursorHome();
172            void historyUp();
173            void historyDown();
174            void historySearchUp();
175            void historySearchDown();
176            void scrollUp();
177            void scrollDown();
178            void exit();
179
180            /// Iterates through all registered @ref ShellListener "shell listeners" and calls the function @a F.
181            template <void (ShellListener::*F)()>
182            void updateListeners()
183            {
184                for (std::list<ShellListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
185                    ((*(it++))->*F)();
186            }
187
188            std::list<ShellListener*> listeners_;           ///< The registered shell listeners
189            InputBuffer*              inputBuffer_;         ///< The input buffer that is needed by the user to enter text
190            std::stringstream         outputBuffer_;        ///< The output buffer that is used to retrieve lines of output from OutputListener
191            bool                      bFinishedLastLine_;   ///< Stores if the most recent output-line was terminated with a line-break or if more output is expected for this line
192            LineList                  outputLines_;         ///< A list of all output-lines that were displayed in the shell so far
193            LineList::const_iterator  scrollIterator_;      ///< An iterator to an entry of the list of output-lines, changes if the user scrolls through the output in the shell
194            unsigned int              scrollPosition_;      ///< The number of the line that is currently being referenced by scrollIterator_
195            unsigned int              historyPosition_;     ///< If the user scrolls through the history of entered commands (stored in commandHistory_), this contains the currently viewed history entry
196
197            const std::string         consoleName_;         ///< The name of this shell - used to define the name of the soft-debug-level config-value
198            const bool                bScrollable_;         ///< If true, the user can scroll through the output-lines
199
200            // Config values
201            unsigned int              maxHistoryLength_;    ///< The maximum number of saved commands
202            unsigned int              historyOffset_;       ///< The command history is a circular buffer, this variable defines the current write-offset
203            std::vector<std::string>  commandHistory_;      ///< The history of commands that were entered by the user
204            int                       debugLevel_;          //!< The maximum level of output that is displayed in the shell (will be passed to OutputListener to filter output)
205            static unsigned int       cacheSize_s;          ///< The maximum cache size of the CommandExecutor - this is stored here for better readability of the config file and because CommandExecutor is no OrxonoxClass
206    };
207}
208
209#endif /* _Shell_H__ */
Note: See TracBrowser for help on using the repository browser.