author | KUDr |
Fri, 20 Apr 2007 19:43:06 +0000 | |
changeset 6513 | 454347ca3dfb |
parent 6204 | 8cfc3aebfa3a |
child 6943 | 1914f26aee04 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
6117
6507b2a7e71d
(svn r8853) -Cleanup: doxygen changes. Correct forgotten c files to cpp files with the @file tag as well as a few general comments style
belugas
parents:
5931
diff
changeset
|
3 |
/** @file bridge_gui.cpp Graphical user interface for bridge construction */ |
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
4 |
|
0 | 5 |
#include "stdafx.h" |
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1781
diff
changeset
|
6 |
#include "openttd.h" |
507
04b5403aaf6b
(svn r815) Include strings.h only in the files which need it.
tron
parents:
337
diff
changeset
|
7 |
#include "table/strings.h" |
2163
b17b313113a0
(svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents:
2159
diff
changeset
|
8 |
#include "functions.h" |
679
04ca2cd69420
(svn r1117) Move map arrays and some related macros into their own files map.c and map.h
tron
parents:
543
diff
changeset
|
9 |
#include "map.h" |
0 | 10 |
#include "window.h" |
11 |
#include "gui.h" |
|
12 |
#include "viewport.h" |
|
13 |
#include "gfx.h" |
|
14 |
#include "command.h" |
|
337
cbe0c766c947
(svn r513) Merge revisions 402, 416, 417, 478, 479, 511, 512 from map to trunk
tron
parents:
203
diff
changeset
|
15 |
#include "sound.h" |
2159
f6284cf5fab0
(svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents:
2133
diff
changeset
|
16 |
#include "variables.h" |
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
17 |
#include "bridge.h" |
0 | 18 |
|
19 |
static struct BridgeData { |
|
2133 | 20 |
uint count; |
0 | 21 |
TileIndex start_tile; |
22 |
TileIndex end_tile; |
|
23 |
byte type; |
|
24 |
byte indexes[MAX_BRIDGES]; |
|
25 |
int32 costs[MAX_BRIDGES]; |
|
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
26 |
} _bridgedata; |
0 | 27 |
|
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1891
diff
changeset
|
28 |
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2) |
0 | 29 |
{ |
541 | 30 |
if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile); |
0 | 31 |
} |
32 |
||
33 |
static void BuildBridge(Window *w, int i) |
|
34 |
{ |
|
35 |
DeleteWindow(w); |
|
2639 | 36 |
DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile, |
37 |
_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge, |
|
0 | 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 |
{ |
|
2952 | 43 |
switch (e->event) { |
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
44 |
case WE_PAINT: |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
45 |
DrawWindowWidgets(w); |
0 | 46 |
|
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
47 |
for (uint i = 0; i < 4 && i + w->vscroll.pos < _bridgedata.count; i++) { |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
48 |
const Bridge *b = &_bridge[_bridgedata.indexes[i + w->vscroll.pos]]; |
0 | 49 |
|
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
50 |
SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
51 |
SetDParam(1, b->speed * 10 / 16); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
52 |
SetDParam(0, b->material); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
53 |
DrawSprite(b->sprite, b->pal, 3, 15 + i * 22); |
0 | 54 |
|
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
55 |
DrawString(44, 15 + i * 22 , STR_500D, 0); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
56 |
} |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
57 |
break; |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
58 |
|
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
59 |
case WE_KEYPRESS: { |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
60 |
uint i = e->we.keypress.keycode - '1'; |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
61 |
if (i < 9 && i < _bridgedata.count) { |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
62 |
e->we.keypress.cont = false; |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
63 |
BuildBridge(w, i); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
64 |
} |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
65 |
|
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
66 |
break; |
0 | 67 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
176
diff
changeset
|
68 |
|
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
69 |
case WE_CLICK: |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
70 |
if (e->we.click.widget == 2) { |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
71 |
uint ind = ((int)e->we.click.pt.y - 14) / 22; |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
72 |
if (ind < 4 && (ind += w->vscroll.pos) < _bridgedata.count) |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
73 |
BuildBridge(w, ind); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
74 |
} |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
75 |
break; |
0 | 76 |
} |
77 |
} |
|
78 |
||
79 |
static const Widget _build_bridge_widgets[] = { |
|
4344
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
80 |
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
81 |
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
82 |
{ WWT_MATRIX, RESIZE_NONE, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
83 |
{ WWT_SCROLLBAR, RESIZE_NONE, 7, 188, 199, 14, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
176
84990c4b9212
(svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents:
0
diff
changeset
|
84 |
{ WIDGETS_END}, |
0 | 85 |
}; |
86 |
||
87 |
static const WindowDesc _build_bridge_desc = { |
|
5070
7f5b13b7e728
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
88 |
WDP_AUTO, WDP_AUTO, 200, 102, |
7f5b13b7e728
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
89 |
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, |
0 | 90 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, |
91 |
_build_bridge_widgets, |
|
92 |
BuildBridgeWndProc |
|
93 |
}; |
|
94 |
||
95 |
||
96 |
static const Widget _build_road_bridge_widgets[] = { |
|
4344
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
97 |
{ WWT_CLOSEBOX, RESIZE_NONE, 7, 0, 10, 0, 13, STR_00C5, STR_018B_CLOSE_WINDOW}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
98 |
{ WWT_CAPTION, RESIZE_NONE, 7, 11, 199, 0, 13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
99 |
{ WWT_MATRIX, RESIZE_NONE, 7, 0, 187, 14, 101, 0x401, STR_101F_BRIDGE_SELECTION_CLICK}, |
7e123fec5b0b
(svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents:
3491
diff
changeset
|
100 |
{ WWT_SCROLLBAR, RESIZE_NONE, 7, 188, 199, 14, 101, 0x0, STR_0190_SCROLL_BAR_SCROLLS_LIST}, |
176
84990c4b9212
(svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents:
0
diff
changeset
|
101 |
{ WIDGETS_END}, |
0 | 102 |
}; |
103 |
||
104 |
static const WindowDesc _build_road_bridge_desc = { |
|
5070
7f5b13b7e728
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
105 |
WDP_AUTO, WDP_AUTO, 200, 102, |
7f5b13b7e728
(svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents:
4634
diff
changeset
|
106 |
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR, |
0 | 107 |
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET, |
108 |
_build_road_bridge_widgets, |
|
109 |
BuildBridgeWndProc |
|
110 |
}; |
|
111 |
||
112 |
||
1977
37bbebf94434
(svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents:
1891
diff
changeset
|
113 |
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type) |
0 | 114 |
{ |
2133 | 115 |
uint j = 0; |
0 | 116 |
int32 ret; |
2470 | 117 |
StringID errmsg; |
0 | 118 |
|
119 |
DeleteWindowById(WC_BUILD_BRIDGE, 0); |
|
120 |
||
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
121 |
_bridgedata.type = bridge_type; |
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
122 |
_bridgedata.start_tile = start; |
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
123 |
_bridgedata.end_tile = end; |
0 | 124 |
|
2470 | 125 |
errmsg = INVALID_STRING_ID; |
0 | 126 |
|
127 |
// only query bridge building possibility once, result is the same for all bridges! |
|
2548
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
128 |
// returns CMD_ERROR on failure, and price on success |
3491
35d747bb5e82
(svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents:
3477
diff
changeset
|
129 |
ret = DoCommand(end, start, (bridge_type << 8), DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE); |
0 | 130 |
|
2737
fb1556b8f5e0
(svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents:
2639
diff
changeset
|
131 |
if (CmdFailed(ret)) { |
0 | 132 |
errmsg = _error_message; |
2548
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
133 |
} else { |
49c8a096033f
(svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents:
2470
diff
changeset
|
134 |
// check which bridges can be built |
0 | 135 |
// get absolute bridge length |
6204
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
136 |
// length of the middle parts of the bridge |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
137 |
int bridge_len = GetBridgeLength(start, end); |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
138 |
// total length of bridge |
8cfc3aebfa3a
(svn r8990) -Fix: more indenting and variable scoping
peter1138
parents:
6117
diff
changeset
|
139 |
int tot_bridgedata_len = bridge_len + 2; |
0 | 140 |
|
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
141 |
tot_bridgedata_len = CalcBridgeLenCostFactor(tot_bridgedata_len); |
0 | 142 |
|
4434
a08cb4b5c179
(svn r6204) -Cleanup: replace non-indentation with spaces; like '}<TAB>else {' -> '} else {', tabs between code and comment, etc.
rubidium
parents:
4344
diff
changeset
|
143 |
for (bridge_type = 0; bridge_type != MAX_BRIDGES; bridge_type++) { // loop for all bridgetypes |
0 | 144 |
if (CheckBridge_Stuff(bridge_type, bridge_len)) { |
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
145 |
const Bridge *b = &_bridge[bridge_type]; |
0 | 146 |
// bridge is accepted, add to list |
147 |
// add to terraforming & bulldozing costs the cost of the bridge itself (not computed with DC_QUERY_COST) |
|
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
148 |
_bridgedata.costs[j] = ret + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8); |
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
149 |
_bridgedata.indexes[j] = bridge_type; |
0 | 150 |
j++; |
151 |
} |
|
152 |
} |
|
153 |
} |
|
154 |
||
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
155 |
_bridgedata.count = j; |
0 | 156 |
|
157 |
if (j != 0) { |
|
2262
252cfd94c54d
(svn r2782) -Codechange: Started cleaning the bridge code. Removed numerous global variables containing bridge information and joined them in a struct. Introduced GetBridgeType and GetBridgePiece and fixed some minor stuff (whitespace etc)
celestar
parents:
2186
diff
changeset
|
158 |
Window *w = AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc); |
0 | 159 |
w->vscroll.cap = 4; |
160 |
w->vscroll.count = (byte)j; |
|
161 |
} else { |
|
3422
a6eba3443452
(svn r4249) -Codechange: Replace more occurences of 16 by TILE_SIZE and of 8 by TILE_HEIGHT. Reverted one change from the previous commit because it was faulty
celestar
parents:
2952
diff
changeset
|
162 |
ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE); |
0 | 163 |
} |
164 |
} |