Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1322 was 1322, checked in by landauf, 17 years ago

The InGameConsole is now using the new Shell. There's a clear separation between the two classes: InGameConsole implements all graphical parts of the console (and has a tick), while Shell handles the internal actions (like listening on input and output changes). That's why InGameConsole has no longer it's own InputBuffer and doesn't care about command-executing or anything else.

There are currently three new features:

  • Every output through COUT(level) is now visible in the InGameConsole, provided the configured output-level for the shell matches. default: 1 (only forced output and errors)
  • The cursor in the input-line is movable with the left and right arrow keys (home and end works too)
  • You can scroll through all output-lines by pressing page up and page down

There's another feature to come, providing a command history, accessible with up and down arrow keys, but I couldn't finish it yet, because there's still a bug, causing Orxonox to scroll through the entire memory - that's maybe a bit too much history ;)

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