mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-02-06 22:37:22 +00:00
(svn r13149) -Codechange: make a class of the BuildBridgeWindow.
This commit is contained in:
parent
8e47178919
commit
736671347b
@ -17,37 +17,34 @@
|
|||||||
#include "viewport_func.h"
|
#include "viewport_func.h"
|
||||||
#include "gfx_func.h"
|
#include "gfx_func.h"
|
||||||
#include "tunnelbridge.h"
|
#include "tunnelbridge.h"
|
||||||
|
#include "sortlist_type.h"
|
||||||
|
|
||||||
#include "table/strings.h"
|
#include "table/strings.h"
|
||||||
|
|
||||||
static struct BridgeData {
|
/**
|
||||||
uint8 last_size;
|
* Carriage for the data we need if we want to build a bridge
|
||||||
uint count;
|
*/
|
||||||
TileIndex start_tile;
|
struct BuildBridgeData {
|
||||||
TileIndex end_tile;
|
BridgeType index;
|
||||||
uint32 type; ///< Data type for the bridge. Bit 16,15 = transport type, 14..8 = road/rail pieces, 7..0 = type of bridge
|
const BridgeSpec *spec;
|
||||||
BridgeType indexes[MAX_BRIDGES];
|
Money cost;
|
||||||
Money costs[MAX_BRIDGES];
|
};
|
||||||
|
|
||||||
BridgeData()
|
typedef GUIList<BuildBridgeData> GUIBridgeList;
|
||||||
: last_size(4)
|
|
||||||
, count(0)
|
|
||||||
{};
|
|
||||||
} _bridgedata;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Callback executed after a build Bridge CMD has been called
|
||||||
|
*
|
||||||
|
* @param scucess True if the build succeded
|
||||||
|
* @param tile The tile where the command has been executed
|
||||||
|
* @param p1 not used
|
||||||
|
* @param p2 not used
|
||||||
|
*/
|
||||||
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
void CcBuildBridge(bool success, TileIndex tile, uint32 p1, uint32 p2)
|
||||||
{
|
{
|
||||||
if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
|
if (success) SndPlayTileFx(SND_27_BLACKSMITH_ANVIL, tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void BuildBridge(Window *w, int i)
|
|
||||||
{
|
|
||||||
delete w;
|
|
||||||
DoCommandP(_bridgedata.end_tile, _bridgedata.start_tile,
|
|
||||||
_bridgedata.type | _bridgedata.indexes[i], CcBuildBridge,
|
|
||||||
CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Names of the build bridge selection window */
|
/* Names of the build bridge selection window */
|
||||||
enum BuildBridgeSelectionWidgets {
|
enum BuildBridgeSelectionWidgets {
|
||||||
BBSW_CLOSEBOX = 0,
|
BBSW_CLOSEBOX = 0,
|
||||||
@ -57,72 +54,111 @@ enum BuildBridgeSelectionWidgets {
|
|||||||
BBSW_RESIZEBOX
|
BBSW_RESIZEBOX
|
||||||
};
|
};
|
||||||
|
|
||||||
static void BuildBridgeWndProc(Window *w, WindowEvent *e)
|
class BuildBridgeWindow : public Window {
|
||||||
{
|
private:
|
||||||
switch (e->event) {
|
/* The last size of the build bridge window
|
||||||
case WE_CREATE:
|
* is saved during runtime */
|
||||||
w->resize.step_height = 22;
|
static uint8 last_size;
|
||||||
w->vscroll.count = _bridgedata.count;
|
|
||||||
|
|
||||||
if (_bridgedata.last_size <= 4) {
|
TileIndex start_tile;
|
||||||
w->vscroll.cap = 4;
|
TileIndex end_tile;
|
||||||
} else {
|
uint32 type;
|
||||||
/* Resize the bridge selection window if we used a bigger one the last time */
|
const GUIBridgeList *bridges;
|
||||||
w->vscroll.cap = (w->vscroll.count > _bridgedata.last_size) ? _bridgedata.last_size : w->vscroll.count;
|
|
||||||
ResizeWindow(w, 0, (w->vscroll.cap - 4) * w->resize.step_height);
|
|
||||||
w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case WE_PAINT: {
|
void BuildBridge(uint8 i)
|
||||||
w->DrawWidgets();
|
{
|
||||||
|
DoCommandP(this->end_tile, this->start_tile, this->type | this->bridges->sort_list[i].index,
|
||||||
|
CcBuildBridge, CMD_BUILD_BRIDGE | CMD_MSG(STR_5015_CAN_T_BUILD_BRIDGE_HERE));
|
||||||
|
}
|
||||||
|
|
||||||
uint y = 15;
|
public:
|
||||||
for (uint i = 0; (i < w->vscroll.cap) && ((i + w->vscroll.pos) < _bridgedata.count); i++) {
|
BuildBridgeWindow(const WindowDesc *desc, TileIndex start, TileIndex end, uint32 br_type, const GUIBridgeList *bl) : Window(desc),
|
||||||
const BridgeSpec *b = GetBridgeSpec(_bridgedata.indexes[i + w->vscroll.pos]);
|
start_tile(start),
|
||||||
|
end_tile(end),
|
||||||
|
type(br_type),
|
||||||
|
bridges(bl)
|
||||||
|
{
|
||||||
|
|
||||||
SetDParam(2, _bridgedata.costs[i + w->vscroll.pos]);
|
/* Change the data, or the caption of the gui. Set it to road or rail, accordingly */
|
||||||
SetDParam(1, b->speed * 10 / 16);
|
this->widget[BBSW_CAPTION].data = (GB(this->type, 15, 2) == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE;
|
||||||
SetDParam(0, b->material);
|
|
||||||
|
|
||||||
DrawSprite(b->sprite, b->pal, 3, y);
|
this->resize.step_height = 22;
|
||||||
DrawString(44, y, STR_500D, TC_FROMSTRING);
|
this->vscroll.count = bl->list_length;
|
||||||
y += w->resize.step_height;
|
|
||||||
}
|
if (this->last_size <= 4) {
|
||||||
break;
|
this->vscroll.cap = 4;
|
||||||
|
} else {
|
||||||
|
/* Resize the bridge selection window if we used a bigger one the last time */
|
||||||
|
this->vscroll.cap = (this->vscroll.count > this->last_size) ? this->last_size : this->vscroll.count;
|
||||||
|
ResizeWindow(this, 0, (this->vscroll.cap - 4) * this->resize.step_height);
|
||||||
|
this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
case WE_KEYPRESS: {
|
this->FindWindowPlacementAndResize(desc);
|
||||||
const uint8 i = e->we.keypress.keycode - '1';
|
}
|
||||||
if (i < 9 && i < _bridgedata.count) {
|
|
||||||
e->we.keypress.cont = false;
|
|
||||||
BuildBridge(w, i);
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
~BuildBridgeWindow()
|
||||||
|
{
|
||||||
|
free(this->bridges->sort_list);
|
||||||
|
delete bridges;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void OnPaint()
|
||||||
|
{
|
||||||
|
this->DrawWidgets();
|
||||||
|
|
||||||
|
uint y = 15;
|
||||||
|
for (uint i = 0; (i < this->vscroll.cap) && ((i + this->vscroll.pos) < this->bridges->list_length); i++) {
|
||||||
|
const BridgeSpec *b = this->bridges->sort_list[i + this->vscroll.pos].spec;
|
||||||
|
|
||||||
|
SetDParam(2, this->bridges->sort_list[i + this->vscroll.pos].cost);
|
||||||
|
SetDParam(1, b->speed * 10 / 16);
|
||||||
|
SetDParam(0, b->material);
|
||||||
|
|
||||||
|
DrawSprite(b->sprite, b->pal, 3, y);
|
||||||
|
DrawString(44, y, STR_500D, TC_FROMSTRING);
|
||||||
|
y += this->resize.step_height;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case WE_CLICK:
|
virtual bool OnKeyPress(uint16 key, uint16 keycode)
|
||||||
if (e->we.click.widget == BBSW_BRIDGE_LIST) {
|
{
|
||||||
uint ind = ((int)e->we.click.pt.y - 14) / w->resize.step_height;
|
const uint8 i = keycode - '1';
|
||||||
if (ind < w->vscroll.cap) {
|
if (i < 9 && i < this->bridges->list_length) {
|
||||||
ind += w->vscroll.pos;
|
/* Build the requested bridge */
|
||||||
if (ind < _bridgedata.count) {
|
this->BuildBridge(i);
|
||||||
BuildBridge(w, ind);
|
delete this;
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void OnClick(Point pt, int widget)
|
||||||
|
{
|
||||||
|
if (widget == BBSW_BRIDGE_LIST) {
|
||||||
|
uint i = ((int)pt.y - 14) / this->resize.step_height;
|
||||||
|
if (i < this->vscroll.cap) {
|
||||||
|
i += this->vscroll.pos;
|
||||||
|
if (i < this->bridges->list_length) {
|
||||||
|
this->BuildBridge(i);
|
||||||
|
delete this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
}
|
||||||
|
|
||||||
case WE_RESIZE:
|
|
||||||
w->vscroll.cap += e->we.sizing.diff.y / (int)w->resize.step_height;
|
|
||||||
w->widget[BBSW_BRIDGE_LIST].data = (w->vscroll.cap << 8) + 1;
|
|
||||||
SetVScrollCount(w, _bridgedata.count);
|
|
||||||
|
|
||||||
_bridgedata.last_size = w->vscroll.cap;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
virtual void OnResize(Point new_size, Point delta)
|
||||||
|
{
|
||||||
|
this->vscroll.cap += delta.y / (int)this->resize.step_height;
|
||||||
|
this->widget[BBSW_BRIDGE_LIST].data = (this->vscroll.cap << 8) + 1;
|
||||||
|
SetVScrollCount(this, this->bridges->list_length);
|
||||||
|
|
||||||
|
this->last_size = this->vscroll.cap;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Set the default size of the Build Bridge Window */
|
||||||
|
uint8 BuildBridgeWindow::last_size = 4;
|
||||||
|
|
||||||
/* Widget definition for the rail bridge selection window */
|
/* Widget definition for the rail bridge selection window */
|
||||||
static const Widget _build_bridge_widgets[] = {
|
static const Widget _build_bridge_widgets[] = {
|
||||||
@ -140,23 +176,62 @@ static const WindowDesc _build_bridge_desc = {
|
|||||||
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
|
WC_BUILD_BRIDGE, WC_BUILD_TOOLBAR,
|
||||||
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
|
WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_RESIZABLE,
|
||||||
_build_bridge_widgets,
|
_build_bridge_widgets,
|
||||||
BuildBridgeWndProc
|
NULL
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a buildable bridge to the list.
|
||||||
|
* If the list is empty a new one is created.
|
||||||
|
*
|
||||||
|
* @param bl The list which we want to manage
|
||||||
|
* @param item The item to add
|
||||||
|
* @return The pointer to the list
|
||||||
|
*/
|
||||||
|
static GUIBridgeList *PushBridgeList(GUIBridgeList *bl, BuildBridgeData item)
|
||||||
|
{
|
||||||
|
if (bl == NULL) {
|
||||||
|
/* Create the list if needed */
|
||||||
|
bl = new GUIBridgeList();
|
||||||
|
bl->flags |= VL_RESORT;
|
||||||
|
bl->list_length = 1;
|
||||||
|
} else {
|
||||||
|
/* Resize the list */
|
||||||
|
bl->list_length++;
|
||||||
|
}
|
||||||
|
|
||||||
|
bl->sort_list = ReallocT(bl->sort_list, bl->list_length);
|
||||||
|
|
||||||
|
bl->sort_list[bl->list_length - 1] = item;
|
||||||
|
|
||||||
|
return bl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prepare the data for the build a bridge window.
|
||||||
|
* If we can't build a bridge under the given conditions
|
||||||
|
* show an error message.
|
||||||
|
*
|
||||||
|
* @parma start The start tile of the bridge
|
||||||
|
* @param end The end tile of the bridge
|
||||||
|
* @param transport_type The transport type
|
||||||
|
* @param bridge_type The bridge type
|
||||||
|
*/
|
||||||
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte bridge_type)
|
void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transport_type, byte bridge_type)
|
||||||
{
|
{
|
||||||
DeleteWindowById(WC_BUILD_BRIDGE, 0);
|
DeleteWindowById(WC_BUILD_BRIDGE, 0);
|
||||||
|
|
||||||
_bridgedata.type = (transport_type << 15) | (bridge_type << 8); //prepare the parameter for use only once
|
/* Data type for the bridge.
|
||||||
_bridgedata.start_tile = start;
|
* Bit 16,15 = transport type,
|
||||||
_bridgedata.end_tile = end;
|
* 14..8 = road/rail pieces,
|
||||||
|
* 7..0 = type of bridge */
|
||||||
|
uint32 type = (transport_type << 15) | (bridge_type << 8);
|
||||||
|
|
||||||
/* only query bridge building possibility once, result is the same for all bridges!
|
/* only query bridge building possibility once, result is the same for all bridges!
|
||||||
* returns CMD_ERROR on failure, and price on success */
|
* returns CMD_ERROR on failure, and price on success */
|
||||||
StringID errmsg = INVALID_STRING_ID;
|
StringID errmsg = INVALID_STRING_ID;
|
||||||
CommandCost ret = DoCommand(end, start, _bridgedata.type, DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
|
CommandCost ret = DoCommand(end, start, type, DC_AUTO | DC_QUERY_COST, CMD_BUILD_BRIDGE);
|
||||||
|
|
||||||
uint8 j = 0;
|
GUIBridgeList *bl = NULL;
|
||||||
if (CmdFailed(ret)) {
|
if (CmdFailed(ret)) {
|
||||||
errmsg = _error_message;
|
errmsg = _error_message;
|
||||||
} else {
|
} else {
|
||||||
@ -171,22 +246,19 @@ void ShowBuildBridgeWindow(TileIndex start, TileIndex end, TransportType transpo
|
|||||||
for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
|
for (BridgeType brd_type = 0; brd_type != MAX_BRIDGES; brd_type++) {
|
||||||
if (CheckBridge_Stuff(brd_type, bridge_len)) {
|
if (CheckBridge_Stuff(brd_type, bridge_len)) {
|
||||||
/* bridge is accepted, add to list */
|
/* bridge is accepted, add to list */
|
||||||
const BridgeSpec *b = GetBridgeSpec(brd_type);
|
BuildBridgeData item;
|
||||||
|
item.index = brd_type;
|
||||||
|
item.spec = GetBridgeSpec(brd_type);
|
||||||
/* Add to terraforming & bulldozing costs the cost of the
|
/* Add to terraforming & bulldozing costs the cost of the
|
||||||
* bridge itself (not computed with DC_QUERY_COST) */
|
* bridge itself (not computed with DC_QUERY_COST) */
|
||||||
_bridgedata.costs[j] = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * b->price) >> 8);
|
item.cost = ret.GetCost() + (((int64)tot_bridgedata_len * _price.build_bridge * item.spec->price) >> 8);
|
||||||
_bridgedata.indexes[j] = brd_type;
|
bl = PushBridgeList(bl, item);
|
||||||
j++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_bridgedata.count = j;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (j != 0) {
|
if (bl != NULL) {
|
||||||
Window *w = new Window(&_build_bridge_desc);
|
new BuildBridgeWindow(&_build_bridge_desc, start, end, type, bl);
|
||||||
/* Change the data, or the caption of the gui. Set it to road or rail, accordingly */
|
|
||||||
w->widget[BBSW_CAPTION].data = (transport_type == TRANSPORT_ROAD) ? STR_1803_SELECT_ROAD_BRIDGE : STR_100D_SELECT_RAIL_BRIDGE;
|
|
||||||
} else {
|
} else {
|
||||||
ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
|
ShowErrorMessage(errmsg, STR_5015_CAN_T_BUILD_BRIDGE_HERE, TileX(end) * TILE_SIZE, TileY(end) * TILE_SIZE);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user