Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core3/src/core/Shell.cc @ 1596

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