9#include <smk/Input.hpp>
10#include <smk/InputImpl.hpp>
14 #include <emscripten.h>
15 #include <emscripten/html5.h>
22void InputImpl::Update(GLFWwindow* window) {
25 glfwGetWindowSize(window, &width, &height);
28 for(
auto& it : key_state_) {
29 it.second.second = it.second.first;
30 it.second.first = glfwGetKey(window, it.first);
34 for (
auto& it : mouse_state_) {
35 it.second.second = it.second.first;
36 it.second.first = glfwGetMouseButton(window, it.first);
42 glfwGetCursorPos(window, &mouse_x, &mouse_y);
43 mouse_ = glm::vec2(mouse_x, mouse_y);
46 cursor_press_previous_ = cursor_press_;
47 if (!touches_.empty()) {
48 cursor_ = touches_.begin()->second.position();
51 }
else if (touching_) {
53 cursor_press_ =
false;
56 cursor_press_ = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1);
60 scroll_old_ = scroll_;
61 scroll_ = glm::vec2(0.f, 0.f);
65void InputImpl::OnTouchEvent(
int eventType,
66 const EmscriptenTouchEvent* keyEvent) {
67 for (
int i = 0; i < keyEvent->numTouches; ++i) {
68 const EmscriptenTouchPoint& touch = keyEvent->touches[i];
70 if (eventType == EMSCRIPTEN_EVENT_TOUCHSTART ||
71 eventType == EMSCRIPTEN_EVENT_TOUCHMOVE) {
72 TouchDataPoint data = {
73 glm::vec2(touch.targetX, touch.targetY),
76 auto& internal_touch = touches_[touch.identifier];
77 internal_touch.finger_id = touch.identifier;
78 internal_touch.data_points.push_back(data);
82 auto it = touches_.find(touch.identifier);
83 if (it != touches_.end()) {
90void InputImpl::OnScrollEvent(glm::vec2 offset) {
95 auto p = key_state_[key];
96 return ((p.first == GLFW_PRESS) && (p.second == GLFW_RELEASE));
100 auto p = key_state_[key];
101 return ((p.first == GLFW_RELEASE) && (p.second == GLFW_PRESS));
105 auto p = key_state_[key];
106 return (p.first == GLFW_PRESS);
110 auto p = mouse_state_[key];
111 return ((p.first == GLFW_PRESS) && (p.second == GLFW_RELEASE));
115 auto p = mouse_state_[key];
116 return ((p.first == GLFW_RELEASE) && (p.second == GLFW_PRESS));
128 auto p = mouse_state_[key];
129 return (p.first == GLFW_PRESS);
133 return cursor_press_ && cursor_press_previous_;
137 return cursor_press_ && !cursor_press_previous_;
141 return !cursor_press_ && cursor_press_previous_;
151class InputImpl::CharacterListenerImpl
154 explicit CharacterListenerImpl(
InputImpl* input) : input_(input) {
155 input_->BindCharacterListener(
this);
157 CharacterListenerImpl(CharacterListenerImpl&) =
delete;
158 CharacterListenerImpl(CharacterListenerImpl&&) =
delete;
159 CharacterListenerImpl& operator=(CharacterListenerImpl&) =
delete;
160 CharacterListenerImpl& operator=(CharacterListenerImpl&&) =
delete;
162 ~CharacterListenerImpl()
override { input_->UnBindCharacterListener(
this); }
164 bool Receive(
wchar_t* c)
override {
165 if (queue_.empty()) {
175 std::queue<wchar_t> queue_;
178void InputImpl::BindCharacterListener(
179 InputImpl::CharacterListenerImpl* listener) {
180 character_listeners_.insert(listener);
183void InputImpl::UnBindCharacterListener(
184 InputImpl::CharacterListenerImpl* listener) {
185 character_listeners_.erase(listener);
188void InputImpl::OnCharacterTyped(
wchar_t character) {
189 for (
const auto& it : character_listeners_) {
190 it->queue_.push(character);
194Input::CharacterListener InputImpl::MakeCharacterListener() {
195 return std::make_unique<CharacterListenerImpl>(
this);