Tuesday, May 3, 2016

Enabling multitouch using CocosSharp

It's been a long time since my last blog post due to personal and professional reasons. Anyhow, now I'm planning to get back in the saddle and start blogging on a regular basis again. Also, this happens to be blog post number 100. Yay!

Short version


To enable multitouch in CocosSharp (Forms version) you must create a custom renderer to set the MultipleTouchEnabled on the CCGameView (platform specific code) and then you must inherit from the CocosSharpView in order to make the registration of the custom renderer work. Code in the bottom of the post.

Long version


I've started a little secret project of my own that required two things:

  1. A simple framework for drawing stuff
  2. A simple input scheme for multitouch

So I checked out the available frameworks out there for Xamarin (especially for Xamarin Forms) and I decided to give CocosSharp a run for its money. I've done some small CocosSharp projects before and it fits my needs for the prototyping of this app.

I'm using the Forms version of CocosSharp since I'm a big Xamarin Forms fan.

The issue


One of the first things I noticed was that the multitouch simply didn't work as I expected. No matter what properties I set there was no multitouch going on. After some google-fu I found out that you have to set the MultipleTouchEnabled to true on the CCGameView instance. Well, the CCGameView is platform specific and there is no exposure of this property in the platform agnostic CocosSharpView that you use in the forms project.

Custom renderer to the rescue


Well, that's a simple fix I figured. Simply create a custom renderer and set the property directly.

    protected override void OnElementChanged (ElementChangedEventArgs<CocosSharpView> e)
    {
        base.OnElementChanged (e);

        if (Control != null) {
            Control.MultipleTouchEnabled = true;
        }
    }

That didn't work. In fact, the method is never called. (Yes, I registered the renderer! :) ).

Create a custom control (the work around)


So the simple work around for this was to create a custom control.

    public class MyCocosSharpView : CocosSharpView 
    {
    }

And then use MyCocosSharpView instead of CocosSharpView. I'm not sure why it behaves this way, but its got to have something to do with the registration of the custom renderer. Hopefully I'll get some time to drill down in the source code and find out why.

What I would like


The MultpleTouchEnabled property should be visible from the CocosSharpView class. That is at least what I'll suggest to the CocosSharp team.

The code


These are the interesting parts of the code. 



4 comments:

  1. In Xamarin.Forms.Forms.Init(), the system searches all loaded assemblies for the ExportRendererAttributes. This creates a Dictionary of the PCL view type and the renderers type. If you register a renderer for CocosSharpView, then there will be two for the same type. The assembly which was loaded last wins.
    Xamarin.Forms.Registrar.RegisterAll is smart enough to load their own assembly first so that custom renderers will always override the XF ones, but CocosSharp is just another external assembly. Whether your renderer or the one from CocosSharp is used will be pretty much random.

    ReplyDelete
    Replies
    1. That makes a lot of sense! Thanks! What would be the solution in your opinion? Inherit the control as I do in the sample above?

      Delete