Beispiel #1
0
    public static tk2dUILayoutItem FixedSizeLayoutItem()
    {
        tk2dUILayoutItem item = new tk2dUILayoutItem();

        item.fixedSize = true;
        return(item);
    }
    protected override void GetItems(Transform t)
    {
        tk2dUILayout objLayout = t.GetComponent <tk2dUILayout>();

        if (objLayout != null)
        {
            tk2dUILayoutItem curItem = null;
            foreach (var item in My.layoutItems)
            {
                if (t.gameObject == item.gameObj)
                {
                    curItem = item;
                    curItem.inLayoutList = true;
                    break;
                }
            }
            if (curItem == null)
            {
                curItem = new tk2dUILayoutItem();
            }
            itemsList.Add(curItem);
            curItem.layout  = objLayout;
            curItem.gameObj = t.gameObject;
        }

        if (objLayout == null)
        {
            for (int i = 0; i < t.childCount; ++i)
            {
                GetItems(t.GetChild(i));
            }
        }
    }
Beispiel #3
0
    protected virtual void GetItems(Transform t)
    {
        tk2dBaseSprite objSprite = t.GetComponent <tk2dBaseSprite>();
        tk2dUIMask     objMask   = t.GetComponent <tk2dUIMask>();
        tk2dUILayout   objLayout = t.GetComponent <tk2dUILayout>();

        tk2dUILayoutItem curItem = null;

        foreach (var item in My.layoutItems)
        {
            if (t.gameObject == item.gameObj)
            {
                curItem = item;
                curItem.inLayoutList = true;
                break;
            }
        }
        if (curItem == null)
        {
            curItem = new tk2dUILayoutItem();
        }
        itemsList.Add(curItem);
        curItem.sprite  = objSprite;
        curItem.UIMask  = objMask;
        curItem.layout  = objLayout;
        curItem.gameObj = t.gameObject;

        if (objLayout == null)
        {
            for (int i = 0; i < t.childCount; ++i)
            {
                GetItems(t.GetChild(i));
            }
        }
    }
