#pragma region CPL License
/*
Nuclex Native Framework
Copyright (C) 2002-2013 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library 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
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion // CPL License
#ifndef NUCLEX_INPUT_DEVICES_BUFFEREDMOUSE_H
#define NUCLEX_INPUT_DEVICES_BUFFEREDMOUSE_H
#include "Nuclex/Input/Devices/Mouse.h"
#include "../../ConcurrentQueue.h"
namespace Nuclex { namespace Input { namespace Devices {
// ------------------------------------------------------------------------------------------- //
/// Mouse that buffers changes until an Update call happens
class BufferedMouse : public Mouse {
/// A snapshot has been recorded
private: static const int SnapshotNotification = 0;
/// Mouse button has been pressed
private: static const int ButtonPressNotification = 1;
/// Mouse button has been pressed
private: static const int ButtonReleaseNotification = 2;
/// Mouse cursor has been moved
private: static const int CursorMoveNotification = 3;
/// Mouse has been moved
private: static const int MoveNotification = 4;
/// Mouse wheel has been rotated
private: static const int WheelRotationNotification = 5;
#pragma region struct Event
/// Stores a buffered change notification
private: struct Event {
/// Type of event that has been recorded
public: int Type;
union {
/// Button that has been pressed or released
public: MouseButtons::Enum PressedOrReleasedButtons;
/// Absolute position of the mouse cursor
struct {
/// X coordinate of the mouse cursor
public: int X;
/// Y coordinate of the mouse cursor
public: int Y;
} Position;
/// Relative movement of the mouse
struct {
/// Amount the mouse was moved on the X axis
public: float DeltaX;
/// Amount the mouse was moved on the Y axis
public: float DeltaY;
} Movement;
/// Amount the mouse wheel has been rotated
public: float WheelDelta;
} State;
};
#pragma endregion // struct Event
/// Initializes a new buffered mouse
protected: BufferedMouse() {}
/// Frees all resources owned by the buffered mouse
public: virtual ~BufferedMouse() {}
/// Retrieves the current state of the input device
/// The input device's current state
public: virtual const MouseState &GetState() const {
return this->currentState;
}
/// Buffers a button press notification
/// Button that has been pressed
protected: void BufferButtonPress(MouseButtons::Enum pressedButton);
/// Buffers a button release notification
/// Button that has been released
protected: void BufferButtonRelease(MouseButtons::Enum releasedButton);
/// Buffers a cursor movement notification
/// New X coordinate of the mouse cursor
/// New Y coordinate of the mouse cursor
protected: void BufferCursorMovement(int x, int y);
/// Buffers a movement notification
/// Amount the mouse was moved on the X axis
/// Amount the mouse was moved on the Y axis
protected: void BufferMovement(float deltaX, float deltaY);
/// Buffers a wheel rotation notification
/// Amount the mouse wheel was rotated
protected: void BufferWheelRotation(float delta);
/// Updates the state of the input device
protected: void Update();
/// Takes a snapshot of the input device's current state
protected: void TakeSnapshot();
/// Removes all snapshots the device has taken
protected: void ClearSnapshots();
/// Plays back an event, updating the mouse's state
/// Event that will be played back
private: void playbackEvent(const Event &recordedEvent);
/// Plays back a button press event, updating the mouse's state
/// Event that will be played back
private: void playbackButtonPressEvent(const Event &buttonPressEvent);
/// Plays back a button release event, updating the mouse's state
/// Event that will be played back
private: void playbackButtonReleaseEvent(const Event &buttonPressEvent);
private: BufferedMouse(const BufferedMouse &);
private: BufferedMouse &operator =(const BufferedMouse &);
/// Events that have been buffered
private: ConcurrentQueue bufferedEvents;
/// Current state of the mouse
private: MouseState currentState;
};
// ------------------------------------------------------------------------------------------- //
}}} // namespace Nuclex::Input::Devices
#endif // NUCLEX_INPUT_DEVICES_BUFFEREDMOUSE_H