Skip to content

Commit

Permalink
fix paint application, comms radio behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
katycat5e committed Feb 3, 2025
1 parent bf6906b commit d772cb3
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 166 deletions.
68 changes: 46 additions & 22 deletions SkinManagerMod/CommsRadioSkinSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ public class CommsRadioSkinSwitcher : MonoBehaviour, ICommsRadioMode
private RaycastHit Hit;
private TrainCar SelectedCar = null;
private TrainCar PointedCar = null;
private bool HasInterior = false;
private MeshRenderer HighlighterRender;

private PaintArea AreaToPaint = PaintArea.All;
private PaintArea AlreadyPainted = PaintArea.None;

private List<CustomPaintTheme> SkinsForCarType = null;
private int SelectedSkinIdx = 0;
private CustomPaintTheme SelectedSkin = null;
private string CurrentThemeName = null;
private (string exterior, string interior) CurrentThemeName;

private const float SIGNAL_RANGE = 100f;
private static readonly Vector3 HIGHLIGHT_BOUNDS_EXTENSION = new Vector3(0.25f, 0.8f, 0f);
Expand Down Expand Up @@ -197,7 +200,7 @@ private void SetState(State newState)
case State.SelectSkin:
UpdateAvailableSkinsList(SelectedCar.carLivery);
SetSelectedSkin(SkinsForCarType?.FirstOrDefault());
(CurrentThemeName, _) = SkinManager.GetCurrentCarSkin(SelectedCar, false);
CurrentThemeName = SkinManager.GetCurrentCarSkin(SelectedCar, false);

