src/autoreplace_gui.cpp
branchNewGRF_ports
changeset 6872 1c4a4a609f85
parent 6871 5a9dc001e1ad
child 6877 889301acc299
--- a/src/autoreplace_gui.cpp	Mon Dec 03 23:39:38 2007 +0000
+++ b/src/autoreplace_gui.cpp	Tue Jan 22 21:00:30 2008 +0000
@@ -5,18 +5,23 @@
 #include "stdafx.h"
 #include "openttd.h"
 #include "debug.h"
-#include "functions.h"
-#include "table/sprites.h"
-#include "table/strings.h"
-#include "strings.h"
-#include "window.h"
 #include "gui.h"
-#include "command.h"
+#include "command_func.h"
 #include "variables.h"
 #include "vehicle_gui.h"
 #include "newgrf_engine.h"
 #include "group.h"
+#include "rail.h"
+#include "strings_func.h"
+#include "window_func.h"
+#include "vehicle_func.h"
+#include "autoreplace_func.h"
+#include "gfx_func.h"
+#include "player_func.h"
+#include "widgets/dropdown_func.h"
 
+#include "table/sprites.h"
+#include "table/strings.h"
 
 static RailType _railtype_selected_in_replace_gui;
 
@@ -31,6 +36,33 @@
 	INVALID_STRING_ID
 };
 
+enum ReplaceVehicleWindowWidgets {
+	RVW_WIDGET_LEFT_DETAILS = 3,
+	RVW_WIDGET_START_REPLACE,
+	RVW_WIDGET_INFO_TAB,
+	RVW_WIDGET_STOP_REPLACE,
+	RVW_WIDGET_LEFT_MATRIX,
+	RVW_WIDGET_LEFT_SCROLLBAR,
+	RVW_WIDGET_RIGHT_MATRIX,
+	RVW_WIDGET_RIGHT_SCROLLBAR,
+	RVW_WIDGET_RIGHT_DETAILS,
+
+	RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE,
+	RVW_WIDGET_TRAIN_FLUFF_LEFT,
+	RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN,
+	RVW_WIDGET_TRAIN_FLUFF_RIGHT,
+	RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE,
+};
+
+static int CDECL TrainEngineNumberSorter(const void *a, const void *b)
+{
+	const EngineID va = *(const EngineID*)a;
+	const EngineID vb = *(const EngineID*)b;
+	int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb);
+
+	return r;
+}
+
 /* General Vehicle GUI based procedures that are independent of vehicle types */
 void InitializeVehiclesGuiList()
 {
@@ -172,6 +204,7 @@
 		if (e == WP(w, replaceveh_d).sel_engine[i]) selected_engine = e; // The selected engine is still in the list
 	}
 	WP(w, replaceveh_d).sel_engine[i] = selected_engine; // update which engine we selected (the same or none, if it's not in the list anymore)
+	if (type == VEH_TRAIN) EngList_Sort(list, &TrainEngineNumberSorter);
 }
 
 /** Generate the lists
@@ -240,7 +273,7 @@
 
 			Player *p = GetPlayer(_local_player);
 			EngineID selected_id[2];
-			const GroupID selected_group = WP(w,replaceveh_d).sel_group;
+			const GroupID selected_group = WP(w, replaceveh_d).sel_group;
 
 			selected_id[0] = WP(w, replaceveh_d).sel_engine[0];
 			selected_id[1] = WP(w, replaceveh_d).sel_engine[1];
@@ -249,7 +282,7 @@
 			 *    Either list is empty
 			 * or The selected replacement engine has a replacement (to prevent loops)
 			 * or The right list (new replacement) has the existing replacement vehicle selected */
-			w->SetWidgetDisabledState(4,
+			w->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
 										 selected_id[0] == INVALID_ENGINE ||
 										 selected_id[1] == INVALID_ENGINE ||
 										 EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
@@ -258,7 +291,7 @@
 			/* Disable the "Stop Replacing" button if:
 			 *   The left list (existing vehicle) is empty
 			 *   or The selected vehicle has no replacement set up */
-			w->SetWidgetDisabledState(6,
+			w->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
 										 selected_id[0] == INVALID_ENGINE ||
 										 !EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
 
@@ -273,18 +306,18 @@
 				SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
 
 				/* sets the colour of that art thing */
-				w->widget[13].color = _player_colors[_local_player];
-				w->widget[16].color = _player_colors[_local_player];
+				w->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color  = _player_colors[_local_player];
+				w->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player];
+			}
+
+			if (w->window_number == VEH_TRAIN) {
+				/* Show the selected railtype in the pulldown menu */
+				RailType railtype = _railtype_selected_in_replace_gui;
+				w->widget[RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN].data = _rail_types_list[railtype];
 			}
 
 			DrawWindowWidgets(w);
 
-			if (w->window_number == VEH_TRAIN) {
-				/* Draw the selected railtype in the pulldown menu */
-				RailType railtype = _railtype_selected_in_replace_gui;
-				DrawString(157, w->widget[14].top + 1, _rail_types_list[railtype], TC_BLACK);
-			}
-
 			/* sets up the string for the vehicle that is being replaced to */
 			if (selected_id[0] != INVALID_ENGINE) {
 				if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
@@ -297,21 +330,21 @@
 				SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
 			}
 
-			DrawString(145, w->widget[5].top + 1, STR_02BD, TC_BLACK);
+			DrawString(145, w->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK);
 
 			/* Draw the lists */
 			for(byte i = 0; i < 2; i++) {
-				uint16 x        = i == 0 ? 2 : 230; // at what X offset
+				uint widget     = (i == 0) ? RVW_WIDGET_LEFT_MATRIX : RVW_WIDGET_RIGHT_MATRIX;
 				EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
 				EngineID start  = i == 0 ? w->vscroll.pos : w->vscroll2.pos; // what is the offset for the start (scrolling)
 				EngineID end    = min((i == 0 ? w->vscroll.cap : w->vscroll2.cap) + start, EngList_Count(&list));
 
 				/* Do the actual drawing */
-				DrawEngineList((VehicleType)w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
+				DrawEngineList((VehicleType)w->window_number, w->widget[widget].left + 2, w->widget[widget].top + 1, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
 
 				/* Also draw the details if an engine is selected */
 				if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
-					const Widget *wi = &w->widget[i == 0 ? 3 : 11];
+					const Widget *wi = &w->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
 					DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
 				}
 			}
@@ -320,39 +353,38 @@
 
 		case WE_CLICK: {
 			switch (e->we.click.widget) {
-				case 12:
+				case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
 					WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
 					WP(w, replaceveh_d).update_left = true;
 					WP(w, replaceveh_d).init_lists  = true;
 					SetWindowDirty(w);
 					break;
 
-				case 14:
-				case 15: /* Railtype selection dropdown menu */
-					ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, 15, 0, ~GetPlayer(_local_player)->avail_railtypes);
+				case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */
+					ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes);
 					break;
 
-				case 17: /* toggle renew_keep_length */
+				case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */
 					DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE);
 					break;
 
-				case 4: { /* Start replacing */
+				case RVW_WIDGET_START_REPLACE: { /* Start replacing */
 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
 					EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
 				} break;
 
-				case 6: { /* Stop replacing */
+				case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */
 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
 				} break;
 
-				case 7:
-				case 9: {
+				case RVW_WIDGET_LEFT_MATRIX:
+				case RVW_WIDGET_RIGHT_MATRIX: {
 					uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
-					uint16 click_scroll_pos = e->we.click.widget == 7 ? w->vscroll.pos : w->vscroll2.pos;
-					uint16 click_scroll_cap = e->we.click.widget == 7 ? w->vscroll.cap : w->vscroll2.cap;
-					byte click_side         = e->we.click.widget == 7 ? 0 : 1;
+					uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
+					uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
+					byte click_side         = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
 					uint16 engine_count     = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
 
 					if (i < click_scroll_cap) {
@@ -390,8 +422,8 @@
 			w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
 			w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
 
-			w->widget[7].data = (w->vscroll.cap  << 8) + 1;
-			w->widget[9].data = (w->vscroll2.cap << 8) + 1;
+			w->widget[RVW_WIDGET_LEFT_MATRIX].data  = (w->vscroll.cap  << 8) + 1;
+			w->widget[RVW_WIDGET_RIGHT_MATRIX].data = (w->vscroll2.cap << 8) + 1;
 			break;
 
 		case WE_INVALIDATE_DATA:
@@ -423,8 +455,7 @@
 // train specific stuff
 { WWT_PUSHTXTBTN,     RESIZE_TB,    14,     0,   138,   228,   239, STR_REPLACE_ENGINE_WAGON_SELECT,       STR_REPLACE_ENGINE_WAGON_SELECT_HELP},  // widget 12
 {      WWT_PANEL,     RESIZE_TB,    14,   139,   153,   240,   251, 0x0,            STR_NULL},
-{      WWT_PANEL,     RESIZE_TB,    14,   154,   277,   240,   251, 0x0,            STR_REPLACE_HELP_RAILTYPE},
-{    WWT_TEXTBTN,     RESIZE_TB,    14,   278,   289,   240,   251, STR_0225,       STR_REPLACE_HELP_RAILTYPE},
+{   WWT_DROPDOWN,     RESIZE_TB,    14,   154,   289,   240,   251, 0x0,            STR_REPLACE_HELP_RAILTYPE},
 {      WWT_PANEL,     RESIZE_TB,    14,   290,   305,   240,   251, 0x0,            STR_NULL},
 { WWT_PUSHTXTBTN,     RESIZE_TB,    14,   317,   455,   228,   239, STR_REPLACE_REMOVE_WAGON,       STR_REPLACE_REMOVE_WAGON_HELP},
 // end of train specific stuff
@@ -491,38 +522,6 @@
 };
 
 
-void ShowReplaceVehicleWindow(VehicleType vehicletype)
-{
-	Window *w;
-
-	DeleteWindowById(WC_REPLACE_VEHICLE, vehicletype);
-
-	switch (vehicletype) {
-		case VEH_TRAIN:
-			w = AllocateWindowDescFront(&_replace_rail_vehicle_desc, vehicletype);
-			w->vscroll.cap  = 8;
-			w->resize.step_height = 14;
-			WP(w, replaceveh_d).wagon_btnstate = true;
-			break;
-		case VEH_ROAD:
-			w = AllocateWindowDescFront(&_replace_road_vehicle_desc, vehicletype);
-			w->vscroll.cap  = 8;
-			w->resize.step_height = 14;
-			break;
-		case VEH_SHIP:
-		case VEH_AIRCRAFT:
-			w = AllocateWindowDescFront(&_replace_ship_aircraft_vehicle_desc, vehicletype);
-			w->vscroll.cap  = 4;
-			w->resize.step_height = 24;
-			break;
-		default: return;
-	}
-
-	w->caption_color = _local_player;
-	w->vscroll2.cap = w->vscroll.cap;   // these two are always the same
-	WP(w, replaceveh_d).sel_group = DEFAULT_GROUP;
- }
-
 void ShowReplaceGroupVehicleWindow(GroupID id_g, VehicleType vehicletype)
 {
 	Window *w;