OpenTTD Source  20241108-master-g80f628063a
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 
26 uint8_t _display_opt;
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, WidgetID 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_TEXT: {
54  int i = widget - WID_TT_BEGIN;
55  if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + WidgetDimensions::scaled.fullbevel.left, r.top + WidgetDimensions::scaled.fullbevel.top);
56  break;
57  }
58  case WID_TT_BUTTONS: {
59  const Rect fr = r.Shrink(WidgetDimensions::scaled.framerect);
60  for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
61  if (i == WID_TT_TEXT) continue; // Loading and cost/income text has no invisibility button.
62 
63  const Rect wr = this->GetWidget<NWidgetBase>(i)->GetCurrentRect().Shrink(WidgetDimensions::scaled.fullbevel);
64  DrawFrameRect(wr.left, fr.top, wr.right, fr.bottom, COLOUR_PALE_GREEN,
66  }
67  break;
68  }
69  }
70  }
71 
72  void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
73  {
74  if (widget >= WID_TT_BEGIN && widget < WID_TT_END) {
75  if (_ctrl_pressed) {
76  /* toggle the bit of the transparencies lock variable */
78  this->SetDirty();
79  } else {
80  /* toggle the bit of the transparencies variable and play a sound */
84  }
85  } else if (widget == WID_TT_BUTTONS) {
86  uint i;
87  for (i = WID_TT_BEGIN; i < WID_TT_END; i++) {
88  const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
89  if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
90  break;
91  }
92  }
93  if (i == WID_TT_TEXT|| i == WID_TT_END) return;
94 
97 
98  /* Redraw whole screen only if transparency is set */
101  } else {
103  }
104  }
105  }
106 
107  Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
108  {
109  Point pt = GetToolbarAlignedWindowPosition(sm_width);
110  pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(WID_TT_BUTTONS)->current_y);
111  return pt;
112  }
113 
119  void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
120  {
121  if (!gui_scope) return;
122  for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
124  }
125  }
126 };
127 
128 static constexpr NWidgetPart _nested_transparency_widgets[] = {
130  NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
131  NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetDataTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
132  NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
133  EndContainer(),
135  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_SIGNS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
136  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TREES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
137  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_HOUSES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
138  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_INDUSTRIES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
139  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUILDINGS), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
140  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BRIDGES), SetMinimalSize(43, 22), SetFill(0, 1), SetDataTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
141  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_STRUCTURES), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
142  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_CATENARY), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
143  NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TEXT), SetMinimalSize(22, 22), SetFill(0, 1), SetDataTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_TEXT_TOOLTIP),
144  NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 1), EndContainer(),
145  EndContainer(),
146  /* Panel with 'invisibility' buttons. */
147  NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_TT_BUTTONS), SetMinimalSize(219, 13), SetDataTip(0x0, STR_TRANSPARENT_INVISIBLE_TOOLTIP),
148  EndContainer(),
149 };
150 
151 static WindowDesc _transparency_desc(
152  WDP_MANUAL, "toolbar_transparency", 0, 0,
154  0,
155  _nested_transparency_widgets
156 );
157 
162 {
163  AllocateWindowDescFront<TransparenciesWindow>(_transparency_desc, 0);
164 }
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Baseclass for nested widgets.
Definition: widget_type.h:144
uint current_x
Current horizontal size (after resizing).
Definition: widget_type.h:243
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition: widget_type.h:248
void OnInvalidateData([[maybe_unused]] int data=0, [[maybe_unused]] bool gui_scope=true) override
Some data on this window has become invalid.
void OnPaint() override
The window must be repainted.
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition: window_gui.h:68
RectPadding fullbevel
Always-scaled bevel thickness.
Definition: window_gui.h:41
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:38
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:988
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition: widget_type.h:1181
constexpr NWidgetPart SetDataTip(uint32_t data, StringID tip)
Widget part function for setting the data and tooltip.
Definition: widget_type.h:1202
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition: widget_type.h:1137
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=-1)
Widget part function for starting a new 'real' widget.
Definition: widget_type.h:1309
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition: widget_type.h:1191
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition: window.cpp:940
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1529
constexpr 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:252
A number of safeguards to prevent using unsafe methods.
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:56
Types related to global configuration settings.
Functions related to sound.
@ SND_15_BEEP
19 == 0x13 GUI button click
Definition: sound_type.h:58
This file contains all sprite-related enums and defines.
Definition of base types and functions in a cross-platform compatible way.
SoundSettings sound
sound effect settings
Partial widget specification to allow NWidgets to be written nested.
Definition: widget_type.h:1075
Coordinates of a point in 2D.
Specification of a rectangle with absolute coordinates of all edges.
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
bool click_beep
Beep on a random selection of buttons.
High level window description.
Definition: window_gui.h:159
Data structure for an opened window.
Definition: window_gui.h:273
void DrawWidgets() const
Paint all widgets of a window.
Definition: widget.cpp:731
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition: window.cpp:551
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition: window_gui.h:447
Window(WindowDesc &desc)
Empty constructor, initialization has been moved to InitNested() called from the constructor of the d...
Definition: window.cpp:1756
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition: window.cpp:1746
WindowNumber window_number
Window number within the window class.
Definition: window_gui.h:302
Functions related to transparency.
void ToggleTransparency(TransparencyOption to)
Toggle the transparency option bit.
Definition: transparency.h:69
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
Definition: transparency.h:22
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
void ToggleInvisibility(TransparencyOption to)
Toggle the invisibility option bit.
Definition: transparency.h:79
uint TransparencyOptionBits
transparency option bits
Definition: transparency.h:36
void ToggleTransparencyLock(TransparencyOption to)
Toggle the transparency lock bit.
Definition: transparency.h:107
TransparencyOptionBits _transparency_opt
The bits that should be transparent.
void ShowTransparencyToolbar()
Show the transparency toolbar.
TransparencyOptionBits _transparency_lock
Prevent these bits from flipping with X.
TransparencyOptionBits _invisibility_opt
The bits that should be invisible.
uint8_t _display_opt
What do we want to draw/do?
Types related to the transparency widgets.
@ WID_TT_TREES
Trees transparency toggle button.
@ WID_TT_BRIDGES
Bridges transparency toggle button.
@ WID_TT_SIGNS
Signs background transparency toggle button.
@ WID_TT_BUTTONS
Panel with 'invisibility' buttons.
@ WID_TT_END
End of toggle buttons.
@ WID_TT_BUILDINGS
Company buildings and structures transparency toggle button.
@ WID_TT_BEGIN
First toggle button.
@ WID_TT_INDUSTRIES
industries transparency toggle button.
@ WID_TT_CATENARY
Catenary transparency toggle button.
@ WID_TT_HOUSES
Houses transparency toggle button.
@ WID_TT_TEXT
Loading and cost/income text transparency toggle button.
@ WID_TT_STRUCTURES
Object structure transparency toggle button.
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition: widget.cpp:281
@ WWT_IMGBTN
(Toggle) Button with image
Definition: widget_type.h:52
@ NWID_HORIZONTAL
Horizontal container.
Definition: widget_type.h:75
@ WWT_PANEL
Simple depressed panel.
Definition: widget_type.h:50
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition: widget_type.h:66
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition: widget_type.h:61
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition: widget_type.h:69
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:1627
Functions, definitions and such used only by the GUI.
@ FR_LOWERED
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
Definition: window_gui.h:28
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition: window_gui.h:146
int WidgetID
Widget ID.
Definition: window_type.h:18
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition: window_type.h:45
@ WC_TRANSPARENCY_TOOLBAR
Transparency toolbar; Window numbers:
Definition: window_type.h:92