资源说明:using System;
using System.Speech.Synthesis;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
namespace BookWriter3D
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
///
/// Public constructor
///
public MainWindow()
{
InitializeComponent();
}
///
/// Represents whether the book is open or not.
///
bool IsBookOpen;
///
/// Event handler for the Loaded event of the MainWindow
///
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
CloseBook(0); // Book starts closed
// Make book fade in
DoubleAnimation da = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(2)));
da.DecelerationRatio = 1;
_Main3D.BeginAnimation(UIElement.OpacityProperty, da);
}
///
/// Event handler for the MouseDown event of the cover, back cover, spine and edges
///
private void Cover_MouseDown(object sender, MouseButtonEventArgs e)
{
if (IsBookOpen)
CloseBook(1.5);
else
OpenBook(1.5);
}
///
/// Event handler for the MouseDoubleClick event of the TextBoxes
///
private void Page_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Read page content out loud
SpeechSynthesizer synth = new SpeechSynthesizer();
synth.SpeakAsync(((TextBox)sender).Text);
}
///
/// Event handler for the PreviewMouseRightButtonDown event of the InkCanvas (right page)
///
private void InkCanvas_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
// Switch InkCanvas editing mode
InkCanvas ic = sender as InkCanvas;
ic.EditingMode = (ic.EditingMode == InkCanvasEditingMode.Ink) ? InkCanvasEditingMode.EraseByPoint : InkCanvasEditingMode.Ink;
}
///
/// Opens the 3D book.
///
/// Time in seconds that the animation will take.
void OpenBook(double durationSeconds)
{
// Transform3D_LeftRotation
RotateTransform3D rot = (RotateTransform3D)TryFindResource("Transform3D_LeftRotation");
DoubleAnimation da = new DoubleAnimation(15, new Duration(TimeSpan.FromSeconds(durationSeconds)));
da.DecelerationRatio = 1;
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_RightRotation
rot = (RotateTransform3D)TryFindResource("Transform3D_RightRotation");
da = new DoubleAnimation(-15, new Duration(TimeSpan.FromSeconds(durationSeconds)));
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_SpineRotation
rot = (RotateTransform3D)TryFindResource("Transform3D_SpineRotation");
da = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(0.8333 * durationSeconds)));
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_SpineCoverTranslation
TranslateTransform3D trans = (TranslateTransform3D)TryFindResource("Transform3D_SpineCoverTranslation");
da = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(0.8333 * durationSeconds)));
trans.BeginAnimation(TranslateTransform3D.OffsetXProperty, da);
// _Main3D.Camera
Point3DAnimation pa = new Point3DAnimation(new Point3D(0, -2.5, 6.5), new Duration(TimeSpan.FromSeconds(durationSeconds)));
pa.AccelerationRatio = 0.5;
pa.DecelerationRatio = 0.5;
((PerspectiveCamera)_Main3D.Camera).BeginAnimation(PerspectiveCamera.PositionProperty, pa);
// Now the book is open.
IsBookOpen = true;
}
///
/// Closes the 3D book.
///
/// Time in seconds that the animation will take.
void CloseBook(double durationSeconds)
{
// Transform3D_LeftRotation
RotateTransform3D rot = (RotateTransform3D)TryFindResource("Transform3D_LeftRotation");
DoubleAnimation da = new DoubleAnimation(180, new Duration(TimeSpan.FromSeconds(durationSeconds)));
da.DecelerationRatio = 1;
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_RightRotation
rot = (RotateTransform3D)TryFindResource("Transform3D_RightRotation");
da = new DoubleAnimation(0, new Duration(TimeSpan.FromSeconds(durationSeconds)));
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_SpineRotation
rot = (RotateTransform3D)TryFindResource("Transform3D_SpineRotation");
da = new DoubleAnimation(90, new Duration(TimeSpan.FromSeconds(0.8333 * durationSeconds)));
rot.Rotation.BeginAnimation(AxisAngleRotation3D.AngleProperty, da);
// Transform3D_SpineCoverTranslation
TranslateTransform3D trans = (TranslateTransform3D)TryFindResource("Transform3D_SpineCoverTranslation");
da = new DoubleAnimation(-0.125, new Duration(TimeSpan.FromSeconds(0.8333 * durationSeconds)));
trans.BeginAnimation(TranslateTransform3D.OffsetXProperty, da);
// _Main3D.Camera
Point3DAnimation pa = new Point3DAnimation(new Point3D(0.72, -2.5, 6.5), new Duration(TimeSpan.FromSeconds(durationSeconds)));
pa.AccelerationRatio = 0.5;
pa.DecelerationRatio = 0.5;
((PerspectiveCamera)_Main3D.Camera).BeginAnimation(PerspectiveCamera.PositionProperty, pa);
// Now the book is closed.
IsBookOpen = false;
}
}
}
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。