none
How I mak to click event with this code RRS feed

  • Question

  • I can't to do it. Please help me!!!

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Kinect;

    namespace WindowsGame3
    {
        /// <summary>
        /// This is the main type for your game
        /// </summary>
        public class Game1 : Microsoft.Xna.Framework.Game
        {
            GraphicsDeviceManager graphics;

            SpriteBatch spriteBatch;
            //SpriteFont spriteFont;

            //private Model model;
            //Model modelMouse;
            //Texture2D overlay;

            Texture2D hand;

            //MouseState mouseState;

            KinectSensor kinectSensor;

            string connectedStatus = "Not connected";
            int Sc = 70;
            private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0))
                /* Matrix.CreateRotationX(45) /* Matrix.CreateRotationY(45)*/
                * Matrix.CreateScale(3.0f);
            private Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 500), new Vector3(0, 0, 0), Vector3.Up);
            private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 1366 / 768, 0.1f, 10000f);

            //Vector2 handPosition = new Vector2();
            int xp = 0;
            int yp = 0;
            Texture2D kinectRGBVideo;


            public Game1()
            {
                graphics = new GraphicsDeviceManager(this);
                Content.RootDirectory = "Content";
                graphics.PreferredBackBufferWidth = Sc * 16;
                graphics.PreferredBackBufferHeight = Sc * 9;
            }

            /// <summary>
            /// Allows the game to perform any initialization it needs to before starting to run.
            /// This is where it can query for any required services and load any non-graphic
            /// related content.  Calling base.Initialize will enumerate through any components
            /// and initialize them as well.
            /// </summary>
            ///

            private void DiscoverKinectSensor()
            {
                foreach (KinectSensor sensor in KinectSensor.KinectSensors)
                {
                    if (sensor.Status == KinectStatus.Connected)
                    {
                        // Found one, set our sensor to this
                        kinectSensor = sensor;
                        break;
                    }
                }

                if (this.kinectSensor == null)
                {
                    connectedStatus = "Found none Kinect Sensors connected to USB";
                    return;
                }

                // You can use the kinectSensor.Status to check for status
                // and give the user some kind of feedback
                switch (kinectSensor.Status)
                {
                    case KinectStatus.Connected:
                        {
                            connectedStatus = "Status: Connected";
                            break;
                        }
                    case KinectStatus.Disconnected:
                        {
                            connectedStatus = "Status: Disconnected";
                            break;
                        }
                    case KinectStatus.NotPowered:
                        {
                            connectedStatus = "Status: Connect the power";
                            break;
                        }
                    default:
                        {
                            connectedStatus = "Status: Error";
                            break;
                        }
                }

                // Init the found and connected device
                if (kinectSensor.Status == KinectStatus.Connected)
                {
                    InitializeKinect();
                }
            }

            void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
            {
                using (ColorImageFrame colorImageFrame = e.OpenColorImageFrame())
                {
                    if (colorImageFrame != null)
                    {

                        byte[] pixelsFromFrame = new byte[colorImageFrame.PixelDataLength];

                        colorImageFrame.CopyPixelDataTo(pixelsFromFrame);

                        Color[] color = new Color[colorImageFrame.Height * colorImageFrame.Width];
                        kinectRGBVideo = new Texture2D(graphics.GraphicsDevice, colorImageFrame.Width, colorImageFrame.Height);

                        // Go through each pixel and set the bytes correctly.
                        // Remember, each pixel got a Rad, Green and Blue channel.
                        int index = 0;
                        for (int y = 0; y < colorImageFrame.Height; y++)
                        {
                            for (int x = 0; x < colorImageFrame.Width; x++, index += 4)
                            {
                                color[y * colorImageFrame.Width + x] = new Color(pixelsFromFrame[index + 2], pixelsFromFrame[index + 1], pixelsFromFrame[index + 0]);
                            }
                        }

                        // Set pixeldata from the ColorImageFrame to a Texture2D
                        kinectRGBVideo.SetData(color);
                    }
                }
            }

            private bool InitializeKinect()
            {
                kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
                kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                kinectSensor.SkeletonStream.Enable(new TransformSmoothParameters()
                {
                    Smoothing = 0.5f,
                    Correction = 0.5f,
                    Prediction = 0.5f,
                    JitterRadius = 0.05f,
                    MaxDeviationRadius = 0.04f
                });
                kinectSensor.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(kinectSensor_SkeletonFrameReady);


                try
                {
                    kinectSensor.Start();
                }
                catch
                {
                    connectedStatus = "Unable to start the Kinect Sensor";
                    return false;
                }
                return true;
            }

            void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
            {
                if (this.kinectSensor == e.Sensor)
                {
                    if (e.Status == KinectStatus.Disconnected ||
                        e.Status == KinectStatus.NotPowered)
                    {
                        this.kinectSensor = null;
                        this.DiscoverKinectSensor();
                    }
                }
            }

            void kinectSensor_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
                {
                    if (skeletonFrame != null)
                    {
                        //int skeletonSlot = 0;
                        Skeleton[] skeletonData = new Skeleton[skeletonFrame.SkeletonArrayLength];

                        skeletonFrame.CopySkeletonDataTo(skeletonData);
                        Skeleton playerSkeleton = (from s in skeletonData where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();
                        if (playerSkeleton != null)
                        {
                            
                            Joint rightHand = playerSkeleton.Joints[JointType.HandRight];
                            xp = (int)(((0.5f * rightHand.Position.X) + 0.5f) * (1366));
                            yp = (int)(((-0.5f * rightHand.Position.Y) + 0.5f) * (768));
                            //handPosition = new Vector2((((0.5f * rightHand.Position.X) + 0.5f) * (640)), (((-0.5f * rightHand.Position.Y) + 0.5f) * (480)));
                        }
                    }
                }
            }

            protected override void Initialize()
            {

                KinectSensor.KinectSensors.StatusChanged += new EventHandler<StatusChangedEventArgs>(KinectSensors_StatusChanged);
                DiscoverKinectSensor();


                // TODO: Add your initialization logic here
                //this.IsMouseVisible = true;
                base.Initialize();
            }

            /// <summary>
            /// LoadContent will be called once per game and is the place to load
            /// all of your content.
            /// </summary>
            protected override void LoadContent()
            {
                // Create a new SpriteBatch, which can be used to draw textures.
                spriteBatch = new SpriteBatch(GraphicsDevice);
                //spriteFont = Content.Load<SpriteFont>("SpriteFont1");
                //model = Content.Load<Model>("ship1");
                //modelMouse = Content.Load<Model>("mousetmp");
                hand = Content.Load<Texture2D>("hand-stop-2");


                // TODO: use this.Content to load your game content here
            }

            /// <summary>
            /// UnloadContent will be called once per game and is the place to unload
            /// all content.
            /// </summary>
            protected override void UnloadContent()
            {
                // TODO: Unload any non ContentManager content here
                kinectSensor.Stop();
                kinectSensor.Dispose();
            }

            /// <summary>
            /// Allows the game to run logic such as updating the world,
            /// checking for collisions, gathering input, and playing audio.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Update(GameTime gameTime)
            {
                // Allows the game to exit
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();

                // TODO: Add your update logic here

                base.Update(gameTime);
            }

            /// <summary>
            /// This is called when the game should draw itself.
            /// </summary>
            /// <param name="gameTime">Provides a snapshot of timing values.</param>
            protected override void Draw(GameTime gameTime)
            {
                GraphicsDevice.Clear(Color.CornflowerBlue);
           

                //DrawModel(model, world, view, projection);
                // TODO: Add your drawing code here
                //mouseState = Mouse.GetState();
                spriteBatch.Begin();
                //String xMouse = mouseState.X.ToString();
                //String yMouse = mouseState.Y.ToString();
                //Mouse.SetPosition((int)handPosition.X,(int)handPosition.Y);
                
                spriteBatch.Draw(kinectRGBVideo, new Rectangle(0, 0, 200, 150), Color.White);
                // spriteBatch.Draw(overlay, new Rectangle(0, 0, 640, 480), Color.White);
                //spriteBatch.DrawString(spriteFont, connectedStatus, new Vector2(20, 80), Color.White);
                //spriteBatch.DrawString(spriteFont, xMouse, new Vector2(20, 20), Color.White);
                //spriteBatch.DrawString(spriteFont, yMouse, new Vector2(60, 20), Color.White);
                //spriteBatch.DrawString(spriteFont, "มมมมมมมมมมมมมมม", new Vector2(20, 60), Color.White);
                
                spriteBatch.Draw(hand,new Rectangle(xp,yp,100,100), Color.White);
                spriteBatch.End();
                base.Draw(gameTime);

                //Matrix world1 = Matrix.CreateTranslation(new Vector3(new Vector2(mouseState.X,mouseState.Y),0)) * Matrix.CreateScale(3.0f);

                /*foreach (ModelMesh mesh in modelMouse.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        //effect.World = world1;
                        effect.View = view;
                        effect.Projection = projection;
                        effect.EnableDefaultLighting();
                    }

                    mesh.Draw();
                }*/
            }

            private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
            {
                Matrix[] transforms = new Matrix[model.Bones.Count];
                foreach (ModelMesh mesh in model.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        effect.World = world;
                        effect.View = view;
                        effect.Projection = projection;
                        effect.EnableDefaultLighting();
                    }

                    mesh.Draw();
                }
            }




        }
    }

    Sunday, June 24, 2012 2:11 PM