Changeset - r9859:8c15dba58099
[Not reviewed]
master
0 3 0
peter1138 - 16 years ago 2008-08-06 07:10:40
peter1138@openttd.org
(svn r14004) -Codechange: Clean of drop down lists.
Move empty item drawing to base ListItem Draw() function.
Remove String() from base class.
Pass correct width to Draw().
3 files changed with 31 insertions and 37 deletions:
0 comments (0 inline, 0 general)
src/player_gui.cpp
Show inline comments
 
@@ -290,38 +290,43 @@ static const LiveryClass _livery_class[L
 
	LC_ROAD, LC_ROAD,
 
	LC_SHIP, LC_SHIP,
 
	LC_AIRCRAFT, LC_AIRCRAFT, LC_AIRCRAFT,
 
	LC_ROAD, LC_ROAD,
 
};
 

	
 
class DropDownListColourItem : public DropDownListItem {
 
public:
 
	DropDownListColourItem(int result, bool masked) : DropDownListItem(result, masked) {}
 

	
 
	virtual ~DropDownListColourItem() {}
 

	
 
	virtual StringID String() const
 
	StringID String() const
 
	{
 
		return _colour_dropdown[this->result];
 
	}
 

	
 
	virtual uint Height(uint width) const
 
	uint Height(uint width) const
 
	{
 
		return 14;
 
	}
 

	
 
	virtual void Draw(int x, int y, uint width, uint height, bool sel) const
 
	bool Selectable() const
 
	{
 
		return true;
 
	}
 

	
 
	void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
 
	{
 
		DrawSprite(SPR_VEH_BUS_SIDE_VIEW, PALETTE_RECOLOR_START + this->result, x + 16, y + 7);
 
		DrawStringTruncated(x + 32, y + 3, this->String(), sel ? TC_WHITE : TC_BLACK, x + width - 30);
 
		DrawStringTruncated(x + 32, y + 3, this->String(), sel ? TC_WHITE : TC_BLACK, width - 30);
 
	}
 
};
 

	
 
struct SelectPlayerLiveryWindow : public Window {
 
private:
 
	uint32 sel;
 
	LiveryClass livery_class;
 

	
 
	enum PlayerLiveryWindowWidgets {
 
		PLW_WIDGET_CLOSE,
 
		PLW_WIDGET_CAPTION,
 
		PLW_WIDGET_CLASS_GENERAL,
src/widgets/dropdown.cpp
Show inline comments
 
@@ -8,50 +8,44 @@
 
#include "../window_gui.h"
 
#include "../strings_func.h"
 
#include "../strings_type.h"
 
#include "../gfx_func.h"
 
#include "../window_func.h"
 
#include "../core/math_func.hpp"
 
#include "dropdown_type.h"
 
#include "dropdown_func.h"
 

	
 
#include "../table/sprites.h"
 
#include "table/strings.h"
 

	
 
StringID DropDownListItem::String() const
 
void DropDownListItem::Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
 
{
 
	return STR_NULL;
 
	int c1 = _colour_gradient[bg_colour][3];
 
	int c2 = _colour_gradient[bg_colour][7];
 

	
 
	GfxFillRect(x + 1, y + 3, x + width - 2, y + 3, c1);
 
	GfxFillRect(x + 1, y + 4, x + width - 2, y + 4, c2);
 
}
 

	
 
uint DropDownListItem::Height(uint width) const
 
void DropDownListStringItem::Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
 
{
 
	return 10;
 
}
 

	
 
StringID DropDownListStringItem::String() const
 
{
 
	return this->string;
 
	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, width);
 
}
 

	
 
StringID DropDownListParamStringItem::String() const
 
{
 
	for (uint i = 0; i < lengthof(this->decode_params); i++) SetDParam(i, this->decode_params[i]);
 
	return this->string;
 
}
 

	
 
void DropDownListItem::Draw(int x, int y, uint width, uint height, bool sel) const
 
{
 
	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, x + width);
 
}
 

	
 
/**
 
 * Delete all items of a drop down list and the list itself
 
 * @param list List to delete.
 
 */
 
static void DeleteDropDownList(DropDownList *list)
 
{
 
	for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
 
		DropDownListItem *item = *it;
 
		delete item;
 
	}
 
	delete list;
 
}
 
