void drawCell(Cell c, float gridSize, float boardSize, GUIStyle style) { float scaledSize = gridSize * c.scale; float diffSize = scaledSize - gridSize; float x = 12 + c.currentX * gridSize + ((gridSize - 5) / 2.0f) - ((scaledSize - 5) / 2.0f); float y = 62 + boardSize - (c.currentY + 1) * gridSize + ((gridSize - 5) / 2.0f) - ((scaledSize - 5) / 2.0f); Rect rect = new Rect(x, y, scaledSize - 5, scaledSize - 5); GUI.color = Setting2048.GetColor(c.value); GUI.Button(rect, c.value.ToString(), style); }
void OnGUI() { //Game informations EditorGUI.LabelField(new Rect(10, 10, 60, 25), "SCORE"); EditorGUI.LabelField(new Rect(60, 10, 80, 20), currentScores.ToString(), GUI.skin.button); EditorGUI.LabelField(new Rect(10, 30, 60, 25), "BEST"); EditorGUI.LabelField(new Rect(60, 30, 80, 20), Setting2048.BestScores.ToString(), GUI.skin.button); EditorGUI.LabelField(new Rect(160, 10, 60, 25), "GOAL"); EditorGUI.LabelField(new Rect(200, 10, 80, 20), goal.ToString(), GUI.skin.box); EditorGUI.LabelField(new Rect(160, 30, 100, 25), "RANDOM SEED"); isRandomSeed = EditorGUI.Toggle(new Rect(260, 30, 15, 25), isRandomSeed); if (isRandomSeed) { if (GUI.Button(new Rect(280, 30, 100, 20), randomSeed.ToString())) { System.Random r = new System.Random(); randomSeed = r.Next(); } } else { randomSeed = EditorGUI.IntField(new Rect(280, 30, 100, 20), randomSeed); } if (GUI.Button(new Rect(300, 10, 80, 20), "New Game")) { ResetBoard(); addNewCell(); } if (GUI.Button(new Rect(400, 10, 80, 20), "Back")) { Move(KeyCode.Backspace); } //Save default gui color Color defaultGuiColor = GUI.color; //Calculate size float topSection = 60; float paddingTop = 10; float paddingLeft = 20; float maxBoardSize = (position.width > position.height - topSection) ? position.height - (topSection + paddingTop) : position.width - paddingLeft; float gridSize = maxBoardSize / (float)size; //According to editor window's size to draw board. drawBoard(size, gridSize); drawKeyRecords(size, gridSize, keyRecords); //Actions HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); Event e = Event.current; if ((e.type == EventType.keyUp) && (e.keyCode == KeyCode.UpArrow || e.keyCode == KeyCode.DownArrow || e.keyCode == KeyCode.LeftArrow || e.keyCode == KeyCode.RightArrow)) { Move(e.keyCode); } //Merged cells behind moving cells. foreach (Cell c in mergedCells) { drawCell(c, gridSize, maxBoardSize, Setting2048.GetCellStyle()); } //Create a sorted list, make the large number cells are above small one. List <Cell> list = (from row in board from cell in row where cell != null orderby cell.value ascending select cell).ToList(); foreach (Cell c in list) { drawCell(c, gridSize, maxBoardSize, Setting2048.GetCellStyle()); } //Return gui color GUI.color = defaultGuiColor; //Gameover mask drawGameOverMask(size, gridSize); }