console_gui.cpp

Go to the documentation of this file.
00001 /* $Id: console_gui.cpp 21897 2011-01-23 00:11:15Z rubidium $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "textbuf_gui.h"
00014 #include "window_gui.h"
00015 #include "console_gui.h"
00016 #include "console_internal.h"
00017 #include "window_func.h"
00018 #include "string_func.h"
00019 #include "strings_func.h"
00020 #include "gfx_func.h"
00021 #include "settings_type.h"
00022 #include "console_func.h"
00023 #include "rev.h"
00024 
00025 #include "table/strings.h"
00026 
00027 static const uint ICON_HISTORY_SIZE       = 20;
00028 static const uint ICON_LINE_SPACING       =  2;
00029 static const uint ICON_RIGHT_BORDERWIDTH  = 10;
00030 static const uint ICON_BOTTOM_BORDERWIDTH = 12;
00031 
00035 struct IConsoleLine {
00036   static IConsoleLine *front; 
00037   static int size;            
00038 
00039   IConsoleLine *previous; 
00040   char *buffer;           
00041   TextColour colour;      
00042   uint16 time;            
00043 
00049   IConsoleLine(char *buffer, TextColour colour) :
00050       previous(IConsoleLine::front),
00051       buffer(buffer),
00052       colour(colour),
00053       time(0)
00054   {
00055     IConsoleLine::front = this;
00056     IConsoleLine::size++;
00057   }
00058 
00062   ~IConsoleLine()
00063   {
00064     IConsoleLine::size--;
00065     free(buffer);
00066 
00067     delete previous;
00068   }
00069 
00073   static const IConsoleLine *Get(uint index)
00074   {
00075     const IConsoleLine *item = IConsoleLine::front;
00076     while (index != 0 && item != NULL) {
00077       index--;
00078       item = item->previous;
00079     }
00080 
00081     return item;
00082   }
00083 
00091   static bool Truncate()
00092   {
00093     IConsoleLine *cur = IConsoleLine::front;
00094     if (cur == NULL) return false;
00095 
00096     int count = 1;
00097     for (IConsoleLine *item = cur->previous; item != NULL; count++, cur = item, item = item->previous) {
00098       if (item->time > _settings_client.gui.console_backlog_timeout &&
00099           count > _settings_client.gui.console_backlog_length) {
00100         delete item;
00101         cur->previous = NULL;
00102         return true;
00103       }
00104 
00105       if (item->time != MAX_UVALUE(uint16)) item->time++;
00106     }
00107 
00108     return false;
00109   }
00110 
00114   static void Reset()
00115   {
00116     delete IConsoleLine::front;
00117     IConsoleLine::front = NULL;
00118     IConsoleLine::size = 0;
00119   }
00120 };
00121 
00122 /* static */ IConsoleLine *IConsoleLine::front = NULL;
00123 /* static */ int IConsoleLine::size  = 0;
00124 
00125 
00126 /* ** main console cmd buffer ** */
00127 static Textbuf _iconsole_cmdline;
00128 static char *_iconsole_history[ICON_HISTORY_SIZE];
00129 static byte _iconsole_historypos;
00130 IConsoleModes _iconsole_mode;
00131 
00132 /* *************** *
00133  *  end of header  *
00134  * *************** */
00135 
00136 static void IConsoleClearCommand()
00137 {
00138   memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE);
00139   _iconsole_cmdline.chars = _iconsole_cmdline.bytes = 1; // only terminating zero
00140   _iconsole_cmdline.pixels = 0;
00141   _iconsole_cmdline.caretpos = 0;
00142   _iconsole_cmdline.caretxoffs = 0;
00143   SetWindowDirty(WC_CONSOLE, 0);
00144 }
00145 
00146 static inline void IConsoleResetHistoryPos()
00147 {
00148   _iconsole_historypos = ICON_HISTORY_SIZE - 1;
00149 }
00150 
00151 
00152 static const char *IConsoleHistoryAdd(const char *cmd);
00153 static void IConsoleHistoryNavigate(int direction);
00154 
00156 enum ConsoleWidgets {
00157   CW_BACKGROUND, 
00158 };
00159 
00160 static const struct NWidgetPart _nested_console_window_widgets[] = {
00161   NWidget(WWT_EMPTY, INVALID_COLOUR, CW_BACKGROUND), SetResize(1, 1),
00162 };
00163 
00164 static const WindowDesc _console_window_desc(
00165   WDP_MANUAL, 0, 0,
00166   WC_CONSOLE, WC_NONE,
00167   0,
00168   _nested_console_window_widgets, lengthof(_nested_console_window_widgets)
00169 );
00170 
00171 struct IConsoleWindow : Window
00172 {
00173   static int scroll;
00174   int line_height;   
00175   int line_offset;
00176 
00177   IConsoleWindow() : Window()
00178   {
00179     _iconsole_mode = ICONSOLE_OPENED;
00180     this->line_height = FONT_HEIGHT_NORMAL;
00181     this->line_offset = GetStringBoundingBox("] ").width + 5;
00182 
00183     this->InitNested(&_console_window_desc, 0);
00184     ResizeWindow(this, _screen.width, _screen.height / 3);
00185   }
00186 
00187   ~IConsoleWindow()
00188   {
00189     _iconsole_mode = ICONSOLE_CLOSED;
00190   }
00191 
00192   virtual void OnPaint()
00193   {
00194     const int right = this->width - 5;
00195 
00196     GfxFillRect(this->left, this->top, this->width, this->height - 1, 0);
00197     int ypos = this->height - this->line_height - ICON_LINE_SPACING;
00198     for (const IConsoleLine *print = IConsoleLine::Get(IConsoleWindow::scroll); print != NULL; print = print->previous) {
00199       SetDParamStr(0, print->buffer);
00200       ypos = DrawStringMultiLine(5, right, top, ypos, STR_JUST_RAW_STRING, print->colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - ICON_LINE_SPACING;
00201       if (ypos <= top) break;
00202     }
00203     /* If the text is longer than the window, don't show the starting ']' */
00204     int delta = this->width - this->line_offset - _iconsole_cmdline.pixels - ICON_RIGHT_BORDERWIDTH;
00205     if (delta > 0) {
00206       DrawString(5, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
00207       delta = 0;
00208     }
00209 
00210     DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.buf, (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
00211 
00212     if (_focused_window == this && _iconsole_cmdline.caret) {
00213       DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
00214     }
00215   }
00216 
00217   virtual void OnHundredthTick()
00218   {
00219     if (IConsoleLine::Truncate() &&
00220         (IConsoleWindow::scroll > IConsoleLine::size)) {
00221       IConsoleWindow::scroll = max(0, IConsoleLine::size - (this->height / this->line_height) + 1);
00222       this->SetDirty();
00223     }
00224   }
00225 
00226   virtual void OnMouseLoop()
00227   {
00228     if (HandleCaret(&_iconsole_cmdline)) this->SetDirty();
00229   }
00230 
00231   virtual EventState OnKeyPress(uint16 key, uint16 keycode)
00232   {
00233     if (_focused_window != this) return ES_NOT_HANDLED;
00234 
00235     const int scroll_height = (this->height / this->line_height) - 1;
00236     switch (keycode) {
00237       case WKC_UP:
00238         IConsoleHistoryNavigate(1);
00239         this->SetDirty();
00240         break;
00241 
00242       case WKC_DOWN:
00243         IConsoleHistoryNavigate(-1);
00244         this->SetDirty();
00245         break;
00246 
00247       case WKC_SHIFT | WKC_PAGEDOWN:
00248         if (IConsoleWindow::scroll - scroll_height < 0) {
00249           IConsoleWindow::scroll = 0;
00250         } else {
00251           IConsoleWindow::scroll -= scroll_height;
00252         }
00253         this->SetDirty();
00254         break;
00255 
00256       case WKC_SHIFT | WKC_PAGEUP:
00257         if (IConsoleWindow::scroll + scroll_height > IConsoleLine::size - scroll_height) {
00258           IConsoleWindow::scroll = IConsoleLine::size - scroll_height;
00259         } else {
00260           IConsoleWindow::scroll += scroll_height;
00261         }
00262         this->SetDirty();
00263         break;
00264 
00265       case WKC_SHIFT | WKC_DOWN:
00266         if (IConsoleWindow::scroll <= 0) {
00267           IConsoleWindow::scroll = 0;
00268         } else {
00269           --IConsoleWindow::scroll;
00270         }
00271         this->SetDirty();
00272         break;
00273 
00274       case WKC_SHIFT | WKC_UP:
00275         if (IConsoleWindow::scroll >= IConsoleLine::size) {
00276           IConsoleWindow::scroll = IConsoleLine::size;
00277         } else {
00278           ++IConsoleWindow::scroll;
00279         }
00280         this->SetDirty();
00281         break;
00282 
00283       case WKC_BACKQUOTE:
00284         IConsoleSwitch();
00285         break;
00286 
00287       case WKC_RETURN: case WKC_NUM_ENTER: {
00288         /* We always want the ] at the left side; we always force these strings to be left
00289          * aligned anyway. So enforce this in all cases by addding a left-to-right marker,
00290          * otherwise it will be drawn at the wrong side with right-to-left texts. */
00291         IConsolePrintF(CC_COMMAND, LRM "] %s", _iconsole_cmdline.buf);
00292         const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf);
00293         IConsoleClearCommand();
00294 
00295         if (cmd != NULL) IConsoleCmdExec(cmd);
00296         break;
00297       }
00298 
00299       case WKC_CTRL | WKC_RETURN:
00300         _iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL;
00301         IConsoleResize(this);
00302         MarkWholeScreenDirty();
00303         break;
00304 
00305 #ifdef WITH_COCOA
00306       case (WKC_META | 'V'):
00307 #endif
00308       case (WKC_CTRL | 'V'):
00309         if (InsertTextBufferClipboard(&_iconsole_cmdline)) {
00310           IConsoleResetHistoryPos();
00311           this->SetDirty();
00312         }
00313         break;
00314 
00315       case (WKC_CTRL | 'L'):
00316         IConsoleCmdExec("clear");
00317         break;
00318 
00319 #ifdef WITH_COCOA
00320       case (WKC_META | 'U'):
00321 #endif
00322       case (WKC_CTRL | 'U'):
00323         DeleteTextBufferAll(&_iconsole_cmdline);
00324         this->SetDirty();
00325         break;
00326 
00327       case WKC_BACKSPACE: case WKC_DELETE:
00328         if (DeleteTextBufferChar(&_iconsole_cmdline, keycode)) {
00329           IConsoleResetHistoryPos();
00330           this->SetDirty();
00331         }
00332         break;
00333 
00334       case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
00335         if (MoveTextBufferPos(&_iconsole_cmdline, keycode)) {
00336           IConsoleResetHistoryPos();
00337           this->SetDirty();
00338         }
00339         break;
00340 
00341       default:
00342         if (IsValidChar(key, CS_ALPHANUMERAL)) {
00343           IConsoleWindow::scroll = 0;
00344           InsertTextBufferChar(&_iconsole_cmdline, key);
00345           IConsoleResetHistoryPos();
00346           this->SetDirty();
00347         } else {
00348           return ES_NOT_HANDLED;
00349         }
00350         break;
00351     }
00352     return ES_HANDLED;
00353   }
00354 };
00355 
00356 int IConsoleWindow::scroll = 0;
00357 
00358 void IConsoleGUIInit()
00359 {
00360   _iconsole_historypos = ICON_HISTORY_SIZE - 1;
00361   _iconsole_mode = ICONSOLE_CLOSED;
00362 
00363   IConsoleLine::Reset();
00364   memset(_iconsole_history, 0, sizeof(_iconsole_history));
00365 
00366   _iconsole_cmdline.buf = CallocT<char>(ICON_CMDLN_SIZE); // create buffer and zero it
00367   _iconsole_cmdline.max_bytes = ICON_CMDLN_SIZE;
00368   _iconsole_cmdline.max_chars = ICON_CMDLN_SIZE;
00369 
00370   IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
00371   IConsolePrint(CC_WHITE,  "------------------------------------");
00372   IConsolePrint(CC_WHITE,  "use \"help\" for more information");
00373   IConsolePrint(CC_WHITE,  "");
00374   IConsoleClearCommand();
00375 }
00376 
00377 void IConsoleClearBuffer()
00378 {
00379   IConsoleLine::Reset();
00380 }
00381 
00382 void IConsoleGUIFree()
00383 {
00384   free(_iconsole_cmdline.buf);
00385   IConsoleClearBuffer();
00386 }
00387 
00389 void IConsoleResize(Window *w)
00390 {
00391   switch (_iconsole_mode) {
00392     case ICONSOLE_OPENED:
00393       w->height = _screen.height / 3;
00394       w->width = _screen.width;
00395       break;
00396     case ICONSOLE_FULL:
00397       w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
00398       w->width = _screen.width;
00399       break;
00400     default: return;
00401   }
00402 
00403   MarkWholeScreenDirty();
00404 }
00405 
00407 void IConsoleSwitch()
00408 {
00409   switch (_iconsole_mode) {
00410     case ICONSOLE_CLOSED:
00411       new IConsoleWindow();
00412       break;
00413 
00414     case ICONSOLE_OPENED: case ICONSOLE_FULL:
00415       DeleteWindowById(WC_CONSOLE, 0);
00416       break;
00417   }
00418 
00419   MarkWholeScreenDirty();
00420 }
00421 
00423 void IConsoleClose()
00424 {
00425   if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch();
00426 }
00427 
00434 static const char *IConsoleHistoryAdd(const char *cmd)
00435 {
00436   /* Strip all spaces at the begin */
00437   while (IsWhitespace(*cmd)) cmd++;
00438 
00439   /* Do not put empty command in history */
00440   if (StrEmpty(cmd)) return NULL;
00441 
00442   /* Do not put in history if command is same as previous */
00443   if (_iconsole_history[0] == NULL || strcmp(_iconsole_history[0], cmd) != 0) {
00444     free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
00445     memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
00446     _iconsole_history[0] = strdup(cmd);
00447   }
00448 
00449   /* Reset the history position */
00450   IConsoleResetHistoryPos();
00451   return _iconsole_history[0];
00452 }
00453 
00458 static void IConsoleHistoryNavigate(int direction)
00459 {
00460   if (_iconsole_history[0] == NULL) return; // Empty history
00461   int i = _iconsole_historypos + direction;
00462 
00463   /* watch out for overflows, just wrap around */
00464   if (i < 0) i = ICON_HISTORY_SIZE - 1;
00465   if ((uint)i >= ICON_HISTORY_SIZE) i = 0;
00466 
00467   if (direction > 0) {
00468     if (_iconsole_history[i] == NULL) i = 0;
00469   }
00470 
00471   if (direction < 0) {
00472     while (i > 0 && _iconsole_history[i] == NULL) i--;
00473   }
00474 
00475   _iconsole_historypos = i;
00476   IConsoleClearCommand();
00477   /* copy history to 'command prompt / bash' */
00478   assert(_iconsole_history[i] != NULL && IsInsideMM(i, 0, ICON_HISTORY_SIZE));
00479   ttd_strlcpy(_iconsole_cmdline.buf, _iconsole_history[i], _iconsole_cmdline.max_bytes);
00480   UpdateTextBufferSize(&_iconsole_cmdline);
00481 }
00482 
00492 void IConsoleGUIPrint(TextColour colour_code, char *str)
00493 {
00494   new IConsoleLine(str, colour_code);
00495   SetWindowDirty(WC_CONSOLE, 0);
00496 }
00497 
00498 
00504 bool IsValidConsoleColour(TextColour c)
00505 {
00506   /* A normal text colour is used. */
00507   if (!(c & TC_IS_PALETTE_COLOUR)) return TC_BEGIN <= c && c < TC_END;
00508 
00509   /* A text colour from the palette is used; must be the company
00510    * colour gradient, so it must be one of those. */
00511   c &= ~TC_IS_PALETTE_COLOUR;
00512   for (uint i = COLOUR_BEGIN; i < COLOUR_END; i++) {
00513     if (_colour_gradient[i][4] == c) return true;
00514   }
00515 
00516   return false;
00517 }

Generated on Fri Feb 4 20:53:39 2011 for OpenTTD by  doxygen 1.6.1