Beispiel #4
0
    public void OnEnable()
    {
        foreach (var item in My.layoutItems)
        {
            item.inLayoutList = false;
        }

        itemsList = new List <tk2dUILayoutItem>();
        for (int i = 0; i < My.transform.childCount; ++i)
        {
            GetItems(My.transform.GetChild(i));
        }

        selItem = null;

        // Remove my items that weren't found in children
        List <tk2dUILayoutItem> removeItems = new List <tk2dUILayoutItem>();

        foreach (var item in My.layoutItems)
        {
            if (!item.inLayoutList)
            {
                removeItems.Add(item);
            }
        }
        foreach (var item in removeItems)
        {
            My.layoutItems.Remove(item);
        }

        OrderItems();
    }
 public void AddLayoutAtIndex(tk2dUILayout layout, tk2dUILayoutItem item, int index)
 {
     item.gameObj = layout.gameObject;
     item.layout = layout;
     base.layoutItems.Insert(index, item);
     layout.gameObject.transform.parent = base.transform;
     base.Refresh();
 }
 public void AddLayout(tk2dUILayout layout, tk2dUILayoutItem item)
 {
     item.gameObj = layout.gameObject;
     item.layout = layout;
     base.layoutItems.Add(item);
     layout.gameObject.transform.parent = base.transform;
     base.Refresh();
 }
    public void AddLayoutAtIndex(tk2dUILayout layout, tk2dUILayoutItem item, int index)
    {
        item.gameObj = layout.gameObject;
        item.layout  = layout;
        layoutItems.Insert(index, item);

        layout.gameObject.transform.parent = transform;

        Refresh();
    }
    public void AddLayout(tk2dUILayout layout, tk2dUILayoutItem item)
    {
        item.gameObj = layout.gameObject;
        item.layout  = layout;
        layoutItems.Add(item);

        layout.gameObject.transform.parent = transform;

        Refresh();
    }
    protected override void ItemInspector(tk2dUILayoutItem item)
    {
        var layout = item.layout;
        var sizer = (tk2dUILayoutContainerSizer)target;

        int selection = 0;
        if (item.fixedSize) selection = 1;
        else if (item.fillPercentage > 0) selection = 2;
        else if (!item.inLayoutList) selection = 0;
        else selection = 3;

        GUILayout.Label("Layout Mode");
        int newSelection = GUILayout.Toolbar(selection, new string[] { "Off", "Fixed Size", "Fill %", "Proportion" });
        if (newSelection != selection) {
            switch (newSelection) {
                case 0:
                    SetItemInLayoutList(item, false);
                    break;
                case 1:
                    SetItemInLayoutList(item, true);
                    item.fixedSize = true;
                    item.fillPercentage = -1;
                    break;
                case 2:
                    item.fillPercentage = sizer.horizontal ? (100 * (layout.bMax.x - layout.bMin.x) / (sizer.bMax.x - sizer.bMin.x)) :
                                                             (100 * (layout.bMax.y - layout.bMin.y) / (sizer.bMax.y - sizer.bMin.y));
                    SetItemInLayoutList(item, true);
                    item.fixedSize = false;
                    break;
                case 3:
                    SetItemInLayoutList(item, true);
                    item.fixedSize = false;
                    item.fillPercentage = -1;
                    item.sizeProportion = 1;
                    break;
            }
        }

        EditorGUI.indentLevel++;
        if (selection == 2) {
            item.fillPercentage = EditorGUILayout.FloatField(sizer.horizontal ? "Width %" : "Height %", item.fillPercentage);
        }
        else if (selection == 3) {
            item.sizeProportion = EditorGUILayout.FloatField("Proportion", item.sizeProportion);
        }
        EditorGUI.indentLevel--;

        UpdateChildren();
    }
 protected void SetItemInLayoutList(tk2dUILayoutItem item, bool value)
 {
     if (value && !My.layoutItems.Contains(item))
     {
         item.fixedSize = true;             // default to fixed size
         My.layoutItems.Add(item);
         item.inLayoutList = true;
         OrderItems();
     }
     else if (!value && My.layoutItems.Contains(item))
     {
         My.layoutItems.Remove(item);
         item.inLayoutList = false;
     }
 }
    protected virtual void ItemInspector(tk2dUILayoutItem item)
    {
        bool newInLayoutList = GUILayout.Toggle(item.inLayoutList, "Active");

        if (newInLayoutList && !My.layoutItems.Contains(item))
        {
            item.fixedSize = true;             // default to fixed size
            My.layoutItems.Add(item);
            item.inLayoutList = true;
            OrderItems();
        }
        else if (!newInLayoutList && My.layoutItems.Contains(item))
        {
            My.layoutItems.Remove(item);
            item.inLayoutList = false;
        }
    }
Beispiel #12
0
    protected void ArrowKeyNav()
    {
        Event ev = Event.current;

        if (ev.type == EventType.KeyDown && (ev.keyCode == KeyCode.DownArrow || ev.keyCode == KeyCode.UpArrow))
        {
            int arrowkeySel = (ev.keyCode == KeyCode.DownArrow) ? 1 : -1;
            int selIdx      = -1;
            for (int i = 0; i < itemsList.Count; ++i)
            {
                if (itemsList[i] == selItem)
                {
                    selIdx = i;
                }
            }
            if (selIdx != -1)
            {
                selIdx += arrowkeySel;
                if (selIdx >= 0 && selIdx < itemsList.Count)
                {
                    selItem = itemsList[selIdx];
                    EditorGUIUtility.PingObject(selItem.gameObj);
                    SceneView.RepaintAll();
                }
                ev.Use();
            }
        }
        if (ev.type == EventType.KeyDown && ev.keyCode == KeyCode.Escape)
        {
            if (selItem != null)
            {
                selItem = null;
                ev.Use();
            }
        }
    }
	protected override void GetItems(Transform t) {
		tk2dUILayout objLayout = t.GetComponent<tk2dUILayout>();

		if (objLayout != null) {
			tk2dUILayoutItem curItem = null;
			foreach (var item in My.layoutItems) {
				if (t.gameObject == item.gameObj) {
					curItem = item;
					curItem.inLayoutList = true;
					break;
				}
			}
			if (curItem == null)
				curItem = new tk2dUILayoutItem();
			itemsList.Add(curItem);
			curItem.layout = objLayout;
			curItem.gameObj = t.gameObject;
		}

		if (objLayout == null) {
			for (int i = 0; i < t.childCount; ++i)
				GetItems(t.GetChild(i));
		}
	}