ButtonBehaviour = ButtonBehaviourType.Override;
break;
Expand Down Expand Up @@ -253,7 +256,7 @@ public void OnUpdate()
{
PointToCar(trainCar);

if (!SkinProvider.IsBuiltInTheme(SelectedSkin) && (SelectedSkin.name == CurrentThemeName))
if (!HasInterior && !SkinProvider.IsBuiltInTheme(SelectedSkin) && (SelectedSkin.name == CurrentThemeName.exterior))
{
display.SetAction(Translations.ReloadAction);
}
Expand All @@ -277,7 +280,15 @@ public void OnUpdate()
(trainCar = TrainCar.Resolve(Hit.transform.root)) && (trainCar == SelectedCar))
{
PointToCar(trainCar);
display.SetAction(Translations.ConfirmAction);

if ((AlreadyPainted == AreaToPaint) && !SkinProvider.IsBuiltInTheme(SelectedSkin))
{
display.SetAction(Translations.ReloadAction);
}
else
{
display.SetAction(Translations.ConfirmAction);
}
}
else
{
Expand All @@ -296,16 +307,13 @@ private string AreaToPaintName
{
get
{
switch (AreaToPaint)
return AreaToPaint switch
{
case PaintArea.Exterior:
return CommsRadioLocalization.MODE_PAINTJOB_EXTERIOR;
case PaintArea.Interior:
return CommsRadioLocalization.MODE_PAINTJOB_INTERIOR;
case PaintArea.All:
default:
return CommsRadioLocalization.MODE_PAINTJOB_ALL;
PaintArea.Exterior => CommsRadioLocalization.MODE_PAINTJOB_EXTERIOR,
PaintArea.Interior => CommsRadioLocalization.MODE_PAINTJOB_INTERIOR,
_ => CommsRadioLocalization.MODE_PAINTJOB_ALL,
};
;
}
}

Expand All @@ -317,6 +325,7 @@ public void OnUse()
if (PointedCar != null)
{
SelectedCar = PointedCar;
HasInterior = SelectedCar.GetComponents<TrainCarPaint>().Any(tcp => tcp.TargetArea == TrainCarPaint.Target.Interior);
PointedCar = null;

HighlightCar(SelectedCar, skinningMaterial);
Expand All @@ -328,24 +337,22 @@ public void OnUse()
case State.SelectSkin:
if ((PointedCar != null) && (PointedCar == SelectedCar))
{
// clicked on the selected car again, this means confirm
if (!SkinProvider.IsBuiltInTheme(SelectedSkin) && (SelectedSkin.name == CurrentThemeName))
if (HasInterior)
{
SkinProvider.ReloadSkin(SelectedCar.carLivery.id, SelectedSkin.name);
ResetState();
SetState(State.SelectAreas);
}
else
{
if (SkinProvider.IsThemeable(PointedCar.carLivery.id))
// for regular cars, skip area selection
if (!SkinProvider.IsBuiltInTheme(SelectedSkin) && (SelectedSkin.name == CurrentThemeName.exterior))
{
SetState(State.SelectAreas);
SkinProvider.ReloadSkin(SelectedCar.carLivery.id, SelectedSkin.name);
}
else
{
// for regular cars, skip area selection
ApplySelectedSkin();
ResetState();
}
ResetState();
}
CommsRadioController.PlayAudioFromRadio(ConfirmSound, transform);
}
Expand All @@ -361,7 +368,14 @@ public void OnUse()
if ((PointedCar != null) && (PointedCar == SelectedCar))
{
// clicked on the selected car again, this means confirm
ApplySelectedSkin();
if ((AlreadyPainted == AreaToPaint) && !SkinProvider.IsBuiltInTheme(SelectedSkin))
{
SkinProvider.ReloadSkin(SelectedCar.carLivery.id, SelectedSkin.name);
}
else
{
ApplySelectedSkin();
}
CommsRadioController.PlayAudioFromRadio(ConfirmSound, transform);
}

Expand Down Expand Up @@ -440,7 +454,7 @@ private void ApplySelectedSkin()
}

SkinManager.ApplySkin(SelectedCar, SelectedSkin, AreaToPaint);
CurrentThemeName = SelectedSkin.name;
//CurrentThemeName = SelectedSkin.name;

if (CarTypes.IsMUSteamLocomotive(SelectedCar.carType) && SelectedCar.rearCoupler.IsCoupled())
{
Expand Down Expand Up @@ -469,6 +483,16 @@ private void SetSelectedSkin(CustomPaintTheme theme)
{
SelectedSkin = theme;

AlreadyPainted = PaintArea.None;
if (CurrentThemeName.exterior == SelectedSkin.name)
{
AlreadyPainted |= PaintArea.Exterior;
}
if (CurrentThemeName.interior == SelectedSkin.name)
{
AlreadyPainted |= PaintArea.Interior;
}

string displayName = $"{Translations.SelectPaintPrompt}\n{SelectedSkin.LocalizedName}";
display.SetContent(displayName);
}
Expand Down
31 changes: 7 additions & 24 deletions SkinManagerMod/Items/PaintFactory.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using DV;
using DV.CabControls;
using DV.CabControls;
using DV.Customization.Paint;
using DV.Interaction;
using DV.Localization;
using DV.Shops;
using DV.ThingTypes;
using OokiiTsuki.Palette;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -442,14 +439,15 @@ public static void InjectShopData()
if (ShopDataInjected) return;
Main.Log("Injecting global shop data");

if (UnityEngine.Object.FindObjectOfType<DisplayLoadingInfo>() is DisplayLoadingInfo info)
if (Object.FindObjectOfType<DisplayLoadingInfo>() is DisplayLoadingInfo info)
{
info.loadProgressTMP.richText = true;
const float LS_SATURATION = 0.7f;
const float LS_VALUE = 0.8f;
var color = (Color32)Random.ColorHSV(0.01f, 0.9f, LS_SATURATION, LS_SATURATION, LS_VALUE, LS_VALUE);

int colorIdx = UnityEngine.Random.Range(0, _loadingScreenColors.Length - 1);
var color = _loadingScreenColors[colorIdx];
string colString = $"{color.r:X2}{color.g:X2}{color.b:X2}";

info.loadProgressTMP.richText = true;
info.loadProgressTMP.text += $"\n<color=#{colString}>{Translations.LoadingScreen}</color>";
}

Expand Down Expand Up @@ -499,24 +497,9 @@ public static void DestroyInjectedShopData()
Main.Log("Destroying global shop data");

_dummyItemSpecs.Clear();
UnityEngine.Object.Destroy(_dummyItemSpecHolder);
Object.Destroy(_dummyItemSpecHolder);

ShopDataInjected = false;
}

private static float LS_SATURATION = 0.6f;
private static float LS_VALUE = 0.7f;

private static Color32[] _loadingScreenColors =
{
Color.HSVToRGB(0.0f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.1f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.15f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.25f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.5f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.6f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.75f, LS_SATURATION, LS_VALUE),
Color.HSVToRGB(0.85f, LS_SATURATION, LS_VALUE),
};
}
}
67 changes: 18 additions & 49 deletions SkinManagerMod/Patches/CarPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HarmonyLib;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;

