void OnGUI()
        {
            GUILayout.Label("View", EditorStyles.boldLabel);

            TesseraTilePaintingState.showBackface = EditorGUILayout.Toggle(new GUIContent("Show Backfaces", "Z to toggle"), TesseraTilePaintingState.showBackface);

            TesseraTilePaintingState.opacity = EditorGUILayout.Slider("Opacity", TesseraTilePaintingState.opacity, 0, 1);

            TesseraTilePaintingState.showAll = EditorGUILayout.Toggle(new GUIContent("Show All"), TesseraTilePaintingState.showAll);

            GUILayout.Label("Painting", EditorStyles.boldLabel);

            // TODO: See C:\Program Files\Unity\Hub\Editor\2019.2.3f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.2d.tilemap\Editor\EditorTools
            // For some further stuff to do here

            GUILayout.Label("Paint Mode");

            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayout.FlexibleSpace();
            EditorGUILayout.EditorToolbar(TesseraTileEditorToolBase.tile3dEditorTools);

            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();

            GUILayout.Label("Paint Color");

            var r = EditorGUILayout.BeginVertical();

            r.width = position.width;

            var boxStyle = GUI.skin.box;
            var tileSize = 20;

            var boxRect   = boxStyle.margin.Remove(r);
            var innerRect = boxStyle.padding.Remove(boxRect);

            // Get palette
            TesseraPalette palette = null;

            if (Selection.activeGameObject != null)
            {
                if (Selection.activeGameObject.GetComponent <TesseraTile>() is TesseraTile t)
                {
                    palette = t.palette;
                }
            }
            if (palette == null)
            {
                palette = TesseraPalette.defaultPalette;
            }

            var tilesPerRow = (int)(innerRect.width / tileSize);

            if (tilesPerRow > 0)
            {
                innerRect.height = (palette.entryCount + tilesPerRow - 1) / tilesPerRow * tileSize;
                boxRect          = boxStyle.padding.Add(innerRect);

                GUI.Box(boxRect, "");

                for (var i = 0; i < palette.entryCount; i++)
                {
                    var x        = innerRect.x + (i % tilesPerRow) * tileSize;
                    var y        = innerRect.y + (i / tilesPerRow) * tileSize;
                    var tileRect = new Rect(x, y, tileSize, tileSize);

                    var selected = TesseraTilePaintingState.paintIndex == i;
                    if (Event.current.type == EventType.Repaint)
                    {
                        TextureUtil.DrawBox(tileRect, selected, palette, i);
                    }
                }

                if (Event.current.type == EventType.MouseDown)
                {
                    var mousePosition = Event.current.mousePosition;
                    if (innerRect.Contains(mousePosition))
                    {
                        var x = (int)(mousePosition.x - innerRect.x) / tileSize;
                        var y = (int)(mousePosition.y - innerRect.y) / tileSize;
                        var i = x + y * tilesPerRow;
                        TesseraTilePaintingState.paintIndex = i;
                        Repaint();
                    }
                }

                // TODO: Handle tooltip
            }
            EditorGUILayout.EndVertical();

            GUILayout.Space(boxStyle.margin.Add(boxRect).height);

            TesseraTileEditorToolBase.CheckKeyPresses();
        }
