# transparency_gui.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "window_gui.h"
    00014 #include "transparency.h"
    00015 #include "sound_func.h"
    00016
    00017 #include "table/sprites.h"
    00018 #include "table/strings.h"
    00019
    00020 TransparencyOptionBits _transparency_opt;
    00021 TransparencyOptionBits _transparency_lock;
    00022 TransparencyOptionBits _invisibility_opt;
    00023 byte _display_opt;
    00024
    00026 enum TransparencyToolbarWidgets {
    00027   /* Button row. */
    00028   TTW_WIDGET_BEGIN,
    00029   TTW_WIDGET_SIGNS = TTW_WIDGET_BEGIN,
    00030   TTW_WIDGET_TREES,
    00031   TTW_WIDGET_HOUSES,
    00032   TTW_WIDGET_INDUSTRIES,
    00033   TTW_WIDGET_BUILDINGS,
    00034   TTW_WIDGET_BRIDGES,
    00035   TTW_WIDGET_STRUCTURES,
    00036   TTW_WIDGET_CATENARY,
    00037   TTW_WIDGET_LOADING,
    00038   TTW_WIDGET_END,
    00039
    00040   /* Panel with buttons for invisibility */
    00041   TTW_WIDGET_BUTTONS,
    00042 };
    00043
    00044 class TransparenciesWindow : public Window
    00045 {
    00046 public:
    00047   TransparenciesWindow(const WindowDesc *desc, int window_number) : Window()
    00048   {
    00049     this->InitNested(desc, window_number);
    00050   }
    00051
    00052   virtual void OnPaint()
    00053   {
    00054     OnInvalidateData(0); // Must be sure that the widgets show the transparency variable changes, also when we use shortcuts.
    00055     this->DrawWidgets();
    00056   }
    00057
    00058   virtual void DrawWidget(const Rect &r, int widget) const
    00059   {
    00060     switch (widget) {
    00061       case TTW_WIDGET_SIGNS:
    00062       case TTW_WIDGET_TREES:
    00063       case TTW_WIDGET_HOUSES:
    00064       case TTW_WIDGET_INDUSTRIES:
    00065       case TTW_WIDGET_BUILDINGS:
    00066       case TTW_WIDGET_BRIDGES:
    00067       case TTW_WIDGET_STRUCTURES:
    00068       case TTW_WIDGET_CATENARY:
    00069       case TTW_WIDGET_LOADING: {
    00070         uint i = widget - TTW_WIDGET_BEGIN;
    00071         if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + 1, r.top + 1);
    00072         break;
    00073       }
    00074       case TTW_WIDGET_BUTTONS:
    00075         for (uint i = TTW_WIDGET_BEGIN; i < TTW_WIDGET_END; i++) {
    00076           if (i == TTW_WIDGET_LOADING) continue; // Do not draw button for invisible loading indicators.
    00077
    00078           const NWidgetBase *wi = this->GetWidget<NWidgetBase>(i);
    00079           DrawFrameRect(wi->pos_x + 1, r.top + 2, wi->pos_x + wi->current_x - 2, r.bottom - 2, COLOUR_PALE_GREEN,
    00080               HasBit(_invisibility_opt, i - TTW_WIDGET_BEGIN) ? FR_LOWERED : FR_NONE);
    00081         }
    00082         break;
    00083     }
    00084   }
    00085
    00086   virtual void OnClick(Point pt, int widget, int click_count)
    00087   {
    00088     if (widget >= TTW_WIDGET_BEGIN && widget < TTW_WIDGET_END) {
    00089       if (_ctrl_pressed) {
    00090         /* toggle the bit of the transparencies lock variable */
    00091         ToggleTransparencyLock((TransparencyOption)(widget - TTW_WIDGET_BEGIN));
    00092         this->SetDirty();
    00093       } else {
    00094         /* toggle the bit of the transparencies variable and play a sound */
    00095         ToggleTransparency((TransparencyOption)(widget - TTW_WIDGET_BEGIN));
    00096         SndPlayFx(SND_15_BEEP);
    00097         MarkWholeScreenDirty();
    00098       }
    00099     } else if (widget == TTW_WIDGET_BUTTONS) {
    00100       uint i;
    00101       for (i = TTW_WIDGET_BEGIN; i < TTW_WIDGET_END; i++) {
    00102         const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
    00103         if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
    00104           break;
    00105         }
    00106       }
    00107       if (i == TTW_WIDGET_LOADING || i == TTW_WIDGET_END) return;
    00108
    00109       ToggleInvisibility((TransparencyOption)(i - TTW_WIDGET_BEGIN));
    00110       SndPlayFx(SND_15_BEEP);
    00111
    00112       /* Redraw whole screen only if transparency is set */
    00113       if (IsTransparencySet((TransparencyOption)(i - TTW_WIDGET_BEGIN))) {
    00114         MarkWholeScreenDirty();
    00115       } else {
    00116         this->SetWidgetDirty(TTW_WIDGET_BUTTONS);
    00117       }
    00118     }
    00119   }
    00120
    00121   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00122   {
    00123     Point pt = GetToolbarAlignedWindowPosition(sm_width);
    00124     pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(TTW_WIDGET_BUTTONS)->current_y);
    00125     return pt;
    00126   }
    00127
    00133   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00134   {
    00135     if (!gui_scope) return;
    00136     for (uint i = TTW_WIDGET_BEGIN; i < TTW_WIDGET_END; i++) {
    00137       this->SetWidgetLoweredState(i, IsTransparencySet((TransparencyOption)(i - TTW_WIDGET_BEGIN)));
    00138     }
    00139   }
    00140 };
    00141
    00142 static const NWidgetPart _nested_transparency_widgets[] = {
    00143   NWidget(NWID_HORIZONTAL),
    00144     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00145     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00146     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00147   EndContainer(),
    00148   NWidget(NWID_HORIZONTAL),
    00149     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_SIGNS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
    00150     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_TREES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
    00151     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_HOUSES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
    00152     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_INDUSTRIES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
    00153     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_BUILDINGS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
    00154     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_BRIDGES), SetMinimalSize(43, 22), SetFill(0, 1), SetDataTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
    00155     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_STRUCTURES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
    00156     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_CATENARY), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
    00157     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, TTW_WIDGET_LOADING), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_LOADING_TOOLTIP),
    00158     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 1), EndContainer(),
    00159   EndContainer(),
    00160   /* Panel with 'inivisibility' buttons. */
    00161   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, TTW_WIDGET_BUTTONS), SetMinimalSize(219, 13), SetDataTip(0x0, STR_TRANSPARENT_INVISIBLE_TOOLTIP),
    00162   EndContainer(),
    00163 };
    00164
    00165 static const WindowDesc _transparency_desc(
    00166   WDP_MANUAL, 0, 0,
    00167   WC_TRANSPARENCY_TOOLBAR, WC_NONE,
    00168   0,
    00169   _nested_transparency_widgets, lengthof(_nested_transparency_widgets)
    00170 );
    00171
    00172 void ShowTransparencyToolbar()
    00173 {
    00174   AllocateWindowDescFront<TransparenciesWindow>(&_transparency_desc, 0);
    00175 }