From 0ca4b4e146aa0904c9d974ab1b3f8b0bf1f9c9cf Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Fri, 15 Dec 2023 13:22:15 +0000 Subject: [PATCH] Change: Allow opening multiple script debug windows by holding Ctrl. --- src/ai/ai_core.cpp | 4 +-- src/game/game_core.cpp | 2 +- src/lang/english.txt | 4 +-- src/script/api/script_log.cpp | 2 +- src/script/script_gui.cpp | 49 ++++++++++++++++++++++++++++------- src/script/script_gui.h | 2 +- src/toolbar_gui.cpp | 2 +- 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index 1251b652af..c910f22a83 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -62,7 +62,7 @@ cur_company.Restore(); - InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); + InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); return; } @@ -113,7 +113,7 @@ cur_company.Restore(); - InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); + InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); CloseWindowById(WC_SCRIPT_SETTINGS, company); } diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index 865aa2a8fc..c7763429ff 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -96,7 +96,7 @@ cur_company.Restore(); - InvalidateWindowData(WC_SCRIPT_DEBUG, 0, -1); + InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); } /* static */ void Game::Uninitialize(bool keepConfig) diff --git a/src/lang/english.txt b/src/lang/english.txt index b9f21666b1..3aa24bce81 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -4653,9 +4653,9 @@ STR_AI_DEBUG_MATCH_CASE :{BLACK}Match ca STR_AI_DEBUG_MATCH_CASE_TOOLTIP :{BLACK}Toggle matching case when comparing AI log messages against the break string STR_AI_DEBUG_CONTINUE :{BLACK}Continue STR_AI_DEBUG_CONTINUE_TOOLTIP :{BLACK}Unpause and continue the AI -STR_AI_DEBUG_SELECT_AI_TOOLTIP :{BLACK}View debug output of this AI +STR_AI_DEBUG_SELECT_AI_TOOLTIP :{BLACK}View debug output of this AI. Ctrl-Click to open in a new window STR_AI_GAME_SCRIPT :{BLACK}Game Script -STR_AI_GAME_SCRIPT_TOOLTIP :{BLACK}Check the Game Script log +STR_AI_GAME_SCRIPT_TOOLTIP :{BLACK}Check the Game Script log. Ctrl-Click to open in a new window STR_ERROR_AI_NO_AI_FOUND :No suitable AI found to load.{}This AI is a dummy AI and won't do anything.{}You can download several AIs via the 'Online Content' system STR_ERROR_AI_PLEASE_REPORT_CRASH :{WHITE}One of the running scripts crashed. Please report this to the script author with a screenshot of the AI/Game Script Debug Window diff --git a/src/script/api/script_log.cpp b/src/script/api/script_log.cpp index adab8f709a..bb6fb0060f 100644 --- a/src/script/api/script_log.cpp +++ b/src/script/api/script_log.cpp @@ -58,5 +58,5 @@ /* Also still print to debug window */ Debug(script, level, "[{}] [{}] {}", (uint)ScriptObject::GetRootCompany(), logc, line.text); - InvalidateWindowData(WC_SCRIPT_DEBUG, 0, ScriptObject::GetRootCompany()); + InvalidateWindowClassesData(WC_SCRIPT_DEBUG, ScriptObject::GetRootCompany()); } diff --git a/src/script/script_gui.cpp b/src/script/script_gui.cpp index 162c49ef65..16b2d678ec 100644 --- a/src/script/script_gui.cpp +++ b/src/script/script_gui.cpp @@ -776,7 +776,7 @@ struct ScriptDebugWindow : public Window { * @param desc The description of the window. * @param number The window number (actually unused). */ - ScriptDebugWindow(WindowDesc *desc, WindowNumber number) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH) + ScriptDebugWindow(WindowDesc *desc, WindowNumber number, Owner show_company) : Window(desc), break_editbox(MAX_BREAK_STR_STRING_LENGTH) { this->filter = ScriptDebugWindow::initial_state; this->break_string_filter = {&this->filter.case_sensitive_break_check, false}; @@ -800,7 +800,11 @@ struct ScriptDebugWindow : public Window { /* Restore the break string value from static variable */ this->break_editbox.text.Assign(this->filter.break_string); - this->SelectValidDebugCompany(); + if (show_company == INVALID_COMPANY) { + this->SelectValidDebugCompany(); + } else { + this->ChangeToScript(show_company); + } this->InvalidateData(-1); } @@ -985,11 +989,18 @@ struct ScriptDebugWindow : public Window { /** * Change all settings to select another Script. * @param show_ai The new AI to show. + * @param new_window Open the script in a new window. */ - void ChangeToScript(CompanyID show_script) + void ChangeToScript(CompanyID show_script, bool new_window = false) { if (!this->IsValidDebugCompany(show_script)) return; + if (new_window) { + ScriptDebugWindow::initial_state = this->filter; + ShowScriptDebugWindow(show_script, true); + return; + } + this->filter.script_debug_company = show_script; this->highlight_row = -1; // The highlight of one Script make little sense for another Script. @@ -1010,12 +1021,12 @@ struct ScriptDebugWindow : public Window { /* Check which button is clicked */ if (IsInsideMM(widget, WID_SCRD_COMPANY_BUTTON_START, WID_SCRD_COMPANY_BUTTON_END + 1)) { - ChangeToScript((CompanyID)(widget - WID_SCRD_COMPANY_BUTTON_START)); + ChangeToScript((CompanyID)(widget - WID_SCRD_COMPANY_BUTTON_START), _ctrl_pressed); } switch (widget) { case WID_SCRD_SCRIPT_GAME: - ChangeToScript(OWNER_DEITY); + ChangeToScript(OWNER_DEITY, _ctrl_pressed); break; case WID_SCRD_RELOAD_TOGGLE: @@ -1250,14 +1261,32 @@ static WindowDesc _script_debug_desc(__FILE__, __LINE__, /** * Open the Script debug window and select the given company. * @param show_company Display debug information about this AI company. + * @param new_window Show in new window instead of existing window. */ -Window *ShowScriptDebugWindow(CompanyID show_company) +Window *ShowScriptDebugWindow(CompanyID show_company, bool new_window) { if (!_networking || _network_server) { - ScriptDebugWindow *w = (ScriptDebugWindow *)BringWindowToFrontById(WC_SCRIPT_DEBUG, 0); - if (w == nullptr) w = new ScriptDebugWindow(&_script_debug_desc, 0); - if (show_company != INVALID_COMPANY) w->ChangeToScript(show_company); - return w; + int i = 0; + if (new_window) { + /* find next free window number for script debug */ + while (FindWindowById(WC_SCRIPT_DEBUG, i) != nullptr) i++; + } else { + /* Find existing window showing show_company. */ + for (Window *w : Window::Iterate()) { + if (w->window_class == WC_SCRIPT_DEBUG && static_cast(w)->filter.script_debug_company == show_company) { + return BringWindowToFrontById(w->window_class, w->window_number); + } + } + + /* Maybe there's a window showing a different company which can be switched. */ + ScriptDebugWindow *w = static_cast(FindWindowByClass(WC_SCRIPT_DEBUG)); + if (w != nullptr) { + BringWindowToFrontById(w->window_class, w->window_number); + w->ChangeToScript(show_company); + return w; + } + } + return new ScriptDebugWindow(&_script_debug_desc, i, show_company); } else { ShowErrorMessage(STR_ERROR_AI_DEBUG_SERVER_ONLY, INVALID_STRING_ID, WL_INFO); } diff --git a/src/script/script_gui.h b/src/script/script_gui.h index 1775d52489..807557ac69 100644 --- a/src/script/script_gui.h +++ b/src/script/script_gui.h @@ -14,7 +14,7 @@ #include "../textfile_type.h" void ShowScriptListWindow(CompanyID slot, bool show_all); -Window *ShowScriptDebugWindow(CompanyID show_company = INVALID_COMPANY); +Window *ShowScriptDebugWindow(CompanyID show_company = INVALID_COMPANY, bool new_window = false); void ShowScriptSettingsWindow(CompanyID slot); void ShowScriptTextfileWindow(TextfileType file_type, CompanyID slot); void ShowScriptDebugWindowIfScriptError(); diff --git a/src/toolbar_gui.cpp b/src/toolbar_gui.cpp index 70b2fb5ec8..b4e3f4ed77 100644 --- a/src/toolbar_gui.cpp +++ b/src/toolbar_gui.cpp @@ -1120,7 +1120,7 @@ static CallBackFunction MenuClickHelp(int index) case 0: return PlaceLandBlockInfo(); case 1: ShowHelpWindow(); break; case 2: IConsoleSwitch(); break; - case 3: ShowScriptDebugWindow(); break; + case 3: ShowScriptDebugWindow(INVALID_COMPANY, _ctrl_pressed); break; case 4: ShowScreenshotWindow(); break; case 5: ShowFramerateWindow(); break; case 6: ShowAboutWindow(); break;