Showing posts with label developers. Show all posts
Showing posts with label developers. Show all posts

Wednesday, 20 February 2013

How to build a multi-touch control for Windows Phone

     What I intend for multi-touch? Windows Phone is already a multi-touch device but for some reason (I guess it is legacy of Silverlight) the standard Xaml controls don’t have a real multi-touch behavior. Real multi-touch means that I should be able to interact with two different objects at the same time with multiple fingers. What happens now for the XAML standard controls is that once a control start receiving touch events (ManipulationStarted, ManipulationEnded, ManipulationDelta) all the other controls will not receive touch events. The easiest test to do is add a standard button on the page, put one finger on the screen outside the button and try to press the button with another finger. You will see that the button does not respond to your commands. For Xaml applications this might not be a problem, but for games it becomes one. Xaml (I mean Xaml+C# or XAML+VB.NET) is fast to develop easy games.

The solution would be to build your own control and use Touch.FrameReported to “drive” it. In this sample I will build a multi-touch button. I will call it ButtonEx (some of you remember OpenNETCF J?) and I will just add three events to it: TouchDown, TouchUpInside, TouchUpOutSide (iOs MonoTouch event names). With this three events I should have better control (Click in reality is a TouchUpInside event) .
So I've created a new Windows Phone Class Library called ControlsEx and I added the control ButtonEx derived from ContentControl. I copied the standard style of the Button control (you can easily generate it from a standard button using Blend and Edit Copy command on the Button Template). I've then added the style to the /Themes/generic.xaml file inside our project.  When I create the control I will subscribe Loaded and Unloaded events as I want to start receiving Touch events when the control loads and unsubscribe the Touch events when the control gets unloaded.

  public ButtonEx()  
{
DefaultStyleKey = typeof(ButtonEx);
this.Loaded += ButtonEx_Loaded;
this.Unloaded += ButtonEx_Unloaded;
IsEnabledChanged += ButtonEx_IsEnabledChanged;
IsPressed = false;
}
void ButtonEx_Loaded(object sender, RoutedEventArgs e)
{
Touch.FrameReported += Touch_FrameReported;
}
void ButtonEx_Unloaded(object sender, RoutedEventArgs e)
{
Touch.FrameReported -= Touch_FrameReported;
}

     Now everything we need “happens” inside the Touch_FrameReported  method. For my button I am interested to trace only one finger(using its id) from TouchAction.Down until TouchAction.Up. Once the first finger is down on the surface of my control I memorize the id and track it’s actions till it leaves the screen. Depending of the control that you are building you might have to take in consideration multiple fingers. One thing that is pretty important when starting to track a finger is to see if your control is in front or not (imagine an MessageBox over your controls and when you press the Ok button you will also press the button which is in the back). To resolve this issue I’ve used TouchDevice.DirectlyOver property of the TouchPoint and the VisualTreeHelper to see if the UIElement returned by DirectlyOver is a member of my control or not.
  bool IsControlChild(DependencyObject element)  
{
DependencyObject parent = element;
while ((parent != this) && (parent != null))
parent=VisualTreeHelper.GetParent(parent);
if (parent == this)
return true;
else
return false;
}
Here is the method Touch_FrameReported method:

 void Touch_FrameReported(object sender, TouchFrameEventArgs e)  
{
if (Visibility == Visibility.Collapsed)
return;
TouchPointCollection pointCollection = e.GetTouchPoints(this);
for (int i = 0; i < pointCollection.Count; i++)
{
if (idPointer == -1)
{
if (IsEnabled&&(Visibility==Visibility.Visible) && (pointCollection[i].Action == TouchAction.Down) && IsControlChild(pointCollection[i].TouchDevice.DirectlyOver))
{
//start tracing this finger
idPointer = pointCollection[i].TouchDevice.Id;
IsPressed = true;
VisualStateManager.GoToState(this,"Pressed", true);
if (TouchDown != null)
TouchDown(this, pointCollection[i].Position);
}
}
else if ((pointCollection[i].TouchDevice.Id == idPointer) && (pointCollection[i].Action == TouchAction.Up))
{
idPointer =-1;
IsPressed = false;
UpdateIsEnabledVisualState();
if ((pointCollection[i].Position.X > 0 && pointCollection[i].Position.X < ActualWidth) && (pointCollection[i].Position.Y > 0 && pointCollection[i].Position.Y < ActualHeight))
{
if (TouchUpInside != null)
TouchUpInside(this, pointCollection[i].Position);
}
else
{
if (TouchUpOutside != null)
TouchUpOutside(this, pointCollection[i].Position);
}
}
}
}
    For the button control we don’t have to trace the movements of the finger until Up action but we might need to if we are writing a Slider control for example. The sample application that you will find in the source code uses 2 ButtonEx controls and a standard Button control. The ButtonEx should always respond to your commands (fingers).

    I’ve also used this approach to develop an multi-touch XAML game for flying an Bluetooth BeeWi helicopter. I will also have a session on the 27th February at Community Days here in Italy where I will present a session on developing a game for Windows Phone and I will use this game as a starting point. This application has multi-touch buttons, slider and joystick control.

Also have to thank the TTT (Train The Trainer) program which awarded me a beautiful Sphero for my multi-touch controls.

As always don’t hesitate to contact me if you have further questions.
NAMASTE

Monday, 25 June 2012

Why now?

    After last week's announcements from Microsoft the only thing that obsesses me is Why now? While both Surface and Windows Phone 8 products look great both announcements   seemed a little bit rushed.
    Lets start with the Surface presentation. The product is UBER COOL, but there are still a lot of things that are not cleared: how much will it cost, when exactly will be available to the public. My feeling was, and I might be right, that Google will announce its own low cost tablet and, at least this time Microsoft didn't want to be the last one to announce the new tablet. If the prices for the Google tablet are really 249USD and 199USD both Microsoft and Apple will be their salea affected by the low cost tablet from Google.
     What really impressed me was the Windows Phone Summit announce of Windows Phone 8. Again Windows Phone 8 will be a really cool product but, from what was shown, it is far from rtm. What I don't understand is the strategy that Microsoft adopted? They presented a cool new product still far from the release, the developers didn't get the new SDK, they've showed only the new start screen and what the main features that the new product will have and the MOST important said that there is no upgrade path to Windows Phone 8 for any of the current devices. Without giving a new toy to developers (that is the new SDK) the announcement did a lot of damage to the actual Windows Phone platform. On short term (till Windows Phone 8 devices will be out in the wild) the sells of Windows Phone will once again slow (nobody wants an old phone), with the sells of the phone the sales in the marketplace will slow so if you are a developer this is how you will be affected.  But the really BIG DAMAGE was done to Nokia. Here are some stock exchange considerations from today :


DJ MARKET TALK: Nomura Cuts Nokia Target Price
25/06/2012 12:36 MF-DJ
1036 GMT [Dow Jones] Nomura cuts its target price for Nokia (NOK1V.MI) (NOKA.AS) (NOK1V.HE) (NOA3.FRA) to EUR2 from EUR3.20, pointing to Microsoft's (MSFT.O) (MSF.FRA) new Windows Phone 8 product, to which none of Nokia's current phones will be able to be upgraded. "Now that Microsoft has confirmed that Windows Phone 7 devices cannot be upgraded to Windows Phone 8, we expect retailers and Nokia to run down inventory levels of the current Lumia range," says Nomura. It expects this to result in a sequential decline in the sales volumes Nokia reports for 3Q. Nomura rates the stock at neutral. Shares down 7% at EUR1.80.(michele.maatouk@dowjones.com)

DJ MARKET TALK: Nokia Shrs Lower After Slew Of Bad News -SocGen
25/06/2012 15:25 MF-DJ
1325 GMT [Dow Jones] Nokia (NOK1V.MI) (NOKA.AS) (NOK1V.HE) (NOA3.FRA) stock is 7.9% lower at EUR1.78, amid the slew of bad news last week, says Societe Generale analyst Andy Perkins. The latest Microsoft (MSFT.O) (MSF.FRA) Windows smartphone operating system won't work on Nokia's current Lumia devices, he notes. "In addition, Microsoft last week unveiled its own Surface tablet device, and it also seems as if Microsoft is considering [whether] to manufacture its own smartphones," Perkins says. "If Microsoft is starting to manufacture its own hardware, it is less likely to end up buying Nokia." Perkins' recommendation on Nokia stock is sell with a EUR1.60 target. (sven.grundberg@dowjones.com)

Today Nokia's stock values is doing again -8% with a one year "performance" of -54%. Why did Microsoft sneaked peaked Windows Phone 8 now when it is far form the release? Do they want to buy Nokia so they have to get the lowest price possible? If not they just want to kill Nokia after they have invested everything in the Windows Phone platform? If Microsoft don't want to buy Nokia then why all the Windows Phone 8 devices have Nokia maps and navigation software? In this moment Microsoft can't let anyone buy Nokia as it could affect their plans for Windows Phone 8. So again WHY NOW? No SDK, no timing for the new devices, no new details for the UI enhancements. A lot of damage with no immediate benefit (developers, Nokia, Microsoft). I can only hope there is a good strategy behind this announcement that I cannot see.

It cannot be the multi-core architecture:

This is from Wikipedia under Windows CE 7: 
7.0Released in March 2011.
  • Multi-core CPU support (SMP)
  • Wi-Fi Positioning System
  • Bluetooth 3.0 + HS
  • DLNA (Digital Living Network Alliance)
  • DRM technology
  • Media Transfer Protocol
  • Windows Phone 7 IE with Flash 10.1 support
  • NDIS 6.1 support
  • UX C++ XAML API using technologies like Windows Presentation Foundation and Silverlight for attractive and functional user interfaces
  • Advanced touch and gesture input
  • Kernel support for 3 GB physical RAM and supports ARMv7 assembly[25]
It cannot be also that the Windows 8 ARM kernel is more stable as it is the first ARM Windows Kernel that Microsoft creates so it still has to show its stability. On the other hand the CE Kernel, even if it is not one of the best, is very stable. The actual windows phone device are pretty good with a lot of applications missing because of some missing features in the SDK and unimpressive market shares.

On long term Microsoft can only benefit from maintaining a single kernel but on short term: Ooooops you did it again! Not the same as the last one (6.5 to 7.x) as there will be 100,000 aps in the marketplace but still some damage is done. Why do the DAMAGE if the backup is not ready yet? They should have waited to have the SDK ready, devices almost ready to sell, the enhanced UI ready and AMAZE everyone. As a developer and geek I am super excited about the new features and cannot wait to develop for WP8. As a small company owner that is investing everything in Windows Phone I don't agree with what Microsoft did and I have more doubts than ever before that Windows Phone will do a fast come back in the smartphone market (I hope that I am wrong and that both Nokia and Microsoft have some killer features ready).

So Why didn't they wait a little bit more at least for the Windows Phone?