Advertisement
Guest User

EasyButton

a guest
Jul 21st, 2013
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.05 KB | None | 0 0
  1. // EasyButton library is copyright (c) of Hedgehog Team
  2. // Please send feedback or bug reports to the.hedgehog.team@gmail.com
  3. using UnityEngine;
  4. using System.Collections;
  5.  
  6. /// <summary>
  7. /// Release notes:
  8. /// EasyButton V1.0 April 2013
  9. /// =============================
  10. ///     * Bugs fixed
  11. ///     ------------
  12. ///     - Fixe Normaltexture property, if you changes the texture during runtime, it taken into account after une first click on a button
  13. ///
  14. /// EasyButton V1.0 April 2013
  15. /// =============================
  16. ///     - First release
  17. ///
  18.  
  19. /// <summary>
  20. /// This is the main class of EasyButton engine.
  21. ///
  22. /// For add EasyButton to your scene, use the menu Hedgehog Team<br>
  23. /// </summary>
  24. [ExecuteInEditMode]
  25. public class EasyButton : MonoBehaviour {
  26.    
  27.     #region Delegate
  28.     public delegate void ButtonUpHandler(string buttonName);
  29.     public delegate void ButtonPressHandler(string buttonName);
  30.     public delegate void ButtonDownHandler(string buttonName);
  31.     #endregion
  32.    
  33.     #region Event
  34.     /// <summary>
  35.     /// Occurs when the button is down for the first time.
  36.     /// </summary>
  37.     public static event ButtonDownHandler On_ButtonDown;
  38.     /// <summary>
  39.     /// Occurs when the button is pressed.
  40.     /// </summary>
  41.     public static event ButtonPressHandler On_ButtonPress;
  42.     /// <summary>
  43.     /// Occurs when the button is up
  44.     /// </summary>
  45.     public static event ButtonUpHandler On_ButtonUp;
  46.     #endregion
  47.    
  48.     #region Enumerations
  49.     /// <summary>
  50.     /// Button anchor.
  51.     /// </summary>
  52.     public enum ButtonAnchor {UpperLeft, UpperCenter, UpperRight, MiddleLeft, MiddleCenter, MiddleRight, LowerLeft, LowerCenter, LowerRight};
  53.     /// <summary>
  54.     /// Broadcast mode for javascript
  55.     /// </summary>
  56.     public enum Broadcast {SendMessage,SendMessageUpwards,BroadcastMessage }
  57.     /// <summary>
  58.     /// Button state, for include mode
  59.     /// </summary>
  60.     public enum ButtonState { Down, Press, Up, None};
  61.     /// <summary>
  62.     /// Interaction type.
  63.     /// </summary>
  64.     public enum InteractionType {Event, Include}
  65.    
  66.     private enum MessageName{On_ButtonDown, On_ButtonPress,On_ButtonUp};
  67.     #endregion
  68.    
  69.     #region Members
  70.    
  71.     #region public members
  72.    
  73.     #region Button properties
  74.     /// <summary>
  75.     /// Enable or disable the button.
  76.     /// </summary>
  77.     public bool enable = true;
  78.    
  79.     /// <summary>
  80.     /// Activacte or deactivate the button
  81.     /// </summary>
  82.     public bool isActivated = true;
  83.    
  84.     public bool showDebugArea=true;
  85.    
  86.     /// <summary>
  87.     /// Disable this lets you skip the GUI layout phase.
  88.     /// </summary>
  89.     public bool isUseGuiLayout=true;
  90.    
  91.     /// <summary>
  92.     /// The state of the button
  93.     /// </summary>
  94.     public ButtonState buttonState = ButtonState.None;
  95.     #endregion
  96.    
  97.     #region Button position & size
  98.     [SerializeField]
  99.     private ButtonAnchor anchor = ButtonAnchor.LowerRight;
  100.     /// <summary>
  101.     /// Gets or sets the anchor.
  102.     /// </summary>
  103.     /// <value>
  104.     /// The anchor.
  105.     /// </value>
  106.     public ButtonAnchor Anchor {
  107.         get {
  108.             return this.anchor;
  109.         }
  110.         set {
  111.             anchor = value;
  112.             ComputeButtonAnchor( anchor);
  113.         }
  114.     }  
  115.  
  116.     [SerializeField]
  117.     private Vector2 offset = Vector2.zero;
  118.     /// <summary>
  119.     /// Gets or sets the offset.
  120.     /// </summary>
  121.     /// <value>
  122.     /// The offset.
  123.     /// </value>
  124.     public Vector2 Offset {
  125.         get {
  126.             return this.offset;
  127.         }
  128.         set {
  129.             offset = value;
  130.             ComputeButtonAnchor( anchor);
  131.         }
  132.     }  
  133.    
  134.     [SerializeField]
  135.     private Vector2 scale = Vector2.one;
  136.     /// <summary>
  137.     /// Gets or sets the scale.
  138.     /// </summary>
  139.     /// <value>
  140.     /// The scale.
  141.     /// </value>
  142.     public Vector2 Scale {
  143.         get {
  144.             return this.scale;
  145.         }
  146.         set {
  147.             scale = value;
  148.             ComputeButtonAnchor( anchor);
  149.         }
  150.     }  
  151.        
  152.     /// <summary>
  153.     /// Enable or disable swipe in option
  154.     /// </summary>
  155.     public bool isSwipeIn = false;
  156.     /// <summary>
  157.     /// enable or disable siwpe out
  158.     /// </summary>
  159.     public bool isSwipeOut = false;
  160.     #endregion
  161.    
  162.     #region Interaction & Events
  163.     /// <summary>
  164.     /// The interaction.
  165.     /// </summary>/
  166.     public InteractionType interaction = InteractionType.Event;
  167.     /// <summary>
  168.     /// Enable or disable Broadcast message mode.
  169.     /// </summary>
  170.     public bool useBroadcast = false;
  171.     // Messaging
  172.     /// <summary>
  173.     /// The receiver gameobject when you're in broacast mode for events
  174.     /// </summary>
  175.     public GameObject receiverGameObject;
  176.     /// <summary>
  177.     /// The message sending mode for broacast
  178.     /// </summary>
  179.     public Broadcast messageMode;
  180.     /// <summary>
  181.     /// The use specifical method.
  182.     /// </summary>
  183.     public bool useSpecificalMethod=false;
  184.     /// <summary>
  185.     /// The name of the method.
  186.     /// </summary>
  187.    
  188.     /// <summary>
  189.     /// The name of the down method.
  190.     /// </summary>
  191.     public string downMethodName;
  192.     /// <summary>
  193.     /// The name of the press method.
  194.     /// </summary>
  195.     public string pressMethodName;
  196.     /// <summary>
  197.     /// The name of the up method.
  198.     /// </summary>
  199.     public string upMethodName;
  200.     #endregion
  201.        
  202.     #region Button texture & color
  203.     /// <summary>
  204.     /// The GUI depth.
  205.     /// </summary>
  206.     public int guiDepth = 0;
  207.    
  208.     // Normal
  209.     [SerializeField]
  210.     private Texture2D normalTexture;
  211.     /// <summary>
  212.     /// Gets or sets the normal texture.
  213.     /// </summary>
  214.     /// <value>
  215.     /// The normal texture.
  216.     /// </value>
  217.     public Texture2D NormalTexture {
  218.         get {
  219.             return this.normalTexture;
  220.         }
  221.         set {
  222.             normalTexture = value;
  223.             if (normalTexture!=null){
  224.                 ComputeButtonAnchor( anchor);
  225.                 currentTexture = normalTexture;
  226.             }
  227.         }
  228.     }  
  229.     /// <summary>
  230.     /// The color of the button normal.
  231.     /// </summary>
  232.     public Color buttonNormalColor = Color.white;
  233.    
  234.     // Active
  235.     [SerializeField]
  236.     private Texture2D activeTexture;
  237.     /// <summary>
  238.     /// Gets or sets the active texture.
  239.     /// </summary>
  240.     /// <value>
  241.     /// The active texture.
  242.     /// </value>
  243.     public Texture2D ActiveTexture {
  244.         get {
  245.             return this.activeTexture;
  246.         }
  247.         set {
  248.             activeTexture = value;
  249.         }
  250.     }  
  251.     /// <summary>
  252.     /// The color of the button active.
  253.     /// </summary>
  254.     public Color buttonActiveColor = Color.white;
  255.     #endregion
  256.  
  257.     #region Inspector
  258.     public bool showInspectorProperties=true;
  259.     public bool showInspectorPosition=true;
  260.     public bool showInspectorEvent=false;
  261.     public bool showInspectorTexture=false;
  262.     #endregion
  263.    
  264.     #endregion
  265.    
  266.     #region Private member
  267.     private Rect buttonRect;
  268.     private int buttonFingerIndex=-1;
  269.     private Texture2D currentTexture;
  270.     private Color currentColor;
  271.     private int frame=0;
  272.     #endregion
  273.    
  274.     #endregion
  275.  
  276.     #region MonoBehaviour methods
  277.  
  278.    
  279.     void OnEnable(){
  280.         EasyTouch.On_TouchStart += On_TouchStart;
  281.         EasyTouch.On_TouchDown += On_TouchDown;
  282.         EasyTouch.On_TouchUp += On_TouchUp;
  283.     }
  284.  
  285.     void OnDisable(){
  286.         EasyTouch.On_TouchStart -= On_TouchStart;
  287.         EasyTouch.On_TouchDown -= On_TouchDown;
  288.         EasyTouch.On_TouchUp -= On_TouchUp;
  289.        
  290.         if (Application.isPlaying){
  291.             EasyTouch.RemoveReservedArea( buttonRect);
  292.         }      
  293.     }
  294.  
  295.     void OnDestroy(){
  296.         EasyTouch.On_TouchStart -= On_TouchStart;
  297.         EasyTouch.On_TouchDown -= On_TouchDown;
  298.         EasyTouch.On_TouchUp -= On_TouchUp;
  299.        
  300.         if (Application.isPlaying){
  301.             EasyTouch.RemoveReservedArea( buttonRect);
  302.         }
  303.     }
  304.  
  305.     void Start(){
  306.         currentTexture = normalTexture;
  307.         currentColor = buttonNormalColor;
  308.         buttonState = ButtonState.None;
  309.         VirtualScreen.ComputeVirtualScreen();
  310.         ComputeButtonAnchor(anchor);       
  311.     }
  312.    
  313.     void OnGUI(){
  314.        
  315.         if (enable){
  316.             GUI.depth = guiDepth;
  317.            
  318.             useGUILayout = isUseGuiLayout;
  319.            
  320.             VirtualScreen.ComputeVirtualScreen();
  321.             VirtualScreen.SetGuiScaleMatrix();
  322.            
  323.             if (normalTexture!=null && activeTexture!=null){
  324.                 ComputeButtonAnchor(anchor);
  325.                
  326.                 if (normalTexture!=null){
  327.                     if (Application.isEditor && !Application.isPlaying){
  328.                         currentTexture = normalTexture;
  329.                     }
  330.                    
  331.                     if (showDebugArea && Application.isEditor){
  332.                         GUI.Box( buttonRect,"");               
  333.                     }
  334.                
  335.                        
  336.                     if (currentTexture!=null){
  337.                         if (isActivated){
  338.                             GUI.color = currentColor;  
  339.                             if (Application.isPlaying){
  340.                                 EasyTouch.RemoveReservedArea( buttonRect);
  341.                                 EasyTouch.AddReservedArea( buttonRect);
  342.                             }                          
  343.                         }
  344.                         else{
  345.                             GUI.color = new Color(currentColor.r,currentColor.g,currentColor.b,0.2f);
  346.                             if (Application.isPlaying){
  347.                                 EasyTouch.RemoveReservedArea( buttonRect);
  348.                             }
  349.                         }
  350.                         GUI.DrawTexture( buttonRect,currentTexture);
  351.                         GUI.color = Color.white;
  352.                     }
  353.                 }
  354.             }
  355.         }
  356.         else{
  357.             EasyTouch.RemoveReservedArea( buttonRect); 
  358.         }
  359.     }
  360.    
  361.     PlatformerPhysics mPlayer;
  362.     bool mHasControl;
  363.    
  364.     void Update(){
  365.    
  366.         if (buttonState == ButtonState.Up){
  367.             //here are actions where the buttons can be held for a longer period
  368.             if (mPlayer && mHasControl)
  369.             {
  370.                 if (Input.GetButton("Jump"))
  371.                     mPlayer.Jump();
  372.  
  373.                 mPlayer.Walk(Input.GetAxisRaw("Horizontal"));
  374.             }
  375.         }
  376.     }
  377.    
  378.     void OnDrawGizmos(){
  379.     }
  380.     #endregion
  381.    
  382.     #region Private methods
  383.     void ComputeButtonAnchor(ButtonAnchor anchor){
  384.    
  385.         if (normalTexture!=null){
  386.             Vector2 buttonSize = new Vector2(normalTexture.width*scale.x, normalTexture.height*scale.y);
  387.             Vector2 anchorPosition = Vector2.zero;
  388.            
  389.             // Anchor position
  390.             switch (anchor){
  391.                 case ButtonAnchor.UpperLeft:
  392.                     anchorPosition = new Vector2( 0, 0);
  393.                     break;
  394.                 case ButtonAnchor.UpperCenter:
  395.                     anchorPosition = new Vector2( VirtualScreen.width/2- buttonSize.x/2, offset.y);
  396.                     break;
  397.                 case ButtonAnchor.UpperRight:
  398.                     anchorPosition = new Vector2( VirtualScreen.width-buttonSize.x ,0);
  399.                     break;
  400.                
  401.                 case ButtonAnchor.MiddleLeft:
  402.                     anchorPosition = new Vector2( 0, VirtualScreen.height/2- buttonSize.y/2);
  403.                     break;
  404.                 case ButtonAnchor.MiddleCenter:
  405.                     anchorPosition = new Vector2( VirtualScreen.width/2- buttonSize.x/2, VirtualScreen.height/2- buttonSize.y/2);
  406.                     break;
  407.                
  408.                 case ButtonAnchor.MiddleRight:
  409.                     anchorPosition = new Vector2( VirtualScreen.width-buttonSize.x,VirtualScreen.height/2- buttonSize.y/2);
  410.                     break;
  411.                
  412.                 case ButtonAnchor.LowerLeft:
  413.                     anchorPosition = new Vector2( 0, VirtualScreen.height- buttonSize.y);
  414.                     break;
  415.                 case ButtonAnchor.LowerCenter:
  416.                     anchorPosition = new Vector2( VirtualScreen.width/2- buttonSize.x/2, VirtualScreen.height- buttonSize.y);
  417.                     break;
  418.                 case ButtonAnchor.LowerRight:
  419.                     anchorPosition = new Vector2( VirtualScreen.width-buttonSize.x,VirtualScreen.height- buttonSize.y);
  420.                     break; 
  421.                                
  422.             }
  423.                    
  424.             //button rect
  425.             buttonRect = new Rect(anchorPosition.x + offset.x, anchorPosition.y + offset.y ,buttonSize.x,buttonSize.y);
  426.         }
  427.        
  428.     }
  429.        
  430.     void RaiseEvent(MessageName msg){
  431.        
  432.         if (interaction == InteractionType.Event){
  433.             if (!useBroadcast){
  434.                 switch (msg){  
  435.                     case MessageName.On_ButtonDown:
  436.                         if (On_ButtonDown!=null){
  437.                             On_ButtonDown( gameObject.name);
  438.                         }  
  439.                         break;
  440.                     case MessageName.On_ButtonUp:
  441.                         if (On_ButtonUp!=null){
  442.                             On_ButtonUp( gameObject.name); 
  443.                         }
  444.                         break;
  445.                     case MessageName.On_ButtonPress:
  446.                        
  447.                         if (On_ButtonPress!=null){
  448.                             On_ButtonPress( gameObject.name);  
  449.                         }
  450.                         break;
  451.                 }
  452.             }
  453.             else{
  454.                 string method = msg.ToString();
  455.                
  456.                 if (msg == MessageName.On_ButtonDown && downMethodName!="" && useSpecificalMethod){
  457.                     method = downMethodName;       
  458.                 }
  459.                
  460.                 if (msg == MessageName.On_ButtonPress && pressMethodName!="" && useSpecificalMethod){
  461.                     method = pressMethodName;      
  462.                 }              
  463.                
  464.                 if (msg == MessageName.On_ButtonUp && upMethodName!="" && useSpecificalMethod){
  465.                     method = upMethodName;     
  466.                 }
  467.                 if (receiverGameObject!=null){     
  468.                     switch(messageMode){
  469.                         case Broadcast.BroadcastMessage:
  470.                             receiverGameObject.BroadcastMessage( method,name,SendMessageOptions.DontRequireReceiver);
  471.                             break;
  472.                         case Broadcast.SendMessage:
  473.                             receiverGameObject.SendMessage( method,name,SendMessageOptions.DontRequireReceiver);
  474.                             break;
  475.                         case Broadcast.SendMessageUpwards:
  476.                             receiverGameObject.SendMessageUpwards( method,name,SendMessageOptions.DontRequireReceiver);
  477.                             break;
  478.                     }  
  479.                 }
  480.                 else{
  481.                     Debug.LogError("Button : " + gameObject.name + " : you must setup receiver gameobject");   
  482.                    
  483.                 }
  484.                
  485.             }
  486.         }
  487.     }
  488.     #endregion
  489.    
  490.     #region EasyTouch Event
  491.     void On_TouchStart (Gesture gesture){
  492.    
  493.         if (gesture.IsInRect( VirtualScreen.GetRealRect(buttonRect),true) && enable && isActivated){
  494.            
  495.             buttonFingerIndex = gesture.fingerIndex;
  496.             currentTexture = activeTexture;
  497.             currentColor = buttonActiveColor;
  498.             buttonState = ButtonState.Down;
  499.             frame=0;
  500.             RaiseEvent( MessageName.On_ButtonDown);
  501.         }
  502.     }
  503.    
  504.     void On_TouchDown (Gesture gesture){
  505.        
  506.         if (gesture.fingerIndex ==  buttonFingerIndex || (isSwipeIn && buttonState==ButtonState.None) ){
  507.            
  508.            
  509.             if (gesture.IsInRect( VirtualScreen.GetRealRect(buttonRect),true) && enable && isActivated){   
  510.                 currentTexture = activeTexture;
  511.                 currentColor = buttonActiveColor;
  512.                
  513.                 frame++;
  514.                
  515.                 if ((buttonState == ButtonState.Down || buttonState == ButtonState.Press) && frame>=2){
  516.                     RaiseEvent(MessageName.On_ButtonPress);
  517.                     buttonState = ButtonState.Press;
  518.                 }
  519.                
  520.                 if (buttonState == ButtonState.None){
  521.                     buttonFingerIndex = gesture.fingerIndex;
  522.                     buttonState = ButtonState.Down;
  523.                     frame=0;
  524.                     RaiseEvent( MessageName.On_ButtonDown);
  525.                    
  526.                 }
  527.             }
  528.            
  529.             else {
  530.                 if (((isSwipeIn || !isSwipeIn ) && !isSwipeOut) && buttonState == ButtonState.Press){
  531.                     buttonFingerIndex=-1;
  532.                     currentTexture = normalTexture;
  533.                     currentColor = buttonNormalColor;  
  534.                     buttonState = ButtonState.None;                
  535.                 }
  536.                 else if (isSwipeOut && buttonState == ButtonState.Press) {
  537.                     RaiseEvent(MessageName.On_ButtonPress);
  538.                     buttonState = ButtonState.Press;
  539.                 }
  540.             }
  541.         }
  542.        
  543.     }
  544.    
  545.     void On_TouchUp (Gesture gesture){
  546.        
  547.         if (gesture.fingerIndex ==  buttonFingerIndex){
  548.             if ((EasyTouch.IsRectUnderTouch( VirtualScreen.GetRealRect(buttonRect),true) || (isSwipeOut && buttonState == ButtonState.Press))  && enable && isActivated){
  549.                 RaiseEvent(MessageName.On_ButtonUp);
  550.             }
  551.             buttonState = ButtonState.Up;
  552.             buttonFingerIndex=-1;
  553.             currentTexture = normalTexture;
  554.             currentColor = buttonNormalColor;
  555.         }
  556.     }
  557.    
  558.     #endregion
  559.    
  560.  
  561. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement