Skip to content

Commit

Permalink
Updated to v.0.9.49
Browse files Browse the repository at this point in the history
  • Loading branch information
Hskovsgaard committed Dec 16, 2014
1 parent 886057e commit fe6babf
Show file tree
Hide file tree
Showing 15 changed files with 718 additions and 643 deletions.
11 changes: 6 additions & 5 deletions Calibration/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
<Window x:Class="Calibration.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="The Eye Tribe BasicUI"
Title="Eye Tribe Calibration"
Height="340"
Width="310"
Closed="WindowClosed"
WindowStartupLocation="CenterScreen"
Background="Black"
Icon="favicon.ico">

<Grid>
<Grid x:Name="TrackingStatusGrid" Width="300" Height="250" Margin="4" HorizontalAlignment="Center" VerticalAlignment="Top" />

<Grid x:Name="ButtonControls" VerticalAlignment="Bottom">
<Button x:Name="btnAction" Content="Calibrate" Click="ButtonClicked" Height="35" Margin="10" VerticalAlignment="Bottom" Background="#FF575757" Foreground="White" />
<Button x:Name="btnCalibrate" Content="" Click="ButtonCalibrateClicked" Height="35" Margin="10,0,159,10" VerticalAlignment="Bottom" Background="#FF575757" Foreground="White" Focusable="False" />
<Button x:Name="btnMouse" Content="" Click="ButtonMouseClicked" Height="35" Margin="158,0,10,10" VerticalAlignment="Bottom" Background="#FF575757" Foreground="White" Focusable="False"/>
</Grid>

<TextBlock x:Name="RatingText" HorizontalAlignment="Center" Text="" VerticalAlignment="Top" Foreground="White" FontSize="15" TextAlignment="Right" TextWrapping="Wrap" FontWeight="Bold" />
</Grid>
</Window>
166 changes: 115 additions & 51 deletions Calibration/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,108 +9,183 @@
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Input;
using TETControls.Calibration;
using TETControls.Cursor;
using TETControls.TrackBox;
using TETCSharpClient.Data;
using System.Windows.Interop;
using TETCSharpClient;

using MessageBox = System.Windows.MessageBox;

namespace Calibration
{
public partial class MainWindow : IConnectionStateListener
{
private Screen activeScreen = Screen.PrimaryScreen;
private CursorControl cursorControl;

private bool isCalibrated;

public MainWindow()
{
ConnectClient();
InitializeComponent();
Loaded += (sender, args) => InitClient();
this.ContentRendered += (sender, args) => InitClient();
this.KeyDown += MainWindow_KeyDown;
}

private void ConnectClient()
private void InitClient()
{
// Create a client for the eye tracker
// Activate/connect client
GazeManager.Instance.Activate(GazeManager.ApiVersion.VERSION_1_0, GazeManager.ClientMode.Push);
}

private void InitClient()
{
// Default content of the action button
btnAction.Content = "Calibrate";
// Listen for changes in connection to server
GazeManager.Instance.AddConnectionStateListener(this);

// Fetch current status
OnConnectionStateChanged(GazeManager.Instance.IsActivated);

// Add a fresh instance of the trackbox in case we reinitialize the client connection.
TrackingStatusGrid.Children.Clear();
TrackingStatusGrid.Children.Add(new TrackBoxStatus());

// Add listener if EyeTribe Server is closed
GazeManager.Instance.AddConnectionStateListener(this);
UpdateState();
}

// What is the current connection state
OnConnectionStateChanged(GazeManager.Instance.IsActivated);
private void MainWindow_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e == null)
return;

if (GazeManager.Instance.IsCalibrated)
switch (e.Key)
{
// Get the latest successful calibration from the EyeTribe server
RatingText.Text = RatingFunction(GazeManager.Instance.LastCalibrationResult);
btnAction.Content = "Re-Calibrate";
// Start calibration on hitting "C"
case Key.C:
ButtonCalibrateClicked(this, null);
break;

// Toggle mouse redirect with "M"
case Key.M:
ButtonMouseClicked(this, null);
break;

// Turn cursor control off on hitting Escape
case Key.Escape:
if (cursorControl != null)
cursorControl.Enabled = false;

UpdateState();
break;
}
}

