Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/command/Shell.h @ 7284

Last change on this file since 7284 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 5.7 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#ifndef _Shell_H__
30#define _Shell_H__
31
32#include "core/CorePrereqs.h"
33
34#include <list>
35#include <sstream>
36#include <string>
37#include <vector>
38
39#include "util/OutputHandler.h"
40#include "core/OrxonoxClass.h"
41#include "core/input/InputBuffer.h"
42
43namespace orxonox
44{
45    class _CoreExport ShellListener
46    {
47        friend class Shell;
48
49        public:
50            virtual ~ShellListener() {}
51
52        private:
53            virtual void linesChanged() {}
54            virtual void onlyLastLineChanged() {}
55            virtual void lineAdded() {}
56            virtual void inputChanged() {}
57            virtual void cursorChanged() {}
58            virtual void executed() {}
59            virtual void exit() {}
60    };
61
62
63    class _CoreExport Shell : virtual public OrxonoxClass, public OutputListener
64    {
65        public:
66            enum LineType
67            {
68                None    = OutputLevel::None,
69                Warning = OutputLevel::Warning,
70                Error   = OutputLevel::Error,
71                Info    = OutputLevel::Info,
72                Debug   = OutputLevel::Debug,
73                Verbose = OutputLevel::Verbose,
74                Ultra   = OutputLevel::Ultra,
75                Input,
76                Command,
77                Hint
78            };
79
80            Shell(const std::string& consoleName, bool bScrollable);
81            ~Shell();
82
83            void setConfigValues();
84            void commandHistoryOffsetChanged();
85            void commandHistoryLengthChanged();
86
87            void registerListener(ShellListener* listener);
88            void unregisterListener(ShellListener* listener);
89
90            inline InputBuffer* getInputBuffer()
91                { return this->inputBuffer_; }
92
93            void setCursorPosition(unsigned int cursor);
94            inline unsigned int getCursorPosition() const
95                { return this->inputBuffer_->getCursorPosition(); }
96
97            inline const std::string& getInput() const
98                { return this->inputBuffer_->get(); }
99
100            typedef std::list<std::pair<std::string, LineType> > LineList;
101            LineList::const_iterator getNewestLineIterator() const;
102            LineList::const_iterator getEndIterator() const;
103
104            void addOutput(const std::string& text, LineType type = None);
105            void clearOutput();
106
107            inline unsigned int getNumLines() const
108                { return this->outputLines_.size(); }
109            inline unsigned int getScrollPosition() const
110                { return this->scrollPosition_; }
111
112            inline const std::string& getPromptPrefix() const { return this->promptPrefix_; }
113            void setPromptPrefix(const std::string& str);
114
115            static inline unsigned int getCacheSize()
116                { return Shell::cacheSize_s; }
117
118        private:
119            Shell(const Shell& other);
120
121            void addToHistory(const std::string& command);
122            const std::string& getFromHistory() const;
123            void clearInput();
124            // OutputListener
125            void outputChanged(int level);
126
127            void configureInputBuffer();
128
129            // InputBuffer callbacks
130            void inputChanged();
131            void execute();
132            void hintAndComplete();
133            void backspace();
134            void deleteChar();
135            void cursorRight();
136            void cursorLeft();
137            void cursorEnd();
138            void cursorHome();
139            void historyUp();
140            void historyDown();
141            void historySearchUp();
142            void historySearchDown();
143            void scrollUp();
144            void scrollDown();
145            void exit();
146
147            template <void (ShellListener::*F)()>
148            void updateListeners()
149            {
150                for (std::list<ShellListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
151                    ((*(it++))->*F)();
152            }
153
154            std::list<ShellListener*> listeners_;
155            InputBuffer*              inputBuffer_;
156            std::stringstream         outputBuffer_;
157            bool                      bFinishedLastLine_;
158            LineList                  outputLines_;
159            LineList::const_iterator  scrollIterator_;
160            unsigned int              scrollPosition_;
161            unsigned int              historyPosition_;
162
163            std::string               promptPrefix_;
164            const std::string         consoleName_;
165            const bool                bScrollable_;
166
167            // Config values
168            unsigned int              maxHistoryLength_;
169            unsigned int              historyOffset_;
170            std::vector<std::string>  commandHistory_;
171            int                       softDebugLevel_;
172            static unsigned int       cacheSize_s;
173    };
174}
175
176#endif /* _Shell_H__ */
Note: See TracBrowser for help on using the repository browser.