Changeset - r18768:3747fd5dec42
[Not reviewed]
master
0 9 0
truebrain - 13 years ago 2011-12-19 21:00:55
truebrain@openttd.org
(svn r23626) -Add: ScriptTown::SetText, which adds custom text to the Town GUI
9 files changed with 61 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/command.cpp
Show inline comments
 
@@ -130,6 +130,7 @@ CommandProc CmdRenameTown;
 
CommandProc CmdDoTownAction;
 
CommandProc CmdTownGrowthRate;
 
CommandProc CmdTownCargoGoal;
 
CommandProc CmdTownSetText;
 
CommandProc CmdExpandTown;
 
CommandProc CmdDeleteTown;
 

	
 
@@ -269,6 +270,7 @@ static const Command _command_proc_table
 
	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
 

	
src/command_type.h
Show inline comments
 
@@ -250,6 +250,7 @@ enum Commands {
 
	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
 

	
src/saveload/town_sl.cpp
Show inline comments
 
@@ -157,6 +157,8 @@ static const SaveLoad _town_desc[] = {
 

	
 
	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),
src/script/api/game/game_town.hpp.sq
Show inline comments
 
@@ -40,6 +40,7 @@ void SQGSTown_Register(Squirrel *engine)
 
	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");
src/script/api/script_town.cpp
Show inline comments
 
@@ -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;
src/script/api/script_town.hpp
Show inline comments
 
@@ -128,6 +128,16 @@ public:
 
	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).
src/town.h
Show inline comments
 
@@ -78,6 +78,8 @@ struct Town : TownPool::PoolItem<&_town_
 
	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. */
src/town_cmd.cpp
Show inline comments
 
@@ -62,6 +62,7 @@ INSTANTIATE_POOL_METHODS(Town)
 
Town::~Town()
 
{
 
	free(this->name);
 
	free(this->text);
 

	
 
	if (CleaningPool()) return;
 

	
 
@@ -2474,6 +2475,30 @@ CommandCost CmdTownCargoGoal(TileIndex t
 
}
 

	
 
/**
 
 * 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.
src/town_gui.cpp
Show inline comments
 
@@ -403,6 +403,10 @@ public:
 
			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 @@ public:
 
	{
 
		switch (widget) {
 
			case WID_TV_INFO:
 
				size->height = GetDesiredInfoHeight();
 
				size->height = GetDesiredInfoHeight(size->width);
 
				break;
 
		}
 
	}
 
@@ -457,7 +461,7 @@ public:
 
	 * 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 @@ public:
 

	
 
		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();
 
		}
0 comments (0 inline, 0 general)