Changeset - r24823:6a09ecd63560
[Not reviewed]
master
0 1 0
Jonathan G Rennison - 3 years ago 2021-02-18 11:17:51
j.g.rennison@gmail.com
Fix: Unnecessary status bar redraws when there is no news to show (#8691)

InvalidateWindowData with mode SBI_NEWS_DELETED was called on the
status bar when checking for a new item of news to be shown in the
ticker, even if there is no news queued and no change occurs.
1 file changed with 1 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/news_gui.cpp
Show inline comments
 
@@ -477,484 +477,483 @@ struct NewsWindow : Window {
 

	
 
	void OnClick(Point pt, int widget, int click_count) override
 
	{
 
		switch (widget) {
 
			case WID_N_CLOSEBOX:
 
				NewsWindow::duration = 0;
 
				delete this;
 
				_forced_news = nullptr;
 
				break;
 

	
 
			case WID_N_CAPTION:
 
				if (this->ni->reftype1 == NR_VEHICLE) {
 
					const Vehicle *v = Vehicle::Get(this->ni->ref1);
 
					ShowVehicleViewWindow(v);
 
				}
 
				break;
 

	
 
			case WID_N_VIEWPORT:
 
				break; // Ignore clicks
 

	
 
			case WID_N_SHOW_GROUP:
 
				if (this->ni->reftype1 == NR_VEHICLE) {
 
					const Vehicle *v = Vehicle::Get(this->ni->ref1);
 
					ShowCompanyGroupForVehicle(v);
 
				}
 
				break;
 
			default:
 
				if (this->ni->reftype1 == NR_VEHICLE) {
 
					const Vehicle *v = Vehicle::Get(this->ni->ref1);
 
					ScrollMainWindowTo(v->x_pos, v->y_pos, v->z_pos);
 
				} else {
 
					TileIndex tile1 = GetReferenceTile(this->ni->reftype1, this->ni->ref1);
 
					TileIndex tile2 = GetReferenceTile(this->ni->reftype2, this->ni->ref2);
 
					if (_ctrl_pressed) {
 
						if (tile1 != INVALID_TILE) ShowExtraViewportWindow(tile1);
 
						if (tile2 != INVALID_TILE) ShowExtraViewportWindow(tile2);
 
					} else {
 
						if ((tile1 == INVALID_TILE || !ScrollMainWindowToTile(tile1)) && tile2 != INVALID_TILE) {
 
							ScrollMainWindowToTile(tile2);
 
						}
 
					}
 
				}
 
				break;
 
		}
 
	}
 

	
 
	/**
 
	 * Some data on this window has become invalid.
 
	 * @param data Information about the changed data.
 
	 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
 
	 */
 
	void OnInvalidateData(int data = 0, bool gui_scope = true) override
 
	{
 
		if (!gui_scope) return;
 
		/* The chatbar has notified us that is was either created or closed */
 
		int newtop = this->top + this->chat_height - data;
 
		this->chat_height = data;
 
		this->SetWindowTop(newtop);
 
	}
 

	
 
	void OnRealtimeTick(uint delta_ms) override
 
	{
 
		int count = this->timer.CountElapsed(delta_ms);
 
		if (count > 0) {
 
			/* Scroll up newsmessages from the bottom */
 
			int newtop = std::max(this->top - 2 * count, _screen.height - this->height - this->status_height - this->chat_height);
 
			this->SetWindowTop(newtop);
 
		}
 

	
 
		/* Decrement the news timer. We don't need to action an elapsed event here,
 
		 * so no need to use TimerElapsed(). */
 
		if (NewsWindow::duration > 0) NewsWindow::duration -= delta_ms;
 
	}
 

	
 
private:
 
	/**
 
	 * Moves the window to a new #top coordinate. Makes screen dirty where needed.
 
	 * @param newtop new top coordinate
 
	 */
 
	void SetWindowTop(int newtop)
 
	{
 
		if (this->top == newtop) return;
 

	
 
		int mintop = std::min(newtop, this->top);
 
		int maxtop = std::max(newtop, this->top);
 
		if (this->viewport != nullptr) this->viewport->top += newtop - this->top;
 
		this->top = newtop;
 

	
 
		AddDirtyBlock(this->left, mintop, this->left + this->width, maxtop + this->height);
 
	}
 

	
 
	StringID GetCompanyMessageString() const
 
	{
 
		/* Company news with a face have a separate headline, so the normal message is shifted by two params */
 
		CopyInDParam(0, this->ni->params + 2, lengthof(this->ni->params) - 2);
 
		return this->ni->params[1];
 
	}
 

	
 
	StringID GetNewVehicleMessageString(int widget) const
 
	{
 
		assert(this->ni->reftype1 == NR_ENGINE);
 
		EngineID engine = this->ni->ref1;
 

	
 
		switch (widget) {
 
			case WID_N_VEH_TITLE:
 
				SetDParam(0, GetEngineCategoryName(engine));
 
				return STR_NEWS_NEW_VEHICLE_NOW_AVAILABLE;
 

	
 
			case WID_N_VEH_NAME:
 
				SetDParam(0, engine);
 
				return STR_NEWS_NEW_VEHICLE_TYPE;
 

	
 
			default:
 
				NOT_REACHED();
 
		}
 
	}
 
};
 

	
 
/* static */ int NewsWindow::duration = 0; // Instance creation.
 

	
 
/** Open up an own newspaper window for the news item */
 
static void ShowNewspaper(const NewsItem *ni)
 
{
 
	SoundFx sound = _news_type_data[ni->type].sound;
 
	if (sound != 0 && _settings_client.sound.news_full) SndPlayFx(sound);
 

	
 
	new NewsWindow(GetNewsWindowLayout(ni->flags), ni);
 
}
 

	
 
/** Show news item in the ticker */
 
static void ShowTicker(const NewsItem *ni)
 
{
 
	if (_settings_client.sound.news_ticker) SndPlayFx(SND_16_MORSE);
 

	
 
	_statusbar_news_item = ni;
 
	InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_TICKER);
 
}
 

	
 
