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