public static ReorderableList CreateBrushReorderableList(Tileset tileset)
        {
            ReorderableList brushRList = new ReorderableList(tileset.Brushes, typeof(Tileset.BrushContainer), true, true, true, true);

            brushRList.displayAdd         = brushRList.displayRemove = false;
            brushRList.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, "Brushes", EditorStyles.boldLabel);
            };
            brushRList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                Tileset.BrushContainer brushContainer = tileset.Brushes[index];
                Rect rTile  = rect; rTile.width = rTile.height = tileset.VisualTileSize.y;
                Rect tileUV = brushContainer.BrushAsset.GetAnimUV();
                if (tileUV != default(Rect))
                {
                    GUI.Box(new Rect(rTile.position - Vector2.one, rTile.size + 2 * Vector2.one), "");
                    GUI.DrawTextureWithTexCoords(rTile, tileset.AtlasTexture, tileUV, true);
                }

                Rect rTileId = rect;
                rTileId.x     += rTile.width + 20; rTileId.width -= rTile.width + 20;
                rTileId.height = rect.height / 2;
                GUI.Label(rTileId, "Id(" + brushContainer.Id + ")");
            };

            return(brushRList);
        }
        public static ReorderableList CreateBrushReorderableList(Tileset tileset)
        {
            ReorderableList brushRList = new ReorderableList(tileset.Brushes, typeof(Tileset.BrushContainer), true, true, true, true);

            brushRList.displayAdd         = brushRList.displayRemove = false;
            brushRList.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, "Brushes", EditorStyles.boldLabel);
            };
            brushRList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                Tileset.BrushContainer brushContainer = tileset.Brushes[index];
                Rect rToggle = new Rect(rect.x, rect.y, 16f, EditorGUIUtility.singleLineHeight);
                Rect rTile   = new Rect(rect.x + 16f, rect.y, tileset.VisualTileSize.x, tileset.VisualTileSize.y);
                Rect rTileId = rTile;
                rTileId.x     += rTile.width + 20; rTileId.width = rect.width - rTileId.x;
                rTileId.height = rect.height / 2;

                Rect tileUV = brushContainer.BrushAsset.GetAnimUV();
                if (tileUV != default(Rect))
                {
                    GUI.Box(new Rect(rTile.position - Vector2.one, rTile.size + 2 * Vector2.one), "");
                    GUI.DrawTextureWithTexCoords(rTile, tileset.AtlasTexture, tileUV, true);
                }

                brushContainer.BrushAsset.ShowInPalette = EditorGUI.Toggle(rToggle, GUIContent.none, brushContainer.BrushAsset.ShowInPalette, STEditorStyles.Instance.visibleToggleStyle);
                GUI.Label(rTileId, "Id(" + brushContainer.Id + ") " + brushContainer.BrushAsset.name);
            };

            return(brushRList);
        }
示例#3
0
        private void DisplayBrushReorderableList()
        {
            Event e = Event.current;

            if (m_brushList == null || m_brushList.list != Tileset.Brushes)
            {
                if (e.type != EventType.Layout)
                {
                    m_brushList = TilesetEditor.CreateBrushReorderableList(Tileset);
                    m_brushList.onSelectCallback += (ReorderableList list) =>
                    {
                        Tileset.BrushContainer brushCont = Tileset.Brushes[list.index];
                        Tileset.SelectedBrushId = brushCont.Id;
                        RemoveTileSelection();
                        if (m_dblClick.IsDblClick)
                        {
                            EditorGUIUtility.PingObject(brushCont.BrushAsset);
                            m_selectBrushInInspector = brushCont.BrushAsset;
                        }
                    };
                }
            }
            else
            {
                GUILayout.BeginVertical(m_customBox);
                m_brushList.index         = Tileset.Brushes.FindIndex(x => x.Id == Tileset.SelectedBrushId);
                m_brushList.index         = Mathf.Clamp(m_brushList.index, -1, Tileset.Brushes.Count - 1);
                m_brushList.elementHeight = Tileset.VisualTileSize.y + 10f;
                m_brushList.DoLayoutList();
                Rect rList = GUILayoutUtility.GetLastRect();
                if (e.isMouse && !rList.Contains(e.mousePosition))
                {
                    m_brushList.ReleaseKeyboardFocus();
                }
                GUILayout.EndVertical();
            }
        }
