# 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 #include "settings_type.h"
    00017
    00018 #include "widgets/transparency_widget.h"
    00019
    00020 #include "table/sprites.h"
    00021 #include "table/strings.h"
    00022
    00023 TransparencyOptionBits _transparency_opt;
    00024 TransparencyOptionBits _transparency_lock;
    00025 TransparencyOptionBits _invisibility_opt;
    00026 byte _display_opt;
    00027
    00028 class TransparenciesWindow : public Window
    00029 {
    00030 public:
    00031   TransparenciesWindow(const WindowDesc *desc, int window_number) : Window()
    00032   {
    00033     this->InitNested(desc, window_number);
    00034   }
    00035
    00036   virtual void OnPaint()
    00037   {
    00038     this->OnInvalidateData(0); // Must be sure that the widgets show the transparency variable changes, also when we use shortcuts.
    00039     this->DrawWidgets();
    00040   }
    00041
    00042   virtual void DrawWidget(const Rect &r, int widget) const
    00043   {
    00044     switch (widget) {
    00045       case WID_TT_SIGNS:
    00046       case WID_TT_TREES:
    00047       case WID_TT_HOUSES:
    00048       case WID_TT_INDUSTRIES:
    00049       case WID_TT_BUILDINGS:
    00050       case WID_TT_BRIDGES:
    00051       case WID_TT_STRUCTURES:
    00052       case WID_TT_CATENARY:
    00053       case WID_TT_LOADING: {
    00054         uint i = widget - WID_TT_BEGIN;
    00055         if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + 1, r.top + 1);
    00056         break;
    00057       }
    00058       case WID_TT_BUTTONS:
    00059         for (uint i = WID_TT_BEGIN; i < WID_TT_END; i++) {
    00060           if (i == WID_TT_LOADING) continue; // Do not draw button for invisible loading indicators.
    00061
    00062           const NWidgetBase *wi = this->GetWidget<NWidgetBase>(i);
    00063           DrawFrameRect(wi->pos_x + 1, r.top + 2, wi->pos_x + wi->current_x - 2, r.bottom - 2, COLOUR_PALE_GREEN,
    00064               HasBit(_invisibility_opt, i - WID_TT_BEGIN) ? FR_LOWERED : FR_NONE);
    00065         }
    00066         break;
    00067     }
    00068   }
    00069
    00070   virtual void OnClick(Point pt, int widget, int click_count)
    00071   {
    00072     if (widget >= WID_TT_BEGIN && widget < WID_TT_END) {
    00073       if (_ctrl_pressed) {
    00074         /* toggle the bit of the transparencies lock variable */
    00075         ToggleTransparencyLock((TransparencyOption)(widget - WID_TT_BEGIN));
    00076         this->SetDirty();
    00077       } else {
    00078         /* toggle the bit of the transparencies variable and play a sound */
    00079         ToggleTransparency((TransparencyOption)(widget - WID_TT_BEGIN));
    00080         if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
    00081         MarkWholeScreenDirty();
    00082       }
    00083     } else if (widget == WID_TT_BUTTONS) {
    00084       uint i;
    00085       for (i = WID_TT_BEGIN; i < WID_TT_END; i++) {
    00086         const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
    00087         if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
    00088           break;
    00089         }
    00090       }
    00091       if (i == WID_TT_LOADING || i == WID_TT_END) return;
    00092
    00093       ToggleInvisibility((TransparencyOption)(i - WID_TT_BEGIN));
    00094       if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
    00095
    00096       /* Redraw whole screen only if transparency is set */
    00097       if (IsTransparencySet((TransparencyOption)(i - WID_TT_BEGIN))) {
    00098         MarkWholeScreenDirty();
    00099       } else {
    00100         this->SetWidgetDirty(WID_TT_BUTTONS);
    00101       }
    00102     }
    00103   }
    00104
    00105   virtual Point OnInitialPosition(const WindowDesc *desc, int16 sm_width, int16 sm_height, int window_number)
    00106   {
    00107     Point pt = GetToolbarAlignedWindowPosition(sm_width);
    00108     pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(WID_TT_BUTTONS)->current_y);
    00109     return pt;
    00110   }
    00111
    00117   virtual void OnInvalidateData(int data = 0, bool gui_scope = true)
    00118   {
    00119     if (!gui_scope) return;
    00120     for (uint i = WID_TT_BEGIN; i < WID_TT_END; i++) {
    00121       this->SetWidgetLoweredState(i, IsTransparencySet((TransparencyOption)(i - WID_TT_BEGIN)));
    00122     }
    00123   }
    00124 };
    00125
    00126 static const NWidgetPart _nested_transparency_widgets[] = {
    00127   NWidget(NWID_HORIZONTAL),
    00128     NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
    00129     NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
    00130     NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
    00131   EndContainer(),
    00132   NWidget(NWID_HORIZONTAL),
    00133     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_SIGNS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
    00134     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TREES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
    00135     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_HOUSES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
    00136     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_INDUSTRIES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
    00137     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUILDINGS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
    00138     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BRIDGES), SetMinimalSize(43, 22), SetFill(0, 1), SetDataTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
    00139     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_STRUCTURES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
    00140     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_CATENARY), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
    00141     NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOADING), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_LOADING_TOOLTIP),
    00142     NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 1), EndContainer(),
    00143   EndContainer(),
    00144   /* Panel with 'invisibility' buttons. */
    00145   NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_TT_BUTTONS), SetMinimalSize(219, 13), SetDataTip(0x0, STR_TRANSPARENT_INVISIBLE_TOOLTIP),
    00146   EndContainer(),
    00147 };
    00148
    00149 static const WindowDesc _transparency_desc(
    00150   WDP_MANUAL, 0, 0,
    00151   WC_TRANSPARENCY_TOOLBAR, WC_NONE,
    00152   0,
    00153   _nested_transparency_widgets, lengthof(_nested_transparency_widgets)
    00154 );
    00155
    00159 void ShowTransparencyToolbar()
    00160 {
    00161   AllocateWindowDescFront<TransparenciesWindow>(&_transparency_desc, 0);
    00162 }