Skip to content

Commit

Permalink
Moved CustomWidgetChassis logic to avoid NRE
Browse files Browse the repository at this point in the history
  • Loading branch information
CptMoore committed Dec 1, 2023
1 parent 6d0398c commit d917050
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
41 changes: 28 additions & 13 deletions source/Features/MechLabSlots/CustomWidgetsFixMechLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,28 @@ internal static bool IsCustomWidget(MechLabLocationWidget widget)

internal static void Setup(MechLabPanel mechLabPanel)
{
var cwcc = mechLabPanel.activeMechDef.Chassis.GetComponent<CustomWidgetChassisCustom>();

SetupWidget(
"TopLeftWidget",
ref TopLeftWidget,
mechLabPanel,
mechLabPanel.rightArmWidget,
MechLabSlotsFeature.settings.TopLeftWidget,
cwcc?.TopLeftWidgetEnabled
MechLabSlotsFeature.settings.TopLeftWidget
);

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

internal static void SetupWidget(string id,
ref MechLabLocationWidget? topWidget,
MechLabPanel mechLabPanel,
MechLabLocationWidget armWidget,
MechLabSlotsSettings.WidgetSettings settings,
bool? chassisEnabled)
MechLabSlotsSettings.WidgetSettings settings)
{
GameObject go;
if (topWidget == null)
Expand Down Expand Up @@ -100,11 +95,6 @@ internal static void Setup(MechLabPanel mechLabPanel)
var mechRectTransform = parent.parent.GetComponent<RectTransform>();
LayoutRebuilder.ForceRebuildLayoutImmediate(mechRectTransform);
}

if (chassisEnabled.HasValue)
{
go.SetActive(chassisEnabled.Value);
}
}

internal static void OnAdditem_SetParent(Transform @this, Transform parent, bool worldPositionStays)
Expand Down Expand Up @@ -202,4 +192,29 @@ internal static void RefreshDropHighlights(MechLabLocationWidget widget, IMechLa
cRef = null;
return false;
}

internal static void ShowOrHideCustomWidgets(MechLabLocationWidget widget)
{
if (widget.loadout.Location != ChassisLocations.CenterTorso)
{
return;
}

var custom = widget.mechLab.activeMechDef.Chassis.GetComponent<CustomWidgetChassisCustom>();
if (custom == null)
{
return;
}

static void ShowOrHideCustomWidget(MechLabLocationWidget? customWidget, bool? enabled)
{
if (customWidget == null || !enabled.HasValue)
{
return;
}
customWidget.gameObject.SetActive(enabled.Value);
}
ShowOrHideCustomWidget(TopLeftWidget, custom.TopLeftWidgetEnabled);
ShowOrHideCustomWidget(TopRightWidget, custom.TopRightWidgetEnabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static void Postfix(MechLabLocationWidget __instance, int ___maxSlots, re
MechLabSlotsFixer.FixSlots(widgetLayout, ___maxSlots);
DynamicSlotsFeature.PrepareWidget(widgetLayout);
AdjustMechLabLocationNaming(widget, loadout.Location);
CustomWidgetsFixMechLab.ShowOrHideCustomWidgets(widget);
}

private static void AdjustMechLabLocationNaming(MechLabLocationWidget widget, ChassisLocations location)
Expand Down

0 comments on commit d917050

Please sign in to comment.