Esempio n. 1
0
        private static void attachEditorComponents(Entity result, Main main)
        {
            Transform transform = result.Get<Transform>();

            Property<bool> selected = new Property<bool> { Value = false, Editable = false, Serialize = false };
            result.Add("EditorSelected", selected);

            Property<Entity.Handle> parentMap = result.GetOrMakeProperty<Entity.Handle>("Parent");

            Command<Entity> toggleEntityConnected = new Command<Entity>
            {
                Action = delegate(Entity entity)
                {
                    parentMap.Value = entity;
                }
            };
            result.Add("ToggleEntityConnected", toggleEntityConnected);

            LineDrawer connectionLines = new LineDrawer { Serialize = false };
            connectionLines.Add(new Binding<bool>(connectionLines.Enabled, selected));

            Color connectionLineColor = new Color(1.0f, 1.0f, 1.0f, 0.5f);

            Action recalculateLine = delegate()
            {
                connectionLines.Lines.Clear();
                Entity parent = parentMap.Value.Target;
                if (parent != null)
                {
                    connectionLines.Lines.Add(new LineDrawer.Line
                    {
                        A = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(transform.Position, connectionLineColor),
                        B = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(parent.Get<Transform>().Position, connectionLineColor)
                    });
                }
            };

            Model model = new Model();
            model.Filename.Value = "Models\\cone";
            model.Editable = false;
            model.Serialize = false;
            result.Add("DirectionModel", model);

            Property<Direction> dir = result.GetProperty<Direction>("Direction");
            Transform mapTransform = result.Get<Transform>("MapTransform");
            model.Add(new Binding<Matrix>(model.Transform, delegate()
            {
                Matrix m = Matrix.Identity;
                m.Translation = transform.Position;

                if (dir == Direction.None)
                    m.Forward = m.Right = m.Up = Vector3.Zero;
                else
                {
                    Vector3 normal = Vector3.TransformNormal(dir.Value.GetVector(), mapTransform.Matrix);

                    m.Forward = -normal;
                    if (normal.Equals(Vector3.Up))
                        m.Right = Vector3.Left;
                    else if (normal.Equals(Vector3.Down))
                        m.Right = Vector3.Right;
                    else
                        m.Right = Vector3.Normalize(Vector3.Cross(normal, Vector3.Down));
                    m.Up = Vector3.Cross(normal, m.Left);
                }
                return m;
            }, transform.Matrix, mapTransform.Matrix));

            NotifyBinding recalculateBinding = null;
            Action rebuildBinding = delegate()
            {
                if (recalculateBinding != null)
                {
                    connectionLines.Remove(recalculateBinding);
                    recalculateBinding = null;
                }
                if (parentMap.Value.Target != null)
                {
                    recalculateBinding = new NotifyBinding(recalculateLine, parentMap.Value.Target.Get<Transform>().Matrix);
                    connectionLines.Add(recalculateBinding);
                }
                recalculateLine();
            };
            connectionLines.Add(new NotifyBinding(rebuildBinding, parentMap));

            connectionLines.Add(new NotifyBinding(recalculateLine, selected));
            connectionLines.Add(new NotifyBinding(recalculateLine, () => selected, transform.Position));
            result.Add(connectionLines);
        }