2017-10-10 02:26:39 +00:00
|
|
|
//
|
|
|
|
// Keyboard.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/9/17.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-10-10 02:26:39 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "Keyboard.hpp"
|
|
|
|
|
|
|
|
using namespace Inputs;
|
|
|
|
|
2019-09-22 17:48:50 +00:00
|
|
|
Keyboard::Keyboard(const std::set<Key> &essential_modifiers) : essential_modifiers_(essential_modifiers) {
|
2018-10-25 01:59:30 +00:00
|
|
|
for(int k = 0; k < int(Key::Help); ++k) {
|
|
|
|
observed_keys_.insert(Key(k));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 17:48:50 +00:00
|
|
|
Keyboard::Keyboard(const std::set<Key> &observed_keys, const std::set<Key> &essential_modifiers) :
|
|
|
|
observed_keys_(observed_keys), essential_modifiers_(essential_modifiers), is_exclusive_(false) {}
|
2017-10-10 02:26:39 +00:00
|
|
|
|
2020-05-30 05:06:43 +00:00
|
|
|
bool Keyboard::set_key_pressed(Key key, char, bool is_pressed) {
|
2020-05-10 03:49:37 +00:00
|
|
|
const size_t key_offset = size_t(key);
|
2017-10-20 02:01:24 +00:00
|
|
|
if(key_offset >= key_states_.size()) {
|
|
|
|
key_states_.resize(key_offset+1, false);
|
2017-10-10 02:26:39 +00:00
|
|
|
}
|
|
|
|
key_states_[key_offset] = is_pressed;
|
|
|
|
|
2020-03-03 03:08:54 +00:00
|
|
|
if(delegate_) return delegate_->keyboard_did_change_key(this, key, is_pressed);
|
|
|
|
return false;
|
2017-10-10 02:26:39 +00:00
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
const std::set<Inputs::Keyboard::Key> &Keyboard::get_essential_modifiers() const {
|
2019-09-22 17:48:50 +00:00
|
|
|
return essential_modifiers_;
|
|
|
|
}
|
|
|
|
|
2017-10-10 02:26:39 +00:00
|
|
|
void Keyboard::reset_all_keys() {
|
|
|
|
std::fill(key_states_.begin(), key_states_.end(), false);
|
2017-10-15 02:07:11 +00:00
|
|
|
if(delegate_) delegate_->reset_all_keys(this);
|
2017-10-10 02:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Keyboard::set_delegate(Delegate *delegate) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
bool Keyboard::get_key_state(Key key) const {
|
2020-05-10 03:49:37 +00:00
|
|
|
const size_t key_offset = size_t(key);
|
2017-10-10 02:26:39 +00:00
|
|
|
if(key_offset >= key_states_.size()) return false;
|
|
|
|
return key_states_[key_offset];
|
|
|
|
}
|
2018-10-25 01:59:30 +00:00
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
const std::set<Keyboard::Key> &Keyboard::observed_keys() const {
|
2018-10-25 01:59:30 +00:00
|
|
|
return observed_keys_;
|
|
|
|
}
|
|
|
|
|
2020-05-21 03:34:26 +00:00
|
|
|
bool Keyboard::is_exclusive() const {
|
2018-10-25 01:59:30 +00:00
|
|
|
return is_exclusive_;
|
|
|
|
}
|