public void OnSceneGUI()
        {
            if (!MyAutoTileMap.IsInitialized)
            {
                return;
            }

            GUIStyle style = new GUIStyle();

            style.normal.textColor = Color.white;
            Handles.Label(HandleUtility.GUIPointToWorldRay(Vector3.zero).origin, " Brush Pos: " + MyAutoTileMap.BrushGizmo.BrushTilePos, style);
            Handles.Label(HandleUtility.GUIPointToWorldRay(new Vector3(0f, 16f)).origin, " Select Tile Idx: " + m_tilesetComponent.SelectedTileIdx, style);

            Rect rAutoTileMap = new Rect(MyAutoTileMap.transform.position.x, MyAutoTileMap.transform.position.y, MyAutoTileMap.MapTileWidth * MyAutoTileMap.Tileset.TileWorldWidth, -MyAutoTileMap.MapTileHeight * MyAutoTileMap.Tileset.TileWorldHeight);

            UtilsGuiDrawing.DrawRectWithOutline(rAutoTileMap, new Color(0f, 0f, 0f, 0f), new Color(1f, 1f, 1f, 1f));
            if (m_showCollisions)
            {
                DrawCollisions();
            }

            if (s_isEditModeOn)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                HandleUtility.AddDefaultControl(controlID);
                EventType currentEventType = Event.current.GetTypeForControl(controlID);
                bool      skip             = false;
                int       saveControl      = GUIUtility.hotControl;

                if (currentEventType == EventType.Layout)
                {
                    skip = true;
                }
                else if (currentEventType == EventType.ScrollWheel)
                {
                    skip = true;
                }

                if (!skip)
                {
                    EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), MouseCursor.Arrow);
                    GUIUtility.hotControl = controlID;

                    m_tilesetComponent.OnSceneGUI();

                    if (currentEventType == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button
                    {
                        // avoid dragging the map
                        Event.current.Use();
                    }
                }

                GUIUtility.hotControl = saveControl;

                if (GUI.changed)
                {
                    EditorUtility.SetDirty(target);
                }
            }
        }
        void DrawCollisions()
        {
            float   fCollW       = MyAutoTileMap.Tileset.TileWorldWidth / 4;
            float   fCollH       = MyAutoTileMap.Tileset.TileWorldHeight / 4;
            Rect    rColl        = new Rect(0, 0, fCollW, -fCollH);
            Color   cColl        = new Color(1f, 0f, 0f, 0.1f);
            Vector3 vTopLeft     = HandleUtility.GUIPointToWorldRay(Vector3.zero).origin;
            Vector3 vBottomRight = HandleUtility.GUIPointToWorldRay(new Vector3(Screen.width, Screen.height)).origin;

            vTopLeft.y      = -vTopLeft.y;
            vBottomRight.y  = -vBottomRight.y;
            vTopLeft.x     -= (vTopLeft.x % fCollW) + fCollW / 2;
            vTopLeft.y     -= (vTopLeft.y % fCollH) + fCollH / 2;
            vBottomRight.x -= (vBottomRight.x % fCollW) - fCollW / 2;
            vBottomRight.y -= (vBottomRight.y % fCollH) - fCollH / 2;
            for (float y = vTopLeft.y; y <= vBottomRight.y; y += MyAutoTileMap.Tileset.TileWorldHeight / 4)
            {
                for (float x = vTopLeft.x; x <= vBottomRight.x; x += MyAutoTileMap.Tileset.TileWorldWidth / 4)
                {
                    AutoTileMap.eTileCollisionType collType = MyAutoTileMap.GetAutotileCollisionAtPosition(new Vector3(x, -y));
                    if (collType != AutoTileMap.eTileCollisionType.PASSABLE)
                    {
                        rColl.position = new Vector2(x - fCollW / 2, -(y - fCollH / 2));
                        UtilsGuiDrawing.DrawRectWithOutline(rColl, cColl, cColl);
                    }
                }
            }
        }
        public void OnSceneGUI()
        {
            if (!MyAutoTileMap.IsInitialized)
            {
                return;
            }

            DoToolBar();

            Rect rAutoTileMap = new Rect(MyAutoTileMap.transform.position.x, MyAutoTileMap.transform.position.y, MyAutoTileMap.MapTileWidth * MyAutoTileMap.Tileset.TileWorldWidth, -MyAutoTileMap.MapTileHeight * MyAutoTileMap.Tileset.TileWorldHeight);

            UtilsGuiDrawing.DrawRectWithOutline(rAutoTileMap, new Color(0f, 0f, 0f, 0f), new Color(1f, 1f, 1f, 1f));
            if (m_showCollisions)
            {
                DrawCollisions();
            }

            if (s_isEditModeOn)
            {
                int controlID = GUIUtility.GetControlID(FocusType.Passive);
                HandleUtility.AddDefaultControl(controlID);
                EventType currentEventType = Event.current.GetTypeForControl(controlID);
                bool      skip             = false;
                int       saveControl      = GUIUtility.hotControl;

                if (currentEventType == EventType.Layout)
                {
                    skip = true;
                }
                else if (currentEventType == EventType.ScrollWheel)
                {
                    skip = true;
                }

                if (!skip)
                {
                    EditorGUIUtility.AddCursorRect(new Rect(0f, 0f, (float)Screen.width, (float)Screen.height), MouseCursor.Arrow);
                    GUIUtility.hotControl = controlID;

                    if (Event.current.type == EventType.MouseUp && Event.current.button == 1)
                    {
                        s_brushMode = eBrushMode.Paint;
                    }
                    m_tilesetComponent.OnSceneGUI();

                    if (currentEventType == EventType.MouseDrag && Event.current.button < 2) // 2 is for central mouse button
                    {
                        // avoid dragging the map
                        Event.current.Use();
                    }
                }

                GUIUtility.hotControl = saveControl;

                if (GUI.changed)
                {
                    EditorUtility.SetDirty(target);
                }
            }
        }
