#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_VIRTUALGAMEPAD_H
#define NUCLEX_INPUT_DEVICES_VIRTUALGAMEPAD_H
#include "Nuclex/Input/Devices/GamePad.h"
namespace Nuclex { namespace Input { namespace Devices {
/// Forwards game pad input from an exchangeable backing device
///
///
/// If this library directly dealt out instances of the input device implementations,
/// disconnecting a device and connecting another one, for example, would mean that
/// any pointers the user has to the disconnected device become either invalid.
///
///
/// So the design choice was to either let the library's user in on when devices are
/// replaced, forcing him to unsubscribe from events, resubscribe, replace pointers
/// he has stored to the old device and so on, or to use a forwarder device that
/// allows the underlying real input device to be replaced at will without anyone
/// outside of the library noticing.
///
///
/// Without a forwarding device assigned, this class can also be used as a dummy
/// device to fill slots for devices not available or not supported by the current
/// system (think keyboard on iPhone, etc.)
///
///
class VirtualGamePad : public GamePad, public sigslot::has_slots<> {
/// Initializes a new virtual game pad
public: VirtualGamePad() :
forwardingGamePad(nullptr) {
this->neutralState.Reset();
}
/// Destroys the virtual game pad
public: virtual ~VirtualGamePad() {
if(this->forwardingGamePad != nullptr) {
unsubscribe(this->forwardingGamePad);
}
}
/// Retrieves the game pad this instance currently forwards input for
/// The game pad this instance forwards input for
public: GamePad *GetForwardingGamePad() const {
return this->forwardingGamePad;
}
/// Assigns the game pad for which this instance forwards input
/// Game pad for which input will be forwarded
public: void SetForwardingGamePad(GamePad *forwardingGamePad) {
if(this->forwardingGamePad != nullptr) {
unsubscribe(this->forwardingGamePad);
}
this->forwardingGamePad = forwardingGamePad;
if(this->forwardingGamePad != nullptr) {
subscribe(this->forwardingGamePad);
}
}
/// Whether the input device is currently attached
/// True if the input device is attached, otherwise false
public: virtual bool IsAttached() const {
if(this->forwardingGamePad == nullptr) {
return false;
} else {
return this->forwardingGamePad->IsAttached();
}
}
/// Retrieves the human-readable name of the input device
/// The human-readable name of the input device
public: virtual const std::string &GetName() const {
static std::string notConnected("No game pad connected");
if(this->forwardingGamePad == nullptr) {
return notConnected;
} else {
return this->forwardingGamePad->GetName();
}
}
/// Retrieves the current state of the input device
/// The input device's current state
public: virtual const GamePadState &GetState() const {
if(this->forwardingGamePad == nullptr) {
return this->neutralState;
} else {
return this->forwardingGamePad->GetState();
}
}
/// Updates the state of the input device
protected: virtual void Update() {
if(this->forwardingGamePad != nullptr) {
this->forwardingGamePad->Update();
}
}
/// Takes a snapshot of the input device's current state
protected: virtual void TakeSnapshot() {
if(this->forwardingGamePad != nullptr) {
this->forwardingGamePad->TakeSnapshot();
}
}
/// Removes all snapshots the device has taken
protected: virtual void ClearSnapshots() {
if(this->forwardingGamePad != nullptr) {
this->forwardingGamePad->ClearSnapshots();
}
}
/// Subscribes to the events of the forwarding game pad
/// Game pad to whose events to subscribe
private: void subscribe(GamePad *gamePad) {
gamePad->ButtonPressed.connect(this, &VirtualGamePad::onButtonPressed);
gamePad->ButtonReleased.connect(this, &VirtualGamePad::onButtonReleased);
}
/// Unsubscribes from the events of the forwarding game pad
/// Game pad from whose events to unsubscribe
private: void unsubscribe(GamePad *gamePad) {
gamePad->ButtonReleased.disconnect(this);
gamePad->ButtonPressed.disconnect(this);
}
/// Forwards a button press notification to the library user
/// Index of the pressed button
private: void onButtonPressed(std::size_t buttonIndex) {
ButtonPressed(buttonIndex);
}
/// Forwards a button release notification to the library user
/// Index of the released button
private: void onButtonReleased(std::size_t buttonIndex) {
ButtonReleased(buttonIndex);
}
/// Disabled copy constructor for the virtual game pad
private: VirtualGamePad(const VirtualGamePad &);
/// Disabled assignment operator for the virtual game pad
private: void operator =(const VirtualGamePad &);
/// Game pad that is forwarded by this instance
private: GamePad *forwardingGamePad;
/// Neutral state that is returned when no game pad is connected
private: GamePadState neutralState;
};
}}} // namespace Nuclex::Input::Devices
#endif // NUCLEX_INPUT_DEVICES_VIRTUALGAMEPAD_H