示例#4
0
        public void Display()
        {
            Event e = Event.current;

            m_dblClick.Update();

            // This way a gui exception is avoided
            if (e.type == EventType.Layout && m_selectBrushInInspector != null)
            {
                Selection.activeObject   = m_selectBrushInInspector;
                m_selectBrushInInspector = null;
            }

            if (m_lastTime == 0f)
            {
                m_lastTime = Time.realtimeSinceStartup;
            }
            m_timeDt   = Time.realtimeSinceStartup - m_lastTime;
            m_lastTime = Time.realtimeSinceStartup;

            if (Tileset == null)
            {
                EditorGUILayout.HelpBox("There is no tileset selected", MessageType.Info);
                return;
            }
            else if (Tileset.AtlasTexture == null)
            {
                EditorGUILayout.HelpBox("There is no atlas texture set", MessageType.Info);
                return;
            }
            else if (Tileset.Tiles.Count == 0)
            {
                EditorGUILayout.HelpBox("There are no tiles to show in the current tileset", MessageType.Info);
                return;
            }

            if (m_scrollStyle == null)
            {
                m_scrollStyle = new GUIStyle("ScrollView");
            }

            if (m_customBox == null)
            {
                m_customBox = new GUIStyle("Box");
            }

            float visualTilePadding       = 1;
            bool  isLeftMouseReleased     = e.type == EventType.MouseUp && e.button == 0;
            bool  isRightMouseReleased    = e.type == EventType.MouseUp && e.button == 1;
            bool  isInsideTileScrollArea  = e.isMouse && m_rTileScrollArea.Contains(e.mousePosition);
            bool  isInsideBrushScrollArea = e.isMouse && m_rBrushScrollArea.Contains(e.mousePosition);

            // TileViews
            if (m_tileViewList == null || m_tileViewList.list != Tileset.TileViews)
            {
                if (e.type != EventType.Layout)
                {
                    m_tileViewList = TilesetEditor.CreateTileViewReorderableList(Tileset);
                    m_tileViewList.onSelectCallback += (ReorderableList list) =>
                    {
                        m_viewMode = eViewMode.TileView;
                        RemoveTileSelection();
                    };
                    m_tileViewList.onRemoveCallback += (ReorderableList list) =>
                    {
                        RemoveTileSelection();
                    };
                }
            }
            else
            {
                GUI.color = Color.cyan;
                GUILayout.BeginVertical(m_customBox);
                m_tileViewList.index = Mathf.Clamp(m_tileViewList.index, -1, Tileset.TileViews.Count - 1);
                m_tileViewList.DoLayoutList();
                Rect rList = GUILayoutUtility.GetLastRect();
                if (e.isMouse && !rList.Contains(e.mousePosition))
                {
                    m_tileViewList.ReleaseKeyboardFocus();
                }
                GUILayout.EndVertical();
                GUI.color = Color.white;
            }
            TileView tileView = m_tileViewList != null && m_tileViewList.index >= 0 ? Tileset.TileViews[m_tileViewList.index] : null;

            if (m_viewMode == eViewMode.Tileset)
            {
                Tileset.TileRowLength = Mathf.Clamp(EditorGUILayout.IntField("TileRowLength", Tileset.TileRowLength), 1, Tileset.Width);
            }

            m_viewMode = (eViewMode)EditorGUILayout.EnumPopup("View Mode", m_viewMode);
            if (tileView == null)
            {
                m_viewMode = eViewMode.Tileset;
            }
            if (m_viewMode != m_prevViewMode)
            {
                m_prevViewMode = m_viewMode;
                RemoveTileSelection();
            }

            // Draw Background Color Selector
            Tileset.BackgroundColor = EditorGUILayout.ColorField("Background Color", Tileset.BackgroundColor);
            if (m_prevBgColor != Tileset.BackgroundColor || m_scrollStyle.normal.background == null)
            {
                m_prevBgColor = Tileset.BackgroundColor;
                if (m_scrollStyle.normal.background == null)
                {
                    m_scrollStyle.normal.background = new Texture2D(1, 1)
                    {
                        hideFlags = HideFlags.DontSave
                    }
                }
                ;
                m_scrollStyle.normal.background.SetPixel(0, 0, Tileset.BackgroundColor);
                m_scrollStyle.normal.background.Apply();
            }
            //---

            string sTileIdLabel = Tileset.SelectedTileId != Tileset.k_TileId_Empty? " (id:" + Tileset.SelectedTileId + ")" : "";

            EditorGUILayout.LabelField("Tile Palette" + sTileIdLabel, EditorStyles.boldLabel);

            // keeps values safe
            m_tileViewRowLength = Mathf.Max(1, m_tileViewRowLength);

            float tileAreaWidth  = m_tileViewRowLength * (Tileset.VisualTileSize.x + visualTilePadding) + 4f;
            float tileAreaHeight = (Tileset.VisualTileSize.y + visualTilePadding) * (1 + (m_visibleTileCount - 1) / m_tileViewRowLength) + 4f;

            m_tileViewRowLength = m_viewMode == eViewMode.TileView && tileView != null ? tileView.tileSelection.rowLength : Tileset.TileRowLength;

            m_tilesScrollPos = EditorGUILayout.BeginScrollView(m_tilesScrollPos, m_scrollStyle);
            {
                // Scroll Moving Drag
                if (e.type == EventType.MouseDrag && (e.button == 1 || e.button == 2))
                {
                    m_tilesScrollPos -= e.delta;
                }
                else
                {
                    DoAutoScroll();
                }

                if (e.isMouse)
                {
                    m_lastTileScrollMousePos = e.mousePosition;
                }
                if (Tileset.Tiles != null)
                {
                    GUILayoutUtility.GetRect(tileAreaWidth, tileAreaHeight);
                    m_visibleTileCount = 0;
                    List <uint> visibleTileList = new List <uint>();
                    int         tileViewWidth   = m_viewMode == eViewMode.Tileset ? Tileset.Width : tileView.tileSelection.rowLength;
                    int         tileViewHeight  = m_viewMode == eViewMode.Tileset ? Tileset.Height : ((tileView.tileSelection.selectionData.Count - 1) / tileView.tileSelection.rowLength) + 1;
                    int         totalCount      = ((((tileViewWidth - 1) / m_tileViewRowLength) + 1) * m_tileViewRowLength) * tileViewHeight;
                    for (int i = 0; i < totalCount; ++i)
                    {
                        int  tileId   = GetTileIdFromIdx(i, m_tileViewRowLength, tileViewWidth, tileViewHeight);
                        uint tileData = (uint)tileId;
                        if (m_viewMode == eViewMode.TileView && tileId != Tileset.k_TileId_Empty)
                        {
                            tileData = tileView.tileSelection.selectionData[tileId];
                            tileId   = (int)(tileData & Tileset.k_TileDataMask_TileId);
                        }
                        Tile tile = tileId != Tileset.k_TileId_Empty && tileId < Tileset.Tiles.Count ? Tileset.Tiles[tileId] : null;
                        visibleTileList.Add(tileData);

                        int  tx          = m_visibleTileCount % m_tileViewRowLength;
                        int  ty          = m_visibleTileCount / m_tileViewRowLength;
                        Rect rVisualTile = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), Tileset.VisualTileSize.x, Tileset.VisualTileSize.y);

                        // Optimization, skipping not visible tiles
                        Rect rLocalVisualTile = rVisualTile; rLocalVisualTile.position -= m_tilesScrollPos;
                        if (!rLocalVisualTile.Overlaps(m_rTileScrollSize))
                        {
                            ; // Do Nothing
                        }
                        else
                        //---
                        {
                            // Draw Tile
                            if (tile == null)
                            {
                                HandlesEx.DrawRectWithOutline(rVisualTile, new Color(0f, 0f, 0f, 0.2f), new Color(0f, 0f, 0f, 0.2f));
                            }
                            else
                            {
                                HandlesEx.DrawRectWithOutline(rVisualTile, new Color(0f, 0f, 0f, 0.1f), new Color(0f, 0f, 0f, 0.1f));
                                TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset);
                            }

                            Rect rTileRect = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), (Tileset.VisualTileSize.x + visualTilePadding), (Tileset.VisualTileSize.y + visualTilePadding));
                            if (rVisualTile.Contains(e.mousePosition))
                            {
                                if (e.type == EventType.MouseDrag && e.button == 0)
                                {
                                    m_pointedTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                }
                                else if (e.type == EventType.MouseDown && e.button == 0)
                                {
                                    m_startDragTileIdxRect = m_pointedTileIdxRect = m_endDragTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                }
                                else if (e.type == EventType.MouseUp && e.button == 0)
                                {
                                    m_endDragTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                    DoSetTileSelection();
                                }
                            }

                            if ((isLeftMouseReleased || isRightMouseReleased) && isInsideTileScrollArea && rVisualTile.Contains(e.mousePosition) &&
                                (m_startDragTileIdxRect.Key == m_endDragTileIdxRect.Key) && // and there is not dragging selection
                                m_rTileScrollSize.Contains(e.mousePosition - m_tilesScrollPos))   // and it's inside the scroll area
                            {
                                Tileset.SelectedTileId = tileId;

                                //Give focus to SceneView to get key events
                                FocusSceneView();

                                if (isRightMouseReleased)
                                {
                                    TilePropertiesWindow.Show(Tileset);
                                }
                            }
                            else if (tile != null && Tileset.SelectedTileId == tileId)
                            {
                                HandlesEx.DrawRectWithOutline(rTileRect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f));
                            }
                        }

                        ++m_visibleTileCount;
                    }
                    m_visibleTileList = visibleTileList;

                    // Draw selection rect
                    if (m_startDragTileIdxRect.Key != m_pointedTileIdxRect.Key /*&& m_startDragTileIdxRect.Key == m_endDragTileIdxRect.Key*/)
                    {
                        Rect rSelection = new Rect(m_startDragTileIdxRect.Value.center, m_pointedTileIdxRect.Value.center - m_startDragTileIdxRect.Value.center);
                        rSelection.Set(Mathf.Min(rSelection.xMin, rSelection.xMax), Mathf.Min(rSelection.yMin, rSelection.yMax), Mathf.Abs(rSelection.width), Mathf.Abs(rSelection.height));
                        rSelection.xMin -= m_startDragTileIdxRect.Value.width / 2;
                        rSelection.xMax += m_startDragTileIdxRect.Value.width / 2;
                        rSelection.yMin -= m_startDragTileIdxRect.Value.height / 2;
                        rSelection.yMax += m_startDragTileIdxRect.Value.height / 2;
                        HandlesEx.DrawRectWithOutline(rSelection, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                    }
                }
            }
            EditorGUILayout.EndScrollView();
            if (e.type == EventType.Repaint)
            {
                m_rTileScrollArea          = GUILayoutUtility.GetLastRect();
                m_rTileScrollSize          = m_rTileScrollArea;
                m_rTileScrollSize.position = Vector2.zero; // reset position to the Contains and Overlaps inside the tile scroll view without repositioning the position of local positions
                if (tileAreaWidth > m_rTileScrollSize.width)
                {
                    m_rTileScrollSize.height -= GUI.skin.verticalScrollbar.fixedWidth;
                }
                if (tileAreaHeight > m_rTileScrollSize.height)
                {
                    m_rTileScrollSize.width -= GUI.skin.verticalScrollbar.fixedWidth;
                }
            }

            EditorGUILayout.BeginHorizontal();
            string sBrushIdLabel = Tileset.SelectedBrushId > 0 ? " (id:" + Tileset.SelectedBrushId + ")" : "";

            EditorGUILayout.LabelField("Brush Palette" + sBrushIdLabel, EditorStyles.boldLabel);
            m_displayBrushReordList = EditorUtils.DoToggleButton("Display List", m_displayBrushReordList);
            EditorGUILayout.EndHorizontal();

            int tileRowLength = (int)(m_rTileScrollSize.width / (Tileset.VisualTileSize.x + visualTilePadding));

            if (tileRowLength <= 0)
            {
                tileRowLength = 1;
            }
            float fBrushesScrollMaxHeight = Screen.height / 4;
            //commented because m_rTileScrollSize.width.height was changed to Screen.height;  fBrushesScrollMaxHeight -= fBrushesScrollMaxHeight % 2; // sometimes because size of tile scroll affects size of brush scroll, the height is dancing between +-1, so this is always taking the pair value
            float fBrushesScrollHeight = Mathf.Min(fBrushesScrollMaxHeight, 4 + (Tileset.VisualTileSize.y + visualTilePadding) * (1 + (Tileset.Brushes.Count / tileRowLength)));

            EditorGUILayout.BeginVertical(GUILayout.MinHeight(fBrushesScrollHeight));
            if (m_displayBrushReordList)
            {
                DisplayBrushReorderableList();
            }
            else
            {
                bool isRefreshBrushes = false;
                m_brushesScrollPos = EditorGUILayout.BeginScrollView(m_brushesScrollPos, m_scrollStyle);
                {
                    Rect rScrollView = new Rect(0, 0, m_rTileScrollSize.width, 0);
                    tileRowLength = Mathf.Clamp((int)rScrollView.width / (int)(Tileset.VisualTileSize.x + visualTilePadding), 1, tileRowLength);
                    if (Tileset.Brushes != null)
                    {
                        GUILayout.Space((Tileset.VisualTileSize.y + visualTilePadding) * (1 + (Tileset.Brushes.Count - 1) / tileRowLength));
                        for (int i = 0; i < Tileset.Brushes.Count; ++i)
                        {
                            Tileset.BrushContainer brushCont = Tileset.Brushes[i];
                            if (brushCont.BrushAsset == null)
                            {
                                isRefreshBrushes = true;
                                continue;
                            }

                            int  tx          = i % tileRowLength;
                            int  ty          = i / tileRowLength;
                            Rect rVisualTile = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), Tileset.VisualTileSize.x, Tileset.VisualTileSize.y);
                            //Fix Missing Tileset reference
                            if (brushCont.BrushAsset.Tileset == null)
                            {
                                Debug.LogWarning("Fixed missing tileset reference in brush " + brushCont.BrushAsset.name + " Id(" + brushCont.Id + ")");
                                brushCont.BrushAsset.Tileset = Tileset;
                            }
                            uint tileData = Tileset.k_TileData_Empty;
                            if (brushCont.BrushAsset.IsAnimated())
                            {
                                tileData = brushCont.BrushAsset.GetAnimTileData();
                            }
                            else
                            {
                                tileData = brushCont.BrushAsset.PreviewTileData();
                            }
                            TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset, brushCont.BrushAsset.GetAnimUV());
                            if ((isLeftMouseReleased || isRightMouseReleased || m_dblClick.IsDblClick) && isInsideBrushScrollArea && rVisualTile.Contains(Event.current.mousePosition))
                            {
                                Tileset.SelectedBrushId = brushCont.Id;
                                RemoveTileSelection();
                                if (m_dblClick.IsDblClick)
                                {
                                    EditorGUIUtility.PingObject(brushCont.BrushAsset);
                                    m_selectBrushInInspector = brushCont.BrushAsset;
                                }
                                if (isRightMouseReleased)
                                {
                                    TilePropertiesWindow.Show(Tileset);
                                }
                            }
                            else if (Tileset.SelectedBrushId == brushCont.Id)
                            {
                                Rect rSelection = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), (Tileset.VisualTileSize.x + visualTilePadding), (Tileset.VisualTileSize.y + visualTilePadding));
                                HandlesEx.DrawRectWithOutline(rSelection, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f));
                            }
                        }
                    }

                    if (isRefreshBrushes)
                    {
                        Tileset.RemoveNullBrushes();
                    }
                }
                EditorGUILayout.EndScrollView();
                if (e.type == EventType.Repaint)
                {
                    m_rBrushScrollArea = GUILayoutUtility.GetLastRect();
                }
            }
            EditorGUILayout.EndVertical();

            if (GUILayout.Button("Import all brushes found"))
            {
                TilesetEditor.AddAllBrushesFoundInTheProject(Tileset);
                EditorUtility.SetDirty(Tileset);
            }
        }
