changeset 8349:cbb2e614bcfa draft

(svn r11915) -Codechange: Add a function to draw a sort button's up/down arrow. Arrows are now drawn in a consistent position based on the widget, instead of randomly positioned by pixel.
author peter1138 <peter1138@openttd.org>
date Fri, 18 Jan 2008 10:36:55 +0000
parents 1a20ebc6285e
children 8cde0dcc6818
files src/build_vehicle_gui.cpp src/gfx_func.h src/group_gui.cpp src/industry_gui.cpp src/misc_gui.cpp src/network/network_gui.cpp src/station_gui.cpp src/town_gui.cpp src/vehicle_gui.cpp src/widget.cpp src/window_gui.h
diffstat 11 files changed, 38 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -995,7 +995,7 @@
 		if (text_end > wi->bottom) ExpandPurchaseInfoWidget(w, text_end - wi->bottom);
 	}
 
-	DoDrawString(bv->descending_sort_order ? DOWNARROW : UPARROW, 69, 15, TC_BLACK);
+	DrawSortButtonState(w, BUILD_VEHICLE_WIDGET_SORT_ASSENDING_DESCENDING, bv->descending_sort_order ? SBS_DOWN : SBS_UP);
 }
 
 static void BuildVehicleClickEvent(Window *w, WindowEvent *e)
--- a/src/gfx_func.h
+++ b/src/gfx_func.h
@@ -83,12 +83,6 @@
 
 void DrawSprite(SpriteID img, SpriteID pal, int x, int y, const SubSprite *sub = NULL);
 
-/* XXX doesn't really belong here, but the only
- * consumers always use it in conjunction with DoDrawString() */
-#define UPARROW   "\xEE\x8A\x80"
-#define DOWNARROW "\xEE\x8A\xAA"
-
-
 int DrawStringCentered(int x, int y, StringID str, uint16 color);
 int DrawStringCenteredTruncated(int xl, int xr, int y, StringID str, uint16 color);
 int DoDrawStringCentered(int x, int y, const char *str, uint16 color);
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -466,9 +466,9 @@
 				DrawStringRightAligned(187, y1 + 1, STR_GROUP_TINY_NUM, (gv->group_sel == g->index) ? TC_WHITE : TC_BLACK);
 			}
 
+			DrawSortButtonState(w, GRP_WIDGET_SORT_BY_ORDER, gv->l.flags & VL_DESC ? SBS_DOWN : SBS_UP);
+
 			/* Draw Matrix Vehicle according to the vehicle list built before */
