/// <summary> /// Flip the map horizontally /// </summary> /// <param name="changeFlags"></param> public void FlipH(bool changeFlags) { List <uint> flippedList = new List <uint>(GridWidth * GridHeight); for (int gx = MinGridX; gx <= MaxGridX; ++gx) { for (int gy = MinGridY; gy <= MaxGridY; ++gy) { int flippedGx = GridWidth - 1 - gx; flippedList.Add(GetTileData(flippedGx, gy)); } } int idx = 0; for (int gx = MinGridX; gx <= MaxGridX; ++gx) { for (int gy = MinGridY; gy <= MaxGridY; ++gy, ++idx) { uint flippedTileData = flippedList[idx]; if ( changeFlags && (flippedTileData != Tileset.k_TileData_Empty) && (flippedTileData & Tileset.k_TileDataMask_BrushId) == 0 // don't activate flip flags on brushes ) { flippedTileData = TilesetBrush.ApplyAndMergeTileFlags(flippedTileData, Tileset.k_TileFlag_FlipH); } SetTileData(gx, gy, flippedTileData); } } }
/// <summary> /// Rotate the map 90 degrees clockwise /// </summary> /// <param name="changeFlags"></param> public void Rot90(bool changeFlags) { List <uint> flippedList = new List <uint>(GridWidth * GridHeight); for (int gy = MinGridY; gy <= MaxGridY; ++gy) { for (int gx = MinGridX; gx <= MaxGridX; ++gx) { flippedList.Add(GetTileData(gx, gy)); } } int minGridX = MinGridX; int minGridY = MinGridY; int maxGridX = MaxGridY; int maxGridY = MaxGridX; ClearMap(); int idx = 0; for (int gx = minGridX; gx <= maxGridX; ++gx) { for (int gy = maxGridY; gy >= minGridY; --gy, ++idx) { uint flippedTileData = flippedList[idx]; if ( changeFlags && (flippedTileData != Tileset.k_TileData_Empty) && (flippedTileData & Tileset.k_TileDataMask_BrushId) == 0 // don't activate flip flags on brushes ) { flippedTileData = TilesetBrush.ApplyAndMergeTileFlags(flippedTileData, Tileset.k_TileFlag_Rot90); } SetTileData(gx, gy, flippedTileData); } } }
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); } }
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) { tileData = TilesetBrush.ApplyAndMergeTileFlags(brush.PreviewTileData(), tileData); } 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); } }