src/transparency_gui.cpp
branchNewGRF_ports
changeset 10724 68a692eacf22
parent 10184 fcf5fb2548eb
child 10731 67db0d431d5e
--- a/src/transparency_gui.cpp	Fri Apr 25 02:15:34 2008 +0000
+++ b/src/transparency_gui.cpp	Mon May 26 20:45:25 2008 +0000
@@ -1,5 +1,7 @@
 /* $Id$ */
 
+/** @file transparency_gui.cpp The transparency GUI. */
+
 #include "stdafx.h"
 #include "openttd.h"
 #include "gui.h"
@@ -15,82 +17,86 @@
 TransparencyOptionBits _transparency_lock;
 TransparencyOptionBits _invisibility_opt;
 
-enum TransparencyToolbarWidgets{
-	TTW_WIDGET_SIGNS = 3,    ///< Make signs background transparent
-	TTW_WIDGET_TREES,        ///< Make trees transparent
-	TTW_WIDGET_HOUSES,       ///< Make houses transparent
-	TTW_WIDGET_INDUSTRIES,   ///< Make Industries transparent
-	TTW_WIDGET_BUILDINGS,    ///< Make player buildings and structures transparent
-	TTW_WIDGET_BRIDGES,      ///< Make bridges transparent
-	TTW_WIDGET_STRUCTURES,   ///< Make unmovable structures transparent
-	TTW_WIDGET_CATENARY,     ///< Make catenary transparent
-	TTW_WIDGET_LOADING,      ///< Make loading indicators transparent
-	TTW_WIDGET_END,          ///< End of toggle buttons
-
-	/* Panel with buttons for invisibility */
-	TTW_BUTTONS = 12,        ///< Panel with 'invisibility' buttons
-};
-
-static void TransparencyToolbWndProc(Window *w, WindowEvent *e)
+class TransparenciesWindow : public Window
 {
-	switch (e->event) {
-		case WE_PAINT:
-			/* must be sure that the widgets show the transparency variable changes
-			 * also when we use shortcuts */
-			for (uint i = TTW_WIDGET_SIGNS; i < TTW_WIDGET_END; i++) {
-				w->SetWidgetLoweredState(i, IsTransparencySet((TransparencyOption)(i - TTW_WIDGET_SIGNS)));
-			}
-
-			DrawWindowWidgets(w);
-			for (uint i = TO_SIGNS; i < TO_END; i++) {
-				if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, w->widget[TTW_WIDGET_SIGNS + i].left + 1, w->widget[TTW_WIDGET_SIGNS + i].top + 1);
-			}
+	enum TransparencyToolbarWidgets{
+		TTW_WIDGET_SIGNS = 3,    ///< Make signs background transparent
+		TTW_WIDGET_TREES,        ///< Make trees transparent
+		TTW_WIDGET_HOUSES,       ///< Make houses transparent
+		TTW_WIDGET_INDUSTRIES,   ///< Make Industries transparent
+		TTW_WIDGET_BUILDINGS,    ///< Make player buildings and structures transparent
+		TTW_WIDGET_BRIDGES,      ///< Make bridges transparent
+		TTW_WIDGET_STRUCTURES,   ///< Make unmovable structures transparent
+		TTW_WIDGET_CATENARY,     ///< Make catenary transparent
+		TTW_WIDGET_LOADING,      ///< Make loading indicators transparent
+		TTW_WIDGET_END,          ///< End of toggle buttons
 
-			/* Do not draw button for invisible loading indicators */
-			for (uint i = 0; i < 8; i++) {
-				if (i < TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) {
-					DrawFrameRect(i * 22, 38, i * 22 + 19, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
-				} else if (i == TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) {
-					DrawFrameRect(i * 22, 38, i * 22 + 41, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
-				} else { // i > TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS
-					DrawFrameRect((i + 1) * 22, 38, (i + 1) * 22 + 19, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
-				}
-			}
-
-			break;
+		/* Panel with buttons for invisibility */
+		TTW_BUTTONS = 12,        ///< Panel with 'invisibility' buttons
+	};
 
-		case WE_CLICK:
-			if (e->we.click.widget >= TTW_WIDGET_SIGNS && e->we.click.widget < TTW_WIDGET_END) {
-				if (_ctrl_pressed) {
-					/* toggle the bit of the transparencies lock variable */
-					ToggleTransparencyLock((TransparencyOption)(e->we.click.widget - TTW_WIDGET_SIGNS));
-					SetWindowDirty(w);
-				} else {
-					/* toggle the bit of the transparencies variable and play a sound */
-					ToggleTransparency((TransparencyOption)(e->we.click.widget - TTW_WIDGET_SIGNS));
-					SndPlayFx(SND_15_BEEP);
-					MarkWholeScreenDirty();
-				}
-			} else if (e->we.click.widget == TTW_BUTTONS) {
-				uint x = e->we.click.pt.x / 22;
+public:
+	TransparenciesWindow(const WindowDesc *desc, int window_number) : Window(desc, window_number)
+	{
+		this->FindWindowPlacementAndResize(desc);
+	}
 
-				if (x > TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) x--;
-				if (x > TTW_WIDGET_CATENARY - TTW_WIDGET_SIGNS) break;
-
-				ToggleInvisibility((TransparencyOption)x);
-				SndPlayFx(SND_15_BEEP);
+	virtual void OnPaint()
+	{
+		/* must be sure that the widgets show the transparency variable changes
+		 * also when we use shortcuts */
+		for (uint i = TTW_WIDGET_SIGNS; i < TTW_WIDGET_END; i++) {
+			this->SetWidgetLoweredState(i, IsTransparencySet((TransparencyOption)(i - TTW_WIDGET_SIGNS)));
+		}
 
-				/* Redraw whole screen only if transparency is set */
-				if (IsTransparencySet((TransparencyOption)x)) {
-					MarkWholeScreenDirty();
-				} else {
-					w->InvalidateWidget(TTW_BUTTONS);
-				}
+		this->DrawWidgets();
+		for (uint i = TO_SIGNS; i < TO_END; i++) {
+			if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, this->widget[TTW_WIDGET_SIGNS + i].left + 1, this->widget[TTW_WIDGET_SIGNS + i].top + 1);
+		}
+
+		/* Do not draw button for invisible loading indicators */
+		for (uint i = 0; i < 8; i++) {
+			if (i < TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) {
+				DrawFrameRect(i * 22, 38, i * 22 + 19, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
+			} else if (i == TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) {
+				DrawFrameRect(i * 22, 38, i * 22 + 41, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
+			} else { // i > TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS
+				DrawFrameRect((i + 1) * 22, 38, (i + 1) * 22 + 19, 46, true, HasBit(_invisibility_opt, i) ? FR_LOWERED : FR_NONE);
 			}
+		}
+	}
 
-			break;
+	virtual void OnClick(Point pt, int widget)
+	{
+		if (widget >= TTW_WIDGET_SIGNS && widget < TTW_WIDGET_END) {
+			if (_ctrl_pressed) {
+				/* toggle the bit of the transparencies lock variable */
+				ToggleTransparencyLock((TransparencyOption)(widget - TTW_WIDGET_SIGNS));
+				this->SetDirty();
+			} else {
+				/* toggle the bit of the transparencies variable and play a sound */
+				ToggleTransparency((TransparencyOption)(widget - TTW_WIDGET_SIGNS));
+				SndPlayFx(SND_15_BEEP);
+				MarkWholeScreenDirty();
+			}
+		} else if (widget == TTW_BUTTONS) {
+			uint x = pt.x / 22;
+
+			if (x > TTW_WIDGET_BRIDGES - TTW_WIDGET_SIGNS) x--;
+			if (x > TTW_WIDGET_CATENARY - TTW_WIDGET_SIGNS) return;
+
+			ToggleInvisibility((TransparencyOption)x);
+			SndPlayFx(SND_15_BEEP);
+
+			/* Redraw whole screen only if transparency is set */
+			if (IsTransparencySet((TransparencyOption)x)) {
+				MarkWholeScreenDirty();
+			} else {
+				this->InvalidateWidget(TTW_BUTTONS);
+			}
+		}
 	}
-}
+};
 
 static const Widget _transparency_widgets[] = {
 { WWT_CLOSEBOX,   RESIZE_NONE,  7,   0,  10,   0,  13, STR_00C5,                 STR_018B_CLOSE_WINDOW},
@@ -119,10 +125,10 @@
 	WC_TRANSPARENCY_TOOLBAR, WC_NONE,
 	WDF_STD_TOOLTIPS | WDF_STD_BTN | WDF_DEF_WIDGET | WDF_STICKY_BUTTON,
 	_transparency_widgets,
-	TransparencyToolbWndProc
+	NULL
 };
 
 void ShowTransparencyToolbar(void)
 {
-	AllocateWindowDescFront(&_transparency_desc, 0);
+	AllocateWindowDescFront<TransparenciesWindow>(&_transparency_desc, 0);
 }