/** Initialize the news-items data structures */
 
void InitNewsItemStructs()
 
{
 
	for (NewsItem *ni = _oldest_news; ni != nullptr; ) {
 
		NewsItem *next = ni->next;
 
		delete ni;
 
		ni = next;
 
	}
 

	
 
	_total_news = 0;
 
	_oldest_news = nullptr;
 
	_latest_news = nullptr;
 
	_forced_news = nullptr;
 
	_current_news = nullptr;
 
	_statusbar_news_item = nullptr;
 
	NewsWindow::duration = 0;
 
}
 

	
 
/**
 
 * Are we ready to show another ticker item?
 
 * Only if nothing is in the newsticker is displayed
 
 */
 
static bool ReadyForNextTickerItem()
 
{
 
	const NewsItem *ni = _statusbar_news_item;
 
	if (ni == nullptr) return true;
 

	
 
	/* Ticker message
 
	 * Check if the status bar message is still being displayed? */
 
	if (IsNewsTickerShown()) return false;
 
	return true;
 
}
 

	
 
/**
 
 * Are we ready to show another news item?
 
 * Only if no newspaper is displayed
 
 */
 
static bool ReadyForNextNewsItem()
 
{
 
	const NewsItem *ni = _forced_news == nullptr ? _current_news : _forced_news;
 
	if (ni == nullptr) return true;
 

	
 
	/* neither newsticker nor newspaper are running */
 
	return (NewsWindow::duration <= 0 || FindWindowById(WC_NEWS_WINDOW, 0) == nullptr);
 
}
 

	
 
/** Move to the next ticker item */
 
static void MoveToNextTickerItem()
 
{
 
	/* There is no status bar, so no reason to show news;
 
	 * especially important with the end game screen when
 
	 * there is no status bar but possible news. */
 
	if (FindWindowById(WC_STATUS_BAR, 0) == nullptr) return;
 

	
 
	InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
 

	
 
	/* if we're not at the last item, then move on */
 
	while (_statusbar_news_item != _latest_news) {
 
		_statusbar_news_item = (_statusbar_news_item == nullptr) ? _oldest_news : _statusbar_news_item->next;
 
		const NewsItem *ni = _statusbar_news_item;
 
		const NewsType type = ni->type;
 

	
 
		/* check the date, don't show too old items */
 
		if (_date - _news_type_data[type].age > ni->date) continue;
 

	
 
		switch (_news_type_data[type].GetDisplay()) {
 
			default: NOT_REACHED();
 
			case ND_OFF: // Off - show nothing only a small reminder in the status bar
 
				InvalidateWindowData(WC_STATUS_BAR, 0, SBI_SHOW_REMINDER);
 
				break;
 

	
 
			case ND_SUMMARY: // Summary - show ticker
 
				ShowTicker(ni);
 
				break;
 

	
 
			case ND_FULL: // Full - show newspaper, skipped here
 
				continue;
 
		}
 
		return;
 
	}
 
}
 

	
 
/** Move to the next news item */
 
static void MoveToNextNewsItem()
 
{
 
	/* There is no status bar, so no reason to show news;
 
	 * especially important with the end game screen when
 
	 * there is no status bar but possible news. */
 
	if (FindWindowById(WC_STATUS_BAR, 0) == nullptr) return;
 

	
 
	DeleteWindowById(WC_NEWS_WINDOW, 0); // close the newspapers window if shown
 
	_forced_news = nullptr;
 

	
 
	/* if we're not at the last item, then move on */
 
	while (_current_news != _latest_news) {
 
		_current_news = (_current_news == nullptr) ? _oldest_news : _current_news->next;
 
		const NewsItem *ni = _current_news;
 
		const NewsType type = ni->type;
 

	
 
		/* check the date, don't show too old items */
 
		if (_date - _news_type_data[type].age > ni->date) continue;
 

	
 
		switch (_news_type_data[type].GetDisplay()) {
 
			default: NOT_REACHED();
 
			case ND_OFF: // Off - show nothing only a small reminder in the status bar, skipped here
 
				continue;
 

	
 
			case ND_SUMMARY: // Summary - show ticker, skipped here
 
				continue;
 

	
 
			case ND_FULL: // Full - show newspaper
 
				ShowNewspaper(ni);
 
				break;
 
		}
 
		return;
 
	}
 
}
 

	
 
/** Delete a news item from the queue */
 
static void DeleteNewsItem(NewsItem *ni)
 
{
 
	/* Delete the news from the news queue. */
 
	if (ni->prev != nullptr) {
 
		ni->prev->next = ni->next;
 
	} else {
 
		assert(_oldest_news == ni);
 
		_oldest_news = ni->next;
 
	}
 

	
 
	if (ni->next != nullptr) {
 
		ni->next->prev = ni->prev;
 
	} else {
 
		assert(_latest_news == ni);
 
		_latest_news = ni->prev;
 
	}
 

	
 
	_total_news--;
 

	
 
	if (_forced_news == ni || _current_news == ni) {
 
		/* When we're the current news, go to the previous item first;
 
		 * we just possibly made that the last news item. */
 
		if (_current_news == ni) _current_news = ni->prev;
 

	
 
		/* About to remove the currently forced item (shown as newspapers) ||
 
		 * about to remove the currently displayed item (newspapers) */
 
		MoveToNextNewsItem();
 
	}
 

	
 
	if (_statusbar_news_item == ni) {
 
		/* When we're the current news, go to the previous item first;
 
		 * we just possibly made that the last news item. */
 
		_statusbar_news_item = ni->prev;
 

	
 
		/* About to remove the currently displayed item (ticker, or just a reminder) */
 
		InvalidateWindowData(WC_STATUS_BAR, 0, SBI_NEWS_DELETED); // invalidate the statusbar
 
		MoveToNextTickerItem();
 
	}
 

	
 
	delete ni;
 

	
 
	SetWindowDirty(WC_MESSAGE_HISTORY, 0);
 
}
 

	
 
