Changeset - r9843:abc3e0e41b05
[Not reviewed]
master
0 2 0
peter1138 - 16 years ago 2008-08-04 05:59:28
peter1138@openttd.org
(svn r13987) -Codechange: Resize small map legend dynamically on window resize and when switching small map types, so that the legend is taller only on the industry type and only when the legend will not fit in the available space.
2 files changed with 52 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/smallmap_gui.cpp
Show inline comments
 
@@ -35,22 +35,22 @@
 
static const Widget _smallmap_widgets[] = {
 
{  WWT_CLOSEBOX,   RESIZE_NONE,  COLOUR_BROWN,     0,    10,     0,    13, STR_00C5,                STR_018B_CLOSE_WINDOW},
 
{   WWT_CAPTION,  RESIZE_RIGHT,  COLOUR_BROWN,    11,   337,     0,    13, STR_00B0_MAP,            STR_018C_WINDOW_TITLE_DRAG_THIS},
 
{ WWT_STICKYBOX,     RESIZE_LR,  COLOUR_BROWN,   338,   349,     0,    13, 0x0,                     STR_STICKY_BUTTON},
 
{     WWT_PANEL,     RESIZE_RB,  COLOUR_BROWN,     0,   349,    14,   157, 0x0,                     STR_NULL},
 
{     WWT_INSET,     RESIZE_RB,  COLOUR_BROWN,     2,   347,    16,   155, 0x0,                     STR_NULL},
 
{     WWT_PANEL,    RESIZE_RTB,  COLOUR_BROWN,     0,   261,   158,   201, 0x0,                     STR_NULL},
 
{     WWT_PANEL,   RESIZE_LRTB,  COLOUR_BROWN,   262,   349,   158,   158, 0x0,                     STR_NULL},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   284,   305,   158,   179, SPR_IMG_SHOW_COUNTOURS,  STR_0191_SHOW_LAND_CONTOURS_ON_MAP},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   306,   327,   158,   179, SPR_IMG_SHOW_VEHICLES,   STR_0192_SHOW_VEHICLES_ON_MAP},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   328,   349,   158,   179, SPR_IMG_INDUSTRY,        STR_0193_SHOW_INDUSTRIES_ON_MAP},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   284,   305,   180,   201, SPR_IMG_SHOW_ROUTES,     STR_0194_SHOW_TRANSPORT_ROUTES_ON},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   306,   327,   180,   201, SPR_IMG_PLANTTREES,      STR_0195_SHOW_VEGETATION_ON_MAP},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   328,   349,   180,   201, SPR_IMG_COMPANY_GENERAL, STR_0196_SHOW_LAND_OWNERS_ON_MAP},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   262,   283,   158,   179, SPR_IMG_SMALLMAP,        STR_SMALLMAP_CENTER},
 
{    WWT_IMGBTN,   RESIZE_LRTB,  COLOUR_BROWN,   262,   283,   180,   201, SPR_IMG_TOWN,            STR_0197_TOGGLE_TOWN_NAMES_ON_OFF},
 
{     WWT_PANEL,    RESIZE_RTB,  COLOUR_BROWN,     0,   261,   158,   201, 0x0,                     STR_NULL},
 
{     WWT_PANEL,   RESIZE_LRTB,  COLOUR_BROWN,   262,   349,   202,   202, 0x0,                     STR_NULL},
 
{     WWT_PANEL,    RESIZE_RTB,  COLOUR_BROWN,     0,   337,   202,   213, 0x0,                     STR_NULL},
 
{   WWT_TEXTBTN,     RESIZE_TB,  COLOUR_BROWN,     0,    99,   202,   213, STR_MESSAGES_ENABLE_ALL, STR_NULL},
 
{   WWT_TEXTBTN,     RESIZE_TB,  COLOUR_BROWN,   100,   201,   202,   213, STR_MESSAGES_DISABLE_ALL,STR_NULL},
 
{ WWT_RESIZEBOX,   RESIZE_LRTB,  COLOUR_BROWN,   338,   349,   202,   213, 0x0,                     STR_RESIZE_BUTTON},
 
{  WIDGETS_END},
 
};
 
