Skip to content

Commit

Permalink
fixed but where window would expand to infinity when closed and opene…
Browse files Browse the repository at this point in the history
…d again
  • Loading branch information
CzBuCHi committed Jul 28, 2024
1 parent 616eb80 commit 5c8b05a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 63 deletions.
29 changes: 10 additions & 19 deletions CarInspectorResizer.Example1/HarmonyPatches/CarInspectorPatches.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace CarInspectorResizer.Example1.HarmonyPatches;

using System;
using System.Collections.Generic;
using CarInspectorResizer.Behaviors;
using Game.Messages;
using HarmonyLib;
Expand All @@ -22,21 +20,20 @@ internal static class CarInspectorPatches {
public static void Populate(ref Window ____window) {
var windowAutoHeight = ____window.gameObject!.GetComponent<CarInspectorAutoHeightBehavior>()!;
windowAutoHeight.ExpandAlways(30);
windowAutoHeight.ExpandTab("equipment", 50);
windowAutoHeight.ExpandOrders(AutoEngineerMode.Road, 50);
windowAutoHeight.UpdateWindowHeight();
windowAutoHeight.ExpandTab("equipment", 45);
windowAutoHeight.ExpandOrders(AutoEngineerMode.Road, 30);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "PopulatePanel")]
public static void PopulatePanel(UIPanelBuilder builder) {
builder.AddField("Global Button", builder.ButtonStrip(strip => strip.AddButton("Example1", () => { }), 4)!);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "PopulateEquipmentPanel")]
public static void PopulateEquipmentPanel(UIPanelBuilder builder) {
builder.AddSection("Equipment Buttons", section => {
section.ButtonStrip(strip => {
strip.AddButton("Example1", () => { });
strip.AddButton("Example1", () => { });
strip.AddButton("Example1", () => { });
}, 4);
});
builder.AddSection("Equipment Buttons", section => section.ButtonStrip(strip => strip.AddButton("Example1", () => { }), 4));
}

[HarmonyPostfix]
Expand All @@ -45,13 +42,7 @@ public static void BuildContextualOrders(UIPanelBuilder builder, AutoEngineerPer
var helper = new AutoEngineerOrdersHelper(____car, persistence);
var mode = helper.Mode();
if (mode == AutoEngineerMode.Road) {
builder.AddSection("Road Buttons", section => {
section.ButtonStrip(strip => {
strip.AddButton("Example1", () => { });
strip.AddButton("Example1", () => { });
strip.AddButton("Example1", () => { });
}, 4);
});
builder.AddField("Road Buttons", builder.ButtonStrip(strip => strip.AddButton("Example1", () => { }), 4)!);
}
}

Expand Down
33 changes: 4 additions & 29 deletions CarInspectorResizer.Example2/HarmonyPatches/CarInspectorPatches.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace CarInspectorResizer.Example2.HarmonyPatches;

