smk::InputImpl Class Reference
Inheritance diagram for smk::InputImpl:

Description

Definition at line 17 of file InputImpl.hpp.

Public Types

using FingerID = int
 
using CharacterListener = std::unique_ptr< CharacterListenerInterface >
 

Public Member Functions

void Update (GLFWwindow *window)
 
void OnScrollEvent (glm::vec2 offset)
 
void OnCharacterTyped (wchar_t character)
 
void BindCharacterListener (CharacterListenerImpl *)
 
void UnBindCharacterListener (CharacterListenerImpl *)
 
bool IsKeyPressed (int key) override
 Whether a keyboard button is pressed or not. More...
 
bool IsKeyReleased (int key) override
 Whether a keyboard button is released or not. More...
 
bool IsKeyHold (int key) override
 Whether a keyboard button is down or not. More...
 
bool IsMousePressed (int key) override
 Whether a mouse button is pressed or not. More...
 
bool IsMouseReleased (int key) override
 Whether a mouse button is released or not. More...
 
glm::vec2 mouse () const override
 The mouse position. More...
 
std::map< FingerID, Touch > & touches () override
 The touch states. More...
 
bool IsMouseHeld (int key) override
 Whether a mouse button is down or not. More...
 
bool IsCursorHeld () override
 Whether the cursor is down or not. More...
 
bool IsCursorPressed () override
 Whether the cursor is pressed or not. More...
 
bool IsCursorReleased () override
 Whether the cursor is released or not. More...
 
glm::vec2 cursor () const override
 The cursor position. More...
 
glm::vec2 scroll_offset () const override
 The mouse/touchpad scrolling offset since the last frame. More...
 
CharacterListener MakeCharacterListener () override
 

Member Typedef Documentation

◆ CharacterListener

using smk::Input::CharacterListener = std::unique_ptr<CharacterListenerInterface>
inherited

Definition at line 118 of file Input.hpp.

◆ FingerID

using smk::Input::FingerID = int
inherited

Definition at line 66 of file Input.hpp.

Member Function Documentation

◆ BindCharacterListener()

void smk::InputImpl::BindCharacterListener ( InputImpl::CharacterListenerImpl *  listener)

Definition at line 178 of file InputImpl.cpp.

179 {
180 character_listeners_.insert(listener);
181}

◆ cursor()

glm::vec2 smk::InputImpl::cursor ( ) const
overridevirtual

The cursor position.

Returns
the cursor position.

Implements smk::Input.

Definition at line 144 of file InputImpl.cpp.

144 {
145 return cursor_;
146}

◆ IsCursorHeld()

bool smk::InputImpl::IsCursorHeld ( )
overridevirtual

Whether the cursor is down or not.

Returns
true whenever the cursor is down.

Implements smk::Input.

Definition at line 132 of file InputImpl.cpp.

132 {
133 return cursor_press_ && cursor_press_previous_;
134}

◆ IsCursorPressed()

bool smk::InputImpl::IsCursorPressed ( )
overridevirtual

Whether the cursor is pressed or not.

Returns
true whenever the cursor is pressed.

Implements smk::Input.

Definition at line 136 of file InputImpl.cpp.

136 {
137 return cursor_press_ && !cursor_press_previous_;
138}

◆ IsCursorReleased()

bool smk::InputImpl::IsCursorReleased ( )
overridevirtual

Whether the cursor is released or not.

Returns
true whenever the cursor is released.

Implements smk::Input.

Definition at line 140 of file InputImpl.cpp.

140 {
141 return !cursor_press_ && cursor_press_previous_;
142}

◆ IsKeyHold()

bool smk::InputImpl::IsKeyHold ( int  key)
overridevirtual

Whether a keyboard button is down or not.

Returns
true whenever a keyboard button is hold.
Parameters
keyThe keyboard button.

Implements smk::Input.

Definition at line 104 of file InputImpl.cpp.

104 {
105 auto p = key_state_[key];
106 return (p.first == GLFW_PRESS);
107}

◆ IsKeyPressed()

bool smk::InputImpl::IsKeyPressed ( int  key)
overridevirtual

Whether a keyboard button is pressed or not.

Returns
true whenever a keyboard button is pressed.
Parameters
keyThe keyboard button.

Implements smk::Input.

Definition at line 94 of file InputImpl.cpp.

94 {
95 auto p = key_state_[key];
96 return ((p.first == GLFW_PRESS) && (p.second == GLFW_RELEASE));
97}

◆ IsKeyReleased()

bool smk::InputImpl::IsKeyReleased ( int  key)
overridevirtual

Whether a keyboard button is released or not.

Returns
true whenever a keyboard button is released
Parameters
keyThe keyboard button.

Implements smk::Input.

Definition at line 99 of file InputImpl.cpp.

