summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-05-12 20:50:10 +0000
committerrubidium <rubidium@openttd.org>2010-05-12 20:50:10 +0000
commit1a5d7b34d8788340f348dc51a3809155b50b2595 (patch)
tree085f25035601ee9209149d209595d593e5baee51 /src
parente91165ef6e907c7868ecf994c91b16bc9960fbf9 (diff)
downloadopenttd-1a5d7b34d8788340f348dc51a3809155b50b2595.tar.xz
(svn r19801) -Add [FS#3691]: custom naming of depots. Based on work by sbr
Diffstat (limited to 'src')
-rw-r--r--src/command.cpp2
-rw-r--r--src/command_type.h1
-rw-r--r--src/depot_base.h2
-rw-r--r--src/depot_cmd.cpp83
-rw-r--r--src/depot_gui.cpp22
-rw-r--r--src/depot_type.h3
-rw-r--r--src/lang/english.txt6
-rw-r--r--src/strings.cpp7
8 files changed, 124 insertions, 2 deletions
diff --git a/src/command.cpp b/src/command.cpp
index f2baa2ff6..19fd1a69c 100644
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -110,6 +110,7 @@ CommandProc CmdRenameCompany;
CommandProc CmdRenamePresident;
CommandProc CmdRenameStation;
+CommandProc CmdRenameDepot;
CommandProc CmdSellAircraft;
CommandProc CmdBuildAircraft;
@@ -260,6 +261,7 @@ static const Command _command_proc_table[] = {
DEF_CMD(CmdRenamePresident, 0), // CMD_RENAME_PRESIDENT
DEF_CMD(CmdRenameStation, 0), // CMD_RENAME_STATION
+ DEF_CMD(CmdRenameDepot, 0), // CMD_RENAME_DEPOT
DEF_CMD(CmdSellAircraft, 0), // CMD_SELL_AIRCRAFT
diff --git a/src/command_type.h b/src/command_type.h
index 28d1855a4..afc1d3f55 100644
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -210,6 +210,7 @@ enum {
CMD_RENAME_COMPANY, ///< change the company name
CMD_RENAME_PRESIDENT, ///< change the president name
CMD_RENAME_STATION, ///< rename a station
+ CMD_RENAME_DEPOT, ///< rename a depot
CMD_SELL_AIRCRAFT, ///< sell an aircraft
CMD_BUILD_AIRCRAFT, ///< build an aircraft
diff --git a/src/depot_base.h b/src/depot_base.h
index 8a7305088..0b05d5418 100644
--- a/src/depot_base.h
+++ b/src/depot_base.h
@@ -20,7 +20,7 @@ extern DepotPool _depot_pool;
struct Depot : DepotPool::PoolItem<&_depot_pool> {
Town *town;
- const char *name;
+ char *name;
TileIndex xy;
uint16 town_cn; ///< The Nth depot for this town (consecutive number)
diff --git a/src/depot_cmd.cpp b/src/depot_cmd.cpp
new file mode 100644
index 000000000..bf4d0201c
--- /dev/null
+++ b/src/depot_cmd.cpp
@@ -0,0 +1,83 @@
+/* $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 depot_cmd.cpp Command Handling for depots. */
+
+#include "stdafx.h"
+#include "command_func.h"
+#include "depot_base.h"
+#include "functions.h"
+#include "string_func.h"
+#include "town.h"
+#include "vehicle_gui.h"
+#include "window_func.h"
+
+#include "table/strings.h"
+
+
+static bool IsUniqueDepotName(const char *name)
+{
+ const Depot *d;
+
+ FOR_ALL_DEPOTS(d) {
+ if (d->name != NULL && strcmp(d->name, name) == 0) return false;
+ }
+
+ return true;
+}
+
+/**
+ * Rename a depot.
+ * @param tile unused
+ * @param flags type of operation
+ * @param p1 id of depot
+ * @param p2 unused
+ * @param text the new name or an empty string when resetting to the default
+ * @return the cost of this operation or an error
+ */
+CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+ Depot *d = Depot::GetIfValid(p1);
+ if (d == NULL) return CMD_ERROR;
+
+ CommandCost ret = CheckTileOwnership(d->xy);
+ if (ret.Failed()) return ret;
+
+ bool reset = StrEmpty(text);
+
+ if (!reset) {
+ if (strlen(text) >= MAX_LENGTH_DEPOT_NAME_BYTES) return CMD_ERROR;
+ if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE);
+ }
+
+ if (flags & DC_EXEC) {
+ free(d->name);
+
+ if (reset) {
+ d->name = NULL;
+ MakeDefaultName(d);
+ } else {
+ d->name = strdup(text);
+ }
+
+ /* Update the orders and depot */
+ SetWindowClassesDirty(WC_VEHICLE_ORDERS);
+ SetWindowDirty(WC_VEHICLE_DEPOT, d->xy);
+
+ /* Update the depot list */
+ WindowNumber wno = (d->index << 16) | VLW_DEPOT_LIST | GetTileOwner(d->xy);
+ switch (GetTileType(d->xy)) {
+ default: break;
+ case MP_RAILWAY: SetWindowDirty(WC_TRAINS_LIST, wno | (VEH_TRAIN << 11)); break;
+ case MP_ROAD: SetWindowDirty(WC_ROADVEH_LIST, wno | (VEH_ROAD << 11)); break;
+ case MP_WATER: SetWindowDirty(WC_SHIPS_LIST, wno | (VEH_SHIP << 11)); break;
+ }
+ }
+ return CommandCost();
+}
diff --git a/src/depot_gui.cpp b/src/depot_gui.cpp
index b6e65982c..33dfaa357 100644
--- a/src/depot_gui.cpp
+++ b/src/depot_gui.cpp
@@ -53,6 +53,8 @@ enum DepotWindowWidgets {
DEPOT_WIDGET_BUILD,
DEPOT_WIDGET_CLONE,
DEPOT_WIDGET_LOCATION,
+ DEPOT_WIDGET_SHOW_RENAME,
+ DEPOT_WIDGET_RENAME,
DEPOT_WIDGET_VEHICLE_LIST,
DEPOT_WIDGET_STOP_ALL,
DEPOT_WIDGET_START_ALL,
@@ -83,6 +85,9 @@ static const NWidgetPart _nested_train_depot_widgets[] = {
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_BUILD), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_TEXTBTN, COLOUR_GREY, DEPOT_WIDGET_CLONE), SetDataTip(0x0, STR_NULL), SetFill(1, 1), SetResize(1, 0),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_LOCATION), SetDataTip(STR_BUTTON_LOCATION, STR_NULL), SetFill(1, 1), SetResize(1, 0),
+ NWidget(NWID_SELECTION, INVALID_COLOUR, DEPOT_WIDGET_SHOW_RENAME), // rename button
+ NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_RENAME), SetDataTip(STR_BUTTON_RENAME, STR_DEPOT_RENAME_TOOLTIP), SetFill(1, 1), SetResize(1, 0),
+ EndContainer(),
NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, DEPOT_WIDGET_VEHICLE_LIST), SetDataTip(0x0, STR_NULL), SetFill(0, 1),
NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_STOP_ALL), SetDataTip(SPR_FLAG_VEH_STOPPED, STR_NULL), SetFill(0, 1),
NWidget(WWT_PUSHIMGBTN, COLOUR_GREY, DEPOT_WIDGET_START_ALL), SetDataTip(SPR_FLAG_VEH_RUNNING, STR_NULL), SetFill(0, 1),
@@ -222,6 +227,8 @@ struct DepotWindow : Window {
this->type = type;
this->CreateNestedTree(desc);
+ /* Don't show 'rename button' of aircraft hangar */
+ this->GetWidget<NWidgetStacked>(DEPOT_WIDGET_SHOW_RENAME)->SetDisplayedPlane(type == VEH_AIRCRAFT ? SZSP_NONE : 0);
this->SetupWidgetData(type);
this->FinishInitNested(desc, tile);
@@ -689,6 +696,7 @@ struct DepotWindow : Window {
DEPOT_WIDGET_SELL_ALL,
DEPOT_WIDGET_BUILD,
DEPOT_WIDGET_CLONE,
+ DEPOT_WIDGET_RENAME,
DEPOT_WIDGET_AUTOREPLACE,
WIDGET_LIST_END);
@@ -734,6 +742,12 @@ struct DepotWindow : Window {
}
break;
+ case DEPOT_WIDGET_RENAME: // Rename button
+ SetDParam(0, this->type);
+ SetDParam(1, Depot::GetByTile((TileIndex)this->window_number)->index);
+ ShowQueryString(STR_DEPOT_NAME, STR_DEPOT_RENAME_DEPOT_CAPTION, MAX_LENGTH_DEPOT_NAME_BYTES, MAX_LENGTH_DEPOT_NAME_PIXELS, this, CS_ALPHANUMERAL, QSF_ENABLE_DEFAULT);
+ break;
+
case DEPOT_WIDGET_STOP_ALL:
case DEPOT_WIDGET_START_ALL:
DoCommandP(this->window_number, 0, this->type | (widget == DEPOT_WIDGET_START_ALL ? (1 << 5) : 0), CMD_MASS_START_STOP);
@@ -767,6 +781,14 @@ struct DepotWindow : Window {
}
}
+ virtual void OnQueryTextFinished(char *str)
+ {
+ if (str == NULL) return;
+
+ /* Do depot renaming */
+ DoCommandP(0, GetDepotIndex(this->window_number), 0, CMD_RENAME_DEPOT | CMD_MSG(STR_ERROR_CAN_T_RENAME_DEPOT), NULL, str);
+ }
+
virtual void OnRightClick(Point pt, int widget)
{
if (widget != DEPOT_WIDGET_MATRIX) return;
diff --git a/src/depot_type.h b/src/depot_type.h
index 2d55f5978..ab6013e13 100644
--- a/src/depot_type.h
+++ b/src/depot_type.h
@@ -15,4 +15,7 @@
typedef uint16 DepotID;
struct Depot;
+static const uint MAX_LENGTH_DEPOT_NAME_BYTES = 31; ///< The maximum length of a depot name in bytes including '\0'
+static const uint MAX_LENGTH_DEPOT_NAME_PIXELS = 180; ///< The maximum length of a depot name in pixels
+
#endif /* DEPOT_TYPE_H */
diff --git a/src/lang/english.txt b/src/lang/english.txt
index c6e54c5d8..8941292c2 100644
--- a/src/lang/english.txt
+++ b/src/lang/english.txt
@@ -2783,6 +2783,9 @@ STR_QUERY_RENAME_AIRCRAFT_TYPE_CAPTION :{WHITE}Rename a
# Depot window
STR_DEPOT_CAPTION :{WHITE}{DEPOT}
+STR_DEPOT_RENAME_TOOLTIP :{BLACK}Change name of depot
+STR_DEPOT_RENAME_DEPOT_CAPTION :Rename depot
+
STR_DEPOT_NO_ENGINE :{BLACK}-
STR_DEPOT_VEHICLE_TOOLTIP :{BLACK}{ENGINE}{RAW_STRING}
STR_DEPOT_VEHICLE_TOOLTIP_CHAIN :{BLACK}{NUM} vehicle{P "" s}{RAW_STRING}
@@ -3489,6 +3492,8 @@ STR_ERROR_CAN_T_BUILD_ROAD_DEPOT :{WHITE}Can't bu
STR_ERROR_CAN_T_BUILD_TRAM_DEPOT :{WHITE}Can't build tram vehicle depot here...
STR_ERROR_CAN_T_BUILD_SHIP_DEPOT :{WHITE}Can't build ship depot here...
+STR_ERROR_CAN_T_RENAME_DEPOT :{WHITE}Can't rename depot...
+
STR_TRAIN_MUST_BE_STOPPED :{WHITE}Train must be stopped inside a depot
STR_ERROR_ROAD_VEHICLE_MUST_BE_STOPPED_INSIDE_DEPOT :{WHITE}... must be stopped inside a road vehicle depot
STR_ERROR_SHIP_MUST_BE_STOPPED_IN_DEPOT :{WHITE}Ship must be stopped in depot
@@ -4092,6 +4097,7 @@ STR_VIEWPORT_WAYPOINT_TINY :{TINYFONT}{WAYP
# Simple strings to get specific types of data
STR_COMPANY_NAME :{COMPANY}
STR_COMPANY_NAME_COMPANY_NUM :{COMPANY} {COMPANYNUM}
+STR_DEPOT_NAME :{DEPOT}
STR_ENGINE_NAME :{ENGINE}
STR_GROUP_NAME :{GROUP}
STR_INDUSTRY_NAME :{INDUSTRY}
diff --git a/src/strings.cpp b/src/strings.cpp
index 6e1130227..56ed57f71 100644
--- a/src/strings.cpp
+++ b/src/strings.cpp
@@ -950,8 +950,13 @@ static char *FormatString(char *buff, const char *str, int64 *argv, uint casei,
if (vt == VEH_AIRCRAFT) {
int64 temp[] = { GetInt32(&argv) };
buff = GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_AIRCRAFT + vt, temp, last);
+ break;
+ }
+
+ const Depot *d = Depot::Get(GetInt32(&argv));
+ if (d->name != NULL) {
+ buff = strecpy(buff, d->name, last);
} else {
- const Depot *d = Depot::Get(GetInt32(&argv));
int64 temp[] = { d->town->index, d->town_cn + 1 };
buff = GetStringWithArgs(buff, STR_FORMAT_DEPOT_NAME_TRAIN + 2 * vt + (d->town_cn == 0 ? 0 : 1), temp, last);
}