Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / zoom_func.h
blob4b998839f06d35cca31f052c0490e4e823a23d5a
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 */
8 /** @file zoom_func.h Functions related to zooming. */
10 #ifndef ZOOM_FUNC_H
11 #define ZOOM_FUNC_H
13 #include "zoom_type.h"
15 /**
16 * Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN)
17 * When shifting right, value is rounded up
18 * @param value value to shift
19 * @param zoom zoom level to shift to
20 * @return shifted value
22 inline int ScaleByZoom(int value, ZoomLevel zoom)
24 return value << zoom;
27 /**
28 * Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN)
29 * When shifting right, value is rounded up
30 * @param value value to shift
31 * @param zoom zoom level to shift to
32 * @return shifted value
34 inline int UnScaleByZoom(int value, ZoomLevel zoom)
36 return (value + (1 << zoom) - 1) >> zoom;
39 /**
40 * Adjust by zoom level; zoom < 0 shifts right, zoom >= 0 shifts left
41 * @param value value to shift
42 * @param zoom zoom level to shift to
43 * @return shifted value
45 inline int AdjustByZoom(int value, int zoom)
47 return zoom < 0 ? UnScaleByZoom(value, ZoomLevel(-zoom)) : ScaleByZoom(value, ZoomLevel(zoom));
50 /**
51 * Scale by zoom level, usually shift left (when zoom > ZOOM_LVL_MIN)
52 * @param value value to shift
53 * @param zoom zoom level to shift to
54 * @return shifted value
56 inline int ScaleByZoomLower(int value, ZoomLevel zoom)
58 return value << zoom;
61 /**
62 * Scale by zoom level, usually shift right (when zoom > ZOOM_LVL_MIN)
63 * @param value value to shift
64 * @param zoom zoom level to shift to
65 * @return shifted value
67 inline int UnScaleByZoomLower(int value, ZoomLevel zoom)
69 return value >> zoom;
72 /**
73 * Short-hand to apply GUI zoom level.
74 * @param value Pixel amount at #ZOOM_LVL_MIN (full zoom in).
75 * @return Pixel amount at #ZOOM_LVL_GUI (current interface size).
77 inline int UnScaleGUI(int value)
79 return UnScaleByZoom(value, ZOOM_LVL_GUI);
82 /**
83 * Scale zoom level relative to GUI zoom.
84 * @param value zoom level to scale
85 * @return scaled zoom level
87 inline ZoomLevel ScaleZoomGUI(ZoomLevel value)
89 return std::clamp(ZoomLevel(value + (ZOOM_LVL_GUI - ZOOM_LVL_NORMAL)), ZOOM_LVL_MIN, ZOOM_LVL_MAX);
92 /**
93 * UnScale zoom level relative to GUI zoom.
94 * @param value zoom level to scale
95 * @return un-scaled zoom level
97 inline ZoomLevel UnScaleZoomGUI(ZoomLevel value)
99 return std::clamp(ZoomLevel(value - (ZOOM_LVL_GUI - ZOOM_LVL_NORMAL)), ZOOM_LVL_MIN, ZOOM_LVL_MAX);
103 * Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
104 * @param value Pixel amount at #ZOOM_BASE (traditional "normal" interface size).
105 * @return Pixel amount at #ZOOM_LVL_GUI (current interface size).
107 inline int ScaleSpriteTrad(int value)
109 return UnScaleGUI(value * ZOOM_BASE);
113 * Scale traditional pixel dimensions to GUI zoom level.
114 * @param value Pixel amount at #ZOOM_BASE (traditional "normal" interface size).
115 * @return Pixel amount at #ZOOM_LVL_GUI (current interface size).
117 inline int ScaleGUITrad(int value)
119 return value * _gui_scale / 100;
122 #endif /* ZOOM_FUNC_H */