Skip to content

Commit

Permalink
Allow replacing ship log map mode icons of existing planets (#885)
Browse files Browse the repository at this point in the history
## Improvements
- Map mode icon setting changes now work when updating an existing
planet. Fixes #875
  • Loading branch information
MegaPiggy committed Jun 4, 2024
2 parents 8b40938 + 51c0462 commit 24a53cb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 7 deletions.
88 changes: 85 additions & 3 deletions NewHorizons/Builder/ShipLog/MapModeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,33 @@
using NewHorizons.Utility.Files;
using NewHorizons.Utility.OuterWilds;
using NewHorizons.Utility.OWML;
using OWML.ModHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using static NewHorizons.External.Modules.ShipLogModule;

namespace NewHorizons.Builder.ShipLog
{
public static class MapModeBuilder
{
// Takes the game object because sometimes we change the AO to an NHAO and it breaks
private static Dictionary<GameObject, ShipLogAstroObject> _astroObjectToShipLog = new();

#region General
public static ShipLogAstroObject[][] ConstructMapMode(string systemName, GameObject transformParent, ShipLogAstroObject[][] currentNav, int layer)
{
_astroObjectToShipLog = new();

// Add stock planets
foreach (var shipLogAstroObject in currentNav.SelectMany(x => x))
{
var astroObject = Locator.GetAstroObject(AstroObject.StringIDToAstroObjectName(shipLogAstroObject._id));
_astroObjectToShipLog[astroObject.gameObject] = shipLogAstroObject;
}

Material greyScaleMaterial = SearchUtilities.Find(ShipLogHandler.PAN_ROOT_PATH + "/TimberHearth/Sprite").GetComponent<Image>().material;
List<NewHorizonsBody> bodies = Main.BodyDict[systemName].Where(
b => !(b.Config.ShipLog?.mapMode?.remove ?? false) && !b.Config.isQuantumState
Expand Down Expand Up @@ -71,16 +85,20 @@ public static ShipLogAstroObject[][] ConstructMapMode(string systemName, GameObj
}
}

ShipLogAstroObject[][] newNavMatrix = null;

if (useAuto)
{
return ConstructMapModeAuto(bodies, transformParent, greyScaleMaterial, layer);
newNavMatrix = ConstructMapModeAuto(bodies, transformParent, greyScaleMaterial, layer);
}
else if (useManual)
{
return ConstructMapModeManual(bodies, transformParent, greyScaleMaterial, currentNav, layer);
newNavMatrix = ConstructMapModeManual(bodies, transformParent, greyScaleMaterial, currentNav, layer);
}

return null;
ReplaceExistingMapModeIcons();

return newNavMatrix;
}

public static string GetAstroBodyShipLogName(string id)
Expand Down Expand Up @@ -133,6 +151,7 @@ private static ShipLogAstroObject AddShipLogAstroObject(GameObject gameObject, N

ShipLogAstroObject astroObject = gameObject.AddComponent<ShipLogAstroObject>();
astroObject._id = ShipLogHandler.GetAstroObjectId(body);
_astroObjectToShipLog[body.Object] = astroObject;

Texture2D image = null;
Texture2D outline = null;
Expand Down Expand Up @@ -604,5 +623,68 @@ private static Color GetDominantPlanetColor(NewHorizonsBody body)

return Color.white;
}

#region Replacement
private static List<(NewHorizonsBody, ModBehaviour, MapModeInfo)> _mapModIconsToUpdate = new();
public static void TryReplaceExistingMapModeIcon(NewHorizonsBody body, ModBehaviour mod, MapModeInfo info)
{
if (!string.IsNullOrEmpty(info.revealedSprite) || !string.IsNullOrEmpty(info.outlineSprite))
{
_mapModIconsToUpdate.Add((body, mod, info));
}
}

private static void ReplaceExistingMapModeIcons()
{
foreach (var (body, mod, info) in _mapModIconsToUpdate)
{
ReplaceExistingMapModeIcon(body, mod, info);
}
_mapModIconsToUpdate.Clear();
}

private static void ReplaceExistingMapModeIcon(NewHorizonsBody body, ModBehaviour mod, MapModeInfo info)
{
var astroObject = _astroObjectToShipLog[body.Object];
var gameObject = astroObject.gameObject;
var layer = gameObject.layer;

if (!string.IsNullOrEmpty(info.revealedSprite))
{
var revealedTexture = ImageUtilities.GetTexture(body.Mod, info.revealedSprite);
if (revealedTexture == null)
{
NHLogger.LogError($"Couldn't load replacement revealed texture {info.revealedSprite}");
}
else
{
GameObject.Destroy(astroObject._imageObj);
if (ShipLogHandler.IsVanillaBody(body) || ShipLogHandler.BodyHasEntries(body))
{
Image revealedImage = astroObject._imageObj.GetComponent<Image>();
revealedImage.material = astroObject._greyscaleMaterial;
revealedImage.color = Color.white;
astroObject._image = revealedImage;
}
astroObject._imageObj = CreateImage(gameObject, revealedTexture, body.Config.name + " Revealed", layer);
}
}
if (!string.IsNullOrEmpty(info.outlineSprite))
{
var outlineTexture = ImageUtilities.GetTexture(body.Mod, info.outlineSprite);
if (outlineTexture == null)
{
NHLogger.LogError($"Couldn't load replacement outline texture {info.outlineSprite}");

}
else
{
GameObject.Destroy(astroObject._outlineObj);
astroObject._outlineObj = CreateImage(gameObject, outlineTexture, body.Config.name + " Outline", layer);
}
}
astroObject._invisibleWhenHidden = info.invisibleWhenHidden;
}
#endregion
}
}
16 changes: 12 additions & 4 deletions NewHorizons/Handlers/PlanetCreationHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
using NewHorizons.Builder.General;
using NewHorizons.Builder.Orbital;
using NewHorizons.Builder.Props;
using NewHorizons.Builder.ShipLog;
using NewHorizons.Builder.Volumes;
using NewHorizons.Components.Orbital;
using NewHorizons.Components.Quantum;
using NewHorizons.Components.Stars;
using NewHorizons.External;
using NewHorizons.OtherMods.OWRichPresence;
using NewHorizons.Streaming;
using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using NewHorizons.Utility.OuterWilds;
using NewHorizons.Utility.OWML;
using Newtonsoft.Json;
using OWML.ModHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using NewHorizons.Streaming;
using Newtonsoft.Json;
using NewHorizons.External.Modules.VariableSize;

namespace NewHorizons.Handlers
{
Expand Down Expand Up @@ -344,6 +345,13 @@ public static GameObject UpdateBody(NewHorizonsBody body, GameObject go)
// Do stuff that's shared between generating new planets and updating old ones
go = SharedGenerateBody(body, go, sector, rb);

if (body.Config.ShipLog?.mapMode != null)
{
MapModeBuilder.TryReplaceExistingMapModeIcon(body, body.Mod as ModBehaviour, body.Config.ShipLog.mapMode);
}

body.Object = go;

return go;
}

Expand Down

0 comments on commit 24a53cb

Please sign in to comment.