From 71ca58c2bb1b72a088acec5b70423a43512ec195 Mon Sep 17 00:00:00 2001 From: frosch Date: Thu, 12 Aug 2010 08:37:01 +0000 Subject: (svn r20453) -Codechange: Remove direct accesses to Window::hscroll, vscroll and vscroll2. Note: All windows get individual members with the same names, which are initialised via Window::GetScrollbar(). This caching is not required at all, but simplifies conversion. --- src/ai/ai_gui.cpp | 76 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 33 deletions(-) (limited to 'src/ai') diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 4938b7ece..fa65f1e26 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -52,15 +52,18 @@ struct AIListWindow : public Window { int selected; CompanyID slot; int line_height; // Height of a row in the matrix widget. + Scrollbar *vscroll; AIListWindow(const WindowDesc *desc, CompanyID slot) : Window(), slot(slot) { this->ai_info_list = AI::GetUniqueInfoList(); - this->InitNested(desc); // Initializes 'this->line_height' as side effect. + this->CreateNestedTree(desc); + this->vscroll = this->GetScrollbar(AIL_WIDGET_SCROLLBAR); + this->FinishInitNested(desc); // Initializes 'this->line_height' as side effect. - this->vscroll.SetCount((int)this->ai_info_list->size() + 1); + this->vscroll->SetCount((int)this->ai_info_list->size() + 1); /* Try if we can find the currently selected AI */ this->selected = -1; @@ -99,13 +102,13 @@ struct AIListWindow : public Window { /* Draw a list of all available AIs. */ int y = this->GetWidget(AIL_WIDGET_LIST)->pos_y; /* First AI in the list is hardcoded to random */ - if (this->vscroll.IsVisible(0)) { + if (this->vscroll->IsVisible(0)) { DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_LEFT, y + WD_MATRIX_TOP, STR_AI_CONFIG_RANDOM_AI, this->selected == -1 ? TC_WHITE : TC_BLACK); y += this->line_height; } AIInfoList::const_iterator it = this->ai_info_list->begin(); for (int i = 1; it != this->ai_info_list->end(); i++, it++) { - if (this->vscroll.IsVisible(i)) { + if (this->vscroll->IsVisible(i)) { DrawString(r.left + WD_MATRIX_LEFT, r.right - WD_MATRIX_RIGHT, y + WD_MATRIX_TOP, (*it).second->GetName(), (this->selected == i - 1) ? TC_WHITE : TC_BLACK); y += this->line_height; } @@ -156,7 +159,7 @@ struct AIListWindow : public Window { { switch (widget) { case AIL_WIDGET_LIST: { // Select one of the AIs - int sel = this->vscroll.GetScrolledRowFromWidget(pt.y, this, AIL_WIDGET_LIST, 0, this->line_height) - 1; + int sel = this->vscroll->GetScrolledRowFromWidget(pt.y, this, AIL_WIDGET_LIST, 0, this->line_height) - 1; if (sel < (int)this->ai_info_list->size()) { this->selected = sel; this->SetDirty(); @@ -183,8 +186,8 @@ struct AIListWindow : public Window { virtual void OnResize() { NWidgetCore *nwi = this->GetWidget(AIL_WIDGET_LIST); - this->vscroll.SetCapacity(nwi->current_y / this->line_height); - nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); + this->vscroll->SetCapacity(nwi->current_y / this->line_height); + nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } }; @@ -246,6 +249,7 @@ struct AISettingsWindow : public Window { int timeout; int clicked_row; int line_height; // Height of a row in the matrix widget. + Scrollbar *vscroll; AISettingsWindow(const WindowDesc *desc, CompanyID slot) : Window(), slot(slot), @@ -254,11 +258,13 @@ struct AISettingsWindow : public Window { { this->ai_config = AIConfig::GetConfig(slot); - this->InitNested(desc, slot); // Initializes 'this->line_height' as side effect. + this->CreateNestedTree(desc); + this->vscroll = this->GetScrollbar(AIS_WIDGET_SCROLLBAR); + this->FinishInitNested(desc, slot); // Initializes 'this->line_height' as side effect. this->SetWidgetDisabledState(AIS_WIDGET_RESET, _game_mode != GM_MENU); - this->vscroll.SetCount((int)this->ai_config->GetConfigList()->size()); + this->vscroll->SetCount((int)this->ai_config->GetConfigList()->size()); } virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) @@ -284,7 +290,7 @@ struct AISettingsWindow : public Window { AIConfig *config = this->ai_config; AIConfigItemList::const_iterator it = config->GetConfigList()->begin(); int i = 0; - for (; !this->vscroll.IsVisible(i); i++) it++; + for (; !this->vscroll->IsVisible(i); i++) it++; bool rtl = _dynlang.text_dir == TD_RTL; uint buttons_left = rtl ? r.right - 23 : r.left + 4; @@ -295,7 +301,7 @@ struct AISettingsWindow : public Window { int y = r.top; - for (; this->vscroll.IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) { + for (; this->vscroll->IsVisible(i) && it != config->GetConfigList()->end(); i++, it++) { int current_value = config->GetSetting((*it).name); bool editable = (_game_mode == GM_MENU) || ((it->flags & AICONFIG_INGAME) != 0); @@ -334,7 +340,7 @@ struct AISettingsWindow : public Window { switch (widget) { case AIS_WIDGET_BACKGROUND: { const NWidgetBase *wid = this->GetWidget(AIS_WIDGET_BACKGROUND); - int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll.GetPosition(); + int num = (pt.y - wid->pos_y) / this->line_height + this->vscroll->GetPosition(); if (num >= (int)this->ai_config->GetConfigList()->size()) break; AIConfigItemList::const_iterator it = this->ai_config->GetConfigList()->begin(); @@ -405,8 +411,8 @@ struct AISettingsWindow : public Window { virtual void OnResize() { NWidgetCore *nwi = this->GetWidget(AIS_WIDGET_BACKGROUND); - this->vscroll.SetCapacity(nwi->current_y / this->line_height); - nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); + this->vscroll->SetCapacity(nwi->current_y / this->line_height); + nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); } virtual void OnTick() @@ -519,15 +525,17 @@ static const WindowDesc _ai_config_desc( struct AIConfigWindow : public Window { CompanyID selected_slot; ///< The currently selected AI slot or \c INVALID_COMPANY. int line_height; ///< Height of a single AI-name line. + Scrollbar *vscroll; AIConfigWindow() : Window() { this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect. + this->vscroll = this->GetScrollbar(AIC_WIDGET_SCROLLBAR); this->selected_slot = INVALID_COMPANY; NWidgetCore *nwi = this->GetWidget(AIC_WIDGET_LIST); - this->vscroll.SetCapacity(nwi->current_y / this->line_height); - this->vscroll.SetCount(MAX_COMPANIES); - nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); + this->vscroll->SetCapacity(nwi->current_y / this->line_height); + this->vscroll->SetCount(MAX_COMPANIES); + nwi->widget_data = (this->vscroll->GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START); this->OnInvalidateData(0); } @@ -566,7 +574,7 @@ struct AIConfigWindow : public Window { switch (widget) { case AIC_WIDGET_LIST: { int y = r.top; - for (int i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < MAX_COMPANIES; i++) { + for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < MAX_COMPANIES; i++) { StringID text; if (AIConfig::GetConfig((CompanyID)i)->GetInfo() != NULL) { @@ -603,7 +611,7 @@ struct AIConfigWindow : public Window { } case AIC_WIDGET_LIST: { // Select a slot - this->selected_slot = (CompanyID)this->vscroll.GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height); + this->selected_slot = (CompanyID)this->vscroll->GetScrolledRowFromWidget(pt.y, this, widget, 0, this->line_height); this->InvalidateData(); if (click_count > 1 && this->selected_slot != INVALID_COMPANY) ShowAIListWindow((CompanyID)this->selected_slot); break; @@ -613,7 +621,7 @@ struct AIConfigWindow : public Window { if (this->selected_slot > 1) { Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot - 1]); this->selected_slot--; - this->vscroll.ScrollTowards(this->selected_slot); + this->vscroll->ScrollTowards(this->selected_slot); this->InvalidateData(); } break; @@ -622,7 +630,7 @@ struct AIConfigWindow : public Window { if (this->selected_slot < _settings_newgame.difficulty.max_no_competitors) { Swap(_settings_newgame.ai_config[this->selected_slot], _settings_newgame.ai_config[this->selected_slot + 1]); this->selected_slot++; - this->vscroll.ScrollTowards(this->selected_slot); + this->vscroll->ScrollTowards(this->selected_slot); this->InvalidateData(); } break; @@ -708,10 +716,12 @@ struct AIDebugWindow : public QueryStringBaseWindow { static char break_string[MAX_BREAK_STR_STRING_LENGTH]; ///< The string to match to the AI output static bool case_sensitive_break_check; ///< Is the matching done case-sensitive int highlight_row; ///< The output row that matches the given string, or -1 + Scrollbar *vscroll; AIDebugWindow(const WindowDesc *desc, WindowNumber number) : QueryStringBaseWindow(MAX_BREAK_STR_STRING_LENGTH) { this->CreateNestedTree(desc); + this->vscroll = this->GetScrollbar(AID_WIDGET_SCROLLBAR); this->show_break_box = _settings_client.gui.ai_developer_tools; this->GetWidget(AID_BREAK_STRING_WIDGETS)->SetDisplayedPlane(this->show_break_box ? 0 : SZSP_HORIZONTAL); this->FinishInitNested(desc, number); @@ -824,8 +834,8 @@ struct AIDebugWindow : public QueryStringBaseWindow { cur_company.Restore(); int scroll_count = (log == NULL) ? 0 : log->used; - if (this->vscroll.GetCount() != scroll_count) { - this->vscroll.SetCount(scroll_count); + if (this->vscroll->GetCount() != scroll_count) { + this->vscroll->SetCount(scroll_count); /* We need a repaint */ this->SetWidgetDirty(AID_WIDGET_SCROLLBAR); @@ -835,20 +845,20 @@ struct AIDebugWindow : public QueryStringBaseWindow { /* Detect when the user scrolls the window. Enable autoscroll when the * bottom-most line becomes visible. */ - if (this->last_vscroll_pos != this->vscroll.GetPosition()) { - this->autoscroll = this->vscroll.GetPosition() >= log->used - this->vscroll.GetCapacity(); + if (this->last_vscroll_pos != this->vscroll->GetPosition()) { + this->autoscroll = this->vscroll->GetPosition() >= log->used - this->vscroll->GetCapacity(); } if (this->autoscroll) { - int scroll_pos = max(0, log->used - this->vscroll.GetCapacity()); - if (scroll_pos != this->vscroll.GetPosition()) { - this->vscroll.SetPosition(scroll_pos); + int scroll_pos = max(0, log->used - this->vscroll->GetCapacity()); + if (scroll_pos != this->vscroll->GetPosition()) { + this->vscroll->SetPosition(scroll_pos); /* We need a repaint */ this->SetWidgetDirty(AID_WIDGET_SCROLLBAR); this->SetWidgetDirty(AID_WIDGET_LOG_PANEL); } } - this->last_vscroll_pos = this->vscroll.GetPosition(); + this->last_vscroll_pos = this->vscroll->GetPosition(); } virtual void SetStringParameters(int widget) const @@ -880,7 +890,7 @@ struct AIDebugWindow : public QueryStringBaseWindow { if (log == NULL) return; int y = this->top_offset; - for (int i = this->vscroll.GetPosition(); this->vscroll.IsVisible(i) && i < log->used; i++) { + for (int i = this->vscroll->GetPosition(); this->vscroll->IsVisible(i) && i < log->used; i++) { int pos = (i + log->pos + 1 - log->used + log->count) % log->count; if (log->lines[pos] == NULL) break; @@ -916,11 +926,11 @@ struct AIDebugWindow : public QueryStringBaseWindow { Backup cur_company(_current_company, ai_debug_company, FILE_LINE); AILog::LogData *log = (AILog::LogData *)AIObject::GetLogPointer(); cur_company.Restore(); - this->vscroll.SetCount((log == NULL) ? 0 : log->used); + this->vscroll->SetCount((log == NULL) ? 0 : log->used); this->LowerWidget(ai_debug_company + AID_WIDGET_COMPANY_BUTTON_START); this->autoscroll = true; - this->last_vscroll_pos = this->vscroll.GetPosition(); + this->last_vscroll_pos = this->vscroll->GetPosition(); this->SetDirty(); /* Close AI settings window to prevent confusion */ DeleteWindowByClass(WC_AI_SETTINGS); @@ -1034,7 +1044,7 @@ struct AIDebugWindow : public QueryStringBaseWindow { virtual void OnResize() { - this->vscroll.SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL); + this->vscroll->SetCapacityFromWidget(this, AID_WIDGET_LOG_PANEL); } }; -- cgit v1.2.3-70-g09d2