Changeset - r19623:907a4ff31fef
[Not reviewed]
master
0 3 0
zuu - 12 years ago 2012-09-23 14:37:59
zuu@openttd.org
(svn r24554) -Add: Hotkeys for widgets in AI Debug window
3 files changed with 78 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_gui.cpp
Show inline comments
 
@@ -18,24 +18,25 @@
 
#include "../company_base.h"
 
#include "../company_gui.h"
 
#include "../strings_func.h"
 
#include "../window_func.h"
 
#include "../gfx_func.h"
 
#include "../command_func.h"
 
#include "../network/network.h"
 
#include "../settings_func.h"
 
#include "../network/network_content.h"
 
#include "../textfile_gui.h"
 
#include "../widgets/dropdown_type.h"
 
#include "../widgets/dropdown_func.h"
 
#include "../hotkeys.h"
 

	
 
#include "ai.hpp"
 
#include "../script/api/script_log.hpp"
 
#include "ai_config.hpp"
 
#include "ai_info.hpp"
 
#include "ai_instance.hpp"
 
#include "../game/game.hpp"
 
#include "../game/game_config.hpp"
 
#include "../game/game_info.hpp"
 
#include "../game/game_instance.hpp"
 

	
 

	
 
@@ -1344,28 +1345,53 @@ struct AIDebugWindow : public QueryStrin
 
		this->RaiseWidget(WID_AID_SETTINGS);
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnMouseLoop()
 
	{
 
		this->HandleEditBox(WID_AID_BREAK_STR_EDIT_BOX);
 
	}
 

	
 
	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
 
	{
 
		EventState state = ES_NOT_HANDLED;
 
		if (this->HandleEditBoxKey(WID_AID_BREAK_STR_EDIT_BOX, key, keycode, state) != HEBR_NOT_FOCUSED) {
 
			/* Save the current string to static member so it can be restored next time the window is opened */
 
		switch (this->HandleEditBoxKey(WID_AID_BREAK_STR_EDIT_BOX, key, keycode, state)) {
 
			case HEBR_EDITING:
 
				/* Save the current string to static member so it can be restored next time the window is opened. */
 
			strecpy(this->break_string, this->edit_str_buf, lastof(this->break_string));
 
			break_string_filter.SetFilterTerm(this->break_string);
 
				break;
 

	
 
			case HEBR_CANCEL:
 
				/* Unfocus the text box. */
 
				this->UnfocusFocusedWidget();
 
				break;
 

	
 
			case HEBR_NOT_FOCUSED: {
 
				/* Edit boxs is not globally foused => handle hotkeys of AI Debug window. */
 
				int num = CheckHotkeyMatch(aidebug_hotkeys, keycode, this);
 
				if (num == -1) return ES_NOT_HANDLED;
 
				if (this->show_break_box && num == WID_AID_BREAK_STR_EDIT_BOX) {
 
					this->SetFocusedWidget(WID_AID_BREAK_STR_EDIT_BOX);
 
					SetFocusedWindow(this);
 
					state = ES_HANDLED;
 
				} else if (this->show_break_box || num < WID_AID_BREAK_STRING_WIDGETS) {
 
					this->OnClick(Point(), num, 1);
 
					state = ES_HANDLED;
 
				}
 
				break;
 
			}
 

	
 
			default:
 
				break;
 
		}
 
		return state;
 
	}
 

	
 
	/**
 
	 * Some data on this window has become invalid.
 
	 * @param data Information about the changed data.
 
	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
 
	 */
 
	virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
 
	{
 
		if (data == -1 || ai_debug_company == data) this->SetDirty();
 
@@ -1398,40 +1424,69 @@ struct AIDebugWindow : public QueryStrin
 

	
 
					/* Highlight row that matched */
 
					this->highlight_row = log->pos;
 
				}
 
			}
 
		}
 
	}
 

	
 
	virtual void OnResize()
 
	{
 
		this->vscroll->SetCapacityFromWidget(this, WID_AID_LOG_PANEL);
 
	}
 

	
 
	static Hotkey<AIDebugWindow> aidebug_hotkeys[];
 
};
 

	
 