示例#5
0
        public void Display()
        {
            AssetPreview.SetPreviewTextureCacheSize(256); //FIX clickeing issues when displaying multiple prefab previews (when a tile has a prefab attached
            Event e = Event.current;

            m_dblClick.Update();

            //FIX: when a tileset is changed, the layout change and during some frames, the BeginScrollView could return wrong values
            // This will make sure the scroll position is updated after mouse is over the control
            if (e.isMouse || e.type == EventType.ScrollWheel)
            {
                m_updateScrollPos = true;
            }

            // This way a gui exception is avoided
            if (e.type == EventType.Layout && m_selectBrushInInspector != null)
            {
                Selection.activeObject   = m_selectBrushInInspector;
                m_selectBrushInInspector = null;
            }

            if (m_lastTime == 0f)
            {
                m_lastTime = Time.realtimeSinceStartup;
            }
            m_timeDt   = Time.realtimeSinceStartup - m_lastTime;
            m_lastTime = Time.realtimeSinceStartup;

            if (Tileset == null)
            {
                EditorGUILayout.HelpBox("There is no tileset selected", MessageType.Info);
                return;
            }
            else if (Tileset.AtlasTexture == null)
            {
                EditorGUILayout.HelpBox("There is no atlas texture set", MessageType.Info);
                return;
            }
            else if (Tileset.Tiles.Count == 0)
            {
                EditorGUILayout.HelpBox("There are no tiles to show in the current tileset", MessageType.Info);
                return;
            }

            m_sharedData = GetSharedTilesetData(Tileset);

            float visualTilePadding       = 1;
            bool  isLeftMouseReleased     = e.type == EventType.MouseUp && e.button == 0;
            bool  isRightMouseReleased    = e.type == EventType.MouseUp && e.button == 1;
            bool  isInsideTileScrollArea  = e.isMouse && m_rTileScrollArea.Contains(e.mousePosition);
            bool  isInsideBrushScrollArea = e.isMouse && m_rBrushScrollArea.Contains(e.mousePosition);

            // Create TileView ReorderableList
            if (m_sharedData.tileViewList == null || m_sharedData.tileViewList.list != Tileset.TileViews)
            {
                m_sharedData.tileViewList = TilesetEditor.CreateTileViewReorderableList(Tileset);
                m_sharedData.tileViewList.onSelectCallback += (ReorderableList list) =>
                {
                    /* NOTE: this will select the tileview for the painting brush. Commented just in case.
                     * if(list.index >= 0)
                     * {
                     *  TileSelection tileSelection = Tileset.TileViews[list.index].tileSelection.Clone();
                     *  tileSelection.FlipVertical();
                     *  Tileset.TileSelection = tileSelection;
                     * }
                     * else*/
                    RemoveTileSelection();
                };
                m_sharedData.tileViewList.onRemoveCallback += (ReorderableList list) =>
                {
                    RemoveTileSelection();
                };
            }

            // Draw TileView ReorderableList
            {
                GUI.color = Color.cyan;
                GUILayout.BeginVertical(Styles.Instance.customBox);
                m_sharedData.tileViewList.index = Mathf.Clamp(m_sharedData.tileViewList.index, -1, Tileset.TileViews.Count - 1);
                m_sharedData.tileViewList.DoLayoutList();
                Rect rList = GUILayoutUtility.GetLastRect();
                if (e.isMouse && !rList.Contains(e.mousePosition))
                {
                    m_sharedData.tileViewList.ReleaseKeyboardFocus();
                }
                GUILayout.EndVertical();
                GUI.color = Color.white;
            }
            TileView tileView = m_sharedData.tileViewList != null && m_sharedData.tileViewList.index >= 0 ? Tileset.TileViews[m_sharedData.tileViewList.index] : null;

            if (tileView == null)
            {
                Tileset.TileRowLength = Mathf.Clamp(EditorGUILayout.IntField("TileRowLength", Tileset.TileRowLength), 1, Tileset.Width);
            }


            List <string> viewNameList = new List <string>()
            {
                "(All)"
            };

            viewNameList.AddRange(Tileset.TileViews.Select(x => x.name));
            string[] tileViewNames  = viewNameList.ToArray();
            int[]    tileViewValues = Enumerable.Range(-1, Tileset.TileViews.Count + 1).ToArray();
            EditorGUI.BeginChangeCheck();
            m_sharedData.tileViewList.index = EditorGUILayout.IntPopup("Tileset View", m_sharedData.tileViewList.index, tileViewNames, tileViewValues);
            if (EditorGUI.EndChangeCheck())
            {
                RemoveTileSelection();
            }

            // Draw Background Color Selector
            Tileset.BackgroundColor = EditorGUILayout.ColorField("Background Color", Tileset.BackgroundColor);
            if (m_prevBgColor != Tileset.BackgroundColor || Styles.Instance.scrollStyle.normal.background == null)
            {
                m_prevBgColor = Tileset.BackgroundColor;
                if (Styles.Instance.scrollStyle.normal.background == null)
                {
                    Styles.Instance.scrollStyle.normal.background = new Texture2D(1, 1)
                    {
                        hideFlags = HideFlags.DontSave
                    }
                }
                ;
                Styles.Instance.scrollStyle.normal.background.SetPixel(0, 0, Tileset.BackgroundColor);
                Styles.Instance.scrollStyle.normal.background.Apply();
            }
            //---

            // Draw Zoom Selector
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label(EditorGUIUtility.FindTexture("ViewToolZoom"), GUILayout.Width(35f));
            float visualTileZoom = EditorGUILayout.Slider(Tileset.VisualTileSize.x / Tileset.TilePxSize.x, 0.25f, 4f);

            Tileset.VisualTileSize = visualTileZoom * Tileset.TilePxSize;
            if (GUILayout.Button("Reset", GUILayout.Width(50f)))
            {
                Tileset.VisualTileSize = new Vector2(32f * Tileset.TilePxSize.x / Tileset.TilePxSize.y, 32f);
            }
            EditorGUILayout.EndHorizontal();
            //---

            string sTileIdLabel = Tileset.SelectedTileId != Tileset.k_TileId_Empty? " (id:" + Tileset.SelectedTileId + ")" : "";

            EditorGUILayout.LabelField("Tile Palette" + sTileIdLabel, EditorStyles.boldLabel);

            // keeps values safe
            m_sharedData.tileViewRowLength = Mathf.Max(1, m_sharedData.tileViewRowLength);

            float tileAreaWidth  = m_sharedData.tileViewRowLength * (Tileset.VisualTileSize.x + visualTilePadding) + 4f;
            float tileAreaHeight = (Tileset.VisualTileSize.y + visualTilePadding) * (1 + (m_visibleTileCount - 1) / m_sharedData.tileViewRowLength) + 4f;

            m_sharedData.tileViewRowLength = tileView != null ? tileView.tileSelection.rowLength : Tileset.TileRowLength;

            //float minTileScrollHeight = (Tileset.VisualTileSize.y + visualTilePadding) * 6f;// NOTE: GUILayout.MinHeight is not working with BeginScrollView
            Vector2 tilesScrollPos = EditorGUILayout.BeginScrollView(m_sharedData.tilesScrollPos, Styles.Instance.scrollStyle /*, GUILayout.MinHeight(minTileScrollHeight)*/);

            if (m_updateScrollPos)
            {
                m_sharedData.tilesScrollPos = tilesScrollPos;
            }
            {
                // Scroll Moving Drag
                if (e.type == EventType.MouseDrag && (e.button == 1 || e.button == 2))
                {
                    m_sharedData.tilesScrollPos -= e.delta;
                }
                else
                {
                    DoAutoScroll();
                }

                if (e.isMouse)
                {
                    m_lastTileScrollMousePos = e.mousePosition;
                }
                if (Tileset.Tiles != null)
                {
                    GUILayoutUtility.GetRect(tileAreaWidth, tileAreaHeight);
                    m_visibleTileCount = 0;
                    List <uint> visibleTileList = new List <uint>();
                    int         tileViewWidth   = tileView != null ? tileView.tileSelection.rowLength : Tileset.Width;
                    int         tileViewHeight  = tileView != null ? ((tileView.tileSelection.selectionData.Count - 1) / tileView.tileSelection.rowLength) + 1 : Tileset.Height;
                    int         totalCount      = ((((tileViewWidth - 1) / m_sharedData.tileViewRowLength) + 1) * m_sharedData.tileViewRowLength) * tileViewHeight;
                    for (int i = 0; i < totalCount; ++i)
                    {
                        int  tileId   = GetTileIdFromIdx(i, m_sharedData.tileViewRowLength, tileViewWidth, tileViewHeight);
                        uint tileData = (uint)tileId;
                        if (tileView != null && tileId != Tileset.k_TileId_Empty)
                        {
                            tileData = tileView.tileSelection.selectionData[tileId];
                            tileId   = (int)(tileData & Tileset.k_TileDataMask_TileId);
                        }
                        Tile tile = Tileset.GetTile(tileId);
                        visibleTileList.Add(tileData);

                        int  tx          = m_visibleTileCount % m_sharedData.tileViewRowLength;
                        int  ty          = m_visibleTileCount / m_sharedData.tileViewRowLength;
                        Rect rVisualTile = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), Tileset.VisualTileSize.x, Tileset.VisualTileSize.y);

                        // Optimization, skipping not visible tiles
                        Rect rLocalVisualTile = rVisualTile; rLocalVisualTile.position -= m_sharedData.tilesScrollPos;
                        if (!rLocalVisualTile.Overlaps(m_rTileScrollSize))
                        {
                            ; // Do Nothing
                        }
                        else
                        //---
                        {
                            // Draw Tile
                            if (tile == null)
                            {
                                HandlesEx.DrawRectWithOutline(rVisualTile, new Color(0f, 0f, 0f, 0.2f), new Color(0f, 0f, 0f, 0.2f));
                            }
                            else
                            {
                                HandlesEx.DrawRectWithOutline(rVisualTile, new Color(0f, 0f, 0f, 0.1f), new Color(0f, 0f, 0f, 0.1f));
                                TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset);
                            }

                            Rect rTileRect = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), (Tileset.VisualTileSize.x + visualTilePadding), (Tileset.VisualTileSize.y + visualTilePadding));
                            if (rVisualTile.Contains(e.mousePosition))
                            {
                                if (e.type == EventType.MouseDrag && e.button == 0)
                                {
                                    m_sharedData.pointedTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                }
                                else if (e.type == EventType.MouseDown && e.button == 0)
                                {
                                    m_sharedData.startDragTileIdxRect = m_sharedData.pointedTileIdxRect = m_sharedData.endDragTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                }
                                else if (e.type == EventType.MouseUp && e.button == 0)
                                {
                                    m_sharedData.endDragTileIdxRect = new KeyValuePair <int, Rect>(m_visibleTileCount, rTileRect);
                                    DoSetTileSelection();
                                }
                            }

                            if ((isLeftMouseReleased || isRightMouseReleased) && isInsideTileScrollArea && rVisualTile.Contains(e.mousePosition) &&
                                (m_sharedData.startDragTileIdxRect.Key == m_sharedData.endDragTileIdxRect.Key) && // and there is not dragging selection
                                m_rTileScrollSize.Contains(e.mousePosition - m_sharedData.tilesScrollPos))   // and it's inside the scroll area
                            {
                                Tileset.SelectedTileId = tileId;

                                //Give focus to SceneView to get key events
                                FocusSceneView();

                                if (isRightMouseReleased)
                                {
                                    TilePropertiesWindow.Show(Tileset);
                                }
                            }
                            else if (tile != null && Tileset.SelectedTileId == tileId)
                            {
                                HandlesEx.DrawRectWithOutline(rTileRect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f));
                            }
                        }

                        ++m_visibleTileCount;
                    }
                    m_visibleTileList = visibleTileList;

                    // Draw selection rect
                    if (m_sharedData.startDragTileIdxRect.Key != m_sharedData.pointedTileIdxRect.Key /*&& m_startDragTileIdxRect.Key == m_endDragTileIdxRect.Key*/)
                    {
                        Rect rSelection = new Rect(m_sharedData.startDragTileIdxRect.Value.center, m_sharedData.pointedTileIdxRect.Value.center - m_sharedData.startDragTileIdxRect.Value.center);
                        rSelection.Set(Mathf.Min(rSelection.xMin, rSelection.xMax), Mathf.Min(rSelection.yMin, rSelection.yMax), Mathf.Abs(rSelection.width), Mathf.Abs(rSelection.height));
                        rSelection.xMin -= m_sharedData.startDragTileIdxRect.Value.width / 2;
                        rSelection.xMax += m_sharedData.startDragTileIdxRect.Value.width / 2;
                        rSelection.yMin -= m_sharedData.startDragTileIdxRect.Value.height / 2;
                        rSelection.yMax += m_sharedData.startDragTileIdxRect.Value.height / 2;
                        HandlesEx.DrawRectWithOutline(rSelection, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                    }
                }
            }
            EditorGUILayout.EndScrollView();
            if (e.type == EventType.Repaint)
            {
                m_rTileScrollArea          = GUILayoutUtility.GetLastRect();
                m_rTileScrollSize          = m_rTileScrollArea;
                m_rTileScrollSize.position = Vector2.zero; // reset position to the Contains and Overlaps inside the tile scroll view without repositioning the position of local positions
                if (tileAreaWidth > m_rTileScrollSize.width)
                {
                    m_rTileScrollSize.height -= GUI.skin.verticalScrollbar.fixedWidth;
                }
                if (tileAreaHeight > m_rTileScrollSize.height)
                {
                    m_rTileScrollSize.width -= GUI.skin.verticalScrollbar.fixedWidth;
                }
            }

            EditorGUILayout.BeginHorizontal();
            string sBrushIdLabel = Tileset.SelectedBrushId > 0 ? " (id:" + Tileset.SelectedBrushId + ")" : "";

            EditorGUILayout.LabelField("Brush Palette" + sBrushIdLabel, EditorStyles.boldLabel);
            m_displayBrushReordList = EditorUtils.DoToggleButton("Display List", m_displayBrushReordList);
            EditorGUILayout.EndHorizontal();
            Tileset.BrushTypeMask = EditorGUILayout.MaskField("Brush Mask", Tileset.BrushTypeMask, Tileset.GetBrushTypeArray());

            int tileRowLength = (int)(m_rTileScrollSize.width / (Tileset.VisualTileSize.x + visualTilePadding));

            if (tileRowLength <= 0)
            {
                tileRowLength = 1;
            }
            float fBrushesScrollMaxHeight = Screen.height / 4;
            //commented because m_rTileScrollSize.width.height was changed to Screen.height;  fBrushesScrollMaxHeight -= fBrushesScrollMaxHeight % 2; // sometimes because size of tile scroll affects size of brush scroll, the height is dancing between +-1, so this is always taking the pair value
            float fBrushesScrollHeight = Mathf.Min(fBrushesScrollMaxHeight, 4 + (Tileset.VisualTileSize.y + visualTilePadding) * (1 + (Tileset.Brushes.Count / tileRowLength)));

            EditorGUILayout.BeginVertical(GUILayout.MinHeight(fBrushesScrollHeight));
            if (m_displayBrushReordList)
            {
                DisplayBrushReorderableList();
            }
            else
            {
                bool    refreshBrushes   = false;
                Vector2 brushesScrollPos = EditorGUILayout.BeginScrollView(m_sharedData.brushesScrollPos, Styles.Instance.scrollStyle);
                if (m_updateScrollPos)
                {
                    m_sharedData.brushesScrollPos = brushesScrollPos;
                }
                {
                    Rect rScrollView = new Rect(0, 0, m_rTileScrollSize.width, 0);
                    tileRowLength = Mathf.Clamp((int)rScrollView.width / (int)(Tileset.VisualTileSize.x + visualTilePadding), 1, tileRowLength);
                    if (Tileset.Brushes != null)
                    {
                        GUILayout.Space((Tileset.VisualTileSize.y + visualTilePadding) * (1 + (Tileset.Brushes.Count - 1) / tileRowLength));
                        for (int i = 0, idx = 0; i < Tileset.Brushes.Count; ++i, ++idx)
                        {
                            Tileset.BrushContainer brushCont = Tileset.Brushes[i];
                            if (brushCont.BrushAsset == null || brushCont.BrushAsset.Tileset != Tileset)
                            {
                                refreshBrushes = true;
                                continue;
                            }
                            if (!brushCont.BrushAsset.ShowInPalette || !Tileset.IsBrushVisibleByTypeMask(brushCont.BrushAsset))
                            {
                                --idx;
                                continue;
                            }

                            int  tx          = idx % tileRowLength;
                            int  ty          = idx / tileRowLength;
                            Rect rVisualTile = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), Tileset.VisualTileSize.x, Tileset.VisualTileSize.y);
                            //Fix Missing Tileset reference
                            if (brushCont.BrushAsset.Tileset == null)
                            {
                                Debug.LogWarning("Fixed missing tileset reference in brush " + brushCont.BrushAsset.name + " Id(" + brushCont.Id + ")");
                                brushCont.BrushAsset.Tileset = Tileset;
                            }
                            uint tileData = Tileset.k_TileData_Empty;
                            if (brushCont.BrushAsset.IsAnimated())
                            {
                                tileData = brushCont.BrushAsset.GetAnimTileData();
                            }
                            else
                            {
                                tileData = brushCont.BrushAsset.PreviewTileData();
                            }
                            TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset, brushCont.BrushAsset.GetAnimUV());
                            if ((isLeftMouseReleased || isRightMouseReleased || m_dblClick.IsDblClick) && isInsideBrushScrollArea && rVisualTile.Contains(Event.current.mousePosition))
                            {
                                Tileset.SelectedBrushId = brushCont.Id;
                                RemoveTileSelection();
                                if (m_dblClick.IsDblClick)
                                {
                                    EditorGUIUtility.PingObject(brushCont.BrushAsset);
                                    m_selectBrushInInspector = brushCont.BrushAsset;
                                }
                                if (isRightMouseReleased)
                                {
                                    TilePropertiesWindow.Show(Tileset);
                                }
                            }
                            else if (Tileset.SelectedBrushId == brushCont.Id)
                            {
                                Rect rSelection = new Rect(2 + tx * (Tileset.VisualTileSize.x + visualTilePadding), 2 + ty * (Tileset.VisualTileSize.y + visualTilePadding), (Tileset.VisualTileSize.x + visualTilePadding), (Tileset.VisualTileSize.y + visualTilePadding));
                                HandlesEx.DrawRectWithOutline(rSelection, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f));
                            }
                        }
                    }

                    if (refreshBrushes)
                    {
                        Tileset.RemoveInvalidBrushes();
                        Tileset.UpdateBrushTypeArray();
                    }
                }
                EditorGUILayout.EndScrollView();
                if (e.type == EventType.Repaint)
                {
                    m_rBrushScrollArea = GUILayoutUtility.GetLastRect();
                }
            }
            EditorGUILayout.EndVertical();

            if (GUILayout.Button("Import all brushes found in the project"))
            {
                TilesetEditor.AddAllBrushesFoundInTheProject(Tileset);
                EditorUtility.SetDirty(Tileset);
            }
        }