WEB开发网
开发学院图形图像Flash silverlight2 游戏 1 你能坚持多少秒 阅读

silverlight2 游戏 1 你能坚持多少秒

 2009-05-15 12:08:26 来源:WEB开发网   
核心提示: DragManager.cs 1using System; 2using System.Net; 3using System.Windows; 4using System.Windows.Controls; 5using System.Windows.Documents; 6using S

DragManager.cs

  1using System;
  2using System.Net;
  3using System.Windows;
  4using System.Windows.Controls;
  5using System.Windows.Documents;
  6using System.Windows.Ink;
  7using System.Windows.Input;
  8using System.Windows.Media;
  9using System.Windows.Media.Animation;
 10using System.Windows.Shapes;
 11using System.Collections.Generic;
 12
 13namespace Movebox
 14{
 15    public class DragManager
 16    {
 17        private bool isStartMove = true;
 18        private bool isDragging = false;
 19        private Point lastMousePosition;
 20        private UIElement layoutRoot;
 21        private UIElement elementToDrag;
 22
 23        /**//// <summary>
 24        /// 碰撞事件(未用)
 25        /// </summary>
 26        public event EventHandler<CollisionEventArgs> OnCollision;
 27        /**//// <summary>
 28        /// 第一次移动触发事件
 29        /// </summary>
 30        public event EventHandler<EventArgs> OnFristTimeMove;
 31        /**//// <summary>
 32        /// 拖动事件
 33        /// </summary>
 34        public event EventHandler<EventArgs> OnDragMove;
 35
 36        public DragManager(UIElement layoutRoot)
 37        {
 38            this.layoutRoot = layoutRoot;
 39        }
 40
 41
 42        public void EnableDragableElement(UIElement elementToDrag)
 43        {
 44            this.elementToDrag = elementToDrag;
 45
 46            this.elementToDrag.MouseLeftButtonDown += element_MouseLeftButtonDown;
 47            this.elementToDrag.MouseMove += elementToDrag_MouseMove;
 48            this.elementToDrag.MouseLeftButtonUp += elementToDrag_MouseLeftButtonUp;
 49
 50        }
 51
 52        public void DisableDragableElement()
 53        {
 54            isDragging = false;
 55            elementToDrag.MouseLeftButtonDown -= element_MouseLeftButtonDown;
 56            elementToDrag.MouseMove -= elementToDrag_MouseMove;
 57            elementToDrag.MouseLeftButtonUp -= elementToDrag_MouseLeftButtonUp;
 58
 59        }
 60
 61        void elementToDrag_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
 62        {
 63            Owner element = (Owner)sender;
 64            element.Cursor =null;
 65            element.ReleaseMouseCapture();
 66            isDragging = false;
 67        }
 68
 69        void elementToDrag_MouseMove(object sender, MouseEventArgs e)
 70        {
 71            if (isDragging)
 72            {
 73
 74                Owner element = (Owner)sender;
 75               // TranslateTransform transform = GetTranslateTransform(element);
 76                Point currentMousePosition = e.GetPosition(layoutRoot);
 77                double mouseX = currentMousePosition.X - lastMousePosition.X;
 78                double mouseY = currentMousePosition.Y - lastMousePosition.Y;
 79                element.X += mouseX;
 80                element.Y += mouseY;
 81
 82                if (OnCollision != null)
 83                {
 84                    List<UIElement> collidedElements = VisualTreeHelper.FindElementsInHostCoordinates(currentMousePosition, layoutRoot) as List<UIElement>;
 85                    collidedElements.Remove(element);
 86                    collidedElements.Remove(layoutRoot);
 87
 88                    if (collidedElements.Count > 0)
 89                    {
 90                        CollisionEventArgs args = new CollisionEventArgs() { Element = element, Position = currentMousePosition, CollidedElements = collidedElements };
 91                        OnCollision(this, args);
 92                    }
 93                }
 94
 95                EventArgs moveagrs = new EventArgs();
 96                OnDragMove(this, moveagrs);
 97
 98                lastMousePosition = currentMousePosition;
 99
100            }
101        }
102
103        void element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
104        {
105            isDragging = true;
106            if (isStartMove)
107            {
108                EventArgs args = new EventArgs() ;
109                OnFristTimeMove(this, args);
110                isStartMove=false;
111            }
112            Owner element = (Owner)sender;
113            element.Cursor = Cursors.Hand;
114
115            lastMousePosition = e.GetPosition(layoutRoot);
116            ((UIElement)sender).CaptureMouse();
117        }
118
119        private TranslateTransform GetTranslateTransform(UIElement element)
120        {
121            TranslateTransform translateTransform = null;
122            if (element.RenderTransform is TranslateTransform)
123            {
124                translateTransform = element.RenderTransform as TranslateTransform;
125            }
126            else if (element.RenderTransform is TransformGroup)
127            {
128                TransformGroup group = element.RenderTransform as TransformGroup;
129                foreach (GeneralTransform transform in group.Children)
130                {
131                    if (transform is TranslateTransform)
132                    {
133                        translateTransform = transform as TranslateTransform;
134                    }
135                }
136            }
137            else
138            {
139                translateTransform = new TranslateTransform();
140                element.RenderTransform = translateTransform;
141            }
142            return translateTransform;
143        }
144
145    }
146
147    public class CollisionEventArgs : EventArgs
148    {
149
150        public UIElement Element { get; set; }
151        public Point Position { get; set; }
152        public List<UIElement> CollidedElements { get; set; }
153    }
154
155}
156

上一页  1 2 3 4 5  下一页

Tags:silverlight 游戏 坚持

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接