00001 using System.Windows.Forms;
00002 using System;
00003 using System.Collections;
00004 using System.Collections.Generic;
00005 using System.Text;
00006 using System.Diagnostics;
00007 using Microsoft.DirectX;
00008 using Microsoft.DirectX.Direct3D;
00009 using Microsoft.Samples.DirectX.UtilityToolkit;
00010
00011 namespace DXGfxLib
00012 {
00022 public class PointAndClickHandler : IInputHandler
00023 {
00024 private Avatar currentAvatar = null;
00025
00026 private bool ready = false;
00027
00028 public Avatar CurrentAvatar
00029 {
00030 get { return currentAvatar; }
00031 set { currentAvatar = value; ready = true; }
00032 }
00033
00034 public PointAndClickHandler()
00035 {
00036 }
00037
00038 public void Dispose()
00039 {
00040 }
00041
00042 public virtual void OnKeyPress(KeyEventArgs e)
00043 {
00044 if (!ready)
00045 return;
00046 }
00047
00048 public virtual void OnKeyDown(KeyEventArgs e)
00049 {
00050 if (!ready)
00051 return;
00052 }
00053
00054 public virtual void OnKeyUp(KeyEventArgs e)
00055 {
00056 if (!ready)
00057 return;
00058 }
00059
00060 public virtual void OnMouseMove(MouseEventArgs e)
00061 {
00062 if (!ready)
00063 return;
00064 }
00065
00066 public virtual void OnMouseDown(MouseEventArgs e)
00067 {
00068 if (!ready)
00069 return;
00070 }
00071
00072 public virtual void OnMouseUp(MouseEventArgs e)
00073 {
00074 if (!ready)
00075 return;
00076 }
00077
00078 public virtual void OnMouseWheel(MouseEventArgs e)
00079 {
00080 if (!ready)
00081 return;
00082 }
00083
00084 public virtual void OnMouseClick(MouseEventArgs e)
00085 {
00086 if (!ready)
00087 return;
00088
00089 Vector2 point = new Vector2(e.X, e.Y);
00090
00091 Vector3 rayO; Vector3 rayD;
00092 MathUtil.RayFromScreenPos(DXGfxManager.GetGlobalInstance().frameworkInUse.Device.Viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, Matrix.Multiply(DXGfxManager.GetGlobalInstance().CurrentCamera.WorldMatrix, DXGfxManager.GetGlobalInstance().CurrentCamera.ViewMatrix), point, out rayO, out rayD);
00093 List<SceneObject> theList = new List<SceneObject>();
00094
00095 if (DXGfxManager.GetGlobalInstance().GetScene().Intersect(rayO, rayD, ref theList))
00096 {
00097 foreach (SceneObject obj in theList)
00098 {
00099 obj.HasBeenClicked = true;
00100 }
00101 }
00102
00103 if (currentAvatar != null)
00104 {
00105 Vector3 dest = DXGfxManager.GetGlobalInstance().GetScene().PickEnvironmentPosition(DXGfxManager.GetGlobalInstance().frameworkInUse.Device.Viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, Matrix.Multiply(DXGfxManager.GetGlobalInstance().CurrentCamera.WorldMatrix, DXGfxManager.GetGlobalInstance().CurrentCamera.ViewMatrix), point);
00106 currentAvatar.MoveTo(dest);
00107 }
00108 }
00109
00110 public virtual void OnMouseDoubleClick(MouseEventArgs e)
00111 {
00112 if (!ready)
00113 return;
00114
00115 Vector2 point = new Vector2(e.X, e.Y);
00116
00117 Viewport viewport = DXGfxManager.GetGlobalInstance().frameworkInUse.Device.Viewport;
00118 viewport.Width = DXGfxManager.GetGlobalInstance().width;
00119 viewport.Height = DXGfxManager.GetGlobalInstance().height;
00120
00121 Vector3 rayO; Vector3 rayD;
00122 MathUtil.RayFromScreenPos(viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, DXGfxManager.GetGlobalInstance().CurrentCamera.ViewMatrix, point, out rayO, out rayD);
00123 List<SceneObject> theList = new List<SceneObject>();
00124
00125 if (DXGfxManager.GetGlobalInstance().GetScene().Intersect(rayO, rayD, ref theList))
00126 {
00127 foreach (SceneObject obj in theList)
00128 {
00129 obj.HasBeenClicked = true;
00130 }
00131 }
00132
00133 if (currentAvatar != null)
00134 {
00135 Vector3 dest = DXGfxManager.GetGlobalInstance().GetScene().PickEnvironmentPosition(viewport, DXGfxManager.GetGlobalInstance().CurrentCamera.ProjectionMatrix, DXGfxManager.GetGlobalInstance().CurrentCamera.ViewMatrix, point);
00136 currentAvatar.MoveTo(dest);
00137 }
00138 }
00139
00140 public virtual void WndProc(ref Message m)
00141 {
00142 if (!ready)
00143 return;
00144 }
00145
00146 public bool HandleMessages(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam)
00147 {
00148 DXGfxManager.GetGlobalInstance().CurrentCamera.HandleMessages(hWnd, msg, wParam, lParam);
00149 return true;
00150 }
00151
00152 public IntPtr OnMsgProc(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam)
00153 {
00154 DXGfxManager.GetGlobalInstance().CurrentCamera.HandleMessages(hWnd, msg, wParam, lParam);
00155 return IntPtr.Zero;
00156 }
00157
00158 public void FrameMove(float elapsedTime)
00159 {
00160
00161 if (DXGfxManager.GetGlobalInstance().CurrentAvatar != null)
00162 {
00163 DXGfxManager.GetGlobalInstance().CurrentAvatar.FrameMove(elapsedTime);
00164 DXGfxManager.GetGlobalInstance().CurrentCamera.FrameMove(elapsedTime);
00165 }
00166 }
00167 }
00168 }