Widgets & Windows

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
burty
Transport Coordinator
Transport Coordinator
Posts: 326
Joined: 16 Jun 2006 17:18
Location: Somwhere near a computer

Widgets & Windows

Post by burty »

I have looked at the Wiki and read the stuff about widgets but I can't seem to get my window for the Dividend options to display properly...

I've put all the dividend window stuff below to try and show as much as I can, I hope it's not too much but I was unsure what would be needed.

Code: Select all

struct SetDividendWindow : Window {
	SetDividendWindow(WindowDesc *desc, WindowNumber window_number) : Window(desc)
	{
		this->InitNested(window_number);
		this->owner = (Owner)this->window_number;
	}
	
	virtual void SetStringParameters(int widget) const
	{
		switch (widget) {
		case WID_CD_CAPTION:
				SetDParam(0, (CompanyID)this->window_number);
				break;
		}
	}
	virtual void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize)
	{
		switch(widget)
		{
		case WID_CD_TYPE_DESC: 
			size->width = max(size->width, GetStringBoundingBox(STR_COMPANY_DIVIDEND_VIEW_TYPE).width);
			break;
		}
	}
	virtual void DrawWidget(const Rect &r, int widget) const
	{
		switch(widget)
		{
		case WID_CD_TYPE_DESC:
			DrawString(r.left, r.right, r.top, STR_COMPANY_DIVIDEND_VIEW_TYPE);
			break;
		}
	}


};

static const NWidgetPart _nested_dividends_widgets[] = {
	/** START CAPTION BOX */
	NWidget(NWID_HORIZONTAL),
		NWidget(WWT_CLOSEBOX, COLOUR_GREY),
			NWidget(WWT_CAPTION, COLOUR_GREY, WID_CD_CAPTION), SetDataTip(STR_COMPANY_DIVIDEND_VIEW_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
			NWidget(WWT_SHADEBOX, COLOUR_GREY),
			NWidget(WWT_STICKYBOX, COLOUR_GREY),
	EndContainer(),
	/* END CAPTION BOX */

	/* START DIVIDEND BOX */
	NWidget(WWT_PANEL, COLOUR_GREY),
		NWidget(NWID_VERTICAL), SetPIP(8, 8, 8),
			NWidget(NWID_HORIZONTAL),
					NWidget(WWT_TEXT, COLOUR_WHITE, WID_CD_TYPE_DESC),
					NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_CD_TYPE_L), SetDataTip(AWV_DECREASE, STR_FACE_HAIR_TOOLTIP),
					NWidget(WWT_PUSHTXTBTN, COLOUR_GREY, WID_CD_TYPE), SetDataTip(STR_EMPTY, STR_FACE_HAIR_TOOLTIP),
					NWidget(WWT_PUSHARROWBTN, COLOUR_GREY, WID_CD_TYPE_R), SetDataTip(AWV_INCREASE, STR_FACE_HAIR_TOOLTIP),
				EndContainer(),
			EndContainer(),
	EndContainer(),
	/* END DIVIDEND BOX */
};
static WindowDesc _set_company_dividend_desc(
	WDP_AUTO, NULL, 0, 0,
	WC_SET_DIVIDENDS, WC_NONE, 
	WDF_CONSTRUCTION,
	_nested_dividends_widgets, lengthof(_nested_dividends_widgets)
	);
I've got the caption bar drawing with the correct company colour as well as the sticky and shade boxes that's all good... The main problem lies in getting the string to display to the left of the arrow buttons.
The reason I need to do it via the draw event is that the user will be able to select "%" or "value" as the first options and so the second question should reflect the first selections choice.

If anyone can point me in the right direction I would be really grateful.

Many Thanks,
Ben
Alberth
OpenTTD Developer
OpenTTD Developer
Posts: 4763
Joined: 09 Sep 2007 05:03
Location: home

Re: Widgets & Windows

Post by Alberth »

Not sure that I understand the question exactly, but the left widget can be done by changing the data parameter of the widget. Most often it is done with SetDataTip
company_gui.cpp, line 1104-1109 wrote:

Code: Select all

		NWidgetCore *wi = this->GetWidget<NWidgetCore>(WID_SCMF_TOGGLE_LARGE_SMALL_BUTTON);
		if (advanced) {
			wi->SetDataTip(STR_FACE_SIMPLE, STR_FACE_SIMPLE_TOOLTIP);
		} else {
			wi->SetDataTip(STR_FACE_ADVANCED, STR_FACE_ADVANCED_TOOLTIP);
		}
After changing it, be sure to mark it as dirty, so it gets repainted to the screen.
User avatar
adf88
Chief Executive
Chief Executive
Posts: 644
Joined: 14 Jan 2008 15:51
Location: PL

Re: Widgets & Windows

Post by adf88 »

Do the same what you did with the WID_CD_CAPTION.

Use some existing string or make your own...

Code: Select all

STR_COMPANY_DIVIDEND_TYPE_DESC:  The value you chose is {NUM}
...assign it to the widget...

Code: Select all

static const NWidgetPart _nested_dividends_widgets[] = {
    (...)
    NWidget(WWT_TEXT, COLOUR_WHITE, WID_CD_TYPE_DESC), SetDataTip(STR_COMPANY_DIVIDEND_TYPE_DESC, STR_EMPTY)
...set the value via SetStringParameters...

Code: Select all

virtual void SetStringParameters(int widget) const
{
    (...)
    case WID_CD_TYPE_DESC:
        SetDParam(0, this->value /* <-- the value to be displayed instead of {NUM} in the string */);
...and don't forget to mark the widget as dirty when value changes

Code: Select all

this->value = ...
this->MarkWidgetDirty(WID_CD_TYPE_DESC);
:] don't worry, be happy and checkout my patches
User avatar
Zuu
OpenTTD Developer
OpenTTD Developer
Posts: 4553
Joined: 09 Jun 2003 18:21
Location: /home/sweden

Re: Widgets & Windows

Post by Zuu »

Also if you add/change widgets, don't forget to do this before submitting a patch to flyspray:

Code: Select all

cd src/script/api
./generate_widget.sh
./squirrel_export.sh
It will update the Game Script API so that the constants to reference widgets/windows are up to date. It is not necessary to do while developing the patch as long as you don't need GS testing. But it has to be done at the right step in a patch queue before its submitted.
My OpenTTD contributions (AIs, Game Scripts, patches, OpenTTD Auto Updater, and some sprites)
Junctioneer (a traffic intersection simulator)
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 61 guests