2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Windows.Forms;
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
2019-08-16 04:39:18 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Actions
|
|
|
|
{
|
|
|
|
internal class MouseInput : IDisposable
|
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
// Mouse input
|
2019-08-08 02:21:57 +00:00
|
|
|
private RawMouse mouse;
|
2009-04-19 18:07:22 +00:00
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructor / Disposer
|
|
|
|
|
|
|
|
// Constructor
|
|
|
|
public MouseInput(Control source)
|
|
|
|
{
|
|
|
|
// Start mouse input
|
2019-08-08 02:21:57 +00:00
|
|
|
mouse = new RawMouse(source);
|
2014-09-17 13:16:11 +00:00
|
|
|
|
2009-04-19 18:07:22 +00:00
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Disposer
|
|
|
|
public void Dispose()
|
|
|
|
{
|
2019-08-08 02:21:57 +00:00
|
|
|
if(mouse != null)
|
2009-04-19 18:07:22 +00:00
|
|
|
{
|
|
|
|
mouse.Dispose();
|
|
|
|
mouse = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Processing
|
|
|
|
|
|
|
|
// This processes the input
|
|
|
|
public Vector2D Process()
|
|
|
|
{
|
2019-08-08 02:21:57 +00:00
|
|
|
MouseState ms = mouse.Poll();
|
|
|
|
|
|
|
|
// Calculate changes depending on sensitivity
|
|
|
|
float changex = ms.X * General.Settings.VisualMouseSensX * General.Settings.MouseSpeed * 0.01f;
|
|
|
|
float changey = ms.Y * General.Settings.VisualMouseSensY * General.Settings.MouseSpeed * 0.01f;
|
|
|
|
|
|
|
|
return new Vector2D(changex, changey);
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
2019-08-08 02:21:57 +00:00
|
|
|
|
|
|
|
public struct MouseState
|
|
|
|
{
|
|
|
|
public MouseState(float x, float y) { X = x; Y = y; }
|
|
|
|
public float X { get; }
|
|
|
|
public float Y { get; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public class RawMouse
|
|
|
|
{
|
|
|
|
public RawMouse(System.Windows.Forms.Control control)
|
|
|
|
{
|
2019-08-16 04:39:18 +00:00
|
|
|
Handle = RawMouse_New(control.Handle);
|
|
|
|
if (Handle == IntPtr.Zero)
|
2019-08-16 11:07:57 +00:00
|
|
|
throw new Exception("RawMouse_New failed");
|
2019-08-16 04:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~RawMouse()
|
|
|
|
{
|
|
|
|
Dispose();
|
2019-08-08 02:21:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MouseState Poll()
|
|
|
|
{
|
2019-08-16 04:39:18 +00:00
|
|
|
return new MouseState(RawMouse_GetX(Handle), RawMouse_GetY(Handle));
|
2019-08-08 02:21:57 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 04:39:18 +00:00
|
|
|
public bool Disposed { get { return Handle == IntPtr.Zero; } }
|
|
|
|
|
2019-08-08 02:21:57 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2019-08-16 04:39:18 +00:00
|
|
|
if (!Disposed)
|
|
|
|
{
|
|
|
|
RawMouse_Delete(Handle);
|
|
|
|
Handle = IntPtr.Zero;
|
|
|
|
}
|
2019-08-08 02:21:57 +00:00
|
|
|
}
|
2019-08-16 04:39:18 +00:00
|
|
|
|
|
|
|
internal IntPtr Handle;
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern IntPtr RawMouse_New(IntPtr windowHandle);
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern void RawMouse_Delete(IntPtr handle);
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern float RawMouse_GetX(IntPtr handle);
|
|
|
|
|
|
|
|
[DllImport("BuilderNative.dll", CallingConvention = CallingConvention.Cdecl)]
|
|
|
|
static extern float RawMouse_GetY(IntPtr handle);
|
2019-08-08 02:21:57 +00:00
|
|
|
}
|
2009-04-19 18:07:22 +00:00
|
|
|
}
|