| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Linq; |
| | 3 | | using MainScripts.DCL.Controllers.SettingsDesktop.SettingsControllers; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace MainScripts.DCL.Controllers.HUD.SettingsPanelHUDDesktop.Scripts |
| | 7 | | { |
| | 8 | | [CreateAssetMenu(menuName = "Settings/Controllers/Controls/Resolution Size", |
| | 9 | | fileName = "ResolutionSizeControlController")] |
| | 10 | | public class ResolutionSizeControlController : SpinBoxSettingsControlControllerDesktop |
| | 11 | | { |
| 0 | 12 | | private readonly List<Resolution> possibleResolutions = new (); |
| | 13 | |
|
| | 14 | | public override void Initialize() |
| | 15 | | { |
| 0 | 16 | | SetupAvailableResolutions(); |
| 0 | 17 | | base.Initialize(); |
| 0 | 18 | | SetupLabels(); |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | // Filter the smallest resolutions as no one will ever use them |
| | 22 | | private void SetupAvailableResolutions() |
| | 23 | | { |
| 0 | 24 | | possibleResolutions.AddRange(ScreenResolutionUtils.Resolutions.SkipWhile(r => r.width <= 1024)); |
| 0 | 25 | | } |
| | 26 | |
|
| | 27 | | private void SetupLabels() |
| | 28 | | { |
| 0 | 29 | | var length = possibleResolutions.Count; |
| 0 | 30 | | var resolutionLabels = new string[length]; |
| | 31 | |
|
| 0 | 32 | | for (var i = 0; i < length; i++) |
| | 33 | | { |
| 0 | 34 | | var resolution = possibleResolutions[i]; |
| | 35 | |
|
| | 36 | | // by design we want the list to be inverted so the biggest resolutions stay on top |
| | 37 | | // our resolutionSizeIndex is based on this decision |
| 0 | 38 | | resolutionLabels[length - 1 - i] = GetLabel(resolution); |
| | 39 | | } |
| | 40 | |
|
| 0 | 41 | | RaiseOnOverrideIndicatorLabel(resolutionLabels); |
| 0 | 42 | | } |
| | 43 | |
|
| | 44 | | private static string GetLabel(Resolution resolution) => |
| 0 | 45 | | $"{resolution.width}x{resolution.height} ({GetAspectRatio(resolution.width, resolution.height)}) {resolution |
| | 46 | |
|
| | 47 | | public override object GetStoredValue() |
| | 48 | | { |
| 0 | 49 | | int index = currentDisplaySettings.resolutionSizeIndex; |
| 0 | 50 | | return index >= 0 ? index : ScreenResolutionUtils.DefaultIndex; |
| | 51 | | } |
| | 52 | |
|
| | 53 | | private static string GetAspectRatio(int width, int height) |
| | 54 | | { |
| | 55 | | int rest; |
| 0 | 56 | | int tempWidth = width; |
| 0 | 57 | | int tempHeight = height; |
| | 58 | |
|
| 0 | 59 | | while (height != 0) |
| | 60 | | { |
| 0 | 61 | | rest = width % height; |
| 0 | 62 | | width = height; |
| 0 | 63 | | height = rest; |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | return $"{tempWidth / width}:{tempHeight / width}"; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | public override void UpdateSetting(object newValue) |
| | 70 | | { |
| 0 | 71 | | var value = (int)newValue; |
| 0 | 72 | | currentDisplaySettings.resolutionSizeIndex = value; |
| 0 | 73 | | ScreenResolutionUtils.Apply(currentDisplaySettings); |
| 0 | 74 | | } |
| | 75 | | } |
| | 76 | | } |