namespace SkinManagerMod.Patches
{
Expand Down Expand Up @@ -37,75 +38,43 @@ private static void BaseSpawn(TrainCar __result, bool uniqueCar)
__result.gameObject.SetActive(true);
}

if (uniqueCar)
{
if (__result.PaintExterior)
{
SkinManager.SetAppliedCarSkin(__result, __result.PaintExterior.CurrentTheme.name, PaintArea.Exterior);
}
if (__result.PaintInterior)
{
SkinManager.SetAppliedCarSkin(__result, __result.PaintInterior.CurrentTheme.name, PaintArea.Interior);
}
}
else
if (!uniqueCar)
{
(string? exterior, string? interior) = SkinManager.GetCurrentCarSkin(__result);

if (__result.PaintExterior && exterior is not null && SkinProvider.TryGetTheme(exterior, out var exteriorTheme))
if (__result.PaintInterior && interior is not null && SkinProvider.TryGetTheme(interior, out var interiorTheme))
{
__result.PaintExterior.CurrentTheme = exteriorTheme;
SkinManager.SetAppliedCarSkin(__result, exterior, PaintArea.Exterior);
__result.PaintInterior.CurrentTheme = interiorTheme;
}

if (__result.PaintInterior && interior is not null && SkinProvider.TryGetTheme(interior, out var interiorTheme))
if (__result.PaintExterior && exterior is not null && SkinProvider.TryGetTheme(exterior, out var exteriorTheme))
{
__result.PaintInterior.CurrentTheme = interiorTheme;
SkinManager.SetAppliedCarSkin(__result, interior, PaintArea.Interior);
__result.PaintExterior.CurrentTheme = exteriorTheme;
}
}
}
}
/*
[HarmonyPatch]
class TrainCar_LoadInterior_Patch
{
static IEnumerable<MethodBase> TargetMethods()
{
yield return AccessTools.Method(typeof(TrainCar), nameof(TrainCar.LoadInterior));
yield return AccessTools.Method(typeof(TrainCar), nameof(TrainCar.LoadExternalInteractables));
yield return AccessTools.Method(typeof(TrainCar), nameof(TrainCar.LoadDummyExternalInteractables));
}

static void Postfix(TrainCar __instance)
[HarmonyPatch(typeof(TrainCar))]
internal static class TrainCarPatches
{
[HarmonyPatch(nameof(TrainCar.InitializeObjectPaint))]
[HarmonyPrefix]
public static void BeforeInitializePaint(GameObject obj)
{
if (SkinProvider.IsThemeable(__instance.carLivery)) return;
if (obj.GetComponent<TrainCar>()) return;

var skinName = SkinManager.GetCurrentCarSkin(__instance);
if (!string.IsNullOrEmpty(skinName))
foreach (var paint in obj.GetComponents<TrainCarPaint>())
{
SkinManager.ApplyNonThemeSkinToInterior(__instance, skinName);
Object.Destroy(paint);
}
}
}

[HarmonyPatch(typeof(TrainCarPaint))]
internal static class TrainCarPaintPatch
{
[HarmonyPatch(nameof(TrainCarPaint.CurrentTheme), MethodType.Setter)]
[HarmonyPatch(nameof(TrainCar.InitializeObjectPaint))]
[HarmonyPostfix]
public static void AfterCurrentThemeSet(TrainCarPaint __instance, PaintTheme ___currentTheme)
public static void AfterInitializePaint(TrainCar __instance)
{
var trainCar = TrainCar.Resolve(__instance.gameObject);
string themeName = ___currentTheme ? ___currentTheme.name : null;
Main.Log($"Applying skin {themeName} to car {trainCar.ID} {__instance.TargetArea}");
if (__instance.TargetArea != TrainCarPaint.Target.Exterior) return;
PaintArea area = __instance.TargetArea.ToPaintArea();
SkinManager.SetAppliedCarSkin(trainCar, themeName, area);
TrainCarPaintPatches.ReapplyThemes(__instance);
}
}
*/
}
Loading

0 comments on commit d772cb3

Please sign in to comment.