Beispiel #14
0
    void ItemInspector(tk2dUILayoutItem item)
    {
        float snapControlSize  = 86;
        float snapButtonSize   = 17;
        float snapButtonBorder = 1;
        float middleMarginSize = snapButtonSize + snapButtonBorder * 2;
        Event ev = Event.current;

        Color32 tmpGray     = Color.gray;
        Color   activeColor = EditorGUIUtility.isProSkin ? Color.white : Color.black;
        Color   grayColor   = EditorGUIUtility.isProSkin ? tmpGray : new Color32(150, 150, 150, 255);
        Color   fillColor   = EditorGUIUtility.isProSkin ? new Color32(136, 201, 27, 255) : new Color32(62, 115, 0, 255);

        GUILayout.BeginHorizontal();
        GUILayout.Space(8);
        Rect rect = GUILayoutUtility.GetRect(snapControlSize, snapControlSize, GUILayout.ExpandWidth(false));

        GUILayout.Space(4);
        GUILayout.EndHorizontal();

        GUIStyle borderStyle = tk2dEditorSkin.GetStyle("Border");

        GUI.color = activeColor; GUI.Box(rect, "", borderStyle); GUI.color = Color.white;

        Rect middleRect = new Rect(rect.x + middleMarginSize, rect.y + middleMarginSize, rect.width - middleMarginSize * 2, rect.height - middleMarginSize * 2);

        GUI.color = EditorGUIUtility.isProSkin ? grayColor : Color.black;
        GUI.Box(middleRect, "", borderStyle);
        GUI.color = Color.white;

        GUIStyle style = new GUIStyle();

        style.border  = new RectOffset(0, 0, 0, 0);
        style.margin  = new RectOffset(0, 0, 0, 0);
        style.padding = new RectOffset(0, 0, 0, 0);

        Rect r = new Rect(rect.x + snapButtonBorder, rect.y + snapButtonBorder, rect.width - snapButtonBorder * 2, rect.height - snapButtonBorder * 2);

        if (item == null)
        {
            GUI.color = grayColor;
            GUI.Toggle(new Rect(r.x, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_lr"), style);
            GUI.Toggle(new Rect(r.x + r.width - snapButtonSize, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_lr"), style);
            GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y + r.height - snapButtonSize, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_ud"), style);
            GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_ud"), style);
            GUI.color = Color.white;
            return;
        }

        GUI.color = item.snapLeft ? activeColor : grayColor; item.snapLeft = GUI.Toggle(new Rect(r.x, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), item.snapLeft, tk2dEditorSkin.GetTexture("anchor_lr"), style);
        GUI.color = item.snapRight ? activeColor : grayColor; item.snapRight = GUI.Toggle(new Rect(r.x + r.width - snapButtonSize, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), item.snapRight, tk2dEditorSkin.GetTexture("anchor_lr"), style);
        GUI.color = item.snapBottom ? activeColor : grayColor; item.snapBottom = GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y + r.height - snapButtonSize, snapButtonSize, snapButtonSize), item.snapBottom, tk2dEditorSkin.GetTexture("anchor_ud"), style);
        GUI.color = item.snapTop ? activeColor : grayColor; item.snapTop = GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y, snapButtonSize, snapButtonSize), item.snapTop, tk2dEditorSkin.GetTexture("anchor_ud"), style);

        bool hasSnap = item.snapLeft || item.snapRight || item.snapBottom || item.snapTop;
        bool allSnap = item.snapLeft && item.snapRight && item.snapTop && item.snapBottom;

        if (ev.type == EventType.MouseUp && middleRect.Contains(ev.mousePosition))
        {
            bool v = !allSnap;
            item.snapLeft = item.snapRight = item.snapTop = item.snapBottom = v;
            ev.Use();
        }

        int  strectRectSize   = 20;
        int  strectRectBorder = 4;
        Rect fullStrectRect   = new Rect(middleRect.x + strectRectBorder, middleRect.y + strectRectBorder, middleRect.width - strectRectBorder * 2, middleRect.height - strectRectBorder * 2);
        Rect strechRect       = new Rect(fullStrectRect.x + fullStrectRect.width / 2 - strectRectSize / 2, fullStrectRect.y + fullStrectRect.height / 2 - strectRectSize / 2, strectRectSize, strectRectSize);

        if (item.snapLeft)
        {
            strechRect.xMin = fullStrectRect.xMin;
        }
        if (item.snapRight)
        {
            strechRect.xMax = fullStrectRect.xMax;
        }
        if (item.snapTop)
        {
            strechRect.yMin = fullStrectRect.yMin;
        }
        if (item.snapBottom)
        {
            strechRect.yMax = fullStrectRect.yMax;
        }
        GUI.color = hasSnap ? fillColor : grayColor;
        GUI.Box(strechRect, "", tk2dEditorSkin.WhiteBox);
        GUI.color = Color.white;

        if (hasSnap && !My.layoutItems.Contains(item))
        {
            My.layoutItems.Add(item);
            item.inLayoutList = true;
            OrderItems();
        }
        else if (!hasSnap && My.layoutItems.Contains(item))
        {
            My.layoutItems.Remove(item);
            item.inLayoutList = false;
        }
    }
