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 
8 /** @file transparency.h Functions related to transparency. */
9 
10 #ifndef TRANSPARENCY_H
11 #define TRANSPARENCY_H
12 
13 #include "gfx_func.h"
14 #include "openttd.h"
15 #include "core/bitmath_func.hpp"
16 
17 /**
18  * Transparency option bits: which position in _transparency_opt stands for which transparency.
19  * If you change the order, change the order of the ShowTransparencyToolbar() stuff in transparency_gui.cpp too.
20  * If you add or remove an option don't forget to change the transparency 'hot keys' in main_gui.cpp.
21  */
22 enum TransparencyOption {
23 	TO_SIGNS = 0,  ///< signs
24 	TO_TREES,      ///< trees
25 	TO_HOUSES,     ///< town buildings
26 	TO_INDUSTRIES, ///< industries
27 	TO_BUILDINGS,  ///< company buildings - depots, stations, HQ, ...
28 	TO_BRIDGES,    ///< bridges
29 	TO_STRUCTURES, ///< other objects such as transmitters and lighthouses
30 	TO_CATENARY,   ///< catenary
31 	TO_LOADING,    ///< loading indicators
32 	TO_END,
33 	TO_INVALID,    ///< Invalid transparency option
34 };
35 
36 typedef uint TransparencyOptionBits; ///< transparency option bits
37 extern TransparencyOptionBits _transparency_opt;
38 extern TransparencyOptionBits _transparency_lock;
39 extern TransparencyOptionBits _invisibility_opt;
40 extern byte _display_opt;
41 
42 /**
43  * Check if the transparency option bit is set
44  * and if we aren't in the game menu (there's never transparency)
45  *
46  * @param to the structure which transparency option is ask for
47  */
IsTransparencySet(TransparencyOption to)48 static inline bool IsTransparencySet(TransparencyOption to)
49 {
50 	return (HasBit(_transparency_opt, to) && _game_mode != GM_MENU);
51 }
52 
53 /**
54  * Check if the invisibility option bit is set
55  * and if we aren't in the game menu (there's never transparency)
56  *
57  * @param to the structure which invisibility option is ask for
58  */
IsInvisibilitySet(TransparencyOption to)59 static inline bool IsInvisibilitySet(TransparencyOption to)
60 {
61 	return (HasBit(_transparency_opt & _invisibility_opt, to) && _game_mode != GM_MENU);
62 }
63 
64 /**
65  * Toggle the transparency option bit
66  *
67  * @param to the transparency option to be toggled
68  */
ToggleTransparency(TransparencyOption to)69 static inline void ToggleTransparency(TransparencyOption to)
70 {
71 	ToggleBit(_transparency_opt, to);
72 }
73 
74 /**
75  * Toggle the invisibility option bit
76  *
77  * @param to the structure which invisibility option is toggle
78  */
ToggleInvisibility(TransparencyOption to)79 static inline void ToggleInvisibility(TransparencyOption to)
80 {
81 	ToggleBit(_invisibility_opt, to);
82 }
83 
84 /**
85  * Toggles between invisible and solid state.
86  * If object is transparent, then it is made invisible.
87  * Used by the keyboard shortcuts.
88  *
89  * @param to the object type which invisibility option to toggle
90  */
ToggleInvisibilityWithTransparency(TransparencyOption to)91 static inline void ToggleInvisibilityWithTransparency(TransparencyOption to)
92 {
93 	if (IsInvisibilitySet(to)) {
94 		ClrBit(_invisibility_opt, to);
95 		ClrBit(_transparency_opt, to);
96 	} else {
97 		SetBit(_invisibility_opt, to);
98 		SetBit(_transparency_opt, to);
99 	}
100 }
101 
102 /**
103  * Toggle the transparency lock bit
104  *
105  * @param to the transparency option to be locked or unlocked
106  */
ToggleTransparencyLock(TransparencyOption to)107 static inline void ToggleTransparencyLock(TransparencyOption to)
108 {
109 	ToggleBit(_transparency_lock, to);
110 }
111 
112 /** Set or clear all non-locked transparency options */
ResetRestoreAllTransparency()113 static inline void ResetRestoreAllTransparency()
114 {
115 	/* if none of the non-locked options are set */
116 	if ((_transparency_opt & ~_transparency_lock) == 0) {
117 		/* set all non-locked options */
118 		_transparency_opt |= GB(~_transparency_lock, 0, TO_END);
119 	} else {
120 		/* clear all non-locked options */
121 		_transparency_opt &= _transparency_lock;
122 	}
123 
124 	MarkWholeScreenDirty();
125 }
126 
127 #endif /* TRANSPARENCY_H */
128