ZunepadDaemon Tips

From XNAWiki
Jump to: navigation, search

The ZunepadDaemon is meant to be modular and easy to use, so it is simple to set up and use. Here is a sample of how to set up the ZunepadDaemon and subscribe to the events. This example changes color whenever the user flicks the pad, assuming you have the all of the code for the ZunepadDaemon in a file in your program. All of the other events in the Zunepad daemon work exactly the same way.

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
 
namespace ZunepadDaemonExample
{
    class ZunepadDaemonExample : Game
    { 
        //make a reference to a zunepaddaemon.
        private ZunepadDaemon daemon;
 
        private Color color;
        GraphicsDeviceManager gdm;
 
        public ZunepadDaemonExample()
            : base()
        {
            this.color = Color.White;
            this.gdm = new GraphicsDeviceManager(this);
            TargetElapsedTime = TimeSpan.FromSeconds(1 / 30.0);
        }
 
        protected override void Initialize()
        {
 
            base.Initialize();
            //instantiate the daemon.
            daemon = new ZunepadDaemon();
            //add the daemon to our game's components.
            this.Components.Add(daemon);
            //enable the daemon.
            daemon.Enabled = true;
            //The events are all static so a class can subscribe to the events without having to know about who 
            //created the instance.
            ZunepadDaemon.Flick += new FlickHandler(ZunepadDaemon_Flick);
            ZunepadDaemon.ClickDown += new DpadDownHandler(ZunepadDaemon_ClickDown);
        }
 
        void ZunepadDaemon_ClickDown(DpadEventArgs e)
        {
            if (e.AbsoluteButtons == ZuneButtons.Back)
                this.Exit();
        }
 
        void ZunepadDaemon_Flick(FlickEventArgs e)
        {
            //change the color of our background
            this.color = new Color((byte)((this.color.R + 30) % 255),
                                   (byte)((this.color.R + 30) % 255),
                                   (byte)((this.color.R + 30) % 255));
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(this.color);
        }
 
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
                using (ZunepadDaemonExample game = new ZunepadDaemonExample())
                {
                    game.Run();
                }
            }
        }
    }
}

If you've your game if your game is rendered in landscape mode (see Rendering in Landscape Mode), then this code will adjust the ZunepadDaemon and its events so that the x axis becomes the y axis, the y becomes the x etc.

this.daemon.SetOrientation(ScreenOrientation.Landscape);

However each event provides a way to access the raw data of the event. For instance to modify the the subscription to the flick event in the above example:

void ZunepadDaemon_Flick(FlickEventArgs e)
{
    //change the color of our background
    if (e.RawVelocity.X > 1f)
        this.color = Color.White;
    else
        this.color = Color.Blue;
}