[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" |
---|
[6105] | 41 | #include "input/InputBuffer.h" |
---|
[1505] | 42 | |
---|
| 43 | namespace 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() {} |
---|
[6105] | 58 | virtual void executed() {} |
---|
[1505] | 59 | virtual void exit() {} |
---|
| 60 | }; |
---|
| 61 | |
---|
[6105] | 62 | |
---|
| 63 | class _CoreExport Shell : virtual public OrxonoxClass, public OutputListener |
---|
[1505] | 64 | { |
---|
| 65 | public: |
---|
[6417] | 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); |
---|
[6105] | 81 | ~Shell(); |
---|
[1505] | 82 | |
---|
[6105] | 83 | void setConfigValues(); |
---|
[1747] | 84 | void commandHistoryOffsetChanged(); |
---|
| 85 | void commandHistoryLengthChanged(); |
---|
[1505] | 86 | |
---|
| 87 | void registerListener(ShellListener* listener); |
---|
| 88 | void unregisterListener(ShellListener* listener); |
---|
| 89 | |
---|
[1755] | 90 | inline InputBuffer* getInputBuffer() |
---|
| 91 | { return this->inputBuffer_; } |
---|
[1505] | 92 | |
---|
| 93 | void setCursorPosition(unsigned int cursor); |
---|
| 94 | inline unsigned int getCursorPosition() const |
---|
| 95 | { return this->inputBuffer_->getCursorPosition(); } |
---|
| 96 | |
---|
[6417] | 97 | inline const std::string& getInput() const |
---|
[1505] | 98 | { return this->inputBuffer_->get(); } |
---|
| 99 | |
---|
[6417] | 100 | typedef std::list<std::pair<std::string, LineType> > LineList; |
---|
| 101 | LineList::const_iterator getNewestLineIterator() const; |
---|
| 102 | LineList::const_iterator getEndIterator() const; |
---|
[1505] | 103 | |
---|
[6417] | 104 | void addOutput(const std::string& text, LineType type = None); |
---|
[6105] | 105 | void clearOutput(); |
---|
[1505] | 106 | |
---|
| 107 | inline unsigned int getNumLines() const |
---|
[6105] | 108 | { return this->outputLines_.size(); } |
---|
[1505] | 109 | inline unsigned int getScrollPosition() const |
---|
| 110 | { return this->scrollPosition_; } |
---|
| 111 | |
---|
[6105] | 112 | inline const std::string& getPromptPrefix() const { return this->promptPrefix_; } |
---|
| 113 | void setPromptPrefix(const std::string& str); |
---|
[1505] | 114 | |
---|
| 115 | private: |
---|
| 116 | Shell(const Shell& other); |
---|
| 117 | |
---|
| 118 | void addToHistory(const std::string& command); |
---|
[6417] | 119 | const std::string& getFromHistory() const; |
---|
[6105] | 120 | void clearInput(); |
---|
| 121 | // OutputListener |
---|
| 122 | void outputChanged(int level); |
---|
[1505] | 123 | |
---|
[6105] | 124 | void configureInputBuffer(); |
---|
| 125 | |
---|
| 126 | // InputBuffer callbacks |
---|
[1505] | 127 | void inputChanged(); |
---|
| 128 | void execute(); |
---|
[6105] | 129 | void hintAndComplete(); |
---|
[1505] | 130 | void backspace(); |
---|
[6105] | 131 | void deleteChar(); |
---|
| 132 | void cursorRight(); |
---|
| 133 | void cursorLeft(); |
---|
| 134 | void cursorEnd(); |
---|
| 135 | void cursorHome(); |
---|
| 136 | void historyUp(); |
---|
| 137 | void historyDown(); |
---|
| 138 | void historySearchUp(); |
---|
| 139 | void historySearchDown(); |
---|
| 140 | void scrollUp(); |
---|
| 141 | void scrollDown(); |
---|
[1505] | 142 | void exit(); |
---|
| 143 | |
---|
[6105] | 144 | template <void (ShellListener::*F)()> |
---|
| 145 | void updateListeners() |
---|
| 146 | { |
---|
| 147 | for (std::list<ShellListener*>::const_iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) |
---|
| 148 | ((*(it++))->*F)(); |
---|
| 149 | } |
---|
| 150 | |
---|
[1505] | 151 | std::list<ShellListener*> listeners_; |
---|
[6105] | 152 | InputBuffer* inputBuffer_; |
---|
| 153 | std::stringstream outputBuffer_; |
---|
| 154 | bool bFinishedLastLine_; |
---|
[6417] | 155 | LineList outputLines_; |
---|
| 156 | LineList::const_iterator scrollIterator_; |
---|
[6105] | 157 | unsigned int scrollPosition_; |
---|
| 158 | unsigned int historyPosition_; |
---|
[1792] | 159 | |
---|
[6105] | 160 | std::string promptPrefix_; |
---|
| 161 | const std::string consoleName_; |
---|
| 162 | const bool bScrollable_; |
---|
[3280] | 163 | |
---|
[6105] | 164 | // Config values |
---|
| 165 | unsigned int maxHistoryLength_; |
---|
| 166 | unsigned int historyOffset_; |
---|
| 167 | std::vector<std::string> commandHistory_; |
---|
| 168 | int softDebugLevel_; |
---|
[1505] | 169 | }; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | #endif /* _Shell_H__ */ |
---|