private void ButtonClicked(object sender, RoutedEventArgs e)
public void OnConnectionStateChanged(bool IsActivated)
{
if (GazeManager.Instance.IsActivated)
// The connection state listener detects when the connection to the EyeTribe server changes
if (btnCalibrate.Dispatcher.Thread != Thread.CurrentThread)
{
Calibrate();
this.Dispatcher.BeginInvoke(new MethodInvoker(() => OnConnectionStateChanged(IsActivated)));
return;
}
else
{
ConnectClient();

if (!IsActivated)
GazeManager.Instance.Deactivate();

UpdateState();
}

private void ButtonCalibrateClicked(object sender, RoutedEventArgs e)
{
// Check connectivitiy status
if (GazeManager.Instance.IsActivated == false)
InitClient();
}

// API needs to be active to start calibrating
if (GazeManager.Instance.IsActivated)
Calibrate();
else
UpdateState(); // show reconnect
}

private void Calibrate()
private void ButtonMouseClicked(object sender, RoutedEventArgs e)
{
btnAction.Content = "Re-Calibrate";
if (GazeManager.Instance.IsCalibrated == false)
return;

//Run the calibration on 'this' monitor
var ActiveScreen = Screen.FromHandle(new WindowInteropHelper(this).Handle);
if (cursorControl == null)
cursorControl = new CursorControl(activeScreen, true, true); // Lazy initialization
else
cursorControl.Enabled = !cursorControl.Enabled; // Toggle on/off

UpdateState();
}

private void Calibrate()
{
// Update screen to calibrate where the window currently is
activeScreen = Screen.FromHandle(new WindowInteropHelper(this).Handle);

// Initialize and start the calibration
CalibrationRunner calRunner = new CalibrationRunner(ActiveScreen, ActiveScreen.Bounds.Size, 9);
CalibrationRunner calRunner = new CalibrationRunner(activeScreen, activeScreen.Bounds.Size, 9);
calRunner.OnResult += calRunner_OnResult;
calRunner.Start();
}

private void calRunner_OnResult(object sender, CalibrationRunnerEventArgs e)
{
// Invoke on UI thread since we are accessing UI elements
if (RatingText.Dispatcher.Thread != Thread.CurrentThread)
{
this.Dispatcher.BeginInvoke(new MethodInvoker(() => calRunner_OnResult(sender, e)));
return;
}

// Show calibration results rating
if (e.Result == CalibrationRunnerResult.Success)
{
RatingText.Text = RatingFunction(e.CalibrationResult);
isCalibrated = true;
UpdateState();
}
else
{
System.Windows.MessageBox.Show("Calibration failed, please try again");
}
MessageBox.Show(this, "Calibration failed, please try again");
}

private void WindowClosed(object sender, EventArgs e)
private void UpdateState()
{
GazeManager.Instance.Deactivate();
// No connection
if (GazeManager.Instance.IsActivated == false)
{
btnCalibrate.Content = "Connect";
btnMouse.Content = "";
RatingText.Text = "";
return;
}

if (GazeManager.Instance.IsCalibrated == false)
{
btnCalibrate.Content = "Calibrate";
}
else
{
btnCalibrate.Content = "Recalibrate";

// Set mouse-button label
btnMouse.Content = "Mouse control On";

if (cursorControl != null && cursorControl.Enabled)
btnMouse.Content = "Mouse control Off";

if (GazeManager.Instance.LastCalibrationResult != null)
RatingText.Text = RatingFunction(GazeManager.Instance.LastCalibrationResult);
}
}

public string RatingFunction(CalibrationResult result)
private string RatingFunction(CalibrationResult result)
{
if (result == null)
return "";

var accuracy = result.AverageErrorDegree;
double accuracy = result.AverageErrorDegree;

if (accuracy < 0.5)
return "Calibration Quality: PERFECT";
Expand All @@ -127,20 +202,9 @@ public string RatingFunction(CalibrationResult result)
return "Calibration Quality: REDO";
}

