using UnityEngine;
using System;
namespace PixelCrushers.DialogueSystem.UnityGUI
{
///
/// A GUI control that implements GUI.Button with optional additional textures.
///
[AddComponentMenu("")] // Deprecated
public class GUIButton : GUIVisibleControl
{
///
/// Is the button clickable (active)?
///
public bool clickable = true;
///
/// The disabled image.
///
public GUIImageParams disabled;
///
/// The normal image.
///
public GUIImageParams normal;
///
/// The hover image.
///
public GUIImageParams hover;
///
/// The pressed image.
///
public GUIImageParams pressed;
public AudioClip hoverSound = null;
public AudioClip clickSound = null;
///
/// The input that triggers the button (an alternative to using the mouse to click it).
///
public InputTrigger trigger;
///
/// The message to send to the target when the button is clicked.
///
public string message = "OnClick";
///
/// The parameter for the message sent to the target. If the data field (below) is assigned,
/// the button will send the data. Otherwise, if the parameter string is set, it will send
/// the string. Otherwise, it will send a reference to this button itself.
///
public string parameter;
///
/// The target to send the message to.
///
public Transform target;
///
/// The data to send to the target as a parameter to the message. If this field is
/// assigned, the button will send it. Otherwise, if the parameter string is set, it will
/// send the string. Otherwise, it will send a reference to this button itself.
///
public object data;
///
/// 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 override GUIStyle DefaultGUIStyle
{
get { return GUI.skin.button; }
}
private bool isHovered = false;
///
/// Draws the control, but not its children.
///
/// Relative mouse position within the window containing this control.
public override void DrawSelf(Vector2 relativeMousePosition)
{
if (clickable)
{
DrawClickable(relativeMousePosition);
}
else
{
DrawUnclickable();
}
}
private void DrawClickable(Vector2 relativeMousePosition)
{
if (rect.Contains(relativeMousePosition))
{
if (Input.GetMouseButton(0))
{
if (pressed != null) pressed.Draw(rect);
}
else
{
if (isHovered == false)
{
isHovered = true;
PlaySound(hoverSound);
}
if (hover != null) hover.Draw(rect);
}
}
else
{
if (isHovered == true) isHovered = false;
if (normal != null) normal.Draw(rect);
}
if (GUI.Button(rect, text, GuiStyle)) Click();
}
private void DrawUnclickable()
{
if (disabled.texture != null)
{
if (disabled != null) disabled.Draw(rect);
}
else if (!string.IsNullOrEmpty(text))
{
GUI.enabled = false;
GUI.Button(rect, text, GuiStyle);
GUI.enabled = true;
}
}
///
/// Checks if the button has been "clicked" by the trigger key or input button.
///
public override void Update()
{
base.Update();
if (clickable && trigger.isDown)
{
Click();
}
}
///
/// Clicks the button. You can call this manually to simulate a mouse click.
///
public void Click()
{
PlaySound(clickSound);
Transform actualTarget = Tools.Select(target, this.transform);
object actualData = null;
if (data != null)
{
actualData = data;
}
else if (!string.IsNullOrEmpty(parameter))
{
actualData = parameter;
}
else
{
actualData = this;
}
actualTarget.SendMessage(message, actualData, SendMessageOptions.DontRequireReceiver);
}
}
}