using System;
using System.Collections.Generic;
using CarInspectorResizer.Behaviors;
using Game.Messages;
using HarmonyLib;
Expand All @@ -21,45 +19,22 @@ internal static class CarInspectorPatches {
[HarmonyPatch(typeof(CarInspector), "Populate")]
public static void Populate(ref Window ____window) {
var windowAutoHeight = ____window.gameObject!.GetComponent<CarInspectorAutoHeightBehavior>()!;
windowAutoHeight.ExpandOrders(AutoEngineerMode.Road, 100);
windowAutoHeight.ExpandOrders(AutoEngineerMode.Yard, 50);
windowAutoHeight.UpdateWindowHeight();
windowAutoHeight.ExpandOrders(AutoEngineerMode.Road, 30);
windowAutoHeight.ExpandOrders(AutoEngineerMode.Yard, 30);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "BuildContextualOrders")]
public static void BuildContextualOrders(UIPanelBuilder builder, AutoEngineerPersistence persistence, Car ____car) {

var helper = new AutoEngineerOrdersHelper(____car, persistence);
var mode = helper.Mode();
switch (mode) {
case AutoEngineerMode.Road:
builder.AddSection("Road Buttons", section => {
section.ButtonStrip(strip => {
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
}, 4);
});

builder.AddSection("More Road buttons", section => {
section.ButtonStrip(strip => {
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
}, 4);
});
builder.AddField("Road Button", builder.ButtonStrip(strip => strip.AddButton("Example2", () => { }), 4)!);
break;

case AutoEngineerMode.Yard:
builder.AddSection("Yard Buttons", section => {
section.ButtonStrip(strip => {
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
strip.AddButton("Example2", () => { });
}, 4);
});

builder.AddField("Yard Button", builder.ButtonStrip(strip => strip.AddButton("Example2", () => { }), 4)!);
break;
}
}
Expand Down
44 changes: 34 additions & 10 deletions CarInspectorResizer/Behaviors/CarInspectorAutoHeightBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,79 +24,103 @@ public sealed class CarInspectorAutoHeightBehavior : MonoBehaviour {
private readonly Dictionary<string, float> _TabExpansions = new();
private readonly Dictionary<AutoEngineerMode, float> _OrdersExpansions = new();

private static Vector2? _OriginalWindowSize;

public float MinHeight { get; set; } = 330;

public void Awake() {
_Window = gameObject!.GetComponent<Window>()!;
SelectedTabState.ValueChanged = _ => UpdateWindowHeight();
SelectedTabState.ValueChanged = value => {
CarInspectorResizerPlugin.ConsoleMessage($"SelectedTabState: {value}");
UpdateWindowHeight();
};
}

internal void Populate(Car car) {
_ExpandAlways = 0;
_TabExpansions.Clear();
_OrdersExpansions.Clear();

if (_Car == car) {
return;
}

_Car = car;

_OriginalWindowSize ??= _Window.GetContentSize();

foreach (var observer in _Observers) {
observer.Dispose();
}

_Observers.Clear();

var persistence = new AutoEngineerPersistence(_Car.KeyValueObject!);
_Observers.Add(persistence.ObserveOrders(_ => UpdateWindowHeight()));
_Observers.Add(persistence.ObserveContextualOrdersChanged(UpdateWindowHeight));
_Observers.Add(persistence.ObserveOrders(_ => {
UpdateWindowHeight();
}, false));
_Observers.Add(persistence.ObserveContextualOrdersChanged(() => {
UpdateWindowHeight();
}));
}

public void ExpandAlways(float height) {
_ExpandAlways += height;
CarInspectorResizerPlugin.ConsoleMessage($"expanded by {height} to {_ExpandAlways}");
}

public void ExpandTab(string tabName, float height) {

_TabExpansions.TryGetValue(tabName, out var value);
_TabExpansions[tabName] = value + height;
CarInspectorResizerPlugin.ConsoleMessage($"'{tabName}' expanded by {height} to {_TabExpansions[tabName]}");
}

public void ExpandOrders(AutoEngineerMode mode, float height) {
_OrdersExpansions.TryGetValue(mode, out var value);
_OrdersExpansions[mode] = value + height;
CarInspectorResizerPlugin.ConsoleMessage($"'{mode}' expanded by {height} to {_OrdersExpansions[mode]}");
}

public void UpdateWindowHeight() {
if (_Car == null || _OriginalWindowSize == null) {
if (_Car == null || SelectedTabState.Value == null) {
return;
}

var persistence = new AutoEngineerPersistence(_Car.KeyValueObject!);
var helper = new AutoEngineerOrdersHelper(_Car, persistence);
var mode = helper.Mode();

var height = _OriginalWindowSize.Value.y + _ExpandAlways;
var height = MinHeight + _ExpandAlways;
if (_ExpandAlways > 0) {
CarInspectorResizerPlugin.ConsoleMessage($"+{_ExpandAlways}");
}

if (SelectedTabState.Value != null) {
_TabExpansions.TryGetValue(SelectedTabState.Value, out var tabExpansion);
height += tabExpansion;
if (tabExpansion > 0) {
CarInspectorResizerPlugin.ConsoleMessage($"+{tabExpansion} (tabExpansion)");
}
}

if (SelectedTabState.Value == "orders") {
_OrdersExpansions.TryGetValue(mode, out var ordersExpansion);
height += ordersExpansion;
if (ordersExpansion > 0) {
CarInspectorResizerPlugin.ConsoleMessage($"+{ordersExpansion} (ordersExpansion)");
}

if (!string.IsNullOrEmpty(persistence.PassengerModeStatus!)) {
height += 30;
CarInspectorResizerPlugin.ConsoleMessage("+30 (PassengerModeStatus)");
}

if (persistence.ContextualOrders!.Count > 0) {
height += 30;
CarInspectorResizerPlugin.ConsoleMessage("+30 (ContextualOrders)");
}
}

_Window.SetContentSize(new Vector2(_OriginalWindowSize.Value.x, Mathf.Max(MinHeight, height)));
var size = _Window.GetContentSize();
CarInspectorResizerPlugin.ConsoleMessage($"Updated window height {height}");
_Window.SetContentSize(new Vector2(size.x, height));
}

}
2 changes: 1 addition & 1 deletion CarInspectorResizer/CarInspectorResizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<!-- Optionally, set a few things to your liking -->
<MajorVersion>1</MajorVersion>
<MinorVersion>1</MinorVersion>
<MinorVersion>2</MinorVersion>
<IsMod>true</IsMod>
<PackageMod>enable</PackageMod>
<Nullable>enable</Nullable>
Expand Down
29 changes: 26 additions & 3 deletions CarInspectorResizer/CarInspectorResizerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,53 @@ namespace CarInspectorResizer;
using JetBrains.Annotations;
using Railloader;
using Serilog;
using UI.Builder;

[UsedImplicitly]
public sealed class CarInspectorResizerPlugin : SingletonPluginBase<CarInspectorResizerPlugin> {
public sealed class CarInspectorResizerPlugin : SingletonPluginBase<CarInspectorResizerPlugin>, IModTabHandler {

private const string ModIdentifier = "CarInspectorResizer";

public static IModdingContext Context { get; private set; } = null!;
public static IUIHelper UiHelper { get; private set; } = null!;
public static Settings Settings { get; private set; } = null!;

private readonly ILogger _Logger = Log.ForContext<CarInspectorResizerPlugin>()!;

public CarInspectorResizerPlugin(IModdingContext context, IUIHelper uiHelper) {
Context = context;
UiHelper = uiHelper;
Settings = Context.LoadSettingsData<Settings>(ModIdentifier) ?? new Settings();
}

public override void OnEnable() {
_Logger.Information("OnEnable");
var harmony = new Harmony("CarInspectorResizer");
var harmony = new Harmony(ModIdentifier);
harmony.PatchAll();
}

public override void OnDisable() {
_Logger.Information("OnDisable");
var harmony = new Harmony("CarInspectorResizer");
var harmony = new Harmony(ModIdentifier);
harmony.UnpatchAll();
}

public void ModTabDidOpen(UIPanelBuilder builder) {
builder
.AddField("Debug messages",
builder.AddToggle(() => Settings.Debug, o => Settings.Debug = o)!
)!
.Tooltip("Debug messages", "Send debug messages to console");
}

public void ModTabDidClose() {
Context.SaveSettingsData(ModIdentifier, Settings);
}

public static void ConsoleMessage(string message) {
if (Settings.Debug) {
UI.Console.Console.shared!.AddLine("[CIR]: " + message);
}
}

}
17 changes: 16 additions & 1 deletion CarInspectorResizer/HarmonyPatches/CarInspectorPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,26 @@ public static void Awake(ref Window ____window, ref HashSet<IDisposable> ____obs
____selectedTabState = windowAutoHeight.SelectedTabState;
}

[HarmonyPrefix]
[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "Populate")]
public static void Populate(Car car, ref Window ____window) {
var windowAutoHeight = ____window.gameObject!.GetComponent<CarInspectorAutoHeightBehavior>()!;
windowAutoHeight.Populate(car);
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "Show")]
public static void Show(Car car) {
var instance = Traverse.Create<CarInspector>()!.Field("_instance")!.GetValue<CarInspector>();
var windowAutoHeight = instance!.gameObject!.GetComponent<CarInspectorAutoHeightBehavior>()!;
windowAutoHeight.UpdateWindowHeight();
}

[HarmonyPostfix]
[HarmonyPatch(typeof(CarInspector), "Rebuild")]
public static void Rebuild(ref Window ____window) {
var windowAutoHeight = ____window.gameObject!.GetComponent<CarInspectorAutoHeightBehavior>()!;
windowAutoHeight.UpdateWindowHeight();
}

}
6 changes: 6 additions & 0 deletions CarInspectorResizer/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CarInspectorResizer;

public sealed class Settings {

public bool Debug { get; set; }
}

0 comments on commit 5c8b05a

Please sign in to comment.