public void OnConnectionStateChanged(bool IsActivated)
private void WindowClosed(object sender, EventArgs e)
{
// The connection state listener detects when the connection to the EyeTribe server changes
if (btnAction.Dispatcher.Thread != Thread.CurrentThread)
{
this.Dispatcher.BeginInvoke(new MethodInvoker(() => OnConnectionStateChanged(IsActivated)));
return;
}
if (!IsActivated)
{
GazeManager.Instance.Deactivate();
RatingText.Text = "";
btnAction.Content = "Re-Connect";
}
GazeManager.Instance.Deactivate();
}
}
}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ To build, open solution file in compliant [Visual Studio](http://www.visualstudi

Changelog
----
0.9.49 (2014-12-16)

- CalibrationSample introduces RefreshAPIConnection public method that helps to refresh listeners when the server has been manually restarted
- Simplified the calibration runner
- Fixed potential threading issue in trackbox control
- Trackbox sample queue is dependent on framerate
- Soved UI-update issue when closing Scroll sample
- Updated C# SDK
- Minor performance improvements

0.9.40 (2014-07-30)

- Stability improvements for the calibration runner
Expand Down
8 changes: 4 additions & 4 deletions Scroll/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
<ControlTemplate TargetType="{x:Type Scroll:ImageButton}">
<Grid Margin="5,5,5,5">
<Image x:Name="VisibleIcon"
Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}"
Stretch="Fill"
Width="100"
Height="100"/>
Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}"
Stretch="Fill"
Width="100"
Height="100"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
Expand Down
52 changes: 32 additions & 20 deletions Scroll/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,22 +115,22 @@ public MainWindow()
scrollTimer.Tick += ScrollTimerTick;

Loaded += (sender, args) =>
{
if (Screen.PrimaryScreen.Bounds.Width > MAX_IMAGE_WIDTH)
WebImage.Width = MAX_IMAGE_WIDTH * dpiScale;
else
WebImage.Width = Screen.PrimaryScreen.Bounds.Width * dpiScale;

// Transformation matrix that accomodate for the DPI settings
var presentationSource = PresentationSource.FromVisual(this);
transfrm = presentationSource.CompositionTarget.TransformFromDevice;

// enable stylus (touch) events
if (IsTouchEnabled)
Factory.EnableStylusEvents(this);

ExecuteSelectedButton("newyorktimes");
};
{
if (Screen.PrimaryScreen.Bounds.Width > MAX_IMAGE_WIDTH)
WebImage.Width = MAX_IMAGE_WIDTH * dpiScale;
else
WebImage.Width = Screen.PrimaryScreen.Bounds.Width * dpiScale;

// Transformation matrix that accomodate for the DPI settings
var presentationSource = PresentationSource.FromVisual(this);
transfrm = presentationSource.CompositionTarget.TransformFromDevice;

// enable stylus (touch) events
if (IsTouchEnabled)
Factory.EnableStylusEvents(this);

ExecuteSelectedButton("newyorktimes");
};
}


Expand Down Expand Up @@ -180,14 +180,15 @@ private void TapUp(object sender, MouseButtonEventArgs e)

private void WindowKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.VolumeDown || e.Key == Key.VolumeUp || e.Key == Key.Escape)
Close();
DoTapDown();
}

private void WindowKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
DoTapUp();
if (e.Key == Key.VolumeDown || e.Key == Key.VolumeUp || e.Key == Key.Escape)
Close();
else
DoTapUp();
}

private void DoTapDown()
Expand Down Expand Up @@ -326,9 +327,20 @@ private void ExecuteSelectedButton(string selectedButtonName)
}
}

private static void CleanUp()
private void CleanUp()
{
GazeManager.Instance.Deactivate();
scrollTimer.Stop();
// De-register events
if (IsTouchEnabled)
{
StylusDown -= MainWindowStylusDown;
StylusUp -= MainWindowStylusUp;
}
PreviewMouseDown -= TapDown;
PreviewMouseUp -= TapUp;
KeyDown -= WindowKeyDown;
KeyUp -= WindowKeyUp;
}

protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
Expand Down
7 changes: 4 additions & 3 deletions TETControls/Calibration/CalibrationPointWPF.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
Height="Auto" Width="Auto">

<Grid x:Name="Target" Margin="0,0,0,0">
<Ellipse x:Name="TargetCenter" Width="50" Height="50" Fill="White" Stroke="#888888" RenderTransformOrigin="0.5,0.5" StrokeThickness="2" />
<Ellipse x:Name="TargetCenterBlackDot" Width="7" Height="7" Fill="#333333" />
<Ellipse x:Name="TargetCenterDot" Width="3" Height="3" Fill="White" />
<Ellipse x:Name="TargetBackground" Width="40" Height="40" Fill="Black" />
<Ellipse x:Name="TargetCenter" Width="13" Height="13" RenderTransformOrigin="0.5,0.5" StrokeThickness="2" Panel.ZIndex="1" Fill="#FF383838" Stroke="Black" />
<Rectangle Fill="White" Height="8" Margin="0" VerticalAlignment="Center"/>
<Rectangle Fill="White" HorizontalAlignment="Center" Margin="0" Width="8"/>
</Grid>
</UserControl>
Loading

0 comments on commit fe6babf

Please sign in to comment.