From iLab Cookbook

Toolkits: FontResizer

<< Back to Proximity Toolkit


Font Resizer - An Introductory Example of Using the Proximity Toolkit

Here we will show you step by step how to construct a simple WPF application by using the Proximity Toolkit. The software will have two words on its main window: pencil and hat. Each word is corresponding to a physical object tagged with markers that can be tracked by Vicon cameras. The words will become larger when the corresponding objects approaching the computer display. [video or pic]

Contents

? Device Setup

This example is focused on showing you how to use the toolkit to write a software. However, you will need to do the following first:

  1. Use markers to create two objects that can be tracked by Vicon Cameras: a pencil and hat.
  2. Define the space that are tracked by the Vicon cameras.
  3. Define the computer display's location and dimension.
  4. Launch the Proximity Toolkit's sever application.

Writing the software

The example here are developed using Visual Studio 2008 and .NET Framework 3.5.

First create a new WPF application in Visual Studio. Switch to the window1.xmal, add two text blocks to the window. The XMAL code will look like this:

<Window x:Class="ViconWPFTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ViconWPFTest" Height="300" Width="612" WindowState="Maximized">
    <StackPanel VerticalAlignment="Center">
        <Grid>
            <TextBlock Name="TextBlock1" Text="Pencil" FontSize="20" HorizontalAlignment="Left" Margin="400"/>
            <TextBlock Name="TextBlock2" FontSize="20" Text="Hat" HorizontalAlignment="Right" Margin="400"/>
        </Grid>
    </StackPanel>
</Window>

Note that the TextBlock's Margin size should be a value that is suitable to your display.

Then add a reference to the ProximityToolkit.dll. Select Project->Add Reference. A new window will appear and select the Browse tab. Browse to the proximity folder and select ProximityToolkit.dll and add the following code at the top of your program:

using ProximityToolkit;
using ProximityToolkit.Networking;
using ProximityToolkit.Presence;

Now we want to declare the member variables being used in the project:

private ClientDictionaryManager client = null;
private ProximitySpace space = null;
private PresenceBase pencil = null;
private PresenceBase smartboard = null;
private PresenceBase jagerHat = null;

ClientDictionaryManager is the class responsible for communicating with the shared dictionary on the server side, ProximitySpace is the class define the space that the vicon camera sees. PresenceBase is the base class for artifacts in the space.

Then you want to initialize these objects.

client = new ClientDictionaryManager();
client.Start("127.0.0.1", 888, true);

space = client.GetSpace();

pencil = space.GetPresence("Pencil");
smartboard = space.GetDisplay("Smartboard");
jagerHat = space.GetPresence("JagerHat");

pencil.WaitForEmbodiment(0);
smartboard.WaitForEmbodiment(0);
jagerHat.WaitForEmbodiment(0);

Note that it takes some time for the GetPresence() to return a valid object for the first time that it is called. Therefore, WaitForEmbodiment() is used to block the program until a valid object is obtained.

Then we want to detect the distance change between the objects th and the display. We also want to hook up a event handler when the distance changes.

space.StartMonitor(pencil, smartboard, ProximityRelation.Location);
space.StartMonitor(smartboard, jagerHat, ProximityRelation.Location);

space.RelationUpdated += new SpaceRelationHandler(space_RelationUpdated);

Last, we want to implement the event handler. All events related with space relation changes will trigger this callback,so it is important for us to identify the events. In this example, we used the presenceA property of the ProximityEventArgs to find out if the event is trigger by pencil or the hat.

The callback happens on a different thread from the main thread where it contains all the UI controls. Therefore, we need to dispatch the text font change actions through a message rather than changing it directly.

void space_RelationUpdated(ProximitySpace space, ProximityEventArgs args)
{
    if(args.PresenceA == pencil)
    {

        TextBlock1.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
                delegate()
                {
                    TextBlock1.FontSize = 1 + (int)(250 - args.Location.HorizontalDistance / 10);
                }
                ));

    }
    else if (args.PresenceA == smartboard)
    {
        TextBlock2.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
            new Action(
                delegate()
                {
                    TextBlock2.FontSize = 1 + (int)(250 - args.Location.HorizontalDistance / 10);
                }
                ));
    }
}

Now you are done! Build and run your code.

Download

Retrieved from http://grouplab.cpsc.ucalgary.ca/cookbook/index.php/Toolkits/FontResizer
Page last modified on September 21, 2010, at 01:01 PM