Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/Shell.cc @ 1317

Last change on this file since 1317 was 1317, checked in by landauf, 16 years ago

changed some parts of InGameConsole to make it work with Shell, but I couldn't test it yet as there seems to be a bug in InputBuffer.

File size: 9.5 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 *      ...
26 *
27 */
28
29#include "Shell.h"
30#include "CommandExecutor.h"
31#include "CoreIncludes.h"
32#include "ConfigValueIncludes.h"
33
34#define SHELL_UPDATE_LISTENERS(function) \
35    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ++it) \
36        (*it)->function()
37
38namespace orxonox
39{
40    Shell::Shell()
41    {
42        RegisterRootObject(Shell);
43
44        this->scrollPosition_ = 0;
45        this->maxHistoryLength_ = 100;
46        this->historyPosition_ = 0;
47        this->historyOffset_ = 0;
48
49        this->clearLines();
50/*
51        this->inputBuffer_.registerListener(this, &Shell::inputChanged, true);
52        this->inputBuffer_.registerListener(this, &Shell::execute, '\r', false);
53        this->inputBuffer_.registerListener(this, &Shell::hintandcomplete, '\t', true);
54        this->inputBuffer_.registerListener(this, &Shell::backspace, '\b', true);
55        this->inputBuffer_.registerListener(this, &Shell::deletechar, OIS::KC_DELETE);
56        this->inputBuffer_.registerListener(this, &Shell::exit, (char)27, true);
57        this->inputBuffer_.registerListener(this, &Shell::cursor_right, OIS::KC_RIGHT);
58        this->inputBuffer_.registerListener(this, &Shell::cursor_left, OIS::KC_LEFT);
59        this->inputBuffer_.registerListener(this, &Shell::cursor_end, OIS::KC_END);
60        this->inputBuffer_.registerListener(this, &Shell::cursor_home, OIS::KC_HOME);
61        this->inputBuffer_.registerListener(this, &Shell::history_up, OIS::KC_UP);
62        this->inputBuffer_.registerListener(this, &Shell::history_down, OIS::KC_DOWN);
63        this->inputBuffer_.registerListener(this, &Shell::scroll_up, OIS::KC_PGUP);
64        this->inputBuffer_.registerListener(this, &Shell::scroll_down, OIS::KC_PGDOWN);
65*/
66        this->setConfigValues();
67    }
68
69    Shell& Shell::getInstance()
70    {
71        static Shell instance;
72        return instance;
73    }
74
75    void Shell::setConfigValues()
76    {
77        SetConfigValue(maxHistoryLength_, 100);
78        SetConfigValue(historyOffset_, 0);
79        SetConfigValueVector(commandHistory_, std::vector<std::string>(1, ""));
80
81        if (this->historyOffset_ >= this->maxHistoryLength_)
82            this->historyOffset_ = 0;
83
84        while (this->commandHistory_.size() > this->maxHistoryLength_)
85        {
86            unsigned int index = this->commandHistory_.size() - 1;
87            this->commandHistory_.erase(this->commandHistory_.begin() + index);
88            ModifyConfigValue(commandHistory_, remove, index);
89        }
90    }
91
92    void Shell::registerListener(ShellListener* listener)
93    {
94        this->listeners_.insert(this->listeners_.end(), listener);
95    }
96
97    void Shell::unregisterListener(ShellListener* listener)
98    {
99        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
100        {
101            if ((*it) == listener)
102                this->listeners_.erase(it++);
103            else
104                ++it;
105        }
106    }
107
108    void Shell::setCursorPosition(unsigned int cursor)
109    {
110        this->inputBuffer_.setCursorPosition(cursor);
111        SHELL_UPDATE_LISTENERS(cursorChanged);
112    }
113
114    void Shell::setInput(const std::string& input)
115    {
116        this->inputBuffer_.set(input);
117        this->inputChanged();
118    }
119
120    void Shell::addLine(const std::string& line, unsigned int level)
121    {
122        if ((*this->lines_.begin()) != "")
123        {
124            orxonox::OutputHandler::getOutStream().setOutputLevel(level) << std::endl;
125        }
126        orxonox::OutputHandler::getOutStream().setOutputLevel(level) << line << std::endl;
127    }
128
129    void Shell::clearLines()
130    {
131        this->lines_.clear();
132        this->lines_.insert(this->lines_.begin(), "");
133        this->scrollIterator_ = this->lines_.begin();
134
135        this->scrollPosition_ = 0;
136
137        SHELL_UPDATE_LISTENERS(linesChanged);
138    }
139
140    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
141    {
142        if (this->scrollPosition_)
143        {
144            return this->scrollIterator_;
145        }
146        else
147        {
148            if ((*this->lines_.begin()) == "" && this->lines_.size() > 1)
149                return (++this->lines_.begin());
150            else
151                return this->lines_.begin();
152        }
153    }
154
155    std::list<std::string>::const_iterator Shell::getEndIterator() const
156    {
157        return this->lines_.end();
158    }
159
160    void Shell::addToHistory(const std::string& command)
161    {
162        this->historyOffset_ = (this->historyOffset_ + 1) % this->maxHistoryLength_;
163        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
164        this->commandHistory_[this->historyOffset_] = command;
165        this->historyPosition_ = 0;
166    }
167
168    std::string Shell::getFromHistory() const
169    {
170        return this->commandHistory_[(this->historyOffset_ - this->historyPosition_) % this->maxHistoryLength_];
171    }
172
173    void Shell::outputChanged()
174    {
175        std::string output;
176        while (this->outputBuffer_.getLine(&output))
177        {
178            bool newline = false;
179            if ((*this->lines_.begin()) == "")
180                newline = true;
181
182            (*this->lines_.begin()) += output;
183
184            SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
185
186            this->lines_.insert(this->lines_.begin(), "");
187
188            if (this->scrollPosition_)
189                this->scrollPosition_++;
190            else
191                this->scrollIterator_ = this->lines_.begin();
192
193            if (newline)
194            {
195                SHELL_UPDATE_LISTENERS(lineAdded);
196            }
197        }
198
199        (*this->lines_.begin()) += output;
200        SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
201    }
202
203    void Shell::inputChanged()
204    {
205        SHELL_UPDATE_LISTENERS(inputChanged);
206        SHELL_UPDATE_LISTENERS(cursorChanged);
207    }
208
209    void Shell::execute()
210    {
211        if (CommandExecutor::execute(this->inputBuffer_.get()))
212            this->addLine(this->inputBuffer_.get(), 0);
213        else
214            this->addLine("Error: Can't execute \"" + this->inputBuffer_.get() + "\".", 1);
215
216        this->clear();
217    }
218
219    void Shell::hintandcomplete()
220    {
221        this->addLine(CommandExecutor::hint(this->inputBuffer_.get()), 0);
222        this->inputBuffer_.set(CommandExecutor::complete(this->inputBuffer_.get()));
223
224        this->inputChanged();
225    }
226
227    void Shell::backspace()
228    {
229        this->inputBuffer_.removeBehindCursor();
230        SHELL_UPDATE_LISTENERS(inputChanged);
231        SHELL_UPDATE_LISTENERS(cursorChanged);
232    }
233
234    void Shell::deletechar()
235    {
236        this->inputBuffer_.removeAtCursor();
237        SHELL_UPDATE_LISTENERS(inputChanged);
238    }
239
240    void Shell::clear()
241    {
242        this->inputBuffer_.clear();
243        SHELL_UPDATE_LISTENERS(inputChanged);
244        SHELL_UPDATE_LISTENERS(cursorChanged);
245    }
246
247    void Shell::cursor_right()
248    {
249        this->inputBuffer_.increaseCursor();
250        SHELL_UPDATE_LISTENERS(cursorChanged);
251    }
252
253    void Shell::cursor_left()
254    {
255        this->inputBuffer_.decreaseCursor();
256        SHELL_UPDATE_LISTENERS(cursorChanged);
257    }
258
259    void Shell::cursor_end()
260    {
261        this->inputBuffer_.setCursorToEnd();
262        SHELL_UPDATE_LISTENERS(cursorChanged);
263    }
264
265    void Shell::cursor_home()
266    {
267        this->inputBuffer_.setCursorToBegin();
268        SHELL_UPDATE_LISTENERS(cursorChanged);
269    }
270
271    void Shell::history_up()
272    {
273        if (this->historyPosition_ < (this->commandHistory_.size() - 1))
274        {
275            this->historyPosition_++;
276            this->inputBuffer_.set(this->getFromHistory());
277        }
278    }
279
280    void Shell::history_down()
281    {
282        if (this->historyPosition_ > 0)
283        {
284            this->historyPosition_++;
285            this->inputBuffer_.set(this->getFromHistory());
286        }
287    }
288
289    void Shell::scroll_up()
290    {
291        if (this->scrollIterator_ != this->lines_.end())
292        {
293            ++this->scrollIterator_;
294            ++this->scrollPosition_;
295
296            SHELL_UPDATE_LISTENERS(linesChanged);
297        }
298    }
299
300    void Shell::scroll_down()
301    {
302        if (this->scrollIterator_ != this->lines_.begin())
303        {
304            --this->scrollIterator_;
305            --this->scrollPosition_;
306
307            SHELL_UPDATE_LISTENERS(linesChanged);
308        }
309    }
310
311    void Shell::exit()
312    {
313        if (this->inputBuffer_.getSize() > 0)
314        {
315            this->clear();
316            return;
317        }
318
319        this->clear();
320        SHELL_UPDATE_LISTENERS(exit);
321    }
322}
Note: See TracBrowser for help on using the repository browser.