예제 #2
0
        void DrawMatching()
        {
            var property = serializedObject.FindProperty("matchOverridesList");

            EditorGUILayout.PropertyField(property, new GUIContent("Matches"), false);

            if (property.isExpanded)
            {
                EditorGUI.indentLevel += 1;

                var r = EditorGUILayout.BeginVertical();

                var boxStyle = GUI.skin.box;
                var tileSize = 20;
                var palette  = (TesseraPalette)target;
                var count    = palette.entries.Count;


                var boxRect   = boxStyle.margin.Remove(r);
                var innerRect = boxStyle.padding.Remove(boxRect);

                innerRect.height = (count + 1) * tileSize;
                boxRect          = boxStyle.padding.Add(innerRect);

                GUI.Box(boxRect, "");
                GUILayout.Space(boxRect.height);

                // work out mouse position
                var mousePosition = Event.current.mousePosition;
                int?mouseI = null, mouseJ = null;
                if (innerRect.Contains(mousePosition))
                {
                    var i = (int)(mousePosition.x - innerRect.x) / tileSize - 1;
                    var j = (int)(mousePosition.y - innerRect.y) / tileSize - 1;
                    if (i >= 0 && i < count && j >= 0 && j < count)
                    {
                        mouseI = i;
                        mouseJ = j;
                    }
                }

                // Draw top row
                for (var i = 0; i < count; i++)
                {
                    var rect = new Rect(tileSize * (i + 1) + innerRect.x, innerRect.y, tileSize, tileSize);
                    TextureUtil.DrawBox(rect, false, palette, i);
                }
                // Draw left column
                for (var i = 0; i < count; i++)
                {
                    var rect = new Rect(innerRect.x, tileSize * (i + 1) + innerRect.y, tileSize, tileSize);
                    TextureUtil.DrawBox(rect, false, palette, i);
                }
                // Draw grid
                var centerStyle = new GUIStyle(GUIStyle.none);
                centerStyle.alignment = TextAnchor.MiddleCenter;
                var centerStyleBold = new GUIStyle(centerStyle);
                centerStyleBold.fontStyle = FontStyle.Bold;

                var t1 = TextureUtil.MakeTexture(1, 1, new Color(0, 0, 0, 0));
                var t2 = TextureUtil.MakeTexture(1, 1, new Color(0, 0, 0, 0.1f));
                var t3 = TextureUtil.MakeTexture(1, 1, new Color(0, 0, 0, 0.2f));

                for (var i = 0; i < count; i++)
                {
                    for (var j = 0; j < count; j++)
                    {
                        var darkness = (i % 2) + (j % 2);
                        centerStyleBold.normal.background = centerStyle.normal.background = darkness == 0 ? t1 : darkness == 1 ? t2 : t3;
                        var rect        = new Rect(tileSize * (i + 1) + innerRect.x, tileSize * (j + 1) + innerRect.y, tileSize, tileSize);
                        var match       = palette.Match(i, j);
                        var tooltip     = palette.entries[i].name + " - " + palette.entries[j].name;
                        var hasOverride = palette.matchOverrides.ContainsKey((i, j));
                        var style       = hasOverride ? centerStyleBold : centerStyle;
                        var mark        = match ? tick : hasOverride ? cross : "";
                        GUI.Box(rect, new GUIContent(mark, tooltip), style);
                    }
                }

                if (Event.current.type == EventType.MouseDown)
                {
                    if (mouseI != null)
                    {
                        var i           = mouseI.Value;
                        var j           = mouseJ.Value;
                        var match       = palette.Match(i, j);
                        var hasOverride = palette.matchOverrides.ContainsKey((i, j));
                        if (hasOverride)
                        {
                            // Sadly, calling seralizedObject.Update() doesn't seem to work.
                            //palette.matchOverrides.Remove((i, j));
                            //palette.matchOverrides.Remove((j, i));
                            var mol = serializedObject.FindProperty("matchOverridesList");
                            for (var u = 0; u < mol.arraySize; u++)
                            {
                                var v  = mol.GetArrayElementAtIndex(u);
                                var v1 = v.FindPropertyRelative("color1").intValue;
                                var v2 = v.FindPropertyRelative("color2").intValue;
                                if (v1 == i && v2 == j || v1 == j && v2 == i)
                                {
                                    mol.DeleteArrayElementAtIndex(u);
                                    u--;
                                }
                            }
                        }
                        else
                        {
                            // Sadly, calling seralizedObject.Update() doesn't seem to work.
                            //palette.matchOverrides[(i, j)] = !match;
                            //palette.matchOverrides[(j, i)] = !match;
                            var mol = serializedObject.FindProperty("matchOverridesList");
                            var s   = mol.arraySize;
                            mol.arraySize += i == j ? 1 : 2;
                            var i1 = mol.GetArrayElementAtIndex(s);
                            i1.FindPropertyRelative("color1").intValue   = i;
                            i1.FindPropertyRelative("color2").intValue   = j;
                            i1.FindPropertyRelative("isMatch").boolValue = !match;
                            if (i != j)
                            {
                                var i2 = mol.GetArrayElementAtIndex(s + 1);
                                i2.FindPropertyRelative("color1").intValue   = j;
                                i2.FindPropertyRelative("color2").intValue   = i;
                                i2.FindPropertyRelative("isMatch").boolValue = !match;
                            }
                        }
                        serializedObject.ApplyModifiedProperties();
                        palette.OnAfterDeserialize();
                        Repaint();
                    }
                }

                EditorGUILayout.EndVertical();
                EditorGUI.indentLevel -= 1;
            }
        }