newgrf_gui.c
changeset 5352 86fca6a41aa1
parent 5345 4bc0f4814f15
child 5364 5ace629d7ed6
equal deleted inserted replaced
5351:c83b547490f0 5352:86fca6a41aa1
     7 #include "gfx.h"
     7 #include "gfx.h"
     8 #include "gui.h"
     8 #include "gui.h"
     9 #include "window.h"
     9 #include "window.h"
    10 #include "table/strings.h"
    10 #include "table/strings.h"
    11 #include "table/sprites.h"
    11 #include "table/sprites.h"
       
    12 #include "newgrf.h"
    12 #include "newgrf_config.h"
    13 #include "newgrf_config.h"
    13 
    14 
    14 
    15 
    15 /** Parse an integerlist string and set each found value
    16 /** Parse an integerlist string and set each found value
    16  * @param p the string to be parsed. Each element in the list is seperated by a
    17  * @param p the string to be parsed. Each element in the list is seperated by a
   217 };
   218 };
   218 
   219 
   219 
   220 
   220 /* 'NewGRF Settings' dialogue */
   221 /* 'NewGRF Settings' dialogue */
   221 typedef struct newgrf_d {
   222 typedef struct newgrf_d {
   222 	GRFConfig **list;
   223 	GRFConfig **orig_list; ///< grf list the window is shown with
   223 	GRFConfig *sel;
   224 	GRFConfig **list;      ///< temporary grf list to which changes are made
   224 	bool editable;
   225 	GRFConfig *sel;        ///< selected grf item
   225 	bool show_params;
   226 	bool editable;         ///< is the window editable
       
   227 	bool show_params;      ///< are the grf-parameters shown in the info-panel
       
   228 	bool execute;          ///< on pressing 'apply changes' are grf changes applied immediately, or only list is updated
   226 } newgrf_d;
   229 } newgrf_d;
   227 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(newgrf_d));
   230 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(newgrf_d));
       
   231 
   228 
   232 
   229 enum ShowNewGRFStateWidgets {
   233 enum ShowNewGRFStateWidgets {
   230 	SNGRFS_ADD = 3,
   234 	SNGRFS_ADD = 3,
   231 	SNGRFS_REMOVE,
   235 	SNGRFS_REMOVE,
   232 	SNGRFS_MOVE_UP,
   236 	SNGRFS_MOVE_UP,
   233 	SNGRFS_MOVE_DOWN,
   237 	SNGRFS_MOVE_DOWN,
   234 	SNGRFS_FILE_LIST = 7,
   238 	SNGRFS_FILE_LIST = 7,
   235 	SNGRFS_NEWGRF_INFO = 9,
   239 	SNGRFS_NEWGRF_INFO = 9,
   236 	SNGRFS_SET_PARAMETERS,
   240 	SNGRFS_SET_PARAMETERS,
       
   241 	SNGRFS_APPLY_CHANGES,
   237 };
   242 };
       
   243 
   238 
   244 
   239 static void SetupNewGRFState(Window *w)
   245 static void SetupNewGRFState(Window *w)
   240 {
   246 {
   241 	bool disable_all = WP(w, newgrf_d).sel == NULL || !WP(w, newgrf_d).editable;
   247 	bool disable_all = WP(w, newgrf_d).sel == NULL || !WP(w, newgrf_d).editable;
   242 
   248 
   264 
   270 
   265 	for (c = *WP(w, newgrf_d).list, i = 0; c != NULL; c = c->next, i++);
   271 	for (c = *WP(w, newgrf_d).list, i = 0; c != NULL; c = c->next, i++);
   266 
   272 
   267 	w->vscroll.cap = (w->widget[SNGRFS_FILE_LIST].bottom - w->widget[SNGRFS_FILE_LIST].top) / 14 + 1;
   273 	w->vscroll.cap = (w->widget[SNGRFS_FILE_LIST].bottom - w->widget[SNGRFS_FILE_LIST].top) / 14 + 1;
   268 	SetVScrollCount(w, i);
   274 	SetVScrollCount(w, i);
       
   275 	SetWindowWidgetDisabledState(w, SNGRFS_APPLY_CHANGES, !WP(w, newgrf_d).editable);
       
   276 }
       
   277 
       
   278 
       
   279 /** Callback function for the newgrf 'apply changes' confirmation window
       
   280  * @param yes_clicked boolean value, true when yes was clicked, false otherwise */
       
   281 static void NewGRFConfirmationCallback(bool yes_clicked)
       
   282 {
       
   283 	if (yes_clicked) {
       
   284 		Window *w = FindWindowById(WC_GAME_OPTIONS, 0);
       
   285 		newgrf_d *nd = &WP(w, newgrf_d);
       
   286 
       
   287 		CopyGRFConfigList(nd->orig_list, *nd->list);
       
   288 		ReloadNewGRFData();
       
   289 	}
   269 }
   290 }
   270 
   291 
   271 
   292 
   272 static void NewGRFWndProc(Window *w, WindowEvent *e)
   293 static void NewGRFWndProc(Window *w, WindowEvent *e)
   273 {
   294 {
   396 
   417 
   397 					SetWindowDirty(w);
   418 					SetWindowDirty(w);
   398 					break;
   419 					break;
   399 				}
   420 				}
   400 
   421 
       
   422 				case SNGRFS_APPLY_CHANGES: /* Apply changes made to GRF list */
       
   423 					if (WP(w, newgrf_d).execute) {
       
   424 						ShowQuery(
       
   425 							STR_POPUP_CAUTION_CAPTION,
       
   426 							STR_NEWGRF_CONFIRMATION_TEXT,
       
   427 							NewGRFConfirmationCallback,
       
   428 							w->window_class,
       
   429 							w->window_number
       
   430 						);
       
   431 					} else {
       
   432 						CopyGRFConfigList(WP(w, newgrf_d).orig_list, *WP(w, newgrf_d).list);
       
   433 					}
       
   434 					break;
       
   435 
   401 				case SNGRFS_SET_PARAMETERS: { /* Edit parameters */
   436 				case SNGRFS_SET_PARAMETERS: { /* Edit parameters */
   402 					char buff[512];
   437 					char buff[512];
   403 					if (WP(w, newgrf_d).sel == NULL) break;
   438 					if (WP(w, newgrf_d).sel == NULL) break;
   404 
   439 
   405 					GRFBuildParamList(buff, WP(w, newgrf_d).sel, lastof(buff));
   440 					GRFBuildParamList(buff, WP(w, newgrf_d).sel, lastof(buff));
   417 
   452 
   418 				/* parse_intlist returns -1 on error */
   453 				/* parse_intlist returns -1 on error */
   419 				if (c->num_params == (byte)-1) c->num_params = 0;
   454 				if (c->num_params == (byte)-1) c->num_params = 0;
   420 			}
   455 			}
   421 			SetWindowDirty(w);
   456 			SetWindowDirty(w);
       
   457 			break;
       
   458 
       
   459 		case WE_DESTROY:
       
   460 			/* Remove the temporary copy of grf-list used in window */
       
   461 			ClearGRFConfigList(WP(w, newgrf_d).list);
   422 			break;
   462 			break;
   423 
   463 
   424 		case WE_RESIZE:
   464 		case WE_RESIZE:
   425 			w->vscroll.cap += e->we.sizing.diff.y / 14;
   465 			w->vscroll.cap += e->we.sizing.diff.y / 14;
   426 			w->widget[SNGRFS_FILE_LIST].data = (w->vscroll.cap << 8) + 1;
   466 			w->widget[SNGRFS_FILE_LIST].data = (w->vscroll.cap << 8) + 1;
   445 {  WWT_SCROLLBAR,   RESIZE_LRB, 10, 288, 299,  30,  99, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST },
   485 {  WWT_SCROLLBAR,   RESIZE_LRB, 10, 288, 299,  30,  99, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST },
   446 
   486 
   447 /* NewGRF file info */
   487 /* NewGRF file info */
   448 {      WWT_PANEL,   RESIZE_RTB, 10,   0, 299, 100, 199, STR_NULL,                    STR_NULL },
   488 {      WWT_PANEL,   RESIZE_RTB, 10,   0, 299, 100, 199, STR_NULL,                    STR_NULL },
   449 
   489 
   450 /* Edit parameter button... */
   490 /* Edit parameter and apply changes button... */
   451 { WWT_PUSHTXTBTN,   RESIZE_RTB, 10,   0, 287, 200, 211, STR_NEWGRF_SET_PARAMETERS,   STR_NULL },
   491 { WWT_PUSHTXTBTN,    RESIZE_TB, 10,   0, 143, 200, 211, STR_NEWGRF_SET_PARAMETERS,   STR_NULL },
       
   492 { WWT_PUSHTXTBTN,   RESIZE_RTB, 10, 144, 287, 200, 211, STR_NEWGRF_APPLY_CHANGES,    STR_NULL },
       
   493 
   452 {  WWT_RESIZEBOX,  RESIZE_LRTB, 10, 288, 299, 200, 211, 0x0,                         STR_RESIZE_BUTTON },
   494 {  WWT_RESIZEBOX,  RESIZE_LRTB, 10, 288, 299, 200, 211, 0x0,                         STR_RESIZE_BUTTON },
   453 
   495 
   454 { WIDGETS_END },
   496 { WIDGETS_END },
   455 };
   497 };
   456 
   498 
   462 	_newgrf_widgets,
   504 	_newgrf_widgets,
   463 	NewGRFWndProc,
   505 	NewGRFWndProc,
   464 };
   506 };
   465 
   507 
   466 
   508 
   467 void ShowNewGRFSettings(bool editable, bool show_params, GRFConfig **config)
   509 /** Setup the NewGRF gui
   468 {
   510  * @param editable allow the user to make changes to the grfconfig in the window
       
   511  * @param show_params show information about what parameters are set for the grf files
       
   512  * @param exec_changes if changes are made to the list (editable is true), apply these
       
   513  *        changes immediately or only update the list
       
   514  * @param config pointer to a linked-list of grfconfig's that will be shown */
       
   515 void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config)
       
   516 {
       
   517 	static GRFConfig *local = NULL;
   469 	Window *w;
   518 	Window *w;
   470 
   519 
   471 	DeleteWindowByClass(WC_GAME_OPTIONS);
   520 	DeleteWindowByClass(WC_GAME_OPTIONS);
   472 	w = AllocateWindowDesc(&_newgrf_desc);
   521 	w = AllocateWindowDesc(&_newgrf_desc);
   473 	if (w == NULL) return;
   522 	if (w == NULL) return;
   474 
   523 
   475 	w->resize.step_height = 14;
   524 	w->resize.step_height = 14;
       
   525 	CopyGRFConfigList(&local, *config);
   476 
   526 
   477 	/* Clear selections */
   527 	/* Clear selections */
   478 	WP(w, newgrf_d).sel         = NULL;
   528 	WP(w, newgrf_d).sel         = NULL;
   479 	WP(w, newgrf_d).list        = config;
   529 	WP(w, newgrf_d).list        = &local;
       
   530 	WP(w, newgrf_d).orig_list   = config;
   480 	WP(w, newgrf_d).editable    = editable;
   531 	WP(w, newgrf_d).editable    = editable;
       
   532 	WP(w, newgrf_d).execute     = exec_changes;
   481 	WP(w, newgrf_d).show_params = show_params;
   533 	WP(w, newgrf_d).show_params = show_params;
   482 
   534 
   483 	SetupNewGRFWindow(w);
   535 	SetupNewGRFWindow(w);
   484 }
   536 }