using UnityEngine;
namespace PixelCrushers.DialogueSystem.UnityGUI
{
///
/// A GUI control that contains text. This is the parent class of GUILabel, GUIButton, and GUIWindow.
///
[AddComponentMenu("")] // Deprecated
public class GUIVisibleControl : GUIControl
{
///
/// The (optional) localized text table to use.
///
public LocalizedTextTable localizedText;
///
/// The text content, or the name of the field in the localized text table.
///
public string text;
///
/// The name of the GUI style to use to draw the text.
///
public string guiStyleName;
///
/// Gets or sets the alpha (transparency) value.
///
/// The alpha.
public float Alpha
{
get { return alpha; }
set { alpha = value; }
}
///
/// Gets a value indicating whether this instance has an alpha that isn't fully opaque.
///
/// true if this instance has alpha; otherwise, false.
public bool HasAlpha
{
get { return (Alpha < 0.999f) && Application.isPlaying; }
}
///
/// Gets the default GUI style to use for this type of control. It can be overridden on a per-control
/// basis using guiStyleName.
///
/// The default GUI style.
protected virtual GUIStyle DefaultGUIStyle
{
get { return GUI.skin.label; }
}
///
/// The current GUI style.
///
/// The GUI style.
protected GUIStyle GuiStyle
{
get { return guiStyle; }
set { guiStyle = value; }
}
private FormattedText formattingToApply = null;
private bool isFormattingApplied = false;
private GUIStyle guiStyle = null;
private Color originalGUIColor = Color.white;
private float alpha = 1;
private string originalTextValue = string.Empty;
public override void Awake()
{
base.Awake();
originalTextValue = text;
}
public virtual void Start()
{
if (localizedText != null) UseLocalizedText(localizedText);
}
public void UseLocalizedText(LocalizedTextTable localizedText)
{
this.localizedText = localizedText;
if (localizedText != null)
{
if (localizedText.ContainsField(originalTextValue))
{
text = localizedText[originalTextValue];
}
}
}
public void ApplyAlphaToGUIColor()
{
if (HasAlpha)
{
originalGUIColor = GUI.color;
GUI.color = UnityGUITools.ColorWithAlpha(GUI.color, Alpha);
}
}
public void RestoreGUIColor()
{
if (HasAlpha)
{
GUI.color = originalGUIColor;
}
}
///
/// Sets the control's text and formatting.
///
///
/// Formatted text.
///
public virtual void SetFormattedText(FormattedText formattedText)
{
text = formattedText.text;
formattingToApply = formattedText;
isFormattingApplied = false;
GuiStyle = null;
NeedToUpdateLayout = true;
}
///
/// Sets the control's text and formatting using just raw text.
///
///
/// Text.
///
public void SetUnformattedText(string text)
{
this.text = text;
formattingToApply = null;
guiStyle = null;
NeedToUpdateLayout = true;
}
///
/// Updates the control's layout but not its children.
///
public override void UpdateLayoutSelf()
{
guiStyle = null;
isFormattingApplied = false;
ApplyFormatting();
base.UpdateLayoutSelf();
}
///
/// Makes sure the guiStyle property is up-to-date.
///
protected void SetGUIStyle()
{
if (guiStyle == null) guiStyle = UnityGUITools.GetGUIStyle(guiStyleName, DefaultGUIStyle);
}
///
/// Applies the formatting recorded in formattingToApply by SetFormattedText().
/// SetFormattedText() can't apply the formatting directly because it needs to
/// run in OnGUI.
///
protected void ApplyFormatting()
{
SetGUIStyle();
if (!(isFormattingApplied || (formattingToApply == null)))
{
text = formattingToApply.text;
guiStyle = UnityGUITools.ApplyFormatting(formattingToApply, guiStyle);
isFormattingApplied = true;
}
}
///
/// Auto-sizes the control according to the autoSize settings.
///
public override void AutoSizeSelf()
{
ApplyFormatting();
// Compute width first. Doesn't handle word-wrapping, which is okay since it doesn't really
// make sense with auto-width. Also have to temporarily zero out padding since padding values
// cause CalcSize() to return strange values:
if (autoSize.autoSizeWidth)
{
GUIStyle newGuiStyle = new GUIStyle(guiStyle);
newGuiStyle.padding = new RectOffset(0, 0, 0, 0);
float width = newGuiStyle.CalcSize(new GUIContent(text)).x + guiStyle.padding.left + guiStyle.padding.right;
width = Mathf.Clamp(width, scaledRect.minPixelWidth, autoSize.maxWidth.GetPixelValue(WindowSize.x));
width += autoSize.padding.left + autoSize.padding.right;
rect = new Rect(GetAutoSizeX(width), rect.y, width, rect.height);
}
if (autoSize.autoSizeHeight)
{
float height = guiStyle.CalcHeight(new GUIContent(text), rect.width);
height = Mathf.Clamp(height, scaledRect.minPixelHeight, autoSize.maxHeight.GetPixelValue(WindowSize.y));
height += autoSize.padding.top + autoSize.padding.bottom;
rect = new Rect(rect.x, GetAutoSizeY(height), rect.width, height);
}
}
private float GetAutoSizeX(float width)
{
switch (scaledRect.alignment)
{
case ScaledRectAlignment.TopLeft:
case ScaledRectAlignment.MiddleLeft:
case ScaledRectAlignment.BottomLeft:
return rect.x;
case ScaledRectAlignment.TopCenter:
case ScaledRectAlignment.MiddleCenter:
case ScaledRectAlignment.BottomCenter:
return rect.x + (0.5f * (rect.width - width));
case ScaledRectAlignment.TopRight:
case ScaledRectAlignment.MiddleRight:
case ScaledRectAlignment.BottomRight:
return rect.x + (rect.width - width);
default:
return rect.x;
}
}
private float GetAutoSizeY(float height)
{
switch (scaledRect.alignment)
{
case ScaledRectAlignment.TopLeft:
case ScaledRectAlignment.TopCenter:
case ScaledRectAlignment.TopRight:
return rect.y;
case ScaledRectAlignment.MiddleLeft:
case ScaledRectAlignment.MiddleCenter:
case ScaledRectAlignment.MiddleRight:
return rect.y + (0.5f * (rect.height - height));
case ScaledRectAlignment.BottomLeft:
case ScaledRectAlignment.BottomCenter:
case ScaledRectAlignment.BottomRight:
return rect.y + (rect.height - height);
default:
return rect.y;
}
}
///
/// Plays an audio clip. If the control itself has an AudioSource, it uses it.
/// Otherwise it uses the AudioSource on the main camera, adding one if the
/// main camera doesn't already have one.
///
/// Audio clip.
public void PlaySound(AudioClip audioClip)
{
if (audioClip != null && Camera.main != null)
{
AudioSource audioSource = Camera.main.GetComponent();
if (audioSource == null)
{
audioSource = Camera.main.gameObject.AddComponent();
}
audioSource.PlayOneShot(audioClip);
}
}
}
}