Example #1
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            if (!AttributesMaster.attributesEnabled)
            {
                EditorGUI.HelpBox(position, "To enable attribute specific behaviours, create an entity attribute enum and decorate it with the EntityAttributesEnum.", MessageType.Info);
                return;
            }

            var attrib = this.attribute as AttributePropertyAttribute;

            //For some reason using the passed in label directly results in other properties that have no tooltip to inherit the last tooltip set.
            label = new GUIContent(label.text, label.tooltip);

            if (!string.IsNullOrEmpty(attrib.label))
            {
                label.text = attrib.label;
            }

            if (!string.IsNullOrEmpty(attrib.tooltip))
            {
                label.tooltip = attrib.tooltip;
            }

            EditorUtilitiesInternal.EnumToIntField(position, property, AttributesMaster.attributesEnumType, label);
        }
        private static void BakeGrid(GridComponent g)
        {
            var builder = g.GetBuilder();

            var matrix = CellMatrix.Create(builder);

            var data = g.bakedData;

            if (data == null)
            {
                data = CellMatrixData.Create(matrix);

                g.bakedData = data;
            }
            else
            {
                data.Refresh(matrix);
            }

            if (g.storeBakedDataAsAsset)
            {
                EditorUtilitiesInternal.CreateOrUpdateAsset(data, g.friendlyName.Trim());
            }
            else
            {
                EditorUtility.SetDirty(data);
            }

            g.ResetGrid();
            EditorUtility.SetDirty(g);

            Debug.Log(string.Format("The grid {0} was successfully baked.", g.friendlyName));
        }
Example #3
0
        private void HandleNormal(Bounds portalBounds, GridPortalComponent p)
        {
            var grid = GridManager.instance.GetGridComponent(_portalRectStart);

            if (grid == null)
            {
                grid = GridManager.instance.GetGridComponent(_portalRectEnd);
                if (grid == null)
                {
                    return;
                }
            }

            portalBounds = EditorUtilitiesInternal.SnapToGrid(grid, portalBounds, _gridConnectMode);

            if (p.relativeToTransform)
            {
                portalBounds.center = p.transform.InverseTransformPoint(portalBounds.center);
            }

            if (_shiftDown)
            {
                p.portalTwo = portalBounds;
            }
            else
            {
                p.portalOne = portalBounds;
            }
        }
