示例#1
0
 public void SelectTemplate(UniTileTemplate newSelection)
 {
     if (newSelection == null)
     {
         return;
     }
     newSelection.CopyTo(selection);
     selection.tilesPicked = true;
     UniTileMarker.Instance.Init(newSelection);
 }
    public static void OnSceneGUI_Ammendum(TileLayer layer, UniTileTemplate manager, TileLayerEditor editor)
    {
        Handles.BeginGUI();

        GUILayout.BeginArea(new Rect(20, 40, 150, 40));
        keepRealtimeChanges = GUILayout.Toggle(keepRealtimeChanges, "Keep Playmode Changes", "Button");
        GUILayout.EndArea();

        Handles.EndGUI();

        TrackPlaymodeChanges(layer, editor);
    }
 public void CopyTo(UniTileTemplate other)
 {
     other.name 						= name;
     other.selectedTilesList 		= new TileInstance[this.selectedTilesList.Length];
     for(int i = 0; i < this.selectedTilesList.Length; i++) {
         other.selectedTilesList[i]	= this.selectedTilesList[i].Clone();
     }
     other.selectedTilesWidth 		= selectedTilesWidth;
     other.selectedTile 				= selectedTile.Clone();
     other.selectedTileEnd 			= selectedTileEnd.Clone();
     other.tilesPicked 				= tilesPicked;
 }
    public static void OnSceneGUI_Ammendum(TileLayer layer, UniTileTemplate manager, TileLayerEditor editor)
    {
        Handles.BeginGUI();

        GUILayout.BeginArea(new Rect(20, 40, 150, 40));
        keepRealtimeChanges = GUILayout.Toggle(keepRealtimeChanges, "Keep Playmode Changes", "Button");
        GUILayout.EndArea();

        Handles.EndGUI();

        TrackPlaymodeChanges(layer, editor);
    }
示例#5
0
 public void CopyTo(UniTileTemplate other)
 {
     other.name = name;
     other.selectedTilesList = new TileInstance[this.selectedTilesList.Length];
     for (int i = 0; i < this.selectedTilesList.Length; i++)
     {
         other.selectedTilesList[i] = this.selectedTilesList[i].Clone();
     }
     other.selectedTilesWidth = selectedTilesWidth;
     other.selectedTile       = selectedTile.Clone();
     other.selectedTileEnd    = selectedTileEnd.Clone();
     other.tilesPicked        = tilesPicked;
 }
