Advertisement
CassataGames

Untitled

May 27th, 2012
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.25 KB | None | 0 0
  1.     public class ButtonsClass
  2.     {
  3.         Toolbar parent;
  4.         public bool Active = true;
  5.         bool Checked;
  6.         bool DisplayingMenu = false;
  7.  
  8.         public string ButtonText = "Button Text";
  9.         Vector2 ButtonAdjust = new Vector2(1.25f, 1.25f);
  10.         public Vector2 ButtonSize = new Vector2(18,3);
  11.         public Vector2 ButtonPosition;
  12.         public Texture2D ButtonTexture;
  13.         public Texture2D HighlightTexture;
  14.         public Texture2D ClickedTexture;
  15.         public Texture2D DisabledTexture;
  16.         Texture2D CurrentTexture;
  17.         public enum ButtonType { Color, MenuObjects, SingleObject, Direction, Orientation }
  18.         public ButtonType Type;
  19.         public string SetName;
  20.         public string[] ObjectList;
  21.  
  22.         float ToolbarSize;
  23.         public Rect ButtonRect()
  24.         {
  25.             return new Rect((int)(ButtonPosition.x * (Screen.height * 0.01f)), // Left
  26.                             (int)(ButtonPosition.y * (Screen.height * 0.01f)), // Top
  27.                             (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
  28.                             (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
  29.         } // The buttons size
  30.         public Rect ButtonRect(Vector2 AdjustPosition)
  31.         {
  32.             return new Rect(((int)(ButtonPosition.x * (Screen.height * 0.01f))) + AdjustPosition.x, // Left
  33.                             ((int)(ButtonPosition.y * (Screen.height * 0.01f))) + AdjustPosition.y, // Top
  34.                             (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
  35.                             (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
  36.         } // The buttons size with adjustments to position
  37.         public Rect ButtonRect(int xMulti)
  38.         {
  39.             return new Rect((int)((ButtonPosition.x*xMulti) * (Screen.height * 0.01f)), // Left
  40.                             (int)(ButtonPosition.y * (Screen.height * 0.01f)), // Top
  41.                             (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
  42.                             (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
  43.         } // Multiply against itself - good for color swatches
  44.         public Rect MenuRect(int ListAmount)
  45.         {
  46.             return new Rect((int)(ButtonPosition.x * (Screen.height * 0.01f)), // Left
  47.                             (int)(ToolbarSize), // Top - This is set to the ToolbarSize so that it draws under the toolbar.
  48.                             (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
  49.                             (int)(((3 * ListAmount) + 2) * (Screen.height * 0.01f))); // Height - Set to the height of a button, plus the length of the list.
  50.         }
  51.         public Rect ObjectRect(int ListIndex, int AddedSize)
  52.         {
  53.             return new Rect((int)((ButtonPosition.x + AddedSize ) * (Screen.height * 0.01f)), // Left
  54.                             (int)(ToolbarSize + 2) + ((3 * ListIndex) * (Screen.height * 0.01f)), // Top - This is set to the ToolbarSize so that it draws under the toolbar.
  55.                             (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
  56.                             (int)((3) * (Screen.height * 0.01f))); // Height
  57.         }
  58.         public Rect ObjectRect(int ListIndex, Vector2 AdjustPosition, int AddedSize)
  59.         {
  60.             return new Rect((int)((ButtonPosition.x + AddedSize) * (Screen.height * 0.01f)) + AdjustPosition.x, // Left
  61.                             (int)((ToolbarSize + 2) + ((3 * ListIndex) * (Screen.height * 0.01f))) + AdjustPosition.y, // Top - This is set to the ToolbarSize so that it draws under the toolbar.
  62.                             (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
  63.                             (int)((3) * (Screen.height * 0.01f))); // Height
  64.         }
  65.  
  66.         public void DrawButton(GUIStyle InSkin)
  67.         {
  68.             CheckMouse(1);
  69.             ToolbarSize = parent.ToolbarSize * (Screen.height * 0.01f); // Gets the size of the toolbar so we know where to place menus
  70.  
  71.             GUI.DrawTexture(ButtonRect(), CurrentTexture, ScaleMode.StretchToFill, true);
  72.  
  73.             InSkin.alignment = TextAnchor.MiddleCenter;
  74.             InSkin.fontSize = (int)(2 * (Screen.height * 0.01f));
  75.             InSkin.normal.textColor = Color.black;
  76.             GUI.Label(ButtonRect(ButtonAdjust), ButtonText, InSkin);
  77.             InSkin.normal.textColor = Color.white;
  78.             GUI.Label(ButtonRect(), ButtonText, InSkin);
  79.             if (DisplayingMenu)
  80.                 DrawMenu(InSkin);
  81.         }
  82.         public void DrawSwatch(int xMultiply, bool active, bool isChecked)
  83.         {
  84.             Active = active;
  85.             Checked = isChecked;
  86.             CheckMouse(xMultiply);
  87.             GUI.DrawTexture(ButtonRect(xMultiply), CurrentTexture, ScaleMode.StretchToFill, true);
  88.             if (Checked)
  89.                 GUI.DrawTexture(ButtonRect(xMultiply), parent.CheckmarkTexture, ScaleMode.StretchToFill, true);
  90.         }
  91.         public void DrawMenu(GUIStyle InSkin)
  92.         {
  93.             GUI.DrawTexture(MenuRect(ObjectList.GetLength(0)), parent.MenuBackground, ScaleMode.StretchToFill, true);
  94.             int ListIndex = 0;
  95.             foreach (string Object in ObjectList)
  96.             {
  97.                 DrawSelector(ListIndex, ObjectRect(ListIndex, 0),Object);
  98.  
  99.                 InSkin.alignment = TextAnchor.MiddleLeft;
  100.                 InSkin.fontSize = (int)(2 * (Screen.height * 0.01f));
  101.                 InSkin.normal.textColor = Color.black;
  102.                 GUI.Label(ObjectRect(ListIndex, ButtonAdjust, 2), Object, InSkin);
  103.                 InSkin.normal.textColor = Color.white;
  104.                 GUI.Label(ObjectRect(ListIndex, 2), Object, InSkin);
  105.  
  106.                 ++ListIndex;
  107.             }
  108.             Vector2 MousePos = new Vector2(Input.mousePosition.x - 2, ((Input.mousePosition.y - Screen.height) * -1) - 2); // Gives us an easier to use variable
  109.             if (MousePos.x > MenuRect(ObjectList.GetLength(0)).x
  110.                 & MousePos.y > MenuRect(ObjectList.GetLength(0)).y
  111.                 & MousePos.x < MenuRect(ObjectList.GetLength(0)).xMax
  112.                 & MousePos.y < MenuRect(ObjectList.GetLength(0)).yMax)
  113.             {
  114.                 parent.overToolbar = true;
  115.             }
  116.             else
  117.                 if (parent.leftHold)
  118.                 {
  119.                     DisplayingMenu = false;
  120.                 }
  121.         }
  122.         public void DrawSelector(int ListIndex, Rect InRect, string InObject)
  123.         {
  124.             Vector2 MousePos = new Vector2(Input.mousePosition.x - 2, ((Input.mousePosition.y - Screen.height) * -1) - 2); // Gives us an easier to use variable
  125.             if (MousePos.x > InRect.x
  126.                 & MousePos.y > InRect.y
  127.                 & MousePos.x < InRect.xMax
  128.                 & MousePos.y < InRect.yMax)
  129.             {
  130.                 GUI.DrawTexture(InRect, parent.MenuSelect, ScaleMode.StretchToFill, true);
  131.                 SetName = InObject;
  132.  
  133.                 if (!parent.leftHold)
  134.                     parent.overButton = true;
  135.                 CurrentTexture = HighlightTexture;
  136.                 if (parent.leftInitial & !parent.leftHold)
  137.                 {
  138.                     ButtonClicked();
  139.                     parent.leftInitial = false;
  140.                 }
  141.             }
  142.  
  143.         }
  144.  
  145.         void CheckMouse(int xMulti)
  146.         {
  147.             if (parent == null)
  148.             {
  149.                 parent = GameObject.Find("EditorScripts").GetComponent<Toolbar>();
  150.             }
  151.            
  152.             Vector2 MousePos = new Vector2(Input.mousePosition.x-2,((Input.mousePosition.y - Screen.height) * -1)-2); // Gives us an easier to use variable
  153.             if (Active)
  154.             {
  155.                 if (MousePos.x > ButtonRect(xMulti).x & MousePos.y > ButtonRect(xMulti).y & MousePos.x < ButtonRect(xMulti).xMax & MousePos.y < ButtonRect(xMulti).yMax)
  156.                 {
  157.                     if (!parent.leftHold)
  158.                         parent.overButton = true;
  159.                     CurrentTexture = HighlightTexture;
  160.                     if (parent.leftInitial & !parent.leftHold)
  161.                     {
  162.                         ButtonClicked();
  163.                         parent.leftInitial = false;
  164.                     }
  165.                     if (parent.leftHold & parent.overButton)
  166.                     {
  167.                         CurrentTexture = ClickedTexture;
  168.                     }
  169.                 }
  170.                 else
  171.                 {
  172.                     CurrentTexture = ButtonTexture;
  173.                 }
  174.             }
  175.             else
  176.                 CurrentTexture = DisabledTexture;
  177.         }
  178.         void ButtonClicked()
  179.         {
  180.             switch (Type)
  181.             {
  182.                 case ButtonType.Color:
  183.                     parent.ChangeColor(SetName);
  184.                     break;
  185.                 case ButtonType.MenuObjects:
  186.                     if (!DisplayingMenu)
  187.                         DisplayingMenu = true;
  188.                     else
  189.                         parent.ChangeObject(SetName);
  190.                     break;
  191.                 case ButtonType.SingleObject:
  192.                     parent.ChangeObject(SetName);
  193.                     break;
  194.                 default:
  195.                     Debug.Log("No type selected.");
  196.                     break;
  197.             }
  198.         }
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement