File diff r5244:3a18e277e353 → r5245:0bb54485b97d
news_gui.c
Show inline comments
 
@@ -216,20 +216,26 @@ static void NewsWindowProc(Window *w, Wi
 

	
 
		SetDirtyBlocks(w->left, w->top - diff, w->left + w->width, w->top + w->height);
 
	} break;
 
	}
 
}
 

	
 
// returns the correct index in the array
 
// (to deal with overflows)
 
static byte increaseIndex(byte i)
 
/** Return the correct index in the pseudo-fifo
 
 * queue and deals with overflows when increasing the index */
 
static inline byte increaseIndex(byte i)
 
{
 
	if (i == INVALID_NEWS) return 0;
 
	i++;
 
	if (i >= MAX_NEWS) i = i % MAX_NEWS;
 
	return i;
 
	return (i + 1) % MAX_NEWS;
 
}
 

	
 
/** Return the correct index in the pseudo-fifo
 
 * queue and deals with overflows when decreasing the index */
 
static inline byte decreaseIndex(byte i)
 
{
 
	assert(i != INVALID_NEWS);
 
	return (i + MAX_NEWS - 1) % MAX_NEWS;
 
}
 

	
 
/** Add a new newsitem to be shown.
 
 * @param string String to display, can have special values based on parameter 'flags'
 
 * @param flags various control bits that will show various news-types. See macro NEWS_FLAGS()
 
 * @param data_a news-specific value based on news type
 
@@ -248,56 +254,57 @@ static byte increaseIndex(byte i)
 
 * @see NewsMode
 
 * @see NewsFlags
 
 * @see NewsType
 
 * @see NewsCallback */
 
void AddNewsItem(StringID string, uint32 flags, uint data_a, uint data_b)
 
{
 
	NewsItem *ni;
 
	Window *w;
 
	byte l_news;
 

	
 
	if (_game_mode == GM_MENU) return;
 

	
 
	// check the rare case that the oldest (to be overwritten) news item is open
 
	if (_total_news==MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news))
 
	if (_total_news == MAX_NEWS && (_oldest_news == _current_news || _oldest_news == _forced_news))
 
		MoveToNexItem();
 

	
 
	_forced_news = INVALID_NEWS;
 
	if (_total_news < MAX_NEWS) _total_news++;
 

	
 
	// make sure our pointer isn't overflowing
 
	l_news = _latest_news;
 
	_latest_news = increaseIndex(_latest_news);
 

	
 
	/* If the fifo-buffer is full, overwrite the oldest entry */
 
	if (l_news != INVALID_NEWS && _latest_news == _oldest_news)
 
		_oldest_news = increaseIndex(_oldest_news); // but make sure we're not overflowing here
 
	if (l_news != INVALID_NEWS && _latest_news == _oldest_news) {
 
		assert(_total_news == MAX_NEWS);
 
		_oldest_news = increaseIndex(_oldest_news);
 
	}
 

	
 
	// add news to _latest_news
 
	ni = &_news_items[_latest_news];
 
	memset(ni, 0, sizeof(*ni));
 
	{ /* Add news to _latest_news */
 
		Window *w;
 
		NewsItem *ni = &_news_items[_latest_news];
 
		memset(ni, 0, sizeof(*ni));
 

	
 
	ni->string_id = string;
 
	ni->display_mode = (byte)flags;
 
	ni->flags = (byte)(flags >> 8);
 
		ni->string_id = string;
 
		ni->display_mode = (byte)flags;
 
		ni->flags = (byte)(flags >> 8);
 

	
 
	// show this news message in color?
 
	if (_cur_year >= _patches.colored_news_year)
 
		ni->flags |= NF_INCOLOR;
 
		// show this news message in color?
 
		if (_cur_year >= _patches.colored_news_year) ni->flags |= NF_INCOLOR;
 

	
 
	ni->type = (byte)(flags >> 16);
 
	ni->callback = (byte)(flags >> 24);
 
	ni->data_a = data_a;
 
	ni->data_b = data_b;
 
	ni->date = _date;
 
	COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
 
		ni->type = (byte)(flags >> 16);
 
		ni->callback = (byte)(flags >> 24);
 
		ni->data_a = data_a;
 
		ni->data_b = data_b;
 
		ni->date = _date;
 
		COPY_OUT_DPARAM(ni->params, 0, lengthof(ni->params));
 

	
 
	w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 
	if (w == NULL) return;
 
	SetWindowDirty(w);
 
	w->vscroll.count = _total_news;
 
		w = FindWindowById(WC_MESSAGE_HISTORY, 0);
 
		if (w == NULL) return;
 
		SetWindowDirty(w);
 
		w->vscroll.count = _total_news;
 
	}
 
}
 

	
 

	
 
