using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using CBR.Core.Helpers;
using CBR.Core.Models;
using CBR.Core.Services;
namespace CBR.Views
{
/// <summary>
/// Interaction logic for SimulateView.xaml
/// </summary>
public partial class SimulateView : Window
{
public SimulateView()
{
InitializeComponent();
IsLandscape = true;
//create a dispatch timer to load the image cache
_TimeLineClock = new DispatcherTimer();
_TimeLineClock.Interval = new TimeSpan(0, 0, 2);
_TimeLineClock.IsEnabled = true;
_TimeLineClock.Tick += new EventHandler(TimeLineClockElapsed);
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
_currentPage = BookData.Pages[0];
_currentZone = null;
_DurationCounter = 0;
}
public Book BookData { get; set; }
internal Page _currentPage;
internal Zone _currentZone;
internal int _DurationCounter = 0;
public bool IsLandscape { get; set; }
private DispatcherTimer _TimeLineClock;
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
private void btnSwapLandscape_Click(object sender, RoutedEventArgs e)
{
if (IsLandscape)
{
this.mainGrid.LayoutTransform = new RotateTransform(90, 0.5, 0.5);
}
else
{
this.mainGrid.LayoutTransform = null;
}
IsLandscape = !IsLandscape;
double previous = this.ActualWidth;
this.Width = this.ActualHeight;
this.Height = previous;
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
_TimeLineClock.Stop();
_TimeLineClock.IsEnabled = false;
this.Close();
}
public void TimeLineClockElapsed(object tag, EventArgs args)
{
try
{
//retreive the image brush
ImageBrush imageBrush = (ImageBrush)rctZoom.Fill;
//increase timer duration
_DurationCounter += 2;
Page oldPage = _currentPage;
Zone oldFrame = _currentZone;
// if no frame stored or duration is over, search the new frame in the page
if (_currentZone == null || _DurationCounter > _currentZone.Duration)
BookServiceFactory.Instance.GetService(BookData).GotoFrame(BookData, ref _currentPage, ref _currentZone, 1);
//we change the page, so change the image
if( oldPage != _currentPage)
{
imageBrush.ImageSource = _currentPage.Image;
}
//we change the zone, so change the parameters
if (oldFrame != _currentZone)
{
Rect viewBox = imageBrush.Viewbox;
viewBox.X = _currentZone.X;
viewBox.Y = _currentZone.Y;
viewBox.Width = _currentZone.Width;
viewBox.Height = _currentZone.Height;
imageBrush.Viewbox = viewBox;
lblDebugInfo.Content = string.Format("Frame {0} for {1} second(s) on location ({2}, {3}, {4}, {5}) ",
_currentZone.OrderNum, _currentZone.Duration, _currentZone.X, _currentZone.Y, _currentZone.Width, _currentZone.Height);
}
}
catch (Exception err)
{
ExceptionHelper.Manage("SimulateView:TimeLineClockElapsed", err);
}
}
}
}