OpenTTD Source: src/transparency_gui.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
16#include "widgets/transparency_widget.h"
17
18#include "table/sprites.h"
19#include "table/strings.h"
20
21#include "safeguards.h"
22
23 TransparencyOptionBits _transparency_opt;
24 TransparencyOptionBits _transparency_lock;
25 TransparencyOptionBits _invisibility_opt;
26uint8_t _display_opt;
27 StationFacilities _facility_display_opt;
28
29 class TransparenciesWindow : public Window
30{
31public:
32 TransparenciesWindow(WindowDesc &desc, int window_number) : Window(desc)
33 {
34 this->InitNested(window_number);
35 }
36
37 void OnPaint() override
38 {
39 this->OnInvalidateData(0); // Must be sure that the widgets show the transparency variable changes, also when we use shortcuts.
40 this->DrawWidgets();
41 }
42
43 void DrawWidget(const Rect &r, WidgetID widget) const override
44 {
45 switch (widget) {
46 case WID_TT_SIGNS:
47 case WID_TT_TREES:
48 case WID_TT_HOUSES:
49 case WID_TT_INDUSTRIES:
50 case WID_TT_BUILDINGS:
51 case WID_TT_BRIDGES:
52 case WID_TT_STRUCTURES:
53 case WID_TT_CATENARY:
54 case WID_TT_TEXT: {
55 int i = widget - WID_TT_BEGIN;
56 if (HasBit(_transparency_lock, i)) DrawSprite(SPR_LOCK, PAL_NONE, r.left + WidgetDimensions::scaled.fullbevel.left, r.top + WidgetDimensions::scaled.fullbevel.top);
57 break;
58 }
59 case WID_TT_BUTTONS: {
60 const Rect fr = r.Shrink(WidgetDimensions::scaled.framerect);
61 for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
62 if (i == WID_TT_TEXT) continue; // Loading and cost/income text has no invisibility button.
63
64 const Rect wr = this->GetWidget<NWidgetBase>(i)->GetCurrentRect().Shrink(WidgetDimensions::scaled.fullbevel);
65 DrawFrameRect(wr.WithY(fr), COLOUR_PALE_GREEN,
66 HasBit(_invisibility_opt, i - WID_TT_BEGIN) ? FrameFlag::Lowered : FrameFlags{});
67 }
68 break;
69 }
70 }
71 }
72
73 void OnClick([[maybe_unused]] Point pt, WidgetID widget, [[maybe_unused]] int click_count) override
74 {
75 if (widget >= WID_TT_BEGIN && widget < WID_TT_END) {
76 if (_ctrl_pressed) {
77 /* toggle the bit of the transparencies lock variable */
78 ToggleTransparencyLock((TransparencyOption)(widget - WID_TT_BEGIN));
79 this->SetDirty();
80 } else {
81 /* toggle the bit of the transparencies variable and play a sound */
82 ToggleTransparency((TransparencyOption)(widget - WID_TT_BEGIN));
83 SndClickBeep();
84 MarkWholeScreenDirty();
85 }
86 } else if (widget == WID_TT_BUTTONS) {
87 uint i;
88 for (i = WID_TT_BEGIN; i < WID_TT_END; i++) {
89 const NWidgetBase *nwid = this->GetWidget<NWidgetBase>(i);
90 if (IsInsideBS(pt.x, nwid->pos_x, nwid->current_x)) {
91 break;
92 }
93 }
94 if (i == WID_TT_TEXT|| i == WID_TT_END) return;
95
96 ToggleInvisibility((TransparencyOption)(i - WID_TT_BEGIN));
97 SndClickBeep();
98
99 /* Redraw whole screen only if transparency is set */
100 if (IsTransparencySet((TransparencyOption)(i - WID_TT_BEGIN))) {
101 MarkWholeScreenDirty();
102 } else {
103 this->SetWidgetDirty(WID_TT_BUTTONS);
104 }
105 }
106 }
107
108 Point OnInitialPosition([[maybe_unused]] int16_t sm_width, [[maybe_unused]] int16_t sm_height, [[maybe_unused]] int window_number) override
109 {
110 Point pt = GetToolbarAlignedWindowPosition(sm_width);
111 pt.y += 2 * (sm_height - this->GetWidget<NWidgetBase>(WID_TT_BUTTONS)->current_y);
112 return pt;
113 }
114
120 void OnInvalidateData([[maybe_unused]] int data = 0, [[maybe_unused]] bool gui_scope = true) override
121 {
122 if (!gui_scope) return;
123 for (WidgetID i = WID_TT_BEGIN; i < WID_TT_END; i++) {
124 this->SetWidgetLoweredState(i, IsTransparencySet((TransparencyOption)(i - WID_TT_BEGIN)));
125 }
126 }
127};
128
129static constexpr std::initializer_list<NWidgetPart> _nested_transparency_widgets = {
130 NWidget(NWID_HORIZONTAL),
131 NWidget(WWT_CLOSEBOX, COLOUR_DARK_GREEN),
132 NWidget(WWT_CAPTION, COLOUR_DARK_GREEN), SetStringTip(STR_TRANSPARENCY_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
133 NWidget(WWT_STICKYBOX, COLOUR_DARK_GREEN),
134 EndContainer(),
135 NWidget(NWID_HORIZONTAL),
136 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_SIGNS), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_SIGN, STR_TRANSPARENT_SIGNS_TOOLTIP),
137 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TREES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_PLANTTREES, STR_TRANSPARENT_TREES_TOOLTIP),
138 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_HOUSES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TOWN, STR_TRANSPARENT_HOUSES_TOOLTIP),
139 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_INDUSTRIES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_INDUSTRY, STR_TRANSPARENT_INDUSTRIES_TOOLTIP),
140 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BUILDINGS), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_COMPANY_LIST, STR_TRANSPARENT_BUILDINGS_TOOLTIP),
141 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_BRIDGES), SetToolbarMinimalSize(2), SetFill(0, 1), SetSpriteTip(SPR_IMG_BRIDGE, STR_TRANSPARENT_BRIDGES_TOOLTIP),
142 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_STRUCTURES), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TRANSMITTER, STR_TRANSPARENT_STRUCTURES_TOOLTIP),
143 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_CATENARY), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_BUILD_X_ELRAIL, STR_TRANSPARENT_CATENARY_TOOLTIP),
144 NWidget(WWT_IMGBTN, COLOUR_DARK_GREEN, WID_TT_TEXT), SetToolbarMinimalSize(1), SetFill(0, 1), SetSpriteTip(SPR_IMG_TRAINLIST, STR_TRANSPARENT_TEXT_TOOLTIP),
145 NWidget(WWT_PANEL, COLOUR_DARK_GREEN), SetFill(1, 1), EndContainer(),
146 EndContainer(),
147 /* Panel with 'invisibility' buttons. */
148 NWidget(WWT_PANEL, COLOUR_DARK_GREEN, WID_TT_BUTTONS), SetMinimalSize(219, 13), SetToolTip(STR_TRANSPARENT_INVISIBLE_TOOLTIP),
149 EndContainer(),
150};
151
152static WindowDesc _transparency_desc(
153 WDP_MANUAL, "toolbar_transparency", 0, 0,
154 WC_TRANSPARENCY_TOOLBAR, WC_NONE,
155 {},
156 _nested_transparency_widgets
157);
158
162 void ShowTransparencyToolbar()
163{
164 AllocateWindowDescFront<TransparenciesWindow>(_transparency_desc, 0);
165}
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
EnumBitSet< StationFacility, uint8_t >
NWidgetBase
Baseclass for nested widgets.
Definition widget_type.h:137
NWidgetBase::current_x
uint current_x
Current horizontal size (after resizing).
Definition widget_type.h:238
NWidgetBase::pos_x
int pos_x
Horizontal position of top-left corner of the widget in the window.
Definition widget_type.h:243
TransparenciesWindow
Definition transparency_gui.cpp:30
TransparenciesWindow::OnInitialPosition
Point OnInitialPosition(int16_t sm_width, int16_t sm_height, int window_number) override
Compute the initial position of the window.
Definition transparency_gui.cpp:108
TransparenciesWindow::OnInvalidateData
void OnInvalidateData(int data=0, bool gui_scope=true) override
Some data on this window has become invalid.
Definition transparency_gui.cpp:120
TransparenciesWindow::OnClick
void OnClick(Point pt, WidgetID widget, int click_count) override
A click with the left mouse button has been made on the window.
Definition transparency_gui.cpp:73
TransparenciesWindow::OnPaint
void OnPaint() override
The window must be repainted.
Definition transparency_gui.cpp:37
TransparenciesWindow::DrawWidget
void DrawWidget(const Rect &r, WidgetID widget) const override
Draw the contents of a nested widget.
Definition transparency_gui.cpp:43
WidgetDimensions::scaled
static WidgetDimensions scaled
Widget dimensions scaled for current zoom level.
Definition window_gui.h:30
WidgetDimensions::fullbevel
RectPadding fullbevel
Always-scaled bevel thickness.
Definition window_gui.h:39
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
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:1034
SetFill
constexpr NWidgetPart SetFill(uint16_t fill_x, uint16_t fill_y)
Widget part function for setting filling.
Definition widget_type.h:1222
SetSpriteTip
constexpr NWidgetPart SetSpriteTip(SpriteID sprite, StringID tip={})
Widget part function for setting the sprite and tooltip.
Definition widget_type.h:1254
SetToolbarMinimalSize
constexpr NWidgetPart SetToolbarMinimalSize(int width)
Widget part function to setting the minimal size for a toolbar button.
Definition widget_type.h:1178
SetStringTip
constexpr NWidgetPart SetStringTip(StringID string, StringID tip={})
Widget part function for setting the string and tooltip.
Definition widget_type.h:1243
SetMinimalSize
constexpr NWidgetPart SetMinimalSize(int16_t x, int16_t y)
Widget part function for setting the minimal size.
Definition widget_type.h:1159
SetToolTip
constexpr NWidgetPart SetToolTip(StringID tip)
Widget part function for setting tooltip and clearing the widget data.
Definition widget_type.h:1321
EndContainer
constexpr NWidgetPart EndContainer()
Widget part function for denoting the end of a container (horizontal, vertical, WWT_FRAME,...
Definition widget_type.h:1232
NWidget
constexpr NWidgetPart NWidget(WidgetType tp, Colours col, WidgetID idx=INVALID_WIDGET)
Widget part function for starting a new 'real' widget.
Definition widget_type.h:1427
Window::SetDirty
void SetDirty() const
Mark entire window as dirty (in need of re-paint)
Definition window.cpp:968
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1549
IsInsideBS
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
safeguards.h
A number of safeguards to prevent using unsafe methods.
settings_type.h
Types related to global configuration settings.
SndClickBeep
void SndClickBeep()
Play a beep sound for a click event if enabled in settings.
Definition sound.cpp:253
sound_func.h
Functions related to sound.
sprites.h
This file contains all sprite-related enums and defines.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Shrink
Rect Shrink(int s) const
Copy and shrink Rect by s pixels.
Definition geometry_type.hpp:124
Rect::WithY
Rect WithY(int new_top, int new_bottom) const
Create a new Rect, replacing the top and bottom coordiates.
Definition geometry_type.hpp:280
WindowDesc
High level window description.
Definition window_gui.h:168
Window
Data structure for an opened window.
Definition window_gui.h:274
Window::DrawWidgets
void DrawWidgets() const
Paint all widgets of a window.
Definition widget.cpp:784
Window::SetWidgetDirty
void SetWidgetDirty(WidgetID widget_index) const
Invalidate a widget, i.e.
Definition window.cpp:558
Window::SetWidgetLoweredState
void SetWidgetLoweredState(WidgetID widget_index, bool lowered_stat)
Sets the lowered/raised status of a widget.
Definition window_gui.h:442
Window::InitNested
void InitNested(WindowNumber number=0)
Perform complete initialization of the Window with nested widgets, to allow use.
Definition window.cpp:1823
Window::window_number
WindowNumber window_number
Window number within the window class.
Definition window_gui.h:303
transparency.h
Functions related to transparency.
ToggleTransparency
void ToggleTransparency(TransparencyOption to)
Toggle the transparency option bit.
Definition transparency.h:71
IsTransparencySet
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:50
ToggleInvisibility
void ToggleInvisibility(TransparencyOption to)
Toggle the invisibility option bit.
Definition transparency.h:81
TransparencyOptionBits
uint TransparencyOptionBits
transparency option bits
Definition transparency.h:37
TransparencyOption
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
Definition transparency.h:23
ToggleTransparencyLock
void ToggleTransparencyLock(TransparencyOption to)
Toggle the transparency lock bit.
Definition transparency.h:109
_transparency_opt
TransparencyOptionBits _transparency_opt
The bits that should be transparent.
Definition transparency_gui.cpp:23
_facility_display_opt
StationFacilities _facility_display_opt
What station facilities to draw.
Definition transparency_gui.cpp:27
ShowTransparencyToolbar
void ShowTransparencyToolbar()
Show the transparency toolbar.
Definition transparency_gui.cpp:162
_transparency_lock
TransparencyOptionBits _transparency_lock
Prevent these bits from flipping with X.
Definition transparency_gui.cpp:24
_invisibility_opt
TransparencyOptionBits _invisibility_opt
The bits that should be invisible.
Definition transparency_gui.cpp:25
_display_opt
uint8_t _display_opt
What do we want to draw/do?
Definition transparency_gui.cpp:26
transparency_widget.h
Types related to the transparency widgets.
WID_TT_TREES
@ WID_TT_TREES
Trees transparency toggle button.
Definition transparency_widget.h:18
WID_TT_BRIDGES
@ WID_TT_BRIDGES
Bridges transparency toggle button.
Definition transparency_widget.h:22
WID_TT_SIGNS
@ WID_TT_SIGNS
Signs background transparency toggle button.
Definition transparency_widget.h:17
WID_TT_BUTTONS
@ WID_TT_BUTTONS
Panel with 'invisibility' buttons.
Definition transparency_widget.h:29
WID_TT_END
@ WID_TT_END
End of toggle buttons.
Definition transparency_widget.h:26
WID_TT_BUILDINGS
@ WID_TT_BUILDINGS
Company buildings and structures transparency toggle button.
Definition transparency_widget.h:21
WID_TT_BEGIN
@ WID_TT_BEGIN
First toggle button.
Definition transparency_widget.h:16
WID_TT_INDUSTRIES
@ WID_TT_INDUSTRIES
industries transparency toggle button.
Definition transparency_widget.h:20
WID_TT_CATENARY
@ WID_TT_CATENARY
Catenary transparency toggle button.
Definition transparency_widget.h:24
WID_TT_HOUSES
@ WID_TT_HOUSES
Houses transparency toggle button.
Definition transparency_widget.h:19
WID_TT_TEXT
@ WID_TT_TEXT
Loading and cost/income text transparency toggle button.
Definition transparency_widget.h:25
WID_TT_STRUCTURES
@ WID_TT_STRUCTURES
Object structure transparency toggle button.
Definition transparency_widget.h:23
DrawFrameRect
void DrawFrameRect(int left, int top, int right, int bottom, Colours colour, FrameFlags flags)
Draw frame rectangle.
Definition widget.cpp:307
WWT_IMGBTN
@ WWT_IMGBTN
(Toggle) Button with image
Definition widget_type.h:41
NWID_HORIZONTAL
@ NWID_HORIZONTAL
Horizontal container.
Definition widget_type.h:66
WWT_PANEL
@ WWT_PANEL
Simple depressed panel.
Definition widget_type.h:39
WWT_STICKYBOX
@ WWT_STICKYBOX
Sticky box (at top-right of a window, after WWT_DEFSIZEBOX)
Definition widget_type.h:57
WWT_CAPTION
@ WWT_CAPTION
Window caption (window title between closebox and stickybox)
Definition widget_type.h:52
WWT_CLOSEBOX
@ WWT_CLOSEBOX
Close box (at top-left of a window)
Definition widget_type.h:60
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:1685
window_gui.h
Functions, definitions and such used only by the GUI.
FrameFlag::Lowered
@ Lowered
If set the frame is lowered and the background colour brighter (ie. buttons when pressed)
WDP_MANUAL
@ WDP_MANUAL
Manually align the window (so no automatic location finding)
Definition window_gui.h:143
WidgetID
int WidgetID
Widget ID.
Definition window_type.h:20
WC_NONE
@ WC_NONE
No window, redirects to WC_MAIN_WINDOW.
Definition window_type.h:50
WC_TRANSPARENCY_TOOLBAR
@ WC_TRANSPARENCY_TOOLBAR
Transparency toolbar; Window numbers:
Definition window_type.h:97