// don't show item if it's older than x days
 
static const byte _news_items_age[] = {60, 60, 90, 60, 90, 30, 150, 30, 90, 180};
 

	
 
@@ -468,13 +475,13 @@ static bool ReadyForNextItem(void)
 

	
 
static void MoveToNexItem(void)
 
{
 
	DeleteWindowById(WC_NEWS_WINDOW, 0);
 
	_forced_news = INVALID_NEWS;
 

	
 
	// if we're not at the last item, than move on
 
	// if we're not at the last item, then move on
 
	if (_current_news != _latest_news) {
 
		NewsItem *ni;
 

	
 
		_current_news = increaseIndex(_current_news);
 
		ni = &_news_items[_current_news];
 

	
 
@@ -533,18 +540,22 @@ static void ShowNewsMessage(byte i)
 
		ShowNewspaper(ni);
 
	}
 
}
 

	
 
void ShowLastNewsMessage(void)
 
{
 
	if (_forced_news == INVALID_NEWS) {
 
		ShowNewsMessage(_current_news);
 
	} else if (_forced_news != 0) {
 
		ShowNewsMessage(_forced_news - 1);
 
	} else {
 
		ShowNewsMessage(_total_news != MAX_NEWS ? _latest_news : MAX_NEWS - 1);
 
	switch (_forced_news) {
 
		case INVALID_NEWS: // Not forced any news yet, show the current one
 
			ShowNewsMessage(_current_news);
 
			break;
 
		case 0: //
 
			ShowNewsMessage(_total_news != MAX_NEWS ? _latest_news : MAX_NEWS - 1);
 
			break;
 
		default: // 'Scrolling' through news history show each one in turn
 
			ShowNewsMessage(_forced_news - 1);
 
			break;
 
	}
 
}
 

	
 

	
 
/* return news by number, with 0 being the most
 
 * recent news. Returns INVALID_NEWS if end of queue reached. */
 
@@ -873,23 +884,22 @@ void ShowMessageOptions(void)
 

	
 

	
 
void DeleteVehicleNews(VehicleID vid, StringID news)
 
{
 
	byte n;
 

	
 
	for (n = _oldest_news; _latest_news != INVALID_NEWS && n != (_latest_news + 1) % MAX_NEWS; n = (n + 1) % MAX_NEWS) {
 
		const NewsItem* ni = &_news_items[n];
 
	for (n = _oldest_news; _latest_news != INVALID_NEWS && n != increaseIndex(_latest_news); n = increaseIndex(n)) {
 
		const NewsItem *ni = &_news_items[n];
 

	
 
		if (ni->flags & NF_VEHICLE &&
 
				ni->data_a == vid &&
 
				(news == INVALID_STRING_ID || ni->string_id == news)) {
 
			Window *w;
 
			byte i;
 

	
 
			if (_forced_news  == n) MoveToNexItem();
 
			if (_current_news == n) MoveToNexItem();
 
			if (_forced_news  == n || _current_news == n) MoveToNexItem();
 

	
 
			// If this is the last news item, invalidate _latest_news
 
			if (_latest_news == _oldest_news) _latest_news = INVALID_NEWS;
 

	
 
			if (n != _oldest_news) {
 
				for (i = n; i != _oldest_news; i = (i + MAX_NEWS - 1) % MAX_NEWS) {