changeset 13695:75560523443c draft

(svn r18220) -Codechange: Reduce number of nested widget dynamic casts.
author alberth <alberth@openttd.org>
date Sun, 22 Nov 2009 13:22:53 +0000 (2009-11-22)
parents c6a83b1d3775
children 619a107df4a1
files src/ai/ai_gui.cpp src/autoreplace_gui.cpp src/build_vehicle_gui.cpp src/cheat_gui.cpp src/company_gui.cpp src/depot_gui.cpp src/genworld_gui.cpp src/graph_gui.cpp src/industry_gui.cpp src/network/network_content_gui.cpp src/network/network_gui.cpp src/newgrf_gui.cpp src/settings_gui.cpp src/smallmap_gui.cpp
diffstat 14 files changed, 48 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -203,8 +203,9 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetCore>(AIL_WIDGET_LIST)->current_y / this->line_height);
-		this->GetWidget<NWidgetCore>(AIL_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(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);
 	}
 };
 
@@ -408,8 +409,9 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND)->current_y / this->line_height);
-		this->GetWidget<NWidgetCore>(AIS_WIDGET_BACKGROUND)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(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);
 	}
 
 	virtual void OnTick()
@@ -520,9 +522,10 @@
 	{
 		this->InitNested(&_ai_config_desc); // Initializes 'this->line_height' as a side effect.
 		this->selected_slot = INVALID_COMPANY;
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(AIC_WIDGET_LIST)->current_y / this->line_height);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST);
+		this->vscroll.SetCapacity(nwi->current_y / this->line_height);
 		this->vscroll.SetCount(MAX_COMPANIES);
