Events are now passing correctly, small bug with held keys that needs fixing

This commit is contained in:
MitchellHansen
2017-01-14 15:15:59 -08:00
parent 0e1e9af37c
commit 10e3ba43fa
8 changed files with 216 additions and 86 deletions

View File

@@ -87,9 +87,44 @@ int Camera::update(double delta_time) {
return 1;
}
void Camera::update(VrEventPublisher* p, vr::Event e) {
void Camera::recieve_event(VrEventPublisher* publisher, std::unique_ptr<vr::Event> event) {
if (event.get()->type == vr::Event::KeyHeld) {
vr::KeyHeld *held_event = static_cast<vr::KeyHeld*>(event.get());
float speed = 1.0f;
if (held_event->code == sf::Keyboard::LShift) {
speed = 0.2f;
}
else if (held_event->code == sf::Keyboard::C) {
look_at_center();
}
else if (held_event->code == sf::Keyboard::Q) {
add_relative_impulse(Camera::DIRECTION::DOWN, speed);
}
else if (held_event->code == sf::Keyboard::E) {
add_relative_impulse(Camera::DIRECTION::UP, speed);
}
else if (held_event->code == sf::Keyboard::W) {
add_relative_impulse(Camera::DIRECTION::FORWARD, speed);
}
else if (held_event->code == sf::Keyboard::S) {
add_relative_impulse(Camera::DIRECTION::REARWARD, speed);
}
else if (held_event->code == sf::Keyboard::A) {
add_relative_impulse(Camera::DIRECTION::LEFT, speed);
}
else if (held_event->code == sf::Keyboard::D) {
add_relative_impulse(Camera::DIRECTION::RIGHT, speed);
}
else if (held_event->code == sf::Keyboard::T) {
set_position(sf::Vector3f(50, 50, 50));
}
}
std::cout << "Cam event" << std::endl;
}
void Camera::look_at_center() {

View File

@@ -27,29 +27,86 @@ void Input::consume_sf_events(sf::RenderWindow *window) {
transpose_sf_events(sf_event_queue);
sf_event_queue.clear();
}
void Input::consume_vr_events() {
}
void Input::set_flags() {
void Input::handle_held_keys() {
// When keys and buttons are pressed, add them to the held list.
// When they are depressed, remove them
for (auto&& event: event_queue) {
if (event->type == vr::Event::KeyPressed) {
vr::KeyPressed *e = static_cast<vr::KeyPressed*>(event.get());
held_keys.push_back(e->code);
}
else if (event->type == vr::Event::KeyReleased) {
vr::KeyReleased *e = static_cast<vr::KeyReleased*>(event.get());
std::remove(held_keys.begin(), held_keys.end(), e->code);
}
else if (event->type == vr::Event::MouseButtonPressed) {
vr::MouseButtonPressed *e = static_cast<vr::MouseButtonPressed*>(event.get());
held_mouse_buttons.push_back(e->button);
}
else if (event->type == vr::Event::MouseButtonReleased) {
vr::MouseButtonReleased *e = static_cast<vr::MouseButtonReleased*>(event.get());
std::remove(held_mouse_buttons.begin(), held_mouse_buttons.end(), e->button);
}
}
// Generate Held events for each of the held buttons and keys
for (auto key : held_keys) {
// Not sure if this is a good idea, but I'm going to grab
// the real-time values of the mod keys and add them to the event
bool alt = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt) || sf::Keyboard::isKeyPressed(sf::Keyboard::RAlt)) {
alt = true;
}
bool control = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl) || sf::Keyboard::isKeyPressed(sf::Keyboard::RControl)) {
control = true;
}
bool shift = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) || sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) {
shift = true;
}
bool system = false;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LSystem) || sf::Keyboard::isKeyPressed(sf::Keyboard::RSystem)) {
system = true;
}
event_queue.push_back(std::make_unique<vr::KeyHeld>(vr::KeyHeld(key, alt, control, shift, system)));
}
for (auto mouse_button : held_mouse_buttons) {
// Again, I'm going to poll the real-time status of this event
// to fill in the X and Y values. I can do this either with screen
// co-ords or with viewport co-ords. I don't have access to the window
// from here so for now I'm going to do screen co-ords
sf::Vector2i mouse_pos = sf::Mouse::getPosition();
event_queue.push_back(std::make_unique<vr::MouseButtonHeld>(vr::MouseButtonHeld(mouse_button, mouse_pos.x, mouse_pos.y)));
}
//for (auto e: event_queue) {
// if (e.type == vr::Event::KeyPressed) {
// vr::KeyPressed e = static_cast<vr::KeyPressed>(e);
//
// }
// else if (e.type == sf::Event::KeyReleased) {
// //std::remove(held_keys.begin(), held_keys.end(), e.key.code);
// }
//}
}
void Input::dispatch_events() {
while (event_queue.size() != 0) {
notify(*event_queue.front().get());
notify_subscribers(std::move(event_queue.front()));
event_queue.pop_front();
}
@@ -163,3 +220,4 @@ void Input::transpose_sf_events(std::list<sf::Event> sf_event_queue) {
}
}
}

