using UnityEngine;
// Sci-Fi Ship Controller. Copyright (c) 2018-2023 SCSM Pty Ltd. All rights reserved.
namespace SciFiShipController
{
///
/// Class containing data for a Ship Display's guage or visual measuring device.
///
[System.Serializable]
public class DisplayGauge
{
#region Enumerations
///
/// Equivalent to UI.Image.FillMethod
///
public enum DGFillMethod
{
Horizontal = 0,
Vertical = 1,
Radial90 = 2,
Radial180 = 3,
Radial360 = 4,
None = 99
}
///
/// The type or style of a gauge
///
public enum DGType
{
Filled = 0,
FilledNumber1 = 1,
NumberWithLabel1 = 10
}
///
/// The direction as rotation in degrees
///
public enum DGTextDirection
{
Horizontal = 0,
BottomTop = 1,
TopBottom = 2
}
///
/// Equavalent to UnityEngine.FontStyle
///
public enum DGFontStyle
{
Normal = 0,
Bold = 1,
Italic = 2,
BoldAndItalic = 3
}
#endregion
#region Public Static variables
public static int DGTypeFilledInt = (int)DGType.Filled;
public static int DGTypeFilledNumber1Int = (int)DGType.FilledNumber1;
public static int DGTypeNumberWithLabel1Int = (int)DGType.NumberWithLabel1;
#endregion
#region Public variables
// IMPORTANT - when changing this section also update SetClassDefault()
// Also update ClassName(ClassName className) Clone Constructor (if there is one)
///
/// The name or description of the gauge. This can be used to identify
/// the gauge. It is not displayed in the heads-up display.
///
public string gaugeName;
///
/// The text to display on the gauge. It can include RichText markup. e.g. Bold Text.
/// At runtime call shipDisplayModule.SetDisplayGaugeText(..)
///
public string gaugeString;
///
/// The label text on a numeric gauge with a label. It can include RichText markup. e.g. Bold Text.
/// For non-numeric gauges, see gaugeString.
///
public string gaugeLabel;
///
/// The current amount or reading on the gauge. Value must be between 0.0 (empty/min) and 1.0 (full/max)
///
[Range(0f,1f)] public float gaugeValue;
///
/// The type or style of the gauge. Default is Filled.
///
public DGType gaugeType;
///
/// Show (or hide) the gauge. At runtime use shipDisplayModule.ShowDisplayGauge() or HideDisplayGauge().
///
public bool showGauge;
///
/// The Display Gauge's normalised offset between the left (-1) and the right (1) from the centre (0) of the screen.
/// Offset is measured from the centre of the Gauge.
/// At runtime call shipDisplayModule.SetDisplayGaugeOffset(..)
///
[Range(-1f, 1f)] public float offsetX;
///
/// The Display Gauge's normalised offset between the bottom (-1) and the top (1) from the centre (0) of the screen.
/// Offset is measured from the centre of the Gauge.
/// At runtime call shipDisplayModule.SetDisplayGaugeOffset(..)
///
[Range(-1f, 1f)] public float offsetY;
///
/// The Display Gauge's normalised width. 1.0 is full screen width, 0.5 is half width.
/// At runtime call shipDisplayModule.SetDisplayGaugeSize(..)
///
[Range(0.01f, 1f)] public float displayWidth;
///
/// The Display Gauge's normalised height. 1.0 is full screen height, 0.5 is half height.
/// At runtime call shipDisplayModule.SetDisplayGaugeSize(..)
///
[Range(0.01f, 1f)] public float displayHeight;
///
/// Whether the gauge is shown as expanded in the inspector window of the editor.
///
public bool showInEditor;
///
/// Hashed GUID code to uniquely identify a gauge.
/// [INTERNAL USE ONLY]
///
public int guidHash;
///
/// Does the colour of the foreground change based on the value of the gauge?
/// At runtime call shipDisplayModule.SetDisplayGaugeValueAffectsColour(..)
///
public bool isColourAffectByValue;
///
/// When isColourAffectByValue is true, the value for the foreground medium colour [default: 0.5]
///
[Range(0.1f, 0.9f)] public float mediumColourValue;
///
/// Colour of the gauge foreground.
/// At runtime call shipDisplayModule.SetDisplayGaugeForegroundColour(..)
///
public Color foregroundColour;
///
/// High foreground colour when gaugeValue = 1.0
///
public Color foregroundHighColour;
///
/// Medium foreground colour when gaugeValue = 0.5
///
public Color foregroundMediumColour;
///
/// Low foreground colour when gaugeValue = 0
///
public Color foregroundLowColour;
///
/// The sprite (texture) for the foreground of the gauge
///
public Sprite foregroundSprite;
///
/// Colour of the gauge background.
/// At runtime call shipDisplayModule.SetDisplayGaugeBackgroundColour(..)
///
public Color backgroundColour;
///
/// The sprite (texture) for the background of the gauge
///
public Sprite backgroundSprite;
///
/// Determines the method used to fill the gauge foreground sprite when the gaugeValue is modified.
///
public DGFillMethod fillMethod;
///
/// Keep the original aspect ratio of the foreground and background sprites. Useful when creating circular gauges
///
public bool isKeepAspectRatio;
///
/// Colour of the gauge text.
/// At runtime call shipDisplayModule.SetDisplayGaugeTextColour(..).
///
public Color textColour;
///
/// The position of the text within the diplay gauge panel.
/// At runtime call shipDisplayModule.SetDisplayGaugeTextAlignment(..)
///
public TextAnchor textAlignment;
///
/// The position of the label within the display gauge panel.
/// This only applies to numeric gauges with a label.
/// At runtime call shipDisplayModule.SetDisplayGaugeLabelAlignment(..)
///
public TextAnchor labelAlignment;
///
/// The rotation of the text within the display gauge panel.
/// At runtime call shipDisplayModule.SetDisplayGaugeTextDirection(..)
///
public DGTextDirection textDirection;
///
/// The style of the text with the display gauge panel.
/// At runtime call shipDisplayGaugeTextFontStyle(..)
///
public DGFontStyle fontStyle;
///
/// Is the text font size automatically changes within the bounds of fontMinSize and fontMaxSize
/// to fill the panel?
/// At runtime call shipDisplayModule.SetDisplayGaugeTextFontSize(..)
///
public bool isBestFit;
///
/// When isBestFit is true will use this minimum font size if required.
/// At runtime call shipDisplayModule.SetDisplayGaugeTextFontSize(..)
///
public int fontMinSize;
///
/// The font size. If isBestFit is true, this will be the maximum font size it can use.
/// At runtime call shipDisplayModule.SetDisplayGaugeTextFontSize(..)
///
public int fontMaxSize;
///
/// When a numeric gaugeType is used, this is the number to display when gaugeValue = 1.0.
///
public float gaugeMaxValue;
///
/// The number of decimal places to display for numeric gauges
///
[Range(0,3)] public int gaugeDecimalPlaces;
///
/// Is the numeric gauge to be displayed as a percentage?
///
public bool isNumericPercentage;
#endregion
#region Public Properties
///
/// Is this a numeric Gauge with a label.
///
public bool HasLabel { get { return gaugeType == DGType.NumberWithLabel1; } }
#endregion
#region Private or Internal variables and properties - not serialized
///
/// [INTERNAL ONLY]
/// Once initialised, the RectTransform of the gauge panel
///
internal RectTransform CachedGaugePanel { get; set; }
///
/// [INTERNAL ONLY]
/// Once initialised, the foreground Image gauge component
///
internal UnityEngine.UI.Image CachedFgImgComponent { get; set; }
///
/// [INTERNAL ONLY]
/// Once initialised, the background Image gauge component
///
internal UnityEngine.UI.Image CachedBgImgComponent { get; set; }
///
/// [INTERNAL ONLY]
/// Once initialised, the gauge Text component
///
internal UnityEngine.UI.Text CachedTextComponent { get; set; }
///
/// [INTERNAL ONLY]
/// Once initialised, the label Text component for numeric gauges
///
internal UnityEngine.UI.Text CachedLabelTextComponent { get; set; }
///
/// [INTERNAL ONLY]
/// Used for background brightness
///
[System.NonSerialized] internal SSCColour baseForegroundColour;
///
/// [INTERNAL ONLY]
/// Used for background brightness
///
[System.NonSerialized] internal SSCColour baseBackgroundColour;
///
/// [INTERNAL ONLY]
/// Used for text brightness
///
[System.NonSerialized] internal SSCColour baseTextColour;
///
/// This is cached to avoid the enumeration lookup at runtime
///
internal bool isFillMethodNone;
private Transform CachedGaugePanelTfrm { get { return CachedGaugePanel == null ? null : CachedGaugePanel.transform; } }
#endregion
#region Constructors
public DisplayGauge()
{
SetClassDefaults();
}
///
/// DisplayMessage copy constructor
///
///
public DisplayGauge(DisplayGauge displayGauge)
{
if (displayGauge == null) { SetClassDefaults(); }
else
{
gaugeName = displayGauge.gaugeName;
gaugeString = displayGauge.gaugeString;
if (string.IsNullOrEmpty(displayGauge.gaugeLabel)) { displayGauge.gaugeLabel = string.Empty; }
else { gaugeLabel = string.Copy(displayGauge.gaugeLabel); }
gaugeValue = displayGauge.gaugeValue;
gaugeType = displayGauge.gaugeType;
showGauge = displayGauge.showGauge;
offsetX = displayGauge.offsetX;
offsetY = displayGauge.offsetY;
displayWidth = displayGauge.displayWidth;
displayHeight = displayGauge.displayHeight;
showInEditor = displayGauge.showInEditor;
guidHash = displayGauge.guidHash;
isColourAffectByValue = displayGauge.isColourAffectByValue;
mediumColourValue = displayGauge.mediumColourValue;
foregroundColour = new Color(displayGauge.foregroundColour.r, displayGauge.foregroundColour.g, displayGauge.foregroundColour.b, displayGauge.foregroundColour.a);
foregroundHighColour = new Color(displayGauge.foregroundHighColour.r, displayGauge.foregroundHighColour.g, displayGauge.foregroundHighColour.b, displayGauge.foregroundHighColour.a);
foregroundMediumColour = new Color(displayGauge.foregroundMediumColour.r, displayGauge.foregroundMediumColour.g, displayGauge.foregroundMediumColour.b, displayGauge.foregroundMediumColour.a);
foregroundLowColour = new Color(displayGauge.foregroundLowColour.r, displayGauge.foregroundLowColour.g, displayGauge.foregroundLowColour.b, displayGauge.foregroundLowColour.a);
foregroundSprite = displayGauge.foregroundSprite;
backgroundColour = new Color(displayGauge.backgroundColour.r, displayGauge.backgroundColour.g, displayGauge.backgroundColour.b, displayGauge.backgroundColour.a);
backgroundSprite = displayGauge.backgroundSprite;
fillMethod = displayGauge.fillMethod;
isKeepAspectRatio = displayGauge.isKeepAspectRatio;
textColour = new Color(displayGauge.textColour.r, displayGauge.textColour.g, displayGauge.textColour.b, displayGauge.textColour.a);
textAlignment = displayGauge.textAlignment;
labelAlignment = displayGauge.labelAlignment;
textDirection = displayGauge.textDirection;
isBestFit = displayGauge.isBestFit;
fontMinSize = displayGauge.fontMinSize;
fontMaxSize = displayGauge.fontMaxSize;
gaugeMaxValue = displayGauge.gaugeMaxValue;
gaugeDecimalPlaces = displayGauge.gaugeDecimalPlaces;
isNumericPercentage = displayGauge.isNumericPercentage;
// Clear cached values
CachedBgImgComponent = null;
CachedFgImgComponent = null;
CachedGaugePanel = null;
CachedLabelTextComponent = null;
CachedTextComponent = null;
}
}
#endregion
#region Public Member Methods
///
/// Set the defaults values for this class
///
public void SetClassDefaults()
{
gaugeName = string.Empty;
gaugeString = string.Empty;
gaugeLabel = string.Empty;
gaugeValue = 0f;
gaugeType = DGType.Filled;
showGauge = false;
// Default to centre of screen
offsetX = 0f;
offsetY = 0.5f;
displayWidth = 0.25f;
displayHeight = 0.05f;
showInEditor = false;
guidHash = SSCMath.GetHashCodeFromGuid();
isColourAffectByValue = false;
mediumColourValue = 0.5f;
foregroundColour = Color.grey;
foregroundHighColour = Color.grey;
foregroundMediumColour = Color.grey;
foregroundLowColour = Color.grey;
foregroundSprite = null;
backgroundColour = Color.white;
backgroundSprite = null;
fillMethod = DGFillMethod.Horizontal;
isKeepAspectRatio = false;
textColour = Color.black;
textAlignment = TextAnchor.MiddleLeft;
labelAlignment = TextAnchor.UpperLeft;
textDirection = DGTextDirection.Horizontal;
isBestFit = true;
fontMinSize = 10;
fontMaxSize = 36;
gaugeMaxValue = 100f;
gaugeDecimalPlaces = 0;
isNumericPercentage = false;
}
#endregion
}
}