Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gui/src/core/Shell.cc @ 1697

Last change on this file since 1697 was 1641, checked in by rgrieder, 16 years ago

some de-bugging
added enum for joy stick buttons
some more little fixes

  • Property svn:eol-style set to native
File size: 11.0 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#include "Core.h"
34#include "ConsoleCommand.h"
35
36#define SHELL_UPDATE_LISTENERS(function) \
37    for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); ) \
38        (*(it++))->function()
39
40namespace orxonox
41{
42    SetConsoleCommand(Shell, clearShell, true);
43    SetConsoleCommand(Shell, history, true);
44
45    Shell::Shell()
46    {
47        RegisterRootObject(Shell);
48
49        this->scrollPosition_ = 0;
50        this->maxHistoryLength_ = 100;
51        this->historyPosition_ = 0;
52        this->historyOffset_ = 0;
53        this->finishedLastLine_ = true;
54        this->bAddOutputLevel_ = false;
55
56        this->clearLines();
57
58        this->inputBuffer_ = new InputBuffer();
59        this->configureInputBuffer();
60
61        this->outputBuffer_.registerListener(this);
62
63        this->setConfigValues();
64    }
65
66    Shell::~Shell()
67    {
68        if (this->inputBuffer_)
69            delete this->inputBuffer_;
70    }
71
72    Shell& Shell::createShell()
73    {
74        int level = Core::getSoftDebugLevel(OutputHandler::LD_Shell);
75        Core::setSoftDebugLevel(OutputHandler::LD_Shell, -1);
76        static Shell instance;
77        Core::setSoftDebugLevel(OutputHandler::LD_Shell, level);
78        return instance;
79    }
80
81    Shell& Shell::getInstance()
82    {
83        static Shell& instance = createShell();
84        return instance;
85    }
86
87    void Shell::setConfigValues()
88    {
89        SetConfigValue(maxHistoryLength_, 100);
90        SetConfigValue(historyOffset_, 0);
91        SetConfigValueVector(commandHistory_, std::vector<std::string>());
92
93        if (this->historyOffset_ >= this->maxHistoryLength_)
94            this->historyOffset_ = 0;
95
96        while (this->commandHistory_.size() > this->maxHistoryLength_)
97        {
98            unsigned int index = this->commandHistory_.size() - 1;
99            this->commandHistory_.erase(this->commandHistory_.begin() + index);
100            ModifyConfigValue(commandHistory_, remove, index);
101        }
102    }
103
104    void Shell::configureInputBuffer()
105    {
106        this->inputBuffer_->registerListener(this, &Shell::inputChanged, true);
107        this->inputBuffer_->registerListener(this, &Shell::execute, '\r', false);
108        this->inputBuffer_->registerListener(this, &Shell::hintandcomplete, '\t', true);
109        this->inputBuffer_->registerListener(this, &Shell::backspace, '\b', true);
110        this->inputBuffer_->registerListener(this, &Shell::deletechar, KeyCode::Delete);
111        this->inputBuffer_->registerListener(this, &Shell::exit, (char)27, true);
112        this->inputBuffer_->registerListener(this, &Shell::cursor_right, KeyCode::Right);
113        this->inputBuffer_->registerListener(this, &Shell::cursor_left, KeyCode::Left);
114        this->inputBuffer_->registerListener(this, &Shell::cursor_end, KeyCode::End);
115        this->inputBuffer_->registerListener(this, &Shell::cursor_home, KeyCode::Home);
116        this->inputBuffer_->registerListener(this, &Shell::history_up, KeyCode::Up);
117        this->inputBuffer_->registerListener(this, &Shell::history_down, KeyCode::Down);
118        this->inputBuffer_->registerListener(this, &Shell::scroll_up, KeyCode::PageUp);
119        this->inputBuffer_->registerListener(this, &Shell::scroll_down, KeyCode::PageDown);
120    }
121
122    void Shell::clearShell()
123    {
124        Shell::getInstance().clearLines();
125    }
126
127    void Shell::history()
128    {
129        Shell& instance = Shell::getInstance();
130
131        for (int i = instance.historyOffset_; i < (int)instance.commandHistory_.size(); ++i)
132            instance.addLine(instance.commandHistory_[i], -1);
133        for (int i =  0; i < (int)instance.historyOffset_; ++i)
134            instance.addLine(instance.commandHistory_[i], -1);
135    }
136
137    void Shell::registerListener(ShellListener* listener)
138    {
139        this->listeners_.insert(this->listeners_.end(), listener);
140    }
141
142    void Shell::unregisterListener(ShellListener* listener)
143    {
144        for (std::list<ShellListener*>::iterator it = this->listeners_.begin(); it != this->listeners_.end(); )
145        {
146            if ((*it) == listener)
147                this->listeners_.erase(it++);
148            else
149                ++it;
150        }
151    }
152
153    void Shell::setCursorPosition(unsigned int cursor)
154    {
155        this->inputBuffer_->setCursorPosition(cursor);
156        SHELL_UPDATE_LISTENERS(cursorChanged);
157    }
158
159    void Shell::setInput(const std::string& input)
160    {
161        this->inputBuffer_->set(input);
162        this->inputChanged();
163    }
164
165    void Shell::addLine(const std::string& line, int level)
166    {
167        int original_level = OutputHandler::getOutStream().getOutputLevel();
168        OutputHandler::getOutStream().setOutputLevel(level);
169
170        if (!this->finishedLastLine_)
171            this->outputBuffer_ << std::endl;
172
173        this->outputBuffer_ << line << std::endl;
174        OutputHandler::getOutStream().setOutputLevel(original_level);
175    }
176
177    void Shell::clearLines()
178    {
179        this->lines_.clear();
180        this->scrollIterator_ = this->lines_.begin();
181
182        this->scrollPosition_ = 0;
183        this->finishedLastLine_ = true;
184
185        SHELL_UPDATE_LISTENERS(linesChanged);
186    }
187
188    std::list<std::string>::const_iterator Shell::getNewestLineIterator() const
189    {
190        if (this->scrollPosition_)
191            return this->scrollIterator_;
192        else
193            return this->lines_.begin();
194    }
195
196    std::list<std::string>::const_iterator Shell::getEndIterator() const
197    {
198        return this->lines_.end();
199    }
200
201    void Shell::addToHistory(const std::string& command)
202    {
203        ModifyConfigValue(commandHistory_, set, this->historyOffset_, command);
204        this->historyPosition_ = 0;
205        ModifyConfigValue(historyOffset_, set, (this->historyOffset_ + 1) % this->maxHistoryLength_);
206    }
207
208    std::string Shell::getFromHistory() const
209    {
210        unsigned int index = mod(((int)this->historyOffset_) - ((int)this->historyPosition_), this->maxHistoryLength_);
211        if (index < this->commandHistory_.size() && this->historyPosition_ != 0)
212            return this->commandHistory_[index];
213        else
214            return "";
215    }
216
217    void Shell::outputChanged()
218    {
219        std::string output;
220        bool newline;
221        do
222        {
223            newline = this->outputBuffer_.getLine(&output);
224
225            if (!newline && output == "")
226                break;
227
228            if (this->finishedLastLine_)
229            {
230                if (this->bAddOutputLevel_)
231                    output.insert(0, 1, (char)OutputHandler::getOutStream().getOutputLevel());
232
233                this->lines_.insert(this->lines_.begin(), output);
234
235                if (this->scrollPosition_)
236                    this->scrollPosition_++;
237                else
238                    this->scrollIterator_ = this->lines_.begin();
239
240                this->finishedLastLine_ = newline;
241
242                if (!this->scrollPosition_)
243                {
244                    SHELL_UPDATE_LISTENERS(lineAdded);
245                }
246            }
247            else
248            {
249                (*this->lines_.begin()) += output;
250                this->finishedLastLine_ = newline;
251                SHELL_UPDATE_LISTENERS(onlyLastLineChanged);
252            }
253
254        } while (newline);
255    }
256
257    void Shell::inputChanged()
258    {
259        SHELL_UPDATE_LISTENERS(inputChanged);
260        SHELL_UPDATE_LISTENERS(cursorChanged);
261    }
262
263    void Shell::execute()
264    {
265        this->addToHistory(this->inputBuffer_->get());
266        this->addLine(this->inputBuffer_->get(), 0);
267
268        if (!CommandExecutor::execute(this->inputBuffer_->get()))
269            this->addLine("Error: Can't execute \"" + this->inputBuffer_->get() + "\".", 1);
270
271        this->clear();
272    }
273
274    void Shell::hintandcomplete()
275    {
276        this->inputBuffer_->set(CommandExecutor::complete(this->inputBuffer_->get()));
277        this->addLine(CommandExecutor::hint(this->inputBuffer_->get()), -1);
278
279        this->inputChanged();
280    }
281
282    void Shell::backspace()
283    {
284        this->inputBuffer_->removeBehindCursor();
285        SHELL_UPDATE_LISTENERS(inputChanged);
286        SHELL_UPDATE_LISTENERS(cursorChanged);
287    }
288
289    void Shell::deletechar()
290    {
291        this->inputBuffer_->removeAtCursor();
292        SHELL_UPDATE_LISTENERS(inputChanged);
293    }
294
295    void Shell::clear()
296    {
297        this->inputBuffer_->clear();
298        this->historyPosition_ = 0;
299        SHELL_UPDATE_LISTENERS(inputChanged);
300        SHELL_UPDATE_LISTENERS(cursorChanged);
301    }
302
303    void Shell::cursor_right()
304    {
305        this->inputBuffer_->increaseCursor();
306        SHELL_UPDATE_LISTENERS(cursorChanged);
307    }
308
309    void Shell::cursor_left()
310    {
311        this->inputBuffer_->decreaseCursor();
312        SHELL_UPDATE_LISTENERS(cursorChanged);
313    }
314
315    void Shell::cursor_end()
316    {
317        this->inputBuffer_->setCursorToEnd();
318        SHELL_UPDATE_LISTENERS(cursorChanged);
319    }
320
321    void Shell::cursor_home()
322    {
323        this->inputBuffer_->setCursorToBegin();
324        SHELL_UPDATE_LISTENERS(cursorChanged);
325    }
326
327    void Shell::history_up()
328    {
329        if (this->historyPosition_ < this->commandHistory_.size())
330        {
331            this->historyPosition_++;
332            this->inputBuffer_->set(this->getFromHistory());
333        }
334    }
335
336    void Shell::history_down()
337    {
338        if (this->historyPosition_ > 0)
339        {
340            this->historyPosition_--;
341            this->inputBuffer_->set(this->getFromHistory());
342        }
343    }
344
345    void Shell::scroll_up()
346    {
347        if (this->scrollIterator_ != this->lines_.end())
348        {
349            ++this->scrollIterator_;
350            ++this->scrollPosition_;
351
352            SHELL_UPDATE_LISTENERS(linesChanged);
353        }
354    }
355
356    void Shell::scroll_down()
357    {
358        if (this->scrollIterator_ != this->lines_.begin())
359        {
360            --this->scrollIterator_;
361            --this->scrollPosition_;
362
363            SHELL_UPDATE_LISTENERS(linesChanged);
364        }
365    }
366
367    void Shell::exit()
368    {
369        if (this->inputBuffer_->getSize() > 0)
370        {
371            this->clear();
372            return;
373        }
374
375        this->clear();
376        this->scrollPosition_ = 0;
377        this->scrollIterator_ = this->lines_.begin();
378
379        SHELL_UPDATE_LISTENERS(exit);
380    }
381}
Note: See TracBrowser for help on using the repository browser.