View File

@@ -3,12 +3,13 @@
#include "Event.hpp"
#include "Pub_Sub.h"
void VrEventSubscriber::subscribe(VrEventPublisher* publisher, vr::Event::EventType type) {
void VrEventSubscriber::subscribe_to_publisher(VrEventPublisher* publisher, vr::Event::EventType type) {
publisher->subscribe(this, type);
}
void VrEventSubscriber::subscribe(VrEventPublisher* publisher, std::vector<vr::Event::EventType> type) {
void VrEventSubscriber::subscribe_to_publisher(VrEventPublisher* publisher, std::vector<vr::Event::EventType> type) {
publisher->subscribe(this, type);
}
@@ -29,13 +30,13 @@ void VrEventPublisher::unsubscribe(VrEventSubscriber *s, vr::Event::EventType ty
std::remove(subscribers[type].begin(), subscribers[type].end(), s);
}
void VrEventPublisher::notify(vr::Event event) {
void VrEventPublisher::notify_subscribers(std::unique_ptr<vr::Event> event) {
// get the bucket containing subscribers to that Event_Class
std::vector<VrEventSubscriber*> *event_type_bucket = &subscribers[event.type];
std::vector<VrEventSubscriber*> *event_type_bucket = &subscribers[event.get()->type];
// Send them the event
for (auto s : *event_type_bucket) {
s->update(this, event);
s->recieve_event(this, std::move(event));
}
}

View File

@@ -148,14 +148,18 @@ int main() {
Input input_handler;
input_handler.subscribe(camera, vr::Event::EventType::KeyPressed);
input_handler.subscribe(camera, vr::Event::EventType::KeyHeld);
WindowHandler win_hand(&window);
win_hand.subscribe_to_publisher(&input_handler, vr::Event::EventType::Closed);
window.setKeyRepeatEnabled(false);
while (window.isOpen()) {
input_handler.consume_sf_events(&window);
input_handler.set_flags();
input_handler.handle_held_keys();
input_handler.dispatch_events();
// Poll for events from the user
sf::Event event;
while (window.pollEvent(event)) {
@@ -190,36 +194,6 @@ int main() {
}
}
float speed = 1.0f;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) {
speed = 0.2f;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::L)) {
camera->look_at_center();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
camera->add_relative_impulse(Camera::DIRECTION::DOWN, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
camera->add_relative_impulse(Camera::DIRECTION::UP, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
camera->add_relative_impulse(Camera::DIRECTION::FORWARD, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
camera->add_relative_impulse(Camera::DIRECTION::REARWARD, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
camera->add_relative_impulse(Camera::DIRECTION::LEFT, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
camera->add_relative_impulse(Camera::DIRECTION::RIGHT, speed);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::T)) {
camera->set_position(sf::Vector3f(50, 50, 50));
}
if (mouse_enabled) {
if (reset) {
reset = false;