#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_VIRTUALKEYBOARD_H #define NUCLEX_INPUT_DEVICES_VIRTUALKEYBOARD_H #include "Nuclex/Input/Devices/Keyboard.h" namespace Nuclex { namespace Input { namespace Devices { /// Forwards keyboard 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 VirtualKeyboard : public Keyboard, public sigslot::has_slots<> { /// Initializes a new virtual keyboard public: VirtualKeyboard() : forwardingKeyboard(nullptr) { this->neutralState.Reset(); } /// Destroys the virtual keyboard public: virtual ~VirtualKeyboard() { if(this->forwardingKeyboard != nullptr) { unsubscribe(this->forwardingKeyboard); } } /// Assigns the keyboard for which this instance forwards input /// Keyboard for which input will be forwarded public: void SetForwardingKeyboard(Keyboard *forwardingKeyboard) { if(this->forwardingKeyboard != nullptr) { unsubscribe(this->forwardingKeyboard); } this->forwardingKeyboard = forwardingKeyboard; if(this->forwardingKeyboard != nullptr) { subscribe(this->forwardingKeyboard); } } /// Whether the input device is currently attached /// True if the input device is attached, otherwise false public: virtual bool IsAttached() const { if(this->forwardingKeyboard == nullptr) { return false; } else { return this->forwardingKeyboard->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 keyboard connected"); if(this->forwardingKeyboard == nullptr) { return notConnected; } else { return this->forwardingKeyboard->GetName(); } } /// Retrieves the current state of the input device /// The input device's current state public: virtual const KeyboardState &GetState() const { if(this->forwardingKeyboard == nullptr) { return this->neutralState; } else { return this->forwardingKeyboard->GetState(); } } /// Updates the state of the input device protected: virtual void Update() { if(this->forwardingKeyboard != nullptr) { this->forwardingKeyboard->Update(); } } /// Takes a snapshot of the input device's current state protected: virtual void TakeSnapshot() { if(this->forwardingKeyboard != nullptr) { this->forwardingKeyboard->TakeSnapshot(); } } /// Removes all snapshots the device has taken protected: virtual void ClearSnapshots() { if(this->forwardingKeyboard != nullptr) { this->forwardingKeyboard->ClearSnapshots(); } } /// Subscribes to the events of the forwarding keyboard /// Keyboard to whose events to subscribe private: void subscribe(Keyboard *keyboard) { keyboard->KeyPressed.connect(this, &VirtualKeyboard::onKeyPressed); keyboard->KeyReleased.connect(this, &VirtualKeyboard::onKeyReleased); keyboard->CharacterEntered.connect(this, &VirtualKeyboard::onCharacterEntered); } /// Unsubscribes from the events of the forwarding keyboard /// Keyboard from whose events to unsubscribe private: void unsubscribe(Keyboard *keyboard) { keyboard->CharacterEntered.disconnect(this); keyboard->KeyReleased.disconnect(this); keyboard->KeyPressed.disconnect(this); } /// Forwards a key press notification to the library user /// Key that has been pressed private: void onKeyPressed(Key::Enum key) { KeyPressed(key); } /// Forwards a key release notification to the library user /// Key that has been released private: void onKeyReleased(Key::Enum key) { KeyReleased(key); } /// Forwards a character entry notification to the library user /// Character that has been entered private: void onCharacterEntered(wchar_t character) { CharacterEntered(character); } /// Disabled copy constructor for the virtual keyboard private: VirtualKeyboard(const VirtualKeyboard &); /// Disabled assignment operator for the virtual keyboard private: void operator =(const VirtualKeyboard &); /// Keyboard that is forwarded by this instance private: Keyboard *forwardingKeyboard; /// Neutral state that is returned when no keyboard is connected private: KeyboardState neutralState; }; }}} // namespace Nuclex::Input::Devices #endif // NUCLEX_INPUT_DEVICES_VIRTUALKEYBOARD_H