private static Manipulations2D ConvertMode(ManipulationModes mode) { Manipulations2D manipulations = Manipulations2D.None; if ((mode & ManipulationModes.TranslateX) != 0) { manipulations |= Manipulations2D.TranslateX; } if ((mode & ManipulationModes.TranslateY) != 0) { manipulations |= Manipulations2D.TranslateY; } if ((mode & ManipulationModes.Scale) != 0) { manipulations |= Manipulations2D.Scale; } if ((mode & ManipulationModes.Rotate) != 0) { manipulations |= Manipulations2D.Rotate; } return(manipulations); }
/// <summary> /// Validates the specified value against the possible range of values /// for Manipulations2D. Throws an ArgumentOutOfRangeException /// listing the specified property name if the value is out of range. /// </summary> /// <param name="value"></param> /// <param name="property"></param> public static void CheckValue(this Manipulations2D value, string property) { if (!value.IsValid()) { throw Exceptions.ArgumentOutOfRange(property, value); } }
// </Snippet_GamePiece_PrivateMembers> /*******************************************/ // <Snippet_GamePiece_Constructor> #region Constructor public GamePiece(SpriteBatch spriteBatch, string fileName) { // For brevity, omitting checking of null parameters. this.spriteBatch = spriteBatch; // Get the texture from the specified file. texture = Texture2D.FromFile(spriteBatch.GraphicsDevice, fileName); // Initial position set to 0,0. position = new Vector2(0); // Set the origin to be the center of the texture. origin = new Vector2(texture.Width / 2.0f, texture.Height / 2.0f); // Set bounds. bounds.X and bounds.Y are set as the position or scale changes. bounds = new Rectangle(0, 0, texture.Width, texture.Height); // Create manipulation processor. Manipulations2D enabledManipulations = Manipulations2D.Translate | Manipulations2D.Rotate; manipulationProcessor = new ManipulationProcessor2D(enabledManipulations); manipulationProcessor.Pivot = new ManipulationPivot2D(); manipulationProcessor.Pivot.Radius = texture.Width / 2; manipulationProcessor.MinimumScaleRotateRadius = 10.0f; manipulationProcessor.Started += OnManipulationStarted; manipulationProcessor.Delta += OnManipulationDelta; manipulationProcessor.Completed += OnManipulationCompleted; // Create inertia processor. inertiaProcessor = new InertiaProcessor2D(); inertiaProcessor.Delta += OnInertiaDelta; inertiaProcessor.Completed += OnInertiaCompleted; inertiaProcessor.TranslationBehavior.DesiredDeceleration = 0.0001F; inertiaProcessor.RotationBehavior.DesiredDeceleration = 1e-6F; inertiaProcessor.ExpansionBehavior.DesiredDeceleration = 0.0001F; // Save the view port. Used to detect when the piece needs to bounce. viewport = spriteBatch.GraphicsDevice.Viewport; // Set the piece in a random location. Random random = new Random((int)Timestamp); X = random.Next(viewport.Width); Y = random.Next(viewport.Height); // Set a random orientation. rotation = (float)(random.NextDouble() * Math.PI * 2.0); dragPoint = new System.Windows.Point(double.NaN, double.NaN); pieceColor = Color.White; // Set scale to normal (100%) Scale = 1.0f; }
/// <summary> /// Creates a new /// <strong><see cref="System.Windows.Input.Manipulations.ManipulationProcessor2D"></see></strong> /// object. /// </summary> /// <param name="supportedManipulations">The initial set of supported manipulations.</param> /// <param name="pivot">Pivot information for single-manipulator rotations.</param> /// <exception cref="ArgumentOutOfRangeException">The <em>supportedManipulations</em> parameter is /// not a valid combination of the /// <strong><see cref="System.Windows.Input.Manipulations.Manipulations2D"></see></strong> /// enumeration values.</exception> public ManipulationProcessor2D( Manipulations2D supportedManipulations, ManipulationPivot2D pivot) { supportedManipulations.CheckValue("supportedManipulations"); this.supportedManipulations = supportedManipulations; this.pivot = pivot; }
/// <summary> /// Each Contact gets its own ManipulationProcessor assigned /// This way we can use the Manipulator data even if there are multiple Contacts /// </summary> /// <param name="c">Contact to add the Manipulator to</param> public void addManipulationProcessor(TouchPoint c) { Manipulations2D supportedManipulations = Manipulations2D.TranslateX | Manipulations2D.TranslateY | Manipulations2D.Rotate | Manipulations2D.Scale; // Create and initialize a manipulation processor with the supported manipulations. ManipulationProcessor2D mp = new ManipulationProcessor2D(supportedManipulations); // Add event handlers for manipulation events. mp.Started += new EventHandler <Manipulation2DStartedEventArgs>(OnAffine2DManipulationStarted); mp.Completed += new EventHandler <Manipulation2DCompletedEventArgs>(OnAffine2DManipulationCompleted); mp.Delta += new EventHandler <Manipulation2DDeltaEventArgs>(OnAffine2DDelta); _contactProcessors.Add(c.Id, mp); }
private Manipulations2D GetSupportedManipulations() { Manipulations2D manipulations = Manipulations2D.None; if (CheckTranslate.IsChecked.Value) { manipulations = manipulations | Manipulations2D.Translate; } if (CheckRotate.IsChecked.Value) { manipulations = manipulations | Manipulations2D.Rotate; } if (CheckScale.IsChecked.Value) { manipulations = manipulations | Manipulations2D.Scale; } return(manipulations); }
/******************************************************************************/ #region Private Methods /// <summary> /// Process a mouse event. /// </summary> /// <param name="mouse"></param> private void ProcessMouse(MouseDevice mouse) { if ((mouse.Captured == this)) { Point position = mouse.GetPosition(Container); List <Manipulator2D> manipulators = new List <Manipulator2D>(); manipulators.Add(new Manipulator2D( 0, (float)(position.X), (float)(position.Y))); // If translation is turned off and the pivot is turned on, // make it act like there's a manipulator on the pivot point, // to allow us to do scaling if (((SupportedManipulations & Manipulations2D.Translate) == Manipulations2D.None) && IsPivotActive) { manipulators.Add(new Manipulator2D( 1, (float)(Center.X), (float)(Center.Y))); } const Manipulations2D translateAndRotate = Manipulations2D.Translate | Manipulations2D.Rotate; if ((manipulators.Count == 1) && ((manipulationProcessor.SupportedManipulations & translateAndRotate) == translateAndRotate) && IsPivotActive) { dragCenter = position; } else { dragCenter.X = double.NaN; dragCenter.Y = double.NaN; } manipulationProcessor.ProcessManipulators( Timestamp, manipulators); } }
/// <summary> /// Gets whether one or more of the specified manipulations are supported. /// </summary> /// <param name="value"></param> /// <param name="supported"></param> /// <returns></returns> public static bool SupportsAny(this Manipulations2D value, Manipulations2D supported) { return((value & supported) != 0); }
/// <summary> /// Extension method that determines whether a Manipulations2D /// falls within the range of allowed values. /// </summary> /// <param name="value"></param> /// <returns></returns> public static bool IsValid(this Manipulations2D value) { int maskedValue = (int)value & ~(int)Manipulations2D.All; return(maskedValue == 0); }
/// <summary> /// Creates a new /// <strong><see cref="System.Windows.Input.Manipulations.ManipulationProcessor2D"></see></strong> /// object. /// </summary> /// <param name="supportedManipulations">The initial set of supported manipulations.</param> /// <exception cref="ArgumentOutOfRangeException">The <em>supportedManipulations</em> parameter is /// not a valid combination of the /// <strong><see cref="System.Windows.Input.Manipulations.Manipulations2D"></see></strong> /// enumeration values.</exception> public ManipulationProcessor2D(Manipulations2D supportedManipulations) : this(supportedManipulations, null) { }