Skip to content

Commit

Permalink
Add UI to set fullscreen width override
Browse files Browse the repository at this point in the history
  • Loading branch information
drojf committed Feb 3, 2023
1 parent e6b498e commit 5025bf1
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 30 deletions.
23 changes: 15 additions & 8 deletions Assets.Scripts.Core/GameSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ public T ChooseJapaneseEnglish<T>(T japanese, T english)
}
}

public Resolution GetFullscreenResolution()
public Resolution GetFullscreenResolution(bool useOverride = true, bool doLogging = true)
{
Resolution resolution = new Resolution();
string source = "";
Expand Down Expand Up @@ -1119,17 +1119,24 @@ public Resolution GetFullscreenResolution()
PlayerPrefs.SetInt("fullscreen_height_override", 0);
}

if (PlayerPrefs.GetInt("fullscreen_width_override") > 0)
if(useOverride)
{
resolution.width = PlayerPrefs.GetInt("fullscreen_width_override");
source += " + Width Override";
if (PlayerPrefs.GetInt("fullscreen_width_override") > 0)
{
resolution.width = PlayerPrefs.GetInt("fullscreen_width_override");
source += " + Width Override";
}
if (PlayerPrefs.GetInt("fullscreen_height_override") > 0)
{
resolution.height = PlayerPrefs.GetInt("fullscreen_height_override");
source += " + Height Override";
}
}
if (PlayerPrefs.GetInt("fullscreen_height_override") > 0)

if(doLogging)
{
resolution.height = PlayerPrefs.GetInt("fullscreen_height_override");
source += " + Height Override";
Debug.Log("Using resolution " + resolution.width + "x" + resolution.height + " as the fullscreen resolution based on " + source + ".");
}
Debug.Log("Using resolution " + resolution.width + "x" + resolution.height + " as the fullscreen resolution based on " + source + ".");
return resolution;
}

Expand Down
5 changes: 5 additions & 0 deletions MOD.Scripts.UI/MODMenuCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public static void Label(string label, params GUILayoutOption[] options)
GUILayout.Label(label, MODStyleManager.OnGUIInstance.Group.label, options);
}

public static void LabelRightAlign(string label, params GUILayoutOption[] options)
{
GUILayout.Label(label, MODStyleManager.OnGUIInstance.Group.labelRightAlign, options);
}