Beispiel #15
0
    public override void OnInspectorGUI()
    {
        GUILayout.Space(16);
        GUILayout.BeginVertical();

        if (My.GetComponent <tk2dBaseSprite>() != null || My.GetComponent <tk2dTextMesh>() != null || My.GetComponent <tk2dUIMask>() != null)
        {
            EditorGUILayout.HelpBox("Please remove Sprite/TextMesh/UIMask from this Object\nin order to use Layout!", MessageType.Error);
            GUILayout.EndVertical();
            return;
        }

        //My.bMin = EditorGUILayout.Vector3Field("bMin", My.bMin);
        //My.bMax = EditorGUILayout.Vector3Field("bMax", My.bMax);

        GUILayout.BeginHorizontal();

        int width = 96;

        GUILayout.BeginVertical();
        updateChildren = !GUILayout.Toggle(!updateChildren, "Edit Mode", "button", GUILayout.ExpandWidth(false), GUILayout.Width(width));
        bool editMode = !updateChildren;

        GUI.enabled = editMode;

        EditorGUI.indentLevel++;
        My.autoResizeCollider = EditorGUILayout.Toggle("Resize Collider", My.autoResizeCollider);

        GUILayout.BeginHorizontal();
        EditorGUILayout.PrefixLabel("Fit Layout");
        if (GUILayout.Button("To Geometry", GUILayout.ExpandWidth(false), GUILayout.Width(width)))
        {
            int numInList = 0;
            foreach (tk2dUILayoutItem v in itemsList)
            {
                if (v.inLayoutList)
                {
                    numInList++;
                }
            }
            if (numInList == 0)
            {
                EditorUtility.DisplayDialog("Fit Layout", "Fit Layout requires items anchored in the layout.", "Ok");
            }
            else
            {
                Object[] deps = EditorUtility.CollectDeepHierarchy(new Object[] { My });
                tk2dUndo.RecordObjects(deps, "Resize");

                Vector3[] minMax = new Vector3[] { Vector3.one *float.MaxValue, Vector3.one * -float.MaxValue };
                GetChildRendererBounds(My.transform.worldToLocalMatrix, minMax, My.transform);
                Vector3 dMin = new Vector3(minMax[0].x - My.bMin.x, minMax[0].y - My.bMin.y);
                Vector3 dMax = new Vector3(minMax[1].x - My.bMax.x, minMax[1].y - My.bMax.y);

                bool    lastAutoResizeCollider = My.autoResizeCollider;
                Vector3 lastPos = My.transform.position;
                My.autoResizeCollider = false;
                My.Reshape(dMin, dMax, false);
                My.autoResizeCollider = lastAutoResizeCollider;
                var box = My.GetComponent <BoxCollider>();
                if (box != null)
                {
                    box.center -= My.transform.worldToLocalMatrix.MultiplyVector(My.transform.position - lastPos);
                }

                foreach (var dep in deps)
                {
                    tk2dUtil.SetDirty(dep);
                }
            }
        }
        GUILayout.EndHorizontal();
        EditorGUI.indentLevel--;
        GUI.enabled = true;

        GUILayout.EndVertical();

        GUILayout.FlexibleSpace();

        GUI.enabled = editMode && (selItem != null);
        ItemInspector(selItem);
        GUI.enabled = true;

        GUILayout.EndHorizontal();

        GUILayout.Space(4);

        bool   warnLayoutHasSprite = false;
        string warnLayoutName      = "";

        GUILayout.BeginVertical("box");
        foreach (var item in itemsList)
        {
            if (item.inLayoutList)
            {
                if (item.layout)
                {
                    GUI.color = new Color(1.0f, 1.0f, 0.3f);
                }
                else
                {
                    GUI.color = Color.green;
                }

                if (item.layout != null && item.gameObj.GetComponent <tk2dBaseSprite>() != null)
                {
                    warnLayoutHasSprite = true;
                    warnLayoutName      = item.gameObj.name;
                }
            }
            else
            {
                GUI.color = Color.white;
            }
            if (GUILayout.Toggle(item == selItem, item.gameObj.name, tk2dEditorSkin.SC_ListBoxItem))
            {
                if (selItem != item)
                {
                    EditorGUIUtility.PingObject(item.gameObj);
                    SceneView.RepaintAll();
                    Repaint();
                }
                selItem = item;
            }
        }
        GUILayout.EndVertical();
        GUI.color = Color.white;

        if (warnLayoutHasSprite)
        {
            EditorGUILayout.HelpBox("Child Layout with Sprite found. Cannot resize \"" + warnLayoutName + "\"", MessageType.Error);
        }

        ArrowKeyNav();

        GUILayout.Space(40);

        GUILayout.EndVertical();

        if (GUI.changed)
        {
            tk2dUtil.SetDirty(target);
        }
    }
	public void OnEnable() {
		foreach (var item in My.layoutItems)
			item.inLayoutList = false;

		itemsList = new List<tk2dUILayoutItem>();
		for (int i = 0; i < My.transform.childCount; ++i)
			GetItems(My.transform.GetChild(i));

		selItem = null;

		// Remove my items that weren't found in children
		List<tk2dUILayoutItem> removeItems = new List<tk2dUILayoutItem>();
		foreach (var item in My.layoutItems)
			if (!item.inLayoutList)
				removeItems.Add(item);
		foreach (var item in removeItems)
			My.layoutItems.Remove(item);

		OrderItems();
	}