99 {
100 auto p = key_state_[key];
101 return ((p.first == GLFW_RELEASE) && (p.second == GLFW_PRESS));
102}

◆ IsMouseHeld()

bool smk::InputImpl::IsMouseHeld ( int  key)
overridevirtual

Whether a mouse button is down or not.

Returns
true whenever a mouse button is hold.
Parameters
keyThe mouse button.

Implements smk::Input.

Definition at line 127 of file InputImpl.cpp.

127 {
128 auto p = mouse_state_[key];
129 return (p.first == GLFW_PRESS);
130}

◆ IsMousePressed()

bool smk::InputImpl::IsMousePressed ( int  key)
overridevirtual

Whether a mouse button is pressed or not.

Returns
true whenever a mouse button is pressed.
Parameters
keyThe mouse button.

Implements smk::Input.

Definition at line 109 of file InputImpl.cpp.

109 {
110 auto p = mouse_state_[key];
111 return ((p.first == GLFW_PRESS) && (p.second == GLFW_RELEASE));
112}

◆ IsMouseReleased()

bool smk::InputImpl::IsMouseReleased ( int  key)
overridevirtual

Whether a mouse button is released or not.

Returns
true whenever a mouse button is released
Parameters
keyThe mouse button.

Implements smk::Input.

Definition at line 114 of file InputImpl.cpp.

114 {
115 auto p = mouse_state_[key];
116 return ((p.first == GLFW_RELEASE) && (p.second == GLFW_PRESS));
117}

◆ MakeCharacterListener()

Input::CharacterListener smk::InputImpl::MakeCharacterListener ( )
overridevirtual

Implements smk::Input.

Definition at line 194 of file InputImpl.cpp.

194 {
195 return std::make_unique<CharacterListenerImpl>(this);
196}

◆ mouse()

glm::vec2 smk::InputImpl::mouse ( ) const
overridevirtual

The mouse position.

Returns
the mouse position.

Implements smk::Input.

Definition at line 119 of file InputImpl.cpp.

119 {
120 return mouse_;
121}

◆ OnCharacterTyped()

void smk::InputImpl::OnCharacterTyped ( wchar_t  character)

Definition at line 188 of file InputImpl.cpp.

188 {
189 for (const auto& it : character_listeners_) {
190 it->queue_.push(character);
191 }
192}

◆ OnScrollEvent()

void smk::InputImpl::OnScrollEvent ( glm::vec2  offset)

Definition at line 90 of file InputImpl.cpp.

90 {
91 scroll_ += offset;
92}

◆ scroll_offset()

glm::vec2 smk::InputImpl::scroll_offset ( ) const
overridevirtual

The mouse/touchpad scrolling offset since the last frame.

Returns
the scrolling offset.

Implements smk::Input.

Definition at line 147 of file InputImpl.cpp.

147 {
148 return scroll_old_;
149}

◆ touches()

std::map< Input::FingerID, Touch > & smk::InputImpl::touches ( )
overridevirtual

The touch states.

Returns
the touches states.

Implements smk::Input.

Definition at line 123 of file InputImpl.cpp.

123 {
124 return touches_;
125}

◆ UnBindCharacterListener()

void smk::InputImpl::UnBindCharacterListener ( InputImpl::CharacterListenerImpl *  listener)

Definition at line 183 of file InputImpl.cpp.

184 {
185 character_listeners_.erase(listener);
186}

◆ Update()

void smk::InputImpl::Update ( GLFWwindow *  window)

Definition at line 22 of file InputImpl.cpp.

22 {
23 int width = 0;
24 int height = 0;
25 glfwGetWindowSize(window, &width, &height);
26
27 // Update key
28 for(auto& it : key_state_) {
29 it.second.second = it.second.first;
30 it.second.first = glfwGetKey(window, it.first);
31 }
32
33 // Update mouse
34 for (auto& it : mouse_state_) {
35 it.second.second = it.second.first;
36 it.second.first = glfwGetMouseButton(window, it.first);
37 }
38
39 // get mouse position
40 double mouse_x = 0.F;
41 double mouse_y = 0.F;
42 glfwGetCursorPos(window, &mouse_x, &mouse_y);
43 mouse_ = glm::vec2(mouse_x, mouse_y);
44
45 // Update cursor.
46 cursor_press_previous_ = cursor_press_;
47 if (!touches_.empty()) {
48 cursor_ = touches_.begin()->second.position();
49 cursor_press_ = true;
50 touching_ = true;
51 } else if (touching_) {
52 touching_ = false;
53 cursor_press_ = false;
54 } else {
55 cursor_ = mouse_;
56 cursor_press_ = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1);
57 }
58
59 // Update scroll.
60 scroll_old_ = scroll_;
61 scroll_ = glm::vec2(0.f, 0.f);
62}

The documentation for this class was generated from the following files: