src/airport_gui.cpp
branchnoai
changeset 10645 8cbdb511a674
parent 10513 33cb70ff2f5d
child 10715 6bdf79ffb022
--- a/src/airport_gui.cpp	Mon May 19 14:14:33 2008 +0000
+++ b/src/airport_gui.cpp	Mon May 19 15:13:58 2008 +0000
@@ -65,53 +65,65 @@
 	BuildAirClick_Demolish,
 };
 
-static void BuildAirToolbWndProc(Window *w, WindowEvent *e)
-{
-	switch (e->event) {
-		case WE_PAINT:
-			DrawWindowWidgets(w);
-			break;
-
-		case WE_CLICK:
-			if (e->we.click.widget - 3 >= 0)
-				_build_air_button_proc[e->we.click.widget - 3](w);
-			break;
-
-		case WE_KEYPRESS: {
-			switch (e->we.keypress.keycode) {
-				case '1': BuildAirClick_Airport(w); break;
-				case '2': BuildAirClick_Demolish(w); break;
-				default: return;
-			}
-		} break;
+struct BuildAirToolbarWindow : Window {
+	BuildAirToolbarWindow(const WindowDesc *desc, WindowNumber window_number) : Window(desc, window_number)
+	{
+		this->FindWindowPlacementAndResize(desc);
+		if (_patches.link_terraform_toolbar) ShowTerraformToolbar(this);
+	}
 
-		case WE_PLACE_OBJ:
-			_place_proc(e->we.place.tile);
-			break;
-
-		case WE_PLACE_DRAG:
-			VpSelectTilesWithMethod(e->we.place.pt.x, e->we.place.pt.y, e->we.place.select_method);
-			break;
+	~BuildAirToolbarWindow()
+	{
+		if (_patches.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0);
+	}
 
-		case WE_PLACE_MOUSEUP:
-			if (e->we.place.pt.x != -1 && e->we.place.select_proc == DDSP_DEMOLISH_AREA) {
-				GUIPlaceProcDragXY(e);
-			}
-			break;
-
-		case WE_ABORT_PLACE_OBJ:
-			w->RaiseButtons();
+	virtual void OnPaint()
+	{
+		this->DrawWidgets();
+	}
 
-			w = FindWindowById(WC_BUILD_STATION, 0);
-			if (w != 0)
-				WP(w, def_d).close = true;
-			break;
+	virtual void OnClick(Point pt, int widget)
+	{
+		if (widget - 3 >= 0) {
+			_build_air_button_proc[widget - 3](this);
+		}
+	}
 
-		case WE_DESTROY:
-			if (_patches.link_terraform_toolbar) DeleteWindowById(WC_SCEN_LAND_GEN, 0);
-			break;
+
+	virtual EventState OnKeyPress(uint16 key, uint16 keycode)
+	{
+		switch (keycode) {
+			case '1': BuildAirClick_Airport(this); break;
+			case '2': BuildAirClick_Demolish(this); break;
+			default: return ES_NOT_HANDLED;
+		}
+		return ES_HANDLED;
 	}
-}
+
+	virtual void OnPlaceObject(Point pt, TileIndex tile)
+	{
+		_place_proc(tile);
+	}
+
+	virtual void OnPlaceDrag(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt)
+	{
+		VpSelectTilesWithMethod(pt.x, pt.y, select_method);
+	}
+
+	virtual void OnPlaceMouseUp(ViewportPlaceMethod select_method, ViewportDragDropSelectionProcess select_proc, Point pt, TileIndex start_tile, TileIndex end_tile)
+	{
+		if (pt.x != -1 && select_proc == DDSP_DEMOLISH_AREA) {
+			GUIPlaceProcDragXY(select_proc, start_tile, end_tile);
+		}
+	}
+
+	virtual void OnPlaceObjectAbort()
+	{
+		this->RaiseButtons();
+
+		delete FindWindowById(WC_BUILD_STATION, 0);
+	}
+};
 
 static const Widget _air_toolbar_widgets[] = {
 {   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,            STR_018B_CLOSE_WINDOW },
@@ -128,7 +140,6 @@
 	WC_BUILD_TOOLBAR, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_air_toolbar_widgets,
-	BuildAirToolbWndProc
 };
 
 void ShowBuildAirToolbar()
@@ -136,114 +147,107 @@
 	if (!IsValidPlayer(_current_player)) return;
 
 	DeleteWindowByClass(WC_BUILD_TOOLBAR);
-	Window *w = AllocateWindowDescFront<Window>(&_air_toolbar_desc, TRANSPORT_AIR);
-	if (_patches.link_terraform_toolbar) ShowTerraformToolbar(w);
+	AllocateWindowDescFront<BuildAirToolbarWindow>(&_air_toolbar_desc, TRANSPORT_AIR);
 }
 
-enum {
-	BAW_BOTTOMPANEL = 10,
-	BAW_SMALL_AIRPORT,
-	BAW_CITY_AIRPORT,
-	BAW_HELIPORT,
-	BAW_METRO_AIRPORT,
-	BAW_STR_INTERNATIONAL_AIRPORT,
-	BAW_COMMUTER_AIRPORT,
-	BAW_HELIDEPOT,
-	BAW_STR_INTERCONTINENTAL_AIRPORT,
-	BAW_HELISTATION,
-	BAW_LAST_AIRPORT = BAW_HELISTATION,
-	BAW_AIRPORT_COUNT = BAW_LAST_AIRPORT - BAW_SMALL_AIRPORT + 1,
-	BAW_BTN_DONTHILIGHT = BAW_LAST_AIRPORT + 1,
-	BAW_BTN_DOHILIGHT,
-};
-
-static void BuildAirportPickerWndProc(Window *w, WindowEvent *e)
-{
-	switch (e->event) {
-		case WE_CREATE:
-			w->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_station_show_coverage);
-			w->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _station_show_coverage);
-			w->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
-			break;
-
-		case WE_PAINT: {
-			int i; // airport enabling loop
-			uint32 avail_airports;
-			const AirportFTAClass *airport;
-
-			if (WP(w, def_d).close) return;
-
-			avail_airports = GetValidAirports();
-
-			w->RaiseWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
-			if (!HasBit(avail_airports, 0) && _selected_airport_type == AT_SMALL) _selected_airport_type = AT_LARGE;
-			if (!HasBit(avail_airports, 1) && _selected_airport_type == AT_LARGE) _selected_airport_type = AT_SMALL;
-			w->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
-
-			/* 'Country Airport' starts at widget BAW_SMALL_AIRPORT, and if its bit is set, it is
-			 * available, so take its opposite value to set the disabled state.
-			 * There are 9 buildable airports
-			 * XXX TODO : all airports should be held in arrays, with all relevant data.
-			 * This should be part of newgrf-airports, i suppose
-			 */
-			for (i = 0; i < BAW_AIRPORT_COUNT; i++) w->SetWidgetDisabledState(i + BAW_SMALL_AIRPORT, !HasBit(avail_airports, i));
-
-			/* select default the coverage area to 'Off' (16) */
-			airport = GetAirport(_selected_airport_type);
-			SetTileSelectSize(airport->size_x, airport->size_y);
+class AirportPickerWindow : public PickerWindowBase {
 
-			int rad = _patches.modified_catchment ? airport->catchment : (uint)CA_UNMODIFIED;
-
-			if (_station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
-
-			DrawWindowWidgets(w);
-			/* strings such as 'Size' and 'Coverage Area' */
-			int text_end = DrawStationCoverageAreaText(2, 206, SCT_ALL, rad, false);
-			text_end = DrawStationCoverageAreaText(2, text_end + 4, SCT_ALL, rad, true) + 4;
-			if (text_end != w->widget[BAW_BOTTOMPANEL].bottom) {
-				w->SetDirty();
-				ResizeWindowForWidget(w, BAW_BOTTOMPANEL, 0, text_end - w->widget[BAW_BOTTOMPANEL].bottom);
-				w->SetDirty();
-			}
-			break;
-		}
+	enum {
+		BAW_BOTTOMPANEL = 10,
+		BAW_SMALL_AIRPORT,
+		BAW_CITY_AIRPORT,
+		BAW_HELIPORT,
+		BAW_METRO_AIRPORT,
+		BAW_STR_INTERNATIONAL_AIRPORT,
+		BAW_COMMUTER_AIRPORT,
+		BAW_HELIDEPOT,
+		BAW_STR_INTERCONTINENTAL_AIRPORT,
+		BAW_HELISTATION,
+		BAW_LAST_AIRPORT = BAW_HELISTATION,
+		BAW_AIRPORT_COUNT = BAW_LAST_AIRPORT - BAW_SMALL_AIRPORT + 1,
+		BAW_BTN_DONTHILIGHT = BAW_LAST_AIRPORT + 1,
+		BAW_BTN_DOHILIGHT,
+	};
 
-		case WE_CLICK: {
-			switch (e->we.click.widget) {
-				case BAW_SMALL_AIRPORT: case BAW_CITY_AIRPORT: case BAW_HELIPORT: case BAW_METRO_AIRPORT:
-				case BAW_STR_INTERNATIONAL_AIRPORT: case BAW_COMMUTER_AIRPORT: case BAW_HELIDEPOT:
-				case BAW_STR_INTERCONTINENTAL_AIRPORT: case BAW_HELISTATION:
-					w->RaiseWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
-					_selected_airport_type = e->we.click.widget - BAW_SMALL_AIRPORT;
-					w->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
-					SndPlayFx(SND_15_BEEP);
-					w->SetDirty();
-					break;
+public:
 
-				case BAW_BTN_DONTHILIGHT: case BAW_BTN_DOHILIGHT:
-					_station_show_coverage = (e->we.click.widget != BAW_BTN_DONTHILIGHT);
-					w->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_station_show_coverage);
-					w->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _station_show_coverage);
-					SndPlayFx(SND_15_BEEP);
-					w->SetDirty();
-					break;
-			}
-		} break;
+	AirportPickerWindow(const WindowDesc *desc) : PickerWindowBase(desc)
+	{
+		this->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_station_show_coverage);
+		this->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _station_show_coverage);
+		this->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
 
