src/bridge_gui.cpp
author rubidium
Sat, 22 Dec 2007 23:30:28 +0000
changeset 8617 931e0970d509
parent 8612 6414fc21c2f3
child 8627 448ebf3a8291
permissions -rw-r--r--
(svn r11682) -Codechange: move some 'generic' geometry related types into a single file and do not include gfx.h everywhere to get a Point type.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6443
b8f06d8eb7be (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: 6182
diff changeset
     3
/** @file bridge_gui.cpp Graphical user interface for bridge construction */
2262
bd59b2d8d75f (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1781
diff changeset
     6
#include "openttd.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 337
diff changeset
     7
#include "table/strings.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
679
e959706a3e4d (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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "gui.h"
8603
88c5ce6a5215 (svn r11668) -Codechange: more refactoring aimed at reducing compile time and making it more logic where function definitions can be found.
rubidium
parents: 8602
diff changeset
    11
#include "window_gui.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "viewport.h"
8612
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    13
#include "command_func.h"
6414fc21c2f3 (svn r11677) -Codechange: move price and command related types/functions to their respective places.
rubidium
parents: 8610
diff changeset
    14
#include "economy_func.h"
337
66647f97e7c0 (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
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2133
diff changeset
    16
#include "variables.h"
2262
bd59b2d8d75f (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"
8610
17cc343a23dd (svn r11675) -Codechange: split the string types from the string functions.
rubidium
parents: 8603
diff changeset
    18
#include "strings_func.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
static struct BridgeData {
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    21
	uint8 last_size;
2133
098c9da8292e (svn r2643) Get rid of some unnecessary casts
tron
parents: 1977
diff changeset
    22
	uint count;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
	TileIndex start_tile;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
	TileIndex end_tile;
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    25
	uint8 type;
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    26
	uint8 indexes[MAX_BRIDGES];
7486
d130c10f4dab (svn r10246) -Fix (r10297): some forgotten money conversions and truncation issues. Thanks to benc for providing the patch.
rubidium
parents: 7446
diff changeset
    27
	Money costs[MAX_BRIDGES];
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    28
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    29
	BridgeData()
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    30
		: last_size(4)
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    31
		, count(0)
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    32
		{};
2262
bd59b2d8d75f (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
    33
} _bridgedata;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
    35
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
{
541
e1cd34389f79 (svn r925) Use sound enums
tron
parents: 534
diff changeset
    37
	if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
static void BuildBridge(Window *w, int i)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
	DeleteWindow(w);
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2548
diff changeset
    43
	DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2548
diff changeset
    44
		_bridgedata.indexes[i] | (_bridgedata.type << 8), CcBuildBridge,
8017
8c46e61c510f (svn r11040) -Fix [FS#1179]: removing CMD_AUTO from some commands could remotely trigger an assertion.
rubidium
parents: 7837
diff changeset
    45
		CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    46
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    48
/* Names of the build bridge selection window */
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    49
enum BuildBridgeSelectionWidgets {
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    50
	BBSW_CLOSEBOX = 0,
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    51
	BBSW_CAPTION,
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    52
	BBSW_BRIDGE_LIST,
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    53
	BBSW_SCROLLBAR,
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    54
	BBSW_RESIZEBOX
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    55
};
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    56
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
static void BuildBridgeWndProc(Window *w, WindowEvent *e)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
{
2952
6a26eeda9679 (svn r3511) More whitespace ([FS#46] by Rubidium)
tron
parents: 2737
diff changeset
    59
	switch (e->event) {
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    60
		case WE_CREATE:
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    61
			w->resize.step_height = 22;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    62
			w->vscroll.count = _bridgedata.count;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    63
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    64
			if (_bridgedata.last_size <= 4) {
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    65
				w->vscroll.cap = 4;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    66
			} else {
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    67
				/* Resize the bridge selection window if we used a bigger one the last time */
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    68
				w->vscroll.cap = (w->vscroll.count > _bridgedata.last_size) ? _bridgedata.last_size : w->vscroll.count;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    69
				ResizeWindow(w, 0, (w->vscroll.cap - 4) * w->resize.step_height);
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    70
				w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    71
			}
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    72
			break;
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
    73
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    74
		case WE_PAINT: {
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    75
			DrawWindowWidgets(w);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    77
			uint y = 15;
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    78
			for (uint i = 0; (i < w->vscroll.cap) && ((i + w->vscroll.pos) < _bridgedata.count); i++) {
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    79
				const Bridge *b = &_bridge[_bridgedata.indexes[i + w->vscroll.pos]];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
7498
ce6588257e8d (svn r10258) -Codechange: as we are now using int64 all over the place, it's better to use int64 variables in the string generating too instead of packing them into two int32s.
rubidium
parents: 7487
diff changeset
    81
				SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]);
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    82
				SetDParam(1, b->speed * 10 / 16);
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    83
				SetDParam(0, b->material);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    85
				DrawSprite(b->sprite, b->pal, 3, y);
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    86
				DrawString(44, y, STR_500D, TC_FROMSTRING);
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    87
				y += w->resize.step_height;
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    88
			}
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    89
			break;
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    90
		}
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    91
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    92
		case WE_KEYPRESS: {
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
    93
			const uint8 i = e->we.keypress.keycode - '1';
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    94
			if (i < 9 && i < _bridgedata.count) {
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    95
				e->we.keypress.cont = false;
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    96
				BuildBridge(w, i);
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    97
			}
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    98
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
    99
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
		}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 176
diff changeset
   101
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
   102
		case WE_CLICK:
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   103
			if (e->we.click.widget == BBSW_BRIDGE_LIST) {
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   104
				uint ind = ((int)e->we.click.pt.y - 14) / w->resize.step_height;
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   105
				if (ind < w->vscroll.cap) {
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   106
					ind += w->vscroll.pos;
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   107
					if (ind < _bridgedata.count) {
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   108
						BuildBridge(w, ind);
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   109
					}
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   110
				}
6530
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
   111
			}
c43fa965e8ae (svn r8990) -Fix: more indenting and variable scoping
peter1138
parents: 6443
diff changeset
   112
			break;
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   113
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   114
		case WE_RESIZE:
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   115
			w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   116
			w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   117
			SetVScrollCount(w, _bridgedata.count);
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   118
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   119
			_bridgedata.last_size = w->vscroll.cap;
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   120
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   124
/* Widget definition for the rail bridge selection window */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
static const Widget _build_bridge_widgets[] = {
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   126
{   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},            // BBSW_CLOSEBOX
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   127
{    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_100D_SELECT_RAIL_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},  // BBSW_CAPTION
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   128
{     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},  // BBSW_BRIDGE_LIST
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   129
{  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   130
{  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},                // BBSW_RESIZEBOX
176
84990c4b9212 (svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents: 0
diff changeset
   131
{   WIDGETS_END},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   134
/* Window definition for the rail bridge selection window */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
static const WindowDesc _build_bridge_desc = {
7837
65d7362153a6 (svn r10704) -Codechange: provide an infrastructure to have resizable windows that are smaller than the default window size.
rubidium
parents: 7762
diff changeset
   136
	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
5070
0bbf5264bfb7 (svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents: 4634
diff changeset
   137
	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   138
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
	_build_bridge_widgets,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
	BuildBridgeWndProc
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   143
/* Widget definition for the road bridge selection window */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
static const Widget _build_road_bridge_widgets[] = {
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   145
{   WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                    STR_018B_CLOSE_WINDOW},            // BBSW_CLOSEBOX
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   146
{    WWT_CAPTION,   RESIZE_NONE,  7,  11, 199,   0,  13, STR_1803_SELECT_ROAD_BRIDGE, STR_018C_WINDOW_TITLE_DRAG_THIS},  // BBSW_CAPTION
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   147
{     WWT_MATRIX, RESIZE_BOTTOM,  7,   0, 187,  14, 101, 0x401,                       STR_101F_BRIDGE_SELECTION_CLICK},  // BBSW_BRIDGE_LIST
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   148
{  WWT_SCROLLBAR, RESIZE_BOTTOM,  7, 188, 199,  14,  89, 0x0,                         STR_0190_SCROLL_BAR_SCROLLS_LIST}, // BBSW_SCROLLBAR
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   149
{  WWT_RESIZEBOX,     RESIZE_TB,  7, 188, 199,  90, 101, 0x0,                         STR_RESIZE_BUTTON},                // BBSW_RESIZEBOX
176
84990c4b9212 (svn r177) -Fix: padded out Widget code to solve warnings on C99 compiler (Tron)
darkvater
parents: 0
diff changeset
   150
{   WIDGETS_END},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   153
/* Window definition for the road bridge selection window */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
static const WindowDesc _build_road_bridge_desc = {
7837
65d7362153a6 (svn r10704) -Codechange: provide an infrastructure to have resizable windows that are smaller than the default window size.
rubidium
parents: 7762
diff changeset
   155
	WDP_AUTO, WDP_AUTO, 200, 102, 200, 102,
5070
0bbf5264bfb7 (svn r7128) -Codechange: Replace magic numbers by magic enums (windowdesc positioning WDP_AUTO = -1)
Darkvater
parents: 4634
diff changeset
   156
	WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   157
	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
	_build_road_bridge_widgets,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
	BuildBridgeWndProc
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
1977
4392ae3d8e31 (svn r2483) Replace almost 500 "uint tile" (and variants) with "TileIndex tile"
tron
parents: 1891
diff changeset
   163
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, byte bridge_type)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
	DeleteWindowById(WC_BUILD_BRIDGE, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
2262
bd59b2d8d75f (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
   167
	_bridgedata.type = bridge_type;
bd59b2d8d75f (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
   168
	_bridgedata.start_tile = start;
bd59b2d8d75f (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
   169
	_bridgedata.end_tile = end;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   171
	/* only query bridge building possibility once, result is the same for all bridges!
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   172
	 * returns CMD_ERROR on failure, and price on success */
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   173
	StringID errmsg = INVALID_STRING_ID;
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   174
	CommandCost ret = DoCommand(end, start, (bridge_type << 8), DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   175
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   176
	uint8 j = 0;
2737
f16e0a808897 (svn r3282) - Codechange: Replace tests against CMD_ERROR with CmdFailed()
peter1138
parents: 2639
diff changeset
   177
	if (CmdFailed(ret)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
		errmsg = _error_message;
2548
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2470
diff changeset
   179
	} else {
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   180
		/* check which bridges can be built
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   181
		 * get absolute bridge length
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   182
		 * length of the middle parts of the bridge */
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   183
		const uint bridge_len = GetBridgeLength(start, end);
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   184
		/* total length of bridge */
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   185
		const uint tot_bridgedata_len = CalcBridgeLenCostFactor(bridge_len + 2);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   187
		/* loop for all bridgetypes */
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   188
		for (bridge_type = 0; bridge_type != MAX_BRIDGES; bridge_type++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
			if (CheckBridge_Stuff(bridge_type, bridge_len)) {
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   190
				/* bridge is accepted, add to list */
2262
bd59b2d8d75f (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
   191
				const Bridge *b = &_bridge[bridge_type];
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   192
				/* Add to terraforming & bulldozing costs the cost of the
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   193
				 * bridge itself (not computed with DC_QUERY_COST) */
7446
1c4d469f986e (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 7439
diff changeset
   194
				_bridgedata.costs[j] = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8);
2262
bd59b2d8d75f (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
   195
				_bridgedata.indexes[j] = bridge_type;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
				j++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
		}
8479
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   199
a1ab2afafb84 (svn r11539) -Feature: Make the bridge selection window resizable
skidd13
parents: 8320
diff changeset
   200
		_bridgedata.count = j;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
	if (j != 0) {
8516
e7ce655f85ef (svn r11580) -Feature: Memorise the resizing of the build bridge selection window during runtime
skidd13
parents: 8479
diff changeset
   204
		AllocateWindowDesc((_bridgedata.type & 0x80) ? &_build_road_bridge_desc : &_build_bridge_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
	} else {
3422
12cdb13ddb56 (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
   206
		ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
}