You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.8 KiB
153 lines
3.8 KiB
11 years ago
|
using UnityEngine;
|
||
11 years ago
|
using System;
|
||
11 years ago
|
using System.Collections;
|
||
10 years ago
|
using Fungus.Script;
|
||
11 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* Button click handler class for making sprites clickable.
|
||
|
* When the user taps on the sprite, an Action delegate method is called
|
||
|
*/
|
||
11 years ago
|
[RequireComponent (typeof (SpriteRenderer))]
|
||
11 years ago
|
public class Button : MonoBehaviour
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* Delegate method to call when the player clicks the button.
|
||
|
*/
|
||
11 years ago
|
public Action buttonAction;
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Automatically hides the button when displaying story text/options or waiting.
|
||
11 years ago
|
*/
|
||
11 years ago
|
[Tooltip("Automatically hides the button when displaying story text/options or waiting.")]
|
||
11 years ago
|
public bool autoHide = true;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Automatically hides the button when the named boolean variable is set using SetBoolean()
|
||
11 years ago
|
*/
|
||
11 years ago
|
[Tooltip("Automatically hides the button when the named boolean variable is set using the SetBoolean() command.")]
|
||
11 years ago
|
public string hideOnBoolean;
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Sound effect to play when button is clicked.
|
||
|
*/
|
||
11 years ago
|
[Tooltip("Sound effect to play when button is clicked.")]
|
||
11 years ago
|
public AudioClip clickSound;
|
||
|
|
||
11 years ago
|
float targetAlpha;
|
||
|
bool showButton;
|
||
|
|
||
|
/**
|
||
|
* Set visibility of a button object and set the delegate method to call when clicked.
|
||
|
* If the object does not already have a Collider2D component, then a BoxCollider2D is added.
|
||
|
* @param _visible Setting this to true makes the button visible, unless overridden by property settings
|
||
|
* @param _buttonAction An Action delegate method to call when the player clicks on the button
|
||
11 years ago
|
*/
|
||
11 years ago
|
public void Show(bool _visible, Action _buttonAction)
|
||
11 years ago
|
{
|
||
11 years ago
|
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
|
||
|
if (spriteRenderer == null)
|
||
11 years ago
|
{
|
||
11 years ago
|
Debug.LogError("Sprite renderer must not be null");
|
||
11 years ago
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
// Add a BoxCollider2d if none currently exists
|
||
|
if (collider2D == null)
|
||
11 years ago
|
{
|
||
11 years ago
|
gameObject.AddComponent<BoxCollider2D>();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
showButton = _visible;
|
||
|
buttonAction = _buttonAction;
|
||
|
|
||
|
if (_visible)
|
||
|
{
|
||
|
targetAlpha = 1f;
|
||
|
}
|
||
|
else
|
||
11 years ago
|
{
|
||
11 years ago
|
targetAlpha = 0f;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
UpdateTargetAlpha();
|
||
|
}
|
||
|
|
||
|
public void SetAlpha(float alpha)
|
||
|
{
|
||
|
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
|
||
|
Color color = spriteRenderer.color;
|
||
|
color.a = alpha;
|
||
|
spriteRenderer.color = color;
|
||
|
}
|
||
|
|
||
|
void UpdateTargetAlpha()
|
||
|
{
|
||
|
// Automatically display button when game is in idle state (not displaying story text/options or waiting)
|
||
|
if (autoHide)
|
||
|
{
|
||
|
if (showButton &&
|
||
11 years ago
|
Game.GetInstance().GetShowAutoButtons())
|
||
11 years ago
|
{
|
||
|
targetAlpha = 1f;
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
targetAlpha = 0f;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
// Hide the button if the specified boolean variable is true
|
||
|
if (hideOnBoolean.Length > 0 &&
|
||
10 years ago
|
GlobalVariables.GetBoolean(hideOnBoolean))
|
||
11 years ago
|
{
|
||
11 years ago
|
targetAlpha = 0f;
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void Update()
|
||
|
{
|
||
|
UpdateTargetAlpha();
|
||
|
|
||
|
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
|
||
|
float fadeSpeed = (1f / Game.GetInstance().buttonFadeDuration);
|
||
11 years ago
|
|
||
|
float alpha = Mathf.MoveTowards(spriteRenderer.color.a, targetAlpha, Time.deltaTime * fadeSpeed);;
|
||
|
|
||
|
// Set alpha for this sprite and any child sprites
|
||
|
SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>();
|
||
|
foreach (SpriteRenderer child in children)
|
||
|
{
|
||
|
Color color = child.color;
|
||
|
color.a = alpha;
|
||
|
child.color = color;
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void OnMouseUpAsButton()
|
||
|
{
|
||
11 years ago
|
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
|
||
|
|
||
11 years ago
|
// Ignore button press if sprite is not fully visible or
|
||
|
// if the game is not in an idle state
|
||
|
if (spriteRenderer.color.a != 1f ||
|
||
11 years ago
|
!Game.GetInstance().GetShowAutoButtons())
|
||
11 years ago
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
// Click sound effect
|
||
|
if (clickSound != null)
|
||
|
{
|
||
|
AudioSource.PlayClipAtPoint(clickSound, Vector3.zero);
|
||
|
}
|
||
11 years ago
|
|
||
10 years ago
|
/*
|
||
11 years ago
|
CommandQueue commandQueue = Game.GetInstance().commandQueue;
|
||
|
commandQueue.CallCommandMethod(buttonAction);
|
||
10 years ago
|
*/
|
||
11 years ago
|
}
|
||
|
}
|
||
|
}
|