Skip to content

Commit

Permalink
Major code optimization and made code more consistant
Browse files Browse the repository at this point in the history
  • Loading branch information
thespbgamer committed Jan 24, 2023
1 parent 45c0bdf commit 0692819
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 119 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
- Ability to change the camera position, you can also reset it to the default position
- Auto Zoom to approximate map size

### Improvements

- Major code optimizations

### Fixed

- UI not showing up when using the 'Reset UI' option with the option 'Hide UI at a certain zoom level' enabled
Expand Down
223 changes: 108 additions & 115 deletions ZoomLevel/ModEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ namespace ZoomLevel
public class ModEntry : Mod
{
private ModConfig configsForTheMod;
private bool wasToggleUIDone = false;
private float previousUIValueToggleUI = 1.0f;
private float previousUIValueToggleUIWithCertainValue = -1.0f;

private bool wasThePreviousButtonPressSucessfull;
private bool wasToggleUIScaleClicked;
private bool wasZoomLevelChanged;

private float uiScaleBeforeTheHidding;
private float currentUIScale;

/// <param name="helper">Provides simplified APIs for writing mods.</param>
public override void Entry(IModHelper helper)
Expand All @@ -26,23 +30,7 @@ public override void Entry(IModHelper helper)

//On area change and on load save
helper.Events.Player.Warped += this.Player_Warped;
helper.Events.GameLoop.DayStarted += this.GameLoop_DayStarted;
}

private void GameLoop_DayStarted(object sender, DayStartedEventArgs e)
{
if (configsForTheMod.AutoZoomToMapSize == true)
{
ChangeZoomLevelToCurrentMapSize();
}
}

private void Player_Warped(object sender, WarpedEventArgs e)
{
if (configsForTheMod.AutoZoomToMapSize == true)
{
ChangeZoomLevelToCurrentMapSize();
}
helper.Events.GameLoop.SaveLoaded += this.GameLoop_SaveLoaded;
}

private void OnLaunched(object sender, GameLaunchedEventArgs e)
Expand Down Expand Up @@ -88,10 +76,34 @@ private void OnLaunched(object sender, GameLaunchedEventArgs e)
}
}

private void GameLoop_SaveLoaded(object sender, SaveLoadedEventArgs e)
{
uiScaleBeforeTheHidding = Game1.options.desiredUIScale;
wasThePreviousButtonPressSucessfull = false;
wasToggleUIScaleClicked = false;
wasZoomLevelChanged = false;

if (configsForTheMod.AutoZoomToMapSize == true)
{
ChangeZoomLevelToCurrentMapSize();
}
}

private void Player_Warped(object sender, WarpedEventArgs e)
{
if (configsForTheMod.AutoZoomToMapSize == true)
{
ChangeZoomLevelToCurrentMapSize();
}
}

