Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ButtonsClass
- {
- Toolbar parent;
- public bool Active = true;
- bool Checked;
- bool DisplayingMenu = false;
- public string ButtonText = "Button Text";
- Vector2 ButtonAdjust = new Vector2(1.25f, 1.25f);
- public Vector2 ButtonSize = new Vector2(18,3);
- public Vector2 ButtonPosition;
- public Texture2D ButtonTexture;
- public Texture2D HighlightTexture;
- public Texture2D ClickedTexture;
- public Texture2D DisabledTexture;
- Texture2D CurrentTexture;
- public enum ButtonType { Color, MenuObjects, SingleObject, Direction, Orientation }
- public ButtonType Type;
- public string SetName;
- public string[] ObjectList;
- float ToolbarSize;
- public Rect ButtonRect()
- {
- return new Rect((int)(ButtonPosition.x * (Screen.height * 0.01f)), // Left
- (int)(ButtonPosition.y * (Screen.height * 0.01f)), // Top
- (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
- (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
- } // The buttons size
- public Rect ButtonRect(Vector2 AdjustPosition)
- {
- return new Rect(((int)(ButtonPosition.x * (Screen.height * 0.01f))) + AdjustPosition.x, // Left
- ((int)(ButtonPosition.y * (Screen.height * 0.01f))) + AdjustPosition.y, // Top
- (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
- (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
- } // The buttons size with adjustments to position
- public Rect ButtonRect(int xMulti)
- {
- return new Rect((int)((ButtonPosition.x*xMulti) * (Screen.height * 0.01f)), // Left
- (int)(ButtonPosition.y * (Screen.height * 0.01f)), // Top
- (int)(ButtonSize.x * (Screen.height * 0.01f)), // Width
- (int)(ButtonSize.y * (Screen.height * 0.01f))); // Height
- } // Multiply against itself - good for color swatches
- public Rect MenuRect(int ListAmount)
- {
- return new Rect((int)(ButtonPosition.x * (Screen.height * 0.01f)), // Left
- (int)(ToolbarSize), // Top - This is set to the ToolbarSize so that it draws under the toolbar.
- (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
- (int)(((3 * ListAmount) + 2) * (Screen.height * 0.01f))); // Height - Set to the height of a button, plus the length of the list.
- }
- public Rect ObjectRect(int ListIndex, int AddedSize)
- {
- return new Rect((int)((ButtonPosition.x + AddedSize ) * (Screen.height * 0.01f)), // Left
- (int)(ToolbarSize + 2) + ((3 * ListIndex) * (Screen.height * 0.01f)), // Top - This is set to the ToolbarSize so that it draws under the toolbar.
- (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
- (int)((3) * (Screen.height * 0.01f))); // Height
- }
- public Rect ObjectRect(int ListIndex, Vector2 AdjustPosition, int AddedSize)
- {
- return new Rect((int)((ButtonPosition.x + AddedSize) * (Screen.height * 0.01f)) + AdjustPosition.x, // Left
- (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.
- (int)((18 * 1.25f) * (Screen.height * 0.01f)), // Width
- (int)((3) * (Screen.height * 0.01f))); // Height
- }
- public void DrawButton(GUIStyle InSkin)
- {
- CheckMouse(1);
- ToolbarSize = parent.ToolbarSize * (Screen.height * 0.01f); // Gets the size of the toolbar so we know where to place menus
- GUI.DrawTexture(ButtonRect(), CurrentTexture, ScaleMode.StretchToFill, true);
- InSkin.alignment = TextAnchor.MiddleCenter;
- InSkin.fontSize = (int)(2 * (Screen.height * 0.01f));
- InSkin.normal.textColor = Color.black;
- GUI.Label(ButtonRect(ButtonAdjust), ButtonText, InSkin);
- InSkin.normal.textColor = Color.white;
- GUI.Label(ButtonRect(), ButtonText, InSkin);
- if (DisplayingMenu)
- DrawMenu(InSkin);
- }
- public void DrawSwatch(int xMultiply, bool active, bool isChecked)
- {
- Active = active;
- Checked = isChecked;
- CheckMouse(xMultiply);
- GUI.DrawTexture(ButtonRect(xMultiply), CurrentTexture, ScaleMode.StretchToFill, true);
- if (Checked)
- GUI.DrawTexture(ButtonRect(xMultiply), parent.CheckmarkTexture, ScaleMode.StretchToFill, true);
- }
- public void DrawMenu(GUIStyle InSkin)
- {
- GUI.DrawTexture(MenuRect(ObjectList.GetLength(0)), parent.MenuBackground, ScaleMode.StretchToFill, true);
- int ListIndex = 0;
- foreach (string Object in ObjectList)
- {
- DrawSelector(ListIndex, ObjectRect(ListIndex, 0),Object);
- InSkin.alignment = TextAnchor.MiddleLeft;
- InSkin.fontSize = (int)(2 * (Screen.height * 0.01f));
- InSkin.normal.textColor = Color.black;
- GUI.Label(ObjectRect(ListIndex, ButtonAdjust, 2), Object, InSkin);
- InSkin.normal.textColor = Color.white;
- GUI.Label(ObjectRect(ListIndex, 2), Object, InSkin);
- ++ListIndex;
- }
- Vector2 MousePos = new Vector2(Input.mousePosition.x - 2, ((Input.mousePosition.y - Screen.height) * -1) - 2); // Gives us an easier to use variable
- if (MousePos.x > MenuRect(ObjectList.GetLength(0)).x
- & MousePos.y > MenuRect(ObjectList.GetLength(0)).y
- & MousePos.x < MenuRect(ObjectList.GetLength(0)).xMax
- & MousePos.y < MenuRect(ObjectList.GetLength(0)).yMax)
- {
- parent.overToolbar = true;
- }
- else
- if (parent.leftHold)
- {
- DisplayingMenu = false;
- }
- }
- public void DrawSelector(int ListIndex, Rect InRect, string InObject)
- {
- Vector2 MousePos = new Vector2(Input.mousePosition.x - 2, ((Input.mousePosition.y - Screen.height) * -1) - 2); // Gives us an easier to use variable
- if (MousePos.x > InRect.x
- & MousePos.y > InRect.y
- & MousePos.x < InRect.xMax
- & MousePos.y < InRect.yMax)
- {
- GUI.DrawTexture(InRect, parent.MenuSelect, ScaleMode.StretchToFill, true);
- SetName = InObject;
- if (!parent.leftHold)
- parent.overButton = true;
- CurrentTexture = HighlightTexture;
- if (parent.leftInitial & !parent.leftHold)
- {
- ButtonClicked();
- parent.leftInitial = false;
- }
- }
- }
- void CheckMouse(int xMulti)
- {
- if (parent == null)
- {
- parent = GameObject.Find("EditorScripts").GetComponent<Toolbar>();
- }
- Vector2 MousePos = new Vector2(Input.mousePosition.x-2,((Input.mousePosition.y - Screen.height) * -1)-2); // Gives us an easier to use variable
- if (Active)
- {
- if (MousePos.x > ButtonRect(xMulti).x & MousePos.y > ButtonRect(xMulti).y & MousePos.x < ButtonRect(xMulti).xMax & MousePos.y < ButtonRect(xMulti).yMax)
- {
- if (!parent.leftHold)
- parent.overButton = true;
- CurrentTexture = HighlightTexture;
- if (parent.leftInitial & !parent.leftHold)
- {
- ButtonClicked();
- parent.leftInitial = false;
- }
- if (parent.leftHold & parent.overButton)
- {
- CurrentTexture = ClickedTexture;
- }
- }
- else
- {
- CurrentTexture = ButtonTexture;
- }
- }
- else
- CurrentTexture = DisabledTexture;
- }
- void ButtonClicked()
- {
- switch (Type)
- {
- case ButtonType.Color:
- parent.ChangeColor(SetName);
- break;
- case ButtonType.MenuObjects:
- if (!DisplayingMenu)
- DisplayingMenu = true;
- else
- parent.ChangeObject(SetName);
- break;
- case ButtonType.SingleObject:
- parent.ChangeObject(SetName);
- break;
- default:
- Debug.Log("No type selected.");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement