train_gui.c
changeset 4786 78938c5e606a
parent 4770 7092e6b90693
child 4788 76ed308037b1
equal deleted inserted replaced
4785:976abaed5d36 4786:78938c5e606a
   437 }
   437 }
   438 
   438 
   439 static void SortTrainBuildList(Window *w)
   439 static void SortTrainBuildList(Window *w)
   440 {
   440 {
   441 	_internal_sort_order = WP(w,buildtrain_d).decenting_sort_order;
   441 	_internal_sort_order = WP(w,buildtrain_d).decenting_sort_order;
   442 	qsort((void*)WP(w,buildtrain_d).engines, WP(w,buildtrain_d).num_engines, sizeof(WP(w,buildtrain_d).engines[0]),
   442 	qsort((void*)WP(w, buildtrain_d).list_a, WP(w, buildtrain_d).list_a_length, sizeof(WP(w, buildtrain_d).list_a[0]),
   443 		  _engine_sorter[WP(w,buildtrain_d).sort_criteria]);
   443 		  _engine_sorter[WP(w,buildtrain_d).sort_criteria]);
   444 }
   444 }
   445 
   445 
   446 static void DrawTrainBuildWindow(Window *w)
   446 static void DrawTrainBuildWindow(Window *w)
   447 {
   447 {
   453 	uint16 scrollcount = 0;
   453 	uint16 scrollcount = 0;
   454 
   454 
   455 	SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in
   455 	SetWindowWidgetDisabledState(w, BUILD_TRAIN_WIDGET_BUILD, w->window_number == 0); // Disable unless we got a depot to build in
   456 
   456 
   457 	/* Draw the clicked engine/wagon/both button pressed and unpress the other two */
   457 	/* Draw the clicked engine/wagon/both button pressed and unpress the other two */
   458 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_ENGINES, WP(w,buildtrain_d).show_engine_wagon == 1);
   458 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_ENGINES, WP(w,buildtrain_d).show_engine_button == 1);
   459 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_WAGONS,  WP(w,buildtrain_d).show_engine_wagon == 2);
   459 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_WAGONS,  WP(w,buildtrain_d).show_engine_button == 2);
   460 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_BOTH,    WP(w,buildtrain_d).show_engine_wagon == 3);
   460 	SetWindowWidgetLoweredState(w, BUILD_TRAIN_WIDGET_BOTH,    WP(w,buildtrain_d).show_engine_button == 3);
   461 
   461 
   462 	if (WP(w,buildtrain_d).data_invalidated) {
   462 	if (WP(w,buildtrain_d).data_invalidated) {
   463 		GenerateBuildList(&WP(w,buildtrain_d).engines, &WP(w,buildtrain_d).num_engines, &WP(w,buildtrain_d).wagons, &WP(w,buildtrain_d).num_wagons, WP(w,buildtrain_d).railtype);
   463 		GenerateBuildList(&WP(w, buildtrain_d).list_a, &WP(w, buildtrain_d).list_a_length, &WP(w, buildtrain_d).list_b, &WP(w, buildtrain_d).list_b_length, WP(w,buildtrain_d).railtype);
   464 		WP(w,buildtrain_d).data_invalidated = false;
   464 		WP(w,buildtrain_d).data_invalidated = false;
   465 		SortTrainBuildList(w);
   465 		SortTrainBuildList(w);
   466 
   466 
   467 		/* Make sure that the selected engine is still in the list*/
   467 		/* Make sure that the selected engine is still in the list*/
   468 		if (WP(w,buildtrain_d).sel_engine != INVALID_ENGINE) {
   468 		if (WP(w,buildtrain_d).sel_engine != INVALID_ENGINE) {
   469 			int i;
   469 			int i;
   470 			bool found = false;
   470 			bool found = false;
   471 			if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0)) {
   471 			if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) {
   472 				for (i = 0; i < WP(w,buildtrain_d).num_engines; i++) {
   472 				for (i = 0; i < WP(w, buildtrain_d).list_a_length; i++) {
   473 					if (WP(w,buildtrain_d).sel_engine != WP(w,buildtrain_d).engines[i]) continue;
   473 					if (WP(w,buildtrain_d).sel_engine != WP(w, buildtrain_d).list_a[i]) continue;
   474 					found = true;
   474 					found = true;
   475 					break;
   475 					break;
   476 				}
   476 				}
   477 			}
   477 			}
   478 			if (!found && HASBIT(WP(w,buildtrain_d).show_engine_wagon, 1)) {
   478 			if (!found && HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) {
   479 				for (i = 0; i < WP(w,buildtrain_d).num_wagons; i++) {
   479 				for (i = 0; i < WP(w, buildtrain_d).list_b_length; i++) {
   480 					if (WP(w,buildtrain_d).sel_engine != WP(w,buildtrain_d).wagons[i]) continue;
   480 					if (WP(w,buildtrain_d).sel_engine != WP(w, buildtrain_d).list_b[i]) continue;
   481 					found = true;
   481 					found = true;
   482 					break;
   482 					break;
   483 				}
   483 				}
   484 			}
   484 			}
   485 			if (!found) WP(w,buildtrain_d).sel_engine = INVALID_ENGINE;
   485 			if (!found) WP(w,buildtrain_d).sel_engine = INVALID_ENGINE;
   486 		}
   486 		}
   487 	}
   487 	}
   488 
   488 
   489 	if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0)) scrollcount += WP(w,buildtrain_d).num_engines;
   489 	if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) scrollcount += WP(w, buildtrain_d).list_a_length;
   490 	if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 1)) scrollcount += WP(w,buildtrain_d).num_wagons;
   490 	if (HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) scrollcount += WP(w, buildtrain_d).list_b_length;
   491 
   491 
   492 	SetVScrollCount(w, scrollcount);
   492 	SetVScrollCount(w, scrollcount);
   493 	SetDParam(0, WP(w,buildtrain_d).railtype + STR_881C_NEW_RAIL_VEHICLES);
   493 	SetDParam(0, WP(w,buildtrain_d).railtype + STR_881C_NEW_RAIL_VEHICLES);
   494 	DrawWindowWidgets(w);
   494 	DrawWindowWidgets(w);
   495 
   495 
   496 	if (selected_id == INVALID_ENGINE && scrollcount != 0) {
   496 	if (selected_id == INVALID_ENGINE && scrollcount != 0) {
   497 		if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0) && WP(w,buildtrain_d).num_engines != 0) {
   497 		if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0) && WP(w, buildtrain_d).list_a_length != 0) {
   498 			selected_id = WP(w,buildtrain_d).engines[0];
   498 			selected_id = WP(w, buildtrain_d).list_a[0];
   499 		} else {
   499 		} else {
   500 			selected_id = WP(w,buildtrain_d).wagons[0];
   500 			selected_id = WP(w, buildtrain_d).list_b[0];
   501 		}
   501 		}
   502 		WP(w,buildtrain_d).sel_engine = selected_id;
   502 		WP(w,buildtrain_d).sel_engine = selected_id;
   503 	}
   503 	}
   504 
   504 
   505 	/* Draw the engines */
   505 	/* Draw the engines */
   506 	if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0)) {
   506 	if (HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) {
   507 		engine_drawing_loop(WP(w,buildtrain_d).engines, WP(w,buildtrain_d).num_engines, x, &y, selected_id, &position, max);
   507 		engine_drawing_loop(WP(w, buildtrain_d).list_a, WP(w, buildtrain_d).list_a_length, x, &y, selected_id, &position, max);
   508 
   508 
   509 		/* Magically set the number 0 line to the one right after the last engine
   509 		/* Magically set the number 0 line to the one right after the last engine
   510 		* This way the line numbers fit the indexes in the wagon array */
   510 		* This way the line numbers fit the indexes in the wagon array */
   511 		position -= WP(w,buildtrain_d).num_engines;
   511 		position -= WP(w, buildtrain_d).list_a_length;
   512 		max      -= WP(w,buildtrain_d).num_engines;
   512 		max      -= WP(w, buildtrain_d).list_a_length;
   513 	}
   513 	}
   514 
   514 
   515 	/* Draw the wagons */
   515 	/* Draw the wagons */
   516 	if (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 1)) {
   516 	if (HASBIT(WP(w,buildtrain_d).show_engine_button, 1)) {
   517 		engine_drawing_loop(WP(w,buildtrain_d).wagons,  WP(w,buildtrain_d).num_wagons,  x, &y, selected_id, &position, max);
   517 		engine_drawing_loop(WP(w, buildtrain_d).list_b,  WP(w, buildtrain_d).list_b_length,  x, &y, selected_id, &position, max);
   518 	}
   518 	}
   519 
   519 
   520 	if (selected_id != INVALID_ENGINE) {
   520 	if (selected_id != INVALID_ENGINE) {
   521 		const RailVehicleInfo *rvi = RailVehInfo(selected_id);
   521 		const RailVehicleInfo *rvi = RailVehInfo(selected_id);
   522 
   522 
   532 
   532 
   533 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
   533 static void NewRailVehicleWndProc(Window *w, WindowEvent *e)
   534 {
   534 {
   535 	switch (e->event) {
   535 	switch (e->event) {
   536 		case WE_CREATE:
   536 		case WE_CREATE:
   537 			WP(w,buildtrain_d).num_engines = 0;
   537 			WP(w, buildtrain_d).list_a_length = 0;
   538 			WP(w,buildtrain_d).num_wagons  = 0;
   538 			WP(w, buildtrain_d).list_b_length = 0;
   539 			WP(w,buildtrain_d).engines = NULL;
   539 			WP(w, buildtrain_d).list_a        = NULL;
   540 			WP(w,buildtrain_d).wagons  = NULL;
   540 			WP(w, buildtrain_d).list_b        = NULL;
   541 			WP(w,buildtrain_d).show_engine_wagon = 3;
   541 			WP(w, buildtrain_d).show_engine_button   = 3;
   542 			WP(w,buildtrain_d).data_invalidated  = true;
   542 			WP(w, buildtrain_d).data_invalidated     = true;
   543 			WP(w,buildtrain_d).sel_engine        = INVALID_ENGINE;
   543 			WP(w, buildtrain_d).sel_engine           = INVALID_ENGINE;
   544 			WP(w,buildtrain_d).sort_criteria     = _last_sort_criteria;
   544 			WP(w, buildtrain_d).sort_criteria        = _last_sort_criteria;
   545 			WP(w,buildtrain_d).decenting_sort_order = _last_sort_order;
   545 			WP(w, buildtrain_d).decenting_sort_order = _last_sort_order;
   546 			break;
   546 			break;
   547 
   547 
   548 		case WE_INVALIDATE_DATA:
   548 		case WE_INVALIDATE_DATA:
   549 			WP(w,buildtrain_d).data_invalidated = true;
   549 			WP(w,buildtrain_d).data_invalidated = true;
   550 			break;
   550 			break;
   551 
   551 
   552 		case WE_DESTROY:
   552 		case WE_DESTROY:
   553 			free((void*)WP(w,buildtrain_d).engines);
   553 			free((void*)WP(w, buildtrain_d).list_a);
   554 			free((void*)WP(w,buildtrain_d).wagons);
   554 			free((void*)WP(w, buildtrain_d).list_b);
   555 			break;
   555 			break;
   556 
   556 
   557 		case WE_PAINT:
   557 		case WE_PAINT:
   558 			DrawTrainBuildWindow(w);
   558 			DrawTrainBuildWindow(w);
   559 			break;
   559 			break;
   572 					ShowDropDownMenu(w, _engine_sort_listing, WP(w,buildtrain_d).sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0);
   572 					ShowDropDownMenu(w, _engine_sort_listing, WP(w,buildtrain_d).sort_criteria, BUILD_TRAIN_WIDGET_SORT_DROPDOWN, 0, 0);
   573 					return;
   573 					return;
   574 
   574 
   575 				case BUILD_TRAIN_WIDGET_LIST: {
   575 				case BUILD_TRAIN_WIDGET_LIST: {
   576 					uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos;
   576 					uint i = ((e->we.click.pt.y - 26) / 14) + w->vscroll.pos;
   577 					if (i < (uint)(WP(w,buildtrain_d).num_engines + WP(w,buildtrain_d).num_wagons)) {
   577 					if (i < (uint)(WP(w, buildtrain_d).list_a_length + WP(w, buildtrain_d).list_b_length)) {
   578 						if (i < WP(w,buildtrain_d).num_engines && HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0)) {
   578 						if (i < WP(w, buildtrain_d).list_a_length && HASBIT(WP(w,buildtrain_d).show_engine_button, 0)) {
   579 							WP(w,buildtrain_d).sel_engine = WP(w,buildtrain_d).engines[i];
   579 							WP(w,buildtrain_d).sel_engine = WP(w, buildtrain_d).list_a[i];
   580 						} else {
   580 						} else {
   581 							WP(w,buildtrain_d).sel_engine = WP(w,buildtrain_d).wagons[i - (HASBIT(WP(w,buildtrain_d).show_engine_wagon, 0) ? WP(w,buildtrain_d).num_engines : 0)];
   581 							WP(w,buildtrain_d).sel_engine = WP(w, buildtrain_d).list_b[i - (HASBIT(WP(w,buildtrain_d).show_engine_button, 0) ? WP(w, buildtrain_d).list_a_length : 0)];
   582 						}
   582 						}
   583 						SetWindowDirty(w);
   583 						SetWindowDirty(w);
   584 					}
   584 					}
   585 				} break;
   585 				} break;
   586 
   586 
   591 					 * Because the button widgets are ordered as they are (in a row), we can calculate as following:
   591 					 * Because the button widgets are ordered as they are (in a row), we can calculate as following:
   592 					 * engines = bit 0 (1 for set), wagons bit 1 (2 for set), both (2 | 1 = 3)
   592 					 * engines = bit 0 (1 for set), wagons bit 1 (2 for set), both (2 | 1 = 3)
   593 					 * Those numbers are the same as the clicked button - BUILD_TRAIN_WIDGET_ENGINES + 1 */
   593 					 * Those numbers are the same as the clicked button - BUILD_TRAIN_WIDGET_ENGINES + 1 */
   594 
   594 
   595 					byte click_state = e->we.click.widget - BUILD_TRAIN_WIDGET_ENGINES + 1;
   595 					byte click_state = e->we.click.widget - BUILD_TRAIN_WIDGET_ENGINES + 1;
   596 					if (WP(w,buildtrain_d).show_engine_wagon == click_state) break; // We clicked the pressed button
   596 					if (WP(w,buildtrain_d).show_engine_button == click_state) break; // We clicked the pressed button
   597 					WP(w,buildtrain_d).sel_engine = INVALID_ENGINE;
   597 					WP(w,buildtrain_d).sel_engine = INVALID_ENGINE;
   598 					WP(w,buildtrain_d).show_engine_wagon = click_state;
   598 					WP(w,buildtrain_d).show_engine_button = click_state;
   599 					w->vscroll.pos = 0;
   599 					w->vscroll.pos = 0;
   600 					SetWindowDirty(w);
   600 					SetWindowDirty(w);
   601 					}
   601 					}
   602 					break;
   602 					break;
   603 
   603