public static void Label(GUIContent content, params GUILayoutOption[] options)
{
GUILayout.Label(content, MODStyleManager.OnGUIInstance.Group.label, options);
Expand Down
146 changes: 124 additions & 22 deletions MOD.Scripts.UI/MODMenuResolution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ namespace MOD.Scripts.UI
{
class MODMenuResolution
{
private string overrideFullScreenResolutionDescription =
"Set a custom fullscreen resolution\n\n" +
"Use this option only if the fullscreen resolution is detected incorrectly (such as on some Linux systems)\n" +
"You can manually type in a resolution to use below.\n\n" +
"Click 'Clear Override' to let the game automatically determine the fullscreen resolution";

private string screenHeightString;
private string fullscreenWidthOverrideString;
private string fullscreenHeightOverrideString;
private int overrideFullResCountdown;

public MODMenuResolution()
{
Expand All @@ -19,55 +28,142 @@ public MODMenuResolution()
public void OnBeforeMenuVisible()
{
screenHeightString = $"{Screen.height}";

if (PlayerPrefs.HasKey("fullscreen_width_override"))
{
fullscreenWidthOverrideString = $"{PlayerPrefs.GetInt("fullscreen_width_override")}";
}

if (PlayerPrefs.HasKey("fullscreen_height_override"))
{
fullscreenHeightOverrideString = $"{PlayerPrefs.GetInt("fullscreen_height_override")}";
}

overrideFullResCountdown = 3;
}

public void OnGUI()
{
Label("Resolution Settings");
Label("Windowed Resolution Settings");
{
GUILayout.BeginHorizontal();
if (Button(new GUIContent("480p", "Set resolution to 853 x 480"))) { SetAndSaveResolution(480); }
if (Button(new GUIContent("720p", "Set resolution to 1280 x 720"))) { SetAndSaveResolution(720); }
if (Button(new GUIContent("1080p", "Set resolution to 1920 x 1080"))) { SetAndSaveResolution(1080); }
if (Button(new GUIContent("1440p", "Set resolution to 2560 x 1440"))) { SetAndSaveResolution(1440); }

if (!GameSystem.Instance.IsFullscreen)
{
if (Button(new GUIContent("480p", "Set resolution to 853 x 480"))) { SetAndSaveResolution(480); }
if (Button(new GUIContent("720p", "Set resolution to 1280 x 720"))) { SetAndSaveResolution(720); }
if (Button(new GUIContent("1080p", "Set resolution to 1920 x 1080"))) { SetAndSaveResolution(1080); }
if (Button(new GUIContent("1440p", "Set resolution to 2560 x 1440"))) { SetAndSaveResolution(1440); }

screenHeightString = GUILayout.TextField(screenHeightString);
if (Button(new GUIContent("Set", "Sets a custom resolution - mainly for windowed mode.\n\n" +
"Height set automatically to maintain 16:9 aspect ratio.")))
{
if (int.TryParse(screenHeightString, out int new_height))
{
if (new_height < 480)
{
MODToaster.Show("Height too small - must be at least 480 pixels");
new_height = 480;
}
else if (new_height > 15360)
{
MODToaster.Show("Height too big - must be less than 15360 pixels");
new_height = 15360;
}
screenHeightString = $"{new_height}";
int new_width = Mathf.RoundToInt(new_height * GameSystem.Instance.AspectRatio);
GameSystem.Instance.SetResolution(new_width, new_height, Screen.fullScreen);
PlayerPrefs.SetInt("width", new_width);
PlayerPrefs.SetInt("height", new_height);
}
}
}

if (GameSystem.Instance.IsFullscreen)
{
if (Button(new GUIContent("Windowed", "Toggle Fullscreen")))
if (Button(new GUIContent("Click here to go Windowed to change these settings", "Toggle Fullscreen")))
{
GameSystem.Instance.DeFullscreen(PlayerPrefs.GetInt("width"), PlayerPrefs.GetInt("height"));
}
}
else
{
if (Button(new GUIContent("Fullscreen", "Toggle Fullscreen")))
if (Button(new GUIContent("Go Fullscreen", "Toggle Fullscreen")))
{
GameSystem.Instance.GoFullscreen();
}
}

screenHeightString = GUILayout.TextField(screenHeightString);
if (Button(new GUIContent("Set", "Sets a custom resolution - mainly for windowed mode.\n\n" +
"Height set automatically to maintain 16:9 aspect ratio.")))
GUILayout.EndHorizontal();
}

GUILayout.Space(20);

Resolution detectedRes = GameSystem.Instance.GetFullscreenResolution(useOverride: false, doLogging: false);
Resolution actualRes = GameSystem.Instance.GetFullscreenResolution(useOverride: true, doLogging: false);
string overrideString = FullscreenOverrideEnabled() ? $"{actualRes.width} x {actualRes.height}" : "Off";
Label($"Fullscreen Resolution Override (Detected: {detectedRes.width} x {detectedRes.height} Override: {overrideString})");
{
GUILayout.BeginHorizontal();

GUILayout.BeginHorizontal();
LabelRightAlign("Width:");
fullscreenWidthOverrideString = GUILayout.TextField(fullscreenWidthOverrideString, 5);
GUILayout.EndHorizontal();

GUILayout.BeginHorizontal();
LabelRightAlign("Height:");
fullscreenHeightOverrideString = GUILayout.TextField(fullscreenHeightOverrideString, 5);
GUILayout.EndHorizontal();


bool shouldUpdateFullscreenResolution = false;
string gameClearButtonText = overrideFullResCountdown > 0 ? $"Click {overrideFullResCountdown} times to override resolution" : $"Override Fullscreen Resolution";
if (Button(new GUIContent(gameClearButtonText, overrideFullScreenResolutionDescription)))
{
if (int.TryParse(screenHeightString, out int new_height))
if (overrideFullResCountdown > 0)
{
overrideFullResCountdown--;
}

if (overrideFullResCountdown <= 0)
{
if (new_height < 480)
if(int.TryParse(fullscreenWidthOverrideString, out int widthOverride) && widthOverride >= 640)
{
MODToaster.Show("Height too small - must be at least 480 pixels");
new_height = 480;
if(int.TryParse(fullscreenHeightOverrideString, out int heightOverride) && heightOverride >= 480)
{
PlayerPrefs.SetInt("fullscreen_width_override", widthOverride);
PlayerPrefs.SetInt("fullscreen_height_override", heightOverride);
shouldUpdateFullscreenResolution = true;
MODToaster.Show($"Fullscreen: {widthOverride} x {heightOverride}");
}
else
{
MODToaster.Show("Invalid Height");
}
}
else if (new_height > 15360)
else
{
MODToaster.Show("Height too big - must be less than 15360 pixels");
new_height = 15360;
MODToaster.Show("Invalid Width");
}
screenHeightString = $"{new_height}";
int new_width = Mathf.RoundToInt(new_height * GameSystem.Instance.AspectRatio);
GameSystem.Instance.SetResolution(new_width, new_height, Screen.fullScreen);
PlayerPrefs.SetInt("width", new_width);
PlayerPrefs.SetInt("height", new_height);
}
}

if (Button(new GUIContent("Clear Override", overrideFullScreenResolutionDescription)))
{
PlayerPrefs.SetInt("fullscreen_width_override", 0);
PlayerPrefs.SetInt("fullscreen_height_override", 0);
fullscreenWidthOverrideString = "0";
fullscreenHeightOverrideString = "0";
shouldUpdateFullscreenResolution = true;
}

if (shouldUpdateFullscreenResolution && GameSystem.Instance.IsFullscreen)
{
GameSystem.Instance.GoFullscreen();
}

GUILayout.EndHorizontal();
}
}
Expand All @@ -90,5 +186,11 @@ private void SetAndSaveResolution(int height)
PlayerPrefs.SetInt("width", width);
PlayerPrefs.SetInt("height", height);
}

private bool FullscreenOverrideEnabled()
{
return PlayerPrefs.GetInt("fullscreen_width_override", 0) != 0 ||
PlayerPrefs.GetInt("fullscreen_height_override", 0) != 0;
}
}
}
7 changes: 7 additions & 0 deletions MOD.Scripts.UI/MODStyleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class StyleGroup
public GUIStyle button;
public GUIStyle selectedButton;
public GUIStyle label; //Used for normal Label widgets
public GUIStyle labelRightAlign;
public GUIStyle headingLabel;
public GUIStyle upperLeftHeadingLabel;
}
Expand Down Expand Up @@ -230,6 +231,11 @@ private StyleGroup GenerateWidgetStyles(int menuWidth, int menuHeight, float gui
padding = padding,
};

GUIStyle labelStyleRightAlign = new GUIStyle(labelStyle)
{
alignment = TextAnchor.MiddleRight,
};

// Heading text style
GUIStyle headingLabelStyle = new GUIStyle(labelStyle)
{
Expand Down Expand Up @@ -264,6 +270,7 @@ private StyleGroup GenerateWidgetStyles(int menuWidth, int menuHeight, float gui
button = buttonStyle,
selectedButton = selectedButtonStyle,
label = labelStyle,
labelRightAlign = labelStyleRightAlign,
headingLabel = headingLabelStyle,
upperLeftHeadingLabel = upperLeftHeadingLabelStyle,
toolTipShrinkage = toolTipShrinkage,
Expand Down

0 comments on commit 5025bf1

Please sign in to comment.