/**
 
 * Add a new newsitem to be shown.
 
 * @param string String to display
 
 * @param type news category
 
 * @param flags display flags for the news
 
 * @param reftype1 Type of ref1
 
 * @param ref1     Reference 1 to some object: Used for a possible viewport, scrolling after clicking on the news, and for deleting the news when the object is deleted.
 
 * @param reftype2 Type of ref2
 
 * @param ref2     Reference 2 to some object: Used for scrolling after clicking on the news, and for deleting the news when the object is deleted.
 
 * @param free_data Pointer to data that must be freed once the news message is cleared
 
 *
 
 * @see NewsSubtype
 
 */
 
void AddNewsItem(StringID string, NewsType type, NewsFlag flags, NewsReferenceType reftype1, uint32 ref1, NewsReferenceType reftype2, uint32 ref2, void *free_data)
 
{
 
	if (_game_mode == GM_MENU) return;
 

	
 
	/* Create new news item node */
 
	NewsItem *ni = new NewsItem;
 

	
 
	ni->string_id = string;
 
	ni->type = type;
 
	ni->flags = flags;
 

	
 
	/* show this news message in colour? */
 
	if (_cur_year >= _settings_client.gui.coloured_news_year) ni->flags |= NF_INCOLOUR;
 

	
 
	ni->reftype1 = reftype1;
 
	ni->reftype2 = reftype2;
 
	ni->ref1 = ref1;
 
	ni->ref2 = ref2;
 
	ni->free_data = free_data;
 
	ni->date = _date;
 
	CopyOutDParam(ni->params, 0, lengthof(ni->params));
 

	
 
	if (_total_news++ == 0) {
 
		assert(_oldest_news == nullptr);
 
		_oldest_news = ni;
 
		ni->prev = nullptr;
 
	} else {
 
		assert(_latest_news->next == nullptr);
 
		_latest_news->next = ni;
 
		ni->prev = _latest_news;
 
	}
 

	
 
	ni->next = nullptr;
 
	_latest_news = ni;
 

	
 
	/* Keep the number of stored news items to a managable number */
 
	if (_total_news > MAX_NEWS_AMOUNT) {
 
		DeleteNewsItem(_oldest_news);
 
	}
 

	
 
	SetWindowDirty(WC_MESSAGE_HISTORY, 0);
 
}
 

	
 
/**
 
 * Create a new custom news item.
 
 * @param tile unused
 
 * @param flags type of operation
 
 * @param p1 various bitstuffed elements
 
 * - p1 = (bit  0 -  7) - NewsType of the message.
 
 * - p1 = (bit  8 - 15) - NewsReferenceType of first reference.
 
 * - p1 = (bit 16 - 23) - Company this news message is for.
 
 * @param p2 First reference of the news message.
 
 * @param text The text of the news message.
 
 * @return the cost of this operation or an error
 
 */
 
