Rotate Selected graphic with mouse move angle ?

908
1
05-19-2013 01:15 AM
MayurDodiya
Occasional Contributor
I have added graphics in graphicslayer. what i want is on mouse click event get selected graphics and when mouse move change the angle of the selected graphic with mouse move event.here it is updating the graphic angle and position with mouse move but it is updating the whole graphics layer not the selected one. how to rotate and update only selected graphic position and angle? any idea ? here my code is below..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using System.Collections.Specialized;

namespace RotateGraphicText
{
    public partial class MainPage : UserControl
    {
        private DragGraphic _dragGraphic = null;
        private Random _random = new Random();
        ESRI.ArcGIS.Samples.RotatingTextSymbol rt = new ESRI.ArcGIS.Samples.RotatingTextSymbol();
        double angle;

        public MainPage()
        {
            InitializeComponent();

            // Get layers
            ArcGISTiledMapServiceLayer topoLayer = this.Map.Layers[0]
                as ArcGISTiledMapServiceLayer;
            GraphicsLayer graphicsLayer = this.Map.Layers[1]
                as GraphicsLayer;

            // Wait until the map service layer has loaded and initialized
            topoLayer.Initialized += (a, b) =>
            {
              
                rt.Text = "MyTest";
               
                for (int i = 0; i < 10; i++)
                {
                    graphicsLayer.Graphics.Add(
                        new Graphic()
                        {
                            // Create a random location
                            Geometry = new MapPoint()
                            {
                                X = topoLayer.InitialExtent.XMin +
                                    this._random.NextDouble() *
                                    (topoLayer.InitialExtent.XMax -
                                    topoLayer.InitialExtent.XMin),
                                Y = topoLayer.InitialExtent.YMin +
                                    this._random.NextDouble() *
                                    (topoLayer.InitialExtent.YMax -
                                    topoLayer.InitialExtent.YMin)
                            },
                            // Use the markersymbol defined in the resource
                          
                            Symbol = rt
                        }
                    );
                }
            };

            graphicsLayer.Graphics.CollectionChanged += (a, b) =>
            {
               
                // Exit if not added graphic
                if (b.Action != NotifyCollectionChangedAction.Add) { return; }

                // Get added graphic
                Graphic graphic = b.NewItems[0] as Graphic;
               
                // When the graphic is clicked, store the current state
                graphic.MouseLeftButtonDown += (sender, e) =>
                {
                   
                    this._dragGraphic = new DragGraphic()
                    {
                        Graphic = graphic,
                        GraphicOrigin = graphic.Geometry as MapPoint,
                        MouseOrigin = this.Map.ScreenToMap(e.GetPosition(null))
                    };
                    e.Handled = true;
                  
                    // Delete selected graphic
                    //graphicsLayer.Graphics.Remove(b.NewItems[0] as Graphic);
                    //angle = Angle(this._dragGraphic.GraphicOrigin.X, this._dragGraphic.GraphicOrigin.Y, this._dragGraphic.MouseOrigin.X, this._dragGraphic.MouseOrigin.Y);
                };
                // When the mouse is moved update the graphic's position
                graphic.MouseMove += (sender, e) =>
                {
                   
                    if (this._dragGraphic == null) { return; }
                    this._dragGraphic.Graphic.Geometry = new MapPoint()
                    {
                        X = this._dragGraphic.GraphicOrigin.X +
                            (this.Map.ScreenToMap(e.GetPosition(null)).X -
                            this._dragGraphic.MouseOrigin.X),
                        Y = this._dragGraphic.GraphicOrigin.Y +
                            (this.Map.ScreenToMap(e.GetPosition(null)).Y -
                            this._dragGraphic.MouseOrigin.Y)
                        //X = this._dragGraphic.GraphicOrigin.X, Y = this._dragGraphic.GraphicOrigin.Y
                    };
                    angle = Angle(this._dragGraphic.GraphicOrigin.X, this._dragGraphic.GraphicOrigin.Y, this._dragGraphic.GraphicOrigin.X +
                            (this.Map.ScreenToMap(e.GetPosition(null)).X -
                            this._dragGraphic.MouseOrigin.X), this._dragGraphic.GraphicOrigin.Y +
                            (this.Map.ScreenToMap(e.GetPosition(null)).Y -
                            this._dragGraphic.MouseOrigin.Y));
                    rt.Angle = angle;
                };

                // Clear the seleced graphic on mouse up
                graphic.MouseLeftButtonUp += (sender, e) =>
                {
                    this._dragGraphic = null;
                };
            };

            // Adjust the selected graphics position if the mouse moves
            this.Map.MouseMove += (sender, e) =>
            {
                //double angle = Angle(this._dragGraphic.MouseOrigin.X, this._dragGraphic.MouseOrigin.Y, this._dragGraphic.MouseOrigin.X + 2, this._dragGraphic.MouseOrigin.Y + 2);
                if (this._dragGraphic == null) { return; }
                this._dragGraphic.Graphic.Geometry = new MapPoint()
                {
                    X = this._dragGraphic.GraphicOrigin.X +
                        (this.Map.ScreenToMap(e.GetPosition(null)).X -
                        this._dragGraphic.MouseOrigin.X),
                    Y = this._dragGraphic.GraphicOrigin.Y +
                        (this.Map.ScreenToMap(e.GetPosition(null)).Y -
                        this._dragGraphic.MouseOrigin.Y)
                    //X = this._dragGraphic.GraphicOrigin.X,
                    //Y = this._dragGraphic.GraphicOrigin.Y
                };
               rt.Angle = angle;
            };

        }

        public double Angle(double px1, double py1, double px2, double py2)
        {

            // Negate X and Y values
            double pxRes = px2 - px1;

            double pyRes = py2 - py1;
            double angle = 0.0;

            // Calculate the angle
            if (pxRes == 0.0)
            {
                if (pxRes == 0.0)

                    angle = 0.0;
                else if (pyRes > 0.0) angle = System.Math.PI / 2.0;

                else
                    angle = System.Math.PI * 3.0 / 2.0;

            }
            else if (pyRes == 0.0)
            {
                if (pxRes > 0.0)

                    angle = 0.0;

                else
                    angle = System.Math.PI;

            }

            else
            {
                if (pxRes < 0.0)

                    angle = System.Math.Atan(pyRes / pxRes) + System.Math.PI;
                else if (pyRes < 0.0) angle = System.Math.Atan(pyRes / pxRes) + (2 * System.Math.PI);

                else
                    angle = System.Math.Atan(pyRes / pxRes);

            }

            // Convert to degrees
            angle = angle * 180 / System.Math.PI; return angle;



        }

    }
    public class DragGraphic
    {
        public Graphic Graphic { get; set; }
        public MapPoint MouseOrigin { get; set; }
        public MapPoint GraphicOrigin { get; set; }
    }

   
}
0 Kudos
1 Reply
DaveOrlando
Occasional Contributor III
sorry not to help,.......but

how do you get access to the
ESRI.ArcGIS.Samples.RotatingTextSymbol 



thanks
0 Kudos