Changeset - r17800:b328f4737f81
[Not reviewed]
master
0 1 0
yexo - 13 years ago 2011-06-17 20:32:25
yexo@openttd.org
(svn r22595) -Fix [FS#4560] (r22593): build railstation gui was broken with newgrf stations
1 file changed with 2 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/station_gui.cpp
Show inline comments
 
/* $Id$ */
 

	
 
/*
 
 * This file is part of OpenTTD.
 
 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
 
 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
/** @file station_gui.cpp The GUI for stations. */
 

	
 
#include "stdafx.h"
 
#include "debug.h"
 
#include "gui.h"
 
#include "textbuf_gui.h"
 
#include "company_func.h"
 
#include "command_func.h"
 
#include "vehicle_gui.h"
 
#include "cargotype.h"
 
#include "station_gui.h"
 
#include "strings_func.h"
 
#include "window_func.h"
 
#include "viewport_func.h"
 
#include "widgets/dropdown_func.h"
 
#include "station_base.h"
 
#include "waypoint_base.h"
 
#include "tilehighlight_func.h"
 
#include "company_base.h"
 
#include "sortlist_type.h"
 
#include "core/geometry_func.hpp"
 
#include "vehiclelist.h"
 

	
 
#include "table/strings.h"
 

	
 
/**
 
 * Draw a (multi)line of cargos seperated by commas, and prefixed with a string.
 
 * @param cargo_mask Mask of cargos to include in the list.
 
 * @param r          Rectangle to draw the cargos in.
 
 * @param prefix     String to use as prefix for the list of cargos.
 
 * @return Bottom position of the last line used for drawing the cargos.
 
 */
 
static int DrawCargoListText(uint32 cargo_mask, const Rect &r, StringID prefix)
 
{
 
	bool first = true;
 
	char string[512];
 
	char *b = string;
 

	
 
	CargoID i;
 
	FOR_EACH_SET_CARGO_ID(i, cargo_mask) {
 
		if (b >= lastof(string) - (1 + 2 * 4)) break; // ',' or ' ' and two calls to Utf8Encode()
 

	
 
		if (first) {
 
			first = false;
 
		} else {
 
			/* Add a comma if this is not the first item */
 
			*b++ = ',';
 
			*b++ = ' ';
 
		}
 
		b = InlineString(b, CargoSpec::Get(i)->name);
 
	}
 

	
 
	/* If first is still true then no cargo is accepted */
 
	if (first) b = InlineString(b, STR_JUST_NOTHING);
 

	
 
	*b = '\0';
 

	
 
	/* Make sure we detect any buffer overflow */
 
	assert(b < endof(string));
 

	
 
	SetDParamStr(0, string);
 
	return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, prefix);
 
}
 

	
 
/**
 
 * Calculates and draws the accepted or supplied cargo around the selected tile(s)
 
 * @param left x position where the string is to be drawn
 
 * @param right the right most position to draw on
 
 * @param top y position where the string is to be drawn
 
 * @param sct which type of cargo is to be displayed (passengers/non-passengers)
 
 * @param rad radius around selected tile(s) to be searched
 
 * @param supplies if supplied cargos should be drawn, else accepted cargos
 
 * @return Returns the y value below the string that was drawn
 
 */
 
int DrawStationCoverageAreaText(int left, int right, int top, StationCoverageType sct, int rad, bool supplies)
 
{
 
	TileIndex tile = TileVirtXY(_thd.pos.x, _thd.pos.y);
 
	uint32 cargo_mask = 0;
 
	if (_thd.drawstyle == HT_RECT && tile < MapSize()) {
 
		CargoArray cargos;
 
		if (supplies) {
 
			cargos = GetProductionAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
 
		} else {
 
			cargos = GetAcceptanceAroundTiles(tile, _thd.size.x / TILE_SIZE, _thd.size.y / TILE_SIZE, rad);
 
		}
 

	
 
		/* Convert cargo counts to a set of cargo bits, and draw the result. */
 
		uint32 cargo_mask = 0;
 
		for (CargoID i = 0; i < NUM_CARGO; i++) {
 
			switch (sct) {
 
				case SCT_PASSENGERS_ONLY: if (!IsCargoInClass(i, CC_PASSENGERS)) continue; break;
 
				case SCT_NON_PASSENGERS_ONLY: if (IsCargoInClass(i, CC_PASSENGERS)) continue; break;
 
				case SCT_ALL: break;
 
				default: NOT_REACHED();
 
			}
 
			if (cargos[i] >= (supplies ? 1U : 8U)) SetBit(cargo_mask, i);
 
		}
 
	}
 
		Rect r = {left, top, right, INT32_MAX};
 
		return DrawCargoListText(cargo_mask, r, supplies ? STR_STATION_BUILD_SUPPLIES_CARGO : STR_STATION_BUILD_ACCEPTS_CARGO);
 
	}
 

	
 
	return top;
 
}
 

	
 
