changeset 17130:97e8cda8fc10 draft

(svn r21867) -Codechange: move creating the rail type dropdown to a more general location
author rubidium <rubidium@openttd.org>
date Thu, 20 Jan 2011 12:40:04 +0000
parents 703380c6e064
children bdd32cc48468
files src/rail_gui.cpp src/rail_gui.h src/toolbar_gui.cpp
diffstat 3 files changed, 55 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/src/rail_gui.cpp
+++ b/src/rail_gui.cpp
@@ -31,6 +31,7 @@
 #include "spritecache.h"
 #include "core/geometry_func.hpp"
 #include "hotkeys.h"
+#include "engine_base.h"
 
 #include "station_map.h"
 #include "tunnelbridge_map.h"
@@ -1969,3 +1970,54 @@
 	_cur_signal_type = _default_signal_type[_settings_client.gui.default_signal_type];
 	ResetSignalVariant();
 }
+
+/**
+ * Compare railtypes based on their sorting order.
+ * @param first  The railtype to compare to.
+ * @param second The railtype to compare.
+ * @return True iff the first should be sorted before the second.
+ */
+static bool CompareRailTypes(const DropDownListItem *first, const DropDownListItem *second)
+{
+	return GetRailTypeInfo((RailType)first->result)->sorting_order < GetRailTypeInfo((RailType)second->result)->sorting_order;
+}
+
+/**
+ * Create a drop down list for all the rail types of the local company.
+ * @param for_replacement Whether this list is for the replacement window.
+ * @return The populated and sorted #DropDownList.
+ */
+DropDownList *GetRailTypeDropDownList(bool for_replacement)
+{
+	RailTypes used_railtypes = RAILTYPES_NONE;
+
+	/* Find the used railtypes. */
+	Engine *e;
+	FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
+		if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
+
+		used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
+	}
+
+	/* Get the date introduced railtypes as well. */
+	used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
+
+	const Company *c = Company::Get(_local_company);
+	DropDownList *list = new DropDownList();
+	for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
+		/* If it's not used ever, don't show it to the user. */
+		if (!HasBit(used_railtypes, rt)) continue;
+
+		const RailtypeInfo *rti = GetRailTypeInfo(rt);
+		/* Skip rail type if it has no label */
+		if (rti->label == 0) continue;
+
+		StringID str = for_replacement ? rti->strings.replace_text : (rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING);
+		DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
+		item->SetParam(0, rti->strings.menu_text);
+		item->SetParam(1, rti->max_speed);
+		list->push_back(item);
+	}
+	list->sort(CompareRailTypes);
+	return list;
+}
--- a/src/rail_gui.h
+++ b/src/rail_gui.h
@@ -13,10 +13,12 @@
 #define RAIL_GUI_H
 
 #include "rail_type.h"
+#include "widgets/dropdown_type.h"
 
 struct Window *ShowBuildRailToolbar(RailType railtype);
 void ReinitGuiAfterToggleElrail(bool disable);
 bool ResetSignalVariant(int32 = 0);
 void InitializeRailGUI();
+DropDownList *GetRailTypeDropDownList(bool for_replacement = false);
 
 #endif /* RAIL_GUI_H */
--- a/src/toolbar_gui.cpp
+++ b/src/toolbar_gui.cpp
@@ -691,50 +691,9 @@
 
 /* --- Rail button menu --- */
 
-/**
- * Compare railtypes based on their sorting order.
- * @param first  The railtype to compare to.
- * @param second The railtype to compare.
- * @return True iff the first should be sorted before the second.
- */
-static bool CompareRailTypes(const DropDownListItem *first, const DropDownListItem *second)
-{
-	return GetRailTypeInfo((RailType)first->result)->sorting_order < GetRailTypeInfo((RailType)second->result)->sorting_order;
-}
-
 static CallBackFunction ToolbarBuildRailClick(Window *w)
 {
-	RailTypes used_railtypes = RAILTYPES_NONE;
-
-	/* Find the used railtypes. */
-	Engine *e;
-	FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) {
-		if (!HasBit(e->info.climates, _settings_game.game_creation.landscape)) continue;
-
-		used_railtypes |= GetRailTypeInfo(e->u.rail.railtype)->introduces_railtypes;
-	}
-
-	/* Get the date introduced railtypes as well. */
-	used_railtypes = AddDateIntroducedRailTypes(used_railtypes, MAX_DAY);
-
-	const Company *c = Company::Get(_local_company);
-	DropDownList *list = new DropDownList();
-	for (RailType rt = RAILTYPE_BEGIN; rt != RAILTYPE_END; rt++) {
-		/* If it's not used ever, don't show it to the user. */
-		if (!HasBit(used_railtypes, rt)) continue;
-
-		const RailtypeInfo *rti = GetRailTypeInfo(rt);
-		/* Skip rail type if it has no label */
-		if (rti->label == 0) continue;
-
-		StringID str = rti->max_speed > 0 ? STR_TOOLBAR_RAILTYPE_VELOCITY : STR_JUST_STRING;
-		DropDownListParamStringItem *item = new DropDownListParamStringItem(str, rt, !HasBit(c->avail_railtypes, rt));
-		item->SetParam(0, rti->strings.menu_text);
-		item->SetParam(1, rti->max_speed);
-		list->push_back(item);
-	}
-	list->sort(CompareRailTypes);
-	ShowDropDownList(w, list, _last_built_railtype, TBN_RAILS, 140, true, true);
+	ShowDropDownList(w, GetRailTypeDropDownList(), _last_built_railtype, TBN_RAILS, 140, true, true);
 	SndPlayFx(SND_15_BEEP);
 	return CBF_NONE;
 }