const int AIDebugWindow::top_offset = WD_FRAMERECT_TOP + 2;
 
const int AIDebugWindow::bottom_offset = WD_FRAMERECT_BOTTOM;
 
CompanyID AIDebugWindow::ai_debug_company = INVALID_COMPANY;
 
char AIDebugWindow::break_string[MAX_BREAK_STR_STRING_LENGTH] = "";
 
bool AIDebugWindow::break_check_enabled = true;
 
bool AIDebugWindow::case_sensitive_break_check = false;
 
StringFilter AIDebugWindow::break_string_filter(&AIDebugWindow::case_sensitive_break_check);
 

	
 
/** Make a number of rows with buttons for each company for the AI debug window. */
 
NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
 
{
 
	return MakeCompanyButtonRows(biggest_index, WID_AID_COMPANY_BUTTON_START, WID_AID_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
 
}
 

	
 
Hotkey<AIDebugWindow> AIDebugWindow::aidebug_hotkeys[] = {
 
	Hotkey<AIDebugWindow>('1', "company_1", WID_AID_COMPANY_BUTTON_START),
 
	Hotkey<AIDebugWindow>('2', "company_2", WID_AID_COMPANY_BUTTON_START + 1),
 
	Hotkey<AIDebugWindow>('3', "company_3", WID_AID_COMPANY_BUTTON_START + 2),
 
	Hotkey<AIDebugWindow>('4', "company_4", WID_AID_COMPANY_BUTTON_START + 3),
 
	Hotkey<AIDebugWindow>('5', "company_5", WID_AID_COMPANY_BUTTON_START + 4),
 
	Hotkey<AIDebugWindow>('6', "company_6", WID_AID_COMPANY_BUTTON_START + 5),
 
	Hotkey<AIDebugWindow>('7', "company_7", WID_AID_COMPANY_BUTTON_START + 6),
 
	Hotkey<AIDebugWindow>('8', "company_8", WID_AID_COMPANY_BUTTON_START + 7),
 
	Hotkey<AIDebugWindow>('9', "company_9", WID_AID_COMPANY_BUTTON_START + 8),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_10", WID_AID_COMPANY_BUTTON_START + 9),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_11", WID_AID_COMPANY_BUTTON_START + 10),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_12", WID_AID_COMPANY_BUTTON_START + 11),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_13", WID_AID_COMPANY_BUTTON_START + 12),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_14", WID_AID_COMPANY_BUTTON_START + 13),
 
	Hotkey<AIDebugWindow>((uint16)0, "company_15", WID_AID_COMPANY_BUTTON_START + 14),
 
	Hotkey<AIDebugWindow>('S', "settings", WID_AID_SETTINGS),
 
	Hotkey<AIDebugWindow>('0', "game_script", WID_AID_SCRIPT_GAME),
 
	Hotkey<AIDebugWindow>((uint16)0, "reload", WID_AID_RELOAD_TOGGLE),
 
	Hotkey<AIDebugWindow>('B', "break_toggle", WID_AID_BREAK_STR_ON_OFF_BTN),
 
	Hotkey<AIDebugWindow>('F', "break_string", WID_AID_BREAK_STR_EDIT_BOX),
 
	Hotkey<AIDebugWindow>('C', "match_case", WID_AID_MATCH_CASE_BTN),
 
	Hotkey<AIDebugWindow>(WKC_RETURN, "continue", WID_AID_CONTINUE_BTN),
 
	HOTKEY_LIST_END(AIDebugWindow)
 
};
 
Hotkey<AIDebugWindow> *_aidebug_hotkeys = AIDebugWindow::aidebug_hotkeys;
 

	
 
/** Widgets for the AI debug window. */
 
