src/bridge_gui.c
branchcustombridgeheads
changeset 5649 55c8267c933f
parent 5648 1608018c5ff2
child 5650 aefc131bf5ce
equal deleted inserted replaced
5648:1608018c5ff2 5649:55c8267c933f
     1 /* $Id$ */
       
     2 
       
     3 /** @file bridge_gui.c Graphical user interface for bridge construction*/
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "table/strings.h"
       
     8 #include "functions.h"
       
     9 #include "map.h"
       
    10 #include "window.h"
       
    11 #include "gui.h"
       
    12 #include "viewport.h"
       
    13 #include "gfx.h"
       
    14 #include "command.h"
       
    15 #include "sound.h"
       
    16 #include "variables.h"
       
    17 #include "bridge.h"
       
    18 
       
    19 static struct BridgeData {
       
    20 	uint count;
       
    21 	TileIndex start_tile;
       
    22 	TileIndex end_tile;
       
    23 	byte type;
       
    24 	byte indexes[MAX_BRIDGES];
       
    25 	int32 costs[MAX_BRIDGES];
       
    26 } _bridgedata;
       
    27 
       
    28 void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
       
    29 {
       
    30 	if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
       
    31 }
       
    32 
       
    33 static void BuildBridge(Window *w, int i)
       
    34 {
       
    35 	DeleteWindow(w);
       
    36 	DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
       
    37 		_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge,
       
    38 		CMD_BUILD_BRIDGE | CMD_AUTO | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
       
    39 }
       
    40 
       
    41 static void BuildBridgeWndProc(Window *w, WindowEvent *e)
       
    42 {
       
    43 	switch (e->event) {
       
    44 	case WE_PAINT: {
       
    45 		uint i;
       
    46 
       
    47 		DrawWindowWidgets(w);
       
    48 
       
    49 		for (i = 0; i < 4 && i + w->vscroll.pos < _bridgedata.count; i++) {
       
    50 			const Bridge *b = &_bridge[_bridgedata.indexes[i + w->vscroll.pos]];
       
    51 
       
    52 			SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]);
       
    53 			SetDParam(1, b->speed);
       
    54 			SetDParam(0, b->material);
       
    55 			DrawSprite(b->sprite, 3, 15 + i * 22);
       
    56 
       
    57 			DrawString(44, 15 + i * 22 , STR_500D, 0);
       
    58 		}
       
    59 	} break;
       
    60 
       
    61 	case WE_KEYPRESS: {
       
    62 		uint i = e->we.keypress.keycode - '1';
       
    63 		if (i < 9 && i < _bridgedata.count) {
       
    64 			e->we.keypress.cont = false;
       
    65 			BuildBridge(w, i);
       
    66 		}
       
    67 
       
    68 		break;
       
    69 	}
       
    70 
       
    71 	case WE_CLICK:
       
    72 		if (e->we.click.widget == 2) {
       
    73 			uint ind = ((int)e->we.click.pt.y - 14) / 22;
       
    74 			if (ind < 4 && (ind += w->vscroll.pos) < _bridgedata.count)
       
    75 				BuildBridge(w, ind);
       
    76 		}
       
    77 		break;
       
    78 	}
       
    79 }
       
    80 
       
    81 static const Widget _build_bridge_widgets[] = {
       
    82 {   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
       
    83 {    WWT_CAPTION,   RESIZE_NONE,     7,    11,   199,     0,    13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
    84 {     WWT_MATRIX,   RESIZE_NONE,     7,     0,   187,    14,   101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},
       
    85 {  WWT_SCROLLBAR,   RESIZE_NONE,     7,   188,   199,    14,   101, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
    86 {   WIDGETS_END},
       
    87 };
       
    88 
       
    89 static const WindowDesc _build_bridge_desc = {
       
    90 	WDP_AUTO, WDP_AUTO, 200, 102,
       
    91 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
       
    92 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
       
    93 	_build_bridge_widgets,
       
    94 	BuildBridgeWndProc
       
    95 };
       
    96 
       
    97 
       
    98 static const Widget _build_road_bridge_widgets[] = {
       
    99 {   WWT_CLOSEBOX,   RESIZE_NONE,     7,     0,    10,     0,    13, STR_00C5,                    STR_018B_CLOSE_WINDOW},
       
   100 {    WWT_CAPTION,   RESIZE_NONE,     7,    11,   199,     0,    13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},
       
   101 {     WWT_MATRIX,   RESIZE_NONE,     7,     0,   187,    14,   101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},
       
   102 {  WWT_SCROLLBAR,   RESIZE_NONE,     7,   188,   199,    14,   101, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST},
       
   103 {   WIDGETS_END},
       
   104 };
       
   105 
       
   106 static const WindowDesc _build_road_bridge_desc = {
       
   107 	WDP_AUTO, WDP_AUTO, 200, 102,
       
   108 	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
       
   109 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET,
       
   110 	_build_road_bridge_widgets,
       
   111 	BuildBridgeWndProc
       
   112 };
       
   113 
       
   114 
       
   115 void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type)
       
   116 {
       
   117 	uint j = 0;
       
   118 	int32 ret;
       
   119 	StringID errmsg;
       
   120 
       
   121 	DeleteWindowById(WC_BUILD_BRIDGE, 0);
       
   122 
       
   123 	_bridgedata.type = bridge_type;
       
   124 	_bridgedata.start_tile = start;
       
   125 	_bridgedata.end_tile = end;
       
   126 
       
   127 	errmsg = INVALID_STRING_ID;
       
   128 
       
   129 	// only query bridge building possibility once, result is the same for all bridges!
       
   130 	// returns CMD_ERROR on failure, and price on success
       
   131 	ret = DoCommand(end, start, (bridge_type << 8), DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
       
   132 
       
   133 	if (CmdFailed(ret)) {
       
   134 		errmsg = _error_message;
       
   135 	} else {
       
   136 		// check which bridges can be built
       
   137 		int bridge_len;         // length of the middle parts of the bridge
       
   138 		int tot_bridgedata_len; // total length of bridge
       
   139 
       
   140 		// get absolute bridge length
       
   141 		bridge_len = GetBridgeLength(start, end);
       
   142 		tot_bridgedata_len = bridge_len + 2;
       
   143 
       
   144 		tot_bridgedata_len = CalcBridgeLenCostFactor(tot_bridgedata_len);
       
   145 
       
   146 		for (bridge_type = 0; bridge_type != MAX_BRIDGES; bridge_type++) { // loop for all bridgetypes
       
   147 			if (CheckBridge_Stuff(bridge_type, bridge_len)) {
       
   148 				const Bridge *b = &_bridge[bridge_type];
       
   149 				// bridge is accepted, add to list
       
   150 				// add to terraforming & bulldozing costs the cost of the bridge itself (not computed with DC_QUERY_COST)
       
   151 				_bridgedata.costs[j] = ret + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8);
       
   152 				_bridgedata.indexes[j] = bridge_type;
       
   153 				j++;
       
   154 			}
       
   155 		}
       
   156 	}
       
   157 
       
   158 	_bridgedata.count = j;
       
   159 
       
   160 	if (j != 0) {
       
   161 		Window *w = AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc);
       
   162 		w->vscroll.cap = 4;
       
   163 		w->vscroll.count = (byte)j;
       
   164 	} else {
       
   165 		ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
       
   166 	}
       
   167 }