-		this->GetWidget<NWidgetCore>(AIC_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 		this->OnInvalidateData(0);
 	}
 
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -376,7 +376,7 @@
 		/* Draw details panels. */
 		for (int side = 0; side < 2; side++) {
 			if (this->sel_engine[side] != INVALID_ENGINE) {
-				NWidgetCore *nwi = this->GetWidget<NWidgetCore>(side == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS);
+				NWidgetBase *nwi = this->GetWidget<NWidgetBase>(side == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS);
 				int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
 						nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine[side]);
 				needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -1017,7 +1017,7 @@
 				break;
 
 			case BUILD_VEHICLE_WIDGET_LIST: {
-				uint i = (pt.y - this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
+				uint i = (pt.y - this->GetWidget<NWidgetBase>(BUILD_VEHICLE_WIDGET_LIST)->pos_y) / this->resize.step_height + this->vscroll.GetPosition();
 				size_t num_items = this->eng_list.Length();
 				this->sel_engine = (i < num_items) ? this->eng_list[i] : INVALID_ENGINE;
 				this->SetDirty();
@@ -1124,7 +1124,7 @@
 		/* Draw details panels. */
 		for (int side = 0; side < 2; side++) {
 			if (this->sel_engine != INVALID_ENGINE) {
-				NWidgetCore *nwi = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_PANEL);
+				NWidgetBase *nwi = this->GetWidget<NWidgetBase>(BUILD_VEHICLE_WIDGET_PANEL);
 				int text_end = DrawVehiclePurchaseInfo(nwi->pos_x + WD_FRAMETEXT_LEFT, nwi->pos_x + nwi->current_x - WD_FRAMETEXT_RIGHT,
 						nwi->pos_y + WD_FRAMERECT_TOP, this->sel_engine);
 				needed_height = max(needed_height, text_end - (int)nwi->pos_y + WD_FRAMERECT_BOTTOM);
@@ -1178,8 +1178,9 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity((this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST)->current_y) / this->resize.step_height);
-		this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(BUILD_VEHICLE_WIDGET_LIST);
+		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
+		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 };
 
--- a/src/cheat_gui.cpp
+++ b/src/cheat_gui.cpp
@@ -273,7 +273,7 @@
 
 	virtual void OnClick(Point pt, int widget)
 	{
-		const NWidgetCore *wid = this->GetWidget<NWidgetCore>(CW_PANEL);
+		const NWidgetBase *wid = this->GetWidget<NWidgetBase>(CW_PANEL);
 		uint btn = (pt.y - wid->pos_y - WD_FRAMERECT_TOP - this->header_height) / (FONT_HEIGHT_NORMAL + WD_PAR_VSEP_NORMAL);
 		uint x = pt.x - wid->pos_x;
 		bool rtl = _dynlang.text_dir == TD_RTL;
--- a/src/company_gui.cpp
+++ b/src/company_gui.cpp
@@ -413,7 +413,7 @@
 		if (!small) {
 			/* Check that the expenses panel height matches the height needed for the layout. */
 			int type = _settings_client.gui.expenses_layout;
-			if (_expenses_list_types[type].GetHeight() != this->GetWidget<NWidgetCore>(CFW_EXPS_CATEGORY)->current_y) {
+			if (_expenses_list_types[type].GetHeight() != this->GetWidget<NWidgetBase>(CFW_EXPS_CATEGORY)->current_y) {
 				this->SetupWidgets();
 				this->ReInit();
 				return;
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -942,13 +942,14 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->current_y / (int)this->resize.step_height);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX);
+		this->vscroll.SetCapacity(nwi->current_y / (int)this->resize.step_height);
 		if (this->type == VEH_TRAIN) {
-			this->hscroll.SetCapacity(this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->current_x - this->header_width - this->count_width);
-			this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+			this->hscroll.SetCapacity(nwi->current_x - this->header_width - this->count_width);
+			nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 		} else {
-			this->hscroll.SetCapacity(this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->current_x / (int)this->resize.step_width);
-			this->GetWidget<NWidgetCore>(DEPOT_WIDGET_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (this->hscroll.GetCapacity() << MAT_COL_START);
+			this->hscroll.SetCapacity(nwi->current_x / (int)this->resize.step_width);
+			nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (this->hscroll.GetCapacity() << MAT_COL_START);
 		}
 	}
 
--- a/src/genworld_gui.cpp
+++ b/src/genworld_gui.cpp
@@ -481,7 +481,7 @@
 				 * spaced without doing much magic. The space we can draw on is
 				 * covered by both the spacer and text widgets, so take their
 				 * outer most boundaries (left and right) as draw locations. */
-				const NWidgetCore *nwi_spacer = this->GetWidget<NWidgetCore>(GLAND_HEIGHTMAP_NAME_SPACER);
+				const NWidgetBase *nwi_spacer = this->GetWidget<NWidgetBase>(GLAND_HEIGHTMAP_NAME_SPACER);
 				DrawString(min(r.left, nwi_spacer->pos_x), max<int>(r.right, nwi_spacer->pos_x + nwi_spacer->current_x), r.top, this->name, TC_ORANGE);
 			} break;
 		}
--- a/src/graph_gui.cpp
+++ b/src/graph_gui.cpp
@@ -465,7 +465,7 @@
 	{
 		this->DrawWidgets();
 
-		NWidgetCore *nwid = this->GetWidget<NWidgetCore>(this->graph_widget);
+		NWidgetBase *nwid = this->GetWidget<NWidgetBase>(this->graph_widget);
 		Rect r;
 		r.left = nwid->pos_x;
 		r.right = nwid->pos_x + nwid->current_x - 1;
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -594,7 +594,7 @@
 	{
 		this->DrawWidgets();
 
-		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(IVW_INFO);
+		NWidgetBase *nwi = this->GetWidget<NWidgetBase>(IVW_INFO);
 		uint expected = this->DrawInfo(nwi->pos_x, nwi->pos_x + nwi->current_x - 1, nwi->pos_y) - nwi->pos_y;
 		if (expected > nwi->current_y - 1) {
 			this->info_height = expected + 1;
@@ -720,7 +720,7 @@
 				uint x = pt.x;
 				int line = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
 				if (pt.y >= this->production_offset_y && IsInsideMM(line, 0, 2) && i->produced_cargo[line] != CT_INVALID) {
-					NWidgetCore *nwi = this->GetWidget<NWidgetCore>(widget);
+					NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
 					uint left = nwi->pos_x + WD_FRAMETEXT_LEFT;
 					uint right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
 					if (IsInsideMM(x, left, left + 20) ) {
--- a/src/network/network_content_gui.cpp
+++ b/src/network/network_content_gui.cpp
@@ -439,9 +439,9 @@
 
 	void DrawMatrix(const Rect &r) const
 	{
-		const NWidgetCore *nwi_checkbox = this->GetWidget<NWidgetCore>(NCLWW_CHECKBOX);
-		const NWidgetCore *nwi_name = this->GetWidget<NWidgetCore>(NCLWW_NAME);
-		const NWidgetCore *nwi_type = this->GetWidget<NWidgetCore>(NCLWW_TYPE);
+		const NWidgetBase *nwi_checkbox = this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX);
+		const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(NCLWW_NAME);
+		const NWidgetBase *nwi_type = this->GetWidget<NWidgetBase>(NCLWW_TYPE);
 
 
 		/* Fill the matrix with the information */
@@ -608,7 +608,7 @@
 				this->selected = *this->content.Get(id_v);
 				this->list_pos = id_v;
 
-				if (pt.x <= (int)(this->GetWidget<NWidgetCore>(NCLWW_CHECKBOX)->pos_y + this->GetWidget<NWidgetCore>(NCLWW_CHECKBOX)->current_y)) {
+				if (pt.x <= (int)(this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX)->pos_y + this->GetWidget<NWidgetBase>(NCLWW_CHECKBOX)->current_y)) {
 					_network_content_client.ToggleSelectedState(this->selected);
 					this->content.ForceResort();
 				}
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -368,8 +368,8 @@
 	 */
 	void DrawServerLine(const NetworkGameList *cur_item, uint y, bool highlight) const
 	{
-		const NWidgetCore *nwi_name = this->GetWidget<NWidgetCore>(NGWW_NAME);
-		const NWidgetCore *nwi_info = this->GetWidget<NWidgetCore>(NGWW_INFO);
+		const NWidgetBase *nwi_name = this->GetWidget<NWidgetBase>(NGWW_NAME);
+		const NWidgetBase *nwi_info = this->GetWidget<NWidgetBase>(NGWW_INFO);
 
 		/* show highlighted item with a different colour */
 		if (highlight) GfxFillRect(nwi_name->pos_x + 1, y - 2, nwi_info->pos_x + nwi_info->current_x - 2, y + FONT_HEIGHT_NORMAL - 1, 10);
@@ -381,7 +381,7 @@
 			const NWidgetServerListHeader *nwi_header = this->GetWidget<NWidgetServerListHeader>(NGWW_HEADER);
 
 			if (nwi_header->IsWidgetVisible(NGWW_CLIENTS)) {
-				const NWidgetCore *nwi_clients = this->GetWidget<NWidgetCore>(NGWW_CLIENTS);
+				const NWidgetBase *nwi_clients = this->GetWidget<NWidgetBase>(NGWW_CLIENTS);
 				SetDParam(0, cur_item->info.clients_on);
 				SetDParam(1, cur_item->info.clients_max);
 				SetDParam(2, cur_item->info.companies_on);
@@ -391,7 +391,7 @@
 
 			if (nwi_header->IsWidgetVisible(NGWW_MAPSIZE)) {
 				/* map size */
-				const NWidgetCore *nwi_mapsize = this->GetWidget<NWidgetCore>(NGWW_MAPSIZE);
+				const NWidgetBase *nwi_mapsize = this->GetWidget<NWidgetBase>(NGWW_MAPSIZE);
 				SetDParam(0, cur_item->info.map_width);
 				SetDParam(1, cur_item->info.map_height);
 				DrawString(nwi_mapsize->pos_x, nwi_mapsize->pos_x + nwi_mapsize->current_x - 1, y, STR_NETWORK_SERVER_LIST_MAP_SIZE_SHORT, TC_FROMSTRING, SA_CENTER);
@@ -399,7 +399,7 @@
 
 			if (nwi_header->IsWidgetVisible(NGWW_DATE)) {
 				/* current date */
-				const NWidgetCore *nwi_date = this->GetWidget<NWidgetCore>(NGWW_DATE);
+				const NWidgetBase *nwi_date = this->GetWidget<NWidgetBase>(NGWW_DATE);
 				YearMonthDay ymd;
 				ConvertDateToYMD(cur_item->info.game_date, &ymd);
 				SetDParam(0, ymd.year);
@@ -408,7 +408,7 @@
 
 			if (nwi_header->IsWidgetVisible(NGWW_YEARS)) {
 				/* number of years the game is running */
-				const NWidgetCore *nwi_years = this->GetWidget<NWidgetCore>(NGWW_YEARS);
+				const NWidgetBase *nwi_years = this->GetWidget<NWidgetBase>(NGWW_YEARS);
 				YearMonthDay ymd_cur, ymd_start;
 				ConvertDateToYMD(cur_item->info.game_date, &ymd_cur);
 				ConvertDateToYMD(cur_item->info.start_date, &ymd_start);
@@ -694,7 +694,7 @@
 				break;
 
 			case NGWW_MATRIX: { // Matrix to show networkgames
-				uint32 id_v = (pt.y - this->GetWidget<NWidgetCore>(NGWW_MATRIX)->pos_y) / this->resize.step_height;
+				uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NGWW_MATRIX)->pos_y) / this->resize.step_height;
 
 				if (id_v >= this->vscroll.GetCapacity()) return; // click out of bounds
 				id_v += this->vscroll.GetPosition();
@@ -1185,7 +1185,7 @@
 				break;
 
 			case NSSW_SELMAP: { // Select map
-				int y = (pt.y - this->GetWidget<NWidgetCore>(NSSW_SELMAP)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
+				int y = (pt.y - this->GetWidget<NWidgetBase>(NSSW_SELMAP)->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_NORMAL;
 
 				y += this->vscroll.GetPosition();
 				if (y >= this->vscroll.GetCount()) return;
@@ -1645,7 +1645,7 @@
 				break;
 
 			case NLWW_MATRIX: { // Company list
-				uint32 id_v = (pt.y - this->GetWidget<NWidgetCore>(NLWW_MATRIX)->pos_y) / this->resize.step_height;
+				uint32 id_v = (pt.y - this->GetWidget<NWidgetBase>(NLWW_MATRIX)->pos_y) / this->resize.step_height;
 
 				if (id_v >= this->vscroll.GetCapacity()) break;
 
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -609,8 +609,9 @@
 
 	virtual void OnResize()
 	{
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->current_y / this->resize.step_height);
-		this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		NWidgetCore *nwi = this->GetWidget<NWidgetCore>(SNGRFS_FILE_LIST);
+		this->vscroll.SetCapacity(nwi->current_y / this->resize.step_height);
+		nwi->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
 	}
 
 	virtual void SetStringParameters(int widget) const
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -1504,7 +1504,7 @@
 	{
 		if (widget != SETTINGSEL_OPTIONSPANEL) return;
 
-		int y = pt.y - this->GetWidget<NWidgetCore>(widget)->pos_y - SETTINGTREE_TOP_OFFSET;  // Shift y coordinate
+		int y = pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y - SETTINGTREE_TOP_OFFSET;  // Shift y coordinate
 		if (y < 0) return;  // Clicked above first entry
 
 		byte btn = this->vscroll.GetPosition() + y / this->resize.step_height;  // Compute which setting is selected
@@ -1767,7 +1767,7 @@
 			case CUSTCURR_SEPARATOR_EDIT:
 			case CUSTCURR_PREFIX_EDIT:
 			case CUSTCURR_SUFFIX_EDIT:
-				size->width  = this->GetWidget<NWidgetCore>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetCore>(CUSTCURR_RATE_UP)->smallest_x;
+				size->width  = this->GetWidget<NWidgetBase>(CUSTCURR_RATE_DOWN)->smallest_x + this->GetWidget<NWidgetBase>(CUSTCURR_RATE_UP)->smallest_x;
 				break;
 
 			/* Make sure the window is wide enough for the widest exchange rate */
--- a/src/smallmap_gui.cpp
+++ b/src/smallmap_gui.cpp
@@ -1016,7 +1016,7 @@
 				/* If industry type small map*/
 				if (this->map_type == SMT_INDUSTRY) {
 					/* If click on industries label, find right industry type and enable/disable it */
-					const NWidgetCore *wi = this->GetWidget<NWidgetCore>(SM_WIDGET_LEGEND); // Label panel
+					const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_LEGEND); // Label panel
 					uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
 					if (line >= this->number_of_rows) break;
 
@@ -1103,7 +1103,7 @@
 			}
 		}
 
-		const NWidgetCore *wi = this->GetWidget<NWidgetCore>(SM_WIDGET_MAP);
+		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_MAP);
 		int hx = wi->current_x / 2;
 		int hy = wi->current_y / 2;
 		int hvx = hx * -4 + hy * 8;
@@ -1135,7 +1135,7 @@
 	void SmallMapCenterOnCurrentPos()
 	{
 		const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
-		const NWidgetCore *wi = this->GetWidget<NWidgetCore>(SM_WIDGET_MAP);
+		const NWidgetBase *wi = this->GetWidget<NWidgetBase>(SM_WIDGET_MAP);
 
 		int x = ((vp->virtual_width  - (int)wi->current_x * TILE_SIZE) / 2 + vp->virtual_left) / 4;
 		int y = ((vp->virtual_height - (int)wi->current_y * TILE_SIZE) / 2 + vp->virtual_top ) / 2 - TILE_SIZE * 2;