153 lines
No EOL
3.8 KiB
C++
153 lines
No EOL
3.8 KiB
C++
//
|
|
// Created by trotfunky on 07/10/2019.
|
|
//
|
|
|
|
#include "KeyStateManager.h"
|
|
#include <iostream>
|
|
|
|
// Initialize static members
|
|
|
|
std::map<unsigned char,bool> KeyStateManager::ascii_keys_status = {};
|
|
std::map<int,bool> KeyStateManager::special_keys_status = {};
|
|
std::map<int,bool> KeyStateManager::mouse_button_status = {};
|
|
|
|
Vec2i KeyStateManager::current_mouse_delta;
|
|
Vec2i KeyStateManager::last_mouse_delta;
|
|
Vec2i KeyStateManager::last_mouse_position;
|
|
|
|
void KeyStateManager::register_glut_callbacks()
|
|
{
|
|
glutKeyboardFunc(KeyStateManager::key_press);
|
|
glutKeyboardUpFunc(KeyStateManager::key_up);
|
|
glutSpecialFunc(KeyStateManager::special_key_press);
|
|
glutSpecialUpFunc(KeyStateManager::special_key_up);
|
|
|
|
glutMouseFunc(KeyStateManager::mouse_click);
|
|
glutPassiveMotionFunc(KeyStateManager::mouse_movement);
|
|
glutMotionFunc(KeyStateManager::mouse_movement);
|
|
}
|
|
|
|
// ==================
|
|
// Keyboard
|
|
// ==================
|
|
|
|
bool KeyStateManager::is_key_pressed(unsigned char key)
|
|
{
|
|
if (ascii_keys_status.find(key) == ascii_keys_status.end())
|
|
{
|
|
return false;
|
|
}
|
|
return ascii_keys_status.at(key);
|
|
}
|
|
|
|
bool KeyStateManager::is_special_key_pressed(int key)
|
|
{
|
|
if (special_keys_status.find(key) == special_keys_status.end())
|
|
{
|
|
return false;
|
|
}
|
|
return special_keys_status.at(key);
|
|
}
|
|
|
|
void KeyStateManager::key_press(unsigned char event_key, int mouse_x, int mouse_y)
|
|
{
|
|
update_key(event_key,true);
|
|
mouse_movement(mouse_x,mouse_y);
|
|
}
|
|
|
|
void KeyStateManager::key_up(unsigned char event_key, int mouse_x, int mouse_y)
|
|
{
|
|
update_key(event_key,false);
|
|
mouse_movement(mouse_x,mouse_y);
|
|
}
|
|
|
|
void KeyStateManager::special_key_press(int event_key, int mouse_x, int mouse_y)
|
|
{
|
|
update_special_key(event_key,true);
|
|
mouse_movement(mouse_x,mouse_y);
|
|
}
|
|
|
|
void KeyStateManager::special_key_up(int event_key, int mouse_x, int mouse_y)
|
|
{
|
|
update_special_key(event_key,false);
|
|
mouse_movement(mouse_x,mouse_y);
|
|
}
|
|
|
|
void KeyStateManager::update_key(unsigned char event_key, bool new_status)
|
|
{
|
|
if (ascii_keys_status.find(event_key) != ascii_keys_status.end())
|
|
{
|
|
ascii_keys_status.at(event_key) = new_status;
|
|
}
|
|
else
|
|
{
|
|
ascii_keys_status.insert({event_key,new_status});
|
|
}
|
|
}
|
|
|
|
void KeyStateManager::update_special_key(int event_key, bool new_status)
|
|
{
|
|
if (special_keys_status.find(event_key) != special_keys_status.end())
|
|
{
|
|
special_keys_status.at(event_key) = new_status;
|
|
}
|
|
else
|
|
{
|
|
special_keys_status.insert({event_key,new_status});
|
|
}
|
|
}
|
|
|
|
// ==================
|
|
// Mouse
|
|
// ==================
|
|
|
|
void KeyStateManager::mouse_movement(int mouse_x, int mouse_y)
|
|
{
|
|
Vec2i current_frame;
|
|
current_frame.x = mouse_x;
|
|
current_frame.y = mouse_y;
|
|
|
|
Vec2i frame_delta = current_frame - last_mouse_position;
|
|
current_mouse_delta.x += frame_delta.x;
|
|
current_mouse_delta.y += frame_delta.y;
|
|
|
|
last_mouse_position = current_frame;
|
|
|
|
std::cout << frame_delta.x << "," << frame_delta.y << std::endl;
|
|
}
|
|
|
|
void KeyStateManager::mouse_click(int mouse_button, int button_state, int mouse_x, int mouse_y)
|
|
{
|
|
bool new_status = button_state == GLUT_DOWN;
|
|
|
|
if (mouse_button_status.find(mouse_button) != mouse_button_status.end())
|
|
{
|
|
mouse_button_status.at(mouse_button) = new_status;
|
|
}
|
|
else
|
|
{
|
|
mouse_button_status.insert({mouse_button,new_status});
|
|
}
|
|
mouse_movement(mouse_x,mouse_y);
|
|
}
|
|
|
|
bool KeyStateManager::is_mouse_button_pressed(int mouse_button)
|
|
{
|
|
|
|
if (mouse_button_status.find(mouse_button) == mouse_button_status.end())
|
|
{
|
|
return false;
|
|
}
|
|
return mouse_button_status.at(mouse_button);
|
|
}
|
|
|
|
const Vec2i& KeyStateManager::get_mouse_delta(bool update)
|
|
{
|
|
if (update)
|
|
{
|
|
last_mouse_delta = current_mouse_delta;
|
|
current_mouse_delta.x = 0;
|
|
current_mouse_delta.y = 0;
|
|
}
|
|
return last_mouse_delta;
|
|
} |