Skip to content

Commit

Permalink
Allow widget slot count of zero, hide widget if zero. Better top widg…
Browse files Browse the repository at this point in the history
…et overrides.
  • Loading branch information
CptMoore committed Dec 3, 2023
1 parent 424e576 commit cc0b74d
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 112 deletions.
13 changes: 8 additions & 5 deletions source/Features/AutoFix/ChassisHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static void AutoFixSlots(ChassisDef chassisDef)
var location = locations[i].Location;
foreach (var change in changes.Where(x => x.Location == location).Select(x => x.Change))
{
ModifyInventorySlots(ref locations[i], location, change);
ModifyInventorySlots(chassisDef, ref locations[i], location, change);
}
}

Expand All @@ -93,8 +93,12 @@ private static void AutoFixLocationNaming(ChassisDef chassisDef)
}
}

private static void ModifyInventorySlots(ref LocationDef locationDef, ChassisLocations location, ValueChange<int> change)
{
private static void ModifyInventorySlots(
ChassisDef chassisDef,
ref LocationDef locationDef,
ChassisLocations location,
ValueChange<int> change
) {
if (locationDef.Location != location)
{
return;
Expand All @@ -108,8 +112,7 @@ private static void ModifyInventorySlots(ref LocationDef locationDef, ChassisLoc

if (location == ChassisLocations.CenterTorso)
{
newValue += MechLabSlotsFeature.settings.TopLeftWidget.Slots +
MechLabSlotsFeature.settings.TopRightWidget.Slots;
newValue += CustomWidgetsFixMechLab.GetCustomWidgetSlotsCount(chassisDef);
}

var info = typeof(LocationDef).GetField("InventorySlots");
Expand Down
12 changes: 4 additions & 8 deletions source/Features/DynamicSlots/DynamicSlotsFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ private class DynamicSlotBuilder : IComparable<DynamicSlotBuilder>
internal readonly MechLabLocationWidget widget;
internal int currentFreeSlots;
internal int fixedSlots;
internal int customSlotsInCenterTorso;

internal DynamicSlotBuilder(
MechDefBuilder builder,
Expand All @@ -59,6 +60,7 @@ private class DynamicSlotBuilder : IComparable<DynamicSlotBuilder>
fixedSlots = locationInfo.Fixed;

maxSlots = widget.maxSlots;
customSlotsInCenterTorso = CustomWidgetsFixMechLab.GetCustomWidgetSlotsCount(widget.mechLab.activeMechDef.Chassis);
}

int IComparable<DynamicSlotBuilder>.CompareTo(DynamicSlotBuilder other)
Expand All @@ -82,8 +84,7 @@ internal int currentFreeSlotIndex
var index = maxSlots - currentFreeSlots;
if (location == ChassisLocations.CenterTorso)
{
index -= MechLabSlotsFeature.settings.TopLeftWidget.Slots;
index -= MechLabSlotsFeature.settings.TopRightWidget.Slots;
index -= customSlotsInCenterTorso;
}
return index;
}
Expand Down Expand Up @@ -180,11 +181,6 @@ public void ValidateMech(MechDef mechDef, Errors errors)
builder.HasOveruseAtAnyLocation(errors);
}

internal static void PrepareWidget(WidgetLayout widgetLayout)
{
AddFillersToSlots(widgetLayout);
}

internal static void ClearFillers(MechLabLocationWidget widget)
{
if (Fillers.TryGetValue(widget.loadout.Location, out var fillers))
Expand All @@ -204,7 +200,7 @@ internal static void ShowFiller(MechLabLocationWidget widget, DynamicSlots slots

private static Dictionary<ChassisLocations, List<Filler>> Fillers = new();

private static void AddFillersToSlots(WidgetLayout layout)
internal static void PrepareFillerSlots(WidgetLayout layout)
{
var location = layout.widget.loadout.Location;

Expand Down
4 changes: 2 additions & 2 deletions source/Features/MechLabSlots/CustomWidgetChassisCustom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace MechEngineer.Features.MechLabSlots;
[CustomComponent("CustomWidgetChassis")]
public class CustomWidgetChassisCustom : SimpleCustomChassis
{
public bool? TopLeftWidgetEnabled { get; set; }
public bool? TopRightWidgetEnabled { get; set; }
internal MechLabSlotsSettings.WidgetOverrideSettings TopLeftWidget { get; set; } = new();
internal MechLabSlotsSettings.WidgetOverrideSettings TopRightWidget { get; set; } = new();
}
103 changes: 52 additions & 51 deletions source/Features/MechLabSlots/CustomWidgetsFixMechLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using BattleTech;
using BattleTech.UI;
using CustomComponents;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
Expand All @@ -26,24 +25,21 @@ internal static void Setup(MechLabPanel mechLabPanel)
"TopLeftWidget",
ref TopLeftWidget,
mechLabPanel,
mechLabPanel.rightArmWidget,
MechLabSlotsFeature.settings.TopLeftWidget
mechLabPanel.rightArmWidget
);

SetupWidget(
"TopRightWidget",
ref TopRightWidget,
mechLabPanel,
mechLabPanel.leftArmWidget,
MechLabSlotsFeature.settings.TopRightWidget
mechLabPanel.leftArmWidget
);
}

internal static void SetupWidget(string id,
private static void SetupWidget(string id,
ref MechLabLocationWidget? topWidget,
MechLabPanel mechLabPanel,
MechLabLocationWidget armWidget,
MechLabSlotsSettings.WidgetSettings settings)
MechLabLocationWidget armWidget)
{
GameObject go;
if (topWidget == null)
Expand All @@ -52,7 +48,7 @@ internal static void Setup(MechLabPanel mechLabPanel)

go = Object.Instantiate(template.gameObject, null);
go.name = id;
go.SetActive(settings.Enabled);
go.SetActive(true);
{
var vlg = go.GetComponent<VerticalLayoutGroup>();
vlg.padding = new RectOffset(0, 0, 0, 3);
Expand All @@ -62,16 +58,17 @@ internal static void Setup(MechLabPanel mechLabPanel)
go.transform.Find("layout_armor").gameObject.SetActive(false);
go.transform.Find("layout_hardpoints").gameObject.SetActive(false);
go.transform.Find("layout_locationText/txt_structure").gameObject.SetActive(false);
go.transform.Find("layout_locationText/txt_location").GetComponent<TextMeshProUGUI>().text = settings.Label;

topWidget = go.GetComponent<MechLabLocationWidget>();
}
else
{
go = topWidget.gameObject;
go.SetActive(settings.Enabled);
go.SetActive(true);
}

topWidget.Init(mechLabPanel);

var parent = armWidget.transform.parent;
go.transform.SetParent(parent, false);
go.transform.SetAsFirstSibling();
Expand All @@ -86,17 +83,6 @@ internal static void Setup(MechLabPanel mechLabPanel)
var clg = parent.GetComponent<VerticalLayoutGroup>();
clg.padding = new RectOffset(0, 0, MechLabSlotsFeature.settings.MechLabArmTopPadding, 0);
}

topWidget.Init(mechLabPanel);

// allow modify layout to go to 0 and
var layout = new WidgetLayout(topWidget);
MechLabSlotsFixer.ModifyLayoutSlotCount(layout, settings.Slots);

{
var mechRectTransform = parent.parent.GetComponent<RectTransform>();
LayoutRebuilder.ForceRebuildLayoutImmediate(mechRectTransform);
}
}

internal static void OnAdditem_SetParent(Transform @this, Transform parent, bool worldPositionStays)
Expand All @@ -122,30 +108,24 @@ internal static void OnAdditem_SetParent(Transform @this, Transform parent, bool

private static MechLabLocationWidget? MechWidgetLocation(MechComponentDef? def)
{
if (def != null && def.Is<CustomWidget>(out var config))
if (def == null || !def.Is<CustomWidget>(out var config))
{
if (config.Location == CustomWidget.MechLabWidgetLocation.TopLeft
&& MechLabSlotsFeature.settings.TopLeftWidget.Enabled)
{
return TopLeftWidget;
}

if (config.Location == CustomWidget.MechLabWidgetLocation.TopRight
&& MechLabSlotsFeature.settings.TopRightWidget.Enabled)
{
return TopRightWidget;
}
return null;
}

return null;
return config.Location switch
{
CustomWidget.MechLabWidgetLocation.TopLeft => TopLeftWidget,
CustomWidget.MechLabWidgetLocation.TopRight => TopRightWidget,
_ => null
};
}

internal static bool OnDrop(MechLabLocationWidget widget, PointerEventData eventData)
{
if (IsCustomWidget(widget))
{
var mechLab = (MechLabPanel)widget.parentDropTarget;
mechLab.centerTorsoWidget.OnDrop(eventData);
widget.mechLab.centerTorsoWidget.OnDrop(eventData);
return true;
}

Expand Down Expand Up @@ -195,29 +175,50 @@ internal static void RefreshDropHighlights(MechLabLocationWidget widget, IMechLa
return false;
}

internal static void ShowOrHideCustomWidgets(MechLabLocationWidget widget)
{
internal static void GetCustomWidgetsAndSlots(
ChassisDef chassisDef,
out MechLabSlotsSettings.WidgetSettings topLeftSettings,
out MechLabSlotsSettings.WidgetSettings topRightSettings,
out MechLabLocationWidget topLeftWidget,
out MechLabLocationWidget topRightWidget
) {
if (TopLeftWidget == null || TopRightWidget == null)
{
Log.Main.Warning?.Log("Top widgets not initialized even though they should be");
return;
throw new Exception("Wtf, too early");
}

if (widget.loadout.Location != ChassisLocations.CenterTorso)
topLeftWidget = TopLeftWidget;
topRightWidget = TopRightWidget;
var custom = chassisDef.GetComponent<CustomWidgetChassisCustom>();
if (custom == null)
{
topLeftSettings = MechLabSlotsFeature.settings.TopLeftWidget;
topRightSettings = MechLabSlotsFeature.settings.TopRightWidget;
return;
}

var custom = widget.mechLab.activeMechDef.Chassis.GetComponent<CustomWidgetChassisCustom>();
static void ShowOrHideCustomWidget(
MechLabLocationWidget customWidget,
bool? enabled,
MechLabSlotsSettings.WidgetSettings settings
)
{
customWidget.gameObject.SetActive(enabled ?? settings.Enabled);
MechLabSlotsSettings.WidgetSettings Create(
MechLabSlotsSettings.WidgetSettings defaults,
MechLabSlotsSettings.WidgetOverrideSettings overrides
) {
return new()
{
Label = overrides.Label ?? defaults.Label,
ShortLabel = overrides.ShortLabel ?? defaults.ShortLabel,
Slots = overrides.Slots ?? defaults.Slots,
};
}
ShowOrHideCustomWidget(TopLeftWidget, custom?.TopLeftWidgetEnabled, MechLabSlotsFeature.settings.TopLeftWidget);
ShowOrHideCustomWidget(TopRightWidget, custom?.TopRightWidgetEnabled, MechLabSlotsFeature.settings.TopRightWidget);

topLeftSettings = Create(MechLabSlotsFeature.settings.TopLeftWidget, custom.TopLeftWidget);
topRightSettings = Create(MechLabSlotsFeature.settings.TopRightWidget, custom.TopRightWidget);
}

internal static int GetCustomWidgetSlotsCount(ChassisDef chassisDef)
{
var custom = chassisDef.GetComponent<CustomWidgetChassisCustom>();
var topLeftSlots = custom?.TopLeftWidget.Slots ?? MechLabSlotsFeature.settings.TopLeftWidget.Slots;
var topRightSlots = custom?.TopRightWidget.Slots ?? MechLabSlotsFeature.settings.TopRightWidget.Slots;
return topLeftSlots + topRightSlots;
}
}
75 changes: 61 additions & 14 deletions source/Features/MechLabSlots/MechLabSlotsFixer.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,73 @@
using System.Linq;
using BattleTech;
using BattleTech.UI;
using MechEngineer.Features.DynamicSlots;
using MechEngineer.Features.OverrideTonnage;
using MechEngineer.Helper;
using UnityEngine;

namespace MechEngineer.Features.MechLabSlots;

internal static class MechLabSlotsFixer
{
internal static void FixSlots(WidgetLayout widgetLayout, int maxSlots)
private static GameObject? s_slotTemplate;

internal static void FixWidgetSlotsActiveNamingFillers(MechLabLocationWidget widget)
{
var mechLabPanel = (MechLabPanel)widgetLayout.widget.parentDropTarget;
// MechPropertiesWidget feature
if (widgetLayout.widget == mechLabPanel.centerTorsoWidget)
var widgetLayout = new WidgetLayout(widget);

if (s_slotTemplate == null)
{
s_slotTemplate = Object.Instantiate(widgetLayout.slots[0].gameObject, SharedGameObjects.ContainerTransform);
s_slotTemplate.SetActive(false); // everything from pool should already be deactivated
}

var chassisDef = widget.mechLab.activeMechDef.Chassis;
var locationDef = widget.chassisLocationDef;

var maxSlots = widget.maxSlots;

if (locationDef.Location == ChassisLocations.CenterTorso)
{
maxSlots = Mathf.Max(0,
maxSlots - MechLabSlotsFeature.settings.TopLeftWidget.Slots -
MechLabSlotsFeature.settings.TopRightWidget.Slots);
// MechPropertiesWidget feature
// a bit much being exposed
CustomWidgetsFixMechLab.GetCustomWidgetsAndSlots(
chassisDef,
out var topLeftSettings,
out var topRightSettings,
out var topLeftWidget,
out var topRightWidget
);
maxSlots -= topLeftSettings.Slots + topRightSettings.Slots;

ModifyLayoutSlots(new WidgetLayout(topLeftWidget), topLeftSettings.Slots);
topLeftWidget.gameObject.SetActive(topLeftSettings.Slots > 0);
topLeftWidget.locationName.SetText(topLeftSettings.Label);

// duplication
ModifyLayoutSlots(new WidgetLayout(topRightWidget), topRightSettings.Slots);
topRightWidget.gameObject.SetActive(topRightSettings.Slots > 0);
topRightWidget.locationName.SetText(topRightSettings.Label);
}

ModifyLayoutSlotCount(widgetLayout, maxSlots);
ModifyLayoutSlots(widgetLayout, maxSlots);
widget.gameObject.SetActive(locationDef.InventorySlots > 0 && !LegacyShouldHide(locationDef));
var text = ChassisLocationNamingUtils.GetLocationLabel(chassisDef, locationDef.Location);
widget.locationName.SetText(text);

DynamicSlotsFeature.PrepareFillerSlots(widgetLayout);
}

internal static void ModifyLayoutSlotCount(WidgetLayout layout, int maxSlots)
private static bool LegacyShouldHide(LocationDef def)
{
// old way of hiding
// hide any location with maxArmor <= 0 && structure <= 1
// for vehicles and troopers
return PrecisionUtils.SmallerOrEqualsTo(def.MaxArmor, 0)
&& PrecisionUtils.SmallerOrEqualsTo(def.InternalStructure, 1);
}

private static void ModifyLayoutSlots(WidgetLayout layout, int maxSlots)
{
var slots = layout.slots;
maxSlots = Mathf.Max(maxSlots, 1);
Expand All @@ -33,22 +80,22 @@ internal static void ModifyLayoutSlotCount(WidgetLayout layout, int maxSlots)
{
var slot = slots.Last();
slots.RemoveAt(slots.Count - 1);
slot.gameObject.SetActive(false);
Object.Destroy(slot.gameObject);
}
}
else if (changedSlotCount > 0)
{
var templateSlot = slots[0];

// add missing
var index = slots[0].GetSiblingIndex();
for (var i = slots.Count; i < maxSlots; i++)
{
var newSlot = Object.Instantiate(templateSlot, layout.layout_slots);
var newSlot = Object.Instantiate(s_slotTemplate!, layout.layout_slots);
//newSlot.localPosition = new Vector3(0, -(1 + i * SlotHeight), 0);
newSlot.SetSiblingIndex(index + i);
newSlot.transform.SetSiblingIndex(index + i);
newSlot.name = "slot (" + i + ")";
slots.Add(newSlot);
newSlot.SetActive(true);
slots.Add(newSlot.transform);
}
}
}
Expand Down
Loading

0 comments on commit cc0b74d

Please sign in to comment.