Example #4
0
        private void HandleConnector(Bounds portalBounds, GridPortalComponent p)
        {
            var g1 = GridManager.instance.GetGridComponent(_portalRectStart);
            var g2 = GridManager.instance.GetGridComponent(_portalRectEnd);

            if (g1 != null && g2 != null && g1 != g2)
            {
                var p1 = EditorUtilitiesInternal.SnapToGridEdge(g1, portalBounds, _gridConnectMode);
                var p2 = EditorUtilitiesInternal.SnapToGridEdge(g2, portalBounds, _gridConnectMode);

                if (p.relativeToTransform)
                {
                    p1.center = p.transform.InverseTransformPoint(p1.center);
                    p2.center = p.transform.InverseTransformPoint(p2.center);
                }

                p.portalOne = p1;
                p.portalTwo = p2;
            }
            else
            {
                HandleNormal(portalBounds, p);
                EnsureValidConnector(p);
            }
        }
        public override void OnInspectorGUI()
        {
            GUI.enabled = !EditorApplication.isPlaying;

            this.serializedObject.Update();

            int baked         = 0;
            var editedObjects = this.serializedObject.targetObjects;

            for (int i = 0; i < editedObjects.Length; i++)
            {
                var g = editedObjects[i] as GridComponent;
                if (g.bakedData != null)
                {
                    baked++;
                }
            }

            EditorGUILayout.Separator();

            if (baked > 0 && baked < editedObjects.Length)
            {
                EditorGUILayout.LabelField("A mix of baked and unbaked grids cannot be edited at the same time.");
                return;
            }

            //If data is baked, only offer an option to edit or rebake
            if (baked == editedObjects.Length)
            {
                EditorGUILayout.LabelField("The grid has been baked. To change it press the Edit button below.");

                GUILayout.BeginHorizontal();

                if (GUILayout.Button("Edit"))
                {
                    foreach (var o in editedObjects)
                    {
                        var g = o as GridComponent;
                        EditorUtilitiesInternal.RemoveAsset(g.bakedData);
                        g.bakedData = null;
                        g.ResetGrid();
                        EditorUtility.SetDirty(g);
                    }
                }

                if (GUILayout.Button("Re-bake Grid"))
                {
                    foreach (var o in editedObjects)
                    {
                        var g = o as GridComponent;
                        BakeGrid(g);
                    }
                }

                GUILayout.EndHorizontal();
                return;
            }

            EditorGUILayout.PropertyField(_friendlyName);

            EditorUtilities.Section("Layout");

            EditorGUILayout.PropertyField(_linkOriginToTransform);

            if (!_linkOriginToTransform.hasMultipleDifferentValues)
            {
                if (_linkOriginToTransform.boolValue)
                {
                    EditorGUI.indentLevel++;
                    EditorGUILayout.PropertyField(_originOffset, true);
                    EditorGUI.indentLevel--;
                }
                else
                {
                    EditorGUILayout.PropertyField(_origin, true);
                }
            }

            EditorGUILayout.PropertyField(_sizeX);
            EditorGUILayout.PropertyField(_sizeZ);
            EditorGUILayout.PropertyField(_cellSize);
            EditorGUILayout.PropertyField(_lowerBoundary);
            EditorGUILayout.PropertyField(_upperBoundary);
            EditorGUILayout.PropertyField(_obstacleSensitivityRange);
            EditorGUILayout.PropertyField(_obstacleAndGroundDetection);

            EditorUtilities.Section("Subsections");

            EditorGUILayout.PropertyField(_subSectionsX);
            EditorGUILayout.PropertyField(_subSectionsZ);
            EditorGUILayout.PropertyField(_subSectionsCellOverlap);

            ShowHeightMapOptions();

            EditorUtilities.Section("Initialization");
            EditorGUILayout.PropertyField(_automaticInitialization);
            EditorGUILayout.PropertyField(_automaticConnections);
            if (_automaticConnections.boolValue)
            {
                EditorGUILayout.PropertyField(_connectorPortalWidth);
            }

            EditorGUILayout.Separator();
            EditorGUILayout.PropertyField(_storeBakedDataAsAsset);

            this.serializedObject.ApplyModifiedProperties();

            if (GUILayout.Button(new GUIContent("Bake Grid", "Calculates grid data such as blocked areas and height map and stores this snapshot. The snapshot is then used to initialize the grid at runtime.\nPlease note that baking is completely optional.")))
            {
                foreach (var o in editedObjects)
                {
                    var g = o as GridComponent;
                    BakeGrid(g);
                }
            }

            GUI.enabled = true;
        }
