Skip to content

Commit

Permalink
Added SetActiveState and Set Color Methods
Browse files Browse the repository at this point in the history
Added two public methods, SetActiveState and SetColor methods. SetActiveState should make it easier to toggle and untoggle buttons depending on the condition. SetColor detaches the color setting from the text making it more modular and flexible.
  • Loading branch information
Hobbeslionheart committed Apr 2, 2022
1 parent d4d0475 commit 30a7243
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions unity/Assets/Scripts/UI/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Assets.Scripts.Content;
using UnityEditor;
using UnityEngine.UI;
using System;
using UnityEngine.Events;

namespace Assets.Scripts.UI
{
Expand Down Expand Up @@ -52,7 +54,7 @@ public UIElement(Transform parent)
public void Destroy()
{
if(bg!=null)
Object.Destroy(bg);
UnityEngine.Object.Destroy(bg);
}

/// <summary>
Expand Down Expand Up @@ -136,7 +138,7 @@ public void SetImage(Texture2D texture,
{
if (!(fitter is null))
{
Object.Destroy(fitter);
UnityEngine.Object.Destroy(fitter);
}
return;
}
Expand Down Expand Up @@ -291,6 +293,11 @@ public void SetText(string content)
SetText(content, Color.white);
}

public void SetColor(Color textColor)
{
text.GetComponent<Text>().color = textColor;
}

/// <summary>
/// Set element text.</summary>
/// <param name="content">Text to display.</param>
Expand Down Expand Up @@ -517,7 +524,7 @@ public float GetStringWidth(string content, int fontSize, Font fontName)
}
textWidthObj.GetComponent<UnityEngine.UI.Text>().text = content;
width = (textWidthObj.GetComponent<UnityEngine.UI.Text>().preferredWidth / UIScaler.GetPixelsPerUnit()) + (textPaddingDefault * 2);
Object.Destroy(textWidthObj);
UnityEngine.Object.Destroy(textWidthObj);
textWidthObj = null;
return width;
}
Expand Down Expand Up @@ -554,7 +561,7 @@ public float GetStringHeight(string content, float width)
}
textHeightObj.GetComponent<UnityEngine.UI.Text>().text = content;
height = (textHeightObj.GetComponent<UnityEngine.UI.Text>().preferredHeight / UIScaler.GetPixelsPerUnit()) + (textPaddingDefault * 2);
Object.Destroy(textHeightObj);
UnityEngine.Object.Destroy(textHeightObj);
textHeightObj = null;
return height;
}
Expand Down Expand Up @@ -642,9 +649,50 @@ public virtual float HeightToTextPadding(float space = 0)
return newHeight;
}

/// <summary>
/// Sets the GameObject Name in Unity. Helps with debugging.
/// </summary>
/// <param name="name">The GameObject Name to name</param>
public void SetGameObjectName(string name)
{
bg.name = name;
}

/// <summary>
/// Sets the condition for the state toggle. UIElementBorder is created here as the color will change depending on the active state.
/// </summary>
/// <param name="condition">Condition to assess</param>
/// <param name="activestateifconditiontrue">The button state if the condition parameter is true. True for active, false for inactive</param>
/// <param name="action">Unity Action to assign to the button when active</param>
public void SetActiveState(bool condition, bool activestateifconditiontrue, UnityAction action = null)
{
//if Condition is true or is met
if (condition)
{
if (activestateifconditiontrue)
{
SetButton(action);
new UIElementBorder(this);
}
else
{
SetColor(Color.gray);
new UIElementBorder(this, Color.gray);
}
}
else
{
if (activestateifconditiontrue)
{
SetColor(Color.gray);
new UIElementBorder(this, Color.gray);
}
else
{
SetButton(action);
new UIElementBorder(this);
}
}
}
}
}

0 comments on commit 30a7243

Please sign in to comment.