Beispiel #17
0
	public static tk2dUILayoutItem FixedSizeLayoutItem() {
		tk2dUILayoutItem item = new tk2dUILayoutItem();
		item.fixedSize = true;
		return item;
	}
	void ItemInspector(tk2dUILayoutItem item) {
		float snapControlSize = 86;
		float snapButtonSize = 17;
		float snapButtonBorder = 1;
		float middleMarginSize = snapButtonSize + snapButtonBorder * 2;
		Event ev = Event.current;

		Color32 tmpGray = Color.gray;
		Color activeColor = EditorGUIUtility.isProSkin ? Color.white : Color.black;
		Color grayColor = EditorGUIUtility.isProSkin ? tmpGray : new Color32(150, 150, 150, 255);
		Color fillColor = EditorGUIUtility.isProSkin ? new Color32(136, 201, 27, 255) : new Color32(62, 115, 0, 255);

		GUILayout.BeginHorizontal();
		GUILayout.Space(8);
		Rect rect = GUILayoutUtility.GetRect(snapControlSize, snapControlSize, GUILayout.ExpandWidth(false));
		GUILayout.Space(4);
		GUILayout.EndHorizontal();

		GUIStyle borderStyle = tk2dEditorSkin.GetStyle("Border");
		GUI.color = activeColor; GUI.Box(rect, "", borderStyle); GUI.color = Color.white;
		
		Rect middleRect = new Rect( rect.x + middleMarginSize, rect.y + middleMarginSize, rect.width - middleMarginSize * 2, rect.height - middleMarginSize * 2 );
		GUI.color = EditorGUIUtility.isProSkin ? grayColor : Color.black;
		GUI.Box(middleRect, "", borderStyle);
		GUI.color = Color.white;

		GUIStyle style = new GUIStyle("");
		style.border = new RectOffset(0, 0, 0, 0);
		style.margin = new RectOffset(0, 0, 0, 0);
		style.padding = new RectOffset(0, 0, 0, 0);

		Rect r = new Rect(rect.x + snapButtonBorder, rect.y + snapButtonBorder, rect.width - snapButtonBorder * 2, rect.height - snapButtonBorder * 2);

		if (item == null) {
			GUI.color = grayColor;
			GUI.Toggle(new Rect(r.x, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_lr"), style);
			GUI.Toggle(new Rect(r.x + r.width - snapButtonSize, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_lr"), style);
			GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y + r.height - snapButtonSize, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_ud"), style);
			GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y, snapButtonSize, snapButtonSize), false, tk2dEditorSkin.GetTexture("anchor_ud"), style);
			GUI.color = Color.white;
			return;
		}

		GUI.color = item.snapLeft ? activeColor : grayColor; item.snapLeft = GUI.Toggle(new Rect(r.x, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), item.snapLeft, tk2dEditorSkin.GetTexture("anchor_lr"), style);
		GUI.color = item.snapRight ? activeColor : grayColor; item.snapRight = GUI.Toggle(new Rect(r.x + r.width - snapButtonSize, r.y + r.height / 2 - snapButtonSize / 2, snapButtonSize, snapButtonSize), item.snapRight, tk2dEditorSkin.GetTexture("anchor_lr"), style);
		GUI.color = item.snapBottom ? activeColor : grayColor; item.snapBottom = GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y + r.height - snapButtonSize, snapButtonSize, snapButtonSize), item.snapBottom, tk2dEditorSkin.GetTexture("anchor_ud"), style);
		GUI.color = item.snapTop ? activeColor : grayColor; item.snapTop = GUI.Toggle(new Rect(r.x + r.width / 2 - snapButtonSize / 2, r.y, snapButtonSize, snapButtonSize), item.snapTop, tk2dEditorSkin.GetTexture("anchor_ud"), style);

		bool hasSnap = item.snapLeft || item.snapRight || item.snapBottom || item.snapTop;
		bool allSnap = item.snapLeft && item.snapRight && item.snapTop && item.snapBottom;

		if ( ev.type == EventType.MouseUp && middleRect.Contains(ev.mousePosition) ) {
			bool v = !allSnap;
			item.snapLeft = item.snapRight = item.snapTop = item.snapBottom = v;
			ev.Use();
		}

		int strectRectSize = 20;
		int strectRectBorder = 4;
		Rect fullStrectRect = new Rect( middleRect.x + strectRectBorder, middleRect.y + strectRectBorder, middleRect.width - strectRectBorder * 2, middleRect.height - strectRectBorder * 2);
		Rect strechRect = new Rect( fullStrectRect.x + fullStrectRect.width / 2 - strectRectSize / 2, fullStrectRect.y + fullStrectRect.height / 2 - strectRectSize / 2, strectRectSize, strectRectSize );
		if (item.snapLeft) strechRect.xMin = fullStrectRect.xMin;
		if (item.snapRight) strechRect.xMax = fullStrectRect.xMax;
		if (item.snapTop) strechRect.yMin = fullStrectRect.yMin;
		if (item.snapBottom) strechRect.yMax = fullStrectRect.yMax;
		GUI.color = hasSnap ? fillColor : grayColor;
		GUI.Box( strechRect, "", tk2dEditorSkin.WhiteBox );
		GUI.color = Color.white;

		if (hasSnap && !My.layoutItems.Contains(item)) {
			My.layoutItems.Add(item);
			item.inLayoutList = true;
			OrderItems();
		} else if (!hasSnap && My.layoutItems.Contains(item)) {
			My.layoutItems.Remove(item);
			item.inLayoutList = false;
		}
	}
	protected virtual void GetItems(Transform t) {
		tk2dBaseSprite objSprite = t.GetComponent<tk2dBaseSprite>();
		tk2dUIMask objMask = t.GetComponent<tk2dUIMask>();
		tk2dUILayout objLayout = t.GetComponent<tk2dUILayout>();

		tk2dUILayoutItem curItem = null;
		foreach (var item in My.layoutItems) {
			if (t.gameObject == item.gameObj) {
				curItem = item;
				curItem.inLayoutList = true;
				break;
			}
		}
		if (curItem == null)
			curItem = new tk2dUILayoutItem();
		itemsList.Add(curItem);
		curItem.sprite = objSprite;
		curItem.UIMask = objMask;
		curItem.layout = objLayout;
		curItem.gameObj = t.gameObject;

		if (objLayout == null) {
			for (int i = 0; i < t.childCount; ++i)
				GetItems(t.GetChild(i));
		}
	}
	protected void SetItemInLayoutList(tk2dUILayoutItem item, bool value) {
		if (value && !My.layoutItems.Contains(item)) {
			item.fixedSize = true; // default to fixed size
			My.layoutItems.Add(item);
			item.inLayoutList = true;
			OrderItems();
		} else if (!value && My.layoutItems.Contains(item)) {
			My.layoutItems.Remove(item);
			item.inLayoutList = false;
		}
	}
	public override void OnInspectorGUI() {
		GUILayout.Space(16);
		GUILayout.BeginVertical();

		if (My.GetComponent<tk2dBaseSprite>() != null || My.GetComponent<tk2dTextMesh>() != null || My.GetComponent<tk2dUIMask>() != null) {
			EditorGUILayout.HelpBox("Please remove Sprite/TextMesh/UIMask from this Object\nin order to use Layout!", MessageType.Error);
			GUILayout.EndVertical();
			return;
		}

		//My.bMin = EditorGUILayout.Vector3Field("bMin", My.bMin);
		//My.bMax = EditorGUILayout.Vector3Field("bMax", My.bMax);

		GUILayout.BeginHorizontal();

		int width = 96;

		GUILayout.BeginVertical();
		updateChildren = !GUILayout.Toggle(!updateChildren, "Edit Mode", "button", GUILayout.ExpandWidth(false), GUILayout.Width(width));
		bool editMode = !updateChildren;
		GUI.enabled = editMode;

		EditorGUI.indentLevel++;
		My.autoResizeCollider = EditorGUILayout.Toggle("Resize Collider", My.autoResizeCollider);

		GUILayout.BeginHorizontal();
		EditorGUILayout.PrefixLabel("Fit Layout");
		if (GUILayout.Button("To Geometry", GUILayout.ExpandWidth(false), GUILayout.Width(width))) {
			int numInList = 0;
			foreach (tk2dUILayoutItem v in itemsList) {
				if (v.inLayoutList) {
					numInList++;
				}
			}
			if (numInList == 0) {
				EditorUtility.DisplayDialog("Fit Layout", "Fit Layout requires items anchored in the layout.", "Ok");
			}
			else {
				Object[] deps = EditorUtility.CollectDeepHierarchy(new Object[] {My});
				tk2dUndo.RecordObjects(deps, "Resize");

				Vector3[] minMax = new Vector3[] {Vector3.one * float.MaxValue, Vector3.one * -float.MaxValue};
				GetChildRendererBounds(My.transform.worldToLocalMatrix, minMax, My.transform);
				Vector3 dMin = new Vector3(minMax[0].x - My.bMin.x, minMax[0].y - My.bMin.y);
				Vector3 dMax = new Vector3(minMax[1].x - My.bMax.x, minMax[1].y - My.bMax.y);

				bool lastAutoResizeCollider = My.autoResizeCollider;
				Vector3 lastPos = My.transform.position;
				My.autoResizeCollider = false;
				My.Reshape(dMin, dMax, false);
				My.autoResizeCollider = lastAutoResizeCollider;
				var box = My.GetComponent<BoxCollider>();
				if (box != null)
					box.center -= My.transform.worldToLocalMatrix.MultiplyVector(My.transform.position - lastPos);

				foreach (var dep in deps)
					EditorUtility.SetDirty(dep);
			}
		}
		GUILayout.EndHorizontal();
		EditorGUI.indentLevel--;
		GUI.enabled = true;

		GUILayout.EndVertical();

		GUILayout.FlexibleSpace();

		GUI.enabled = editMode && (selItem != null);
		ItemInspector(selItem);
		GUI.enabled = true;

		GUILayout.EndHorizontal();

		GUILayout.Space(4);

		bool warnLayoutHasSprite = false;
		string warnLayoutName = "";

		GUILayout.BeginVertical("box");
		foreach (var item in itemsList) {
			if (item.inLayoutList) {
				if (item.layout) GUI.color = new Color(1.0f, 1.0f, 0.3f);
				else GUI.color = Color.green;

				if (item.layout != null && item.gameObj.GetComponent<tk2dBaseSprite>() != null) {
					warnLayoutHasSprite = true;
					warnLayoutName = item.gameObj.name;
				}
			} else {
				GUI.color = Color.white;
			}
			if (GUILayout.Toggle(item == selItem, item.gameObj.name, tk2dEditorSkin.SC_ListBoxItem)) {
				if (selItem != item) {
					EditorGUIUtility.PingObject( item.gameObj );
					SceneView.RepaintAll();
					Repaint();
				}
				selItem = item;
			}
		}
		GUILayout.EndVertical();
		GUI.color = Color.white;

		if (warnLayoutHasSprite) {
			EditorGUILayout.HelpBox("Child Layout with Sprite found. Cannot resize \"" + warnLayoutName + "\"", MessageType.Error);
		}

		ArrowKeyNav();

		GUILayout.Space(40);

		GUILayout.EndVertical();

		if (GUI.changed) {
			EditorUtility.SetDirty(target);
		}
	}
	protected virtual void ItemInspector(tk2dUILayoutItem item) {
		bool newInLayoutList = GUILayout.Toggle(item.inLayoutList, "Active");

		if (newInLayoutList && !My.layoutItems.Contains(item)) {
			item.fixedSize = true; // default to fixed size
			My.layoutItems.Add(item);
			item.inLayoutList = true;
			OrderItems();
		} else if (!newInLayoutList && My.layoutItems.Contains(item)) {
			My.layoutItems.Remove(item);
			item.inLayoutList = false;
		}
	}
	protected void ArrowKeyNav() {
		Event ev = Event.current;
		if (ev.type == EventType.KeyDown && (ev.keyCode == KeyCode.DownArrow || ev.keyCode == KeyCode.UpArrow)) {
			int arrowkeySel = (ev.keyCode == KeyCode.DownArrow) ? 1 : -1;
			int selIdx = -1;
			for (int i = 0; i < itemsList.Count; ++i)
				if (itemsList[i] == selItem)
					selIdx = i;
			if (selIdx != -1) {
				selIdx += arrowkeySel;
				if (selIdx >= 0 && selIdx < itemsList.Count) {
					selItem = itemsList[selIdx];
					EditorGUIUtility.PingObject(selItem.gameObj);
					SceneView.RepaintAll();
				}
				ev.Use();
			}
		}
		if (ev.type == EventType.KeyDown && ev.keyCode == KeyCode.Escape) {
			if (selItem != null) {
				selItem = null;
				ev.Use();
			}
		}
	}
