changeset 15876:625f70985d01 draft

(svn r20558) -Codechange: use one generic function to create a list of company buttons
author yexo <yexo@openttd.org>
date Thu, 19 Aug 2010 12:56:31 +0000
parents 0b2f6a3e0f65
children 82c114e71a7d
files src/ai/ai_gui.cpp src/graph_gui.cpp src/widget.cpp src/widget_type.h
diffstat 4 files changed, 69 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -701,7 +701,7 @@
 	AID_WIDGET_LOG_PANEL,
 	AID_WIDGET_SCROLLBAR,
 	AID_WIDGET_COMPANY_BUTTON_START,
-	AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + 14,
+	AID_WIDGET_COMPANY_BUTTON_END = AID_WIDGET_COMPANY_BUTTON_START + MAX_COMPANIES - 1,
 	AID_BREAK_STRING_WIDGETS,
 	AID_WIDGET_BREAK_STR_ON_OFF_BTN,
 	AID_WIDGET_BREAK_STR_EDIT_BOX,
@@ -1066,6 +1066,12 @@
 bool AIDebugWindow::break_check_enabled = true;
 bool AIDebugWindow::case_sensitive_break_check = false;
 
+/** Make a number of rows with buttons for each company for the AI debug window. */
+NWidgetBase *MakeCompanyButtonRowsAIDebug(int *biggest_index)
+{
+	return MakeCompanyButtonRows(biggest_index, AID_WIDGET_COMPANY_BUTTON_START, AID_WIDGET_COMPANY_BUTTON_END, 8, STR_AI_DEBUG_SELECT_AI_TOOLTIP);
+}
+
 /** Widgets for the AI debug window. */
 static const NWidgetPart _nested_ai_debug_widgets[] = {
 	NWidget(NWID_HORIZONTAL),
@@ -1075,45 +1081,7 @@
 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
 	EndContainer(),
 	NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_VIEW),
-		NWidget(NWID_HORIZONTAL),
-			NWidget(NWID_SPACER), SetMinimalSize(2, 0),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 1), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 2), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 3), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 4), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 5), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 6), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 7), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(NWID_SPACER), SetMinimalSize(2, 0),
-		EndContainer(),
-		NWidget(NWID_HORIZONTAL),
-			NWidget(NWID_SPACER), SetMinimalSize(2, 0),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 8), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 9), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 10), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 11), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 12), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 13), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(WWT_PANEL, COLOUR_GREY, AID_WIDGET_COMPANY_BUTTON_START + 14), SetMinimalSize(37, 13), SetResize(1, 0), SetDataTip(0x0, STR_AI_DEBUG_SELECT_AI_TOOLTIP),
-			EndContainer(),
-			NWidget(NWID_SPACER), SetMinimalSize(39, 0), SetResize(1, 0),
-		EndContainer(),
-		NWidget(NWID_SPACER), SetMinimalSize(0, 1), SetResize(1, 0),
+		NWidgetFunction(MakeCompanyButtonRowsAIDebug), SetPadding(0, 2, 1, 2),
 	EndContainer(),
 	NWidget(NWID_HORIZONTAL),
 		NWidget(WWT_TEXTBTN, COLOUR_GREY, AID_WIDGET_NAME_TEXT), SetFill(1, 0), SetResize(1, 0), SetDataTip(STR_JUST_STRING, STR_AI_DEBUG_NAME_TOOLTIP),
--- a/src/graph_gui.cpp
+++ b/src/graph_gui.cpp
@@ -1554,55 +1554,10 @@
 	return vert;
 }
 