@@ -521,23 +521,24 @@ static void DrawHorizMapIndicator(int x,
 
{
 
	GfxFillRect(x,      y, x + 3, y2, 69);
 
	GfxFillRect(x2 - 3, y, x2,    y2, 69);
 
}
 

	
 
enum SmallMapWindowWidgets {
 
	SM_WIDGET_MAP = 4,
 
	SM_WIDGET_MAP_BORDER = 3,
 
	SM_WIDGET_MAP,
 
	SM_WIDGET_LEGEND,
 
	SM_WIDGET_BUTTONSPANEL,
 
	SM_WIDGET_CONTOUR,
 
	SM_WIDGET_VEHICLES,
 
	SM_WIDGET_INDUSTRIES,
 
	SM_WIDGET_ROUTES,
 
	SM_WIDGET_VEGETATION,
 
	SM_WIDGET_OWNERS,
 
	SM_WIDGET_CENTERMAP,
 
	SM_WIDGET_TOGGLETOWNNAME,
 
	SM_WIDGET_LEGEND,
 
	SM_WIDGET_BUTTONSPANEL,
 
	SM_WIDGET_BOTTOMPANEL,
 
	SM_WIDGET_ENABLEINDUSTRIES,
 
	SM_WIDGET_DISABLEINDUSTRIES,
 
	SM_WIDGET_RESIZEBOX,
 
};
 

	
 
@@ -555,12 +556,15 @@ class SmallMapWindow : public Window
 

	
 
	int32 scroll_x;
 
	int32 scroll_y;
 
	int32 subscroll;
 
	uint8 refresh;
 

	
 
	static const int COLUMN_WIDTH = 119;
 
	static const int MIN_LEGEND_HEIGHT = 6 * 7;
 

	
 
public:
 
	/**
 
	 * Draws the small map.
 
	 *
 
	 * Basically, the small map is draw column of pixels by column of pixels. The pixels
 
	 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
 
@@ -782,28 +786,43 @@ public:
 
		y  = ((vp->virtual_height - (this->widget[SM_WIDGET_MAP].bottom - this->widget[SM_WIDGET_MAP].top ) * TILE_SIZE) / 2 + vp->virtual_top ) / 2 - TILE_SIZE * 2;
 
		this->scroll_x = (y - x) & ~0xF;
 
		this->scroll_y = (x + y) & ~0xF;
 
		this->SetDirty();
 
	}
 

	
 
	void ResizeLegend()
 
	{
 
		Widget *legend = &this->widget[SM_WIDGET_LEGEND];
 
		int rows = (legend->bottom - legend->top) - 1;
 
		int columns = (legend->right - legend->left) / COLUMN_WIDTH;
 
		int new_rows = (this->map_type == SMT_INDUSTRY) ? ((_smallmap_industry_count + columns - 1) / columns) * 6 : MIN_LEGEND_HEIGHT;
 

	
 
		new_rows = max(new_rows, MIN_LEGEND_HEIGHT);
 

	
 
		if (new_rows != rows) {
 
			this->SetDirty();
 

	
 
			/* The legend widget needs manual adjustment as by default
 
			 * it lays outside the filler widget's bounds. */
 
			legend->top--;
 
			/* Resize the filler widget, and move widgets below it. */
 
			ResizeWindowForWidget(this, SM_WIDGET_BUTTONSPANEL, 0, new_rows - rows);
 
			legend->top++;
 

	
 
			/* Resize map border widget so the window stays the same size */
 
			ResizeWindowForWidget(this, SM_WIDGET_MAP_BORDER, 0, rows - new_rows);
 
			/* Manually adjust the map widget as it lies completely within
 
			 * the map border widget */
 
			this->widget[SM_WIDGET_MAP].bottom += rows - new_rows;
 

	
 
			this->SetDirty();
 
		}
 
	}
 

	
 
	SmallMapWindow(const WindowDesc *desc, int window_number) : Window(desc, window_number)
 
	{
 
		/* Resize the window to fit industries list */
 
		Widget *legend = &this->widget[SM_WIDGET_LEGEND];
 
		int rows = (legend->bottom - legend->top) / 6;
 
		int columns = 2;
 

	
 
		if ((_smallmap_industry_count + columns - 1) / columns > rows) {
 
			int delta = ((_smallmap_industry_count + columns - 1) / columns - rows) * 6;
 
			/* Resize the filler widget, and move widgets below it. */
 
			ResizeWindowForWidget(this, SM_WIDGET_BUTTONSPANEL, 0, delta);
 
			/* The legend widget needs manual adjustment as it lays outside
 
			 * the filler widget's bounds. */
 
			legend->bottom += delta;
 
		}
 

	
 
		this->LowerWidget(this->map_type + SM_WIDGET_CONTOUR);
 
		this->SetWidgetLoweredState(SM_WIDGET_TOGGLETOWNNAME, this->show_towns);
 

	
 
		this->SmallMapCenterOnCurrentPos();
 
		this->FindWindowPlacementAndResize(desc);
 
	}
 
