changeset 13594:75bdacca0c55 draft

(svn r18118) -Codechange: improve behaviour of the engine lists (both build vehicle/autoreplace) w.r.t. large(r) fonts and RTL. RTL support for 'wider than usual' vehicles is still needed though.
author rubidium <rubidium@openttd.org>
date Mon, 16 Nov 2009 16:22:14 +0000
parents c053bbc41f23
children 359508f4dd22
files src/autoreplace_gui.cpp src/build_vehicle_gui.cpp
diffstat 2 files changed, 41 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -28,6 +28,7 @@
 
 #include "table/strings.h"
 
+uint GetEngineListHeight(VehicleType type);
 void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group);
 
 /** Widget numbers of the autoreplace GUI. */
@@ -226,11 +227,8 @@
 
 		this->InitNested(desc, vehicletype);
 
-		this->vscroll.SetCapacity(this->GetWidget<NWidgetBase>(RVW_WIDGET_LEFT_MATRIX)->current_y / this->resize.step_height);
-		this->vscroll2.SetCapacity(this->vscroll.GetCapacity());   // these two are always the same
-
-		this->GetWidget<NWidgetCore>(RVW_WIDGET_LEFT_MATRIX)->widget_data =
-				this->GetWidget<NWidgetCore>(RVW_WIDGET_RIGHT_MATRIX)->widget_data = (this->vscroll.GetCapacity() << MAT_ROW_START) + (1 << MAT_COL_START);
+		/* Update the scrollbars/matrix 'definitions' */
+		this->OnResize();
 
 		this->owner = _local_company;
 		this->sel_group = id_g;
@@ -241,8 +239,8 @@
 		switch (widget) {
 			case RVW_WIDGET_LEFT_MATRIX:
 			case RVW_WIDGET_RIGHT_MATRIX:
-				resize->height = GetVehicleListHeight((VehicleType)this->window_number);
-				size->height = (resize->height <= 14 ? 8 : 4) * resize->height;
+				resize->height = GetEngineListHeight((VehicleType)this->window_number);
+				size->height = (this->window_number <= VEH_ROAD ? 8 : 4) * resize->height;
 				break;
 
 			case RVW_WIDGET_LEFT_DETAILS:
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -35,6 +35,16 @@
 #include "table/sprites.h"
 #include "table/strings.h"
 
+/**
+ * Get the height of a single 'entry' in the engine lists.
+ * @param type the vehicle type to get the height of
+ * @return the height for the entry
+ */
+uint GetEngineListHeight(VehicleType type)
+{
+	return max<uint>(FONT_HEIGHT_NORMAL + WD_MATRIX_TOP + WD_MATRIX_BOTTOM, GetVehicleListHeight(type));
+}
+
 enum BuildVehicleWidgets {
 	BUILD_VEHICLE_WIDGET_CLOSEBOX = 0,
 	BUILD_VEHICLE_WIDGET_CAPTION,
@@ -678,7 +688,7 @@
 
 /** Engine drawing loop
  * @param type Type of vehicle (VEH_*)
- * @param x The left most location of the list
+ * @param l The left most location of the list
  * @param r The right most location of the list
  * @param y The top most location of teh list
  * @param eng_list What engines to draw
@@ -688,38 +698,28 @@
  * @param show_count Whether to show the amount of engines or not
  * @param selected_group the group to list the engines of
  */
-void DrawEngineList(VehicleType type, int x, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group)
+void DrawEngineList(VehicleType type, int l, int r, int y, const GUIEngineList *eng_list, uint16 min, uint16 max, EngineID selected_id, bool show_count, GroupID selected_group)
 {
-	byte step_size = GetVehicleListHeight(type);
-	byte x_offset = 0;
-	byte y_offset = 0;
+	static const int sprite_widths[]  = { 60, 60, 76, 67 };
+	static const int sprite_y_offsets[] = { -1, -1, -2, -2 };
 
+	/* Obligatory sanity checks! */
+	assert((uint)type < lengthof(sprite_widths));
+	assert_compile(lengthof(sprite_y_offsets) == lengthof(sprite_widths));
 	assert(max <= eng_list->Length());
 
-	switch (type) {
-		case VEH_TRAIN:
-			x++; // train and road vehicles use the same offset, except trains are one more pixel to the right
-			/* Fallthough */
-		case VEH_ROAD:
-			x += 26;
-			x_offset = 30;
-			y += 2;
-			y_offset = 4;
-			break;
-		case VEH_SHIP:
-			x += 35;
-			x_offset = 40;
-			y += 7;
-			y_offset = 3;
-			break;
-		case VEH_AIRCRAFT:
-			x += 27;
-			x_offset = 33;
-			y += 7;
-			y_offset = 3;
-			break;
-		default: NOT_REACHED();
-	}
+	bool rtl = _dynlang.text_dir == TD_RTL;
+	int step_size = GetEngineListHeight(type);
+	int sprite_width = sprite_widths[type];
+
+	int sprite_x        = (rtl ? r - sprite_width / 2 : l + sprite_width / 2) - 1;
+	int sprite_y_offset = sprite_y_offsets[type] + step_size / 2;
+
+	int text_left  = l + (rtl ? WD_FRAMERECT_LEFT : sprite_width);
+	int text_right = r - (rtl ? sprite_width : WD_FRAMERECT_RIGHT);
+
+	int normal_text_y_offset = (step_size - FONT_HEIGHT_NORMAL) / 2;
+	int small_text_y_offset  = step_size - FONT_HEIGHT_SMALL - WD_FRAMERECT_BOTTOM - 1;
 
 	for (; min < max; min++, y += step_size) {
 		const EngineID engine = (*eng_list)[min];
@@ -727,11 +727,11 @@
 		const uint num_engines = GetGroupNumEngines(_local_company, selected_group, engine);
 
 		SetDParam(0, engine);
-		DrawString(x + x_offset, r, y, STR_ENGINE_NAME, engine == selected_id ? TC_WHITE : TC_BLACK);
-		DrawVehicleEngine(x, y + y_offset, engine, (show_count && num_engines == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_company));
+		DrawString(text_left, text_right, y + normal_text_y_offset, STR_ENGINE_NAME, engine == selected_id ? TC_WHITE : TC_BLACK);
+		DrawVehicleEngine(sprite_x, y + sprite_y_offset, engine, (show_count && num_engines == 0) ? PALETTE_CRASH : GetEnginePalette(engine, _local_company));
 		if (show_count) {
 			SetDParam(0, num_engines);
-			DrawString(x, r, y + (GetVehicleListHeight(type) == 14 ? 3 : 8), STR_TINY_BLACK_COMA, TC_FROMSTRING, SA_RIGHT);
+			DrawString(text_left, text_right, y + small_text_y_offset, STR_TINY_BLACK_COMA, TC_FROMSTRING, SA_RIGHT);
 		}
 	}
 }
@@ -837,6 +837,9 @@
 
 		this->FinishInitNested(desc, tile == INVALID_TILE ? (int)type : tile);
 
+		/* Update the scrollbars/matrix 'definitions' */
+		this->OnResize();
+
 		this->eng_list.ForceRebuild();
 		this->GenerateBuildList(); // generate the list, since we need it in the next line
 		/* Select the first engine in the list as default when opening the window */
@@ -1090,7 +1093,7 @@
 	{
 		switch (widget) {
 			case BUILD_VEHICLE_WIDGET_LIST:
-				resize->height = GetVehicleListHeight(this->vehicle_type);
+				resize->height = GetEngineListHeight(this->vehicle_type);
 				size->height = 3 * resize->height;
 				break;