[Test] public void Alignment() { Assert.AreEqual(new Rect(-5, -5, 10, 10), Rect.AlignCenter(0, 0, 10, 10)); Assert.AreEqual(new Rect(0, -5, 10, 10), Rect.AlignLeft(0, 0, 10, 10)); Assert.AreEqual(new Rect(-10, -5, 10, 10), Rect.AlignRight(0, 0, 10, 10)); Assert.AreEqual(new Rect(-5, 0, 10, 10), Rect.AlignTop(0, 0, 10, 10)); Assert.AreEqual(new Rect(-5, -10, 10, 10), Rect.AlignBottom(0, 0, 10, 10)); Assert.AreEqual(new Rect(0, 0, 10, 10), Rect.AlignTopLeft(0, 0, 10, 10)); Assert.AreEqual(new Rect(-10, 0, 10, 10), Rect.AlignTopRight(0, 0, 10, 10)); Assert.AreEqual(new Rect(0, -10, 10, 10), Rect.AlignBottomLeft(0, 0, 10, 10)); Assert.AreEqual(new Rect(-10, -10, 10, 10), Rect.AlignBottomRight(0, 0, 10, 10)); }
private void DrawRows(Rect rect, IPropertyValueEntry <TArray> entry, Context context, int rowIndex) { if (rowIndex < context.RowCount) { GUI.Label(rect, rowIndex.ToString(), SirenixGUIStyles.LabelCentered); // Handle Row dragging. if (!context.Attribute.IsReadOnly) { var id = GUIUtility.GetControlID(FocusType.Passive); if (GUI.enabled && Event.current.type == EventType.MouseDown && Event.current.button == 0 && rect.Contains(Event.current.mousePosition)) { GUIHelper.RemoveFocusControl(); GUIUtility.hotControl = id; EditorGUIUtility.SetWantsMouseJumping(1); Event.current.Use(); context.RowDragFrom = rowIndex; context.RowDragTo = rowIndex; context.dragStartPos = Event.current.mousePosition; } else if (GUIUtility.hotControl == id) { if ((context.dragStartPos - Event.current.mousePosition).sqrMagnitude > 5 * 5) { context.IsDraggingRow = true; } if (Event.current.type == EventType.MouseDrag) { Event.current.Use(); } else if (Event.current.type == EventType.MouseUp) { GUIUtility.hotControl = 0; EditorGUIUtility.SetWantsMouseJumping(0); Event.current.Use(); context.IsDraggingRow = false; ApplyArrayModifications(entry, arr => MultiDimArrayUtilities.MoveRow(arr, context.RowDragFrom, context.RowDragTo)); } } if (context.IsDraggingRow && Event.current.type == EventType.Repaint) { float mouseY = Event.current.mousePosition.y; if (mouseY > rect.y - 1 && mouseY < rect.y + rect.height + 1) { Rect arrowRect; if (mouseY > rect.y + rect.height * 0.5f) { arrowRect = rect.AlignBottom(16); arrowRect.width = 16; arrowRect.y += 8; arrowRect.x -= 13; context.RowDragTo = rowIndex; } else { arrowRect = rect.AlignTop(16); arrowRect.width = 16; arrowRect.y -= 8; arrowRect.x -= 13; context.RowDragTo = rowIndex - 1; } entry.Property.Tree.DelayActionUntilRepaint(() => { //GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear; GUI.DrawTexture(arrowRect, EditorIcons.ArrowRight.Active); //GL.sRGBWrite = false; var lineRect = arrowRect; lineRect.y = lineRect.center.y - 2 + 1; lineRect.height = 3; lineRect.x += 14; lineRect.xMax = context.Table.TableRect.xMax; EditorGUI.DrawRect(lineRect, new Color(0, 0, 0, 0.6f)); }); } if (rowIndex == context.RowCount - 1) { entry.Property.Tree.DelayActionUntilRepaint(() => { var cell = context.Table[context.Table.ColumnCount - 1, context.Table.RowCount - context.RowCount + context.RowDragFrom]; var rowRect = cell.Rect; rowRect.xMin = rect.xMin; SirenixEditorGUI.DrawSolidRect(rowRect, new Color(0, 0, 0, 0.2f)); }); } } } } else { GUI.Label(rect, "...", EditorStyles.centeredGreyMiniLabel); } if (!context.Attribute.IsReadOnly && Event.current.type == EventType.MouseDown && Event.current.button == 1 && rect.Contains(Event.current.mousePosition)) { Event.current.Use(); GenericMenu menu = new GenericMenu(); menu.AddItem(new GUIContent("Insert 1 above"), false, () => ApplyArrayModifications(entry, arr => MultiDimArrayUtilities.InsertOneRowAbove(arr, rowIndex))); menu.AddItem(new GUIContent("Insert 1 below"), false, () => ApplyArrayModifications(entry, arr => MultiDimArrayUtilities.InsertOneRowBelow(arr, rowIndex))); menu.AddItem(new GUIContent("Duplicate"), false, () => ApplyArrayModifications(entry, arr => MultiDimArrayUtilities.DuplicateRow(arr, rowIndex))); menu.AddSeparator(""); menu.AddItem(new GUIContent("Delete"), false, () => ApplyArrayModifications(entry, arr => MultiDimArrayUtilities.DeleteRow(arr, rowIndex))); menu.ShowAsContext(); } }
private void Game(GameContext game) { Rect rect = EditorGUILayout.GetControlRect(true, TileSize * BoardSize + 20); rect = rect.AlignCenter(TileSize * BoardSize); // Toolbar { SirenixEditorGUI.DrawSolidRect(rect.AlignTop(20), new Color(0.5f, 0.5f, 0.5f, 1f)); SirenixEditorGUI.DrawBorders(rect.AlignTop(20).SetHeight(21).SetWidth(rect.width + 1), 1); if (Event.current.type == EventType.Repaint && !game.GameOver) { double t = EditorApplication.timeSinceStartup; game.Time += t - game.PrevTime; game.PrevTime = t; } var time = GUIHelper.TempContent(((int)game.Time).ToString()); GUIHelper.PushContentColor(Color.black); GUI.Label(rect.AlignTop(20).HorizontalPadding(4).AlignMiddle(18).AlignRight(EditorStyles.label.CalcSize(time).x), time); GUIHelper.PopContentColor(); GUI.Label(rect.AlignTop(20).AlignCenter(20), EditorIcons.PacmanGhost.ActiveGUIContent); if (game.GameOver) { GUIHelper.PushContentColor(game.FlaggedBombs == game.NumberOfBombs ? Color.green : Color.red); GUI.Label(rect.AlignTop(20).HorizontalPadding(4).AlignMiddle(18), game.FlaggedBombs == game.NumberOfBombs ? "You win!" : "Game over!"); GUIHelper.PopContentColor(); } } rect = rect.AlignBottom(rect.height - 20); SirenixEditorGUI.DrawSolidRect(rect, new Color(0.7f, 0.7f, 0.7f, 1f)); for (int i = 0; i < BoardSize * BoardSize; i++) { Rect tileRect = rect.SplitGrid(TileSize, TileSize, i); SirenixEditorGUI.DrawBorders(tileRect.SetWidth(tileRect.width + 1).SetHeight(tileRect.height + 1), 1); int x = i % BoardSize; int y = i / BoardSize; var tile = game.Tiles[x, y]; var visible = game.VisibleTiles[x, y]; if (game.GameOver || visible == Tile.Open) { SirenixEditorGUI.DrawSolidRect(new Rect(tileRect.x + 1, tileRect.y + 1, tileRect.width - 1, tileRect.height - 1), new Color(0.3f, 0.3f, 0.3f, 1f)); } if ((game.GameOver || visible == Tile.Open) && tile == Tile.Bomb) { GUIHelper.PushColor(visible == Tile.Flag ? Color.black : Color.white); GUI.Label(tileRect.AlignCenter(18).AlignMiddle(18), EditorIcons.SettingsCog.ActiveGUIContent); GUIHelper.PopColor(); } if (visible == Tile.Flag) { GUIHelper.PushColor(Color.red); GUI.Label(tileRect.AlignCenter(18).AlignMiddle(18), EditorIcons.Flag.ActiveGUIContent); GUIHelper.PopColor(); } if ((game.GameOver || visible == Tile.Open) && (int)tile >= 1 && (int)tile <= 8) { GUIHelper.PushColor(this.NumberColors[(int)tile - 1]); GUI.Label(tileRect.AlignCenter(18).AlignCenter(18).AddX(2).AddY(2), ((int)tile).ToString(), EditorStyles.boldLabel); GUIHelper.PopColor(); } if (!game.GameOver && tileRect.Contains(Event.current.mousePosition)) { SirenixEditorGUI.DrawSolidRect(new Rect(tileRect.x + 1, tileRect.y + 1, tileRect.width - 1, tileRect.height - 1), new Color(0f, 1f, 0f, 0.3f)); // Input // Reveal if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { if (visible != Tile.Flag) { if (tile == Tile.Bomb) { // LOSE game.GameOver = true; SirenixEditorGUI.StartShakingGroup(game.Key, 3f); } else { this.Reveal(game, x, y); } } Event.current.Use(); } // Place flag else if (Event.current.type == EventType.MouseDown && Event.current.button == 1) { if (visible == Tile.None) { game.VisibleTiles[x, y] = Tile.Flag; if (tile == Tile.Bomb) { game.FlaggedBombs++; if (game.FlaggedBombs == game.NumberOfBombs) { game.GameOver = true; } } } else if (visible == Tile.Flag) { game.VisibleTiles[x, y] = Tile.None; if (tile == Tile.Bomb) { game.FlaggedBombs--; } } Event.current.Use(); } } } GUIHelper.RequestRepaint(); }
protected override void OnGUI() { _repaintSheduled = false; GUILayout.BeginHorizontal(); if (GUILayout.Button("Clear")) { _properties.Clear(); } Rect settingsRect = GUILayoutUtility.GetRect(24, 24, GUILayout.ExpandWidth(false)).AlignLeft(20).AlignCenterY(20); if (SirenixEditorGUI.IconButton(settingsRect, _showSettings ? EditorIcons.SettingsCog.Inactive : EditorIcons.SettingsCog.Active, "Settings")) { _showSettings = !_showSettings; } GUILayout.EndHorizontal(); if (_showSettings) { GUILayout.BeginHorizontal(); GUILayout.Space(40); GUI.changed = false; Rect rect = GUILayoutUtility.GetRect(1, EditorGUIUtility.singleLineHeight, GUILayout.ExpandWidth(true)); _labelWidth = GUI.HorizontalSlider(rect, _labelWidth, rect.xMin, rect.xMax); if (GUI.changed) { EditorPrefs.SetFloat("OWW_labelWidth", _labelWidth); } EditorGUILayout.LabelField("Label Width", GUILayout.Width(70)); GUILayout.EndHorizontal(); } GUILayout.Space(5); bool first = true; if (_properties.Count == 0) { EditorGUILayout.LabelField("Right-click any property in an Inspector and select 'Watch' to make it show up here.", SirenixGUIStyles.MultiLineCenteredLabel); } GUIHelper.PushLabelWidth(_labelWidth - 30); for (int i = 0; i < _properties.Count; i++) { TreeValuesHolder holder = _properties[i]; holder.CheckRefresh(); if (!first) { GUILayout.Space(5); } first = false; Rect titleRect = SirenixEditorGUI.BeginBox(" " + holder.Tree.TargetType.Name); titleRect = titleRect.AlignTop(21); if (holder.ParentObject != null) { Rect alignRight = titleRect.AlignRight(200).AlignCenterY(16).AlignLeft(180); GUIHelper.PushGUIEnabled(false); SirenixEditorFields.UnityObjectField(alignRight, holder.ParentObject, typeof(GameObject), true); GUIHelper.PopGUIEnabled(); } if (SirenixEditorGUI.IconButton(titleRect.AlignRight(20).AlignCenterY(18), EditorIcons.X)) { _properties.RemoveAt(i--); } Rect titleDragDropRect = titleRect.AlignLeft(30).AlignCenter(20, 20); EditorIcons.List.Draw(titleDragDropRect); TreeValuesHolder treedragdrop = (TreeValuesHolder)DragAndDropUtilities.DragAndDropZone(titleDragDropRect, holder, typeof(TreeValuesHolder), false, false); if (treedragdrop != holder) { int treeDragDropIndex = _properties.IndexOf(treedragdrop); Swap(_properties, treeDragDropIndex, i); } if (holder.Tree.UnitySerializedObject?.targetObject == null) { EditorGUILayout.LabelField($"This component is no longer valid in the current context (loaded different scene?)", SirenixGUIStyles.MultiLineLabel); } else { InspectorUtilities.BeginDrawPropertyTree(holder.Tree, true); for (int index = 0; index < holder.ValuePaths.Count; index++) { string path = holder.ValuePaths[index]; GUILayout.BeginHorizontal(); Rect rect1 = GUILayoutUtility.GetRect(EditorGUIUtility.singleLineHeight + 5, EditorGUIUtility.singleLineHeight + 3, GUILayout.ExpandWidth(false)).AlignRight(EditorGUIUtility.singleLineHeight + 2); EditorIcons.List.Draw(rect1); ValueDragDropHolder dragdrop = (ValueDragDropHolder)DragAndDropUtilities.DragAndDropZone(rect1, new ValueDragDropHolder(holder, index), typeof(ValueDragDropHolder), false, false); if (dragdrop.TreeValuesHolder == holder && dragdrop.Index != index) { string ptemp = holder.ValuePaths[index]; holder.ValuePaths[index] = holder.ValuePaths[dragdrop.Index]; holder.ValuePaths[dragdrop.Index] = ptemp; } InspectorProperty propertyAtPath = holder.Tree.GetPropertyAtPath(path); if (propertyAtPath == null) { propertyAtPath = holder.Tree.GetPropertyAtUnityPath(path); } if (propertyAtPath != null) { propertyAtPath.Draw(); } else { EditorGUILayout.LabelField($"Could not find property ({path})"); } if (SirenixEditorGUI.IconButton(EditorIcons.X)) { holder.ValuePaths.RemoveAt(index--); if (holder.ValuePaths.Count == 0) { _properties.RemoveAt(i--); } } GUILayout.Space(3); GUILayout.EndHorizontal(); } InspectorUtilities.EndDrawPropertyTree(holder.Tree); } SirenixEditorGUI.EndBox(); } GUIHelper.PopLabelWidth(); }