-/**
- * Make a number of rows with button-like graphics, for enabling/disabling each company.
- * @param biggest_index Storage for collecting the biggest index used in the returned tree.
- * @return Panel with rows of company buttons.
- * @post \c *biggest_index contains the largest used index in the tree.
- */
-static NWidgetBase *MakeCompanyButtonRows(int *biggest_index)
+/** Make a number of rows with buttons for each company for the performance rating detail window. */
+NWidgetBase *MakeCompanyButtonRowsGraphGUI(int *biggest_index)
 {
-	static const int MAX_LENGTH = 8; // Maximal number of company buttons in one row.
-	NWidgetVertical *vert = NULL; // Storage for all rows.
-	NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
-	int hor_length = 0;
-
-	Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
-	sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
-	sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
-
-	for (int widnum = PRW_COMPANY_FIRST; widnum <= PRW_COMPANY_LAST; widnum++) {
-		/* Ensure there is room in 'hor' for another button. */
-		if (hor_length == MAX_LENGTH) {
-			if (vert == NULL) vert = new NWidgetVertical();
-			vert->Add(hor);
-			hor = NULL;
-			hor_length = 0;
-		}
-		if (hor == NULL) {
-			hor = new NWidgetHorizontal();
-			hor_length = 0;
-		}
-
-		NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
-		panel->SetMinimalSize(sprite_size.width, sprite_size.height);
-		panel->SetFill(1, 0);
-		panel->SetDataTip(0x0, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
-		hor->Add(panel);
-		hor_length++;
-	}
-	*biggest_index = PRW_COMPANY_LAST;
-	if (vert == NULL) return hor; // All buttons fit in a single row.
-
-	if (hor_length > 0 && hor_length < MAX_LENGTH) {
-		/* Last row is partial, add a spacer at the end to force all buttons to the left. */
-		NWidgetSpacer *spc = new NWidgetSpacer(0, 0);
-		spc->SetMinimalSize(sprite_size.width, sprite_size.height);
-		spc->SetFill(1, 0);
-		hor->Add(spc);
-	}
-	if (hor != NULL) vert->Add(hor);
-	return vert;
+	return MakeCompanyButtonRows(biggest_index, PRW_COMPANY_FIRST, PRW_COMPANY_LAST, 8, STR_PERFORMANCE_DETAIL_SELECT_COMPANY_TOOLTIP);
 }
 
 static const NWidgetPart _nested_performance_rating_detail_widgets[] = {
@@ -1613,7 +1568,7 @@
 		NWidget(WWT_STICKYBOX, COLOUR_GREY),
 	EndContainer(),
 	NWidget(WWT_PANEL, COLOUR_GREY),
-		NWidgetFunction(MakeCompanyButtonRows), SetPadding(0, 1, 1, 2),
+		NWidgetFunction(MakeCompanyButtonRowsGraphGUI), SetPadding(0, 1, 1, 2),
 	EndContainer(),
 	NWidgetFunction(MakePerformanceDetailPanels),
 };
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -2404,3 +2404,58 @@
 	*biggest_index = max(*biggest_index, biggest2);
 	return root;
 }
+
+/**
+ * Make a number of rows with button-like graphics, for enabling/disabling each company.
+ * @param biggest_index Storage for collecting the biggest index used in the returned tree.
+ * @param widget_first The first widget index to use.
+ * @param widget_last The last widget index to use.
+ * @param max_length Maximal number of company buttons in one row.
+ * @param button_tooltip The tooltip-string of every button.
+ * @return Panel with rows of company buttons.
+ * @post \c *biggest_index contains the largest used index in the tree.
+ */
+NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip)
+{
+	NWidgetVertical *vert = NULL; // Storage for all rows.
+	NWidgetHorizontal *hor = NULL; // Storage for buttons in one row.
+	int hor_length = 0;
+
+	Dimension sprite_size = GetSpriteSize(SPR_COMPANY_ICON);
+	sprite_size.width  += WD_MATRIX_LEFT + WD_MATRIX_RIGHT;
+	sprite_size.height += WD_MATRIX_TOP + WD_MATRIX_BOTTOM + 1; // 1 for the 'offset' of being pressed
+
+	for (int widnum = widget_first; widnum <= widget_last; widnum++) {
+		/* Ensure there is room in 'hor' for another button. */
+		if (hor_length == max_length) {
+			if (vert == NULL) vert = new NWidgetVertical();
+			vert->Add(hor);
+			hor = NULL;
+			hor_length = 0;
+		}
+		if (hor == NULL) {
+			hor = new NWidgetHorizontal();
+			hor_length = 0;
+		}
+
+		NWidgetBackground *panel = new NWidgetBackground(WWT_PANEL, COLOUR_GREY, widnum);
+		panel->SetMinimalSize(sprite_size.width, sprite_size.height);
+		panel->SetFill(1, 0);
+		panel->SetResize(1, 0);
+		panel->SetDataTip(0x0, button_tooltip);
+		hor->Add(panel);
+		hor_length++;
+	}
+	*biggest_index = widget_last;
+	if (vert == NULL) return hor; // All buttons fit in a single row.
+
+	if (hor_length > 0 && hor_length < max_length) {
+		/* Last row is partial, add a spacer at the end to force all buttons to the left. */
+		NWidgetSpacer *spc = new NWidgetSpacer(sprite_size.width, sprite_size.height);
+		spc->SetFill(1, 0);
+		spc->SetResize(1, 0);
+		hor->Add(spc);
+	}
+	if (hor != NULL) vert->Add(hor);
+	return vert;
+}
--- a/src/widget_type.h
+++ b/src/widget_type.h
@@ -1022,4 +1022,6 @@
 NWidgetContainer *MakeNWidgets(const NWidgetPart *parts, int count, int *biggest_index, NWidgetContainer *container);
 NWidgetContainer *MakeWindowNWidgetTree(const NWidgetPart *parts, int count, int *biggest_index, NWidgetStacked **shade_select);
 
+NWidgetBase *MakeCompanyButtonRows(int *biggest_index, int widget_first, int widget_last, int max_length, StringID button_tooltip);
+
 #endif /* WIDGET_TYPE_H */