From a447a350a732d1ff070ae0cb7ce2e3bda3bc8f3f Mon Sep 17 00:00:00 2001 From: "DESKTOP-B4ME9GF\\Joshua Reason" Date: Wed, 1 Aug 2018 16:51:47 +1000 Subject: [PATCH] Made the clicked cell also use styles --- .../MineSweeper/MineSweeperEditor.cs | 21 ++-- .../MineSweeper/MineSweeperUtil.cs | 106 ++++++++++++++++++ 2 files changed, 113 insertions(+), 14 deletions(-) diff --git a/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs b/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs index 3f2d6bc..840b454 100644 --- a/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs +++ b/Assets/Editor Add On/MineSweeper/MineSweeperEditor.cs @@ -56,6 +56,8 @@ public class MineSweeperEditor : EditorWindow { public static Color getColor(int count){ switch (count) { + case 0: + return new Color(0.75f, 0.75f, 0.75f); case 1: return Color.blue; case 2: @@ -102,9 +104,6 @@ public class Cell { public void DrawCell() { - GUIStyle style = new GUIStyle(); - style.alignment = TextAnchor.MiddleCenter; - if (!isOpen) { if (isMarked) GUI.Label(rect, "!",MineSweeperConstants.STYLE_CELL_BUTTON); @@ -114,14 +113,12 @@ public class Cell { } } else { - GUI.DrawTexture(rect, MineSweeperConstants.CELL_CLICKED); if (containsMine) - GUI.Label(rect, "X",style); - else if (adjacentMines > 0) { - style.normal.textColor = MineSweeperEditor.getColor(adjacentMines); - - GUI.Label(rect,adjacentMines.ToString(),style); - //GUI.contentColor = Color.black; + GUI.Label(rect, "X",MineSweeperConstants.STYLE_CELL_CLICKED); + else { + MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = MineSweeperEditor.getColor(adjacentMines); + GUI.Label(rect,adjacentMines.ToString(), MineSweeperConstants.STYLE_CELL_CLICKED); + MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = Color.black; } } } @@ -133,11 +130,7 @@ public class Cell { DoLeftClick(); else if (button == 1) DoRightClick(); - - //EditorUtility.SetDirty(editor); - } - Debug.Log("MousePos: " + mousePos); } private void DoLeftClick() diff --git a/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs index a705955..020cf57 100644 --- a/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs +++ b/Assets/Editor Add On/MineSweeper/MineSweeperUtil.cs @@ -9,6 +9,7 @@ public static class MineSweeperConstants private static Texture2D Cell_Button; private static Texture2D Cell_Clicked; private static GUIStyle Button_Style; + private static GUIStyle Clicked_Style; /// /// Height and Width of a Cell in Pixels @@ -67,6 +68,24 @@ public static class MineSweeperConstants } } + /// + /// Style to use with a Cell_Button + /// + public static GUIStyle STYLE_CELL_CLICKED + { + get + { + if (Clicked_Style == null) + { + Debug.Log("Creating Cell Style Clicked"); + Clicked_Style = new GUIStyle(); + Clicked_Style.alignment = TextAnchor.MiddleCenter; + Clicked_Style.normal.background = CELL_CLICKED; + } + return Clicked_Style; + } + } + private static Texture2D CreateButtonTexture(Color highLight, Color mainColor, Color lowLight) { Texture2D retval = new Texture2D(CELL_SIZE, CELL_SIZE); @@ -167,6 +186,93 @@ public class Board } +public class Cell +{ + public enum Mark { None,Flag,Question} + + public bool isClicked = false; + public bool containsMine = false; + public int adjacentMines = 0; + + public Board board; + private Rect rect; + + Vector2Int coords; + public bool isMarked = false; + + + public Cell(Board board, Vector2Int coords) + { + this.board = board; + this.coords = coords; + + rect = new Rect(coords.x * MineSweeperConstants.CELL_SIZE + 1, coords.y * MineSweeperConstants.CELL_SIZE + 1, MineSweeperConstants.CELL_SIZE, MineSweeperConstants.CELL_SIZE); + } + + public void DrawCell() + { + if (!isClicked) + { + if (isMarked) + GUI.Label(rect, "!", MineSweeperConstants.STYLE_CELL_BUTTON); + else + { + GUI.Label(rect, "", MineSweeperConstants.STYLE_CELL_BUTTON); + } + + } + else + { + if (containsMine) + GUI.Label(rect, "X", MineSweeperConstants.STYLE_CELL_CLICKED); + else + { + MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = MineSweeperEditor.getColor(adjacentMines); + GUI.Label(rect, adjacentMines.ToString(), MineSweeperConstants.STYLE_CELL_CLICKED); + MineSweeperConstants.STYLE_CELL_CLICKED.normal.textColor = Color.black; + } + } + } + + public void OnClick(int button, Vector2 mousePos) + { + if (rect.Contains(mousePos)) + { + if (button == 0) + DoLeftClick(); + else if (button == 1) + DoRightClick(); + } + } + + private void DoLeftClick() + { + if (isClicked) + return; + + isClicked = true; + + Cell[] adjacentCells = board.AdjacentCells(coords); + + foreach (Cell cell in adjacentCells) + { + if (cell.containsMine) + adjacentMines++; + } + + if (adjacentMines == 0 && !containsMine) + foreach (Cell cell in adjacentCells) + if (!cell.isClicked) + cell.DoLeftClick(); + + } + + private void DoRightClick() + { + isMarked = !isMarked; + } +} + public struct Vector2Int { public int x;