Changeset - r16185:2e45f981e36d
[Not reviewed]
master
0 1 0
frosch - 14 years ago 2010-10-04 19:14:43
frosch@openttd.org
(svn r20895) -Codechange: Use an enum to identify the rows of buttons in the industryview.
1 file changed with 36 insertions and 19 deletions:
0 comments (0 inline, 0 general)
src/industry_gui.cpp
Show inline comments
 
@@ -630,36 +630,43 @@ static inline bool IsProductionAlterable
 

	
 
/** Names of the widgets of the view industry gui */
 
enum IndustryViewWidgets {
 
	IVW_CAPTION,
 
	IVW_VIEWPORT,
 
	IVW_INFO,
 
	IVW_GOTO,
 
	IVW_DISPLAY,
 
};
 

	
 
class IndustryViewWindow : public Window
 
{
 
	byte editbox_line;        ///< The line clicked to open the edit box
 
	byte clicked_line;        ///< The line of the button that has been clicked
 
	/** Specific lines in the info panel */
 
	enum InfoLine {
 
		IL_NONE,              ///< No line
 
		IL_RATE1,             ///< Production rate of cargo 1
 
		IL_RATE2,             ///< Production rate of cargo 2
 
	};
 

	
 
	InfoLine editbox_line;    ///< The line clicked to open the edit box
 
	InfoLine clicked_line;    ///< The line of the button that has been clicked
 
	byte clicked_button;      ///< The button that has been clicked (to raise)
 
	int production_offset_y;  ///< The offset of the production texts/buttons
 
	int info_height;          ///< Height needed for the #IVW_INFO panel
 

	
 
public:
 
	IndustryViewWindow(const WindowDesc *desc, WindowNumber window_number) : Window()
 
	{
 
		this->flags4 |= WF_DISABLE_VP_SCROLL;
 
		this->editbox_line = 0;
 
		this->clicked_line = 0;
 
		this->editbox_line = IL_NONE;
 
		this->clicked_line = IL_NONE;
 
		this->clicked_button = 0;
 
		this->info_height = WD_FRAMERECT_TOP + 2 * FONT_HEIGHT_NORMAL + WD_FRAMERECT_BOTTOM + 1; // Info panel has at least two lines text.
 

	
 
		this->InitNested(desc, window_number);
 
		NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
 
		nvp->InitializeViewport(this, Industry::Get(window_number)->location.tile + TileDiffXY(1, 1), ZOOM_LVL_INDUSTRY);
 
	}
 

	
 
	virtual void OnPaint()
 
	{
 
		this->DrawWidgets();
 

	
 
@@ -734,25 +741,25 @@ public:
 
				this->production_offset_y = y;
 
				first = false;
 
			}
 

	
 
			SetDParam(0, i->produced_cargo[j]);
 
			SetDParam(1, i->last_month_production[j]);
 
			SetDParamStr(2, cargo_suffix[j]);
 
			SetDParam(3, ToPercent8(i->last_month_pct_transported[j]));
 
			uint x = left + WD_FRAMETEXT_LEFT + (IsProductionAlterable(i) ? 30 : 0);
 
			DrawString(x, right - WD_FRAMERECT_RIGHT, y, STR_INDUSTRY_VIEW_TRANSPORTED);
 
			/* Let's put out those buttons.. */
 
			if (IsProductionAlterable(i)) {
 
				DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == j + 1) ? this->clicked_button : 0,
 
