Ejemplo n.º 1
0
        void OnBrushSelected(Tileset source, int prevBrushId, int newBrushId)
        {
            if (AllowBrushSelection)
            {
                if (m_selectedTileIdx >= 0 && m_hasFocus)
                {
                    m_tileIdOff = 0;
                    uint brushTileData = m_getTileDataFunc(m_selectedTileIdx);
                    if (brushTileData == Tileset.k_TileData_Empty)
                    {
                        brushTileData = 0u; // reset flags and everything
                    }
                    Undo.RecordObject(m_target, "BrushChanged");

                    TilesetBrush brush  = Tileset.FindBrush(newBrushId);
                    int          tileId = (int)(brush.PreviewTileData() & Tileset.k_TileDataMask_TileId);
                    brushTileData &= Tileset.k_TileDataMask_Flags;
                    brushTileData |= (uint)(newBrushId << 16) & Tileset.k_TileDataMask_BrushId;
                    brushTileData |= (uint)(tileId & Tileset.k_TileDataMask_TileId);

                    m_setTileDataFunc(m_selectedTileIdx, brushTileData);
                    m_hasChanged = true;
                }
                EditorUtility.SetDirty(m_target);
            }
        }
Ejemplo n.º 2
0
        public static void TileDataField(Rect position, GUIContent label, SerializedProperty property, Tileset tileset)
        {
            Event e = Event.current;
            bool  isLeftMouseReleased = e.type == EventType.MouseUp && e.button == 0;
            //NOTE: there is a bug with DrawTextureWithTexCoords where the texture disappears. It is fixed by overriding the Editor Script with a CustomEditor.
            Rect         rVisualTile = new Rect(position.x, position.y, k_TilePreviewSize, k_TilePreviewSize);
            uint         tileData    = (uint)property.intValue;
            TilesetBrush brush       = tileset.FindBrush(Tileset.GetBrushIdFromTileData(tileData));

            if (brush)
            {
                tileData = brush.PreviewTileData();
            }

            TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, tileset);
            if (isLeftMouseReleased && rVisualTile.Contains(e.mousePosition))
            {
                EditorWindow wnd = EditorWindow.focusedWindow;
                TileSelectionWindow.Show(tileset);
                TileSelectionWindow.Instance.Ping();
                wnd.Focus();
                GUI.FocusControl("");
            }
            EditorGUI.PropertyField(new Rect(position.x, position.y + k_TilePreviewSize, position.width, position.height - k_TilePreviewSize), property, label);
        }
