Ejemplo n.º 1
0
        private static bool DrawItem(Rect position, OCBlock block, bool selected, int index)
        {
            Rect texturePosition = position;

            texturePosition.height = texturePosition.width;
            Rect labelPosition = position;

            labelPosition.yMin += texturePosition.height;

            if (selected)
            {
                EditorGUIUtils.FillRect(labelPosition, new Color(61 / 255f, 128 / 255f, 223 / 255f));
            }
            if (block != null)
            {
                block.DrawPreview(texturePosition);
                GUI.Label(labelPosition, block.GetName());
            }
            else
            {
                EditorGUIUtils.FillRect(texturePosition, Color.grey);
                GUI.Label(labelPosition, "Null");
            }

            if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && position.Contains(Event.current.mousePosition))
            {
                Event.current.Use();
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
    private static bool DrawItem(Rect position, Block block, bool selected, int index)
    {
        bool isClicked = false;

        if (Event.current.type == EventType.MouseDown && Event.current.button == 0 && position.Contains(Event.current.mousePosition))
        {
            isClicked = true;
        }

        Rect texturePosition = position;

        texturePosition.height = texturePosition.width;
        Rect labelPosition = position;

        labelPosition.yMin += texturePosition.height;

        Editor.CreateCachedEditor(block.Material, typeof(MaterialEditor), ref block.Editor);
        if (block.Editor != null)
        {
            block.Editor.DrawPreview(texturePosition);
        }

        if (selected)
        {
            EditorGUIUtils.FillRect(labelPosition, new Color(61 / 255f, 128 / 255f, 223 / 255f));
        }

        var style = new GUIStyle();

        style.fontStyle        = FontStyle.Bold;
        style.normal.textColor = Color.white;
        GUI.Label(labelPosition, block.Name, style);

        return(isClicked);
    }
Ejemplo n.º 3
0
        private static int SelectionGrid(IList <OCBlock> items, int index)
        {
            Rect rect;
            int  xCount, yCount;

            index = SelectionGrid(items, index, out rect, out xCount, out yCount);
            float itemWidth  = rect.width / xCount;
            float itemHeight = rect.height / yCount;

            GUI.BeginGroup(rect);
            Vector2 mouse     = Event.current.mousePosition;
            int     posX      = Mathf.FloorToInt(mouse.x / itemWidth);
            int     posY      = Mathf.FloorToInt(mouse.y / itemHeight);
            int     realIndex = -1; // номер элемента под курсором

            if (posX >= 0 && posX < xCount && posY >= 0 && posY < yCount)
            {
                realIndex = posY * xCount + posX;
            }

            int dropX = Mathf.Clamp(posX, 0, xCount - 1);
            int dropY = Mathf.Clamp(posY, 0, yCount - 1);

            if (dropY == yCount - 1 && items.Count % xCount != 0)
            {
                dropX = Mathf.Clamp(dropX, 0, items.Count % xCount);
            }
            int dropIndex = dropY * xCount + dropX;   // ближайший элемент к курсору

            if (Event.current.type == EventType.MouseDrag && Event.current.button == 0 && realIndex == index)
            {
                DragAndDrop.PrepareStartDrag();
                DragAndDrop.objectReferences = new Object[0];
                DragAndDrop.paths            = new string[0];
                DragAndDrop.SetGenericData(DRAG_AND_DROP, new Container <int>(index));
                DragAndDrop.StartDrag("DragAndDrop");
                Event.current.Use();
            }

            if (Event.current.type == EventType.DragUpdated)
            {
                Container <int> data = (Container <int>)DragAndDrop.GetGenericData(DRAG_AND_DROP);
                if (data != null)
                {
                    DragAndDrop.visualMode = DragAndDropVisualMode.Link;
                    Event.current.Use();
                }
            }

            if (Event.current.type == EventType.DragPerform)
            {
                Container <int> oldIndex = (Container <int>)DragAndDrop.GetGenericData(DRAG_AND_DROP);

                if (dropIndex > oldIndex.value)
                {
                    dropIndex--;
                }
                dropIndex = Mathf.Clamp(dropIndex, 0, items.Count - 1);
                Insert(items, dropIndex, oldIndex);

                index = dropIndex;

                DragAndDrop.AcceptDrag();
                DragAndDrop.PrepareStartDrag();
                Event.current.Use();
            }

            if (Event.current.type == EventType.Repaint && DragAndDrop.visualMode == DragAndDropVisualMode.Link)
            {
                Vector2 pos      = new Vector2(2 + dropX * itemWidth, 2 + dropY * itemHeight);
                Rect    lineRect = new Rect(pos.x - 2, pos.y, 2, itemWidth - 2);
                EditorGUIUtils.FillRect(lineRect, Color.red);
            }
            GUI.EndGroup();

            return(index);
        }