CommandCost CmdCustomNewsItem(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	if (_current_company != OWNER_DEITY) return CMD_ERROR;
 

	
 
	NewsType type = (NewsType)GB(p1, 0, 8);
 
	NewsReferenceType reftype1 = (NewsReferenceType)GB(p1, 8, 8);
 
	CompanyID company = (CompanyID)GB(p1, 16, 8);
 

	
 
	if (company != INVALID_OWNER && !Company::IsValidID(company)) return CMD_ERROR;
 
	if (type >= NT_END) return CMD_ERROR;
 
	if (StrEmpty(text)) return CMD_ERROR;
 

	
 
	switch (reftype1) {
 
		case NR_NONE: break;
 
		case NR_TILE:
 
			if (!IsValidTile(p2)) return CMD_ERROR;
 
			break;
 

	
 
		case NR_VEHICLE:
 
			if (!Vehicle::IsValidID(p2)) return CMD_ERROR;
 
			break;
 

	
 
		case NR_STATION:
 
			if (!Station::IsValidID(p2)) return CMD_ERROR;
 
			break;
 

	
 
		case NR_INDUSTRY:
 
			if (!Industry::IsValidID(p2)) return CMD_ERROR;
 
			break;
 

	
 
		case NR_TOWN:
 
			if (!Town::IsValidID(p2)) return CMD_ERROR;
 
			break;
 

	
 
		case NR_ENGINE:
 
			if (!Engine::IsValidID(p2)) return CMD_ERROR;
 
			break;
 

	
 
		default: return CMD_ERROR;
 
	}
 

	
 
	if (company != INVALID_OWNER && company != _local_company) return CommandCost();
 

	
 
	if (flags & DC_EXEC) {
 
		char *news = stredup(text);
 
		SetDParamStr(0, news);
 
		AddNewsItem(STR_NEWS_CUSTOM_ITEM, type, NF_NORMAL, reftype1, p2, NR_NONE, UINT32_MAX, news);
 
	}
 

	
 
	return CommandCost();
 
}
 

	
 
/**
 
 * Delete a news item type about a vehicle.
 
 * When the news item type is INVALID_STRING_ID all news about the vehicle gets deleted.
 
 * @param vid  The vehicle to remove the news for.
 
 * @param news The news type to remove.
 
 */
 
void DeleteVehicleNews(VehicleID vid, StringID news)
 
{
 
	NewsItem *ni = _oldest_news;
 

	
 
	while (ni != nullptr) {
 
		NewsItem *next = ni->next;
 
		if (((ni->reftype1 == NR_VEHICLE && ni->ref1 == vid) || (ni->reftype2 == NR_VEHICLE && ni->ref2 == vid)) &&
 
				(news == INVALID_STRING_ID || ni->string_id == news)) {
 
			DeleteNewsItem(ni);
 
		}
 
		ni = next;
 
	}
 
}
 

	
 
/**
 
 * Remove news regarding given station so there are no 'unknown station now accepts Mail'
 
 * or 'First train arrived at unknown station' news items.
 
 * @param sid station to remove news about
 
 */
 
void DeleteStationNews(StationID sid)
 
{
 
	NewsItem *ni = _oldest_news;
 

	
 
	while (ni != nullptr) {
 
		NewsItem *next = ni->next;
 
		if ((ni->reftype1 == NR_STATION && ni->ref1 == sid) || (ni->reftype2 == NR_STATION && ni->ref2 == sid)) {
 
			DeleteNewsItem(ni);
 
		}
 
		ni = next;
 
	}
 
}
 

	
 
/**
 
 * Remove news regarding given industry
 
 * @param iid industry to remove news about
 
 */
 
void DeleteIndustryNews(IndustryID iid)
 
{
 
	NewsItem *ni = _oldest_news;
 

	
 
	while (ni != nullptr) {
 
		NewsItem *next = ni->next;
 
		if ((ni->reftype1 == NR_INDUSTRY && ni->ref1 == iid) || (ni->reftype2 == NR_INDUSTRY && ni->ref2 == iid)) {
 
			DeleteNewsItem(ni);
 
		}
 
		ni = next;
 
	}
 
}
 

	
 
/**
 
 * Remove engine announcements for invalid engines.
 
 */
 
void DeleteInvalidEngineNews()
 
{
 
	NewsItem *ni = _oldest_news;
 

	
 
	while (ni != nullptr) {
 
		NewsItem *next = ni->next;
0 comments (0 inline, 0 general)