-		case WE_TICK: {
-			if (WP(w, def_d).close) {
-				delete w;
-				return;
-			}
+		this->FindWindowPlacementAndResize(desc);
+	}
 
-			CheckRedrawStationCoverage(w);
-		} break;
+	virtual void OnPaint()
+	{
+		int i; // airport enabling loop
+		uint32 avail_airports;
+		const AirportFTAClass *airport;
 
-		case WE_DESTROY:
-			if (!WP(w, def_d).close) ResetObjectToPlace();
-			break;
+		avail_airports = GetValidAirports();
+
+		this->RaiseWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
+		if (!HasBit(avail_airports, 0) && _selected_airport_type == AT_SMALL) _selected_airport_type = AT_LARGE;
+		if (!HasBit(avail_airports, 1) && _selected_airport_type == AT_LARGE) _selected_airport_type = AT_SMALL;
+		this->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
+
+		/* 'Country Airport' starts at widget BAW_SMALL_AIRPORT, and if its bit is set, it is
+		 * available, so take its opposite value to set the disabled state.
+		 * There are 9 buildable airports
+		 * XXX TODO : all airports should be held in arrays, with all relevant data.
+		 * This should be part of newgrf-airports, i suppose
+		 */
+		for (i = 0; i < BAW_AIRPORT_COUNT; i++) this->SetWidgetDisabledState(i + BAW_SMALL_AIRPORT, !HasBit(avail_airports, i));
+
+		/* select default the coverage area to 'Off' (16) */
+		airport = GetAirport(_selected_airport_type);
+		SetTileSelectSize(airport->size_x, airport->size_y);
+
+		int rad = _patches.modified_catchment ? airport->catchment : (uint)CA_UNMODIFIED;
+
+		if (_station_show_coverage) SetTileSelectBigSize(-rad, -rad, 2 * rad, 2 * rad);
+
+		this->DrawWidgets();
+		/* strings such as 'Size' and 'Coverage Area' */
+		int text_end = DrawStationCoverageAreaText(2, 206, SCT_ALL, rad, false);
+		text_end = DrawStationCoverageAreaText(2, text_end + 4, SCT_ALL, rad, true) + 4;
+		if (text_end != this->widget[BAW_BOTTOMPANEL].bottom) {
+			this->SetDirty();
+			ResizeWindowForWidget(this, BAW_BOTTOMPANEL, 0, text_end - this->widget[BAW_BOTTOMPANEL].bottom);
+			this->SetDirty();
+		}
 	}
-}
+
+	virtual void OnClick(Point pt, int widget)
+	{
+		switch (widget) {
+			case BAW_SMALL_AIRPORT: case BAW_CITY_AIRPORT: case BAW_HELIPORT: case BAW_METRO_AIRPORT:
+			case BAW_STR_INTERNATIONAL_AIRPORT: case BAW_COMMUTER_AIRPORT: case BAW_HELIDEPOT:
+			case BAW_STR_INTERCONTINENTAL_AIRPORT: case BAW_HELISTATION:
+				this->RaiseWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
+				_selected_airport_type = widget - BAW_SMALL_AIRPORT;
+				this->LowerWidget(_selected_airport_type + BAW_SMALL_AIRPORT);
+				SndPlayFx(SND_15_BEEP);
+				this->SetDirty();
+				break;
+
+			case BAW_BTN_DONTHILIGHT: case BAW_BTN_DOHILIGHT:
+				_station_show_coverage = (widget != BAW_BTN_DONTHILIGHT);
+				this->SetWidgetLoweredState(BAW_BTN_DONTHILIGHT, !_station_show_coverage);
+				this->SetWidgetLoweredState(BAW_BTN_DOHILIGHT, _station_show_coverage);
+				SndPlayFx(SND_15_BEEP);
+				this->SetDirty();
+				break;
+		}
+	}
+
+	virtual void OnTick()
+	{
+		CheckRedrawStationCoverage(this);
+	}
+};
 
 static const Widget _build_airport_picker_widgets[] = {
 {   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,                         STR_018B_CLOSE_WINDOW},
@@ -277,12 +281,11 @@
 	WC_BUILD_STATION, WC_BUILD_TOOLBAR,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
 	_build_airport_picker_widgets,
-	BuildAirportPickerWndProc
 };
 
 static void ShowBuildAirportPicker()
 {
-	new Window(&_build_airport_desc);
+	new AirportPickerWindow(&_build_airport_desc);
 }
 
 void InitializeAirportGui()