Beispiel #24
0
    protected override void ItemInspector(tk2dUILayoutItem item)
    {
        var layout = item.layout;

        var sizer = (tk2dUILayoutContainerSizer)target;



        int selection = 0;

        if (item.fixedSize)
        {
            selection = 1;
        }

        else if (item.fillPercentage > 0)
        {
            selection = 2;
        }

        else if (!item.inLayoutList)
        {
            selection = 0;
        }

        else
        {
            selection = 3;
        }



        GUILayout.Label("Layout Mode");

        int newSelection = GUILayout.Toolbar(selection, new string[] { "Off", "Fixed Size", "Fill %", "Proportion" });

        if (newSelection != selection)
        {
            switch (newSelection)
            {
            case 0:

                SetItemInLayoutList(item, false);

                break;

            case 1:

                SetItemInLayoutList(item, true);

                item.fixedSize = true;

                item.fillPercentage = -1;

                break;

            case 2:

                item.fillPercentage = sizer.horizontal ? (100 * (layout.bMax.x - layout.bMin.x) / (sizer.bMax.x - sizer.bMin.x)) :

                                      (100 * (layout.bMax.y - layout.bMin.y) / (sizer.bMax.y - sizer.bMin.y));

                SetItemInLayoutList(item, true);

                item.fixedSize = false;

                break;

            case 3:

                SetItemInLayoutList(item, true);

                item.fixedSize = false;

                item.fillPercentage = -1;

                item.sizeProportion = 1;

                break;
            }
        }



        EditorGUI.indentLevel++;

        if (selection == 2)
        {
            item.fillPercentage = EditorGUILayout.FloatField(sizer.horizontal ? "Width %" : "Height %", item.fillPercentage);
        }

        else if (selection == 3)
        {
            item.sizeProportion = EditorGUILayout.FloatField("Proportion", item.sizeProportion);
        }

        EditorGUI.indentLevel--;



        UpdateChildren();
    }