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