private void Events_Input_ButtonPressed(object sender, ButtonPressedEventArgs e)
{
if (!Context.IsWorldReady || (!Context.IsPlayerFree && !configsForTheMod.ZoomAndUIControlEverywhere)) { return; }
bool wasThePreviousButtonPressSucessfull = false;

wasThePreviousButtonPressSucessfull = false;
wasToggleUIScaleClicked = false;
wasZoomLevelChanged = false;

if (configsForTheMod.KeybindListHoldToChangeUI.IsDown())
{
Expand All @@ -107,28 +119,33 @@ private void Events_Input_ButtonPressed(object sender, ButtonPressedEventArgs e)
}
else if (configsForTheMod.KeybindListResetZoomOrUI.JustPressed())
{
ResetUI();
UpdateUIScale(configsForTheMod.ResetZoomOrUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListMaxZoomOrUI.JustPressed())
{
CapUILevel(configsForTheMod.MaxZoomInLevelAndUIValue);
UpdateUIScale(configsForTheMod.MaxZoomInLevelAndUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListMinZoomOrUI.JustPressed())
{
CapUILevel(configsForTheMod.MaxZoomOutLevelAndUIValue);
UpdateUIScale(configsForTheMod.MaxZoomOutLevelAndUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListToggleUI.JustPressed())
{
ToggleUI();
ToggleUIScale();
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListToggleHideUIWithCertainZoom.JustPressed())
{
configsForTheMod.IsHideUIWithCertainZoom = !configsForTheMod.IsHideUIWithCertainZoom;
//Game1.addHUDMessage(new HUDMessage("Hide UI With Certain Zoom is now: " + configsForTheMod.IsHideUIWithCertainZoom.ToString(), 2));
Game1.addHUDMessage(new HUDMessage(Helper.Translation.Get("hudMessages.HideUIWithCertainZoomIs.message", new { value = configsForTheMod.IsHideUIWithCertainZoom.ToString() }), 2));
ToggleHideWithUIWithCertainZoom();
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListChangeZoomToApproximateCurrentMapSize.JustPressed())
{
ChangeZoomLevelToCurrentMapSize();
wasThePreviousButtonPressSucessfull = true;
}
}
else if (configsForTheMod.KeybindListIncreaseZoomOrUI.JustPressed())
Expand All @@ -143,27 +160,29 @@ private void Events_Input_ButtonPressed(object sender, ButtonPressedEventArgs e)
}
else if (configsForTheMod.KeybindListResetZoomOrUI.JustPressed())
{
ResetZoom();
UpdateZoomValue(configsForTheMod.ResetZoomOrUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListMaxZoomOrUI.JustPressed())
{
CapZoomLevel(configsForTheMod.MaxZoomInLevelAndUIValue);
UpdateZoomValue(configsForTheMod.MaxZoomInLevelAndUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListMinZoomOrUI.JustPressed())
{
CapZoomLevel(configsForTheMod.MaxZoomOutLevelAndUIValue);
UpdateZoomValue(configsForTheMod.MaxZoomOutLevelAndUIValue);
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListToggleUI.JustPressed())
{
ToggleUI();
wasToggleUIScaleClicked = true;
ToggleUIScale();
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListToggleHideUIWithCertainZoom.JustPressed())
{
configsForTheMod.IsHideUIWithCertainZoom = !configsForTheMod.IsHideUIWithCertainZoom;
Game1.addHUDMessage(new HUDMessage(Helper.Translation.Get("hudMessages.HideUIWithCertainZoomIs.message", new { value = configsForTheMod.IsHideUIWithCertainZoom.ToString() }), 2));
ToggleHideWithUIWithCertainZoom();
wasThePreviousButtonPressSucessfull = true;
}
else if (configsForTheMod.KeybindListChangeZoomToApproximateCurrentMapSize.JustPressed())
{
Expand Down Expand Up @@ -218,7 +237,13 @@ private void Events_Input_ButtonPressed(object sender, ButtonPressedEventArgs e)
}
}

private static void ChangeZoomLevelToCurrentMapSize()
private void ToggleHideWithUIWithCertainZoom()
{
configsForTheMod.IsHideUIWithCertainZoom = !configsForTheMod.IsHideUIWithCertainZoom;
Game1.addHUDMessage(new HUDMessage(Helper.Translation.Get("hudMessages.HideUIWithCertainZoomIs.message", new { value = configsForTheMod.IsHideUIWithCertainZoom.ToString() }), 2));
}

private void ChangeZoomLevelToCurrentMapSize()
{
if (Game1.currentLocation != null)
{
Expand All @@ -235,120 +260,88 @@ private static void ChangeZoomLevelToCurrentMapSize()
{
zoomLevel = (float)screenHeight / (float)mapHeight;
}
Game1.options.desiredBaseZoomLevel = zoomLevel;
RefreshWindow();
UpdateZoomValue(zoomLevel);
}
}

private void CheckAndUpdateUIValues()
private void ToggleUIScale()
{
if (configsForTheMod.IsHideUIWithCertainZoom == true)
float uiValue = 0.0f;

if ((configsForTheMod.IsHideUIWithCertainZoom == true && wasZoomLevelChanged == true))
{
if (Game1.options.desiredBaseZoomLevel <= configsForTheMod.ZoomLevelThatHidesUI && previousUIValueToggleUIWithCertainValue <= 0.0f)
if (configsForTheMod.ZoomLevelThatHidesUI >= Game1.options.desiredBaseZoomLevel && currentUIScale > 0.0f)
{
previousUIValueToggleUIWithCertainValue = Game1.options.desiredUIScale;
Game1.options.desiredUIScale = 0.0f;
UpdateUIScale(uiValue);
}
else if (Game1.options.desiredBaseZoomLevel > configsForTheMod.ZoomLevelThatHidesUI && previousUIValueToggleUIWithCertainValue > 0.0f)
else if (configsForTheMod.ZoomLevelThatHidesUI < Game1.options.desiredBaseZoomLevel && currentUIScale <= 0.0f)
{
Game1.options.desiredUIScale = previousUIValueToggleUIWithCertainValue <= 0.0f ? 1.0f : previousUIValueToggleUIWithCertainValue;
previousUIValueToggleUIWithCertainValue = 0.0f;
uiValue = uiScaleBeforeTheHidding;
UpdateUIScale(uiValue);
}
RefreshWindow();
}
}

private void ToggleUI()
{
float uiValue = 0.0f;

if (wasToggleUIDone == true)
{
uiValue = previousUIValueToggleUI <= 0.0f ? 1.0f : previousUIValueToggleUI;
}
wasToggleUIDone = !wasToggleUIDone;

if (wasToggleUIDone == true)
if ((wasZoomLevelChanged == false && wasToggleUIScaleClicked == true))
{
previousUIValueToggleUI = Game1.options.desiredUIScale;
if (currentUIScale > 0.0f)
{
UpdateUIScale(uiValue);
}
else
{
uiValue = uiScaleBeforeTheHidding;
UpdateUIScale(uiValue);
}
}

//Changes ZoomLevel
Game1.options.desiredUIScale = uiValue;

RefreshWindow();
}

private void CapZoomLevel(float zoomValue)
{
Game1.options.desiredBaseZoomLevel = zoomValue;

RefreshWindow();
CheckAndUpdateUIValues();
}

private static void CapUILevel(float uiValue)
{
Game1.options.desiredUIScale = uiValue;

RefreshWindow();
}

private void ResetUI()
private void ChangeZoomLevel(float amount = 0)
{
Game1.options.desiredUIScale = configsForTheMod.ResetZoomOrUIValue;
float zoomLevelValue = (float)Math.Round(Game1.options.desiredBaseZoomLevel + amount, 2);

RefreshWindow();
UpdateZoomValue(zoomLevelValue);
}

private void ResetZoom()
private void ChangeUILevel(float amount = 0)
{
Game1.options.desiredBaseZoomLevel = configsForTheMod.ResetZoomOrUIValue;
CheckAndUpdateUIValues();
float uiScale = (float)Math.Round(Game1.options.desiredUIScale + amount, 2);

RefreshWindow();
UpdateUIScale(uiScale);
}

private void ChangeZoomLevel(float amount = 0)
private void UpdateZoomValue(float zoomLevelValue)
{
//Changes ZoomLevel
Game1.options.desiredBaseZoomLevel = (float)Math.Round(Game1.options.desiredBaseZoomLevel + amount, 2);

//Caps Max Zoom In Level
Game1.options.desiredBaseZoomLevel = Game1.options.desiredBaseZoomLevel >= configsForTheMod.MaxZoomInLevelAndUIValue ? configsForTheMod.MaxZoomInLevelAndUIValue : Game1.options.desiredBaseZoomLevel;
zoomLevelValue = zoomLevelValue >= configsForTheMod.MaxZoomInLevelAndUIValue ? configsForTheMod.MaxZoomInLevelAndUIValue : zoomLevelValue;

//Caps Max Zoom Out Level
Game1.options.desiredBaseZoomLevel = Game1.options.desiredBaseZoomLevel <= configsForTheMod.MaxZoomOutLevelAndUIValue ? configsForTheMod.MaxZoomOutLevelAndUIValue : Game1.options.desiredBaseZoomLevel;
zoomLevelValue = zoomLevelValue <= configsForTheMod.MaxZoomOutLevelAndUIValue ? configsForTheMod.MaxZoomOutLevelAndUIValue : zoomLevelValue;

CheckAndUpdateUIValues();

RefreshWindow();
//Changes ZoomLevel
Game1.options.desiredBaseZoomLevel = zoomLevelValue;
wasZoomLevelChanged = true;
ToggleUIScale();
}

private void ChangeUILevel(float amount = 0)
private void UpdateUIScale(float uiScale)
{
//Changes UI Zoom Level
Game1.options.desiredUIScale = (float)Math.Round(Game1.options.desiredUIScale + amount, 2);

//Caps Max UI Zoom In Level
Game1.options.desiredUIScale = Game1.options.desiredUIScale >= configsForTheMod.MaxZoomInLevelAndUIValue ? configsForTheMod.MaxZoomInLevelAndUIValue : Game1.options.desiredUIScale;
if (uiScale != 0)
{
//Caps Max UI Scale
uiScale = uiScale >= configsForTheMod.MaxZoomInLevelAndUIValue ? configsForTheMod.MaxZoomInLevelAndUIValue : uiScale;

//Caps Max UI Zoom Out Level
Game1.options.desiredUIScale = Game1.options.desiredUIScale <= configsForTheMod.MaxZoomOutLevelAndUIValue ? configsForTheMod.MaxZoomOutLevelAndUIValue : Game1.options.desiredUIScale;
//Caps Min UI Scale
uiScale = uiScale <= configsForTheMod.MaxZoomOutLevelAndUIValue ? configsForTheMod.MaxZoomOutLevelAndUIValue : uiScale;
}
else
{
uiScaleBeforeTheHidding = Game1.options.desiredUIScale;
}

RefreshWindow();
}
//Changes UI Scale
Game1.options.desiredUIScale = uiScale;

private static void RefreshWindow()
{
/*
//Monitor Current Zoom Level
this.Monitor.Log($"{Game1.options.desiredBaseZoomLevel}.", LogLevel.Debug);
//Monitor Current UI Level
this.Monitor.Log($"{Game1.options.desiredUIScale}.", LogLevel.Debug);
*/

Program.gamePtr.refreshWindowSettings();
currentUIScale = Game1.options.desiredUIScale;
}

private string FormatPercentage(float val)
Expand Down
2 changes: 1 addition & 1 deletion ZoomLevel/i18n/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"others.ZoomAndUIAnywhere.description": "If activated you can control your Zoom and UI Levels anywhere.",
"others.HideUIWithCertainZoom.name": "Hide UI with Certain Zoom",
"others.HideUIWithCertainZoom.description": "If activated your UI hides when it reaches a certain zoom level.\nIt can be changed by the 'Toggle Hide UI at Certain Zoom'.",
"others.PressAnyButtonToCenterCamera.name": "Press Button to Center Camera",
"others.PressAnyButtonToCenterCamera.name": "Any Button Resets Camera",
"others.PressAnyButtonToCenterCamera.description": "If activated your camera centers if any key is pressed.\nIf not, you will need to press the 'Reset Camera Movement' key.",
"others.AutoZoomToMapSize.name": "Auto Zoom to Map Size",
"others.AutoZoomToMapSize.description": "If activated it auto zooms to map size.\nIf not, it will not do that.",
Expand Down
2 changes: 1 addition & 1 deletion ZoomLevel/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"others.ZoomAndUIAnywhere.description": "If activated you can control your Zoom and UI Levels anywhere.",
"others.HideUIWithCertainZoom.name": "Hide UI with Certain Zoom",
"others.HideUIWithCertainZoom.description": "If activated your UI hides when it reaches a certain zoom level.\nIt can be changed by the 'Toggle Hide UI at Certain Zoom'.",
"others.PressAnyButtonToCenterCamera.name": "Press Button to Center Camera",
"others.PressAnyButtonToCenterCamera.name": "Any Button Resets Camera",
"others.PressAnyButtonToCenterCamera.description": "If activated your camera centers if any key is pressed.\nIf not, you will need to press the 'Reset Camera Movement' key.",
"others.AutoZoomToMapSize.name": "Auto Zoom to Map Size",
"others.AutoZoomToMapSize.description": "If activated it auto zooms to map size.\nIf not, it will not do that.",
Expand Down
4 changes: 2 additions & 2 deletions ZoomLevel/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"Name": "Zoom Level",
"Author": "thespbgamer",
"Version": "1.15.0",
"Description": "Change the Zoom and UI levels with a simple keybind.",
"Version": "2.0.0",
"Description": "Change the Zoom and UI levels with a simple keybind.Now with camera position change!",
"UniqueID": "thespbgamer.ZoomLevel",
"EntryDll": "ZoomLevel.dll",
"MinimumApiVersion": "3.18.1",
Expand Down

0 comments on commit 0692819

Please sign in to comment.