src/industry_gui.cpp
changeset 7284 d7903fff2d0b
parent 7283 510097a30c0c
child 7289 9d5fe9be6f2f
equal deleted inserted replaced
7283:510097a30c0c 7284:d7903fff2d0b
   378 	AllocateWindowDescFront(&_build_industry_dynamic_desc, 0);
   378 	AllocateWindowDescFront(&_build_industry_dynamic_desc, 0);
   379 }
   379 }
   380 
   380 
   381 static void UpdateIndustryProduction(Industry *i);
   381 static void UpdateIndustryProduction(Industry *i);
   382 
   382 
   383 static inline bool isProductionMinimum(const Industry *i, int pt) {
   383 static inline bool isProductionMinimum(const Industry *i, int pt)
       
   384 {
   384 	return i->production_rate[pt] == 1;
   385 	return i->production_rate[pt] == 1;
   385 }
   386 }
   386 
   387 
   387 static inline bool isProductionMaximum(const Industry *i, int pt) {
   388 static inline bool isProductionMaximum(const Industry *i, int pt)
       
   389 {
   388 	return i->production_rate[pt] == 255;
   390 	return i->production_rate[pt] == 255;
   389 }
   391 }
   390 
   392 
   391 static inline bool IsProductionAlterable(const Industry *i)
   393 static inline bool IsProductionAlterable(const Industry *i)
   392 {
   394 {
   393 	const IndustrySpec *ind = GetIndustrySpec(i->type);
   395 	const IndustrySpec *ind = GetIndustrySpec(i->type);
   394 	return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
   396 	return ((_game_mode == GM_EDITOR || _cheats.setup_prod.value) &&
   395 			(ind->accepts_cargo[0] == CT_INVALID || ind->accepts_cargo[0] == CT_VALUABLES));
   397 			(ind->accepts_cargo[0] == CT_INVALID || ind->accepts_cargo[0] == CT_VALUABLES));
   396 }
   398 }
   397 
   399 
       
   400 /** Information to store about the industry window */
       
   401 struct indview_d : public vp_d {
       
   402 	byte editbox_line;        ///< The line clicked to open the edit box
       
   403 	byte clicked_line;        ///< The line of the button that has been clicked
       
   404 	byte clicked_button;      ///< The button that has been clicked (to raise)
       
   405 	byte production_offset_y; ///< The offset of the production texts/buttons
       
   406 };
       
   407 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(indview_d));
       
   408 
       
   409 
   398 static void IndustryViewWndProc(Window *w, WindowEvent *e)
   410 static void IndustryViewWndProc(Window *w, WindowEvent *e)
   399 {
   411 {
   400 	/* WP(w,vp2_d).data_1 is for the editbox line
       
   401 	 * WP(w,vp2_d).data_2 is for the clickline
       
   402 	 * WP(w,vp2_d).data_3 is for the click pos (left or right) */
       
   403 
       
   404 	switch (e->event) {
   412 	switch (e->event) {
   405 	case WE_CREATE: {
   413 	case WE_CREATE: {
   406 		/* Count the number of lines that we need to resize the GUI with */
   414 		/* Count the number of lines that we need to resize the GUI with */
   407 		const IndustrySpec *ind = GetIndustrySpec(GetIndustry(w->window_number)->type);
   415 		const IndustrySpec *ind = GetIndustrySpec(GetIndustry(w->window_number)->type);
   408 		int lines = -3;
   416 		int lines = -3;
   472 		for (uint j = 0; j < 2 && ind->produced_cargo[j] != CT_INVALID; j++) {
   480 		for (uint j = 0; j < 2 && ind->produced_cargo[j] != CT_INVALID; j++) {
   473 			if (j == 0) {
   481 			if (j == 0) {
   474 				if (ind->accepts_cargo[0] != CT_INVALID) y += 10;
   482 				if (ind->accepts_cargo[0] != CT_INVALID) y += 10;
   475 				DrawString(2, y, STR_482A_PRODUCTION_LAST_MONTH, 0);
   483 				DrawString(2, y, STR_482A_PRODUCTION_LAST_MONTH, 0);
   476 				y += 10;
   484 				y += 10;
       
   485 				WP(w, indview_d).production_offset_y = y;
   477 			}
   486 			}
   478 
   487 
   479 			SetDParam(0, ind->produced_cargo[j]);
   488 			SetDParam(0, ind->produced_cargo[j]);
   480 			SetDParam(1, i->last_month_production[j]);
   489 			SetDParam(1, i->last_month_production[j]);
   481 
   490 
   482 			SetDParam(2, i->last_month_pct_transported[j] * 100 >> 8);
   491 			SetDParam(2, i->last_month_pct_transported[j] * 100 >> 8);
   483 			DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), y, STR_482B_TRANSPORTED, 0);
   492 			DrawString(4 + (IsProductionAlterable(i) ? 30 : 0), y, STR_482B_TRANSPORTED, 0);
   484 			/* Let's put out those buttons.. */
   493 			/* Let's put out those buttons.. */
   485 			if (IsProductionAlterable(i)) {
   494 			if (IsProductionAlterable(i)) {
   486 				DrawArrowButtons(5, y, 3, (WP(w, vp2_d).data_2 == j + 1) ? WP(w, vp2_d).data_3 : 0,
   495 				DrawArrowButtons(5, y, 3, (WP(w, indview_d).clicked_line == j + 1) ? WP(w, indview_d).clicked_button : 0,
   487 						!isProductionMinimum(i, j), !isProductionMaximum(i, j));
   496 						!isProductionMinimum(i, j), !isProductionMaximum(i, j));
   488 			}
   497 			}
   489 			y += 10;
   498 			y += 10;
   490 		}
   499 		}
   491 
   500 
   513 
   522 
   514 			i = GetIndustry(w->window_number);
   523 			i = GetIndustry(w->window_number);
   515 
   524 
   516 			/* We should work if needed.. */
   525 			/* We should work if needed.. */
   517 			if (!IsProductionAlterable(i)) return;
   526 			if (!IsProductionAlterable(i)) return;
   518 
       
   519 			x = e->we.click.pt.x;
   527 			x = e->we.click.pt.x;
   520 			line = (e->we.click.pt.y - 121) / 10;
   528 			line = (e->we.click.pt.y - WP(w, indview_d).production_offset_y) / 10;
   521 			if (e->we.click.pt.y >= 121 && IS_INT_INSIDE(line, 0, 2) &&
   529 			if (e->we.click.pt.y >= WP(w, indview_d).production_offset_y && IS_INT_INSIDE(line, 0, 2) &&
   522 					GetIndustrySpec(i->type)->produced_cargo[line] != CT_INVALID) {
   530 					GetIndustrySpec(i->type)->produced_cargo[line] != CT_INVALID) {
   523 				if (IS_INT_INSIDE(x, 5, 25) ) {
   531 				if (IS_INT_INSIDE(x, 5, 25) ) {
   524 					/* Clicked buttons, decrease or increase production */
   532 					/* Clicked buttons, decrease or increase production */
   525 					if (x < 15) {
   533 					if (x < 15) {
   526 						if (isProductionMinimum(i, line)) return;
   534 						if (isProductionMinimum(i, line)) return;
   531 					}
   539 					}
   532 
   540 
   533 					UpdateIndustryProduction(i);
   541 					UpdateIndustryProduction(i);
   534 					SetWindowDirty(w);
   542 					SetWindowDirty(w);
   535 					w->flags4 |= 5 << WF_TIMEOUT_SHL;
   543 					w->flags4 |= 5 << WF_TIMEOUT_SHL;
   536 					WP(w, vp2_d).data_2 = line + 1;
   544 					WP(w, indview_d).clicked_line = line + 1;
   537 					WP(w, vp2_d).data_3 = (x < 15 ? 1 : 2);
   545 					WP(w, indview_d).clicked_button = (x < 15 ? 1 : 2);
   538 				} else if (IS_INT_INSIDE(x, 34, 160)) {
   546 				} else if (IS_INT_INSIDE(x, 34, 160)) {
   539 					/* clicked the text */
   547 					/* clicked the text */
   540 					WP(w, vp2_d).data_1 = line;
   548 					WP(w, indview_d).editbox_line = line;
   541 					SetDParam(0, i->production_rate[line] * 8);
   549 					SetDParam(0, i->production_rate[line] * 8);
   542 					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_GAME_PRODUCTION, 10, 100, w, CS_ALPHANUMERAL);
   550 					ShowQueryString(STR_CONFIG_PATCHES_INT32, STR_CONFIG_GAME_PRODUCTION, 10, 100, w, CS_ALPHANUMERAL);
   543 				}
   551 				}
   544 			}
   552 			}
   545 		} break;
   553 		} break;
   549 		} break;
   557 		} break;
   550 
   558 
   551 		}
   559 		}
   552 		break;
   560 		break;
   553 	case WE_TIMEOUT:
   561 	case WE_TIMEOUT:
   554 		WP(w, vp2_d).data_2 = 0;
   562 		WP(w, indview_d).clicked_line = 0;
   555 		WP(w, vp2_d).data_3 = 0;
   563 		WP(w, indview_d).clicked_button = 0;
   556 		SetWindowDirty(w);
   564 		SetWindowDirty(w);
   557 		break;
   565 		break;
   558 
   566 
   559 	case WE_ON_EDIT_TEXT:
   567 	case WE_ON_EDIT_TEXT:
   560 		if (e->we.edittext.str[0] != '\0') {
   568 		if (e->we.edittext.str[0] != '\0') {
   561 			Industry* i = GetIndustry(w->window_number);
   569 			Industry* i = GetIndustry(w->window_number);
   562 			int line = WP(w, vp2_d).data_1;
   570 			int line = WP(w, indview_d).editbox_line;
   563 
   571 
   564 			i->production_rate[line] = clampu(atoi(e->we.edittext.str), 0, 255);
   572 			i->production_rate[line] = clampu(atoi(e->we.edittext.str), 0, 255);
   565 			UpdateIndustryProduction(i);
   573 			UpdateIndustryProduction(i);
   566 			SetWindowDirty(w);
   574 			SetWindowDirty(w);
   567 		}
   575 		}
   603 {
   611 {
   604 	Window *w = AllocateWindowDescFront(&_industry_view_desc, industry);
   612 	Window *w = AllocateWindowDescFront(&_industry_view_desc, industry);
   605 
   613 
   606 	if (w != NULL) {
   614 	if (w != NULL) {
   607 		w->flags4 |= WF_DISABLE_VP_SCROLL;
   615 		w->flags4 |= WF_DISABLE_VP_SCROLL;
   608 		WP(w, vp2_d).data_1 = 0;
   616 		WP(w, indview_d).editbox_line = 0;
   609 		WP(w, vp2_d).data_2 = 0;
   617 		WP(w, indview_d).clicked_line = 0;
   610 		WP(w, vp2_d).data_3 = 0;
   618 		WP(w, indview_d).clicked_button = 0;
   611 		AssignWindowViewport(w, 3, 17, 0xFE, 0x56, GetIndustry(w->window_number)->xy + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
   619 		AssignWindowViewport(w, 3, 17, 0xFE, 0x56, GetIndustry(w->window_number)->xy + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
   612 	}
   620 	}
   613 }
   621 }
   614 
   622 
   615 enum {
   623 enum {