changeset 18778:153e9c1e0d36 draft

(svn r23626) -Add: ScriptTown::SetText, which adds custom text to the Town GUI
author truebrain <truebrain@openttd.org>
date Mon, 19 Dec 2011 21:00:55 +0000
parents 693e2f94cbd1
children 967b174c9628
files src/command.cpp src/command_type.h src/saveload/town_sl.cpp src/script/api/game/game_town.hpp.sq src/script/api/script_town.cpp src/script/api/script_town.hpp src/town.h src/town_cmd.cpp src/town_gui.cpp
diffstat 9 files changed, 61 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/command.cpp
+++ b/src/command.cpp
@@ -130,6 +130,7 @@
 CommandProc CmdDoTownAction;
 CommandProc CmdTownGrowthRate;
 CommandProc CmdTownCargoGoal;
+CommandProc CmdTownSetText;
 CommandProc CmdExpandTown;
 CommandProc CmdDeleteTown;
 
@@ -269,6 +270,7 @@
 	DEF_CMD(CmdDoTownAction,                                   0, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DO_TOWN_ACTION
 	DEF_CMD(CmdTownCargoGoal,                          CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_CARGO_GOAL
 	DEF_CMD(CmdTownGrowthRate,                         CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_GROWTH_RATE
+	DEF_CMD(CmdTownSetText,                            CMD_DEITY, CMDT_OTHER_MANAGEMENT      ), // CMD_TOWN_SET_TEXT
 	DEF_CMD(CmdExpandTown,                             CMD_DEITY, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_EXPAND_TOWN
 	DEF_CMD(CmdDeleteTown,                           CMD_OFFLINE, CMDT_LANDSCAPE_CONSTRUCTION), // CMD_DELETE_TOWN
 
--- a/src/command_type.h
+++ b/src/command_type.h
@@ -250,6 +250,7 @@
 	CMD_DO_TOWN_ACTION,               ///< do a action from the town detail window (like advertises or bribe)
 	CMD_TOWN_CARGO_GOAL,              ///< set the goal of a cargo for a town
 	CMD_TOWN_GROWTH_RATE,             ///< set the town growth rate
+	CMD_TOWN_SET_TEXT,                ///< set the custom text of a town
 	CMD_EXPAND_TOWN,                  ///< expand a town
 	CMD_DELETE_TOWN,                  ///< delete a town
 
--- a/src/saveload/town_sl.cpp
+++ b/src/saveload/town_sl.cpp
@@ -157,6 +157,8 @@
 
 	SLE_CONDARR(Town, goal, SLE_UINT32, NUM_TE, 165, SL_MAX_VERSION),
 
+	SLE_CONDSTR(Town, text,                  SLE_STR, 0, 168, SL_MAX_VERSION),
+
 	SLE_CONDVAR(Town, time_until_rebuild,    SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
 	SLE_CONDVAR(Town, grow_counter,          SLE_FILE_U8 | SLE_VAR_U16,  0, 53),
 	SLE_CONDVAR(Town, growth_rate,           SLE_FILE_U8 | SLE_VAR_I16,  0, 53),
--- a/src/script/api/game/game_town.hpp.sq
+++ b/src/script/api/game/game_town.hpp.sq
@@ -40,6 +40,7 @@
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetTownCount,                      "GetTownCount",                      1, ".");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::IsValidTown,                       "IsValidTown",                       2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetName,                           "GetName",                           2, ".i");
+	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::SetText,                           "SetText",                           3, ".i.");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetPopulation,                     "GetPopulation",                     2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetHouseCount,                     "GetHouseCount",                     2, ".i");
 	SQGSTown.DefSQStaticMethod(engine, &ScriptTown::GetLocation,                       "GetLocation",                       2, ".i");
--- a/src/script/api/script_town.cpp
+++ b/src/script/api/script_town.cpp
@@ -43,6 +43,12 @@
 	return town_name;
 }
 