@@ -825,15 +844,15 @@ public:
 
		int y_org = legend->top + 1;
 
		int x = 4;
 
		int y = y_org;
 

	
 
		for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
 
			if (tbl->col_break || y >= legend->bottom) {
 
				/* Column break needed, continue at top, 123 pixels (one "row")
 
				 * to the right. */
 
				x += 123;
 
				/* Column break needed, continue at top, COLUMN_WIDTH pixels
 
				 * (one "row") to the right. */
 
				x += COLUMN_WIDTH;
 
				y = y_org;
 
			}
 

	
 
			if (this->map_type == SMT_INDUSTRY) {
 
				/* Industry name must be formated, since it's not in tiny font in the specs.
 
				 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font.*/
 
@@ -893,12 +912,14 @@ public:
 
			case SM_WIDGET_VEGETATION: // Show vegetation
 
			case SM_WIDGET_OWNERS:     // Show land owners
 
				this->RaiseWidget(this->map_type + SM_WIDGET_CONTOUR);
 
				this->map_type = (SmallMapType)(widget - SM_WIDGET_CONTOUR);
 
				this->LowerWidget(this->map_type + SM_WIDGET_CONTOUR);
 

	
 
				this->ResizeLegend();
 

	
 
				this->SetDirty();
 
				SndPlayFx(SND_15_BEEP);
 
				break;
 

	
 
			case SM_WIDGET_CENTERMAP: // Center the smallmap again
 
				this->SmallMapCenterOnCurrentPos();
 
@@ -917,13 +938,13 @@ public:
 

	
 
			case SM_WIDGET_LEGEND: // Legend
 
				/* if industry type small map*/
 
				if (this->map_type == SMT_INDUSTRY) {
 
					/* if click on industries label, find right industry type and enable/disable it */
 
					Widget *wi = &this->widget[SM_WIDGET_LEGEND]; // label panel
 
					uint column = (pt.x - 4) / 123;
 
					uint column = (pt.x - 4) / COLUMN_WIDTH;
 
					uint line = (pt.y - wi->top - 2) / 6;
 
					int rows_per_column = (wi->bottom - wi->top) / 6;
 

	
 
					/* check if click is on industry label*/
 
					int industry_pos = (column * rows_per_column) + line;
 
					if (industry_pos < _smallmap_industry_count) {
 
@@ -1025,12 +1046,17 @@ public:
 
		this->scroll_x = x;
 
		this->scroll_y = y;
 
		this->subscroll = sub;
 

	
 
		this->SetDirty();
 
	}
 

	
 
	virtual void OnResize(Point new_size, Point delta)
 
	{
 
		if (delta.x != 0 && this->map_type == SMT_INDUSTRY) this->ResizeLegend();
 
	}
 
};
 

	
 
SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
 
bool SmallMapWindow::show_towns = true;
 

	
 
static const WindowDesc _smallmap_desc = {
src/widget.cpp
Show inline comments
 
@@ -604,12 +604,15 @@ void ResizeWindowForWidget(Window *w, ui
 

	
 
	/* A hidden widget has bottom == top or right == left, we need to make it
 
	 * one less to fit in its new gap. */
 
	if (right  == w->widget[widget].left) w->widget[widget].right--;
 
	if (bottom == w->widget[widget].top)  w->widget[widget].bottom--;
 

	
 
	if (w->widget[widget].left > w->widget[widget].right)  w->widget[widget].right  = w->widget[widget].left;
 
	if (w->widget[widget].top  > w->widget[widget].bottom) w->widget[widget].bottom = w->widget[widget].top;
 

	
 
	w->width  += delta_x;
 
	w->height += delta_y;
 
	w->resize.width  += delta_x;
 
	w->resize.height += delta_y;
 
}
 

	
0 comments (0 inline, 0 general)