@@ -97,74 +91,66 @@ struct DropdownWindow : Window {
 
		int pos   = this->vscroll.pos;
 

	
 
		const DropDownList *list = this->list;
 

	
 
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
 
			/* Skip items that are scrolled up */
 
			if (--pos >= 0) continue;
 

	
 
			const DropDownListItem *item = *it;
 
			int item_height = item->Height(width);
 

	
 
			if (y < item_height) {
 
				if (item->masked || item->String() == STR_NULL) return false;
 
				if (item->masked || !item->Selectable()) return false;
 
				value = item->result;
 
				return true;
 
			}
 

	
 
			y -= item_height;
 
		}
 

	
 
		return false;
 
	}
 

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

	
 
		int x = 1;
 
		int y = 2;
 

	
 
		int sel    = this->selected_index;
 
		int width  = this->widget[0].right - 3;
 
		int width  = this->widget[0].right - 2;
 
		int height = this->widget[0].bottom;
 
		int pos    = this->vscroll.pos;
 

	
 
		DropDownList *list = this->list;
 

	
 
		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
 
			const DropDownListItem *item = *it;
 
			int item_height = item->Height(width);
 

	
 
			/* Skip items that are scrolled up */
 
			if (--pos >= 0) continue;
 

	
 
			if (y + item_height < height) {
 
				if (item->String() != STR_NULL) {
 
					if (sel == item->result) GfxFillRect(x + 1, y, x + width, y + item_height - 1, 0);
 
				if (sel == item->result) GfxFillRect(x + 1, y, x + width - 1, y + item_height - 1, 0);
 

	
 
					item->Draw(x, y, width, 10, sel == item->result);
 
				item->Draw(x, y, width, height, sel == item->result, (TextColour)this->widget[0].color);
 

	
 
					if (item->masked) {
 
						GfxFillRect(x, y, x + width, y + item_height - 1,
 
					GfxFillRect(x, y, x + width - 1, y + item_height - 1,
 
							_colour_gradient[this->widget[0].color][5], FILLRECT_CHECKER
 
						);
 
					}
 
				} else {
 
					int c1 = _colour_gradient[this->widget[0].color][3];
 
					int c2 = _colour_gradient[this->widget[0].color][7];
 

	
 
					GfxFillRect(x + 1, y + 3, x + this->width - 5, y + 3, c1);
 
					GfxFillRect(x + 1, y + 4, x + this->width - 5, y + 4, c2);
 
				}
 
			}
 
			y += item_height;
 
		}
 
	};
 

	
 
	virtual void OnClick(Point pt, int widget)
 
	{
 
		if (widget != 0) return;
 
		int item;
 
		if (this->GetDropDownItem(item)) {
 
			this->click_delay = 4;
 
			this->selected_index = item;
src/widgets/dropdown_type.h
Show inline comments
 
@@ -10,54 +10,57 @@
 

	
 
/**
 
 * Base list item class from which others are derived. If placed in a list it
 
 * will appear as a horizontal line in the menu.
 
 */
 
class DropDownListItem {
 
public:
 
	int result;  ///< Result code to return to window on selection
 
	bool masked; ///< Masked and unselectable item
 

	
 
	DropDownListItem(int result, bool masked) : result(result), masked(masked) {}
 
	virtual ~DropDownListItem() {}
 
	virtual StringID String() const;
 
	virtual uint Height(uint width) const;
 
	virtual void Draw(int x, int y, uint width, uint height, bool sel) const;
 

	
 
	virtual bool Selectable() const { return false; }
 
	virtual uint Height(uint width) const { return 10; }
 
	virtual void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const;
 
};
 

	
 
/**
 
 * Common string list item.
 
 */
 
class DropDownListStringItem : public DropDownListItem {
 
public:
 
	StringID string; ///< String ID of item
 

	
 
	DropDownListStringItem(StringID string, int result, bool masked) : DropDownListItem(result, masked), string(string) {}
 
	virtual ~DropDownListStringItem() {}
 

	
 
	StringID String() const;
 
	virtual bool Selectable() const { return true; }
 
	virtual void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const;
 
	virtual StringID String() const { return this->string; }
 
};
 

	
 
/**
 
 * String list item with parameters.
 
 */
 
class DropDownListParamStringItem : public DropDownListStringItem {
 
public:
 
	uint64 decode_params[10]; ///< Parameters of the string
 

	
 
	DropDownListParamStringItem(StringID string, int result, bool masked) : DropDownListStringItem(string, result, masked) {}
 
	virtual ~DropDownListParamStringItem() {}
 

	
 
	StringID String() const;
 
	void SetParam(uint index, uint64 value) { decode_params[index] = value; }
 
	virtual StringID String() const;
 
	virtual void SetParam(uint index, uint64 value) { decode_params[index] = value; }
 
};
 

	
 
/**
 
 * A drop down list is a collection of drop down list items.
 
 */
 
typedef std::list<DropDownListItem *> DropDownList;
 

	
 
/**
 
 * Show a drop down list.
 
 * @param w        Parent window for the list.
 
 * @param list     Prepopulated DropDownList. Will be deleted when the list is
 
 *                 closed.
0 comments (0 inline, 0 general)