示例#6
0
    private static void MouseInfo(UniTileTemplate selection)
    {
        Handles.BeginGUI();
        GUI.Box(new Rect(0, Screen.height - 218, 90, 198), "");
        GUILayout.BeginArea(new Rect(5, Screen.height - 208, 75, 188));
        GUILayout.Label(string.Format("{0}, {1}", (int)TileLayerEditor.gridMousePosition.x, (int)TileLayerEditor.gridMousePosition.y), "button", GUILayout.ExpandWidth(true));

        GUILayout.Label(string.Format("{0} x {1}", selection.selectedTilesWidth, selection.selectedTilesList.Length / selection.selectedTilesWidth), "button", GUILayout.ExpandWidth(true));

        GUILayout.Toggle(Event.current.alt, "alt: pick", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(Event.current.control, Event.current.alt ? "ctrl: cut":"ctrl: erase", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(Event.current.shift, "shift: axis", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "t: turn tile", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "h: flip hor.", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "v: flip vert.", "button", GUILayout.ExpandWidth(true));

        GUILayout.EndArea();
    }
    public void OnInspectorGUI(TileLayer layer, UniTileTemplate selection, TileLayerEditor editor)
    {
        if(layer.material == null)
            return;
        useScrollView = EditorGUILayout.Toggle("Use scrollview", useScrollView);

        float w = Mathf.Min(Screen.width, layer.material.mainTexture.width); //Mathf.Min(rect.width,layer.material.mainTexture.width);
        float scale = w / layer.material.mainTexture.width;
        float h = layer.material.mainTexture.height * scale;
        Rect rect = GUILayoutUtility.GetLastRect();
        float top = rect.yMax;

        int tilesX = (int)Mathf.Floor((float)layer.material.mainTexture.width / (layer.tileSize.x + layer.borderSize.x * 2f));

        int tileX1 = (int)Mathf.Floor((float)selection.selectedTile.id % (float)tilesX);
        int tileY1 = (int)Mathf.Floor((float)selection.selectedTile.id / (float)tilesX);

        int tileX2 = (int)Mathf.Floor((float)selection.selectedTileEnd.id % (float)tilesX);
        int tileY2 = (int)Mathf.Floor((float)selection.selectedTileEnd.id / (float)tilesX);

        int tileXMin=Mathf.Min(tileX1, tileX2);
        int tileYMin=Mathf.Min(tileY1, tileY2);
        int tileXMax=Mathf.Max(tileX1, tileX2);
        int tileYMax=Mathf.Max(tileY1, tileY2);

        int tileWidth = tileXMax - tileXMin + 1;
        int tileHeight = tileYMax - tileYMin + 1;

        float tempTop = top;

        if(useScrollView) {
            scale = 1;
            w = layer.material.mainTexture.width;
            h = layer.material.mainTexture.height;

            float scrollWidth = Screen.width-25;
            float scrollHeight = h+10;
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(scrollHeight), GUILayout.Width(scrollWidth));
            GUILayout.Label("", GUILayout.Width(scrollWidth), GUILayout.Height(scrollHeight));
            tempTop = 0;
        }
        else
        {
            GUILayout.Label("", GUILayout.Width(w), GUILayout.Height(h));
        }

        GUI.DrawTexture(new Rect(0,tempTop,w,h), layer.material.mainTexture);

        if(selection.selectedTile.id>=0 && !selection.tilesPicked) {
            GUIStyle style = new GUIStyle(GUI.skin.customStyles[0]);
            style.normal.background = SelectionTexture;
            GUI.Box(
                new Rect(
                    (tileXMin * (layer.tileSize.x + layer.borderSize.x * 2f) + layer.borderSize.x) * scale,
                    tempTop + (tileYMin * (layer.tileSize.y + layer.borderSize.y * 2f) + layer.borderSize.y) * scale,
                    (tileWidth * (layer.tileSize.x + layer.borderSize.x * 2f) - layer.borderSize.x * 2f) * scale,
                    (tileHeight * (layer.tileSize.y + layer.borderSize.y * 2f) - layer.borderSize.y * 2f) * scale
                ),
                "",
                style
            );
        }

        if(useScrollView)
        {
            EditorGUILayout.EndScrollView();
        }

        if(Event.current!=null)
        {
            if(Event.current.type==EventType.MouseDown)
            {
                if(Event.current.button==0)
                {
                    Vector2 pos = Event.current.mousePosition;
                    if(useScrollView) pos += scrollPos;
                    this.selecting=true;
                    selection.tilesPicked = false;

                    if(pos.x>=0 && pos.x<=layer.material.mainTexture.width * scale && pos.y>=top && pos.y<=top+layer.material.mainTexture.height * scale)
                    {
                        editor.selectedTemplate = - 1;
                        selection.selectedTile.id = selection.selectedTileEnd.id = (int)Mathf.Floor((pos.x / scale) /(layer.tileSize.x + layer.borderSize.x * 2f)) + (int)(Mathf.Floor((pos.y - top)/(scale * (layer.tileSize.y + layer.borderSize.y * 2f))) * tilesX);
                        if(Event.current.clickCount==2) {
                            TileEditorWindow window = TileEditorWindow.CreateInstance<TileEditorWindow>();
                            window.Setup(editor, selection.selectedTilesList);
                            window.ShowUtility();
                        }
                    }
                } else
                {
                    selection.tilesPicked = false;
                    selection.selectedTile.id = -1;
                    selection.selectedTileEnd.id = -1;
                }
            }

            if(Event.current.type==EventType.mouseDrag)
            {
                if(selecting)
                {
                    Vector2 pos = Event.current.mousePosition;
                    if(useScrollView) pos += scrollPos;
                    if(pos.x>=0 && pos.x<=layer.material.mainTexture.width * scale && pos.y>=top && pos.y<=top+layer.material.mainTexture.height * scale)
                    {
                        selection.selectedTileEnd.id = (int)Mathf.Floor((pos.x / scale) /(layer.tileSize.x + layer.borderSize.x * 2f)) + (int)(Mathf.Floor((pos.y - top)/(scale * (layer.tileSize.y + layer.borderSize.y * 2f))) * tilesX);
                        selection.tilesPicked = false;
                        selection.selectedTilesWidth = tileXMax - tileXMin + 1;
                        selection.selectedTilesList = new TileInstance[(tileXMax - tileXMin + 1) * (tileYMax - tileYMin + 1)];
                        HandleUtility.Repaint ();
                        if(selection.selectedTile.id>=0)
                        {
                            for(int i = tileXMin; i<= tileXMax; i++)
                            {
                                for(int j = tileYMin; j<= tileYMax; j++)
                                {
                                    int x = i-tileXMin;
                                    int y = j-tileYMin;
                                    selection.selectedTilesList[x + y * selection.selectedTilesWidth] = new TileInstance(i + j * tilesX);
                                }
                            }

                        } else
                        {
                            selection.selectedTilesList[0]=new TileInstance(-1);
                        }
                        UniTileMarker.Instance.Init(selection);

                    }
                }
            }

            if(Event.current.type==EventType.MouseUp)
            {
                selection.tilesPicked = false;
                selection.selectedTilesWidth = tileXMax - tileXMin + 1;
                selection.selectedTilesList = new TileInstance[(tileXMax - tileXMin + 1) * (tileYMax - tileYMin + 1)];
                HandleUtility.Repaint ();
                if(selection.selectedTile.id>=0)
                {
                    for(int i = tileXMin; i<= tileXMax; i++)
                    {
                        for(int j = tileYMin; j<= tileYMax; j++)
                        {
                            int x = i-tileXMin;
                            int y = j-tileYMin;
                            selection.selectedTilesList[x + y * selection.selectedTilesWidth] = new TileInstance(i + j * tilesX);
                        }
                    }
                }
                else
                {
                    selection.selectedTilesList[0]=new TileInstance(-1);
                }
                UniTileMarker.Instance.Init(selection);
                this.selecting=false;
            }

        }
    }
示例#8
0
 public void Init(UniTileTemplate other)
 {
     other.CopyTo(this);
     this.name = "Template " + UniTileManager.instance.templateCount;
 }
示例#9
0
    public void OnSceneGUI(TileLayer layer, UniTileTemplate selection, TileLayerEditor editor)
    {
        DrawGrid(layer, selection.selectedTilesList, selection.selectedTilesWidth);

        MouseInfo(selection);

        GUI.Box(new Rect(10, 10, 170, 60), "");
        GUILayout.BeginArea(new Rect(20, 20, 150, 40));
        LayerSelect(layer);
        GUILayout.EndArea();

        if (justSwitchedLayers)
        {
            return;
        }

        Handles.BeginGUI();

        GUI.Box(new Rect(Screen.width - 160, Screen.height - 90, 160, 70), "");
        GUILayout.BeginArea(new Rect(Screen.width - 150, Screen.height - 80, 140, 50));
        CreateObjectButton(layer);
        TemplatePopup(layer, editor);
        GUILayout.EndArea();


        if (Event.current.keyCode == KeyCode.H && Event.current.type == EventType.KeyUp)
        {
            for (int i = 0; i < selection.selectedTilesList.Length; i++)
            {
                if (selection.selectedTilesList[i] != null)
                {
                    selection.selectedTilesList[i].flippedHorizontally = !selection.selectedTilesList[i].flippedHorizontally;
                }
            }
            TileInstance [] temp = new TileInstance[selection.selectedTilesList.Length];
            for (int i = 0; i < selection.selectedTilesWidth; i++)
            {
                for (int j = 0; j < selection.selectedTilesHeight; j++)
                {
                    int x1     = i;
                    int x2     = selection.selectedTilesWidth - x1 - 1;
                    int index1 = x1 + j * selection.selectedTilesWidth;
                    int index2 = x2 + j * selection.selectedTilesWidth;
                    temp[index2] = selection.selectedTilesList[index1];
                }
            }
            selection.selectedTilesList = temp;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        if (Event.current.keyCode == KeyCode.V && Event.current.type == EventType.KeyUp)
        {
            for (int i = 0; i < selection.selectedTilesList.Length; i++)
            {
                if (selection.selectedTilesList[i] != null)
                {
                    selection.selectedTilesList[i].flippedVertically = !selection.selectedTilesList[i].flippedVertically;
                }
            }

            TileInstance [] temp = new TileInstance[selection.selectedTilesList.Length];
            for (int i = 0; i < selection.selectedTilesWidth; i++)
            {
                for (int j = 0; j < selection.selectedTilesHeight; j++)
                {
                    int y1     = j;
                    int y2     = selection.selectedTilesHeight - y1 - 1;
                    int index1 = i + y1 * selection.selectedTilesWidth;
                    int index2 = i + y2 * selection.selectedTilesWidth;
                    temp[index2] = selection.selectedTilesList[index1];
                }
            }
            selection.selectedTilesList = temp;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        if (Event.current.keyCode == KeyCode.T && Event.current.type == EventType.KeyUp)
        {
            for (int i = 0; i < selection.selectedTilesList.Length; i++)
            {
                if (selection.selectedTilesList[i] != null)
                {
                    selection.selectedTilesList[i].rotation++;
                    if ((uint)selection.selectedTilesList[i].rotation > 3)
                    {
                        selection.selectedTilesList[i].rotation = TileInstance.Rotation.r0;
                    }
                }
            }


            TileInstance [] temp     = new TileInstance[selection.selectedTilesList.Length];
            int             newWidth = selection.selectedTilesHeight;

            for (int i = 0; i < selection.selectedTilesWidth; i++)
            {
                for (int j = 0; j < selection.selectedTilesHeight; j++)
                {
                    int x1     = i;
                    int y1     = j;
                    int index1 = x1 + y1 * selection.selectedTilesWidth;

                    int x2     = selection.selectedTilesHeight - y1 - 1;
                    int y2     = x1;
                    int index2 = x2 + y2 * newWidth;

                    temp[index2] = selection.selectedTilesList[index1];
                }
            }

            selection.selectedTilesList  = temp;
            selection.selectedTilesWidth = newWidth;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        Handles.EndGUI();
    }
    public void Init(UniTileTemplate template)
    {
        if (this.meshFilter.sharedMesh != null)
        {
            DestroyImmediate(this.meshFilter.sharedMesh);
        }

        UniTileManager manager = UniTileManager.instance;
        TileLayer      layer   = manager.activeLayer;

        if (layer.material == null || layer.material.mainTexture == null)
        {
            return;
        }

        if (layer.tileSpacing == new Vector2(-1, -1))
        {
            layer.tileSpacing = layer.tileSize;
        }

        this.meshRenderer.material = layer.material;

        Mesh m = new Mesh();

        int count = template.selectedTilesList.Length;

        Vector3 [] vertices  = new Vector3[4 * count];
        int []     triangles = new int[6 * count];
        Vector2 [] uv        = new Vector2[4 * count];

        for (int i = 0; i < count; i++)
        {
            int tileX = i % template.selectedTilesWidth;
            int tileY = (int)Mathf.Floor(i / template.selectedTilesWidth);
            int x     = (int)((tileX) * layer.tileSpacing.x);
            int y     = (int)((-tileY) * layer.tileSpacing.y);

            // Triangles
            triangles[(i * 6) + 0] = (i * 4) + 0;
            triangles[(i * 6) + 1] = (i * 4) + 1;
            triangles[(i * 6) + 2] = (i * 4) + 2;
            triangles[(i * 6) + 3] = (i * 4) + 1;
            triangles[(i * 6) + 4] = (i * 4) + 3;
            triangles[(i * 6) + 5] = (i * 4) + 2;

            if (template.selectedTilesList[i] != null && template.selectedTilesList[i].id != -1)
            {
                vertices[(i * 4) + 0] = new Vector3(0 + (int)x, layer.tileSpacing.y + (int)y, 0);
                vertices[(i * 4) + 1] = new Vector3(layer.tileSpacing.x + (int)x, layer.tileSpacing.y + (int)y, 0);
                vertices[(i * 4) + 2] = new Vector3(0 + (int)x, 0 + (int)y, 0);
                vertices[(i * 4) + 3] = new Vector3(layer.tileSpacing.x + (int)x, 0 + (int)y, 0);

                int   columns = (int)(layer.material.mainTexture.width / (layer.tileSize.x + layer.borderSize.x * 2f));
                float uvx     = Mathf.Floor((int)template.selectedTilesList[i].id % columns) * (layer.tileSize.x + layer.borderSize.x * 2f) + layer.borderSize.x;
                float uvy     = Mathf.Floor((int)template.selectedTilesList[i].id / columns) * (layer.tileSize.x + layer.borderSize.y * 2f) + layer.borderSize.y;
                float uvx2    = uvx + layer.tileSize.x;
                float uvy2    = uvy + layer.tileSize.y;

                int id0 = (i * 4) + 0;
                int id1 = (i * 4) + 1;
                int id2 = (i * 4) + 2;
                int id3 = (i * 4) + 3;

                if (template.selectedTilesList[i].flippedHorizontally)
                {
                    int temp = id0;
                    id0  = id1;
                    id1  = temp;
                    temp = id2;
                    id2  = id3;
                    id3  = temp;
                }

                if (template.selectedTilesList[i].flippedVertically)
                {
                    int temp = id0;
                    id0  = id2;
                    id2  = temp;
                    temp = id1;
                    id1  = id3;
                    id3  = temp;
                }

                for (int j = 0; j < (uint)template.selectedTilesList[i].rotation; j++)
                {
                    int temp = id0;
                    id0 = id1;
                    id1 = id3;
                    id3 = id2;
                    id2 = temp;
                }

                // UVS
                uv[id0] = new Vector2(uvx / (float)layer.material.mainTexture.width, 1f - uvy / (float)layer.material.mainTexture.height);                      //TL
                uv[id1] = new Vector2(uvx2 / (float)layer.material.mainTexture.width, 1f - uvy / (float)layer.material.mainTexture.height);                     //TR
                uv[id2] = new Vector2(uvx / (float)layer.material.mainTexture.width, 1f - uvy2 / (float)layer.material.mainTexture.height);                     //BL
                uv[id3] = new Vector2(uvx2 / (float)layer.material.mainTexture.width, 1f - uvy2 / (float)layer.material.mainTexture.height);                    //BR
            }
        }

        m.vertices  = vertices;
        m.triangles = triangles;
        m.uv        = uv;

        m.RecalculateNormals();

        this.meshFilter.mesh = m;
    }
 public void Init(UniTileTemplate other)
 {
     other.CopyTo(this);
     this.name = "Template "+UniTileManager.instance.templateCount;
 }
    public void OnInspectorGUI(TileLayer layer, UniTileTemplate selection, TileLayerEditor editor)
    {
        if (layer.material == null)
        {
            return;
        }
        useScrollView = EditorGUILayout.Toggle("Use scrollview", useScrollView);

        float w     = Mathf.Min(Screen.width, layer.material.mainTexture.width);     //Mathf.Min(rect.width,layer.material.mainTexture.width);
        float scale = w / layer.material.mainTexture.width;
        float h     = layer.material.mainTexture.height * scale;
        Rect  rect  = GUILayoutUtility.GetLastRect();
        float top   = rect.yMax;

        int tilesX = (int)Mathf.Floor((float)layer.material.mainTexture.width / (layer.tileSize.x + layer.borderSize.x * 2f));

        int tileX1 = (int)Mathf.Floor((float)selection.selectedTile.id % (float)tilesX);
        int tileY1 = (int)Mathf.Floor((float)selection.selectedTile.id / (float)tilesX);

        int tileX2 = (int)Mathf.Floor((float)selection.selectedTileEnd.id % (float)tilesX);
        int tileY2 = (int)Mathf.Floor((float)selection.selectedTileEnd.id / (float)tilesX);

        int tileXMin = Mathf.Min(tileX1, tileX2);
        int tileYMin = Mathf.Min(tileY1, tileY2);
        int tileXMax = Mathf.Max(tileX1, tileX2);
        int tileYMax = Mathf.Max(tileY1, tileY2);

        int tileWidth  = tileXMax - tileXMin + 1;
        int tileHeight = tileYMax - tileYMin + 1;

        float tempTop = top;

        if (useScrollView)
        {
            scale = 1;
            w     = layer.material.mainTexture.width;
            h     = layer.material.mainTexture.height;

            float scrollWidth  = Screen.width - 25;
            float scrollHeight = h + 10;
            scrollPos = EditorGUILayout.BeginScrollView(scrollPos, false, false, GUILayout.Height(scrollHeight), GUILayout.Width(scrollWidth));
            GUILayout.Label("", GUILayout.Width(scrollWidth), GUILayout.Height(scrollHeight));
            tempTop = 0;
        }
        else
        {
            GUILayout.Label("", GUILayout.Width(w), GUILayout.Height(h));
        }

        GUI.DrawTexture(new Rect(0, tempTop, w, h), layer.material.mainTexture);

        if (selection.selectedTile.id >= 0 && !selection.tilesPicked)
        {
            GUIStyle style = new GUIStyle(GUI.skin.customStyles[0]);
            style.normal.background = SelectionTexture;
            GUI.Box(
                new Rect(
                    (tileXMin * (layer.tileSize.x + layer.borderSize.x * 2f) + layer.borderSize.x) * scale,
                    tempTop + (tileYMin * (layer.tileSize.y + layer.borderSize.y * 2f) + layer.borderSize.y) * scale,
                    (tileWidth * (layer.tileSize.x + layer.borderSize.x * 2f) - layer.borderSize.x * 2f) * scale,
                    (tileHeight * (layer.tileSize.y + layer.borderSize.y * 2f) - layer.borderSize.y * 2f) * scale
                    ),
                "",
                style
                );
        }

        if (useScrollView)
        {
            EditorGUILayout.EndScrollView();
        }

        if (Event.current != null)
        {
            if (Event.current.type == EventType.MouseDown)
            {
                if (Event.current.button == 0)
                {
                    Vector2 pos = Event.current.mousePosition;
                    if (useScrollView)
                    {
                        pos += scrollPos;
                    }
                    this.selecting        = true;
                    selection.tilesPicked = false;

                    if (pos.x >= 0 && pos.x <= layer.material.mainTexture.width * scale && pos.y >= top && pos.y <= top + layer.material.mainTexture.height * scale)
                    {
                        editor.selectedTemplate   = -1;
                        selection.selectedTile.id = selection.selectedTileEnd.id = (int)Mathf.Floor((pos.x / scale) / (layer.tileSize.x + layer.borderSize.x * 2f)) + (int)(Mathf.Floor((pos.y - top) / (scale * (layer.tileSize.y + layer.borderSize.y * 2f))) * tilesX);
                        if (Event.current.clickCount == 2)
                        {
                            TileEditorWindow window = TileEditorWindow.CreateInstance <TileEditorWindow>();
                            window.Setup(editor, selection.selectedTilesList);
                            window.ShowUtility();
                        }
                    }
                }
                else
                {
                    selection.tilesPicked        = false;
                    selection.selectedTile.id    = -1;
                    selection.selectedTileEnd.id = -1;
                }
            }

            if (Event.current.type == EventType.mouseDrag)
            {
                if (selecting)
                {
                    Vector2 pos = Event.current.mousePosition;
                    if (useScrollView)
                    {
                        pos += scrollPos;
                    }
                    if (pos.x >= 0 && pos.x <= layer.material.mainTexture.width * scale && pos.y >= top && pos.y <= top + layer.material.mainTexture.height * scale)
                    {
                        selection.selectedTileEnd.id = (int)Mathf.Floor((pos.x / scale) / (layer.tileSize.x + layer.borderSize.x * 2f)) + (int)(Mathf.Floor((pos.y - top) / (scale * (layer.tileSize.y + layer.borderSize.y * 2f))) * tilesX);
                        selection.tilesPicked        = false;
                        selection.selectedTilesWidth = tileXMax - tileXMin + 1;
                        selection.selectedTilesList  = new TileInstance[(tileXMax - tileXMin + 1) * (tileYMax - tileYMin + 1)];
                        HandleUtility.Repaint();
                        if (selection.selectedTile.id >= 0)
                        {
                            for (int i = tileXMin; i <= tileXMax; i++)
                            {
                                for (int j = tileYMin; j <= tileYMax; j++)
                                {
                                    int x = i - tileXMin;
                                    int y = j - tileYMin;
                                    selection.selectedTilesList[x + y * selection.selectedTilesWidth] = new TileInstance(i + j * tilesX);
                                }
                            }
                        }
                        else
                        {
                            selection.selectedTilesList[0] = new TileInstance(-1);
                        }
                        UniTileMarker.Instance.Init(selection);
                    }
                }
            }

            if (Event.current.type == EventType.MouseUp)
            {
                selection.tilesPicked        = false;
                selection.selectedTilesWidth = tileXMax - tileXMin + 1;
                selection.selectedTilesList  = new TileInstance[(tileXMax - tileXMin + 1) * (tileYMax - tileYMin + 1)];
                HandleUtility.Repaint();
                if (selection.selectedTile.id >= 0)
                {
                    for (int i = tileXMin; i <= tileXMax; i++)
                    {
                        for (int j = tileYMin; j <= tileYMax; j++)
                        {
                            int x = i - tileXMin;
                            int y = j - tileYMin;
                            selection.selectedTilesList[x + y * selection.selectedTilesWidth] = new TileInstance(i + j * tilesX);
                        }
                    }
                }
                else
                {
                    selection.selectedTilesList[0] = new TileInstance(-1);
                }
                UniTileMarker.Instance.Init(selection);
                this.selecting = false;
            }
        }
    }
    private static void MouseInfo(UniTileTemplate selection)
    {
        Handles.BeginGUI();
        GUI.Box(new Rect (0, Screen.height - 218, 90, 198), "");
        GUILayout.BeginArea(new Rect(5, Screen.height-208, 75,188));
        GUILayout.Label(string.Format("{0}, {1}", (int)TileLayerEditor.gridMousePosition.x, (int)TileLayerEditor.gridMousePosition.y), "button", GUILayout.ExpandWidth(true));

        GUILayout.Label(string.Format("{0} x {1}", selection.selectedTilesWidth, selection.selectedTilesList.Length/selection.selectedTilesWidth), "button", GUILayout.ExpandWidth(true));

        GUILayout.Toggle(Event.current.alt, "alt: pick", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(Event.current.control, Event.current.alt ? "ctrl: cut":"ctrl: erase", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(Event.current.shift, "shift: axis", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "t: turn tile", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "h: flip hor.", "button", GUILayout.ExpandWidth(true));
        GUILayout.Toggle(false, "v: flip vert.", "button", GUILayout.ExpandWidth(true));

        GUILayout.EndArea();
    }
    public void OnSceneGUI(TileLayer layer, UniTileTemplate selection, TileLayerEditor editor)
    {
        DrawGrid(layer, selection.selectedTilesList, selection.selectedTilesWidth);

        MouseInfo(selection);

        GUI.Box(new Rect (10, 10, 170, 60), "");
        GUILayout.BeginArea(new Rect (20, 20, 150, 40));
        LayerSelect(layer);
        GUILayout.EndArea();

        if(justSwitchedLayers)
            return;

        Handles.BeginGUI();

        GUI.Box(new Rect (Screen.width - 160, Screen.height - 90, 160, 70), "");
        GUILayout.BeginArea(new Rect (Screen.width - 150, Screen.height - 80, 140, 50));
        CreateObjectButton(layer);
        TemplatePopup(layer,editor);
        GUILayout.EndArea();

        if(Event.current.keyCode == KeyCode.H && Event.current.type == EventType.KeyUp) {
            for(int i = 0; i < selection.selectedTilesList.Length; i++) {
                if(selection.selectedTilesList[i] != null) {
                    selection.selectedTilesList[i].flippedHorizontally = !selection.selectedTilesList[i].flippedHorizontally;
                }
            }
            TileInstance [] temp = new TileInstance[selection.selectedTilesList.Length];
            for(int i = 0; i < selection.selectedTilesWidth; i++) {
                for(int j = 0; j < selection.selectedTilesHeight; j++) {
                    int x1 = i;
                    int x2 = selection.selectedTilesWidth - x1 - 1;
                    int index1 = x1 + j * selection.selectedTilesWidth;
                    int index2 = x2 + j * selection.selectedTilesWidth;
                    temp[index2] = selection.selectedTilesList[index1];
                }
            }
            selection.selectedTilesList = temp;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        if(Event.current.keyCode == KeyCode.V && Event.current.type == EventType.KeyUp) {
            for(int i = 0; i < selection.selectedTilesList.Length; i++) {
                if(selection.selectedTilesList[i] != null) {
                    selection.selectedTilesList[i].flippedVertically = !selection.selectedTilesList[i].flippedVertically;
                }
            }

            TileInstance [] temp = new TileInstance[selection.selectedTilesList.Length];
            for(int i = 0; i < selection.selectedTilesWidth; i++) {
                for(int j = 0; j < selection.selectedTilesHeight; j++) {
                    int y1 = j;
                    int y2 = selection.selectedTilesHeight - y1 - 1;
                    int index1 = i + y1 * selection.selectedTilesWidth;
                    int index2 = i + y2 * selection.selectedTilesWidth;
                    temp[index2] = selection.selectedTilesList[index1];
                }
            }
            selection.selectedTilesList = temp;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        if(Event.current.keyCode == KeyCode.T && Event.current.type == EventType.KeyUp) {
            for(int i = 0; i < selection.selectedTilesList.Length; i++) {
                if(selection.selectedTilesList[i] != null) {
                    selection.selectedTilesList[i].rotation++;
                    if((uint)selection.selectedTilesList[i].rotation > 3) selection.selectedTilesList[i].rotation = TileInstance.Rotation.r0;
                }
            }

            TileInstance [] temp = new TileInstance[selection.selectedTilesList.Length];
            int newWidth = selection.selectedTilesHeight;

            for(int i = 0; i < selection.selectedTilesWidth; i++) {
                for(int j = 0; j < selection.selectedTilesHeight; j++) {
                    int x1 = i;
                    int y1 = j;
                    int index1 = x1 + y1 * selection.selectedTilesWidth;

                    int x2 = selection.selectedTilesHeight - y1 - 1;
                    int y2 = x1;
                    int index2 = x2 + y2 * newWidth;

                    temp[index2] = selection.selectedTilesList[index1];
                }
            }

            selection.selectedTilesList = temp;
            selection.selectedTilesWidth = newWidth;

            UniTileMarker.Instance.Init(selection);
            editor.selectedTemplate = -1;
        }

        Handles.EndGUI();
    }
示例#15
0
    private void OnInspectorGUI_Properties(TileLayer layer)
    {
        EditorGUILayout.BeginVertical();

        showProperties = EditorGUILayout.Foldout(showProperties, "Layer Properties");

        if (layer.material != null && layer.tileset == null)
        {
            this.GetTileset(layer);
        }

        if (showProperties)
        {
            propertiesEditor.OnInspectorGUI(layer);

            if (layer.material != null)
            {
                GUILayout.BeginHorizontal();

                int length = 2 + layer.tileset.templates.Length;

                string[] templates = new string[length];
                templates[0] = "Choose a template";
                for (int i = 0; i < layer.tileset.templates.Length; i++)
                {
                    templates[i + 1] = i.ToString() + " - " + layer.tileset.templates[i].name;
                }

                templates[length - 1] = "Add current selection as a template";
                int newSelectedTemplate = EditorGUILayout.Popup("Template", Mathf.Min(selectedTemplate + 1, length - 2), templates);
                selectedTemplate = newSelectedTemplate - 1;
                if (newSelectedTemplate == length - 1)
                {
                    UniTileTemplate[] newList = new UniTileTemplate[layer.tileset.templates.Length + 1];
                    for (int i = 0; i < layer.tileset.templates.Length; i++)
                    {
                        newList[i] = layer.tileset.templates[i];
                    }
                    newList[newList.Length - 1] = new UniTileTemplate();
                    newList[newList.Length - 1].Init(selection);
                    layer.tileset.templates = newList;
                }
                else if (newSelectedTemplate > 0)
                {
                    //layer.tileset.templates[selection - 1].Use();
                    SelectTemplate(layer.tileset.templates[newSelectedTemplate - 1]);
                }

                if (selectedTemplate >= 0 && selectedTemplate < layer.tileset.templates.Length && layer.tileset.templates[selectedTemplate] != null)
                {
                    layer.tileset.templates[selectedTemplate].name = EditorGUILayout.TextField(layer.tileset.templates[selectedTemplate].name);
                    if (GUILayout.Button("Delete"))
                    {
                        List <UniTileTemplate> newList = new List <UniTileTemplate>();
                        foreach (UniTileTemplate t in layer.tileset.templates)
                        {
                            if (t != layer.tileset.templates[selectedTemplate])
                            {
                                newList.Add(t);
                            }
                        }
                        layer.tileset.templates = newList.ToArray();
                    }
                }
                GUILayout.EndHorizontal();

                UniTileMarker.Instance.Init(this.selection);
            }

            m_Object.Update();
            EditorGUILayout.PropertyField(material);
            if (layer.material != material.objectReferenceValue)
            {
                layer.material = (Material)material.objectReferenceValue;
                m_Object.ApplyModifiedProperties();
                this.GetTileset(layer);
            }
        }

        EditorGUILayout.EndVertical();

        if (showProperties)
        {
            int x = 5;
            GUILayout.BeginHorizontal();

            if (layer.parent == null)
            {
                if (GUILayout.Button(new GUIContent("Rebuild map", "Apply changes to the size properties."), GUILayout.Width(80), GUILayout.Height(30)))
                {
                    //Undo.RegisterSceneUndo("Resize map");
                    Vector2 prevLayerSize = layer.layerSize;
                    propertiesEditor.SetPropertiesTo(layer);

                    for (int i = 0; i < layer.transform.childCount; i++)
                    {
                        TileLayer child = layer.transform.GetChild(i).GetComponent <TileLayer>();
                        if (child != null)
                        {
                            propertiesEditor.SetPropertiesTo(child);
                        }
                    }

                    this.RebuildMap(prevLayerSize);

                    UniTileMarker.Instance.Init(selection);
                }

                x += 80;

                if (GUILayout.Button(new GUIContent("Revert", "Cancel changes to the size properties."), GUILayout.Width(50), GUILayout.Height(30)))
                {
                    propertiesEditor.SetPropertiesFrom(layer);
                }

                x += 50;
            }

            if (GUILayout.Button(new GUIContent("Clear layer", "Erase all tile data from the layer."), GUILayout.Width(70), GUILayout.Height(30)))
            {
                //Undo.RegisterSceneUndo("Clear layer");
                this.ClearMap();
            }


            x += 70;

            if (GUILayout.Button(new GUIContent("Create objects", "Instantiate tile prefabs and box colliders."), GUILayout.Width(90), GUILayout.Height(30)))
            {
                //Undo.RegisterSceneUndo("Clear layer");
                TileLayerEditor.InstantiatePrefabs(layer);
            }


            x += 90;

            if (layer.parent == null && layer.material != null && layer.material.mainTexture != null)
            {
                if (GUILayout.Button(new GUIContent("Padder", "Add padding to textures."), GUILayout.Width(50), GUILayout.Height(30)))
                {
                    PadderEditorWindow window = PadderEditorWindow.CreateInstance <PadderEditorWindow>();
                    window.Setup((Texture2D)layer.material.mainTexture, layer.tileSize, layer.borderSize);
                    window.ShowUtility();
                }
            }

            x += 50;

            if (selection.selectedTilesList.Length > 0 && layer.material != null && layer.material.mainTexture != null)
            {
                if (GUILayout.Button(new GUIContent("Edit Tiles", "Edit currently selected tiles."), GUILayout.Width(70), GUILayout.Height(30)))
                {
                    TileEditorWindow window = TileEditorWindow.CreateInstance <TileEditorWindow>();
                    window.Setup(this, selection.selectedTilesList);
                    window.ShowUtility();
                }
            }

            GUILayout.EndHorizontal();
        }
    }