static const NWidgetPart _nested_ai_debug_widgets[] = {
 
	NWidget(NWID_HORIZONTAL),
 
		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
 
		NWidget(WWT_CAPTION, COLOUR_GREY), SetDataTip(STR_AI_DEBUG, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
 
		NWidget(WWT_SHADEBOX, COLOUR_GREY),
 
		NWidget(WWT_STICKYBOX, COLOUR_GREY),
 
	EndContainer(),
 
	NWidget(WWT_PANEL, COLOUR_GREY, WID_AID_VIEW),
 
		NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
 
	EndContainer(),
 
	NWidget(NWID_HORIZONTAL),
 
@@ -1470,33 +1525,48 @@ static const NWidgetPart _nested_ai_debu
 
/** Window definition for the AI debug window. */
 
static const WindowDesc _ai_debug_desc(
 
	WDP_AUTO, 600, 450,
 
	WC_AI_DEBUG, WC_NONE,
 
	0,
 
	_nested_ai_debug_widgets, lengthof(_nested_ai_debug_widgets)
 
);
 

	
 
/**
 
 * Open the AI debug window and select the given company.
 
 * @param show_company Display debug information about this AI company.
 
 */
 
void ShowAIDebugWindow(CompanyID show_company)
 
Window *ShowAIDebugWindow(CompanyID show_company)
 
{
 
	if (!_networking || _network_server) {
 
		AIDebugWindow *w = (AIDebugWindow *)BringWindowToFrontById(WC_AI_DEBUG, 0);
 
		if (w == NULL) w = new AIDebugWindow(&_ai_debug_desc, 0);
 
		if (show_company != INVALID_COMPANY) w->ChangeToAI(show_company);
 
		return w;
 
	} else {
 
		ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO);
 
	}
 

	
 
	return NULL;
 
}
 

	
 
/**
 
 * Handler for global AI debug window hotkeys.
 
 */
 
EventState AIDebugGlobalHotkeys(uint16 key, uint16 keycode)
 
{
 
	int num = CheckHotkeyMatch<AIDebugWindow>(_aidebug_hotkeys, keycode, NULL, true);
 
	if (num == -1) return ES_NOT_HANDLED;
 
	Window *w = ShowAIDebugWindow(INVALID_COMPANY);
 
	if (w == NULL) return ES_NOT_HANDLED;
 
	return w->OnKeyPress(key, keycode);
 
}
 

	
 
/**
 
 * Reset the AI windows to their initial state.
 
 */
 
void InitializeAIGui()
 
{
 
	AIDebugWindow::ai_debug_company = INVALID_COMPANY;
 
}
 

	
 
/** Open the AI debug window if one of the AI scripts has crashed. */
 
void ShowAIDebugWindowIfAIError()
src/ai/ai_gui.hpp
Show inline comments
 
@@ -5,18 +5,18 @@
 
 * 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/>.
 
 */
 

	
 
/** @file ai_gui.hpp %Window for configuring the AIs  */
 

	
 
#ifndef AI_GUI_HPP
 
#define AI_GUI_HPP
 

	
 
#include "../company_type.h"
 

	
 
void ShowAIDebugWindow(CompanyID show_company = INVALID_COMPANY);
 
Window* ShowAIDebugWindow(CompanyID show_company = INVALID_COMPANY);
 
void ShowAIConfigWindow();
 
void ShowAIDebugWindowIfAIError();
 
void InitializeAIGui();
 

	
 
#endif /* AI_GUI_HPP */
src/hotkeys.cpp
Show inline comments
 
@@ -240,48 +240,50 @@ void SaveLoadHotkeyGroup(IniGroup *group
 

	
 
struct MainWindow;
 
struct MainToolbarWindow;
 
struct ScenarioEditorToolbarWindow;
 
struct TerraformToolbarWindow;
 
struct ScenarioEditorLandscapeGenerationWindow;
 
struct OrdersWindow;
 
struct BuildAirToolbarWindow;
 
struct BuildDocksToolbarWindow;
 
struct BuildRailToolbarWindow;
 
struct BuildRoadToolbarWindow;
 
struct SignListWindow;
 
struct AIDebugWindow;
 

	
 
static void SaveLoadHotkeys(bool save)
 
{
 
	IniFile *ini = new IniFile();
 
	ini->LoadFromDisk(_hotkeys_file, BASE_DIR);
 

	
 
	IniGroup *group;
 

	
 
#define SL_HOTKEYS(name, window_name) \
 
	extern Hotkey<window_name> *_##name##_hotkeys;\
 
	group = ini->GetGroup(#name);\
 
	SaveLoadHotkeyGroup(group, _##name##_hotkeys, save);
 

	
 
	SL_HOTKEYS(global, MainWindow);
 
	SL_HOTKEYS(maintoolbar, MainToolbarWindow);
 
	SL_HOTKEYS(scenedit_maintoolbar, ScenarioEditorToolbarWindow);
 
	SL_HOTKEYS(terraform, TerraformToolbarWindow);
 
	SL_HOTKEYS(terraform_editor, ScenarioEditorLandscapeGenerationWindow);
 
	SL_HOTKEYS(order, OrdersWindow);
 
	SL_HOTKEYS(airtoolbar, BuildAirToolbarWindow);
 
	SL_HOTKEYS(dockstoolbar, BuildDocksToolbarWindow);
 
	SL_HOTKEYS(railtoolbar, BuildRailToolbarWindow);
 
	SL_HOTKEYS(roadtoolbar, BuildRoadToolbarWindow);
 
	SL_HOTKEYS(signlist, SignListWindow);
 
	SL_HOTKEYS(aidebug, AIDebugWindow);
 

	
 

	
 
#undef SL_HOTKEYS
 
	if (save) ini->SaveToDisk(_hotkeys_file);
 
	delete ini;
 
}
 

	
 

	
 
/** Load the hotkeys from the config file */
 
void LoadHotkeysFromConfig()
 
{
 
	SaveLoadHotkeys(false);
 
@@ -294,33 +296,35 @@ void SaveHotkeysToConfig()
 
}
 

	
 
typedef EventState GlobalHotkeyHandler(uint16, uint16);
 

	
 
GlobalHotkeyHandler RailToolbarGlobalHotkeys;
 
GlobalHotkeyHandler DockToolbarGlobalHotkeys;
 
GlobalHotkeyHandler AirportToolbarGlobalHotkeys;
 
GlobalHotkeyHandler TerraformToolbarGlobalHotkeys;
 
GlobalHotkeyHandler TerraformToolbarEditorGlobalHotkeys;
 
GlobalHotkeyHandler RoadToolbarGlobalHotkeys;
 
GlobalHotkeyHandler RoadToolbarEditorGlobalHotkeys;
 
GlobalHotkeyHandler SignListGlobalHotkeys;
 
GlobalHotkeyHandler AIDebugGlobalHotkeys;
 

	
 

	
 
GlobalHotkeyHandler *_global_hotkey_handlers[] = {
 
	RailToolbarGlobalHotkeys,
 
	DockToolbarGlobalHotkeys,
 
	AirportToolbarGlobalHotkeys,
 
	TerraformToolbarGlobalHotkeys,
 
	RoadToolbarGlobalHotkeys,
 
	SignListGlobalHotkeys,
 
	AIDebugGlobalHotkeys,
 
};
 

	
 
GlobalHotkeyHandler *_global_hotkey_handlers_editor[] = {
 
	TerraformToolbarEditorGlobalHotkeys,
 
	RoadToolbarEditorGlobalHotkeys,
 
};
 

	
 

	
 
void HandleGlobalHotkeys(uint16 key, uint16 keycode)
 
{
 
	if (_game_mode == GM_NORMAL) {
 
		for (uint i = 0; i < lengthof(_global_hotkey_handlers); i++) {
0 comments (0 inline, 0 general)