2008-05-24 11:55:25 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-10-14 19:27:08 +00:00
|
|
|
/** @file console_gui.cpp Handling the GUI of the in-game console. */
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "textbuf_gui.h"
|
|
|
|
#include "window_gui.h"
|
|
|
|
#include "console_gui.h"
|
|
|
|
#include "console_internal.h"
|
|
|
|
#include "window_func.h"
|
|
|
|
#include "string_func.h"
|
2010-07-02 13:59:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-05-24 11:55:25 +00:00
|
|
|
#include "gfx_func.h"
|
2008-08-12 20:49:27 +00:00
|
|
|
#include "settings_type.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "console_func.h"
|
2008-05-24 11:55:25 +00:00
|
|
|
#include "rev.h"
|
|
|
|
|
2010-07-02 13:59:27 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
|
2010-05-13 10:14:29 +00:00
|
|
|
static const uint ICON_HISTORY_SIZE = 20;
|
|
|
|
static const uint ICON_LINE_SPACING = 2;
|
|
|
|
static const uint ICON_RIGHT_BORDERWIDTH = 10;
|
|
|
|
static const uint ICON_BOTTOM_BORDERWIDTH = 12;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
/**
|
|
|
|
* Container for a single line of console output
|
|
|
|
*/
|
|
|
|
struct IConsoleLine {
|
|
|
|
static IConsoleLine *front; ///< The front of the console backlog buffer
|
|
|
|
static int size; ///< The amount of items in the backlog
|
|
|
|
|
|
|
|
IConsoleLine *previous; ///< The previous console message.
|
2009-02-09 02:09:47 +00:00
|
|
|
char *buffer; ///< The data to store.
|
|
|
|
TextColour colour; ///< The colour of the line.
|
|
|
|
uint16 time; ///< The amount of time the line is in the backlog.
|
2008-08-12 20:49:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the console line.
|
|
|
|
* @param buffer the data to print.
|
|
|
|
* @param colour the colour of the line.
|
|
|
|
*/
|
2009-02-09 02:09:47 +00:00
|
|
|
IConsoleLine(char *buffer, TextColour colour) :
|
2008-08-12 20:49:27 +00:00
|
|
|
previous(IConsoleLine::front),
|
|
|
|
buffer(buffer),
|
|
|
|
colour(colour),
|
|
|
|
time(0)
|
|
|
|
{
|
|
|
|
IConsoleLine::front = this;
|
|
|
|
IConsoleLine::size++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear this console line and any further ones.
|
|
|
|
*/
|
|
|
|
~IConsoleLine()
|
|
|
|
{
|
|
|
|
IConsoleLine::size--;
|
|
|
|
free(buffer);
|
|
|
|
|
|
|
|
delete previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the index-ed item in the list.
|
|
|
|
*/
|
|
|
|
static const IConsoleLine *Get(uint index)
|
|
|
|
{
|
|
|
|
const IConsoleLine *item = IConsoleLine::front;
|
|
|
|
while (index != 0 && item != NULL) {
|
|
|
|
index--;
|
|
|
|
item = item->previous;
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Truncate the list removing everything older than/more than the amount
|
|
|
|
* as specified in the config file.
|
|
|
|
* As a side effect also increase the time the other lines have been in
|
|
|
|
* the list.
|
|
|
|
* @return true if and only if items got removed.
|
|
|
|
*/
|
|
|
|
static bool Truncate()
|
|
|
|
{
|
|
|
|
IConsoleLine *cur = IConsoleLine::front;
|
|
|
|
if (cur == NULL) return false;
|
|
|
|
|
|
|
|
int count = 1;
|
|
|
|
for (IConsoleLine *item = cur->previous; item != NULL; count++, cur = item, item = item->previous) {
|
|
|
|
if (item->time > _settings_client.gui.console_backlog_timeout &&
|
|
|
|
count > _settings_client.gui.console_backlog_length) {
|
|
|
|
delete item;
|
|
|
|
cur->previous = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-13 14:26:20 +00:00
|
|
|
if (item->time != MAX_UVALUE(uint16)) item->time++;
|
2008-08-12 20:49:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset the complete console line backlog.
|
|
|
|
*/
|
|
|
|
static void Reset()
|
|
|
|
{
|
|
|
|
delete IConsoleLine::front;
|
|
|
|
IConsoleLine::front = NULL;
|
|
|
|
IConsoleLine::size = 0;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ IConsoleLine *IConsoleLine::front = NULL;
|
|
|
|
/* static */ int IConsoleLine::size = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* ** main console cmd buffer ** */
|
2008-08-12 20:49:27 +00:00
|
|
|
static Textbuf _iconsole_cmdline;
|
2008-05-24 11:55:25 +00:00
|
|
|
static char *_iconsole_history[ICON_HISTORY_SIZE];
|
|
|
|
static byte _iconsole_historypos;
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleModes _iconsole_mode;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
/* *************** *
|
|
|
|
* end of header *
|
|
|
|
* *************** */
|
|
|
|
|
|
|
|
static void IConsoleClearCommand()
|
|
|
|
{
|
|
|
|
memset(_iconsole_cmdline.buf, 0, ICON_CMDLN_SIZE);
|
2008-10-22 19:12:10 +00:00
|
|
|
_iconsole_cmdline.size = 1; // only terminating zero
|
2008-05-24 11:55:25 +00:00
|
|
|
_iconsole_cmdline.width = 0;
|
|
|
|
_iconsole_cmdline.caretpos = 0;
|
|
|
|
_iconsole_cmdline.caretxoffs = 0;
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_CONSOLE, 0);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
2010-06-26 14:51:27 +00:00
|
|
|
static inline void IConsoleResetHistoryPos()
|
|
|
|
{
|
|
|
|
_iconsole_historypos = ICON_HISTORY_SIZE - 1;
|
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
|
2009-12-25 19:17:36 +00:00
|
|
|
static const char *IConsoleHistoryAdd(const char *cmd);
|
2008-05-24 11:55:25 +00:00
|
|
|
static void IConsoleHistoryNavigate(int direction);
|
|
|
|
|
2009-08-01 12:42:58 +00:00
|
|
|
/** Widgets of the console window. */
|
|
|
|
enum ConsoleWidgets {
|
|
|
|
CW_BACKGROUND, ///< Background of the console
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct NWidgetPart _nested_console_window_widgets[] = {
|
|
|
|
NWidget(WWT_EMPTY, INVALID_COLOUR, CW_BACKGROUND), SetResize(1, 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
static const WindowDesc _console_window_desc(
|
2009-11-28 14:42:35 +00:00
|
|
|
WDP_MANUAL, 0, 0,
|
2009-08-01 12:42:58 +00:00
|
|
|
WC_CONSOLE, WC_NONE,
|
|
|
|
0,
|
2009-11-15 10:26:01 +00:00
|
|
|
_nested_console_window_widgets, lengthof(_nested_console_window_widgets)
|
2009-08-01 12:42:58 +00:00
|
|
|
);
|
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
struct IConsoleWindow : Window
|
|
|
|
{
|
2008-08-12 20:49:27 +00:00
|
|
|
static int scroll;
|
2010-06-26 15:22:10 +00:00
|
|
|
int line_height; ///< Height of one line of text in the console.
|
2009-04-10 18:55:38 +00:00
|
|
|
int line_offset;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2009-08-01 12:42:58 +00:00
|
|
|
IConsoleWindow() : Window()
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
|
|
|
_iconsole_mode = ICONSOLE_OPENED;
|
2010-07-02 13:59:27 +00:00
|
|
|
this->line_height = FONT_HEIGHT_NORMAL;
|
2009-04-10 18:55:38 +00:00
|
|
|
this->line_offset = GetStringBoundingBox("] ").width + 5;
|
2009-08-01 12:42:58 +00:00
|
|
|
|
|
|
|
this->InitNested(&_console_window_desc, 0);
|
|
|
|
ResizeWindow(this, _screen.width, _screen.height / 3);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~IConsoleWindow()
|
|
|
|
{
|
|
|
|
_iconsole_mode = ICONSOLE_CLOSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void OnPaint()
|
|
|
|
{
|
2009-03-25 01:10:24 +00:00
|
|
|
const int right = this->width - 5;
|
2009-03-21 22:46:17 +00:00
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
GfxFillRect(this->left, this->top, this->width, this->height - 1, 0);
|
2010-07-02 13:59:27 +00:00
|
|
|
int ypos = this->height - this->line_height - ICON_LINE_SPACING;
|
|
|
|
for (const IConsoleLine *print = IConsoleLine::Get(IConsoleWindow::scroll); print != NULL; print = print->previous) {
|
|
|
|
SetDParamStr(0, print->buffer);
|
|
|
|
ypos = DrawStringMultiLine(5, right, top, ypos, STR_JUST_RAW_STRING, print->colour, SA_LEFT | SA_BOTTOM | SA_FORCE) - ICON_LINE_SPACING;
|
|
|
|
if (ypos <= top) break;
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
/* If the text is longer than the window, don't show the starting ']' */
|
2009-04-10 18:55:38 +00:00
|
|
|
int delta = this->width - this->line_offset - _iconsole_cmdline.width - ICON_RIGHT_BORDERWIDTH;
|
2008-05-24 11:55:25 +00:00
|
|
|
if (delta > 0) {
|
2009-04-10 18:21:40 +00:00
|
|
|
DrawString(5, right, this->height - this->line_height, "]", (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
delta = 0;
|
|
|
|
}
|
|
|
|
|
2009-04-10 18:55:38 +00:00
|
|
|
DrawString(this->line_offset + delta, right, this->height - this->line_height, _iconsole_cmdline.buf, (TextColour)CC_COMMAND, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2009-02-09 01:22:29 +00:00
|
|
|
if (_focused_window == this && _iconsole_cmdline.caret) {
|
2009-04-10 18:55:38 +00:00
|
|
|
DrawString(this->line_offset + delta + _iconsole_cmdline.caretxoffs, right, this->height - this->line_height, "_", TC_WHITE, SA_LEFT | SA_FORCE);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
virtual void OnHundredthTick()
|
|
|
|
{
|
|
|
|
if (IConsoleLine::Truncate() &&
|
|
|
|
(IConsoleWindow::scroll > IConsoleLine::size)) {
|
2009-04-10 18:21:40 +00:00
|
|
|
IConsoleWindow::scroll = max(0, IConsoleLine::size - (this->height / this->line_height) + 1);
|
2008-08-12 20:49:27 +00:00
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
virtual void OnMouseLoop()
|
|
|
|
{
|
|
|
|
if (HandleCaret(&_iconsole_cmdline)) this->SetDirty();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual EventState OnKeyPress(uint16 key, uint16 keycode)
|
|
|
|
{
|
2009-02-09 01:22:29 +00:00
|
|
|
if (_focused_window != this) return ES_NOT_HANDLED;
|
|
|
|
|
2009-04-10 18:21:40 +00:00
|
|
|
const int scroll_height = (this->height / this->line_height) - 1;
|
2008-05-24 11:55:25 +00:00
|
|
|
switch (keycode) {
|
|
|
|
case WKC_UP:
|
2009-03-14 18:16:29 +00:00
|
|
|
IConsoleHistoryNavigate(1);
|
2008-05-24 11:55:25 +00:00
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_DOWN:
|
|
|
|
IConsoleHistoryNavigate(-1);
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_PAGEDOWN:
|
|
|
|
if (IConsoleWindow::scroll - scroll_height < 0) {
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleWindow::scroll = 0;
|
|
|
|
} else {
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleWindow::scroll -= scroll_height;
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_PAGEUP:
|
|
|
|
if (IConsoleWindow::scroll + scroll_height > IConsoleLine::size - scroll_height) {
|
|
|
|
IConsoleWindow::scroll = IConsoleLine::size - scroll_height;
|
2008-05-24 11:55:25 +00:00
|
|
|
} else {
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleWindow::scroll += scroll_height;
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_DOWN:
|
2008-05-24 11:55:25 +00:00
|
|
|
if (IConsoleWindow::scroll <= 0) {
|
|
|
|
IConsoleWindow::scroll = 0;
|
|
|
|
} else {
|
|
|
|
--IConsoleWindow::scroll;
|
|
|
|
}
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
case WKC_SHIFT | WKC_UP:
|
|
|
|
if (IConsoleWindow::scroll >= IConsoleLine::size) {
|
|
|
|
IConsoleWindow::scroll = IConsoleLine::size;
|
2008-05-24 11:55:25 +00:00
|
|
|
} else {
|
|
|
|
++IConsoleWindow::scroll;
|
|
|
|
}
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_BACKQUOTE:
|
|
|
|
IConsoleSwitch();
|
|
|
|
break;
|
|
|
|
|
2009-12-25 19:17:36 +00:00
|
|
|
case WKC_RETURN: case WKC_NUM_ENTER: {
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsolePrintF(CC_COMMAND, "] %s", _iconsole_cmdline.buf);
|
2009-12-25 19:17:36 +00:00
|
|
|
const char *cmd = IConsoleHistoryAdd(_iconsole_cmdline.buf);
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleClearCommand();
|
2009-12-25 19:17:36 +00:00
|
|
|
|
|
|
|
if (cmd != NULL) IConsoleCmdExec(cmd);
|
|
|
|
} break;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
case WKC_CTRL | WKC_RETURN:
|
|
|
|
_iconsole_mode = (_iconsole_mode == ICONSOLE_FULL) ? ICONSOLE_OPENED : ICONSOLE_FULL;
|
|
|
|
IConsoleResize(this);
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
break;
|
|
|
|
|
2009-10-04 21:08:38 +00:00
|
|
|
#ifdef WITH_COCOA
|
|
|
|
case (WKC_META | 'V'):
|
|
|
|
#endif
|
2008-05-24 11:55:25 +00:00
|
|
|
case (WKC_CTRL | 'V'):
|
|
|
|
if (InsertTextBufferClipboard(&_iconsole_cmdline)) {
|
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case (WKC_CTRL | 'L'):
|
|
|
|
IConsoleCmdExec("clear");
|
|
|
|
break;
|
|
|
|
|
2009-10-04 21:08:38 +00:00
|
|
|
#ifdef WITH_COCOA
|
|
|
|
case (WKC_META | 'U'):
|
|
|
|
#endif
|
2008-05-24 11:55:25 +00:00
|
|
|
case (WKC_CTRL | 'U'):
|
|
|
|
DeleteTextBufferAll(&_iconsole_cmdline);
|
|
|
|
this->SetDirty();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_BACKSPACE: case WKC_DELETE:
|
|
|
|
if (DeleteTextBufferChar(&_iconsole_cmdline, keycode)) {
|
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WKC_LEFT: case WKC_RIGHT: case WKC_END: case WKC_HOME:
|
|
|
|
if (MoveTextBufferPos(&_iconsole_cmdline, keycode)) {
|
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (IsValidChar(key, CS_ALPHANUMERAL)) {
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleWindow::scroll = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
InsertTextBufferChar(&_iconsole_cmdline, key);
|
|
|
|
IConsoleResetHistoryPos();
|
|
|
|
this->SetDirty();
|
|
|
|
} else {
|
|
|
|
return ES_NOT_HANDLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ES_HANDLED;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
int IConsoleWindow::scroll = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
void IConsoleGUIInit()
|
|
|
|
{
|
|
|
|
_iconsole_historypos = ICON_HISTORY_SIZE - 1;
|
|
|
|
_iconsole_mode = ICONSOLE_CLOSED;
|
|
|
|
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleLine::Reset();
|
2008-05-24 11:55:25 +00:00
|
|
|
memset(_iconsole_history, 0, sizeof(_iconsole_history));
|
2008-08-12 20:49:27 +00:00
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
_iconsole_cmdline.buf = CallocT<char>(ICON_CMDLN_SIZE); // create buffer and zero it
|
2008-10-22 19:12:10 +00:00
|
|
|
_iconsole_cmdline.maxsize = ICON_CMDLN_SIZE;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
IConsolePrintF(CC_WARNING, "OpenTTD Game Console Revision 7 - %s", _openttd_revision);
|
|
|
|
IConsolePrint(CC_WHITE, "------------------------------------");
|
|
|
|
IConsolePrint(CC_WHITE, "use \"help\" for more information");
|
|
|
|
IConsolePrint(CC_WHITE, "");
|
|
|
|
IConsoleClearCommand();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleClearBuffer()
|
|
|
|
{
|
2008-08-12 20:49:27 +00:00
|
|
|
IConsoleLine::Reset();
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IConsoleGUIFree()
|
|
|
|
{
|
|
|
|
free(_iconsole_cmdline.buf);
|
|
|
|
IConsoleClearBuffer();
|
|
|
|
}
|
|
|
|
|
2010-06-26 15:22:10 +00:00
|
|
|
/** Change the size of the in-game console window after the screen size changed, or the window state changed. */
|
2008-05-24 11:55:25 +00:00
|
|
|
void IConsoleResize(Window *w)
|
|
|
|
{
|
|
|
|
switch (_iconsole_mode) {
|
|
|
|
case ICONSOLE_OPENED:
|
|
|
|
w->height = _screen.height / 3;
|
|
|
|
w->width = _screen.width;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_FULL:
|
|
|
|
w->height = _screen.height - ICON_BOTTOM_BORDERWIDTH;
|
|
|
|
w->width = _screen.width;
|
|
|
|
break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2010-06-26 15:22:10 +00:00
|
|
|
/** Toggle in-game console between opened and closed. */
|
2008-05-24 11:55:25 +00:00
|
|
|
void IConsoleSwitch()
|
|
|
|
{
|
|
|
|
switch (_iconsole_mode) {
|
|
|
|
case ICONSOLE_CLOSED:
|
2009-03-30 01:15:51 +00:00
|
|
|
new IConsoleWindow();
|
2008-05-24 11:55:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ICONSOLE_OPENED: case ICONSOLE_FULL:
|
|
|
|
DeleteWindowById(WC_CONSOLE, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2010-06-26 14:51:27 +00:00
|
|
|
/** Close the in-game console. */
|
|
|
|
void IConsoleClose()
|
|
|
|
{
|
|
|
|
if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch();
|
|
|
|
}
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add the entered line into the history so you can look it back
|
|
|
|
* scroll, etc. Put it to the beginning as it is the latest text
|
|
|
|
* @param cmd Text to be entered into the 'history'
|
2009-12-25 19:17:36 +00:00
|
|
|
* @return the command to execute
|
2008-05-24 11:55:25 +00:00
|
|
|
*/
|
2009-12-25 19:17:36 +00:00
|
|
|
static const char *IConsoleHistoryAdd(const char *cmd)
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2009-05-23 22:40:14 +00:00
|
|
|
/* Strip all spaces at the begin */
|
|
|
|
while (IsWhitespace(*cmd)) cmd++;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
2009-05-23 22:40:14 +00:00
|
|
|
/* Do not put empty command in history */
|
2009-12-25 19:17:36 +00:00
|
|
|
if (StrEmpty(cmd)) return NULL;
|
2009-05-23 22:40:14 +00:00
|
|
|
|
|
|
|
/* Do not put in history if command is same as previous */
|
|
|
|
if (_iconsole_history[0] == NULL || strcmp(_iconsole_history[0], cmd) != 0) {
|
|
|
|
free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
|
|
|
|
memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
|
|
|
|
_iconsole_history[0] = strdup(cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reset the history position */
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleResetHistoryPos();
|
2009-12-25 19:17:36 +00:00
|
|
|
return _iconsole_history[0];
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate Up/Down in the history of typed commands
|
|
|
|
* @param direction Go further back in history (+1), go to recently typed commands (-1)
|
|
|
|
*/
|
|
|
|
static void IConsoleHistoryNavigate(int direction)
|
|
|
|
{
|
2009-05-23 22:40:14 +00:00
|
|
|
if (_iconsole_history[0] == NULL) return; // Empty history
|
2008-05-24 11:55:25 +00:00
|
|
|
int i = _iconsole_historypos + direction;
|
|
|
|
|
|
|
|
/* watch out for overflows, just wrap around */
|
|
|
|
if (i < 0) i = ICON_HISTORY_SIZE - 1;
|
2010-05-13 10:14:29 +00:00
|
|
|
if ((uint)i >= ICON_HISTORY_SIZE) i = 0;
|
2008-05-24 11:55:25 +00:00
|
|
|
|
|
|
|
if (direction > 0) {
|
|
|
|
if (_iconsole_history[i] == NULL) i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction < 0) {
|
|
|
|
while (i > 0 && _iconsole_history[i] == NULL) i--;
|
|
|
|
}
|
|
|
|
|
|
|
|
_iconsole_historypos = i;
|
|
|
|
IConsoleClearCommand();
|
|
|
|
/* copy history to 'command prompt / bash' */
|
|
|
|
assert(_iconsole_history[i] != NULL && IsInsideMM(i, 0, ICON_HISTORY_SIZE));
|
2008-10-22 19:12:10 +00:00
|
|
|
ttd_strlcpy(_iconsole_cmdline.buf, _iconsole_history[i], _iconsole_cmdline.maxsize);
|
2008-05-24 11:55:25 +00:00
|
|
|
UpdateTextBufferSize(&_iconsole_cmdline);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the printing of text entered into the console or redirected there
|
2008-09-30 20:39:50 +00:00
|
|
|
* by any other means. Text can be redirected to other clients in a network game
|
2008-05-24 11:55:25 +00:00
|
|
|
* as well as to a logfile. If the network server is a dedicated server, all activities
|
|
|
|
* are also logged. All lines to print are added to a temporary buffer which can be
|
|
|
|
* used as a history to print them onscreen
|
2009-02-09 02:57:15 +00:00
|
|
|
* @param colour_code the colour of the command. Red in case of errors, etc.
|
2009-09-19 09:51:14 +00:00
|
|
|
* @param str the message entered or output on the console (notice, error, etc.)
|
2008-05-24 11:55:25 +00:00
|
|
|
*/
|
2009-02-09 02:57:15 +00:00
|
|
|
void IConsoleGUIPrint(ConsoleColour colour_code, char *str)
|
2008-05-24 11:55:25 +00:00
|
|
|
{
|
2009-02-09 02:57:15 +00:00
|
|
|
new IConsoleLine(str, (TextColour)colour_code);
|
2009-09-13 19:15:59 +00:00
|
|
|
SetWindowDirty(WC_CONSOLE, 0);
|
2008-05-24 11:55:25 +00:00
|
|
|
}
|