src/autoreplace_gui.cpp
changeset 8345 228a97078526
parent 8333 b01316b861c2
child 8346 5baabe791d29
equal deleted inserted replaced
8344:1cd2a831b06c 8345:228a97078526
    34 	STR_MONORAIL_VEHICLES,
    34 	STR_MONORAIL_VEHICLES,
    35 	STR_MAGLEV_VEHICLES,
    35 	STR_MAGLEV_VEHICLES,
    36 	INVALID_STRING_ID
    36 	INVALID_STRING_ID
    37 };
    37 };
    38 
    38 
       
    39 enum ReplaceVehicleWindowWidgets {
       
    40 	RVW_WIDGET_LEFT_DETAILS = 3,
       
    41 	RVW_WIDGET_START_REPLACE,
       
    42 	RVW_WIDGET_INFO_TAB,
       
    43 	RVW_WIDGET_STOP_REPLACE,
       
    44 	RVW_WIDGET_LEFT_MATRIX,
       
    45 	RVW_WIDGET_LEFT_SCROLLBAR,
       
    46 	RVW_WIDGET_RIGHT_MATRIX,
       
    47 	RVW_WIDGET_RIGHT_SCROLLBAR,
       
    48 	RVW_WIDGET_RIGHT_DETAILS,
       
    49 
       
    50 	RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE,
       
    51 	RVW_WIDGET_TRAIN_FLUFF_LEFT,
       
    52 	RVW_WIDGET_TRAIN_RAILTYPE_TEXT,
       
    53 	RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN,
       
    54 	RVW_WIDGET_TRAIN_FLUFF_RIGHT,
       
    55 	RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE,
       
    56 };
       
    57 
    39 static int CDECL TrainEngineNumberSorter(const void *a, const void *b)
    58 static int CDECL TrainEngineNumberSorter(const void *a, const void *b)
    40 {
    59 {
    41 	const EngineID va = *(const EngineID*)a;
    60 	const EngineID va = *(const EngineID*)a;
    42 	const EngineID vb = *(const EngineID*)b;
    61 	const EngineID vb = *(const EngineID*)b;
    43 	int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb);
    62 	int r = ListPositionOfEngine(va) - ListPositionOfEngine(vb);
   262 
   281 
   263 			/* Disable the "Start Replacing" button if:
   282 			/* Disable the "Start Replacing" button if:
   264 			 *    Either list is empty
   283 			 *    Either list is empty
   265 			 * or The selected replacement engine has a replacement (to prevent loops)
   284 			 * or The selected replacement engine has a replacement (to prevent loops)
   266 			 * or The right list (new replacement) has the existing replacement vehicle selected */
   285 			 * or The right list (new replacement) has the existing replacement vehicle selected */
   267 			w->SetWidgetDisabledState(4,
   286 			w->SetWidgetDisabledState(RVW_WIDGET_START_REPLACE,
   268 										 selected_id[0] == INVALID_ENGINE ||
   287 										 selected_id[0] == INVALID_ENGINE ||
   269 										 selected_id[1] == INVALID_ENGINE ||
   288 										 selected_id[1] == INVALID_ENGINE ||
   270 										 EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
   289 										 EngineReplacementForPlayer(p, selected_id[1], selected_group) != INVALID_ENGINE ||
   271 										 EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]);
   290 										 EngineReplacementForPlayer(p, selected_id[0], selected_group) == selected_id[1]);
   272 
   291 
   273 			/* Disable the "Stop Replacing" button if:
   292 			/* Disable the "Stop Replacing" button if:
   274 			 *   The left list (existing vehicle) is empty
   293 			 *   The left list (existing vehicle) is empty
   275 			 *   or The selected vehicle has no replacement set up */
   294 			 *   or The selected vehicle has no replacement set up */
   276 			w->SetWidgetDisabledState(6,
   295 			w->SetWidgetDisabledState(RVW_WIDGET_STOP_REPLACE,
   277 										 selected_id[0] == INVALID_ENGINE ||
   296 										 selected_id[0] == INVALID_ENGINE ||
   278 										 !EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
   297 										 !EngineHasReplacementForPlayer(p, selected_id[0], selected_group));
   279 
   298 
   280 			/* now the actual drawing of the window itself takes place */
   299 			/* now the actual drawing of the window itself takes place */
   281 			SetDParam(0, _vehicle_type_names[w->window_number]);
   300 			SetDParam(0, _vehicle_type_names[w->window_number]);
   286 
   305 
   287 				/* set wagon/engine button */
   306 				/* set wagon/engine button */
   288 				SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
   307 				SetDParam(2, WP(w, replaceveh_d).wagon_btnstate ? STR_ENGINES : STR_WAGONS);
   289 
   308 
   290 				/* sets the colour of that art thing */
   309 				/* sets the colour of that art thing */
   291 				w->widget[13].color = _player_colors[_local_player];
   310 				w->widget[RVW_WIDGET_TRAIN_FLUFF_LEFT].color  = _player_colors[_local_player];
   292 				w->widget[16].color = _player_colors[_local_player];
   311 				w->widget[RVW_WIDGET_TRAIN_FLUFF_RIGHT].color = _player_colors[_local_player];
   293 			}
   312 			}
   294 
   313 
   295 			DrawWindowWidgets(w);
   314 			DrawWindowWidgets(w);
   296 
   315 
   297 			if (w->window_number == VEH_TRAIN) {
   316 			if (w->window_number == VEH_TRAIN) {
   298 				/* Draw the selected railtype in the pulldown menu */
   317 				/* Draw the selected railtype in the pulldown menu */
   299 				RailType railtype = _railtype_selected_in_replace_gui;
   318 				RailType railtype = _railtype_selected_in_replace_gui;
   300 				DrawString(157, w->widget[14].top + 1, _rail_types_list[railtype], TC_BLACK);
   319 				DrawString(157, w->widget[RVW_WIDGET_TRAIN_RAILTYPE_TEXT].top + 1, _rail_types_list[railtype], TC_BLACK);
   301 			}
   320 			}
   302 
   321 
   303 			/* sets up the string for the vehicle that is being replaced to */
   322 			/* sets up the string for the vehicle that is being replaced to */
   304 			if (selected_id[0] != INVALID_ENGINE) {
   323 			if (selected_id[0] != INVALID_ENGINE) {
   305 				if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
   324 				if (!EngineHasReplacementForPlayer(p, selected_id[0], selected_group)) {
   310 				}
   329 				}
   311 			} else {
   330 			} else {
   312 				SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
   331 				SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);
   313 			}
   332 			}
   314 
   333 
   315 			DrawString(145, w->widget[5].top + 1, STR_02BD, TC_BLACK);
   334 			DrawString(145, w->widget[RVW_WIDGET_INFO_TAB].top + 1, STR_02BD, TC_BLACK);
   316 
   335 
   317 			/* Draw the lists */
   336 			/* Draw the lists */
   318 			for(byte i = 0; i < 2; i++) {
   337 			for(byte i = 0; i < 2; i++) {
   319 				uint16 x        = i == 0 ? 2 : 230; // at what X offset
   338 				uint16 x        = i == 0 ? 2 : 230; // at what X offset
   320 				EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
   339 				EngineList list = WP(w, replaceveh_d).list[i]; // which list to draw
   324 				/* Do the actual drawing */
   343 				/* Do the actual drawing */
   325 				DrawEngineList((VehicleType)w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
   344 				DrawEngineList((VehicleType)w->window_number, x, 15, list, start, end, WP(w, replaceveh_d).sel_engine[i], i == 0, selected_group);
   326 
   345 
   327 				/* Also draw the details if an engine is selected */
   346 				/* Also draw the details if an engine is selected */
   328 				if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
   347 				if (WP(w, replaceveh_d).sel_engine[i] != INVALID_ENGINE) {
   329 					const Widget *wi = &w->widget[i == 0 ? 3 : 11];
   348 					const Widget *wi = &w->widget[i == 0 ? RVW_WIDGET_LEFT_DETAILS : RVW_WIDGET_RIGHT_DETAILS];
   330 					DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
   349 					DrawVehiclePurchaseInfo(wi->left + 2, wi->top + 1, wi->right - wi->left - 2, WP(w, replaceveh_d).sel_engine[i]);
   331 				}
   350 				}
   332 			}
   351 			}
   333 
   352 
   334 		} break;   // end of paint
   353 		} break;   // end of paint
   335 
   354 
   336 		case WE_CLICK: {
   355 		case WE_CLICK: {
   337 			switch (e->we.click.widget) {
   356 			switch (e->we.click.widget) {
   338 				case 12:
   357 				case RVW_WIDGET_TRAIN_ENGINEWAGON_TOGGLE:
   339 					WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
   358 					WP(w, replaceveh_d).wagon_btnstate = !(WP(w, replaceveh_d).wagon_btnstate);
   340 					WP(w, replaceveh_d).update_left = true;
   359 					WP(w, replaceveh_d).update_left = true;
   341 					WP(w, replaceveh_d).init_lists  = true;
   360 					WP(w, replaceveh_d).init_lists  = true;
   342 					SetWindowDirty(w);
   361 					SetWindowDirty(w);
   343 					break;
   362 					break;
   344 
   363 
   345 				case 14:
   364 				case RVW_WIDGET_TRAIN_RAILTYPE_TEXT:
   346 				case 15: /* Railtype selection dropdown menu */
   365 				case RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN: /* Railtype selection dropdown menu */
   347 					ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, 15, 0, ~GetPlayer(_local_player)->avail_railtypes);
   366 					ShowDropDownMenu(w, _rail_types_list, _railtype_selected_in_replace_gui, RVW_WIDGET_TRAIN_RAILTYPE_DROPDOWN, 0, ~GetPlayer(_local_player)->avail_railtypes);
   348 					break;
   367 					break;
   349 
   368 
   350 				case 17: /* toggle renew_keep_length */
   369 				case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: /* toggle renew_keep_length */
   351 					DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE);
   370 					DoCommandP(0, 5, GetPlayer(_local_player)->renew_keep_length ? 0 : 1, NULL, CMD_SET_AUTOREPLACE);
   352 					break;
   371 					break;
   353 
   372 
   354 				case 4: { /* Start replacing */
   373 				case RVW_WIDGET_START_REPLACE: { /* Start replacing */
   355 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
   374 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
   356 					EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
   375 					EngineID veh_to = WP(w, replaceveh_d).sel_engine[1];
   357 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
   376 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16) , veh_from + (veh_to << 16), NULL, CMD_SET_AUTOREPLACE);
   358 				} break;
   377 				} break;
   359 
   378 
   360 				case 6: { /* Stop replacing */
   379 				case RVW_WIDGET_STOP_REPLACE: { /* Stop replacing */
   361 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
   380 					EngineID veh_from = WP(w, replaceveh_d).sel_engine[0];
   362 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
   381 					DoCommandP(0, 3 + (WP(w, replaceveh_d).sel_group << 16), veh_from + (INVALID_ENGINE << 16), NULL, CMD_SET_AUTOREPLACE);
   363 				} break;
   382 				} break;
   364 
   383 
   365 				case 7:
   384 				case RVW_WIDGET_LEFT_MATRIX:
   366 				case 9: {
   385 				case RVW_WIDGET_RIGHT_MATRIX: {
   367 					uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
   386 					uint i = (e->we.click.pt.y - 14) / w->resize.step_height;
   368 					uint16 click_scroll_pos = e->we.click.widget == 7 ? w->vscroll.pos : w->vscroll2.pos;
   387 					uint16 click_scroll_pos = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.pos : w->vscroll2.pos;
   369 					uint16 click_scroll_cap = e->we.click.widget == 7 ? w->vscroll.cap : w->vscroll2.cap;
   388 					uint16 click_scroll_cap = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? w->vscroll.cap : w->vscroll2.cap;
   370 					byte click_side         = e->we.click.widget == 7 ? 0 : 1;
   389 					byte click_side         = e->we.click.widget == RVW_WIDGET_LEFT_MATRIX ? 0 : 1;
   371 					uint16 engine_count     = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
   390 					uint16 engine_count     = EngList_Count(&WP(w, replaceveh_d).list[click_side]);
   372 
   391 
   373 					if (i < click_scroll_cap) {
   392 					if (i < click_scroll_cap) {
   374 						i += click_scroll_pos;
   393 						i += click_scroll_pos;
   375 						EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE;
   394 						EngineID e = engine_count > i ? WP(w, replaceveh_d).list[click_side][i] : INVALID_ENGINE;
   403 
   422 
   404 		case WE_RESIZE:
   423 		case WE_RESIZE:
   405 			w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
   424 			w->vscroll.cap  += e->we.sizing.diff.y / (int)w->resize.step_height;
   406 			w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
   425 			w->vscroll2.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
   407 
   426 
   408 			w->widget[7].data = (w->vscroll.cap  << 8) + 1;
   427 			w->widget[RVW_WIDGET_LEFT_MATRIX].data  = (w->vscroll.cap  << 8) + 1;
   409 			w->widget[9].data = (w->vscroll2.cap << 8) + 1;
   428 			w->widget[RVW_WIDGET_RIGHT_MATRIX].data = (w->vscroll2.cap << 8) + 1;
   410 			break;
   429 			break;
   411 
   430 
   412 		case WE_INVALIDATE_DATA:
   431 		case WE_INVALIDATE_DATA:
   413 			if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true;
   432 			if (_rebuild_left_list) WP(w, replaceveh_d).update_left = true;
   414 			if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true;
   433 			if (_rebuild_right_list) WP(w, replaceveh_d).update_right = true;