public override void Start() { if (!LinkedObject.HasComponent <Button2DComponent>()) { Debug.LogWarning("The 2DObject \"" + LinkedObject.ObjectName + "\" has a Dropdown 2D Component but no button to interact with it. A button 2D Component has automatically been added.", true); LinkedObject.AddComponent(new Button2DComponent()); } DropDownParent = new Object2D("Dropdown_" + LinkedObject.ObjectName, LinkedObject.Position + new Vector2(0, LinkedObject.Size.Y / 2), Size, LinkedObject.Rotation, new Component2D[] { new Image2DComponent(DefaultValues.PixelTexture, DropDownColor), new Spacer2DComponent(SpacerOption.VerticalSpacer, ItemSpacing, Alignment.TopLeft, ItemSpacing) }, Alignment.TopLeft, LinkedObject.Layer, LinkedObject); if (MustClick) { LinkedObject.GetComponent <Button2DComponent>().OnClick += () => OpenDropdown(); LinkedObject.GetComponent <Button2DComponent>().OnClickOutside += () => CloseDropdown(); } else { LinkedObject.GetComponent <Button2DComponent>().OnEnter += () => OpenDropdown(); LinkedObject.GetComponent <Button2DComponent>().OnExit += () => CloseDropdown(); } foreach (Object2D obj in DropdownItems.ToArray()) { DropDownParent.AddChild(obj); } CloseDropdown(); }
void OpenDropdown() { if (!IsOpen) { DropDownParent.Enabled = true; foreach (Object2D obj in DropdownItems.ToArray()) { if (!DropDownParent.Children.Contains(obj)) { DropDownParent.AddChild(obj); } else { obj.Enabled = true; } } IsOpen = true; } }
/// <summary> /// Creates a new 2D Object /// </summary> /// <param name="objectName">The name of the object. If the name of the object already exists, the name will be appended with a number</param> /// <param name="position">The position of the object in relative space</param> /// <param name="size">The size of the object in relative space</param> /// <param name="rotation">The rotation of the object in relative space</param> /// <param name="components">An array of components that are bound to the object</param> /// <param name="alignment">The pivot point of the object</param> /// <param name="layer">The sorting layer of the object</param> /// <param name="parent">The object's parent</param> /// <param name="subLayer">If the object has a parent, this will be its sub-layer</param> /// <param name="children">This object's children</param> public Object2D(string objectName, Vector2 position, Vector2 size, float rotation = 0, Component2D[] components = null, Alignment alignment = Alignment.TopLeft, float layer = 0, Object2D parent = null, int subLayer = 1, Object2D[] children = null) { Parent = parent; if (Parent != null) { Parent.AddChild(this, subLayer); } RelativePosition = position; RelativeRotation = rotation; RelativeSize = size; Layer = layer; ObjectName = objectName; // Goes through all the possible errors if (ObjectName == "") { Debug.LogError("Object name can't be blank!", true, 3); } else if (Level.Objects2D.ContainsKey(ObjectName)) { Debug.LogWarning("The 2DObject \"" + ObjectName + "\" already exists! Appending name.", true); List <string> Num = Level.Objects2D.Keys.ToList().FindAll((x) => x.Contains(ObjectName + "_")); int CurrentBiggest = 1; foreach (string item in Num.ToList()) { int.TryParse(item.Substring(ObjectName.Length + 1), out int num); CurrentBiggest = Math.Max(num, CurrentBiggest) + 1; } ObjectName += "_" + CurrentBiggest; Level.Objects2D.Add(ObjectName, this); } else { Level.Objects2D.Add(ObjectName, this); } // Adds and Starts the scripts if (components != null) { foreach (Component2D com in components) { AddComponent(com); } } if (children != null) { foreach (Object2D Child in children) { AddChild(Child); Child.Position += position; Child.Size *= Size; Child.Rotation += rotation; } } SetPivot(alignment); CalcTransform(); }