Beispiel #4
0
        void UpdateTilesetOnInspector(Rect rTileset)
        {
            if (rTileset.Contains(Event.current.mousePosition))
            {
                UpdateMouseInputs();
                Vector2 mouseLocalPos = Event.current.mousePosition - new Vector2(rTileset.x, rTileset.y);
                int     tx            = (int)(mouseLocalPos.x / k_visualTileWidth);
                int     ty            = (int)(mouseLocalPos.y / k_visualTileHeight);
                int     autotileIdx   = ty * m_autoTileMap.Tileset.AutoTilesPerRow + tx + ((int)m_subTilesetIdx * 256);

                if (m_isMouseLeftDown)
                {
                    // select pressed tile
                    m_selectedTileIdx = autotileIdx;

                    // Remove Brush
                    m_autoTileMap.BrushGizmo.Clear();
                    m_tilesetSelStart = m_tilesetSelEnd = -1;
                }
                else if (m_isMouseRightDown)
                {
                    m_tilesetSelStart = m_tilesetSelEnd = autotileIdx;
                }
                else if (m_isMouseRight)
                {
                    m_tilesetSelEnd = autotileIdx;
                }
                else if (m_tilesetSelStart >= 0 && m_tilesetSelEnd >= 0)
                {
                    m_autoTileMap.BrushGizmo.RefreshBrushGizmoFromTileset(m_tilesetSelStart, m_tilesetSelEnd);
                    m_tilesetSelStart = m_tilesetSelEnd = -1;
                }

                // Draw selection rect
                if (m_tilesetSelStart >= 0 && m_tilesetSelEnd >= 0)
                {
                    int  tilesetIdxStart = m_tilesetSelStart - ((int)m_subTilesetIdx * 256);                // make it relative to selected tileset
                    int  tilesetIdxEnd   = m_tilesetSelEnd - ((int)m_subTilesetIdx * 256);                  // make it relative to selected tileset
                    Rect selRect         = new Rect( );
                    int  TileStartX      = tilesetIdxStart % m_autoTileMap.Tileset.AutoTilesPerRow;
                    int  TileStartY      = tilesetIdxStart / m_autoTileMap.Tileset.AutoTilesPerRow;
                    int  TileEndX        = tilesetIdxEnd % m_autoTileMap.Tileset.AutoTilesPerRow;
                    int  TileEndY        = tilesetIdxEnd / m_autoTileMap.Tileset.AutoTilesPerRow;
                    selRect.width  = (Mathf.Abs(TileEndX - TileStartX) + 1) * k_visualTileWidth;
                    selRect.height = (Mathf.Abs(TileEndY - TileStartY) + 1) * k_visualTileHeight;
                    float scrX = Mathf.Min(TileStartX, TileEndX) * k_visualTileWidth;
                    float scrY = Mathf.Min(TileStartY, TileEndY) * k_visualTileHeight;
                    selRect.position  = new Vector2(scrX, scrY);
                    selRect.position += rTileset.position;
                    //selRect.y = Screen.height - selRect.y;
                    UtilsGuiDrawing.DrawRectWithOutline(selRect, new Color(0f, 1f, 0f, 0.2f), new Color(0f, 1f, 0f, 1f));
                }
            }
        }
        void OnGUI()
        {
            if (!m_isInitialized)
            {
                return;
            }

            if (m_isGuiHidden)
            {
                m_rEditorRect  = new Rect(0, 0, 30, 32);
                m_rMapViewRect = new Rect(m_rEditorRect.x + m_rEditorRect.width, 0f, Screen.width - m_rEditorRect.width, Screen.height);
                if (GUI.Button(m_rEditorRect, ">"))
                {
                    m_isGuiHidden = false;
                }
                return;
            }

        #if UNITY_EDITOR
            m_isCtrlKeyHold = Event.current.shift;
        #else
            m_isCtrlKeyHold = Event.current.control || Event.current.shift;
        #endif
            Rect vRectTemp;

            float fPad                 = 4f;
            float fTilesetOffset       = m_isLayersMenuHidden? 64f : 128f;
            float fScrollBarWidth      = 16f;
            float fTileGroupGridHeight = 30f * (1 + (m_tileGroupNames.Length - 1) / 5);

            int tilesWidth  = k_visualTileWidth * m_autoTileMap.Tileset.AutoTilesPerRow;
            int tilesHeight = k_visualTileHeight * (256 / m_autoTileMap.Tileset.AutoTilesPerRow);

            m_rEditorRect          = new Rect(0f, 0f, tilesWidth + 2 * fPad + fScrollBarWidth, Screen.height);
            m_rMapViewRect         = new Rect(m_rEditorRect.x + m_rEditorRect.width, 0f, Screen.width - m_rEditorRect.width, Screen.height);
            m_rTilesetRect         = new Rect(fPad, fTilesetOffset + fPad, tilesWidth + fScrollBarWidth, Screen.height);
            m_rTilesetRect.height -= (m_rTilesetRect.y + fPad + fTileGroupGridHeight);
            float minimapRectW = Mathf.Min(m_rMapViewRect.width * 0.25f, m_autoTileMap.MinimapTexture.width);  // fix to limit the size of minimap for big maps
            float minimapRectH = m_autoTileMap.MinimapTexture.height * minimapRectW / m_autoTileMap.MinimapTexture.width;
            m_rMinimapRect = new Rect(Screen.width - minimapRectW, Screen.height - minimapRectH, minimapRectW, minimapRectH);

            //+++ Draw Tileset Selection Buttons
            vRectTemp       = new Rect(m_rTilesetRect.x, Screen.height - fTileGroupGridHeight, tilesWidth, fTileGroupGridHeight);
            m_subTilesetIdx = GUI.SelectionGrid(vRectTemp, m_subTilesetIdx, m_tileGroupNames, 5);
            //---

            GUI.Box(m_rEditorRect, "");

            if (GUI.Button(new Rect(0, 0, 130, 32), m_showCollisions? "Hide Collisions (C)" : "Show Collisions (C)"))
            {
                m_showCollisions = !m_showCollisions;
            }

            if (GUI.Button(new Rect(0, 32, 130, 32), m_showMinimap? "Hide Minimap (M)" : "Show Minimap (M)"))
            {
                m_showMinimap = !m_showMinimap;
                m_autoTileMap.BrushGizmo.IsRefreshMinimapEnabled = m_showMinimap;
                if (m_showMinimap)
                {
                    m_autoTileMap.RefreshMinimapTexture();
                }
            }

            if (GUI.Button(new Rect(130, 0, 120, 32), "Save"))
            {
                m_autoTileMap.ShowSaveDialog();
            }
            if (GUI.Button(new Rect(130, 32, 120, 32), "Load"))
            {
                m_autoTileMap.ShowLoadDialog();
            }
            if (GUI.Button(new Rect(250, 0, 30, 32), "<"))
            {
                m_isGuiHidden = true;
            }
            if (GUI.Button(new Rect(250, 32, 30, 32), "L"))
            {
                m_isLayersMenuHidden = !m_isLayersMenuHidden;
            }

            if (!m_isLayersMenuHidden)
            {
                if (GUI.Button(new Rect(0, 64, m_rTilesetRect.width, 32), m_autoTileMap.BrushGizmo.SmartBrushEnabled ? "Smart Brush Enabled" : "Smart Brush Disabled"))
                {
                    m_autoTileMap.BrushGizmo.SmartBrushEnabled = !m_autoTileMap.BrushGizmo.SmartBrushEnabled;
                }
                comboBoxControl.Rect.y                 = 96;
                comboBoxControl.Rect.width             = m_rTilesetRect.width;
                comboBoxControl.Rect.height            = 32;
                m_autoTileMap.BrushGizmo.SelectedLayer = comboBoxControl.Show();
            }

            if (!comboBoxControl.IsDropDownListVisible)
            {
                Rect viewRect = new Rect(0, 0, m_rTilesetRect.width - fScrollBarWidth, tilesHeight);
                m_scrollPos = GUI.BeginScrollView(m_rTilesetRect, m_scrollPos, viewRect);
                //+++ Draw Tiles Thumbnails
                {
                    float fTileRowNb = 32;
                    vRectTemp           = new Rect(0f, 0f, k_visualTileWidth * m_autoTileMap.Tileset.AutoTilesPerRow, k_visualTileHeight * fTileRowNb);
                    vRectTemp.position += m_rEditorRect.position;
                    int thumbIdx = 0;
                    GUI.DrawTexture(vRectTemp, m_thumbnailTextures[m_subTilesetIdx]);
                    for (int y = 0; thumbIdx < 256; ++y) //256 number of tileset for each tileset group
                    {
                        for (int x = 0; x < m_autoTileMap.Tileset.AutoTilesPerRow; ++x, ++thumbIdx)
                        {
                            Rect rDst = new Rect(x * k_visualTileWidth, y * k_visualTileHeight, k_visualTileWidth, k_visualTileHeight);
                            rDst.position += vRectTemp.position;
                            //if( MyAutoTileMap.IsAutoTileHasAlpha( x, y ) ) GUI.Box( rDst, "A" ); //for debug
                            if (m_isCtrlKeyHold)
                            {
                                string sCollision = "";
                                switch (m_autoTileMap.Tileset.AutotileCollType[m_subTilesetIdx * 256 + thumbIdx])
                                {
                                //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                                case AutoTileMap.eTileCollisionType.BLOCK: sCollision = "■"; break;

                                case AutoTileMap.eTileCollisionType.FENCE: sCollision = "#"; break;

                                case AutoTileMap.eTileCollisionType.WALL: sCollision = "□"; break;

                                case AutoTileMap.eTileCollisionType.OVERLAY: sCollision = "★"; break;
                                }
                                if (sCollision.Length > 0)
                                {
                                    GUI.color = new Color(1f, 1f, 1f, 1f);
                                    GUIStyle style = new GUIStyle();
                                    style.fontSize         = 30;
                                    style.fontStyle        = FontStyle.Bold;
                                    style.alignment        = TextAnchor.MiddleCenter;
                                    style.normal.textColor = Color.white;
                                    GUI.Box(rDst, sCollision, style);
                                    GUI.color = Color.white;
                                }
                            }
                        }
                    }
                    Rect rSelected = new Rect(0, 0, k_visualTileWidth, k_visualTileHeight);

                    int tileWithSelectMark = m_selectedTileIdx;
                    tileWithSelectMark -= (m_subTilesetIdx * 256);
                    rSelected.position  = vRectTemp.position + new Vector2((tileWithSelectMark % m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileWidth, (tileWithSelectMark / m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileHeight);

                    UtilsGuiDrawing.DrawRectWithOutline(rSelected, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                }
                //----
                GUI.EndScrollView();
            }

            if (m_showMinimap)
            {
                float minimapScale = m_rMinimapRect.width / m_autoTileMap.MinimapTexture.width;

                //NOTE: the texture is drawn blurred in web player unless default quality is set to Fast in project settings
                // see here for solution http://forum.unity3d.com/threads/webplayer-gui-issue.100256/#post-868451
                GUI.DrawTexture(m_rMinimapRect, m_autoTileMap.MinimapTexture);
                UtilsGuiDrawing.DrawRectWithOutline(m_rMinimapRect, new Color(0, 0, 0, 0), Color.black);

                // Draw camera region on minimap
                Vector3 vCameraPos  = m_autoTileMap.ViewCamera.ScreenPointToRay(new Vector3(0, Screen.height - 1)).origin;
                int     camTileX    = (int)(vCameraPos.x / m_autoTileMap.Tileset.TileWorldWidth);
                int     camTileY    = (int)(-vCameraPos.y / m_autoTileMap.Tileset.TileWorldHeight);
                Rect    rMinimapCam = new Rect(camTileX, camTileY, minimapScale * Screen.width / (m_camera2D.Zoom * m_autoTileMap.Tileset.TileWidth), minimapScale * Screen.height / (m_camera2D.Zoom * m_autoTileMap.Tileset.TileHeight));
                rMinimapCam.position *= minimapScale;
                rMinimapCam.position += m_rMinimapRect.position;
                UtilsGuiDrawing.DrawRectWithOutline(rMinimapCam, new Color(0, 0, 0, 0), Color.white);

                // Draw player on minimap
                if (m_camera2DFollowBehaviour != null && m_camera2DFollowBehaviour.Target != null)
                {
                    int  plyTileX = -1 + (int)(m_camera2DFollowBehaviour.Target.transform.position.x / m_autoTileMap.Tileset.TileWorldWidth);
                    int  plyTileY = -1 + (int)(-m_camera2DFollowBehaviour.Target.transform.position.y / m_autoTileMap.Tileset.TileWorldHeight);
                    Rect rPlayer  = new Rect(plyTileX, plyTileY, 3, 3);
                    rPlayer.position *= minimapScale;
                    rPlayer.position += m_rMinimapRect.position;
                    UtilsGuiDrawing.DrawRectWithOutline(rPlayer, Color.yellow, Color.blue);
                }
            }

            #region Draw Selection Rect
            // Map Version
            if (m_drawSelectionRect)
            {
                Rect selRect = new Rect( );
                selRect.width  = (Mathf.Abs(m_dragTileX - m_startDragTileX) + 1) * m_autoTileMap.Tileset.TileWidth * m_camera2D.Zoom;
                selRect.height = (Mathf.Abs(m_dragTileY - m_startDragTileY) + 1) * m_autoTileMap.Tileset.TileHeight * m_camera2D.Zoom;
                float   worldX  = Mathf.Min(m_startDragTileX, m_dragTileX) * m_autoTileMap.Tileset.TileWorldWidth;
                float   worldY  = -Mathf.Min(m_startDragTileY, m_dragTileY) * m_autoTileMap.Tileset.TileWorldHeight;
                Vector3 vScreen = m_camera2D.Camera.WorldToScreenPoint(new Vector3(worldX, worldY) + m_autoTileMap.transform.position);
                selRect.position = new Vector2(vScreen.x, vScreen.y);
                selRect.y        = Screen.height - selRect.y;
                UtilsGuiDrawing.DrawRectWithOutline(selRect, new Color(0f, 1f, 0f, 0.2f), new Color(0f, 1f, 0f, 1f));
            }
            // Tileset Version
            if (m_tilesetSelStart >= 0 && m_tilesetSelEnd >= 0)
            {
                int  tilesetIdxStart = m_tilesetSelStart - (m_subTilesetIdx * 256);            // make it relative to selected tileset
                int  tilesetIdxEnd   = m_tilesetSelEnd - (m_subTilesetIdx * 256);              // make it relative to selected tileset
                Rect selRect         = new Rect( );
                int  TileStartX      = tilesetIdxStart % m_autoTileMap.Tileset.AutoTilesPerRow;
                int  TileStartY      = tilesetIdxStart / m_autoTileMap.Tileset.AutoTilesPerRow;
                int  TileEndX        = tilesetIdxEnd % m_autoTileMap.Tileset.AutoTilesPerRow;
                int  TileEndY        = tilesetIdxEnd / m_autoTileMap.Tileset.AutoTilesPerRow;
                selRect.width  = (Mathf.Abs(TileEndX - TileStartX) + 1) * k_visualTileWidth;
                selRect.height = (Mathf.Abs(TileEndY - TileStartY) + 1) * k_visualTileHeight;
                float scrX = Mathf.Min(TileStartX, TileEndX) * k_visualTileWidth;
                float scrY = Mathf.Min(TileStartY, TileEndY) * k_visualTileHeight;
                selRect.position  = new Vector2(scrX, scrY - m_scrollPos.y);
                selRect.position += m_rTilesetRect.position;
                //selRect.y = Screen.height - selRect.y;
                UtilsGuiDrawing.DrawRectWithOutline(selRect, new Color(0f, 1f, 0f, 0.2f), new Color(0f, 1f, 0f, 1f));
            }
            #endregion
        }
Beispiel #6
0
        public void OnSceneGUI()
        {
            #region Undo / Redo
            if (Event.current.isKey && Event.current.shift && Event.current.type == EventType.KeyUp)
            {
                if (Event.current.keyCode == KeyCode.Z)
                {
                    m_autoTileMap.BrushGizmo.UndoAction();
                }
                if (Event.current.keyCode == KeyCode.Y)
                {
                    m_autoTileMap.BrushGizmo.RedoAction();
                }
            }
            #endregion

            Rect rSceneView = new Rect(0, 0, Screen.width, Screen.height);
            if (rSceneView.Contains(Event.current.mousePosition))
            {
                UpdateMouseInputs();

                Ray   ray      = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                Plane hPlane   = new Plane(Vector3.forward, Vector3.zero);
                float distance = 0;
                if (hPlane.Raycast(ray, out distance))
                {
                    // get the hit point:
                    Vector3 vPos = ray.GetPoint(distance);
                    m_autoTileMap.BrushGizmo.UpdateBrushGizmo(vPos);

                    if (m_isMouseRight || m_isMouseLeft)
                    {
                        int tile_x = (int)(vPos.x / m_autoTileMap.Tileset.TileWorldWidth);
                        int tile_y = (int)(-vPos.y / m_autoTileMap.Tileset.TileWorldHeight);

                        // for optimization, is true when mouse is over a diffent tile during the first update
                        bool isMouseTileChanged = (tile_x != m_prevMouseTileX) || (tile_y != m_prevMouseTileY);

                        //if ( m_autoTileMap.IsValidAutoTilePos(tile_x, tile_y)) // commented to allow drawing outside map, useful when brush has a lot of copied tiles
                        {
                            int gndTileType        = m_autoTileMap.GetAutoTile(tile_x, tile_y, m_autoTileMap.BrushGizmo.SelectedLayer).Id;
                            int gndOverlayTileType = m_autoTileMap.GetAutoTile(tile_x, tile_y, m_autoTileMap.BrushGizmo.SelectedLayer + 1).Id;

                            // mouse right for tile selection
                            if (m_isMouseRightDown || m_isMouseRight && isMouseTileChanged)
                            {
                                if (m_isMouseRightDown)
                                {
                                    m_startDragTileX = tile_x;
                                    m_startDragTileY = tile_y;

                                    // Remove Brush
                                    m_autoTileMap.BrushGizmo.Clear();
                                    m_tilesetSelStart = m_tilesetSelEnd = -1;

                                    // copy tile
                                    if (Event.current.shift)
                                    {
                                        m_selectedTileId = -2;                                         //NOTE: -2 means, ignore this tile when painting
                                    }
                                    else
                                    {
                                        m_selectedTileId = gndTileType >= 0? gndTileType : gndOverlayTileType;
                                    }
                                }
                                m_dragTileX = tile_x;
                                m_dragTileY = tile_y;
                            }
                            // isMouseLeft
                            else if (m_isMouseLeftDown || isMouseTileChanged)                              // avoid Push the same action twice during mouse drag
                            {
                                AutoTileBrush.TileAction action = new AutoTileBrush.TileAction();
                                if (m_autoTileMap.BrushGizmo.BrushAction != null)
                                {
                                    //+++ case of multiple tiles painting
                                    action.CopyRelative(m_autoTileMap, m_autoTileMap.BrushGizmo.BrushAction, tile_x, tile_y);
                                    if (Event.current.shift && (m_autoTileMap.BrushGizmo.SelectedLayer + 1) < m_autoTileMap.GetLayerCount())
                                    {
                                        // old functionality: ground tiles become ground overlay, ground overlay are removed, overlay tiles remains
                                        // Tiles in SelectedLayer are moved to next layer
                                        action.BecomeOverlay(m_autoTileMap.BrushGizmo.SelectedLayer);
                                    }
                                }
                                else
                                {
                                    //+++ case of single tile painting
                                    // If smart brush is enabled, the tiles with collision type Overlay will be placed directly in the first overlay layer found over current SelectedLayer
                                    int overlayLayer = m_autoTileMap.FindFirstLayerIdx(eLayerType.Overlay, m_autoTileMap.BrushGizmo.SelectedLayer);
                                    if (m_autoTileMap.BrushGizmo.SmartBrushEnabled && overlayLayer >= 0 && m_selectedTileId >= 0 && m_autoTileMap.Tileset.AutotileCollType[m_selectedTileId] == eTileCollisionType.OVERLAY)
                                    {
                                        action.Push(m_autoTileMap, tile_x, tile_y, m_selectedTileId, overlayLayer);
                                    }
                                    else
                                    {
                                        if (Event.current.shift || m_autoTileMap.IsAutoTileHasAlpha(m_selectedTileId) && m_autoTileMap.BrushGizmo.SmartBrushEnabled)
                                        {
                                            // Put tiles with alpha in the layer over Selected Layer
                                            action.Push(m_autoTileMap, tile_x, tile_y, m_selectedTileId, m_autoTileMap.BrushGizmo.SelectedLayer + 1);
                                        }
                                        else
                                        {
                                            action.Push(m_autoTileMap, tile_x, tile_y, m_selectedTileId, m_autoTileMap.BrushGizmo.SelectedLayer);
                                        }
                                    }
                                }

                                m_autoTileMap.BrushGizmo.PerformAction(action);
                                EditorUtility.SetDirty(m_autoTileMap);
                            }
                        }

                        m_prevMouseTileX = tile_x;
                        m_prevMouseTileY = tile_y;
                    }
                    else
                    {
                        // Copy selected tiles
                        if (m_dragTileX != -1 && m_dragTileY != -1)
                        {
                            m_autoTileMap.BrushGizmo.BrushAction = new AutoTileBrush.TileAction();
                            int startTileX = Mathf.Min(m_startDragTileX, m_dragTileX);
                            int startTileY = Mathf.Min(m_startDragTileY, m_dragTileY);
                            int endTileX   = Mathf.Max(m_startDragTileX, m_dragTileX);
                            int endTileY   = Mathf.Max(m_startDragTileY, m_dragTileY);

                            for (int tile_x = startTileX; tile_x <= endTileX; ++tile_x)
                            {
                                for (int tile_y = startTileY; tile_y <= endTileY; ++tile_y)
                                {
                                    // Tile position is relative to last released position ( m_dragTile )
                                    if (Event.current.shift)
                                    {
                                        // Copy overlay only
                                        for (int i = m_autoTileMap.BrushGizmo.SelectedLayer + 1; i < m_autoTileMap.GetLayerCount(); ++i)
                                        {
                                            int tileType = m_autoTileMap.GetAutoTile(tile_x, tile_y, i).Id;
                                            if (
                                                (m_autoTileMap.MapLayers[i].LayerType != eLayerType.Ground) ||
                                                (tileType >= 0) // this allow paste overlay tiles without removing ground or ground overlay
                                                )
                                            {
                                                m_autoTileMap.BrushGizmo.BrushAction.Push(m_autoTileMap, tile_x - m_dragTileX, tile_y - m_dragTileY, tileType, i);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        for (int i = 0; i < m_autoTileMap.GetLayerCount(); ++i)
                                        {
                                            int tileType = m_autoTileMap.GetAutoTile(tile_x, tile_y, i).Id;
                                            m_autoTileMap.BrushGizmo.BrushAction.Push(m_autoTileMap, tile_x - m_dragTileX, tile_y - m_dragTileY, tileType, i);
                                        }
                                    }
                                }
                            }

                            m_autoTileMap.BrushGizmo.RefreshBrushGizmo(startTileX, startTileY, endTileX, endTileY, m_dragTileX, m_dragTileY, Event.current.shift);

                            m_dragTileX = m_dragTileY = -1;
                        }
                    }
                }

                // Draw selection rect
                if (m_isMouseRight)
                {
                    float rX         = m_autoTileMap.transform.position.x + Mathf.Min(m_startDragTileX, m_dragTileX) * m_autoTileMap.Tileset.TileWorldWidth;
                    float rY         = m_autoTileMap.transform.position.y + Mathf.Min(m_startDragTileY, m_dragTileY) * m_autoTileMap.Tileset.TileWorldHeight;
                    float rWidth     = (Mathf.Abs(m_dragTileX - m_startDragTileX) + 1) * m_autoTileMap.Tileset.TileWorldWidth;
                    float rHeight    = (Mathf.Abs(m_dragTileY - m_startDragTileY) + 1) * m_autoTileMap.Tileset.TileWorldHeight;
                    Rect  rSelection = new Rect(rX, -rY, rWidth, -rHeight);
                    UtilsGuiDrawing.DrawRectWithOutline(rSelection, new Color(0f, 1f, 0f, 0.2f), new Color(0f, 1f, 0f, 1f));
                }
            }
        }
Beispiel #7
0
        public void OnInspectorGUI()
        {
            // refresh data if needed
            if (m_subTilesetNames == null || m_subTilesetNames.Length != m_autoTileMap.Tileset.SubTilesets.Count)
            {
                _refreshSubTilesetNames();
                m_subTilesetIdx = Mathf.Clamp(m_subTilesetIdx, 0, m_autoTileMap.Tileset.SubTilesets.Count);
            }
            m_subTilesetIdx = EditorGUILayout.Popup("Tileset: ", m_subTilesetIdx, m_subTilesetNames);

            if (GUI.changed || m_tilesetTexture == null)
            {
                m_tilesetTexture = UtilsAutoTileMap.GenerateTilesetTexture(m_autoTileMap.Tileset, m_autoTileMap.Tileset.SubTilesets[m_subTilesetIdx]);
            }


            if (m_tilesetTexture != null)
            {
                Rect rTileset = new Rect();
                Rect rTile    = new Rect(0, 0, k_visualTileWidth, k_visualTileHeight);

                if (IsEditCollision)
                {
                    //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                    EditorGUILayout.HelpBox("■ - Block Collision\n□ - Wall Collision\n# - Fence Collision\n★ - Overlay", MessageType.Info);
                }
                else
                {
                    EditorGUILayout.HelpBox("Press Shift Key to change collisions by pressing left/right mouse button over the tile", MessageType.Info);
                }

                // BeginScrollView
                m_scrollPos = EditorGUILayout.BeginScrollView(m_scrollPos, GUILayout.MinHeight(16f * k_visualTileHeight));
                {
                    GUIStyle tilesetStyle = new GUIStyle(GUI.skin.box);
                    tilesetStyle.normal.background = m_tilesetTexture;
                    tilesetStyle.border            = tilesetStyle.margin = tilesetStyle.padding = new RectOffset(0, 0, 0, 0);
                    float fWidth  = m_autoTileMap.Tileset.AutoTilesPerRow * k_visualTileWidth;
                    float fHeight = m_tilesetTexture.height * fWidth / m_tilesetTexture.width;
                    GUILayout.Box("", tilesetStyle, GUILayout.Width(fWidth), GUILayout.Height(fHeight));
                    rTileset = GUILayoutUtility.GetLastRect();

                    if (IsEditCollision)
                    {
                        for (int autoTileLocalIdx = 0; autoTileLocalIdx < 256; ++autoTileLocalIdx)                          //autoTileLocalIdx: index of current tileset group
                        {
                            rTile.x = rTileset.x + (autoTileLocalIdx % m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileWidth;
                            rTile.y = rTileset.y + (autoTileLocalIdx / m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileHeight;

                            int autoTileIdx = autoTileLocalIdx + (int)m_subTilesetIdx * 256;                             // global autotile idx
                            if (Event.current.type == EventType.MouseUp)
                            {
                                if (rTile.Contains(Event.current.mousePosition))
                                {
                                    int collType = (int)m_autoTileMap.Tileset.AutotileCollType[autoTileIdx];
                                    if (Event.current.button == 0)
                                    {
                                        collType += 1;                                         // go next
                                    }
                                    else if (Event.current.button == 1)
                                    {
                                        collType += (int)eTileCollisionType._SIZE - 1;                                         // go back
                                    }
                                    collType %= (int)eTileCollisionType._SIZE;
                                    m_autoTileMap.Tileset.AutotileCollType[autoTileIdx] = (eTileCollisionType)(collType);
                                }
                                EditorUtility.SetDirty(m_autoTileMap.Tileset);
                            }


                            string sCollision = "";
                            switch (m_autoTileMap.Tileset.AutotileCollType[autoTileIdx])
                            {
                            //NOTE: if you don't see the special characters properly, be sure this file is saved in UTF-8
                            case eTileCollisionType.BLOCK: sCollision = "■"; break;

                            case eTileCollisionType.FENCE: sCollision = "#"; break;

                            case eTileCollisionType.WALL: sCollision = "□"; break;

                            case eTileCollisionType.OVERLAY: sCollision = "★"; break;
                            }

                            if (sCollision.Length > 0)
                            {
                                GUI.color = new Color(1f, 1f, 1f, 1f);
                                GUIStyle style = new GUIStyle();
                                style.fontSize         = 30;
                                style.fontStyle        = FontStyle.Bold;
                                style.alignment        = TextAnchor.MiddleCenter;
                                style.normal.textColor = Color.white;
                                GUI.Box(rTile, sCollision, style);
                                GUI.color = Color.white;
                            }

                            //debug Alpha tiles

                            /*/
                             * if( m_autoTileMap.Tileset.IsAutoTileHasAlpha[autoTileIdx] )
                             * {
                             *      GUIStyle style = new GUIStyle();
                             *      style.fontSize = 30;
                             *      style.fontStyle = FontStyle.Bold;
                             *      style.alignment = TextAnchor.MiddleCenter;
                             *      style.normal.textColor = Color.blue;
                             *      GUI.Box( rTile, "A", style );
                             * }
                             * //*/
                        }
                    }
                    else
                    {
                        UpdateTilesetOnInspector(rTileset);

                        Rect rSelected          = new Rect(0, 0, k_visualTileWidth, k_visualTileHeight);
                        int  tileWithSelectMark = m_selectedTileId;
                        tileWithSelectMark -= (int)m_subTilesetIdx * 256;
                        rSelected.position  = rTileset.position + new Vector2((tileWithSelectMark % m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileWidth, (tileWithSelectMark / m_autoTileMap.Tileset.AutoTilesPerRow) * k_visualTileHeight);
                        UtilsGuiDrawing.DrawRectWithOutline(rSelected, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f));
                    }
                }
                EditorGUILayout.EndScrollView();
            }
        }