|
| 1 | +using System; |
| 2 | + |
1 | 3 | namespace UnityEngine.InputSystem.Samples.RebindUI
|
2 | 4 | {
|
| 5 | + /// <summary> |
| 6 | + /// Conversion utility class for physical coordinate transforms. |
| 7 | + /// </summary> |
3 | 8 | internal static class UnitConverter
|
4 | 9 | {
|
5 |
| - private const float kMillimetersPerInch = 25.4f; |
6 |
| - private const string kWarningMessage = "Screen.dpi not available, assuming 96 DPI."; |
7 |
| - |
8 |
| - private static float EffectivePpi(float ppi) => ppi <= 0.0f ? 96.0f : ppi; |
9 |
| - private static float PixelsToMillimetersFactor(float ppi) => kMillimetersPerInch / EffectivePpi(ppi); |
10 |
| - private static float MillimetersToPixelsFactor(float ppi) => EffectivePpi(ppi) / kMillimetersPerInch; |
11 |
| - |
12 |
| - #region PixelsToMillimeters |
13 |
| - |
14 |
| - public static float PixelsToMillimeters(float pixels, float ppi) => pixels * PixelsToMillimetersFactor(ppi); |
15 |
| - |
16 |
| - public static float PixelsToMillimeters(float millimeters) => PixelsToMillimeters(millimeters, Screen.dpi); |
17 |
| - |
18 | 10 | /// <summary>
|
19 |
| - /// Converts a pixel size to millimeters, based on Screen.dpi. |
| 11 | + /// Conversion factor for millimeters per inch. |
20 | 12 | /// </summary>
|
21 |
| - /// <remarks>Screen.dpi may not be available on some platforms. In such cases, 96 DPI is assumed.</remarks> |
22 |
| - public static Vector2 PixelsToMillimeters(Vector2 pixels) => PixelsToMillimeters(pixels, Screen.dpi); |
23 |
| - |
| 13 | + /// <example> |
| 14 | + /// var millimeters = 10.0f; |
| 15 | + /// Debug.Log($"{millimeters} mm is equal to {millimeters / MillimetersPerInch} inches."); |
| 16 | + /// </example> |
| 17 | + public const float MillimetersPerInch = 25.4f; |
24 | 18 |
|
25 | 19 | /// <summary>
|
26 |
| - /// Converts a size in pixels to millimeters based on the given PPI (Pixels-per-inch). |
| 20 | + /// Returns a conversion factor for converting pixels to millimeters using the given pixel density. |
27 | 21 | /// </summary>
|
28 |
| - /// <param name="pixels">The size in pixels (screen-space).</param> |
29 |
| - /// <param name="ppi">Pixels-per-inch (PPI). If this is zero or negative DefaultPixelsPerInch will be used.</param> |
30 |
| - /// <returns></returns> |
31 |
| - public static Vector2 PixelsToMillimeters(Vector2 pixels, float ppi) => pixels * PixelsToMillimetersFactor(ppi); |
32 |
| - |
33 |
| - #endregion |
34 |
| - |
35 |
| - #region MillimetersToPixels |
36 |
| - |
37 |
| - public static float MillimetersToPixels(float millimeters, float ppi) => millimeters * MillimetersToPixelsFactor(ppi); |
38 |
| - |
39 |
| - public static float MillimetersToPixels(float millimeters) => MillimetersToPixels(millimeters, Screen.dpi); |
| 22 | + /// <param name="ppi">Pixel density expressed as pixels-per-inch (PPI).</param> |
| 23 | + /// <returns>Conversion ratio for converting pixels to millimeters.</returns> |
| 24 | + public static float PixelsToMillimetersConversionFactor(float ppi) |
| 25 | + { |
| 26 | + if (ppi <= 0.0f) |
| 27 | + throw new ArgumentOutOfRangeException($"Invalid pixel density: {ppi}"); |
| 28 | + return MillimetersPerInch / ppi; |
| 29 | + } |
40 | 30 |
|
41 | 31 | /// <summary>
|
42 |
| - /// Converts a size in millimeters to pixels based on the given PPI (Pixels-per-inch). |
| 32 | + /// Returns a conversion factor for converting millimeters to pixels using the given pixel density. |
43 | 33 | /// </summary>
|
44 |
| - /// <param name="millimeters">The size in millimeters</param> |
45 |
| - /// <param name="ppi">Pixels-per-inch (PPI). If this is zero or negative kDefaultPixelsPerInch will be used.</param> |
46 |
| - /// <returns>Size expressed in pixels.</returns> |
47 |
| - public static Vector2 MillimetersToPixels(Vector2 millimeters, float ppi) => millimeters * MillimetersToPixelsFactor(ppi); |
| 34 | + /// <param name="ppi">Pixel density expressed as pixels-per-inch (PPI).</param> |
| 35 | + /// <returns>Conversion ratio for converting millimeters to pixels.</returns> |
| 36 | + public static float MillimetersToPixelsConversionFactor(float ppi) |
| 37 | + { |
| 38 | + if (ppi <= 0.0f) |
| 39 | + throw new ArgumentOutOfRangeException($"Invalid pixel density: {ppi}"); |
| 40 | + return ppi / MillimetersPerInch; |
| 41 | + } |
48 | 42 |
|
49 | 43 | /// <summary>
|
50 |
| - /// Converts a size in millimeters to pixels, based on Screen.dpi. |
| 44 | + /// Given a pixel density, returns the same pixel density (if positive), or returns a fallback PPI of 96. |
51 | 45 | /// </summary>
|
52 |
| - /// <remarks>Screen.dpi may not be available on some platforms. In such cases, 96 DPI is assumed.</remarks> |
53 |
| - public static Vector2 MillimetersToPixels(Vector2 millimeters) => MillimetersToPixels(millimeters, Screen.dpi); |
54 |
| - |
55 |
| - #endregion |
| 46 | + /// <param name="ppi">The candidate pixel density expressed as pixels-per-inch (PPI).</param> |
| 47 | + /// <returns>Returns <paramref name="ppi"/> if positive, otherwise 96 PPI.</returns> |
| 48 | + public static float EffectivePixelDensity(float ppi) => ppi <= 0.0f ? 96.0f : ppi; |
56 | 49 | }
|
57 | 50 | }
|
0 commit comments