#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_VIRTUALTOUCHPANEL_H
#define NUCLEX_INPUT_DEVICES_VIRTUALTOUCHPANEL_H
#include "Nuclex/Input/Devices/TouchPanel.h"
namespace Nuclex { namespace Input { namespace Devices {
/// Forwards touch panel 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 VirtualTouchPanel : public TouchPanel, public sigslot::has_slots<> {
/// Initializes a new virtual touch panel
public: VirtualTouchPanel() :
forwardingTouchPanel(nullptr) {
this->neutralState.Reset();
}
/// Destroys the virtual touch panel
public: virtual ~VirtualTouchPanel() {
if(this->forwardingTouchPanel != nullptr) {
unsubscribe(this->forwardingTouchPanel);
}
}
/// Assigns the touch panel for which this instance forwards input
/// TouchPanel for which input will be forwarded
public: void SetForwardingTouchPanel(TouchPanel *forwardingTouchPanel) {
if(this->forwardingTouchPanel != nullptr) {
unsubscribe(this->forwardingTouchPanel);
}
this->forwardingTouchPanel = forwardingTouchPanel;
if(this->forwardingTouchPanel != nullptr) {
subscribe(this->forwardingTouchPanel);
}
}
/// Whether the input device is currently attached
/// True if the input device is attached, otherwise false
public: virtual bool IsAttached() const {
if(this->forwardingTouchPanel == nullptr) {
return false;
} else {
return this->forwardingTouchPanel->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 touch panel connected");
if(this->forwardingTouchPanel == nullptr) {
return notConnected;
} else {
return this->forwardingTouchPanel->GetName();
}
}
/// Retrieves the current state of the input device
/// The input device's current state
public: virtual const TouchPanelState &GetState() const {
if(this->forwardingTouchPanel == nullptr) {
return this->neutralState;
} else {
return this->forwardingTouchPanel->GetState();
}
}
/// Updates the state of the input device
protected: virtual void Update() {
if(this->forwardingTouchPanel != nullptr) {
this->forwardingTouchPanel->Update();
}
}
/// Takes a snapshot of the input device's current state
protected: virtual void TakeSnapshot() {
if(this->forwardingTouchPanel != nullptr) {
this->forwardingTouchPanel->TakeSnapshot();
}
}
/// Removes all snapshots the device has taken
protected: virtual void ClearSnapshots() {
if(this->forwardingTouchPanel != nullptr) {
this->forwardingTouchPanel->ClearSnapshots();
}
}
/// Subscribes to the events of the forwarding touch panel
/// Touch panel to whose events to subscribe
private: void subscribe(TouchPanel *touchPanel) {
touchPanel->Pressed.connect(this, &VirtualTouchPanel::onPressed);
touchPanel->Dragged.connect(this, &VirtualTouchPanel::onDragged);
touchPanel->Released.connect(this, &VirtualTouchPanel::onReleased);
}
/// Unsubscribes from the events of the forwarding touch panel
private: void unsubscribe(TouchPanel *touchPanel) {
touchPanel->Released.disconnect(this);
touchPanel->Dragged.disconnect(this);
touchPanel->Pressed.disconnect(this);
}
/// Forwards a touch panel press notification to the library user
/// Id the touch can be identified by
/// X coordinate of the touch
/// Y coordinate of the touch
private: void onPressed(TouchId id, int x, int y) {
Pressed(id, x, y);
}
/// Forwards a touch panel drag notification to the library user
/// Id the touch was given when it was first detected
/// New X coordinate of the touch
/// New Y coordinate of the touch
private: void onDragged(TouchId id, int x, int y) {
Dragged(id, x, y);
}
/// Forwards a touch panel release notification to the library user
/// Id of the touch that has stopped
private: void onReleased(TouchId id) {
Released(id);
}
/// Disabled copy constructor for the virtual touch panel
private: VirtualTouchPanel(const VirtualTouchPanel &);
/// Disabled assignment operator for the virtual touch panel
private: void operator =(const VirtualTouchPanel &);
/// TouchPanel that is forwarded by this instance
private: TouchPanel *forwardingTouchPanel;
/// Neutral state that is returned when no touch panel is connected
private: TouchPanelState neutralState;
};
}}} // namespace Nuclex::Input::Devices
#endif // NUCLEX_INPUT_DEVICES_VIRTUALTOUCHPANEL_H