+/* static */ bool ScriptTown::SetText(TownID town_id, const char *text)
+{
+	EnforcePrecondition(false, IsValidTown(town_id));
+	return ScriptObject::DoCommand(::Town::Get(town_id)->xy, town_id, 0, CMD_TOWN_SET_TEXT, text);
+}
+
 /* static */ int32 ScriptTown::GetPopulation(TownID town_id)
 {
 	if (!IsValidTown(town_id)) return -1;
--- a/src/script/api/script_town.hpp
+++ b/src/script/api/script_town.hpp
@@ -128,6 +128,16 @@
 	static char *GetName(TownID town_id);
 
 	/**
+	 * Set the custom text of a town, shown in the GUI.
+	 * @param town_id The town to set the custom text of.
+	 * @param text The text to set it to.
+	 * @pre IsValidTown(town_id).
+	 * @return True if the action succeeded.
+	 * @api -ai
+	 */
+	static bool SetText(TownID town_id, const char *text);
+
+	/**
 	 * Gets the number of inhabitants in the town.
 	 * @param town_id The town to get the population of.
 	 * @pre IsValidTown(town_id).
--- a/src/town.h
+++ b/src/town.h
@@ -78,6 +78,8 @@
 	TransportedCargoStat<uint16> received[NUM_TE];    ///< Cargo statistics about received cargotypes.
 	uint32 goal[NUM_TE];                              ///< Amount of cargo required for the town to grow.
 
+	char *text; ///< General text with additional information.
+
 	inline byte GetPercentTransported(CargoID cid) const { return this->supplied[cid].old_act * 256 / (this->supplied[cid].old_max + 1); }
 
 	/* Cargo production and acceptance stats. */
--- a/src/town_cmd.cpp
+++ b/src/town_cmd.cpp
@@ -62,6 +62,7 @@
 Town::~Town()
 {
 	free(this->name);
+	free(this->text);
 
 	if (CleaningPool()) return;
 
@@ -2474,6 +2475,30 @@
 }
 
 /**
+ * Set a custom text in the Town window.
+ * @param tile Unused.
+ * @param flags Type of operation.
+ * @param p1 Town ID to change the text of.
+ * @param p2 Unused.
+ * @param text The new text (empty to remove the text).
+ * @return Empty cost or an error.
+ */
+CommandCost CmdTownSetText(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
+{
+	if (_current_company != OWNER_DEITY) return CMD_ERROR;
+	Town *t = Town::GetIfValid(p1);
+	if (t == NULL) return CMD_ERROR;
+
+	if (flags & DC_EXEC) {
+		free(t->text);
+		t->text = StrEmpty(text) ? NULL : strdup(text);
+		InvalidateWindowData(WC_TOWN_VIEW, p1);
+	}
+
+	return CommandCost();
+}
+
+/**
  * Change the growth rate of the town.
  * @param tile Unused.
  * @param flags Type of operation.
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -403,6 +403,10 @@
 			SetDParam(1, this->town->MaxTownNoise());
 			DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, STR_TOWN_VIEW_NOISE_IN_TOWN);
 		}
+
+		if (this->town->text != NULL) {
+			DrawStringMultiLine(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_LEFT, y += FONT_HEIGHT_NORMAL, UINT16_MAX, this->town->text, TC_BLACK);
+		}
 	}
 
 	virtual void OnClick(Point pt, int widget, int click_count)
@@ -448,7 +452,7 @@
 	{
 		switch (widget) {
 			case WID_TV_INFO:
-				size->height = GetDesiredInfoHeight();
+				size->height = GetDesiredInfoHeight(size->width);
 				break;
 		}
 	}
@@ -457,7 +461,7 @@
 	 * Gets the desired height for the information panel.
 	 * @return the desired height in pixels.
 	 */
-	uint GetDesiredInfoHeight() const
+	uint GetDesiredInfoHeight(int width) const
 	{
 		uint aimed_height = 3 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_TOP + WD_FRAMERECT_BOTTOM;
 
@@ -477,13 +481,18 @@
 
 		if (_settings_game.economy.station_noise_level) aimed_height += FONT_HEIGHT_NORMAL;
 
+		if (this->town->text != NULL) {
+			SetDParamStr(0, this->town->text);
+			aimed_height += GetStringHeight(STR_JUST_RAW_STRING, width);
+		}
+
 		return aimed_height;
 	}
 
 	void ResizeWindowAsNeeded()
 	{
 		const NWidgetBase *nwid_info = this->GetWidget<NWidgetBase>(WID_TV_INFO);
-		uint aimed_height = GetDesiredInfoHeight();
+		uint aimed_height = GetDesiredInfoHeight(nwid_info->current_x);
 		if (aimed_height > nwid_info->current_y || (aimed_height < nwid_info->current_y && nwid_info->current_y > nwid_info->smallest_y)) {
 			this->ReInit();
 		}