src/settings_gui.cpp
branchNewGRF_ports
changeset 6877 889301acc299
parent 6872 1c4a4a609f85
child 6878 7d1ff2f621c7
equal deleted inserted replaced
6876:2c40faeef7a5 6877:889301acc299
    28 #include "widgets/dropdown_func.h"
    28 #include "widgets/dropdown_func.h"
    29 
    29 
    30 #include "table/sprites.h"
    30 #include "table/sprites.h"
    31 #include "table/strings.h"
    31 #include "table/strings.h"
    32 
    32 
    33 static uint32 _difficulty_click_a;
       
    34 static uint32 _difficulty_click_b;
       
    35 static byte _difficulty_timeout;
       
    36 
       
    37 static const StringID _units_dropdown[] = {
    33 static const StringID _units_dropdown[] = {
    38 	STR_UNITS_IMPERIAL,
    34 	STR_UNITS_IMPERIAL,
    39 	STR_UNITS_METRIC,
    35 	STR_UNITS_METRIC,
    40 	STR_UNITS_SI,
    36 	STR_UNITS_SI,
    41 	INVALID_STRING_ID
    37 	INVALID_STRING_ID
   402 
   398 
   403 static const GameSettingData _game_setting_info[] = {
   399 static const GameSettingData _game_setting_info[] = {
   404 	{  0,   7,  1, STR_NULL},
   400 	{  0,   7,  1, STR_NULL},
   405 	{  0,   3,  1, STR_6830_IMMEDIATE},
   401 	{  0,   3,  1, STR_6830_IMMEDIATE},
   406 	{  0,   3,  1, STR_NUM_VERY_LOW},
   402 	{  0,   3,  1, STR_NUM_VERY_LOW},
   407 	{  0,   4,  1, STR_26816_NONE},
   403 	{  0,   4,  1, STR_NONE},
   408 	{100, 500, 50, STR_NULL},
   404 	{100, 500, 50, STR_NULL},
   409 	{  2,   4,  1, STR_NULL},
   405 	{  2,   4,  1, STR_NULL},
   410 	{  0,   2,  1, STR_6820_LOW},
   406 	{  0,   2,  1, STR_6820_LOW},
   411 	{  0,   4,  1, STR_681B_VERY_SLOW},
   407 	{  0,   4,  1, STR_681B_VERY_SLOW},
   412 	{  0,   2,  1, STR_6820_LOW},
   408 	{  0,   2,  1, STR_6820_LOW},
   416 	{  0,   3,  1, STR_682A_VERY_FLAT},
   412 	{  0,   3,  1, STR_682A_VERY_FLAT},
   417 	{  0,   3,  1, STR_VERY_LOW},
   413 	{  0,   3,  1, STR_VERY_LOW},
   418 	{  0,   1,  1, STR_682E_STEADY},
   414 	{  0,   1,  1, STR_682E_STEADY},
   419 	{  0,   1,  1, STR_6834_AT_END_OF_LINE_AND_AT_STATIONS},
   415 	{  0,   1,  1, STR_6834_AT_END_OF_LINE_AND_AT_STATIONS},
   420 	{  0,   1,  1, STR_6836_OFF},
   416 	{  0,   1,  1, STR_6836_OFF},
   421 	{  0,   2,  1, STR_6839_PERMISSIVE},
   417 	{  0,   2,  1, STR_PERMISSIVE},
   422 };
   418 };
   423 
   419 
   424 /*
   420 /*
   425  * A: competitors
   421  * A: competitors
   426  * B: start time in months / 3
   422  * B: start time in months / 3
   487 /* Temporary holding place of values in the difficulty window until 'Save' is clicked */
   483 /* Temporary holding place of values in the difficulty window until 'Save' is clicked */
   488 static GameOptions _opt_mod_temp;
   484 static GameOptions _opt_mod_temp;
   489 // 0x383E = (1 << 13) | (1 << 12) | (1 << 11) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1)
   485 // 0x383E = (1 << 13) | (1 << 12) | (1 << 11) | (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1)
   490 #define DIFF_INGAME_DISABLED_BUTTONS 0x383E
   486 #define DIFF_INGAME_DISABLED_BUTTONS 0x383E
   491 
   487 
       
   488 #define NO_SETTINGS_BUTTON 0xFF
       
   489 
       
   490 /** Carriage for the game settings window data */
       
   491 struct difficulty_d {
       
   492 	bool clicked_increase;
       
   493 	uint8 clicked_button;
       
   494 	uint8 timeout;
       
   495 };
       
   496 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(difficulty_d));
       
   497 
   492 /* Names of the game difficulty settings window */
   498 /* Names of the game difficulty settings window */
   493 enum GameDifficultyWidgets {
   499 enum GameDifficultyWidgets {
   494 	GDW_CLOSEBOX = 0,
   500 	GDW_CLOSEBOX = 0,
   495 	GDW_CAPTION,
   501 	GDW_CAPTION,
   496 	GDW_UPPER_BG,
   502 	GDW_UPPER_BG,
   505 	GDW_CANCEL,
   511 	GDW_CANCEL,
   506 };
   512 };
   507 
   513 
   508 static void GameDifficultyWndProc(Window *w, WindowEvent *e)
   514 static void GameDifficultyWndProc(Window *w, WindowEvent *e)
   509 {
   515 {
       
   516 	difficulty_d *diffic_d = &WP(w, difficulty_d);
   510 	switch (e->event) {
   517 	switch (e->event) {
   511 		case WE_CREATE:
   518 		case WE_CREATE:
       
   519 			diffic_d->clicked_increase = false;
       
   520 			diffic_d->clicked_button = NO_SETTINGS_BUTTON;
       
   521 			diffic_d->timeout = 0;
   512 			/* Hide the closebox to make sure that the user aborts or confirms his changes */
   522 			/* Hide the closebox to make sure that the user aborts or confirms his changes */
   513 			w->HideWidget(GDW_CLOSEBOX);
   523 			w->HideWidget(GDW_CLOSEBOX);
   514 			w->widget[GDW_CAPTION].left = 0;
   524 			w->widget[GDW_CAPTION].left = 0;
   515 			/* Setup disabled buttons when creating window
   525 			/* Setup disabled buttons when creating window
   516 			 * disable all other difficulty buttons during gameplay except for 'custom' */
   526 			 * disable all other difficulty buttons during gameplay except for 'custom' */
   543 			for (uint i = 0; i != GAME_DIFFICULTY_NUM; i++) {
   553 			for (uint i = 0; i != GAME_DIFFICULTY_NUM; i++) {
   544 				const GameSettingData *gsd = &_game_setting_info[i];
   554 				const GameSettingData *gsd = &_game_setting_info[i];
   545 				value = ((GDType*)&_opt_mod_temp.diff)[i];
   555 				value = ((GDType*)&_opt_mod_temp.diff)[i];
   546 
   556 
   547 				DrawArrowButtons(5, y, 3,
   557 				DrawArrowButtons(5, y, 3,
   548 						!!HasBit(_difficulty_click_a, i) | !!HasBit(_difficulty_click_b, i) << 1,
   558 						(diffic_d->clicked_button == i) ? 1 << diffic_d->clicked_increase : 0,
   549 						!(HasBit(disabled, i) || gsd->min == value),
   559 						!(HasBit(disabled, i) || gsd->min == value),
   550 						!(HasBit(disabled, i) || gsd->max == value));
   560 						!(HasBit(disabled, i) || gsd->max == value));
   551 
   561 
   552 				value += _game_setting_info[i].str;
   562 				value += _game_setting_info[i].str;
   553 				if (i == 4) value *= 1000; // XXX - handle currency option
   563 				if (i == 4) value *= 1000; // XXX - handle currency option
   570 
   580 
   571 					const int y = e->we.click.pt.y - GAMEDIFF_WND_TOP_OFFSET;
   581 					const int y = e->we.click.pt.y - GAMEDIFF_WND_TOP_OFFSET;
   572 					if (y < 0) return;
   582 					if (y < 0) return;
   573 
   583 
   574 					/* Get button from Y coord. */
   584 					/* Get button from Y coord. */
   575 					const uint btn = y / (GAMEDIFF_WND_ROWSIZE + 2);
   585 					const uint8 btn = y / (GAMEDIFF_WND_ROWSIZE + 2);
   576 					if (btn >= GAME_DIFFICULTY_NUM || y % (GAMEDIFF_WND_ROWSIZE + 2) >= 9)
   586 					if (btn >= GAME_DIFFICULTY_NUM || y % (GAMEDIFF_WND_ROWSIZE + 2) >= 9)
   577 						return;
   587 						return;
   578 
   588 
   579 					/* Clicked disabled button? */
   589 					/* Clicked disabled button? */
   580 					if (_game_mode == GM_NORMAL && HasBit(DIFF_INGAME_DISABLED_BUTTONS, btn))
   590 					if (_game_mode == GM_NORMAL && HasBit(DIFF_INGAME_DISABLED_BUTTONS, btn))
   581 						return;
   591 						return;
   582 
   592 
   583 					_difficulty_timeout = 5;
   593 					diffic_d->timeout = 5;
   584 
   594 
   585 					int16 val = ((GDType*)&_opt_mod_temp.diff)[btn];
   595 					int16 val = ((GDType*)&_opt_mod_temp.diff)[btn];
   586 
   596 
   587 					const GameSettingData *info = &_game_setting_info[btn]; // get information about the difficulty setting
   597 					const GameSettingData *info = &_game_setting_info[btn]; // get information about the difficulty setting
   588 					if (x >= 10) {
   598 					if (x >= 10) {
   589 						/* Increase button clicked */
   599 						/* Increase button clicked */
   590 						val = min(val + info->step, info->max);
   600 						val = min(val + info->step, info->max);
   591 						SetBit(_difficulty_click_b, btn);
   601 						diffic_d->clicked_increase = true;
   592 					} else {
   602 					} else {
   593 						/* Decrease button clicked */
   603 						/* Decrease button clicked */
   594 						val -= info->step;
   604 						val -= info->step;
   595 						val = max(val,  info->min);
   605 						val = max(val,  info->min);
   596 						SetBit(_difficulty_click_a, btn);
   606 						diffic_d->clicked_increase = false;
   597 					}
   607 					}
       
   608 					diffic_d->clicked_button = btn;
   598 
   609 
   599 					/* save value in temporary variable */
   610 					/* save value in temporary variable */
   600 					((GDType*)&_opt_mod_temp.diff)[btn] = val;
   611 					((GDType*)&_opt_mod_temp.diff)[btn] = val;
   601 					w->RaiseWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
   612 					w->RaiseWidget(GDW_LVL_EASY + _opt_mod_temp.diff_level);
   602 					SetDifficultyLevel(3, &_opt_mod_temp); // set difficulty level to custom
   613 					SetDifficultyLevel(3, &_opt_mod_temp); // set difficulty level to custom
   640 					DeleteWindow(w);
   651 					DeleteWindow(w);
   641 					break;
   652 					break;
   642 			} break;
   653 			} break;
   643 
   654 
   644 		case WE_MOUSELOOP: /* Handle the visual 'clicking' of the buttons */
   655 		case WE_MOUSELOOP: /* Handle the visual 'clicking' of the buttons */
   645 			if (_difficulty_timeout != 0 && !--_difficulty_timeout) {
   656 			if (diffic_d->timeout != 0) {
   646 				_difficulty_click_a = 0;
   657 				diffic_d->timeout--;
   647 				_difficulty_click_b = 0;
   658 				if (diffic_d->timeout == 0) diffic_d->clicked_button = NO_SETTINGS_BUTTON;
   648 				SetWindowDirty(w);
   659 				SetWindowDirty(w);
   649 			}
   660 			}
   650 			break;
   661 			break;
   651 	}
   662 	}
   652 }
   663 }