Skip to content

Commit

Permalink
live guard
Browse files Browse the repository at this point in the history
  • Loading branch information
kianzarrin committed Aug 2, 2022
1 parent 2650aee commit b18f883
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 29 deletions.
23 changes: 15 additions & 8 deletions UnifiedUILib/GUI/MainPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void ResetSettings() {
ControlToDrag.value = ControlToDrag_DEF;
SwitchToPrevTool.value = SwitchToPrevTool_DEF;
ClearInfoPanelsOnToolChanged.value = ClearInfoPanelsOnToolChanged_DEF;
instance_?.LoadPosition();
if(instance_)instance_.LoadPosition();
}

#endregion
Expand All @@ -94,16 +94,23 @@ public static void ResetSettings() {
#region Instantiation

static MainPanel instance_;
public static MainPanel Instance =>
instance_ ??= UIView.GetAView().AddUIComponent(typeof(MainPanel)) as MainPanel;
public static MainPanel Instance {
get {
if(!instance_) instance_ = UIView.GetAView().AddUIComponent(typeof(MainPanel)) as MainPanel;
return instance_;
}
}


public static MainPanel RowInstance_ => instance_;

public static bool Exists => instance_;

public static void Ensure() => _ = Instance;

public static void Release() => DestroyImmediate(instance_?.gameObject);
public static void Release() {
if(instance_) DestroyImmediate(instance_.gameObject);
}

#endregion Instantiation

Expand All @@ -128,7 +135,7 @@ public override void OnDestroy() {
Destroy(MainAtlas);
this.SetAllDeclaredFieldsToNull();
instance_ = null;
DestroyImmediate(floatingButton_?.gameObject);
if(floatingButton_)DestroyImmediate(floatingButton_.gameObject);
base.OnDestroy();
}

Expand Down Expand Up @@ -273,7 +280,7 @@ public void Refresh() {

int visibleButtons = ModButtons.Count(_b => _b && _b.isVisibleSelf);
Log.Info("Visible buttons = " + visibleButtons);
floatingButton_.isVisible = visibleButtons > 0;
if(floatingButton_) floatingButton_.isVisible = visibleButtons > 0;
isVisible &= visibleButtons > 0;
if (visibleButtons == 0)
return;
Expand All @@ -288,10 +295,10 @@ public void Refresh() {
else
dragHandle_.tooltip = lblCaption_.tooltip = "";

floatingButton_?.Refresh();
if (floatingButton_) floatingButton_.Refresh();
DoRefreshButtons();
dragHandle_.width = lblCaption_.width; // minimum width
containerPanel_?.FitToChildrenWithPadding();
if(containerPanel_)containerPanel_.FitToChildrenWithPadding();
RefreshDragAndCaptionPos();
LoadPosition();
//Invalidate();
Expand Down
4 changes: 2 additions & 2 deletions UnifiedUILib/Helpers/UUICustomButton.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UnifiedUI.Helpers {
namespace UnifiedUI.Helpers {
using ColossalFramework.UI;
using System.Reflection;
using UnityEngine;
Expand All @@ -22,8 +22,8 @@ public bool IsPressed {
}

public void Release() {
Button = null;
Button?.Destroy();
Button = null;
}
}
}
4 changes: 3 additions & 1 deletion UnifiedUILib/Helpers/UUIHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ public static void RegisterHotkeys(
/// <summary>
/// Destroy all gameObjects, components, and children
/// </summary>
public static void Destroy(this Component button) => GameObject.Destroy(button?.gameObject);
public static void Destroy(this Component button) {
if (button) GameObject.Destroy(button.gameObject);
}

/// <summary>
/// Gets the path to the mod that has the user mod instance.
Expand Down
16 changes: 8 additions & 8 deletions UnifiedUILib/UnifiedUILib.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions UnifiedUIMod/GUI/ModButtons/GenericModButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ public override void Activate() {
Log.Debug("GenericModButton.Open() called for " + Name);
base.Activate();
SetTool(Tool);
Widnow?.Show();
if (Widnow) Widnow.Show();
}

public override void Deactivate() {
Log.Debug("GenericModButton.Close() called for " + Name);
base.Deactivate();
UnsetTool(Tool);
Widnow?.Hide();
if (Widnow) Widnow.Hide();
}

/// <summary>
Expand Down
9 changes: 5 additions & 4 deletions UnifiedUIMod/LifeCycle/UUISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static void ReviveDisabledKeys() {
}

public static void Reset() {
MainPanel.RowInstance_?.Close();
if(MainPanel.RowInstance_) MainPanel.RowInstance_.Close();
HideOriginalButtons.value = true;
HandleESC.value = true;
Grabber.Instance?.RemoveAll();
if(Grabber.Instance)Grabber.Instance.RemoveAll();
UUIGrabberData.ResetSettings();
FloatingButton.ResetSettings();
MainPanel.ResetSettings();
Expand Down Expand Up @@ -107,7 +107,7 @@ public static void Collisions(UIHelper helper) {

var keys = new List<SavedInputKey>();
foreach(var b in mainPanel.ModButtons) {
if(b?.ActivationKey != null)
if(b && b.ActivationKey != null)
keys.Add(b.ActivationKey);
}
keys.AddRange(mainPanel.CustomHotkeys.Keys);
Expand Down Expand Up @@ -151,8 +151,9 @@ public static void Collisions(UIHelper helper) {

static void OnConflictResolved(UIComponent c, UIComponentEventParameter p) {
try {
if (!c) return;
var mi =
c?.objectUserData
c.objectUserData
?.GetType()
?.GetMethod(nameof(UnsavedInputKey.OnConflictResolved), types: new Type [0] ,throwOnError: false);
mi?.Invoke(c.objectUserData, null);
Expand Down
4 changes: 2 additions & 2 deletions Version.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<AssemblyVersion>2.2.11.*</AssemblyVersion>
<FileVersion>2.2.11</FileVersion>
<AssemblyVersion>2.2.12.*</AssemblyVersion>
<FileVersion>2.2.12</FileVersion>
</PropertyGroup>
</Project>

0 comments on commit b18f883

Please sign in to comment.