OpenTTD Source  1.11.2
transparency_gui.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "window_gui.h"
12 #include "transparency.h"
13 #include "sound_func.h"
14 #include "settings_type.h"
15 
17 
18 #include "table/sprites.h"
19 #include "table/strings.h"
20 
21 #include "safeguards.h"
22 
27 
29 {
30 public:
32  {
33  this->InitNested(window_number);
34  }
35 
36  void OnPaint() override
37  {
38  this->OnInvalidateData(0); // Must be sure that the widgets show the transparency variable changes, also when we use shortcuts.
39  this->DrawWidgets();
40  }
41 
42  void DrawWidget(const Rect &r, int widget) const override
43  {
44  switch (widget) {
45  case WID_TT_SIGNS:
46  case WID_TT_TREES:
47  case WID_TT_HOUSES:
48  case WID_TT_INDUSTRIES:
49  case WID_TT_BUILDINGS:
50  case WID_TT_BRIDGES:
51  case WID_TT_STRUCTURES:
52  case WID_TT_CATENARY:
53  case WID_TT_LOADING: {
54  uint i = widget - WID_TT_BEGIN;
55  if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + 1, r.top + 1);
56  break;
57  }
58  case WID_TT_BUTTONS:
59  for (uint i = WID_TT_BEGIN; i < WID_TT_END; i++) {
60  if (i == WID_TT_LOADING) continue; // Do not draw button for invisible loading indicators.
61 
62  const NWidgetBase *wi = this->GetWidget<NWidgetBase>(i);
63  DrawFrameRect(wi->pos_x + 1, r.top + 2, wi->pos_x + wi->current_x - 2, r.bottom - 2, COLOUR_PALE_GREEN,
65  }
66  break;
67  }
68  }
69 
70  void OnClick(Point pt, int widget, int click_count) override
71  {
72  if (widget >= WID_TT_BEGIN && widget < WID_TT_END) {
73  if (_ctrl_pressed) {
74  /* toggle the bit of the transparencies lock variable */
76  this->SetDirty();
77  } else {
78  /* toggle the bit of the transparencies variable and play a sound */
82  }
83  } else if (widget == WID_TT_BUTTONS) {
84  uint i;
85  for (i = WID_TT_BEGIN; i < WID_TT_END; i++) {
86  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
87  if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
88  break;
89  }
90  }
91  if (i == WID_TT_LOADING || i == WID_TT_END) return;
92 
95 
96  /* Redraw whole screen only if transparency is set */
99  } else {
101  }
102  }
103  }
104 
105  Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
106  {
107  Point pt = GetToolbarAlignedWindowPosition(sm_width);
108  pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(WID_TT_BUTTONS)->current_y);
109  return pt;
110  }
111 
117  void OnInvalidateData(int data = 0, bool gui_scope = true) override
118  {
119  if (!gui_scope) return;
120  for (uint i = WID_TT_BEGIN; i < WID_TT_END; i++) {
122  }
123  }
124 };
125 
126 static const NWidgetPart _nested_transparency_widgets[] = {
128  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
129  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
130  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
131  EndContainer(),
133  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_SIGNS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
134  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TREES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
135  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_HOUSES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
136  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_INDUSTRIES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
137  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUILDINGS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
138  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BRIDGES), SetMinimalSize(43, 22), SetFill(0, 1), SetDataTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
139  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_STRUCTURES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
140  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_CATENARY), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
141  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_LOADING), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_LOADING_TOOLTIP),
142  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 1), EndContainer(),
143  EndContainer(),
144  /* Panel with 'invisibility' buttons. */
145  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_TT_BUTTONS), SetMinimalSize(219, 13), SetDataTip(0x0, STR_TRANSPARENT_INVISIBLE_TOOLTIP),
146  EndContainer(),
147 };
148 
149 static WindowDesc _transparency_desc(
150  WDP_MANUAL, "toolbar_transparency", 0, 0,
152  0,
153  _nested_transparency_widgets, lengthof(_nested_transparency_widgets)
154 );
155 
160 {
161  AllocateWindowDescFront<TransparenciesWindow>(&_transparency_desc, 0);
162 }
WID_TT_LOADING
@ WID_TT_LOADING
Loading indicators transparency toggle button.
Definition: transparency_widget.h:25
sound_func.h
WC_TRANSPARENCY_TOOLBAR
@ WC_TRANSPARENCY_TOOLBAR
Transparency toolbar; Window numbers:
Definition: window_type.h:85
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:64
IsTransparencySet
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
Definition: transparency.h:48
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:59
WWT_IMGBTN
@ WWT_IMGBTN
(Toggle) Button with image
Definition: widget_type.h:50
WID_TT_END
@ WID_TT_END
End of toggle buttons.
Definition: transparency_widget.h:26
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:73
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
SND_15_BEEP
@ SND_15_BEEP
19 == 0x13 GUI button click
Definition: sound_type.h:58
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
TransparenciesWindow
Definition: transparency_gui.cpp:28
FR_LOWERED
@ FR_LOWERED
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
Definition: window_gui.h:29
Window::Window
Window(WindowDesc *desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1871
NWidgetPart
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:919
SetDataTip
static NWidgetPart SetDataTip(uint32 data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1023
WID_TT_BUTTONS
@ WID_TT_BUTTONS
Panel with 'invisibility' buttons.
Definition: transparency_widget.h:29
WID_TT_HOUSES
@ WID_TT_HOUSES
Houses transparency toggle button.
Definition: transparency_widget.h:19
WindowDesc
High level window description.
Definition: window_gui.h:166
WID_TT_TREES
@ WID_TT_TREES
Trees transparency toggle button.
Definition: transparency_widget.h:18
WID_TT_BEGIN
@ WID_TT_BEGIN
First toggle button.
Definition: transparency_widget.h:16
window_gui.h
TransparenciesWindow::OnClick
void OnClick(Point pt, int widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition: transparency_gui.cpp:70
ClientSettings::sound
SoundSettings sound
sound effect settings
Definition: settings_type.h:584
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1861
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:984
IsInsideBS
static bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
Definition: math_func.hpp:188
NWidgetBase
Baseclass for nested widgets.
Definition: widget_type.h:124
TransparenciesWindow::OnInitialPosition
Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) override
Compute the initial position of the window.
Definition: transparency_gui.cpp:105
_invisibility_opt
TransparencyOptionBits _invisibility_opt
The bits that should be invisible.
Definition: transparency_gui.cpp:25
TransparencyOptionBits
uint TransparencyOptionBits
transparency option bits
Definition: transparency.h:36
safeguards.h
GetToolbarAlignedWindowPosition
Point GetToolbarAlignedWindowPosition(int window_width)
Computer the position of the top-left corner of a window to be opened right under the toolbar.
Definition: window.cpp:1736
WID_TT_CATENARY
@ WID_TT_CATENARY
Catenary transparency toggle button.
Definition: transparency_widget.h:24
WID_TT_SIGNS
@ WID_TT_SIGNS
Signs background transparency toggle button.
Definition: transparency_widget.h:17
WID_TT_BUILDINGS
@ WID_TT_BUILDINGS
Company buildings and structures transparency toggle button.
Definition: transparency_widget.h:21
TransparenciesWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition: transparency_gui.cpp:36
DrawSprite
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:978
settings_type.h
sprites.h
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
ToggleInvisibility
static void ToggleInvisibility(TransparencyOption to)
Toggle the invisibility option bit.
Definition: transparency.h:79
stdafx.h
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:313
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:38
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:67
WID_TT_BRIDGES
@ WID_TT_BRIDGES
Bridges transparency toggle button.
Definition: transparency_widget.h:22
TransparencyOption
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
Definition: transparency.h:22
EndContainer
static NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1008
TransparenciesWindow::DrawWidget
void DrawWidget(const Rect &r, int widget) const override
Draw the contents of a nested widget.
Definition: transparency_gui.cpp:42
NWidgetBase::pos_x
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:185
NWidget
static NWidgetPart NWidget(WidgetType tp, Colours col, int16 idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1123
ShowTransparencyToolbar
void ShowTransparencyToolbar()
Show the transparency toolbar.
Definition: transparency_gui.cpp:159
SetMinimalSize
static NWidgetPart SetMinimalSize(int16 x, int16 y)
Widget part function for setting the minimal size.
Definition: widget_type.h:956
transparency.h
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:48
_transparency_opt
TransparencyOptionBits _transparency_opt
The bits that should be transparent.
Definition: transparency_gui.cpp:23
WID_TT_INDUSTRIES
@ WID_TT_INDUSTRIES
industries transparency toggle button.
Definition: transparency_widget.h:20
_transparency_lock
TransparencyOptionBits _transparency_lock
Prevent these bits from flipping with X.
Definition: transparency_gui.cpp:24
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:175
_display_opt
byte _display_opt
What do we want to draw/do?
Definition: transparency_gui.cpp:26
SoundSettings::click_beep
bool click_beep
Beep on a random selection of buttons.
Definition: settings_type.h:198
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:369
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1597
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition: window_gui.h:153
ToggleTransparency
static void ToggleTransparency(TransparencyOption to)
Toggle the transparency option bit.
Definition: transparency.h:69
SetFill
static NWidgetPart SetFill(uint fill_x, uint fill_y)
Widget part function for setting filling.
Definition: widget_type.h:992
ToggleTransparencyLock
static void ToggleTransparencyLock(TransparencyOption to)
Toggle the transparency lock bit.
Definition: transparency.h:107
Window
Data structure for an opened window.
Definition: window_gui.h:277
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:602
WID_TT_STRUCTURES
@ WID_TT_STRUCTURES
Object structure transparency toggle button.
Definition: transparency_widget.h:23
TransparenciesWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition: transparency_gui.cpp:117
Window::SetWidgetDirty
void SetWidgetDirty(byte widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:597
transparency_widget.h
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
NWidgetBase::current_x
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:182
Window::SetWidgetLoweredState
void SetWidgetLoweredState(byte widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:454