-			DoDrawString(gv->l.flags & VL_DESC ? DOWNARROW : UPARROW, 269, 15, TC_BLACK);
-
 			max = min(w->vscroll2.pos + w->vscroll2.cap, gv->l.list_length);
 			for (i = w->vscroll2.pos ; i < max ; ++i) {
 				const Vehicle* v = gv->sort_list[i];
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -781,10 +781,6 @@
 {
 	switch (e->event) {
 	case WE_PAINT: {
-		int n;
-		uint p;
-		static const uint16 _indicator_positions[4] = {88, 187, 284, 387};
-
 		if (_industry_sort_dirty) {
 			_industry_sort_dirty = false;
 			MakeSortedIndustryList();
@@ -793,10 +789,10 @@
 		SetVScrollCount(w, _num_industry_sort);
 
 		DrawWindowWidgets(w);
-		DoDrawString(_industry_sort_order & 1 ? DOWNARROW : UPARROW, _indicator_positions[_industry_sort_order >> 1], 15, TC_BLACK);
+		DrawSortButtonState(w, IDW_SORTBYNAME + (_industry_sort_order >> 1), _industry_sort_order & 1 ? SBS_DOWN : SBS_UP);
 
-		p = w->vscroll.pos;
-		n = 0;
+		uint p = w->vscroll.pos;
+		int n = 0;
 
 		while (p < _num_industry_sort) {
 			const Industry* i = _industry_sort[p];
--- a/src/misc_gui.cpp
+++ b/src/misc_gui.cpp
@@ -1505,11 +1505,7 @@
 		}
 
 		GfxFillRect(w->widget[7].left + 1, w->widget[7].top + 1, w->widget[7].right, w->widget[7].bottom, 0xD7);
-		DoDrawString(
-			_savegame_sort_order & SORT_DESCENDING ? DOWNARROW : UPARROW,
-			_savegame_sort_order & SORT_BY_NAME ? w->widget[2].right - 9 : w->widget[3].right - 9,
-			15, TC_BLACK
-		);
+		DrawSortButtonState(w, _savegame_sort_order & SORT_BY_NAME ? 2 : 3, _savegame_sort_order & SORT_DESCENDING ? SBS_DOWN : SBS_UP);
 
 		y = w->widget[7].top + 1;
 		for (pos = w->vscroll.pos; pos < _fios_num; pos++) {
--- a/src/network/network_gui.cpp
+++ b/src/network/network_gui.cpp
@@ -268,7 +268,7 @@
 
 	case WE_PAINT: {
 		const NetworkGameList *sel = nd->server;
-		const char *arrow = (ld->flags & VL_DESC) ? DOWNARROW : UPARROW;
+		const SortButtonState arrow = (ld->flags & VL_DESC) ? SBS_DOWN : SBS_UP;
 
 		if (ld->flags & VL_REBUILD) {
 			BuildNetworkGameList(&WP(w, network_ql_d));
@@ -300,9 +300,9 @@
 
 		/* Sort based on widgets: name, clients, compatibility */
 		switch (ld->sort_type) {
-			case NGWW_NAME    - NGWW_NAME: DoDrawString(arrow, w->widget[NGWW_NAME].right    - 10, 42, TC_BLACK); break;
-			case NGWW_CLIENTS - NGWW_NAME: DoDrawString(arrow, w->widget[NGWW_CLIENTS].right - 10, 42, TC_BLACK); break;
-			case NGWW_INFO    - NGWW_NAME: DoDrawString(arrow, w->widget[NGWW_INFO].right    - 10, 42, TC_BLACK); break;
+			case NGWW_NAME    - NGWW_NAME: DrawSortButtonState(w, NGWW_NAME,    arrow); break;
+			case NGWW_CLIENTS - NGWW_NAME: DrawSortButtonState(w, NGWW_CLIENTS, arrow); break;
+			case NGWW_INFO    - NGWW_NAME: DrawSortButtonState(w, NGWW_INFO,    arrow); break;
 		}
 
 		{ // draw list of games
--- a/src/station_gui.cpp
+++ b/src/station_gui.cpp
@@ -344,7 +344,7 @@
 			DrawWindowWidgets(w);
 
 			/* draw arrow pointing up/down for ascending/descending sorting */
-			DoDrawString(sl->flags & SL_ORDER ? DOWNARROW : UPARROW, 69, 26, TC_BLACK);
+			DrawSortButtonState(w, SLW_SORTBY, sl->flags & SL_ORDER ? SBS_DOWN : SBS_UP);
 
 			int cg_ofst;
 			int x = 89;
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -478,7 +478,7 @@
 		SetVScrollCount(w, _num_town_sort);
 
 		DrawWindowWidgets(w);
-		DoDrawString(_town_sort_order & 1 ? DOWNARROW : UPARROW, (_town_sort_order <= 1) ? 88 : 187, 15, TC_BLACK);
+		DrawSortButtonState(w, (_town_sort_order <= 1) ? 3 : 4, _town_sort_order & 1 ? SBS_DOWN : SBS_UP);
 
 		{
 			int n = 0;
--- a/src/vehicle_gui.cpp
+++ b/src/vehicle_gui.cpp
@@ -988,7 +988,7 @@
 	/* draw sorting criteria string */
 	DrawString(85, 15, _vehicle_sort_listing[vl->l.sort_type], TC_BLACK);
 	/* draw arrow pointing up/down for ascending/descending sorting */
-	DoDrawString(vl->l.flags & VL_DESC ? DOWNARROW : UPARROW, 69, 15, TC_BLACK);
+	DrawSortButtonState(w, VLW_WIDGET_SORT_ORDER, vl->l.flags & VL_DESC ? SBS_DOWN : SBS_UP);
 
 	max = min(w->vscroll.pos + w->vscroll.cap, vl->l.list_length);
 	for (i = w->vscroll.pos; i < max; ++i) {
--- a/src/widget.cpp
+++ b/src/widget.cpp
@@ -14,6 +14,9 @@
 #include "table/sprites.h"
 #include "table/strings.h"
 
+static const char *UPARROW   = "\xEE\x8A\x80";
+static const char *DOWNARROW = "\xEE\x8A\xAA";
+
 static Point HandleScrollbarHittest(const Scrollbar *sb, int top, int bottom)
 {
 	Point pt;
@@ -579,3 +582,16 @@
 		}
 	}
 }
+
+/** Draw a sort button's up or down arrow symbol.
+ * @param w Window of widget
+ * @param widget Sort button widget
+ * @param state State of sort button
+ */
+void DrawSortButtonState(const Window *w, int widget, SortButtonState state)
+{
+	if (state == SBS_OFF) return;
+
+	int offset = w->IsWidgetLowered(widget) ? 1 : 0;
+	DoDrawString(state == SBS_DOWN ? DOWNARROW : UPARROW, w->widget[widget].right - 11 + offset, w->widget[widget].top + 1 + offset, TC_BLACK);
+}
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -581,6 +581,14 @@
 int GetWidgetFromPos(const Window *w, int x, int y);
 void DrawWindowWidgets(const Window *w);
 
+enum SortButtonState {
+	SBS_OFF,
+	SBS_DOWN,
+	SBS_UP,
+};
+
+void DrawSortButtonState(const Window *w, int widget, SortButtonState state);
+
 
 Window *GetCallbackWnd();
 void DeleteNonVitalWindows();