/**
 
 * Check whether we need to redraw the station coverage text.
 
 * If it is needed actually make the window for redrawing.
 
 * @param w the window to check.
 
 */
 
void CheckRedrawStationCoverage(const Window *w)
 
{
 
	if (_thd.dirty & 1) {
 
		_thd.dirty &= ~1;
 
		w->SetDirty();
 
	}
 
}
 

	
 
/**
 
 * Draw small boxes of cargo amount and ratings data at the given
 
 * coordinates. If amount exceeds 576 units, it is shown 'full', same
 
 * goes for the rating: at above 90% orso (224) it is also 'full'
 
 *
 
 * @param left   left most coordinate to draw the box at
 
 * @param right  right most coordinate to draw the box at
 
 * @param y      coordinate to draw the box at
 
 * @param type   Cargo type
 
 * @param amount Cargo amount
 
 * @param rating ratings data for that particular cargo
 
 *
 
 * @note Each cargo-bar is 16 pixels wide and 6 pixels high
 
 * @note Each rating 14 pixels wide and 1 pixel high and is 1 pixel below the cargo-bar
 
 */
 
static void StationsWndShowStationRating(int left, int right, int y, CargoID type, uint amount, byte rating)
 
{
 
	static const uint units_full  = 576; ///< number of units to show station as 'full'
 
	static const uint rating_full = 224; ///< rating needed so it is shown as 'full'
 

	
 
	const CargoSpec *cs = CargoSpec::Get(type);
 
	if (!cs->IsValid()) return;
 

	
 
	int colour = cs->rating_colour;
 
	uint w = (minu(amount, units_full) + 5) / 36;
 

	
 
	int height = GetCharacterHeight(FS_SMALL);
 

	
 
	/* Draw total cargo (limited) on station (fits into 16 pixels) */
 
	if (w != 0) GfxFillRect(left, y, left + w - 1, y + height, colour);
 

	
 
	/* Draw a one pixel-wide bar of additional cargo meter, useful
 
	 * for stations with only a small amount (<=30) */
 
	if (w == 0) {
 
		uint rest = amount / 5;
 
		if (rest != 0) {
 
			w += left;
 
			GfxFillRect(w, y + height - rest, w, y + height, colour);
 
		}
 
	}
 

	
 
	DrawString(left + 1, right, y, cs->abbrev, TC_BLACK);
 

	
 
	/* Draw green/red ratings bar (fits into 14 pixels) */
 
	y += height + 2;
 
	GfxFillRect(left + 1, y, left + 14, y, PC_RED);
 
	rating = minu(rating, rating_full) / 16;
 
	if (rating != 0) GfxFillRect(left + 1, y, left + rating, y, PC_GREEN);
 
}
 

	
 
typedef GUIList<const Station*> GUIStationList;
 

	
 
/** Enum for CompanyStations, referring to _company_stations_widgets */
 
enum StationListWidgets {
 
	SLW_CAPTION,        ///< Window caption
 
	SLW_LIST,           ///< The main panel, list of stations
 
	SLW_SCROLLBAR,      ///< Scrollbar next to the main panel
 

	
 
	/* Vehicletypes need to be in order of StationFacility due to bit magic */
 
	SLW_TRAIN,          ///< 'TRAIN' button - list only facilities where is a railroad station
 
	SLW_TRUCK,          ///< 'TRUCK' button - list only facilities where is a truck stop
 
	SLW_BUS,            ///< 'BUS' button - list only facilities where is a bus stop
 
	SLW_AIRPLANE,       ///< 'AIRPLANE' button - list only facilities where is an airport
 
	SLW_SHIP,           ///< 'SHIP' button - list only facilities where is a dock
 
	SLW_FACILALL,       ///< 'ALL' button - list all facilities
 

	
 
	SLW_NOCARGOWAITING, ///< 'NO' button - list stations where no cargo is waiting
 
	SLW_CARGOALL,       ///< 'ALL' button - list all stations
 

	
 
	SLW_SORTBY,         ///< 'Sort by' button - reverse sort direction
 
	SLW_SORTDROPBTN,    ///< Dropdown button
 

	
 
	SLW_CARGOSTART,     ///< Widget numbers used for list of cargo types (not present in _company_stations_widgets)
 
};
 

	
 
/**
 
 * The list of stations per company.
 
 */
 
class CompanyStationsWindow : public Window
 
{
 
protected:
 
	/* Runtime saved values */
 
	static Listing last_sorting;
0 comments (0 inline, 0 general)