changeset 13996:3f141ce0d4ec draft

(svn r18538) -Codechange: Split DrawStationCoverageText into a calculation part and a drawing part.
author alberth <alberth@openttd.org>
date Sat, 19 Dec 2009 15:52:50 +0000
parents bc8b836e1fb9
children fe669c716006
files src/misc_gui.cpp src/station_gui.h
diffstat 2 files changed, 27 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -866,24 +866,22 @@
 	new TooltipsWindow(str, paramcount, params, use_left_mouse_button);
 }
 
-
-static int DrawStationCoverageText(const CargoArray &cargos,
-	int left, int right, int top, StationCoverageType sct, bool supplies)
+/**
+ * Draw a (multi)line of cargos seperated by commas, and prefixed with a string.
+ * @param cargo_mask Mask of cargos to include in the list.
+ * @param r          Rectangle to draw the cargos in.
+ * @param prefix     String to use as prefix for the list of cargos.
+ * @return Bottom position of the last line used for drawing the cargos.
+ */
+int DrawCargoListText(uint32 cargo_mask, const Rect &r, StringID prefix)
 {
 	bool first = true;
-
 	char string[512];
-	char *b = InlineString(string, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
+	char *b = InlineString(string, prefix);
 
 	for (CargoID i = 0; i < NUM_CARGO; i++) {
 		if (b >= lastof(string) - (1 + 2 * 4)) break; // ',' or ' ' and two calls to Utf8Encode()
-		switch (sct) {
-			case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
-			case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
-			case SCT_ALL: break;
-			default: NOT_REACHED();
-		}
-		if (cargos[i] >= (supplies ? 1U : 8U)) {
+		if (HasBit(cargo_mask, i)) {
 			if (first) {
 				first = false;
 			} else {
@@ -904,7 +902,7 @@
 	assert(b < endof(string));
 
 	SetDParamStr(0, string);
-	return DrawStringMultiLine(left, right, top, INT32_MAX, STR_JUST_RAW_STRING);
+	return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, STR_JUST_RAW_STRING);
 }
 
 /**
@@ -927,7 +925,20 @@
 		} else {
 			cargos = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
 		}
-		return DrawStationCoverageText(cargos, left, right, top, sct, supplies);
+
+		/* Convert cargo counts to a set of cargo bits, and draw the result. */
+		uint32 cargo_mask = 0;
+		for (CargoID i = 0; i < NUM_CARGO; i++) {
+			switch (sct) {
+				case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
+				case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
+				case SCT_ALL: break;
+				default: NOT_REACHED();
+			}
+			if (cargos[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
+		}
+		Rect r = {left, top, right, INT32_MAX};
+		return DrawCargoListText(cargo_mask, r, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
 	}
 
 	return top;
--- a/src/station_gui.h
+++ b/src/station_gui.h
@@ -39,6 +39,8 @@
 	SCT_ALL,                 ///< Draw all cargos.
 };
 
+/* misc_gui.cpp */
+int DrawCargoListText(uint32 cargo_mask, const Rect &r, StringID prefix);
 int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies);
 void CheckRedrawStationCoverage(const Window *w);