Example #6
0
        private void HandlePortalDefinition()
        {
            if (!_inPortalMode)
            {
                return;
            }

            var p          = this.target as GridPortalComponent;
            int id         = GUIUtility.GetControlID(_idHash, FocusType.Passive);
            var groundRect = new Plane(Vector3.up, new Vector3(0f, 0f, 0f));

            var evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0)
            {
                GUIUtility.hotControl = id;

                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _portalRectStart))
                {
                    GUIUtility.hotControl = 0;
                }

                _shiftDown       = (evt.modifiers & EventModifiers.Shift) > 0;
                _gridConnectMode = (evt.modifiers & EventModifiers.Control) > 0 || (evt.modifiers & EventModifiers.Command) > 0;
                _portalRectEnd   = _portalRectStart;
                return;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = id;
                evt.Use();
                return;
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                ToggleEditMode(false);
                this.Repaint();

                return;
            }
            else if (GUIUtility.hotControl != id)
            {
                if (evt.type == EventType.Repaint)
                {
                    DrawActiveIndication(p);
                }

                return;
            }

            if (evt.type == EventType.MouseDrag)
            {
                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _portalRectEnd))
                {
                    _portalRectEnd = _portalRectStart;
                }
            }
            else if (evt.type == EventType.MouseUp)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                var startToEnd   = (_portalRectEnd - _portalRectStart);
                var portalBounds = new Bounds(
                    _portalRectStart + (startToEnd * 0.5f),
                    new Vector3(Mathf.Abs(startToEnd.x), Mathf.Max(Mathf.Abs(startToEnd.y), 0.1f), Mathf.Abs(startToEnd.z)));

                _gridConnectMode = _gridConnectMode || (evt.modifiers & EventModifiers.Control) > 0 || (evt.modifiers & EventModifiers.Command) > 0;
                _shiftDown       = _shiftDown || (evt.modifiers & EventModifiers.Shift) > 0;

                if (p.type == PortalType.Connector)
                {
                    HandleConnector(portalBounds, p);
                }
                else
                {
                    HandleNormal(portalBounds, p);
                }

                EditorUtility.SetDirty(p);
            }
            else if (evt.type == EventType.Repaint)
            {
                Handles.color = _shiftDown ? p.portalTwoColor : p.portalOneColor;
                var y  = Mathf.Max(_portalRectStart.y, _portalRectEnd.y);
                var c1 = new Vector3(_portalRectStart.x, y, _portalRectEnd.z);
                var c2 = new Vector3(_portalRectEnd.x, y, _portalRectStart.z);
                Handles.DrawDottedLine(_portalRectStart, c1, 10f);
                Handles.DrawDottedLine(c1, _portalRectEnd, 10f);
                Handles.DrawDottedLine(_portalRectEnd, c2, 10f);
                Handles.DrawDottedLine(c2, _portalRectStart, 10f);
            }
        }
        private void OnSceneGUI()
        {
            var evt = Event.current;
            var p   = this.target as PatrolPointsComponent;

            if (evt.type == EventType.Repaint)
            {
                if (_emphasize && GUIUtility.hotControl != _id)
                {
                    DrawActiveIndication(p);
                }

                DrawNumberLabels(p);
                return;
            }

            if (!_inPlacementMode)
            {
                return;
            }

            var points     = p.points;
            var groundRect = new Plane(Vector3.up, Vector3.zero);

            if (evt.type == EventType.KeyDown && evt.keyCode == _addKey)
            {
                GUIUtility.hotControl = _id;

                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == _addKey)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    GUIUtility.hotControl = 0;
                    return;
                }

                if (p.relativeToTransform)
                {
                    point = p.transform.InverseTransformPoint(point);
                }

                int idx = points.Length;

                var tmp = new Vector3[idx + 1];
                Array.Copy(points, tmp, points.Length);
                points   = tmp;
                p.points = tmp;

                points[idx] = point;
                EditorUtility.SetDirty(p);
                GUIUtility.hotControl = 0;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == _removeKey)
            {
                GUIUtility.hotControl = _id;

                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == _removeKey)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    GUIUtility.hotControl = 0;
                    return;
                }

                var removeIdx  = -1;
                var lineOrigin = Camera.current.transform.position;

                for (int i = 0; i < points.Length; i++)
                {
                    //If Gizmos change so should this
                    var sphereCenter = points[i] + Vector3.up;
                    if (Geometry.DoesLineIntersectSphere(lineOrigin, point, sphereCenter, 0.3f))
                    {
                        removeIdx = i;
                        break;
                    }
                }

                if (removeIdx >= 0)
                {
                    var tmp = new Vector3[points.Length - 1];
                    Array.Copy(points, tmp, removeIdx);
                    Array.Copy(points, removeIdx + 1, tmp, removeIdx, points.Length - (removeIdx + 1));
                    points   = tmp;
                    p.points = tmp;

                    EditorUtility.SetDirty(p);
                }

                GUIUtility.hotControl = 0;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = _id;
                evt.Use();
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                _inPlacementMode = false;
                this.Repaint();
            }
            else if (evt.control)
            {
                return;
            }
            else if (evt.type == EventType.MouseDown && evt.button == MouseButton.left)
            {
                GUIUtility.hotControl = _id;
                evt.Use();
                _movingIdx = -1;

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    return;
                }

                var lineOrigin = Camera.current.transform.position;

                for (int i = 0; i < points.Length; i++)
                {
                    //If Gizmos change so should this
                    var sphereCenter = points[i] + Vector3.up;
                    if (Geometry.DoesLineIntersectSphere(lineOrigin, point, sphereCenter, 0.3f))
                    {
                        _movingIdx = i;
                        break;
                    }
                }
            }
            else if (evt.type == EventType.MouseUp && evt.button == MouseButton.left)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                if (_movingIdx >= 0)
                {
                    EditorUtility.SetDirty(p);
                    _movingIdx = -1;
                }
            }
            else if (evt.type == EventType.MouseDrag && evt.button == MouseButton.left && _movingIdx >= 0)
            {
                evt.Use();

                Vector3 point;
                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out point))
                {
                    return;
                }

                points[_movingIdx] = point;
            }
        }
