protected override void OnApplyTemplate() { base.OnApplyTemplate(); ContentPresenter = (ContentControl)GetTemplateChild("ContentPresenter"); planeProjection = (PlaneProjection)GetTemplateChild("Rotator"); LayoutRoot = (FrameworkElement)GetTemplateChild("LayoutRoot"); Animation = (Storyboard)GetTemplateChild("Animation"); Animation.Completed += Animation_Completed; rotationKeyFrame = (EasingDoubleKeyFrame)GetTemplateChild("rotationKeyFrame"); offestZKeyFrame = (EasingDoubleKeyFrame)GetTemplateChild("offestZKeyFrame"); scaleXKeyFrame = (EasingDoubleKeyFrame)GetTemplateChild("scaleXKeyFrame"); scaleYKeyFrame = (EasingDoubleKeyFrame)GetTemplateChild("scaleYKeyFrame"); scaleTransform = (ScaleTransform)GetTemplateChild("scaleTransform"); planeProjection.RotationY = yRotation; planeProjection.LocalOffsetZ = zOffset; if (ContentPresenter != null) { ContentPresenter.Tapped += ContentPresenter_Tapped; } if (Animation != null) { xAnimation = new DoubleAnimation(); Animation.Children.Add(xAnimation); Storyboard.SetTarget(xAnimation, this); Storyboard.SetTargetProperty(xAnimation, "(Canvas.Left)"); } }
// 构造函数 做一些初始化工作 public TileButton() { projection = new PlaneProjection(); Projection = projection; transform = new CompositeTransform(); RenderTransform = transform; ManipulationMode = ManipulationModes.Rotate | ManipulationModes.Scale | ManipulationModes.TranslateX | ManipulationModes.TranslateY; }
/// <summary> /// 在此页将要在 Frame 中显示时进行调用。 /// </summary> /// <param name="e">描述如何访问此页的事件数据。Parameter /// 属性通常用于配置页。</param> protected override void OnNavigatedTo(NavigationEventArgs e) { CompositeTransform transform = new CompositeTransform(); transform.Rotation = 45; transform.ScaleX = 0.5; transform.ScaleY = 0.5; image1.RenderTransform = transform; PlaneProjection project = new PlaneProjection(); project.RotationY = 60; image2.Projection = project; }
private void OpenPopup(Popup popupPanel, PlaneProjection panelCarrot, Button panelButton, int horizontalOffset, int verticalOffset) { //First we need to find out how big our window is, so we can center to it. CoreWindow currentWindow = Window.Current.CoreWindow; //Set our background rectangle to fill the entire window rectBackgroundHide.Height = currentWindow.Bounds.Height; rectBackgroundHide.Width = currentWindow.Bounds.Width; rectBackgroundHide.Margin = new Thickness(0, 0, 0, 0); //Make sure the background is visible rectBackgroundHide.Visibility = Windows.UI.Xaml.Visibility.Visible; //Now we figure out where the center of the screen is, and we //move the popup to that location. var buttonBounds = panelButton.GetElementBounds(this); popupPanel.HorizontalOffset = buttonBounds.Left + horizontalOffset; popupPanel.VerticalOffset = buttonBounds.Top + verticalOffset; panelCarrot.RotationZ = 0; popupPanel.IsOpen = true; }
/// <summary> /// Prepares a control to be tilted by setting up a plane projection and /// some event handlers. /// </summary> /// <param name="element">The control that is to be tilted.</param> /// <param name="centerDelta">Delta between the element's center and the /// tilt container's.</param> /// <returns>true if successful; false otherwise.</returns> /// <remarks> /// This method is conservative; it will fail any attempt to tilt a /// control that already has a projection on it. /// </remarks> private static bool PrepareControlForTilt(FrameworkElement element, Point centerDelta, Pointer p) { // Prevents interference with any existing transforms if (element.Projection != null || (element.RenderTransform != null && element.RenderTransform.GetType() != typeof(MatrixTransform))) { return false; } _originalCacheMode[element] = element.CacheMode; element.CacheMode = new BitmapCache(); TranslateTransform transform = new TranslateTransform(); transform.X = centerDelta.X; transform.Y = centerDelta.Y; element.RenderTransform = transform; PlaneProjection projection = new PlaneProjection(); projection.GlobalOffsetX = -1 * centerDelta.X; projection.GlobalOffsetY = -1 * centerDelta.Y; element.Projection = projection; element.PointerMoved += TiltEffect_PointerMoved; element.PointerReleased += TiltEffect_PointerReleased; element.CapturePointer(p); return true; }
/// <summary> /// Runs the cascades animation. /// </summary> /// <exception cref="System.InvalidOperationException"></exception> public void Cascade() { if (!_isLoaded || _layoutGrid == null) { return; } if (Rows < 1) Rows = 1; if (Columns < 1) Columns = 1; _layoutGrid.Children.Clear(); _layoutGrid.RowDefinitions.Clear(); _layoutGrid.ColumnDefinitions.Clear(); for (int row = 0; row < Rows; row++) { _layoutGrid.RowDefinitions.Add(new RowDefinition()); } for (int column = 0; column < Columns; column++) { _layoutGrid.ColumnDefinitions.Add(new ColumnDefinition()); } var sb = new Storyboard(); var totalDurationInSeconds = RowDelay.TotalSeconds * (Rows - 1) + ColumnDelay.TotalSeconds * (Columns - 1) + TileDuration.TotalSeconds; CascadeDirection direction = this.CascadeDirection; if (direction == CascadeDirection.Random) { direction = (CascadeDirection)Random.Next((int)CascadeDirection.Random); } int startColumn; int exclusiveEndColumn; int columnIncrement; int startRow; int exclusiveEndRow; int rowIncrement; switch (direction) { case CascadeDirection.Shuffle: case CascadeDirection.TopLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.TopRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.BottomRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; case CascadeDirection.BottomLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; default: throw new InvalidOperationException(); } List<Tuple<int, int>> rectCoords = new List<Tuple<int, int>>(Rows * Columns); List<Rectangle> rects = new List<Rectangle>(Rows * Columns); List<PlaneProjection> projs = new List<PlaneProjection>(Rows * Columns); for (int row = startRow; row != exclusiveEndRow; row = row + rowIncrement) for (int column = startColumn; column != exclusiveEndColumn; column = column + columnIncrement) { var rect = new Rectangle(); rects.Add(rect); Grid.SetRow(rect, row); Grid.SetColumn(rect, column); rectCoords.Add(new Tuple<int, int>(column, row)); var brush = new ImageBrush(); brush.ImageSource = this.ImageSource; rect.Fill = brush; var transform = new CompositeTransform(); transform.TranslateX = -column; transform.ScaleX = Columns; transform.TranslateY = -row; transform.ScaleY = Rows; brush.RelativeTransform = transform; var projection = new PlaneProjection(); projection.CenterOfRotationY = 0; rect.Projection = projection; projs.Add(projection); _layoutGrid.Children.Add(rect); } var indices = new List<int>(Rows * Columns); for (int i = 0; i < Rows * Columns; i++) indices.Add(i); if (direction == CascadeDirection.Shuffle) { indices = indices.Shuffle(); } for (int ii = 0; ii < indices.Count; ii++) { var i = indices[ii]; var projection = projs[i]; var rect = rects[i]; var column = rectCoords[ii].Item1; var row = rectCoords[ii].Item2; //Debug.WriteLine("i: {0}, p: {1}, rect: {2}, c: {3}, r: {4}", i, projection.GetHashCode(), rect.GetHashCode(), column, row); var rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, projection); Storyboard.SetTargetProperty(rotationAnimation, "RotationX"); var endKeyTime = this.CascadeSequence == CascadeSequence.EndTogether ? TimeSpan.FromSeconds(totalDurationInSeconds) : TimeSpan.FromSeconds( (double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds + TileDuration.TotalSeconds); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 90 }); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 90 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 0 }); sb.Children.Add(rotationAnimation); var opacityAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(opacityAnimation, rect); Storyboard.SetTargetProperty(opacityAnimation, "Opacity"); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 0 }); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 0 }); opacityAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 1 }); sb.Children.Add(opacityAnimation); } sb.Begin(); }
protected PlaneProjection GetProjection(FrameworkElement element) { var projection = element.Projection as PlaneProjection; if (projection == null) element.Projection = projection = new PlaneProjection(); return projection; }
private void CreateItem2(object item, double opacity = 1) { var element = ItemTemplate.LoadContent() as FrameworkElement; if (element != null) { element.DataContext = item; element.Opacity = opacity; element.RenderTransformOrigin = new Point(0.5, 0.5); var planeProjection = new PlaneProjection { CenterOfRotationX = 0.5, CenterOfRotationY = 0.5 }; element.Projection = planeProjection; _internalList.Add(element); Children.Add(element); } }
protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { base.PrepareContainerForItemOverride(element, item); ListBoxItem item2 = element as ListBoxItem; if (item2 != null && !items.Contains(item2)) { item2.Visibility = Visibility.Collapsed; if (Orientation == Orientation.Horizontal) item2.HorizontalAlignment = HorizontalAlignment.Center; else item2.VerticalAlignment = VerticalAlignment.Center; items.Add(item2); TransformGroup myTransformGroup = new TransformGroup(); ScaleTransform scaleTransform = new ScaleTransform(); TranslateTransform translateTransform = new TranslateTransform(); PlaneProjection planeProjection = new PlaneProjection() { CenterOfRotationX = .5, CenterOfRotationY = .5, CenterOfRotationZ = .5 }; myTransformGroup.Children.Add(scaleTransform); myTransformGroup.Children.Add(translateTransform); // Associate the transforms to the object item2.RenderTransformOrigin = itemRenderTransformOrigin; item2.RenderTransform = myTransformGroup; item2.Projection = planeProjection; if (items.Count < pageCount + 1) { SetLocation(item2, items.Count - 1); } } }
/// <summary> /// animation for banner ad /// </summary> private void RotateInterstitialAd() { Storyboard _storyBourd = new Storyboard(); DoubleAnimation _doubleAnimation = new DoubleAnimation(); _doubleAnimation.From = 90; _doubleAnimation.To = 0; PlaneProjection projection1 = new PlaneProjection(); Maingrid.Projection = projection1; _doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(1.25)); Storyboard.SetTarget(_doubleAnimation, Maingrid.Projection); Storyboard.SetTargetProperty(_doubleAnimation, new PropertyPath(PlaneProjection.RotationXProperty)); _storyBourd.Children.Add(_doubleAnimation); _storyBourd.Seek(TimeSpan.FromSeconds(0.5)); _storyBourd.Begin(); }
static bool PrepareControlForTilt(FrameworkElement element, Point centerDelta) #endif { // Prevents interference with any existing transforms if (element.Projection != null || (element.RenderTransform != null && element.RenderTransform.GetType() != typeof(MatrixTransform))) { return false; } OriginalCacheMode[element] = element.CacheMode; element.CacheMode = new BitmapCache(); var transform = new TranslateTransform {X = centerDelta.X, Y = centerDelta.Y}; element.RenderTransform = transform; var projection = new PlaneProjection {GlobalOffsetX = -1*centerDelta.X, GlobalOffsetY = -1*centerDelta.Y}; element.Projection = projection; #if WINDOWS_STORE element.PointerMoved += TiltEffect_PointerMoved; element.PointerReleased += TiltEffect_PointerReleased; element.PointerCaptureLost += TiltEffect_PointerCaptureLost; element.CapturePointer(p); #elif WINDOWS_PHONE element.ManipulationDelta += TiltEffectDelta; element.ManipulationCompleted += TiltEffectCompleted; #endif return true; }
//private void AnimateLeft() //{ // var animDuration = new Duration(TimeSpan.FromMilliseconds(1000d)); // var offset = this.BorderThickness.Right + this.BorderThickness.Left; // if (this.IsFrontSide) // { // var storyboard = new Storyboard(); // storyboard = this.AddToStoryboard(storyboard, -this.ActualWidth + offset, 0d, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard = this.AddToStoryboard(storyboard, 0d, this.ActualWidth - offset, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // this.IsFrontSide = false; // storyboard.Begin(); // } // else // { // var storyboard = new Storyboard(); // storyboard = this.AddToStoryboard(storyboard, -this.ActualWidth + offset, 0d, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard = this.AddToStoryboard(storyboard, 0d, this.ActualWidth - offset, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard.Begin(); // this.IsFrontSide = true; // } //} //private void AnimateRight() //{ // var animDuration = new Duration(TimeSpan.FromMilliseconds(1000d)); // var offset = this.BorderThickness.Right + this.BorderThickness.Left; // if (this.IsFrontSide) // { // var storyboard = new Storyboard(); // storyboard = this.AddToStoryboard(storyboard, 0d, -this.ActualWidth + offset, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard = this.AddToStoryboard(storyboard, this.ActualWidth - offset, 0d, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // this.IsFrontSide = false; // storyboard.Begin(); // } // else // { // var storyboard = new Storyboard(); // storyboard = this.AddToStoryboard(storyboard, 0d, -this.ActualWidth + offset, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard = this.AddToStoryboard(storyboard, this.ActualWidth - offset, 0d, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)"); // storyboard.Begin(); // this.IsFrontSide = true; // } //} //private void AnimateUp() //{ // var animDuration = new Duration(TimeSpan.FromMilliseconds(1000d)); // var offset = this.BorderThickness.Top + this.BorderThickness.Bottom; // if (this.IsFrontSide) // { // var storyboard = new Storyboard(); // this.backContentPresenter.Visibility = Visibility.Visible; // storyboard = this.AddToStoryboard(storyboard, 0d, -this.ActualHeight + offset, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard = this.AddToStoryboard(storyboard, this.ActualHeight - offset, 0d, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard.Completed += (sender1, o1) => this.frontContentPresenter.Visibility = Visibility.Collapsed; // this.IsFrontSide = false; // storyboard.Begin(); // } // else // { // var storyboard = new Storyboard(); // this.frontContentPresenter.Visibility = Visibility.Visible; // storyboard = this.AddToStoryboard(storyboard, 0d, -this.ActualHeight + offset, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard = this.AddToStoryboard(storyboard, this.ActualHeight - offset, 0d, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard.Completed += (sender1, o1) => this.backContentPresenter.Visibility = Visibility.Collapsed; // storyboard.Begin(); // this.IsFrontSide = true; // } //} //private void AnimateDown() //{ // var animDuration = new Duration(TimeSpan.FromMilliseconds(1000d)); // var offset = this.BorderThickness.Top + this.BorderThickness.Bottom; // if (this.IsFrontSide) // { // var storyboard = new Storyboard(); // this.backContentPresenter.Visibility = Visibility.Visible; // storyboard = this.AddToStoryboard(storyboard, -this.ActualHeight + offset, 0d, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard = this.AddToStoryboard(storyboard, 0d, this.ActualHeight - offset, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard.Completed += (sender1, o1) => this.frontContentPresenter.Visibility = Visibility.Collapsed; // this.IsFrontSide = false; // storyboard.Begin(); // } // else // { // var storyboard = new Storyboard(); // this.frontContentPresenter.Visibility = Visibility.Visible; // storyboard = this.AddToStoryboard(storyboard, -this.ActualHeight + offset, 0d, animDuration, // this.frontContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard = this.AddToStoryboard(storyboard, 0d, this.ActualHeight - offset, animDuration, // this.backContentPresenter, // "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)"); // storyboard.Completed += (sender1, o1) => this.backContentPresenter.Visibility = Visibility.Collapsed; // storyboard.Begin(); // this.IsFrontSide = true; // } //} private void SetPlaneProjection(UIElement contentGrid) { PlaneProjection planeProjection = new PlaneProjection(); contentGrid.Projection = planeProjection; }
private static DoubleAnimation CreateProjectionAnimation(PlaneProjection projection, double to, double seconds, string projectionRotation) { var projectionAnimation = new DoubleAnimation { To = to, Duration = new Duration(TimeSpan.FromSeconds(seconds)), }; Storyboard.SetTarget(projectionAnimation, projection); Storyboard.SetTargetProperty(projectionAnimation, projectionRotation); return projectionAnimation; }
public static DoubleAnimation CreateProjectionZAnimation(PlaneProjection projection, double to, double seconds) { return CreateProjectionAnimation(projection, to, seconds, "RotationZ"); }
/// <summary> /// Create an item (Load data template and bind) /// </summary> private FrameworkElement CreateItem(object item, Double opacity = 1) { FrameworkElement element = ItemTemplate.LoadContent() as FrameworkElement; if (element == null) return null; element.DataContext = item; element.Opacity = opacity; element.RenderTransformOrigin = new Point(0.5, 0.5); PlaneProjection planeProjection = new PlaneProjection(); planeProjection.CenterOfRotationX = 0.5; planeProjection.CenterOfRotationY = 0.5; element.Projection = planeProjection; this.internalList.Add(element); this.Children.Add(element); return element; }
protected override void OnApplyTemplate() { base.OnApplyTemplate(); _contentPresenter = (ContentPresenter)GetTemplateChild(ContentPresenterName); _topBarContentPresenter = (ContentPresenter)GetTemplateChild(TopBarContentPresenterName); _informationGrid = (ContentPresenter)GetTemplateChild(InformationContentPresenterName); _rightFlyoutContentPresenter = (ContentPresenter)GetTemplateChild(RightFlyoutContentPresenterName); _rightFlyoutFadeIn = (Storyboard)GetTemplateChild(RightFlyoutFadeInName); _rightFlyoutFadeOut = (Storyboard)GetTemplateChild(RightFlyoutFadeOutName); _topBarFadeOut = (Storyboard) GetTemplateChild(TopBarFadeOutName); _topBarFadeIn = (Storyboard) GetTemplateChild(TopBarFadeInName); _rightFlyoutPlaneProjection = (PlaneProjection)GetTemplateChild(RightFlyoutPlaneProjectionName); _rightFlyoutGridContainer = (Grid)GetTemplateChild(RightFlyoutGridContainerName); _flyoutBackgroundGrid = (Grid)GetTemplateChild(FlyoutBackgroundGridName); _footerContentPresenter = (ContentPresenter) GetTemplateChild(FooterContentPresenterName); _titleBarContentPresenter = (ContentPresenter) GetTemplateChild(TitleBarContentPresenterName); TemplateApplied.SetResult(true); _rightFlyoutGridContainer.Visibility = Visibility.Collapsed; _flyoutBackgroundGrid.Tapped += RightFlyoutGridContainerOnTapped; }
/// <summary> /// Ease PlaneProjection with values eased from startValue to endValue using a time percentage 0 -> 1. /// </summary> public static void EaseValue(PlaneProjection current, PlaneProjection startValue, PlaneProjection endValue, double percent) { if (current.RotationX != endValue.RotationX) current.RotationX = EaseValue(startValue.RotationX, endValue.RotationX, percent); if (current.RotationY != endValue.RotationY) current.RotationY = EaseValue(startValue.RotationY, endValue.RotationY, percent); if (current.RotationZ != endValue.RotationZ) current.RotationZ = EaseValue(startValue.RotationZ, endValue.RotationZ, percent); if (current.LocalOffsetX != endValue.LocalOffsetX) current.LocalOffsetX = EaseValue(startValue.LocalOffsetX, endValue.LocalOffsetX, percent); if (current.LocalOffsetY != endValue.LocalOffsetY) current.LocalOffsetY = EaseValue(startValue.LocalOffsetY, endValue.LocalOffsetY, percent); if (current.LocalOffsetZ != endValue.LocalOffsetZ) current.LocalOffsetZ = EaseValue(startValue.LocalOffsetZ, endValue.LocalOffsetZ, percent); if (current.GlobalOffsetX != endValue.GlobalOffsetX) current.GlobalOffsetX = EaseValue(startValue.GlobalOffsetX, endValue.GlobalOffsetX, percent); if (current.GlobalOffsetY != endValue.GlobalOffsetY) current.GlobalOffsetY = EaseValue(startValue.GlobalOffsetY, endValue.GlobalOffsetY, percent); if (current.GlobalOffsetZ != endValue.GlobalOffsetZ) current.GlobalOffsetZ = EaseValue(startValue.GlobalOffsetZ, endValue.GlobalOffsetZ, percent); if (current.CenterOfRotationX != endValue.CenterOfRotationX) current.CenterOfRotationX = EaseValue(startValue.CenterOfRotationX, endValue.CenterOfRotationX, percent); if (current.CenterOfRotationY != endValue.CenterOfRotationY) current.CenterOfRotationY = EaseValue(startValue.CenterOfRotationY, endValue.CenterOfRotationY, percent); if (current.CenterOfRotationZ != endValue.CenterOfRotationZ) current.CenterOfRotationZ = EaseValue(startValue.CenterOfRotationZ, endValue.CenterOfRotationZ, percent); // TO INCLUDE? //if (current.ProjectionMatrix != endValue.ProjectionMatrix) EaseValue(current.ProjectionMatrix, startValue.ProjectionMatrix, endValue.ProjectionMatrix, percent); }
public void Cascade() { RH = RW = double.NaN; if (!_isLoaded || _layoutGrid == null) { return; } if (Rows < 1) Rows = 1; if (Columns < 1) Columns = 1; _layoutGrid.Children.Clear(); _layoutGrid.RowDefinitions.Clear(); _layoutGrid.ColumnDefinitions.Clear(); for (int row = 0; row < Rows; row++) { _layoutGrid.RowDefinitions.Add(new RowDefinition()); } for (int column = 0; column < Columns; column++) { _layoutGrid.ColumnDefinitions.Add(new ColumnDefinition()); } var sb = new Storyboard(); var totalDurationInSeconds = RowDelay.TotalSeconds * (Rows - 1) + ColumnDelay.TotalSeconds * (Columns - 1) + TileDuration.TotalSeconds; CascadeDirection direction = this.CascadeDirection; if (direction == CascadeDirection.Random) { direction = (CascadeDirection)Random.Next((int)CascadeDirection.Random); } int startColumn; int exclusiveEndColumn; int columnIncrement; int startRow; int exclusiveEndRow; int rowIncrement; switch (direction) { case CascadeDirection.Shuffle: case CascadeDirection.TopLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.TopRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = 0; exclusiveEndRow = Rows; rowIncrement = 1; break; case CascadeDirection.BottomRight: startColumn = Columns - 1; exclusiveEndColumn = -1; columnIncrement = -1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; case CascadeDirection.BottomLeft: startColumn = 0; exclusiveEndColumn = Columns; columnIncrement = 1; startRow = Rows - 1; exclusiveEndRow = -1; rowIncrement = -1; break; default: throw new InvalidOperationException(); } List<Tuple<int, int>> rectCoords = new List<Tuple<int, int>>(Rows * Columns); List<Rectangle> rects = new List<Rectangle>(Rows * Columns); //List<PlaneProjection> projs = new List<PlaneProjection>(Rows * Columns); //List<CompositeTransform> ct = new List<CompositeTransform>(Rows * Columns); //**********************开始的位置 List<double> translateXInfo = new List<double>(); List<double> translateYInfo = new List<double>(); for (int row = startRow; row != exclusiveEndRow; row = row + rowIncrement) for (int column = startColumn; column != exclusiveEndColumn; column = column + columnIncrement) { var rect = new Rectangle(); rects.Add(rect); Grid.SetRow(rect, row); Grid.SetColumn(rect, column); rectCoords.Add(new Tuple<int, int>(column, row)); var brush = new ImageBrush(); brush.ImageSource = this.ImageSource; brush.Stretch = this.Stretch; rect.Fill = brush; var transform = new CompositeTransform(); transform.TranslateX = -column; transform.ScaleX = Columns; transform.TranslateY = -row; transform.ScaleY = Rows; brush.RelativeTransform = transform; var projection = new PlaneProjection(); projection.CenterOfRotationY = 0; rect.Projection = projection; //projs.Add(projection); var rectTransform = new CompositeTransform(); rectTransform.CenterX = rectTransform.CenterY = 0.5; ////////////////////////写到这 //rectTransform.TranslateX = -column; //rectTransform.TranslateY = -row; rectTransform.Rotation = 0; rect.RenderTransformOrigin = new Windows.Foundation.Point(0.5, 0.5); rect.RenderTransform = rectTransform; //ct.Add(rectTransform); //if (!double.IsNaN(RH) && !double.IsNaN(RW)) //{ // rect.Margin = new Thickness(column * 52, row * 50, 0, 0); //} _layoutGrid.Children.Add(rect); } //GetRHAndRW(); if (double.IsNaN(RH) || double.IsNaN(RW)) { rects[0].SizeChanged -= sizeChanged; rects[0].SizeChanged += sizeChanged = (ss, ee) => { this.RH = ee.NewSize.Height; this.RW = ee.NewSize.Width; var indices = new List<int>(Rows * Columns); for (int i = 0; i < Rows * Columns; i++) { indices.Add(i); ////////////////////////写到这 var transform = rects[i].RenderTransform as CompositeTransform; //transform.TranslateX = transform.TranslateX * RW; //transform.TranslateY = transform.TranslateY * RH; //*****************************恶心点,随便弄个起始位置,到时候可以根据行列信息或者奇偶信息来设置起始位置 //transform.TranslateX = i * RW; //transform.TranslateY = i * RH; if (i < Rows * Columns / 2) { transform.TranslateX =-RW; transform.TranslateY = -RH; } else { transform.TranslateX = RW; transform.TranslateY = RH; } translateXInfo.Add(transform.TranslateX); translateYInfo.Add(transform.TranslateY); System.Diagnostics.Debug.WriteLine("TX = " + transform.TranslateX + " TY = " + transform.TranslateY + "\n"); } if (direction == CascadeDirection.Shuffle) { indices = indices.Shuffle(); } for (int ii = 0; ii < indices.Count; ii++) { var i = indices[ii]; //var projection = projs[i]; var projection = rects[i].Projection; var rect = rects[i]; var column = rectCoords[ii].Item1; var row = rectCoords[ii].Item2; //*******************拿到当前的transform var transfrom = rect.RenderTransform as CompositeTransform; //Debug.WriteLine("i: {0}, p: {1}, rect: {2}, c: {3}, r: {4}", i, projection.GetHashCode(), rect.GetHashCode(), column, row); var rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, projection); Storyboard.SetTargetProperty(rotationAnimation, "RotationX"); var endKeyTime = this.CascadeSequence == CascadeSequence.EndTogether ? TimeSpan.FromSeconds(totalDurationInSeconds) : TimeSpan.FromSeconds( (double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds + TileDuration.TotalSeconds); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 90 }); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 90 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 0 }); sb.Children.Add(rotationAnimation); //****************************y,z的效果可以研究下如何加比较好 rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, projection); Storyboard.SetTargetProperty(rotationAnimation, "RotationY"); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 180 }); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 90 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 0 }); sb.Children.Add(rotationAnimation); rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, projection); Storyboard.SetTargetProperty(rotationAnimation, "RotationZ"); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 180 }); rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 90 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 0 }); sb.Children.Add(rotationAnimation); var opacityAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(opacityAnimation, rect); Storyboard.SetTargetProperty(opacityAnimation, "Opacity"); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.Zero, Value = 0 }); opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = TimeSpan.FromSeconds((double)row * RowDelay.TotalSeconds + (double)column * ColumnDelay.TotalSeconds), Value = 0 }); opacityAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = endKeyTime, EasingFunction = CascadeInEasingFunction, Value = 1 }); sb.Children.Add(opacityAnimation); //******************** 效果差不错出来了,主要是起始点的设置,还有,这里的动画要不要弄成关键祯? var translateXanimation = new DoubleAnimation(); translateXanimation.From = translateXInfo[i]; translateXanimation.To = 0; translateXanimation.Duration = endKeyTime; Storyboard.SetTarget(translateXanimation, transfrom); Storyboard.SetTargetProperty(translateXanimation, "TranslateX"); sb.Children.Add(translateXanimation); var translateYanimation = new DoubleAnimation(); translateYanimation.From = translateYInfo[i]; translateYanimation.To = 0; translateYanimation.Duration = endKeyTime; Storyboard.SetTarget(translateYanimation, transfrom); Storyboard.SetTargetProperty(translateYanimation, "TranslateY"); sb.Children.Add(translateYanimation); } sb.Begin(); }; } }
/// <summary> /// Applies the target properties. /// </summary> /// <param name="target">The target.</param> /// <param name="animation">The animation.</param> protected override void ApplyTargetProperties(DependencyObject target, Storyboard animation) { var fe = (FrameworkElement)target; var projection = fe.Projection as PlaneProjection; if (projection == null) { fe.Projection = projection = new PlaneProjection(); } var transform = fe.RenderTransform as TranslateTransform; if (transform == null) { fe.RenderTransform = transform = new TranslateTransform(); } if (this.Direction == DirectionOfMotion.TopToBottom || this.Direction == DirectionOfMotion.BottomToTop) { if (this.AxisOfFlip == AxisOfFlip.LeftOrTop) { projection.CenterOfRotationY = 0; projection.GlobalOffsetY = fe.ActualHeight / 2; transform.Y = -fe.ActualHeight / 2; } else if (this.AxisOfFlip == AxisOfFlip.RightOrBottom) { projection.CenterOfRotationY = 1.0; projection.GlobalOffsetY = -fe.ActualHeight / 2; transform.Y = fe.ActualHeight / 2; } else { projection.CenterOfRotationY = 0.5; } } else { if (this.AxisOfFlip == AxisOfFlip.LeftOrTop) { projection.CenterOfRotationX = 0; projection.GlobalOffsetX = fe.ActualWidth / 2; transform.X = -fe.ActualWidth / 2; } else if (this.AxisOfFlip == AxisOfFlip.RightOrBottom) { projection.CenterOfRotationX = 1.0; projection.GlobalOffsetX = -fe.ActualWidth / 2; transform.X = fe.ActualWidth / 2; } else { projection.CenterOfRotationX = 0.5; } } var flipAnimation = (DoubleAnimation)animation.Children[0]; Storyboard.SetTarget(flipAnimation, projection); Storyboard.SetTargetProperty(flipAnimation, Direction == DirectionOfMotion.TopToBottom || Direction == DirectionOfMotion.BottomToTop ? "RotationX" : "RotationY"); var visibilityAnimation = (ObjectAnimationUsingKeyFrames)animation.Children[1]; Storyboard.SetTarget(visibilityAnimation, fe); Storyboard.SetTargetProperty(visibilityAnimation, "Visibility"); if (this.AxisOfFlip == AxisOfFlip.Center) { const double TopAngleFull = -180; const double BottomAngleFull = 180; const double LeftAngleFull = 180; const double RightAngleFull = -180; if (this.Mode == AnimationMode.ForwardOut || this.Mode == AnimationMode.BackwardOut) { switch (this.Direction) { case DirectionOfMotion.TopToBottom: flipAnimation.From = 0; flipAnimation.To = BottomAngleFull; break; case DirectionOfMotion.BottomToTop: flipAnimation.From = 0; flipAnimation.To = TopAngleFull; break; case DirectionOfMotion.LeftToRight: flipAnimation.From = 0; flipAnimation.To = RightAngleFull; break; case DirectionOfMotion.RightToLeft: flipAnimation.From = 0; flipAnimation.To = LeftAngleFull; break; } } else //if (this.Mode == AnimationMode.In) { switch (this.Direction) { case DirectionOfMotion.TopToBottom: flipAnimation.From = -TopAngleFull; flipAnimation.To = 0; break; case DirectionOfMotion.BottomToTop: flipAnimation.From = BottomAngleFull; flipAnimation.To = 0; break; case DirectionOfMotion.LeftToRight: flipAnimation.From = LeftAngleFull; flipAnimation.To = 0; break; case DirectionOfMotion.RightToLeft: flipAnimation.From = RightAngleFull; flipAnimation.To = 0; break; } } } else { const double TopAngle = -90; const double BottomAngle = 90; const double LeftAngle = 90; const double RightAngle = -90; switch (this.Mode) { case AnimationMode.ForwardIn: case AnimationMode.BackwardOut: flipAnimation.From = 0; flipAnimation.To = 0; break; case AnimationMode.BackwardIn: switch (this.Direction) { case DirectionOfMotion.TopToBottom: case DirectionOfMotion.BottomToTop: flipAnimation.From = this.AxisOfFlip == AxisOfFlip.LeftOrTop ? TopAngle : BottomAngle; flipAnimation.To = 0; break; case DirectionOfMotion.LeftToRight: case DirectionOfMotion.RightToLeft: flipAnimation.From = this.AxisOfFlip == AxisOfFlip.LeftOrTop ? LeftAngle : RightAngle; flipAnimation.To = 0; break; } break; case AnimationMode.ForwardOut: switch (this.Direction) { case DirectionOfMotion.TopToBottom: case DirectionOfMotion.BottomToTop: flipAnimation.From = 0; flipAnimation.To = this.AxisOfFlip == AxisOfFlip.LeftOrTop ? TopAngle : BottomAngle; break; case DirectionOfMotion.LeftToRight: case DirectionOfMotion.RightToLeft: flipAnimation.From = 0; flipAnimation.To = this.AxisOfFlip == AxisOfFlip.LeftOrTop ? LeftAngle : RightAngle; break; } break; } } // NOTE: removing the projection animation by uncommenting the line below prevents the bug in WinRT where tapping a button in the transformed visual tree causes an A/V on null pointer in the Jupiter library //animation.Children.Remove(flipAnimation); }
private void ClosePopup(Popup popupPanel, PlaneProjection panelCarrot) { panelCarrot.RotationZ = 270; rectBackgroundHide.Visibility = Windows.UI.Xaml.Visibility.Collapsed; popupPanel.IsOpen = false; }
public static void ClearAnimationProperties(FrameworkElement element) { var planeProjection = element.Projection as PlaneProjection; if (planeProjection == null) element.Projection = planeProjection = new PlaneProjection(); var transform = element.RenderTransform as CompositeTransform; if (transform == null) element.RenderTransform = transform = new CompositeTransform { CenterX = 0.5, CenterY = 0.5 }; element.RenderTransformOrigin = new Point(0.5, 0.5); element.Opacity = 1; transform.Rotation = 0; transform.TranslateX = 0; transform.TranslateY = 0; transform.ScaleX = 1; transform.ScaleY = 1; planeProjection.RotationX = 0; planeProjection.RotationY = 0; planeProjection.RotationZ = 0; planeProjection.GlobalOffsetX = 0; planeProjection.GlobalOffsetY = 0; planeProjection.GlobalOffsetZ = 0; planeProjection.CenterOfRotationX = 0; planeProjection.CenterOfRotationY = 0; planeProjection.CenterOfRotationZ = 0; }
/// <summary> /// Begins the cascading transition asynchronously (waits for it to complete). /// </summary> /// <returns></returns> public async Task BeginCascadingTransitionAsync() { var transparentBrush = new SolidColorBrush(Colors.Transparent); LayoutRoot.Children.Clear(); var totalDelay = TimeSpan.FromSeconds(0); var cascadeStoryboard = new Storyboard(); var previousCharacterRect = new Rect(-100000,0,0,0); for (int i = 0; i < Text.Length; ) { int j = 1; while ( i + j < Text.Length && Text[i + j] == ' ') { j++; } var tt = new TranslateTransform(); if (CascadeIn) { tt.Y = FromVerticalOffset; } TextBlock tb = CreateTextBlock(tt); if (i > 0) { tb.Inlines.Add( new Run { Text = Text.Substring(0, i), Foreground = transparentBrush }); } var singleLetterRun = new Run { Text = Text.Substring(i, j) }; tb.Inlines.Add(singleLetterRun); //.GetPositionAtOffset(1, LogicalDirection.Backward) if (i + j < Text.Length) { tb.Inlines.Add( new Run { Text = Text.Substring(i + j), Foreground = transparentBrush }); } LayoutRoot.Children.Add(tb); DoubleAnimationUsingKeyFrames opacityAnimation = null; if (UseFade) { opacityAnimation = new DoubleAnimationUsingKeyFrames(); if (CascadeIn) tb.Opacity = 0; Storyboard.SetTarget(opacityAnimation, tb); Storyboard.SetTargetProperty(opacityAnimation, "UIElement.Opacity"); cascadeStoryboard.Children.Add(opacityAnimation); } DoubleAnimationUsingKeyFrames yAnimation = null; if (CascadeIn || CascadeOut) { yAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(yAnimation, tt); Storyboard.SetTargetProperty(yAnimation, "TranslateTransform.Y"); cascadeStoryboard.Children.Add(yAnimation); } DoubleAnimationUsingKeyFrames rotationAnimation = null; PlaneProjection planeProjection = null; if (UseRotation) { await tb.WaitForNonZeroSizeAsync(); //await Task.Delay(100); var aw = tb.ActualWidth; var ah = tb.ActualHeight; var characterRect = tb.GetCharacterRect(i); tb.Projection = planeProjection = new PlaneProjection(); planeProjection.CenterOfRotationX = (characterRect.X + (characterRect.Width / 2)) / aw; if (CascadeIn) planeProjection.RotationY = FromRotation; //var pointer = tb.ContentStart.GetPositionAtOffset(offset, LogicalDirection.Forward); //var rect = pointer.GetCharacterRect(LogicalDirection.Forward); //while ( // rect == previousCharacterRect || // rect.X - previousCharacterRect.X < 4) //{ // offset++; // if (offset > tb.ContentEnd.Offset) // break; // pointer = tb.ContentStart.GetPositionAtOffset(offset, LogicalDirection.Forward); // rect = pointer.GetCharacterRect(LogicalDirection.Forward); //} //previousCharacterRect = rect; //var x = rect.X; //var y = rect.Y; //var w = rect.Width; //var h = rect.Height; //tb.Projection = planeProjection = new PlaneProjection(); //planeProjection.CenterOfRotationX = (x + (w / 2)) / aw; //planeProjection.RotationY = FromRotation; //if (!headerPrinted) //{ // Debug.WriteLine("ActualWidth: {0}", aw); // Debug.WriteLine("ActualHeight: {0}\r\n", ah); // Debug.WriteLine("po\ti\tj\tx\ty\tw\th\tpx"); // headerPrinted = true; //} //Debug.WriteLine( // "{0:F0}\t{1:F0}\t{2:F0}\t{3:F0}\t{4:F0}\t{5:F0}\t{6:F0}\t{7:F3}", // pointer.Offset, i, j, x, y, w, h, planeProjection.CenterOfRotationX); rotationAnimation = new DoubleAnimationUsingKeyFrames(); Storyboard.SetTarget(rotationAnimation, planeProjection); Storyboard.SetTargetProperty(rotationAnimation, "PlaneProjection.RotationY"); cascadeStoryboard.Children.Add(rotationAnimation); if (CascadeIn) { rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay, Value = FromRotation }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + CascadeInDuration, EasingFunction = CascadeInEasingFunction, Value = 0 }); } if (CascadeOut) { rotationAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration, Value = 0 }); rotationAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration + CascadeOutDuration, EasingFunction = CascadeOutEasingFunction, Value = ToRotation }); } } if (CascadeIn) { yAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay, Value = FromVerticalOffset }); yAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + CascadeInDuration, EasingFunction = CascadeInEasingFunction, Value = 0 }); if (UseFade) { opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay, Value = 0 }); opacityAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + CascadeInDuration, EasingFunction = FadeInEasingFunction, Value = 1.0 }); } } if (CascadeOut) { yAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration, Value = 0 }); yAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration + CascadeOutDuration, EasingFunction = CascadeOutEasingFunction, Value = ToVerticalOffset }); if (UseFade) { opacityAnimation.KeyFrames.Add( new DiscreteDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration, Value = 1.00 }); opacityAnimation.KeyFrames.Add( new EasingDoubleKeyFrame { KeyTime = totalDelay + (CascadeIn ? CascadeInDuration : TimeSpan.Zero) + HoldDuration + CascadeOutDuration, EasingFunction = FadeOutEasingFunction, Value = 0.0 }); } } totalDelay += CascadeInterval; i += j; } EventHandler<object> eh = null; eh = (s, e) => { cascadeStoryboard.Completed -= eh; //LayoutRoot.Children.Clear(); //var tb2 = CreateTextBlock(null); //tb2.Text = Text; //LayoutRoot.Children.Add(tb2); #if CascadingTextBlock_REPEATFOREVER BeginCascadingTransition(); #else if (CascadeCompleted != null) CascadeCompleted(this, EventArgs.Empty); #endif }; cascadeStoryboard.Completed += eh; await Task.Delay(StartDelay); await cascadeStoryboard.BeginAsync(); }
private Projection CreateRotateYProjection(double degrees) { Debug.Assert(midPoint != 0); if (midPoint == 0) { midPoint = 1; } PlaneProjection pp = new PlaneProjection() { RotationY = degrees, LocalOffsetX = midPoint, CenterOfRotationX = radius / midPoint, CenterOfRotationY = 0, CenterOfRotationZ = -radius }; return pp; }