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;

namespace XNA_test_lapTexture
{
    class MouseXNA
    {
        Point dragPointA, dragPointB;
        MouseState mouseState;
        Rectangle workArea;

        public bool IsMouseCaptured;
        public Point DragPointA { get { return dragPointA; } }
        public Point DragPointB { get { return dragPointB; } }
        public Point DragPointA_Grid8 { get { return CatchGrid(DragPointA, 8); } }
        public Point DragPointB_Grid8 { get { return CatchGrid(DragPointB, 8); } }
        public Point DragPointA_Grid16 { get { return CatchGrid(DragPointA, 16); } }
        public Point DragPointB_Grid16 { get { return CatchGrid(DragPointB, 16); } }
        public Point DragPointA_Grid32 { get { return CatchGrid(DragPointA, 32); } }
        public Point DragPointB_Grid32 { get { return CatchGrid(DragPointB, 32); } }

        public MouseXNA(Rectangle wa)
        {
            IsMouseCaptured = false;
            workArea = wa;
        }

        public bool UpdateMouse()
        {
            mouseState = Mouse.GetState();
            if (mouseState.LeftButton == ButtonState.Released)
            {
                if (IsMouseCaptured && workArea.Contains(mouseState.X, mouseState.Y))
                {
                    dragPointB.X = mouseState.X - workArea.X;
                    dragPointB.Y = mouseState.Y - workArea.Y;
                }
                IsMouseCaptured = false;
            }

            if (!IsMouseCaptured)
            {
                if (mouseState.LeftButton == ButtonState.Pressed && workArea.Contains(mouseState.X, mouseState.Y))
                {
                    IsMouseCaptured = true;
                    dragPointA.X = mouseState.X - workArea.X;
                    dragPointA.Y = mouseState.Y - workArea.Y;
                }
            }
            else
                if (mouseState.LeftButton == ButtonState.Pressed && workArea.Contains(mouseState.X, mouseState.Y))
                {
                    IsMouseCaptured = true;
                    dragPointB.X = mouseState.X - workArea.X;
                    dragPointB.Y = mouseState.Y - workArea.Y;
                }
            return IsMouseCaptured;
        }

        Point CatchGrid(Point value, int gridSize)
        {
            value.X = ((value.X / gridSize) * gridSize);
            value.Y = ((value.Y / gridSize) * gridSize);
            return value;
        }
    }
}
最終更新:2012年08月02日 01:31