GibTreaty

Button Controller

Jul 6th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4.  
  5. public class ButtonController : MonoBehaviour {
  6.  
  7.     public event Action<ButtonController> OnPressed;
  8.     public event Action<ButtonController> OnReleased;
  9.     public event Action<ButtonController> MouseEnter;
  10.     public event Action<ButtonController> MouseExit;
  11.  
  12.     public static int buttonsHovering { get; protected set; }
  13.  
  14.     public GameObject[] highlight;
  15.     public Color normalColor = Color.white;
  16.     public Color pressedColor = Color.blue;
  17.     public Color hightlightColor = Color.gray;
  18.     public Color disabledColor = Color.black;
  19.  
  20.     public FunctionInfo[] functions;
  21.  
  22.     protected bool buttonEnabled = true;
  23.     protected bool mouseOver { get; private set; }
  24.  
  25.     void OnMouseEnter() {
  26.         buttonsHovering++;
  27.         mouseOver = true;
  28.  
  29.         Highlight();
  30.         if(MouseEnter != null) MouseEnter(this);
  31.     }
  32.  
  33.     void OnMouseExit() {
  34.         buttonsHovering--;
  35.         mouseOver = false;
  36.  
  37.         Unhighlight();
  38.         if(MouseExit != null) MouseExit(this);
  39.     }
  40.  
  41.     void OnMouseDown() {
  42.         PressHighlight();
  43.  
  44.         if(buttonEnabled && OnPressed != null)
  45.             OnPressed(this);
  46.     }
  47.  
  48.     void OnMouseUp() {
  49.         Highlight();
  50.  
  51.         if(buttonEnabled && OnReleased != null)
  52.             OnReleased(this);
  53.     }
  54.  
  55.     void OnMouseUpAsButton() {
  56.         ButtonEvent();
  57.     }
  58.  
  59.     protected virtual void OnEnable() {
  60.         Unhighlight();
  61.     }
  62.  
  63.     protected virtual void OnDisable() {
  64.         if(mouseOver) {
  65.             mouseOver = false;
  66.             buttonsHovering--;
  67.  
  68.             Unhighlight();
  69.             if(MouseExit != null) MouseExit(this);
  70.         }
  71.     }
  72.  
  73.     [ContextMenu("Highlight")]
  74.     public virtual void Highlight() {
  75.         if(buttonEnabled) SetColors(hightlightColor);
  76.     }
  77.  
  78.     [ContextMenu("Unhighlight")]
  79.     public virtual void Unhighlight() {
  80.         if(buttonEnabled) SetColors(normalColor);
  81.     }
  82.  
  83.     public virtual void PressHighlight() {
  84.         if(buttonEnabled) SetColors(pressedColor);
  85.     }
  86.  
  87.     public virtual void DisabledHighlight() {
  88.         if(!buttonEnabled) SetColors(disabledColor);
  89.     }
  90.  
  91.     void SetColors(Color color) {
  92.         foreach(GameObject a in highlight)
  93.             SetColor(a, color);
  94.     }
  95.  
  96.     static void SetColor(GameObject gameObject, Color color) {
  97.         if(gameObject & gameObject.renderer) {
  98.             SpriteRenderer spriteRenderer = gameObject.renderer as SpriteRenderer;
  99.  
  100.             if(spriteRenderer)
  101.                 spriteRenderer.color = color;
  102.             else
  103.                 gameObject.renderer.material.color = color;
  104.         }
  105.     }
  106.  
  107.     public virtual void ButtonEvent() {
  108.         foreach(FunctionInfo a in functions)
  109.             a.SendMessage(this);
  110.     }
  111.  
  112.     public void SetEnabled(bool enabled) {
  113.         buttonEnabled = enabled;
  114.  
  115.         if(!buttonEnabled)
  116.             DisabledHighlight();
  117.         else if(mouseOver)
  118.             Highlight();
  119.         else
  120.             Unhighlight();
  121.     }
  122.  
  123.     [ContextMenu("Enable")]
  124.     public void EnableButton() {
  125.         SetEnabled(true);
  126.     }
  127.  
  128.     [ContextMenu("Disable")]
  129.     public void DisableButton() {
  130.         SetEnabled(false);
  131.     }
  132.  
  133.     [System.Serializable]
  134.     public class FunctionInfo {
  135.         public string function = "";
  136.         public GameObject functionObject;
  137.  
  138.         public string findObjectOfType = "";
  139.         public string findObjectWithTag = "";
  140.  
  141.         public FunctionInfo(GameObject functionObject, string function) {
  142.             this.functionObject = functionObject;
  143.             this.function = function;
  144.         }
  145.  
  146.         public void SendMessage(ButtonController button) {
  147.             if(function != "") {
  148.                 GameObject sendTo = functionObject;
  149.  
  150.                 if(!sendTo && findObjectOfType != "") {
  151.                     Type type = Type.GetType(findObjectOfType);
  152.                     UnityEngine.Object obj = FindObjectOfType(type) as UnityEngine.Object;
  153.  
  154.                     if(obj as MonoBehaviour)
  155.                         sendTo = (obj as MonoBehaviour).gameObject;
  156.                     else if(obj as Behaviour)
  157.                         sendTo = (obj as Behaviour).gameObject;
  158.                 }
  159.  
  160.                 if(!sendTo && findObjectWithTag != "")
  161.                     sendTo = GameObject.FindGameObjectWithTag(findObjectWithTag);
  162.  
  163.                 if(!sendTo) return;
  164.  
  165.                 sendTo.SendMessage(function, button);
  166.             }
  167.         }
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment