Skip to content

Commit

Permalink
Feature: Add station coverage area display for towns.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed Apr 22, 2019
1 parent 1013bd1 commit 364a6fa
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/script/api/game/game_window.hpp.sq
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,7 @@ void SQGSWindow_Register(Squirrel *engine)
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_CENTER_VIEW, "WID_TV_CENTER_VIEW");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_SHOW_AUTHORITY, "WID_TV_SHOW_AUTHORITY");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_CHANGE_NAME, "WID_TV_CHANGE_NAME");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_CATCHMENT, "WID_TV_CATCHMENT");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_EXPAND, "WID_TV_EXPAND");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TV_DELETE, "WID_TV_DELETE");
SQGSWindow.DefSQConst(engine, ScriptWindow::WID_TF_NEW_TOWN, "WID_TF_NEW_TOWN");
Expand Down
1 change: 1 addition & 0 deletions src/script/api/script_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2498,6 +2498,7 @@ class ScriptWindow : public ScriptObject {
WID_TV_CENTER_VIEW = ::WID_TV_CENTER_VIEW, ///< Center the main view on this town.
WID_TV_SHOW_AUTHORITY = ::WID_TV_SHOW_AUTHORITY, ///< Show the town authority window.
WID_TV_CHANGE_NAME = ::WID_TV_CHANGE_NAME, ///< Change the name of this town.
WID_TV_CATCHMENT = ::WID_TV_CATCHMENT, ///< Toggle catchment area highlight.
WID_TV_EXPAND = ::WID_TV_EXPAND, ///< Expand this town (scenario editor only).
WID_TV_DELETE = ::WID_TV_DELETE, ///< Delete this town (scenario editor only).
};
Expand Down
18 changes: 18 additions & 0 deletions src/town_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,11 +321,24 @@ struct TownViewWindow : Window {
this->SetWidgetDisabledState(WID_TV_CHANGE_NAME, _networking && !_network_server);
}

~TownViewWindow()
{
SetViewportCatchmentTown(Town::Get(this->window_number), false);
}

void SetStringParameters(int widget) const override
{
if (widget == WID_TV_CAPTION) SetDParam(0, this->town->index);
}

void OnPaint() override
{
extern const Town *_viewport_highlight_town;
this->SetWidgetLoweredState(WID_TV_CATCHMENT, _viewport_highlight_town == this->town);

this->DrawWidgets();
}

void DrawWidget(const Rect &r, int widget) const override
{
if (widget != WID_TV_INFO) return;
Expand Down Expand Up @@ -432,6 +445,10 @@ struct TownViewWindow : Window {
ShowQueryString(STR_TOWN_NAME, STR_TOWN_VIEW_RENAME_TOWN_BUTTON, MAX_LENGTH_TOWN_NAME_CHARS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT | QSF_LEN_IN_CHARS);
break;

case WID_TV_CATCHMENT:
SetViewportCatchmentTown(Town::Get(this->window_number), !this->IsWidgetLowered(WID_TV_CATCHMENT));
break;

case WID_TV_EXPAND: { // expand town - only available on Scenario editor
/* Warn the user if towns are not allowed to build roads, but do this only once per OpenTTD run. */
static bool _warn_town_no_roads = false;
Expand Down Expand Up @@ -552,6 +569,7 @@ static const NWidgetPart _nested_town_game_view_widgets[] = {
NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_SHOW_AUTHORITY), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON, STR_TOWN_VIEW_LOCAL_AUTHORITY_TOOLTIP),
NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_TV_CHANGE_NAME), SetMinimalSize(80, 12), SetFill(1, 1), SetResize(1, 0), SetDataTip(STR_BUTTON_RENAME, STR_TOWN_VIEW_RENAME_TOOLTIP),
EndContainer(),
NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_TV_CATCHMENT), SetMinimalSize(14, 12), SetFill(0, 1), SetDataTip(STR_BUTTON_CATCHMENT, STR_TOOLTIP_CATCHMENT),
NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
EndContainer(),
};
Expand Down
46 changes: 46 additions & 0 deletions src/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -983,9 +983,11 @@ enum TileHighlightType {
THT_NONE,
THT_WHITE,
THT_BLUE,
THT_RED,
};

const Station *_viewport_highlight_station; ///< Currently selected station for coverage area highlight
const Town *_viewport_highlight_town; ///< Currently selected town for coverage area highlight

