Changeset - r28752:a27b176a812f
[Not reviewed]
master
0 1 0
Peter Nelson - 8 months ago 2024-02-13 07:41:24
peter1138@openttd.org
Add: Show cargo icons on subsidy list window. (#12079)
1 file changed with 20 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/subsidy_gui.cpp
Show inline comments
 
@@ -18,33 +18,39 @@
 
#include "subsidy_func.h"
 
#include "subsidy_base.h"
 
#include "core/geometry_func.hpp"
 

	
 
#include "widgets/subsidy_widget.h"
 

	
 
#include "table/strings.h"
 

	
 
#include "safeguards.h"
 

	
 
struct SubsidyListWindow : Window {
 
	Scrollbar *vscroll;
 
	Dimension cargo_icon_size;
 

	
 
	SubsidyListWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
 
	{
 
		this->CreateNestedTree();
 
		this->vscroll = this->GetScrollbar(WID_SUL_SCROLLBAR);
 
		this->FinishInitNested(window_number);
 
		this->OnInvalidateData(0);
 
	}
 

	
 
	void OnInit() override
 
	{
 
		this->cargo_icon_size = GetLargestCargoIconSize();
 
	}
 

	
 
	void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
 
	{
 
		if (widget != WID_SUL_PANEL) return;
 

	
 
		int y = this->vscroll->GetScrolledRowFromWidget(pt.y, this, WID_SUL_PANEL, WidgetDimensions::scaled.framerect.top);
 
		int num = 0;
 
		for (const Subsidy *s : Subsidy::Iterate()) {
 
			if (!s->IsAwarded()) {
 
				y--;
 
				if (y == 0) {
 
					this->HandleClick(s);
 
					return;
 
@@ -129,55 +135,66 @@ struct SubsidyListWindow : Window {
 
	{
 
		if (widget != WID_SUL_PANEL) return;
 
		Dimension d = maxdim(GetStringBoundingBox(STR_SUBSIDIES_OFFERED_TITLE), GetStringBoundingBox(STR_SUBSIDIES_SUBSIDISED_TITLE));
 

	
 
		resize->height = GetCharacterHeight(FS_NORMAL);
 

	
 
		d.height *= 5;
 
		d.width += WidgetDimensions::scaled.framerect.Horizontal();
 
		d.height += WidgetDimensions::scaled.framerect.Vertical();
 
		*size = maxdim(*size, d);
 
	}
 

	
 
	void DrawCargoIcon(const Rect &r, int y_offset, CargoID cid) const
 
	{
 
		bool rtl = _current_text_dir == TD_RTL;
 
		SpriteID icon = CargoSpec::Get(cid)->GetCargoIcon();
 
		Dimension d = GetSpriteSize(icon);
 
		Rect ir = r.WithWidth(this->cargo_icon_size.width, rtl).WithHeight(GetCharacterHeight(FS_NORMAL));
 
		DrawSprite(icon, PAL_NONE, CenterBounds(ir.left, ir.right, d.width), CenterBounds(ir.top, ir.bottom, this->cargo_icon_size.height) + y_offset);
 
	}
 

	
 
	void DrawWidget(const Rect &r, WidgetID widget) const override
 
	{
 
		if (widget != WID_SUL_PANEL) return;
 

	
 
		TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
 

	
 
		Rect tr = r.Shrink(WidgetDimensions::scaled.framerect);
 
		Rect sr = tr.Indent(this->cargo_icon_size.width + WidgetDimensions::scaled.hsep_normal, _current_text_dir == TD_RTL);
 

	
 
		int pos = -this->vscroll->GetPosition();
 
		const int cap = this->vscroll->GetCapacity();
 

	
 
		/* Section for drawing the offered subsidies */
 
		if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_OFFERED_TITLE);
 
		pos++;
 

	
 
		uint num = 0;
 
		for (const Subsidy *s : Subsidy::Iterate()) {
 
			if (!s->IsAwarded()) {
 
				if (IsInsideMM(pos, 0, cap)) {
 
					/* Displays the two offered towns */
 
					SetupSubsidyDecodeParam(s, SubsidyDecodeParamType::Gui);
 
					/* If using wallclock units, show minutes remaining. Otherwise show the date when the subsidy ends. */
 
					if (TimerGameEconomy::UsingWallclockUnits()) {
 
						SetDParam(7, STR_SUBSIDIES_OFFERED_EXPIRY_TIME);
 
						SetDParam(8, s->remaining + 1); // We get the rest of the current economy month for free, since the expiration is checked on each new month.
 
					} else {
 
						SetDParam(7, STR_SUBSIDIES_OFFERED_EXPIRY_DATE);
 
						SetDParam(8, TimerGameEconomy::date - ymd.day + s->remaining * 32);
 
					}
 

	
 
					DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_OFFERED_FROM_TO);
 
					DrawCargoIcon(tr, pos * GetCharacterHeight(FS_NORMAL), s->cargo_type);
 
					DrawString(sr.left, sr.right, sr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_OFFERED_FROM_TO);
 
				}
 
				pos++;
 
				num++;
 
			}
 
		}
 

	
 
		if (num == 0) {
 
			if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_NONE);
 
			pos++;
 
		}
 

	
 
		/* Section for drawing the already granted subsidies */
 
@@ -193,25 +210,26 @@ struct SubsidyListWindow : Window {
 
					SetDParam(7, s->awarded);
 
					/* If using wallclock units, show minutes remaining. Otherwise show the date when the subsidy ends. */
 
					if (TimerGameEconomy::UsingWallclockUnits()) {
 
						SetDParam(8, STR_SUBSIDIES_SUBSIDISED_EXPIRY_TIME);
 
						SetDParam(9, s->remaining);
 
					}
 
					else {
 
						SetDParam(8, STR_SUBSIDIES_SUBSIDISED_EXPIRY_DATE);
 
						SetDParam(9, TimerGameEconomy::date - ymd.day + s->remaining * 32);
 
					}
 

	
 
					/* Displays the two connected stations */
 
					DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_SUBSIDISED_FROM_TO);
 
					DrawCargoIcon(tr, pos * GetCharacterHeight(FS_NORMAL), s->cargo_type);
 
					DrawString(sr.left, sr.right, sr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_SUBSIDISED_FROM_TO);
 
				}
 
				pos++;
 
				num++;
 
			}
 
		}
 

	
 
		if (num == 0) {
 
			if (IsInsideMM(pos, 0, cap)) DrawString(tr.left, tr.right, tr.top + pos * GetCharacterHeight(FS_NORMAL), STR_SUBSIDIES_NONE);
 
			pos++;
 
		}
 
	}
 

	
0 comments (0 inline, 0 general)