00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <console/ConsoleImpl.h>
00022 #include <common/Keyboard.h>
00023 #include <common/Defines.h>
00024 #include <common/Logger.h>
00025 #include <GLEXT/GLState.h>
00026 #include <GLEXT/GLViewPort.h>
00027 #include <GLW/GLWFont.h>
00028 #include <GLW/GLWToolTip.h>
00029
00030 ConsoleImpl::ConsoleImpl() :
00031 GameStateI("Console"),
00032 height_(0.0f), opening_(false),
00033 lines_(1000), historyPosition_(-1), font_(0),
00034 showCursor_(true)
00035 {
00036 Logger::addLogger(this);
00037 }
00038
00039 ConsoleImpl::~ConsoleImpl()
00040 {
00041
00042 }
00043
00044 void ConsoleImpl::init()
00045 {
00046 methods_.init();
00047 }
00048
00049 void ConsoleImpl::logMessage(LoggerInfo &info)
00050 {
00051 addLine(false, info.getMessage());
00052 }
00053
00054 void ConsoleImpl::keyboardCheck(const unsigned state, float frameTime,
00055 char *buffer, unsigned int keyState,
00056 KeyboardHistory::HistoryElement *history, int hisCount,
00057 bool &skipRest)
00058 {
00059
00060
00061 KEYBOARDKEY("CONSOLE", consoleKey);
00062 for (int i=0; i<hisCount; i++)
00063 {
00064 unsigned int dik = history[i].sdlKey;
00065 if (consoleKey->keyMatch(dik))
00066 {
00067 resetPositions();
00068 if (currentLine_.empty()) opening_ = !opening_;
00069 else currentLine_ = "";
00070 skipRest = true;
00071 }
00072 }
00073
00074 if (height_ > 0.0f)
00075 {
00076
00077
00078 if (!skipRest)
00079 for (int i=0; i<hisCount; i++)
00080 {
00081 char unicodeKey = history[i].representedUnicode;
00082 unsigned int dik = history[i].sdlKey;
00083
00084 if (unicodeKey >= ' ')
00085 {
00086 resetPositions();
00087 if (unicodeKey < 127) currentLine_ += (char) unicodeKey;
00088 }
00089 else
00090 {
00091 switch (dik)
00092 {
00093 case SDLK_ESCAPE:
00094 resetPositions();
00095 if (currentLine_.empty()) opening_ = false;
00096 else currentLine_ = "";
00097 break;
00098 case SDLK_TAB:
00099 resetPositions();
00100 {
00101 std::vector<ConsoleRule *> matches;
00102 std::string result =
00103 rules_.matchRule(currentLine_.c_str(), matches);
00104 if (result.length() > 0) currentLine_ = result;
00105
00106 addLine(false, "-------------------");
00107 std::vector<ConsoleRule *>::iterator itor;
00108 for (itor = matches.begin();
00109 itor != matches.end();
00110 itor++)
00111 {
00112 std::string text = (*itor)->toString();
00113 addLine(false, text.c_str());
00114 }
00115 }
00116 break;
00117 case SDLK_BACKSPACE:
00118 case SDLK_DELETE:
00119 resetPositions();
00120 currentLine_ = currentLine_.substr(0, currentLine_.length() - 1);
00121 break;
00122 case SDLK_RETURN:
00123 if (!currentLine_.empty())
00124 {
00125 resetPositions();
00126 addLine(true, currentLine_.c_str());
00127 history_.push_front(currentLine_.c_str());
00128 currentLine_.erase();
00129 }
00130 break;
00131 case SDLK_UP:
00132 historyPosition_++;
00133 if (historyPosition_ >= int(history_.size()))
00134 historyPosition_ = int(history_.size()) - 1;
00135 if (historyPosition_ >= 0)
00136 currentLine_ = history_[historyPosition_];
00137 break;
00138 case SDLK_DOWN:
00139 historyPosition_--;
00140 if (historyPosition_ <= 0)
00141 historyPosition_ = (history_.empty()?-1:0);
00142 if (historyPosition_ >= 0)
00143 currentLine_ = history_[historyPosition_];
00144 break;
00145 case SDLK_PAGEUP:
00146 lines_.scroll(-5);
00147 break;
00148 case SDLK_PAGEDOWN:
00149 lines_.scroll(+5);
00150 break;
00151 case SDLK_HOME:
00152 lines_.scroll(INT_MIN);
00153 break;
00154 case SDLK_END:
00155 lines_.scroll(INT_MAX);
00156 break;
00157 }
00158 }
00159 }
00160 skipRest = true;
00161 }
00162 else
00163 {
00164 std::list<KeyboardKey *> &keys = Keyboard::instance()->getCommandKeys();
00165 std::list<KeyboardKey *>::iterator keyItor = keys.begin();
00166 std::list<KeyboardKey *>::iterator keyEndItor = keys.end();
00167 for (; keyItor != keyEndItor; keyItor++)
00168 {
00169 KeyboardKey *key = (*keyItor);
00170 if (key->keyDown(buffer, keyState, false))
00171 {
00172 resetPositions();
00173 addLine(true, key->getName());
00174 }
00175 }
00176 }
00177 }
00178
00179 void ConsoleImpl::resetPositions()
00180 {
00181 historyPosition_ = -1;
00182 lines_.resetScroll();
00183 }
00184
00185 void ConsoleImpl::simulate(const unsigned state, float frameTime)
00186 {
00187 const GLfloat dropSpeed = 1200.0f;
00188 if (opening_)
00189 {
00190 height_ += frameTime * dropSpeed;
00191 }
00192 else
00193 {
00194 height_ -= frameTime * dropSpeed;
00195 if (height_ < 0.0f) height_ = 0.0f;
00196 }
00197
00198 static float ctime = 0;
00199 ctime += frameTime;
00200 if (ctime > 0.5f)
00201 {
00202 ctime = 0.0f;
00203 showCursor_ = !showCursor_;
00204 }
00205 }
00206
00207 void ConsoleImpl::draw(const unsigned state)
00208 {
00209 if (height_ <= 0.0f) return;
00210
00211 GLState currentState(GLState::DEPTH_OFF | GLState::TEXTURE_OFF | GLState::BLEND_ON);
00212
00213 GLfloat width = (GLfloat) GLViewPort::getWidth();
00214 GLfloat top = (GLfloat) GLViewPort::getHeight();
00215
00216 drawBackdrop(width, top);
00217 drawText(width, top);
00218 }
00219
00220 void ConsoleImpl::drawBackdrop(float width, float top)
00221 {
00222 if (height_ > top * .85f) height_ = top * .85f;
00223
00224 GLWToolTip::instance()->clearToolTip(
00225 0.0f, top - height_ + 10.0f,
00226 width, height_);
00227
00228 glColor4f(0.0f, 0.0f, 0.0f, 0.9f);
00229 glBegin(GL_QUADS);
00230 glVertex2f(0.0f, top - height_ + 10.0f);
00231 glVertex2f(width, top - height_ + 10.0f);
00232 glVertex2f(width, top);
00233 glVertex2f(0.0f, top);
00234
00235 glVertex2f(10.0f, top - height_);
00236 glVertex2f(width - 10.0f, top - height_);
00237 glVertex2f(width, top - height_ + 10.0f);
00238 glVertex2f(0.0f, top - height_ + 10.0f);
00239 glEnd();
00240
00241 glColor4f(0.1f, 0.1f, 0.1f, 1.0f);
00242 glBegin(GL_LINE_STRIP);
00243 glVertex2f(0.0f, top - height_ + 10.0f);
00244 glVertex2f(10.0f, top - height_);
00245 glVertex2f(width - 10.0f, top - height_);
00246 glVertex2f(width, top - height_ + 10.0f);
00247 glEnd();
00248 }
00249
00250 void ConsoleImpl::drawText(float width, float top)
00251 {
00252 static Vector color(1.0f, 1.0f, 1.0f);
00253 if (!font_) font_ = GLWFont::instance()->getCourierFont();
00254 font_->draw(color, 14,
00255 10.0f, top - (height_ - 14.0f), 0.0f,
00256 S3D::formatStringBuffer("> %s%c",
00257 currentLine_.c_str(),
00258 (showCursor_?'_':' ')));
00259
00260 lines_.drawLines(font_, top - (height_ - 14.0f), top, width);
00261 }
00262
00263 void ConsoleImpl::addLine(bool parse, const std::string &text)
00264 {
00265 lines_.addLine(text, parse);
00266 if (parse)
00267 {
00268 rules_.addLine(this, text.c_str());
00269 }
00270 }
00271
00272 void ConsoleImpl::help()
00273 {
00274 std::vector<std::string>::iterator itor;
00275 std::vector<std::string> result;
00276 rules_.dump(result);
00277
00278 for (itor = result.begin();
00279 itor != result.end();
00280 itor++)
00281 {
00282 addLine(false, *itor);
00283 }
00284 }