changeset 18908:b7744a321dc6 draft

(svn r23757) -Codechange: Unify the drawing of toggle buttons for boolean settings.
author frosch <frosch@openttd.org>
date Thu, 05 Jan 2012 19:32:51 +0000
parents fc4ea3329e13
children 403293ec3333
files src/ai/ai_gui.cpp src/cheat_gui.cpp src/gui.h src/newgrf_gui.cpp src/settings_gui.cpp
diffstat 5 files changed, 18 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/ai_gui.cpp
+++ b/src/ai/ai_gui.cpp
@@ -384,7 +384,7 @@
 			}
 
 			if ((config_item.flags & SCRIPTCONFIG_BOOLEAN) != 0) {
-				DrawFrameRect(buttons_left, y  + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE);
+				DrawBoolButton(buttons_left, y + 2, current_value != 0, editable);
 				SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
 			} else {
 				DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > config_item.min_value, editable && current_value < config_item.max_value);
--- a/src/cheat_gui.cpp
+++ b/src/cheat_gui.cpp
@@ -197,7 +197,7 @@
 				case SLE_BOOL: {
 					bool on = (*(bool*)ce->variable);
 
-					DrawFrameRect(button_left, y + 1, button_left + 20 - 1, y + FONT_HEIGHT_NORMAL - 1, on ? COLOUR_GREEN : COLOUR_RED, on ? FR_LOWERED : FR_NONE);
+					DrawBoolButton(button_left, y, on, true);
 					SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 					break;
 				}
--- a/src/gui.h
+++ b/src/gui.h
@@ -29,6 +29,7 @@
 void ShowGameDifficulty();
 void ShowGameSettings();
 void DrawArrowButtons(int x, int y, Colours button_colour, byte state, bool clickable_left, bool clickable_right);
+void DrawBoolButton(int x, int y, bool state, bool clickable);
 
 /* train_gui.cpp */
 void ShowOrdersWindow(const Vehicle *v);
--- a/src/newgrf_gui.cpp
+++ b/src/newgrf_gui.cpp
@@ -247,7 +247,7 @@
 			bool selected = (i == this->clicked_row);
 
 			if (par_info->type == PTYPE_BOOL) {
-				DrawFrameRect(buttons_left, y  + 2, buttons_left + 19, y + 10, (current_value != 0) ? COLOUR_GREEN : COLOUR_RED, (current_value != 0) ? FR_LOWERED : FR_NONE);
+				DrawBoolButton(buttons_left, y + 2, current_value != 0, true);
 				SetDParam(2, par_info->GetValue(this->grf_config) == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON);
 			} else if (par_info->type == PTYPE_UINT_ENUM) {
 				DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, current_value > par_info->min_value, current_value < par_info->max_value);
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -1197,11 +1197,10 @@
 	if ((sdb->flags & SGF_NO_NETWORK) && _networking) editable = false;
 
 	if (sdb->cmd == SDT_BOOLX) {
-		static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
 		/* Draw checkbox for boolean-value either on/off */
 		bool on = ReadValue(var, sd->save.conv) != 0;
 
-		DrawFrameRect(buttons_left, button_y, buttons_left + 19, button_y + 8, _bool_ctabs[!!on][!!editable], on ? FR_LOWERED : FR_NONE);
+		DrawBoolButton(buttons_left, button_y, on, editable);
 		SetDParam(0, on ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 	} else {
 		int32 value;
@@ -1822,6 +1821,19 @@
 	}
 }
 
+/**
+ * Draw a toggle button.
+ * @param x the x position to draw
+ * @param y the y position to draw
+ * @param state true = lowered
+ * @param clickable is the button clickable?
+ */
+void DrawBoolButton(int x, int y, bool state, bool clickable)
+{
+	static const Colours _bool_ctabs[2][2] = {{COLOUR_CREAM, COLOUR_RED}, {COLOUR_DARK_GREEN, COLOUR_GREEN}};
+	DrawFrameRect(x, y + 1, x + 19, y + 9, _bool_ctabs[state][clickable], state ? FR_LOWERED : FR_NONE);
+}
+
 struct CustomCurrencyWindow : Window {
 	int query_widget;