settings_gui.c
changeset 164 0cbdf3c9bde1
parent 156 8fef5e5752d6
child 176 84990c4b9212
equal deleted inserted replaced
163:deb9b58db3f4 164:0cbdf3c9bde1
     1 #include "stdafx.h"
     1 #include "stdafx.h"
     2 #include "ttd.h"
     2 #include "ttd.h"
     3 
     3 
     4 #include "window.h"
     4 #include "window.h"
     5 #include "gui.h"
     5 #include "gui.h"
     6 #include "viewport.h"
       
     7 #include "gfx.h"
     6 #include "gfx.h"
     8 #include "command.h"
     7 #include "command.h"
     9 #include "engine.h"
     8 #include "engine.h"
    10 
     9 
    11 static uint32 _dropdown_disabled;
       
    12 static const StringID *_dropdown_items;
       
    13 static int _dropdown_selindex;
       
    14 static uint _dropdown_item_count;
       
    15 static byte _dropdown_button;
       
    16 static WindowClass _dropdown_windowclass;
       
    17 static WindowNumber _dropdown_windownum;
       
    18 static byte _dropdown_var1;
       
    19 static byte _dropdown_var2;
       
    20 
       
    21 static uint32 _difficulty_click_a;
    10 static uint32 _difficulty_click_a;
    22 static uint32 _difficulty_click_b;
    11 static uint32 _difficulty_click_b;
    23 static byte _difficulty_timeout;
    12 static byte _difficulty_timeout;
    24 
       
    25 static Widget _dropdown_menu_widgets[] = {
       
    26 {     WWT_IMGBTN,     0,     0, 0,     0, 0, 0x0},
       
    27 {      WWT_LAST},
       
    28 };
       
    29 
       
    30 static int GetDropdownItem(Window *w)
       
    31 {
       
    32 	uint item;
       
    33 	int y;
       
    34 
       
    35 	if (GetWidgetFromPos(w, _cursor.pos.x - w->left, _cursor.pos.y - w->top) < 0)
       
    36 		return -1;
       
    37 	
       
    38 	y = _cursor.pos.y - w->top - 2;
       
    39 
       
    40 	if (y < 0)
       
    41 		return - 1;
       
    42 
       
    43 	item = y / 10;
       
    44 	if (item >= _dropdown_item_count || HASBIT(_dropdown_disabled,item) || _dropdown_items[item] == 0)
       
    45 		return - 1;
       
    46 
       
    47 	return item;
       
    48 }
       
    49 
       
    50 void DropdownMenuWndProc(Window *w, WindowEvent *e)
       
    51 {
       
    52 	int item;
       
    53 
       
    54 	switch(e->event) {
       
    55 	case WE_PAINT: {
       
    56 		int x,y,i,sel;
       
    57 		uint32 dis;
       
    58 
       
    59 		DrawWindowWidgets(w);
       
    60 
       
    61 		x = 1;
       
    62 		y = 2;
       
    63 		sel = _dropdown_selindex;
       
    64 		dis = _dropdown_disabled;
       
    65 
       
    66 		for(i=0; _dropdown_items[i] != INVALID_STRING_ID; i++) {
       
    67 			if (_dropdown_items[i] != 0) {
       
    68 				if (sel == 0) {
       
    69 					GfxFillRect(x+1, y, x+w->width-4, y + 9, 0);
       
    70 				}
       
    71 				DrawString(x+2, y, _dropdown_items[i], sel==0 ? 12 : 16);
       
    72 
       
    73 				if (dis & 1) {
       
    74 					GfxFillRect(x, y, x+w->width-3, y + 9, 0x8000 + 
       
    75 						_color_list[_dropdown_menu_widgets[0].color].window_color_bga);
       
    76 				}
       
    77 			} else {
       
    78 				int color_1 = _color_list[_dropdown_menu_widgets[0].color].window_color_1a;
       
    79 				int color_2 = _color_list[_dropdown_menu_widgets[0].color].window_color_2;
       
    80 				GfxFillRect(x+1, y+3, x+w->width-5, y+3, color_1);
       
    81 				GfxFillRect(x+1, y+4, x+w->width-5, y+4, color_2);
       
    82 			}
       
    83 			y += 10;
       
    84 			sel--;
       
    85 			dis>>=1;
       
    86 		}
       
    87 	} break;
       
    88 
       
    89 	case WE_CLICK: {
       
    90 		item = GetDropdownItem(w);
       
    91 		if (item >= 0) {
       
    92 			_dropdown_var1 = 4;
       
    93 			_dropdown_selindex = item;
       
    94 			SetWindowDirty(w);
       
    95 		}
       
    96 	} break;
       
    97 
       
    98 	case WE_MOUSELOOP: {
       
    99 		Window *w2 = FindWindowById(_dropdown_windowclass, _dropdown_windownum);
       
   100 		if (w2 == NULL) {
       
   101 			DeleteWindow(w);
       
   102 			return;
       
   103 		}
       
   104 
       
   105 		if (_dropdown_var1 != 0 && --_dropdown_var1 == 0) {
       
   106 			WindowEvent e;
       
   107 			e.event = WE_DROPDOWN_SELECT;
       
   108 			e.dropdown.button = _dropdown_button;
       
   109 			e.dropdown.index = _dropdown_selindex;
       
   110 			w2->wndproc(w2, &e);
       
   111 			DeleteWindow(w);
       
   112 			return;
       
   113 		}
       
   114 
       
   115 		if (_dropdown_var2 != 0) {
       
   116 			item = GetDropdownItem(w);
       
   117 
       
   118 			if (!_left_button_clicked) {
       
   119 				_dropdown_var2 = 0;
       
   120 				if (item < 0)
       
   121 					return;
       
   122 				_dropdown_var1 = 2;
       
   123 			} else {
       
   124 				if (item < 0)
       
   125 					return;
       
   126 			}
       
   127 
       
   128 			_dropdown_selindex = item;
       
   129 			SetWindowDirty(w);
       
   130 		}
       
   131 	} break;
       
   132 		
       
   133 	case WE_DESTROY: {
       
   134 		Window *w2 = FindWindowById(_dropdown_windowclass, _dropdown_windownum);
       
   135 		if (w2 != NULL) {
       
   136 			CLRBIT(w2->click_state, _dropdown_button);
       
   137 			InvalidateWidget(w2, _dropdown_button);
       
   138 		}
       
   139 	} break;
       
   140 	}
       
   141 }
       
   142 
       
   143 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask)
       
   144 {
       
   145 	WindowNumber num;
       
   146 	WindowClass cls;
       
   147 	int i,t1,t2;
       
   148 	const Widget *wi;
       
   149 	Window *w2;
       
   150 	uint32 old_click_state = w->click_state;
       
   151 	
       
   152 	_dropdown_disabled = disabled_mask;
       
   153 
       
   154 	cls = w->window_class;
       
   155 	num = w->window_number;
       
   156 	DeleteWindowById(WC_DROPDOWN_MENU, 0);
       
   157 	w = FindWindowById(cls, num);
       
   158 
       
   159 	if (HASBIT(old_click_state, button))
       
   160 		return;
       
   161 
       
   162 	SETBIT(w->click_state, button);
       
   163 
       
   164 	InvalidateWidget(w, button);
       
   165 	
       
   166 	for(i=0;strings[i] != INVALID_STRING_ID;i++);
       
   167 	if (i == 0)
       
   168 		return;
       
   169 
       
   170 	_dropdown_items = strings;
       
   171 	_dropdown_item_count = i;
       
   172 	_dropdown_selindex = selected;
       
   173 	
       
   174 	_dropdown_windowclass = w->window_class;
       
   175 	_dropdown_windownum = w->window_number;
       
   176 	_dropdown_button = button;
       
   177 	
       
   178 	_dropdown_var1 = 0;
       
   179 	_dropdown_var2 = 1;
       
   180 
       
   181 	wi = &w->widget[button];
       
   182 
       
   183 	_dropdown_menu_widgets[0].color = wi->color;
       
   184 
       
   185 	w2 = AllocateWindow(
       
   186 		w->left + wi[-1].left + 1,
       
   187 		w->top + wi->bottom + 2,
       
   188 		(_dropdown_menu_widgets[0].right=t1=wi->right - wi[-1].left, t1 + 1), 
       
   189 		(_dropdown_menu_widgets[0].bottom=t2=i*10+3, t2+1), 
       
   190 		DropdownMenuWndProc,
       
   191 		0x3F,
       
   192 		_dropdown_menu_widgets);
       
   193 
       
   194 
       
   195 	w2->flags4 &= ~WF_WHITE_BORDER_MASK;
       
   196 }
       
   197 
    13 
   198 extern const StringID _currency_string_list[];
    14 extern const StringID _currency_string_list[];
   199 extern uint GetMaskOfAllowedCurrencies();
    15 extern uint GetMaskOfAllowedCurrencies();
   200 
    16 
   201 static const StringID _distances_dropdown[] = {
    17 static const StringID _distances_dropdown[] = {