src/bridge_gui.cpp
changeset 8020 ba100ceda5b4
parent 7983 3b1fec9d5e31
child 8106 e6790dd9e750
equal deleted inserted replaced
8019:fc0e94dee165 8020:ba100ceda5b4
    16 #include "sound.h"
    16 #include "sound.h"
    17 #include "variables.h"
    17 #include "variables.h"
    18 #include "bridge.h"
    18 #include "bridge.h"
    19 
    19 
    20 static struct BridgeData {
    20 static struct BridgeData {
       
    21 	uint8 last_size;
    21 	uint count;
    22 	uint count;
    22 	TileIndex start_tile;
    23 	TileIndex start_tile;
    23 	TileIndex end_tile;
    24 	TileIndex end_tile;
    24 	uint8 type;
    25 	uint8 type;
    25 	uint8 indexes[MAX_BRIDGES];
    26 	uint8 indexes[MAX_BRIDGES];
    26 	Money costs[MAX_BRIDGES];
    27 	Money costs[MAX_BRIDGES];
       
    28 
       
    29 	BridgeData()
       
    30 		: last_size(4)
       
    31 		, count(0)
       
    32 		{};
    27 } _bridgedata;
    33 } _bridgedata;
    28 
    34 
    29 void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
    35 void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
    30 {
    36 {
    31 	if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
    37 	if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
    37 	DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
    43 	DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
    38 		_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge,
    44 		_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge,
    39 		CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
    45 		CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
    40 }
    46 }
    41 
    47 
       
    48 /* Names of the build bridge selection window */
       
    49 enum BuildBridgeSelectionWidgets {
       
    50 	BBSW_CLOSEBOX = 0,
       
    51 	BBSW_CAPTION,
       
    52 	BBSW_BRIDGE_LIST,
       
    53 	BBSW_SCROLLBAR,
       
    54 	BBSW_RESIZEBOX
       
    55 };
       
    56 
    42 static void BuildBridgeWndProc(Window *w, WindowEvent *e)
    57 static void BuildBridgeWndProc(Window *w, WindowEvent *e)
    43 {
    58 {
    44 	switch (e->event) {
    59 	switch (e->event) {
       
    60 		case WE_CREATE:
       
    61 			w->resize.step_height = 22;
       
    62 			w->vscroll.count = _bridgedata.count;
       
    63 
       
    64 			if (_bridgedata.last_size <= 4) {
       
    65 				w->vscroll.cap = 4;
       
    66 			} else {
       
    67 				/* Resize the bridge selection window if we used a bigger one the last time */
       
    68 				w->vscroll.cap = (w->vscroll.count > _bridgedata.last_size) ? _bridgedata.last_size : w->vscroll.count;
       
    69 				ResizeWindow(w, 0, (w->vscroll.cap - 4) * w->resize.step_height);
       
    70 				w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
       
    71 			}
       
    72 			break;
       
    73 
    45 		case WE_PAINT: {
    74 		case WE_PAINT: {
    46 			DrawWindowWidgets(w);
    75 			DrawWindowWidgets(w);
    47 
    76 
    48 			uint y = 15;
    77 			uint y = 15;
    49 			for (uint i = 0; (i < w->vscroll.cap) && ((i + w->vscroll.pos) < _bridgedata.count); i++) {
    78 			for (uint i = 0; (i < w->vscroll.cap) && ((i + w->vscroll.pos) < _bridgedata.count); i++) {
    69 
    98 
    70 			break;
    99 			break;
    71 		}
   100 		}
    72 
   101 
    73 		case WE_CLICK:
   102 		case WE_CLICK:
    74 			if (e->we.click.widget == 2) {
   103 			if (e->we.click.widget == BBSW_BRIDGE_LIST) {
    75 				uint ind = ((int)e->we.click.pt.y - 14) / w->resize.step_height;
   104 				uint ind = ((int)e->we.click.pt.y - 14) / w->resize.step_height;
    76 				if (ind < w->vscroll.cap) {
   105 				if (ind < w->vscroll.cap) {
    77 					ind += w->vscroll.pos;
   106 					ind += w->vscroll.pos;
    78 					if (ind < _bridgedata.count) {
   107 					if (ind < _bridgedata.count) {
    79 						BuildBridge(w, ind);
   108 						BuildBridge(w, ind);
    82 			}
   111 			}
    83 			break;
   112 			break;
    84 
   113 
    85 		case WE_RESIZE:
   114 		case WE_RESIZE:
    86 			w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
   115 			w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
    87 			w->widget[2].data = (w->vscroll.cap << 8) + 1;
   116 			w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
    88 			SetVScrollCount(w, _bridgedata.count);
   117 			SetVScrollCount(w, _bridgedata.count);
       
   118 
       
   119 			_bridgedata.last_size = w->vscroll.cap;
    89 			break;
   120 			break;
    90 	}
   121 	}
    91 }
   122 }
    92 
   123 
       
   124 /* Widget definition for the rail bridge selection window */
    93 static const Widget _build_bridge_widgets[] = {
   125 static const Widget _build_bridge_widgets[] = {
    94 {   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
   126 {   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},            // BBSW_CLOSEBOX
    95 {    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},
   127 {    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},  // BBSW_CAPTION
    96 {     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},
   128 {     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},  // BBSW_BRIDGE_LIST
    97 {  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST},
   129 {  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
    98 {  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},
   130 {  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},                // BBSW_RESIZEBOX
    99 {   WIDGETS_END},
   131 {   WIDGETS_END},
   100 };
   132 };
   101 
   133 
       
   134 /* Window definition for the rail bridge selection window */
   102 static const WindowDesc _build_bridge_desc = {
   135 static const WindowDesc _build_bridge_desc = {
   103 	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
   136 	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
   104 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
   137 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
   105 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
   138 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
   106 	_build_bridge_widgets,
   139 	_build_bridge_widgets,
   107 	BuildBridgeWndProc
   140 	BuildBridgeWndProc
   108 };
   141 };
   109 
   142 
   110 
   143 /* Widget definition for the road bridge selection window */
   111 static const Widget _build_road_bridge_widgets[] = {
   144 static const Widget _build_road_bridge_widgets[] = {
   112 {   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
   145 {   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},            // BBSW_CLOSEBOX
   113 {    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},
   146 {    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},  // BBSW_CAPTION
   114 {     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},
   147 {     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},  // BBSW_BRIDGE_LIST
   115 {  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST},
   148 {  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
   116 {  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},
   149 {  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},                // BBSW_RESIZEBOX
   117 {   WIDGETS_END},
   150 {   WIDGETS_END},
   118 };
   151 };
   119 
   152 
       
   153 /* Window definition for the road bridge selection window */
   120 static const WindowDesc _build_road_bridge_desc = {
   154 static const WindowDesc _build_road_bridge_desc = {
   121 	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
   155 	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
   122 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
   156 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
   123 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
   157 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
   124 	_build_road_bridge_widgets,
   158 	_build_road_bridge_widgets,
   165 
   199 
   166 		_bridgedata.count = j;
   200 		_bridgedata.count = j;
   167 	}
   201 	}
   168 
   202 
   169 	if (j != 0) {
   203 	if (j != 0) {
   170 		Window *w = AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc);
   204 		AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc);
   171 		w->vscroll.cap = 4;
       
   172 		w->vscroll.count = j;
       
   173 		w->resize.step_height = 22;
       
   174 	} else {
   205 	} else {
   175 		ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
   206 		ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
   176 	}
   207 	}
   177 }
   208 }