Events are now passing correctly, small bug with held keys that needs fixing
This commit is contained in:
@@ -35,7 +35,7 @@ public:
|
||||
sf::Vector2f get_direction();
|
||||
|
||||
|
||||
void update(VrEventPublisher* p, vr::Event e) override;
|
||||
void recieve_event(VrEventPublisher* p, std::unique_ptr<vr::Event> event) override;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
@@ -16,8 +16,9 @@ namespace vr {
|
||||
|
||||
// A result of getting rid of the union and extracting
|
||||
// event types into individual classes is the fact that
|
||||
// there is going to be a lot of repeat code i.e the
|
||||
// difference between KeyPressed and KeyReleased
|
||||
// there is going to be a lot of repeat code i.e
|
||||
// KeyPressed, KeyHeld, and KeyReleased all hold the same
|
||||
// data, it's just their names that are different
|
||||
|
||||
class Event {
|
||||
public:
|
||||
@@ -30,15 +31,18 @@ namespace vr {
|
||||
GainedFocus,
|
||||
TextEntered,
|
||||
KeyPressed,
|
||||
KeyHeld,
|
||||
KeyReleased,
|
||||
MouseWheelMoved,
|
||||
MouseWheelScrolled,
|
||||
MouseButtonPressed,
|
||||
MouseButtonHeld,
|
||||
MouseButtonReleased,
|
||||
MouseMoved,
|
||||
MouseEntered,
|
||||
MouseLeft,
|
||||
JoystickButtonPressed,
|
||||
JoystickButtonHeld,
|
||||
JoystickButtonReleased,
|
||||
JoystickMoved,
|
||||
JoystickConnected,
|
||||
@@ -48,6 +52,7 @@ namespace vr {
|
||||
TouchEnded,
|
||||
SensorChanged,
|
||||
NetworkJoystickButtonPressed,
|
||||
NetworkJoystickButtonHeld,
|
||||
NetworkJoystickButtonReleased,
|
||||
NetworkJoystickMoved,
|
||||
NetworkJoystickConnected,
|
||||
@@ -105,6 +110,18 @@ namespace vr {
|
||||
bool system;
|
||||
};
|
||||
|
||||
class KeyHeld : public Event {
|
||||
public:
|
||||
KeyHeld(sf::Keyboard::Key code, bool alt, bool control, bool shift, bool system) :
|
||||
code(code), alt(alt), control(control), shift(shift), system(system), Event(vr::Event::EventType::KeyHeld) {};
|
||||
|
||||
sf::Keyboard::Key code;
|
||||
bool alt;
|
||||
bool control;
|
||||
bool shift;
|
||||
bool system;
|
||||
};
|
||||
|
||||
class KeyReleased : public Event {
|
||||
public:
|
||||
KeyReleased(sf::Keyboard::Key code, bool alt, bool control, bool shift, bool system) :
|
||||
@@ -144,6 +161,16 @@ namespace vr {
|
||||
int y;
|
||||
};
|
||||
|
||||
class MouseButtonHeld : public Event {
|
||||
public:
|
||||
MouseButtonHeld(sf::Mouse::Button button, int x, int y) :
|
||||
button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonHeld) {};
|
||||
|
||||
sf::Mouse::Button button;
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
class MouseButtonReleased : public Event {
|
||||
public:
|
||||
MouseButtonReleased(sf::Mouse::Button button, int x, int y) :
|
||||
@@ -190,6 +217,15 @@ namespace vr {
|
||||
unsigned int button;
|
||||
};
|
||||
|
||||
class JoystickButtonHeld : public Event {
|
||||
public:
|
||||
JoystickButtonHeld(unsigned int joystickId, unsigned int button) :
|
||||
joystickId(joystickId), button(button), Event(vr::Event::EventType::JoystickButtonHeld) {};
|
||||
|
||||
unsigned int joystickId;
|
||||
unsigned int button;
|
||||
};
|
||||
|
||||
class JoystickButtonReleased : public Event {
|
||||
public:
|
||||
JoystickButtonReleased(unsigned int joystickId, unsigned int button) :
|
||||
@@ -280,6 +316,15 @@ namespace vr {
|
||||
unsigned int button;
|
||||
};
|
||||
|
||||
class NetworkJoystickButtonHeld : public Event {
|
||||
public:
|
||||
NetworkJoystickButtonHeld(unsigned int joystickId, unsigned int button) :
|
||||
joystickId(joystickId), button(button), Event(vr::Event::EventType::NetworkJoystickButtonHeld) {};
|
||||
|
||||
unsigned int joystickId;
|
||||
unsigned int button;
|
||||
};
|
||||
|
||||
class NetworkJoystickButtonReleased : public Event {
|
||||
public:
|
||||
NetworkJoystickButtonReleased(unsigned int joystickId, unsigned int button) :
|
||||
|
||||
@@ -18,7 +18,7 @@ public:
|
||||
void consume_sf_events(sf::RenderWindow *window);
|
||||
void consume_vr_events();
|
||||
|
||||
void set_flags();
|
||||
void handle_held_keys();
|
||||
void dispatch_events();
|
||||
|
||||
private:
|
||||
@@ -38,3 +38,19 @@ private:
|
||||
std::list<std::unique_ptr<vr::Event>> event_queue;
|
||||
|
||||
};
|
||||
|
||||
class WindowHandler : public VrEventSubscriber {
|
||||
|
||||
public:
|
||||
WindowHandler(sf::RenderWindow *window) : window_ref(window) { };
|
||||
|
||||
virtual void recieve_event(VrEventPublisher* p, std::unique_ptr<vr::Event> event) override {
|
||||
if (event.get()->type == vr::Event::Closed) {
|
||||
window_ref->close();
|
||||
}
|
||||
};
|
||||
|
||||
private:
|
||||
sf::RenderWindow* window_ref;
|
||||
|
||||
};
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <iostream>
|
||||
#include "Event.hpp"
|
||||
#include <memory>
|
||||
|
||||
|
||||
class VrEventPublisher;
|
||||
@@ -9,9 +10,9 @@ class VrEventPublisher;
|
||||
class VrEventSubscriber {
|
||||
public:
|
||||
virtual ~VrEventSubscriber() {};
|
||||
virtual void update(VrEventPublisher* p, vr::Event e) = 0;
|
||||
void subscribe(VrEventPublisher* publisher, vr::Event::EventType type);
|
||||
void subscribe(VrEventPublisher* publisher, std::vector<vr::Event::EventType> type);
|
||||
virtual void recieve_event(VrEventPublisher* publisher, std::unique_ptr<vr::Event> event) = 0;
|
||||
void subscribe_to_publisher(VrEventPublisher* publisher, vr::Event::EventType type);
|
||||
void subscribe_to_publisher(VrEventPublisher* publisher, std::vector<vr::Event::EventType> type);
|
||||
protected:
|
||||
std::vector<vr::Event::EventType> subscribed_event_types;
|
||||
};
|
||||
@@ -24,7 +25,7 @@ public:
|
||||
virtual void subscribe(VrEventSubscriber *subscriber, vr::Event::EventType type);
|
||||
virtual void subscribe(VrEventSubscriber *subscriber, std::vector<vr::Event::EventType> type);
|
||||
virtual void unsubscribe(VrEventSubscriber *s, vr::Event::EventType c);
|
||||
virtual void notify(vr::Event e);
|
||||
virtual void notify_subscribers(std::unique_ptr<vr::Event> event);
|
||||
private:
|
||||
std::map<vr::Event::EventType, std::vector<VrEventSubscriber*>> subscribers;
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
38
src/main.cpp
38
src/main.cpp
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user