Example #8
0
        private void HandleFieldDefinition()
        {
            if (!_inDrawMode)
            {
                return;
            }

            var p          = this.target as SimpleCostField;
            int id         = GUIUtility.GetControlID(_idHash, FocusType.Passive);
            var groundRect = new Plane(Vector3.up, new Vector3(0f, 0f, 0f));

            var evt = Event.current;

            if (evt.type == EventType.MouseDown && evt.button == 0)
            {
                GUIUtility.hotControl = id;

                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _boundsRectStart))
                {
                    GUIUtility.hotControl = 0;
                }

                _boundsRectEnd = _boundsRectStart;
                return;
            }
            else if (evt.type == EventType.KeyDown && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = id;
                evt.Use();
                return;
            }
            else if (evt.type == EventType.KeyUp && evt.keyCode == KeyCode.Escape)
            {
                GUIUtility.hotControl = 0;
                evt.Use();
                _inDrawMode = false;
                this.Repaint();

                return;
            }
            else if (GUIUtility.hotControl != id)
            {
                return;
            }

            if (evt.type == EventType.MouseDrag)
            {
                evt.Use();

                if (!EditorUtilitiesInternal.MouseToWorldPoint(groundRect, out _boundsRectEnd))
                {
                    _boundsRectEnd = _boundsRectStart;
                }
            }
            else if (evt.type == EventType.MouseUp)
            {
                GUIUtility.hotControl = 0;
                evt.Use();

                _boundsRectStart.y = _boundsRectEnd.y = Mathf.Max(_boundsRectStart.y, _boundsRectEnd.y);

                var grid = GridManager.instance.GetGridComponent(_boundsRectStart);
                if (grid == null)
                {
                    grid = GridManager.instance.GetGridComponent(_boundsRectEnd);
                    if (grid == null)
                    {
                        return;
                    }
                }

                var startToEnd  = (_boundsRectEnd - _boundsRectStart);
                var fieldBounds = new Bounds(
                    _boundsRectStart + (startToEnd * 0.5f),
                    new Vector3(Mathf.Abs(startToEnd.x), Mathf.Abs(startToEnd.y) + 0.1f, Mathf.Abs(startToEnd.z)));

                fieldBounds = EditorUtilitiesInternal.SnapToGrid(grid, fieldBounds, false);

                if (p.relativeToTransform)
                {
                    fieldBounds.center = p.transform.InverseTransformPoint(fieldBounds.center);
                }

                p.bounds = fieldBounds;

                EditorUtility.SetDirty(p);
            }
            else if (evt.type == EventType.Repaint)
            {
                Handles.color = p.gizmoColor;
                var c1 = new Vector3(_boundsRectStart.x, _boundsRectStart.y, _boundsRectEnd.z);
                var c2 = new Vector3(_boundsRectEnd.x, _boundsRectEnd.y, _boundsRectStart.z);
                Handles.DrawDottedLine(_boundsRectStart, c1, 10f);
                Handles.DrawDottedLine(c1, _boundsRectEnd, 10f);
                Handles.DrawDottedLine(_boundsRectEnd, c2, 10f);
                Handles.DrawDottedLine(c2, _boundsRectStart, 10f);
            }
        }