/**
* Get tile highlight type of coverage area for a given tile.
Expand All @@ -1000,6 +1002,25 @@ static TileHighlightType GetTileHighlightType(TileIndex t)
return THT_NONE;
}

if (_viewport_highlight_town != nullptr) {
if (IsTileType(t, MP_HOUSE)) {
if (GetTownIndex(t) == _viewport_highlight_town->index) {
TileHighlightType type = THT_RED;
for (const Station *st : _viewport_highlight_town->stations_near) {
if (st->owner != _current_company) continue;
if (st->TileIsInCatchment(t)) return THT_BLUE;
}
return type;
}
} else if (IsTileType(t, MP_STATION)) {
for (const Station *st : _viewport_highlight_town->stations_near) {
if (st->owner != _current_company) continue;
if (GetStationIndex(t) == st->index) return THT_WHITE;
}
}
return THT_NONE;
}

return THT_NONE;
}

Expand All @@ -1015,6 +1036,7 @@ static void DrawTileHighlightType(const TileInfo *ti, TileHighlightType tht)
case THT_NONE: break;
case THT_WHITE: DrawTileSelectionRect(ti, PAL_NONE); break;
case THT_BLUE: DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE); break;
case THT_RED: DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING); break;
}
}

Expand Down Expand Up @@ -3404,19 +3426,43 @@ static void MarkCatchmentTilesDirty()

/**
* Select or deselect station for coverage area highlight.
* Selecting a station will deselect a town.
* @param *st Station in question
* @param sel Select or deselect given station
*/
void SetViewportCatchmentStation(const Station *st, bool sel)
{
if (_viewport_highlight_station != nullptr) SetWindowDirty(WC_STATION_VIEW, _viewport_highlight_station->index);
if (_viewport_highlight_town != nullptr) SetWindowDirty(WC_TOWN_VIEW, _viewport_highlight_town->index);
if (sel && _viewport_highlight_station != st) {
MarkCatchmentTilesDirty();
_viewport_highlight_station = st;
_viewport_highlight_town = nullptr;
MarkCatchmentTilesDirty();
} else if (!sel && _viewport_highlight_station == st) {
MarkCatchmentTilesDirty();
_viewport_highlight_station = nullptr;
}
if (_viewport_highlight_station != nullptr) SetWindowDirty(WC_STATION_VIEW, _viewport_highlight_station->index);
}

/**
* Select or deselect town for coverage area highlight.
* Selecting a town will deselect a station.
* @param *t Town in question
* @param sel Select or deselect given town
*/
void SetViewportCatchmentTown(const Town *t, bool sel)
{
if (_viewport_highlight_town != nullptr) SetWindowDirty(WC_TOWN_VIEW, _viewport_highlight_town->index);
if (_viewport_highlight_station != nullptr) SetWindowDirty(WC_STATION_VIEW, _viewport_highlight_station->index);
if (sel && _viewport_highlight_town != t) {
_viewport_highlight_station = nullptr;
_viewport_highlight_town = t;
MarkWholeScreenDirty();
} else if (!sel && _viewport_highlight_town == t) {
_viewport_highlight_town = nullptr;
MarkWholeScreenDirty();
}
if (_viewport_highlight_town != nullptr) SetWindowDirty(WC_TOWN_VIEW, _viewport_highlight_town->index);
}
2 changes: 2 additions & 0 deletions src/viewport_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ static inline void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset =
Point GetViewportStationMiddle(const ViewPort *vp, const Station *st);

struct Station;
struct Town;

void SetViewportCatchmentStation(const Station *st, bool sel);
void SetViewportCatchmentTown(const Town *t, bool sel);

#endif /* VIEWPORT_FUNC_H */
1 change: 1 addition & 0 deletions src/widgets/town_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum TownViewWidgets {
WID_TV_CENTER_VIEW, ///< Center the main view on this town.
WID_TV_SHOW_AUTHORITY, ///< Show the town authority window.
WID_TV_CHANGE_NAME, ///< Change the name of this town.
WID_TV_CATCHMENT, ///< Toggle catchment area highlight.
WID_TV_EXPAND, ///< Expand this town (scenario editor only).
WID_TV_DELETE, ///< Delete this town (scenario editor only).
};
Expand Down

0 comments on commit 364a6fa

Please sign in to comment.