				DrawArrowButtons(left + WD_FRAMETEXT_LEFT, y, COLOUR_YELLOW, (this->clicked_line == IL_RATE1 + j) ? this->clicked_button : 0,
 
						i->production_rate[j] > 0, i->production_rate[j] < 255);
 
			}
 
			y += FONT_HEIGHT_NORMAL;
 
		}
 

	
 
		/* Get the extra message for the GUI */
 
		if (HasBit(ind->callback_mask, CBM_IND_WINDOW_MORE_TEXT)) {
 
			uint16 callback_res = GetIndustryCallback(CBID_INDUSTRY_WINDOW_MORE_TEXT, 0, 0, i, i->type, i->location.tile);
 
			if (callback_res != CALLBACK_FAILED) {
 
				StringID message = GetGRFStringID(ind->grf_prop.grffile->grfid, 0xD000 + callback_res);
 
				if (message != STR_NULL && message != STR_UNDEFINED) {
 
					y += WD_PAR_VSEP_WIDE;
 
@@ -775,54 +782,65 @@ public:
 
	}
 

	
 
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
 
	{
 
		if (widget == IVW_INFO) size->height = this->info_height;
 
	}
 

	
 
	virtual void OnClick(Point pt, int widget, int click_count)
 
	{
 
		switch (widget) {
 
			case IVW_INFO: {
 
				Industry *i = Industry::Get(this->window_number);
 
				InfoLine line = IL_NONE;
 

	
 
				/* We should work if needed.. */
 
				if (!IsProductionAlterable(i)) return;
 
				if (pt.y >= this->production_offset_y) {
 
					int row = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
 
					for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
 
						if (i->produced_cargo[j] == CT_INVALID) continue;
 
						row--;
 
						if (row < 0) {
 
							line = (InfoLine)(IL_RATE1 + j);
 
							break;
 
						}
 
					}
 
				}
 
				if (line == IL_NONE) return;
 

	
 
				uint x = pt.x;
 
				int line = (pt.y - this->production_offset_y) / FONT_HEIGHT_NORMAL;
 
				if (pt.y >= this->production_offset_y && IsInsideMM(line, 0, 2) && i->produced_cargo[line] != CT_INVALID) {
 
				if (IsProductionAlterable(i)) {
 
					NWidgetBase *nwi = this->GetWidget<NWidgetBase>(widget);
 
					uint left = nwi->pos_x + WD_FRAMETEXT_LEFT;
 
					uint right = nwi->pos_x + nwi->current_x - 1 - WD_FRAMERECT_RIGHT;
 
					if (IsInsideMM(x, left, left + 20) ) {
 
						/* Clicked buttons, decrease or increase production */
 
						if (x < left + 10) {
 
							if (i->production_rate[line] <= 0) return;
 
							i->production_rate[line] = max(i->production_rate[line] / 2, 0);
 
							if (i->production_rate[line - IL_RATE1] <= 0) return;
 
							i->production_rate[line - IL_RATE1] = max(i->production_rate[line - IL_RATE1] / 2, 0);
 
						} else {
 
							if (i->production_rate[line - IL_RATE1] >= 255) return;
 
							/* a zero production industry is unlikely to give anything but zero, so push it a little bit */
 
							int new_prod = i->production_rate[line] == 0 ? 1 : i->production_rate[line] * 2;
 
							if (i->production_rate[line] >= 255) return;
 
							i->production_rate[line] = minu(new_prod, 255);
 
							int new_prod = i->production_rate[line - IL_RATE1] == 0 ? 1 : i->production_rate[line - IL_RATE1] * 2;
 
							i->production_rate[line - IL_RATE1] = minu(new_prod, 255);
 
						}
 

	
 
						UpdateIndustryProduction(i);
 
						this->SetDirty();
 
						this->flags4 |= WF_TIMEOUT_BEGIN;
 
						this->clicked_line = line + 1;
 
						this->clicked_line = line;
 
						this->clicked_button = (x < left + 10 ? 1 : 2);
 
					} else if (IsInsideMM(x, left + 30, right)) {
 
						/* clicked the text */
 
						this->editbox_line = line;
 
						SetDParam(0, i->production_rate[line] * 8);
 
						SetDParam(0, i->production_rate[line - IL_RATE1] * 8);
 
						ShowQueryString(STR_JUST_INT, STR_CONFIG_GAME_PRODUCTION, 10, 100, this, CS_ALPHANUMERAL, QSF_NONE);
 
					}
 
				}
 
				break;
 
			}
 

	
 
			case IVW_GOTO: {
 
				Industry *i = Industry::Get(this->window_number);
 
				if (_ctrl_pressed) {
 
					ShowExtraViewPortWindow(i->location.tile + TileDiffXY(1, 1));
 
				} else {
 
					ScrollMainWindowToTile(i->location.tile + TileDiffXY(1, 1));
 
@@ -831,45 +849,44 @@ public:
 
			}
 

	
 
			case IVW_DISPLAY: {
 
				Industry *i = Industry::Get(this->window_number);
 
				ShowIndustryCargoesWindow(i->type);
 
				break;
 
			}
 
		}
 
	}
 

	
 
	virtual void OnTimeout()
 
	{
 
		this->clicked_line = 0;
 
		this->clicked_line = IL_NONE;
 
		this->clicked_button = 0;
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnResize()
 
	{
 
		if (this->viewport != NULL) {
 
			NWidgetViewport *nvp = this->GetWidget<NWidgetViewport>(IVW_VIEWPORT);
 
			nvp->UpdateViewportCoordinates(this);
 
		}
 
	}
 

	
 
	virtual void OnQueryTextFinished(char *str)
 
	{
 
		if (StrEmpty(str)) return;
 

	
 
		Industry *i = Industry::Get(this->window_number);
 
		int line = this->editbox_line;
 

	
 
		i->production_rate[line] = ClampU(atoi(str) / 8, 0, 255);
 
		i->production_rate[this->editbox_line - IL_RATE1] = ClampU(atoi(str) / 8, 0, 255);
 
		UpdateIndustryProduction(i);
 
		this->SetDirty();
 
	}
 

	
 
	virtual bool IsNewGRFInspectable() const
 
	{
 
		return ::IsNewGRFInspectable(GSF_INDUSTRIES, this->window_number);
 
	}
 

	
 
	virtual void ShowNewGRFInspectWindow() const
 
	{
 
		::ShowNewGRFInspectWindow(GSF_INDUSTRIES, this->window_number);
0 comments (0 inline, 0 general)