| File keys.cpp copied from file KeyDispatcher.cpp (similarity 55%) (mode: 100644) (index 30aacda..70f08fb) |
| 16 |
16 |
// You should have received a copy of the GNU General Public License |
// You should have received a copy of the GNU General Public License |
| 17 |
17 |
// along with libvle. If not, see <https://www.gnu.org/licenses/>. |
// along with libvle. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
18 |
|
|
| 19 |
|
#include "KeyDispatcher.hpp" |
|
| 20 |
|
|
|
| 21 |
|
#include <cassert> |
|
| 22 |
|
#include <cstddef> |
|
|
19 |
|
#include "keys.hpp" |
| 23 |
20 |
|
|
|
21 |
|
#include <stdexcept> |
| 24 |
22 |
#include <string> |
#include <string> |
| 25 |
23 |
|
|
| 26 |
|
#include "engine/keys.h" |
|
| 27 |
|
|
|
| 28 |
24 |
using namespace vle; |
using namespace vle; |
| 29 |
25 |
|
|
| 30 |
|
KeyDispatcher::KeyDispatcher() : lastResult(0) |
|
| 31 |
|
{ } |
|
| 32 |
|
|
|
| 33 |
|
void |
|
| 34 |
|
KeyDispatcher::dispatch(wchar_t key) |
|
|
26 |
|
Key::Key(wchar_t key) : key(key) |
| 35 |
27 |
{ |
{ |
| 36 |
|
buffer.push_back(key); |
|
| 37 |
|
|
|
| 38 |
|
std::size_t counter = vle_keys_counter(); |
|
| 39 |
|
lastResult = vle_keys_exec(buffer.c_str()); |
|
| 40 |
|
|
|
| 41 |
|
std::size_t consumedCount = vle_keys_counter() - counter; |
|
| 42 |
|
if (consumedCount > 0) { |
|
| 43 |
|
assert(consumedCount <= buffer.size()); |
|
| 44 |
|
buffer.erase(0, consumedCount); |
|
| 45 |
|
} |
|
| 46 |
|
|
|
| 47 |
|
if (lastResult == KEYS_UNKNOWN) { |
|
| 48 |
|
reset(); |
|
|
28 |
|
if (key == L'\0') { |
|
29 |
|
throw std::invalid_argument("Key can't be L'\\0'"); |
| 49 |
30 |
} |
} |
| 50 |
31 |
} |
} |
| 51 |
32 |
|
|
| 52 |
|
std::wstring |
|
| 53 |
|
KeyDispatcher::getPendingInput() const |
|
|
33 |
|
Key & |
|
34 |
|
Key::ctrl() |
| 54 |
35 |
{ |
{ |
| 55 |
|
return buffer; |
|
|
36 |
|
if ((key >= L'a' && key <= L'z') || (key >= L'A' && key <= L'Z') || |
|
37 |
|
key == L'[' || key == L'\\' || key == L']' || |
|
38 |
|
key == L'^' || key == L'_') { |
|
39 |
|
key &= 0x1f; |
|
40 |
|
return *this; |
|
41 |
|
} |
|
42 |
|
|
|
43 |
|
throw std::logic_error("Can't apply control modifier to a key with value " + |
|
44 |
|
std::to_string(key)); |
| 56 |
45 |
} |
} |
| 57 |
46 |
|
|
| 58 |
|
void |
|
| 59 |
|
KeyDispatcher::reset() |
|
|
47 |
|
Key::operator std::wstring() const |
| 60 |
48 |
{ |
{ |
| 61 |
|
buffer.clear(); |
|
| 62 |
|
lastResult = 0; |
|
|
49 |
|
return std::wstring(1, key); |
| 63 |
50 |
} |
} |
| File keys.hpp copied from file KeyDispatcher.cpp (similarity 52%) (mode: 100644) (index 30aacda..6aa8759) |
| 16 |
16 |
// You should have received a copy of the GNU General Public License |
// You should have received a copy of the GNU General Public License |
| 17 |
17 |
// along with libvle. If not, see <https://www.gnu.org/licenses/>. |
// along with libvle. If not, see <https://www.gnu.org/licenses/>. |
| 18 |
18 |
|
|
| 19 |
|
#include "KeyDispatcher.hpp" |
|
| 20 |
|
|
|
| 21 |
|
#include <cassert> |
|
| 22 |
|
#include <cstddef> |
|
|
19 |
|
#ifndef LIBVLE__KEYS_HPP__ |
|
20 |
|
#define LIBVLE__KEYS_HPP__ |
| 23 |
21 |
|
|
| 24 |
22 |
#include <string> |
#include <string> |
| 25 |
23 |
|
|
| 26 |
|
#include "engine/keys.h" |
|
| 27 |
|
|
|
| 28 |
|
using namespace vle; |
|
|
24 |
|
namespace vle { |
| 29 |
25 |
|
|
| 30 |
|
KeyDispatcher::KeyDispatcher() : lastResult(0) |
|
| 31 |
|
{ } |
|
| 32 |
|
|
|
| 33 |
|
void |
|
| 34 |
|
KeyDispatcher::dispatch(wchar_t key) |
|
|
26 |
|
// Represents a single key to provide operations on it. |
|
27 |
|
class Key |
| 35 |
28 |
{ |
{ |
| 36 |
|
buffer.push_back(key); |
|
|
29 |
|
public: |
|
30 |
|
// Remembers the key value. Throws `std::invalid_argument` if `key` is |
|
31 |
|
// L'\0'. |
|
32 |
|
explicit Key(wchar_t key); |
| 37 |
33 |
|
|
| 38 |
|
std::size_t counter = vle_keys_counter(); |
|
| 39 |
|
lastResult = vle_keys_exec(buffer.c_str()); |
|
|
34 |
|
public: |
|
35 |
|
// Adds Control modifier to the key. Throws `std::logic_error` if `key` is |
|
36 |
|
// not one of []\[a-zA-Z_^]. |
|
37 |
|
Key & ctrl(); |
| 40 |
38 |
|
|
| 41 |
|
std::size_t consumedCount = vle_keys_counter() - counter; |
|
| 42 |
|
if (consumedCount > 0) { |
|
| 43 |
|
assert(consumedCount <= buffer.size()); |
|
| 44 |
|
buffer.erase(0, consumedCount); |
|
| 45 |
|
} |
|
|
39 |
|
// Returns the key as a wide string. |
|
40 |
|
operator std::wstring() const; |
| 46 |
41 |
|
|
| 47 |
|
if (lastResult == KEYS_UNKNOWN) { |
|
| 48 |
|
reset(); |
|
| 49 |
|
} |
|
| 50 |
|
} |
|
|
42 |
|
private: |
|
43 |
|
wchar_t key; // The key. |
|
44 |
|
}; |
| 51 |
45 |
|
|
| 52 |
|
std::wstring |
|
| 53 |
|
KeyDispatcher::getPendingInput() const |
|
| 54 |
|
{ |
|
| 55 |
|
return buffer; |
|
| 56 |
46 |
} |
} |
| 57 |
47 |
|
|
| 58 |
|
void |
|
| 59 |
|
KeyDispatcher::reset() |
|
| 60 |
|
{ |
|
| 61 |
|
buffer.clear(); |
|
| 62 |
|
lastResult = 0; |
|
| 63 |
|
} |
|
|
48 |
|
#endif // LIBVLE__KEYS_HPP__ |