Ejemplo n.º 3
0
        public static uint DoTileDataPropertiesLayout(uint tileData, Tileset tileset, bool displayBrush = true)
        {
            EditorGUILayout.BeginVertical(EditorStyles.helpBox);

            GUI.enabled = tileData != Tileset.k_TileData_Empty;
            EditorGUIUtility.labelWidth = 100;

            EditorGUI.BeginChangeCheck();
            EditorGUILayout.Toggle("Flip Horizontally", (tileData & Tileset.k_TileFlag_FlipH) != 0);
            if (EditorGUI.EndChangeCheck())
            {
                tileData ^= Tileset.k_TileFlag_FlipH;
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.Toggle("Flip Vertically", (tileData & Tileset.k_TileFlag_FlipV) != 0);
            if (EditorGUI.EndChangeCheck())
            {
                tileData ^= Tileset.k_TileFlag_FlipV;
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.Toggle("Rotate 90º", (tileData & Tileset.k_TileFlag_Rot90) != 0);
            if (EditorGUI.EndChangeCheck())
            {
                tileData ^= Tileset.k_TileFlag_Rot90;
            }

            if (displayBrush)
            {
                EditorGUI.BeginChangeCheck();
                int          brushId = Tileset.GetBrushIdFromTileData(tileData);
                TilesetBrush brush   = tileset.FindBrush(brushId);
                brush = (TilesetBrush)EditorGUILayout.ObjectField("Brush", brush, typeof(TilesetBrush), false);
                if (EditorGUI.EndChangeCheck())
                {
                    brushId = brush != null?tileset.FindBrushId(brush.name) : Tileset.k_BrushId_Default;

                    int tileId = brush != null ? (int)(brush.PreviewTileData() & Tileset.k_TileDataMask_TileId) : Tileset.GetTileIdFromTileData(tileData);
                    tileData &= Tileset.k_TileDataMask_Flags;
                    tileData |= (uint)(brushId << 16) & Tileset.k_TileDataMask_BrushId;
                    tileData |= (uint)(tileId & Tileset.k_TileDataMask_TileId);
                }
            }

            if (GUILayout.Button("Reset"))
            {
                tileData = Tileset.k_TileData_Empty;
            }

            EditorGUIUtility.labelWidth = 0;
            GUI.enabled = true;

            EditorGUILayout.EndVertical();
            return(tileData);
        }
Ejemplo n.º 4
0
 private void OnBrushSelected(Tileset source, int prevBrushId, int newBrushId)
 {
     if (m_randTileListHasFocus && m_randTileList.index >= 0 && m_randTileList.index < m_brush.RandomTileList.Count)
     {
         if (m_brush.RandomTileList[m_randTileList.index].tileData == Tileset.k_TileData_Empty)
         {
             m_brush.RandomTileList[m_randTileList.index].tileData = 0u;// reset flags and everything
         }
         m_brush.RandomTileList[m_randTileList.index].tileData &= ~Tileset.k_TileDataMask_TileId;
         m_brush.RandomTileList[m_randTileList.index].tileData &= ~Tileset.k_TileDataMask_BrushId;
         m_brush.RandomTileList[m_randTileList.index].tileData |= (uint)(newBrushId << 16);
         TilesetBrush brush = m_brush.Tileset.FindBrush(newBrushId);
         m_brush.RandomTileList[m_randTileList.index].tileData |= (uint)(brush.PreviewTileData() & Tileset.k_TileDataMask_TileId);
     }
     EditorUtility.SetDirty(target);
     m_brush.InvalidateSortedList();
 }
Ejemplo n.º 5
0
        private bool m_displayAutocompleteBtn = false; // fix gui warning when button appears
        public void Display(Vector2 visualTileSize)
        {
            GUI.changed |= m_hasChanged;
            m_hasChanged = false;
            Event e = Event.current;
            bool  isLeftMouseReleased = e.type == EventType.MouseUp && e.button == 0;

            if (isLeftMouseReleased)
            {
                m_hasFocus = m_tileSelectionRect.Contains(e.mousePosition);
            }

            bool  hasEmptyTiles        = false;
            int   size                 = m_width * m_height;
            Color cSelectedBorderColor = new Color(1f, 1f, 0f, 1f);

            if (!m_hasFocus)
            {
                cSelectedBorderColor *= .8f;
            }
            GUILayout.BeginHorizontal();
            {
                // Draw Autotile Combination Control
                GUI.backgroundColor = Tileset.BackgroundColor;
                GUILayoutUtility.GetRect(visualTileSize.x * m_width, visualTileSize.y * m_height + 1f);
                Rect rArea = GUILayoutUtility.GetLastRect();
                {
                    if (m_backgroundTexture)
                    {
                        GUI.DrawTexture(new Rect(rArea.position, Vector2.Scale(visualTileSize, new Vector2(m_width, m_height))), m_backgroundTexture);
                    }
                    GUI.backgroundColor = Color.white;
                    for (int tileIdx = 0; tileIdx < size; ++tileIdx)
                    {
                        int  gx          = tileIdx % m_width;
                        int  gy          = tileIdx / m_width;
                        Rect rVisualTile = new Rect(gx * visualTileSize.x, gy * visualTileSize.y, visualTileSize.x, visualTileSize.y);
                        rVisualTile.position += rArea.position;
                        uint tileData = m_getTileDataFunc(tileIdx);
                        hasEmptyTiles |= tileData == Tileset.k_TileData_Empty;
                        TilesetBrush brush = Tileset.FindBrush(Tileset.GetBrushIdFromTileData(tileData));
                        if (brush)
                        {
                            tileData = TilesetBrush.ApplyAndMergeTileFlags(brush.PreviewTileData(), tileData);
                        }
                        int tileId = (int)(tileData & Tileset.k_TileDataMask_TileId);
                        if (tileId != Tileset.k_TileId_Empty)
                        {
                            TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset);
                        }

                        Color bgColor = new Color(1f - Tileset.BackgroundColor.r, 1f - Tileset.BackgroundColor.g, 1f - Tileset.BackgroundColor.b, Tileset.BackgroundColor.a);
                        HandlesEx.DrawRectWithOutline(rVisualTile, m_selectedTileIdx == tileIdx ? new Color(0f, 0f, 0f, 0.1f) : new Color(), m_selectedTileIdx == tileIdx ? cSelectedBorderColor : bgColor);

                        if (isLeftMouseReleased && rVisualTile.Contains(e.mousePosition))
                        {
                            m_selectedTileIdx = tileIdx;
                            EditorWindow wnd = EditorWindow.focusedWindow;
                            TileSelectionWindow.Show(Tileset);
                            TileSelectionWindow.Instance.Ping();
                            wnd.Focus();
                            GUI.FocusControl("");
                        }
                    }
                }

                uint brushTileData = m_selectedTileIdx >= 0 ? m_getTileDataFunc(m_selectedTileIdx) : Tileset.k_TileData_Empty;
                brushTileData = DoTileDataPropertiesLayout(brushTileData, Tileset, AllowBrushSelection);
                if (m_selectedTileIdx >= 0)
                {
                    m_setTileDataFunc(m_selectedTileIdx, brushTileData);
                }
            }
            GUILayout.EndHorizontal();

            if (e.type == EventType.Repaint)
            {
                m_tileSelectionRect = GUILayoutUtility.GetLastRect();
            }

            m_displayAutocompleteBtn = e.type == EventType.Layout ? !hasEmptyTiles && m_tileIdOff != 0 : m_displayAutocompleteBtn;
            if (size > 1 && m_displayAutocompleteBtn && GUILayout.Button("Autocomplete relative to last change"))
            {
                Undo.RecordObject(m_target, "MultipleTileChanged");
                for (int tileIdx = 0; tileIdx < size; ++tileIdx)
                {
                    if (tileIdx != m_tileIdOffSkipIdx)
                    {
                        int brushTileId = (int)(m_getTileDataFunc(tileIdx) & Tileset.k_TileDataMask_TileId);
                        brushTileId += m_tileIdOff;
                        if (brushTileId < 0 || brushTileId >= m_tileset.Tiles.Count)
                        {
                            m_setTileDataFunc(tileIdx, Tileset.k_TileData_Empty);
                        }
                        else
                        {
                            uint tileData = m_getTileDataFunc(tileIdx);
                            tileData &= ~Tileset.k_TileDataMask_TileId;
                            tileData |= (uint)(brushTileId & Tileset.k_TileDataMask_TileId);
                            m_setTileDataFunc(tileIdx, tileData);
                        }
                    }
                }
                m_tileIdOff = 0;
                EditorUtility.SetDirty(m_target);
            }
            if (Tileset.TileSelection != null && Tileset.TileSelection.selectionData.Count == m_width * m_height && Tileset.TileSelection.rowLength == m_width)
            {
                if (GUILayout.Button("Autocomplete from selection"))
                {
                    Undo.RecordObject(m_target, "MultipleTileChanged");
                    for (int tileIdx = 0; tileIdx < size; ++tileIdx)
                    {
                        int selectionIdx = (tileIdx % m_width) + (m_height - 1 - tileIdx / m_width) * m_width;
                        int brushTileId  = (int)(Tileset.TileSelection.selectionData[selectionIdx] & Tileset.k_TileDataMask_TileId);
                        m_setTileDataFunc(tileIdx, (uint)(brushTileId & Tileset.k_TileDataMask_TileId));
                    }
                    m_tileIdOff = 0;
                    EditorUtility.SetDirty(m_target);
                }
            }

            if (ShowHelpBox)
            {
                EditorGUILayout.HelpBox(HelpBoxText, MessageType.Info);
            }
        }
Ejemplo n.º 6
0
        private bool m_displayAutocompleteBtn = false; // fix gui warning when button appears
        public void Display(Object target, uint[] aTileData, int[] tileIdxMap, int gridWidth, int gridHeight, Vector2 visualTileSize, int[] symbolIdxMap)
        {
            GUI.changed      = m_hasChanged;
            m_hasChanged     = false;
            m_target         = target;
            m_aBrushTileData = aTileData;
            Event e = Event.current;
            bool  isRightMouseReleased = e.type == EventType.MouseUp && e.button == 0;

            if (isRightMouseReleased && !m_tileSelectionRect.Contains(e.mousePosition))
            {
                m_selectedTileIdx = -1;
            }

            GUILayout.BeginHorizontal();

            // Draw Autotile Combination Control
            GUI.backgroundColor = Tileset.BackgroundColor;
            GUILayout.BeginScrollView(Vector2.zero, (GUIStyle)"Button", GUILayout.Width(visualTileSize.x * gridWidth), GUILayout.Height(visualTileSize.y * gridHeight + 1f));
            GUI.backgroundColor = Color.white;
            for (int i = 0; i < gridWidth * gridHeight; ++i)
            {
                int          gx          = i % gridWidth;
                int          gy          = i / gridWidth;
                int          tileIdx     = tileIdxMap[i];
                Rect         rVisualTile = new Rect(gx * visualTileSize.x, gy * visualTileSize.y, visualTileSize.x, visualTileSize.y);
                uint         tileData    = m_aBrushTileData[tileIdx];
                TilesetBrush brush       = Tileset.FindBrush(Tileset.GetBrushIdFromTileData(tileData));
                if (brush)
                {
                    uint brushPreviewTileData = (brush.PreviewTileData() & ~Tileset.k_TileDataMask_BrushId) | (tileData & Tileset.k_TileDataMask_BrushId);
                    uint combinedFlags        = (brushPreviewTileData ^ tileData) & Tileset.k_TileDataMask_Flags;
                    tileData = (brushPreviewTileData & ~Tileset.k_TileDataMask_Flags) | combinedFlags;
                }
                int tileId = (int)(tileData & Tileset.k_TileDataMask_TileId);
                if (tileId != Tileset.k_TileId_Empty)
                {
                    //Rect tileUV = Tileset.Tiles[tileId].uv;
                    //GUI.DrawTextureWithTexCoords(rVisualTile, Tileset.AtlasTexture, tileUV, true);
                    TilesetEditor.DoGUIDrawTileFromTileData(rVisualTile, tileData, Tileset);
                }
                else if (symbolIdxMap != null)
                {
                    GUI.DrawTexture(rVisualTile, GetTileSymbolTexture((byte)symbolIdxMap[i]), ScaleMode.ScaleToFit, true);
                }

                Color bgColor = new Color(1f - Tileset.BackgroundColor.r, 1f - Tileset.BackgroundColor.g, 1f - Tileset.BackgroundColor.b, Tileset.BackgroundColor.a);
                HandlesEx.DrawRectWithOutline(rVisualTile, m_selectedTileIdx == tileIdx ? new Color(0f, 0f, 0f, 0.1f) : new Color(), m_selectedTileIdx == tileIdx ? new Color(1f, 1f, 0f, 1f) : bgColor);

                if (isRightMouseReleased && rVisualTile.Contains(e.mousePosition))
                {
                    m_selectedTileIdx = tileIdx;
                    EditorWindow wnd = EditorWindow.focusedWindow;
                    TileSelectionWindow.Show(Tileset);
                    TileSelectionWindow.Instance.Ping();
                    wnd.Focus();
                    GUI.FocusControl("");
                }
            }
            GUILayout.EndScrollView();

            uint brushTileData = m_selectedTileIdx >= 0 ? m_aBrushTileData[m_selectedTileIdx] : Tileset.k_TileData_Empty;

            brushTileData = DoTileDataPropertiesLayout(brushTileData, Tileset);
            if (m_selectedTileIdx >= 0)
            {
                m_aBrushTileData[m_selectedTileIdx] = brushTileData;
            }

            GUILayout.EndHorizontal();
            if (e.type == EventType.Repaint)
            {
                m_tileSelectionRect = GUILayoutUtility.GetLastRect();
            }

            bool hasEmptyTiles = ArrayUtility.Contains <uint>(m_aBrushTileData, Tileset.k_TileData_Empty);

            m_displayAutocompleteBtn = e.type == EventType.Layout ? !hasEmptyTiles && m_tileIdOff != 0 : m_displayAutocompleteBtn;
            if (m_displayAutocompleteBtn && GUILayout.Button("Autocomplete relative to last change"))
            {
                Undo.RecordObject(m_target, "MultipleTileChanged");
                for (int i = 0; i < tileIdxMap.Length; ++i)
                {
                    int tileIdx = tileIdxMap[i];
                    if (tileIdx != m_tileIdOffSkipIdx)
                    {
                        int brushTileId = (int)(m_aBrushTileData[tileIdx] & Tileset.k_TileDataMask_TileId);
                        brushTileId += m_tileIdOff;
                        if (brushTileId < 0 || brushTileId >= m_tileset.Tiles.Count)
                        {
                            m_aBrushTileData[tileIdx] = Tileset.k_TileData_Empty;
                        }
                        else
                        {
                            m_aBrushTileData[tileIdx] &= ~Tileset.k_TileDataMask_TileId;
                            m_aBrushTileData[tileIdx] |= (uint)(brushTileId & Tileset.k_TileDataMask_TileId);
                        }
                    }
                }
                m_tileIdOff = 0;
                EditorUtility.SetDirty(m_target);
            }
            if (Tileset.TileSelection != null && Tileset.TileSelection.selectionData.Count == gridWidth * gridHeight && Tileset.TileSelection.rowLength == gridWidth)
            {
                if (GUILayout.Button("Autocomplete from selection"))
                {
                    Undo.RecordObject(m_target, "MultipleTileChanged");
                    for (int i = 0; i < tileIdxMap.Length; ++i)
                    {
                        int tileIdx      = tileIdxMap[i];
                        int selectionIdx = (i % gridWidth) + (gridHeight - 1 - i / gridWidth) * gridWidth;
                        int brushTileId  = (int)(Tileset.TileSelection.selectionData[selectionIdx] & Tileset.k_TileDataMask_TileId);
                        m_aBrushTileData[tileIdx] = (uint)(brushTileId & Tileset.k_TileDataMask_TileId);
                    }
                    m_tileIdOff = 0;
                    EditorUtility.SetDirty(m_target);
                }
            }

            if (ShowHelpBox)
            {
                EditorGUILayout.HelpBox("Select  a tile from the grid, then select a tile from Tile Selection Window to change the tile.\nOr select a group of tiles and press Autocomplete from selection.", MessageType.Info);
            }
        }
Ejemplo n.º 7
0
        public static uint DoTileDataProperties(Rect position, uint tileData, Tileset tileset, bool displayBrush = true)
        {
            position.height = 90f;
            GUI.BeginGroup(position, EditorStyles.helpBox);
            {
                GUI.enabled = tileData != Tileset.k_TileData_Empty;
                EditorGUIUtility.labelWidth = 100;

                Rect fieldRect = new Rect(2f, 2f, position.width - 4f, EditorGUIUtility.singleLineHeight);
                EditorGUI.BeginChangeCheck();
                EditorGUI.Toggle(fieldRect, "Flip Horizontally", (tileData & Tileset.k_TileFlag_FlipH) != 0);
                if (EditorGUI.EndChangeCheck())
                {
                    tileData ^= Tileset.k_TileFlag_FlipH;
                }
                fieldRect.y += EditorGUIUtility.singleLineHeight;
                EditorGUI.BeginChangeCheck();
                EditorGUI.Toggle(fieldRect, "Flip Vertically", (tileData & Tileset.k_TileFlag_FlipV) != 0);
                if (EditorGUI.EndChangeCheck())
                {
                    tileData ^= Tileset.k_TileFlag_FlipV;
                }
                fieldRect.y += EditorGUIUtility.singleLineHeight;
                EditorGUI.BeginChangeCheck();
                EditorGUI.Toggle(fieldRect, "Rotate 90º", (tileData & Tileset.k_TileFlag_Rot90) != 0);
                if (EditorGUI.EndChangeCheck())
                {
                    tileData ^= Tileset.k_TileFlag_Rot90;
                }

                if (displayBrush)
                {
                    fieldRect.y += EditorGUIUtility.singleLineHeight;
                    EditorGUI.BeginChangeCheck();
                    int          brushId = Tileset.GetBrushIdFromTileData(tileData);
                    TilesetBrush brush   = tileset.FindBrush(brushId);
                    brush = (TilesetBrush)EditorGUI.ObjectField(fieldRect, "Brush", brush, typeof(TilesetBrush), false);
                    if (EditorGUI.EndChangeCheck())
                    {
                        if (brush && brush.Tileset != tileset)
                        {
                            Debug.LogWarning("The brush " + brush.name + " belongs to a different tileset and cannot be selected! ");
                        }
                        else
                        {
                            brushId = brush != null?tileset.FindBrushId(brush.name) : Tileset.k_BrushId_Default;

                            int tileId = brush != null ? (int)(brush.PreviewTileData() & Tileset.k_TileDataMask_TileId) : Tileset.GetTileIdFromTileData(tileData);
                            tileData &= Tileset.k_TileDataMask_Flags;
                            tileData |= (uint)(brushId << 16) & Tileset.k_TileDataMask_BrushId;
                            tileData |= (uint)(tileId & Tileset.k_TileDataMask_TileId);
                        }
                    }
                }

                fieldRect.y += EditorGUIUtility.singleLineHeight + 4f;
                fieldRect.x += 2f; fieldRect.width -= 4f;
                if (GUI.Button(fieldRect, "Reset"))
                {
                    tileData = Tileset.k_TileData_Empty;
                }

                EditorGUIUtility.labelWidth = 0;
                GUI.enabled = true;
            }
            GUI.EndGroup();
            return(tileData);
        }