More work on the event system, sf::Events are completely wrapped now.

master
MitchellHansen 8 years ago
parent 36851ad011
commit 0e1e9af37c

@ -61,7 +61,11 @@ target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) target_link_libraries (${PNAME} ${OpenCL_LIBRARY})
target_link_libraries (${PNAME} ${OPENGL_LIBRARIES}) target_link_libraries (${PNAME} ${OPENGL_LIBRARIES})
target_link_libraries (${PNAME} ${GLEW_LIBRARIES}) target_link_libraries (${PNAME} ${GLEW_LIBRARIES})
target_link_libraries (${PNAME} -lpthread)
if (NOT WIN32)
target_link_libraries (${PNAME} -lpthread)
endif()
#target_link_libraries (${PNAME} ${Vulkan_LIBRARIES}) #target_link_libraries (${PNAME} ${Vulkan_LIBRARIES})
# Setup to use C++11 # Setup to use C++11

@ -2,9 +2,9 @@
#include <SFML/System/Vector3.hpp> #include <SFML/System/Vector3.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include "util.hpp" #include "util.hpp"
#include "Pub_Sub.hpp" #include "Pub_Sub.h"
class Camera : public SfEventSubscriber{ class Camera : public VrEventSubscriber{
public: public:
enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN }; enum DIRECTION { FORWARD, REARWARD, LEFT, RIGHT, UP, DOWN };
@ -35,7 +35,7 @@ public:
sf::Vector2f get_direction(); sf::Vector2f get_direction();
virtual void update(SfEventPublisher* p, sf::Event e) override; void update(VrEventPublisher* p, vr::Event e) override;
private: private:

@ -4,7 +4,7 @@
#include <SFML/Window/Mouse.hpp> #include <SFML/Window/Mouse.hpp>
#include <SFML/Window/Joystick.hpp> #include <SFML/Window/Joystick.hpp>
#include <SFML/Window/Sensor.hpp> #include <SFML/Window/Sensor.hpp>
#include <GL/glew.h>
namespace vr { namespace vr {
@ -125,7 +125,7 @@ namespace vr {
class MouseWheelScrolled : public Event { class MouseWheelScrolled : public Event {
public: public:
MouseWheelScrolled(sf::Mouse::Wheel wheel, bool delta, bool x, bool y) : MouseWheelScrolled(sf::Mouse::Wheel wheel, float delta, int x, int y) :
wheel(wheel), delta(delta), x(x), y(y), Event(vr::Event::EventType::MouseWheelScrolled) {}; wheel(wheel), delta(delta), x(x), y(y), Event(vr::Event::EventType::MouseWheelScrolled) {};
sf::Mouse::Wheel wheel; sf::Mouse::Wheel wheel;
@ -136,7 +136,7 @@ namespace vr {
class MouseButtonPressed : public Event { class MouseButtonPressed : public Event {
public: public:
MouseButtonPressed(sf::Mouse::Button button, bool x, bool y) : MouseButtonPressed(sf::Mouse::Button button, int x, int y) :
button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonPressed) {}; button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonPressed) {};
sf::Mouse::Button button; sf::Mouse::Button button;
@ -146,7 +146,7 @@ namespace vr {
class MouseButtonReleased : public Event { class MouseButtonReleased : public Event {
public: public:
MouseButtonReleased(sf::Mouse::Button button, bool x, bool y) : MouseButtonReleased(sf::Mouse::Button button, int x, int y) :
button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonReleased) {}; button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonReleased) {};
sf::Mouse::Button button; sf::Mouse::Button button;
@ -156,7 +156,7 @@ namespace vr {
class MouseMoved : public Event { class MouseMoved : public Event {
public: public:
MouseMoved(bool x, bool y) : MouseMoved(int x, int y) :
x(x), y(y), Event(vr::Event::EventType::MouseMoved) {}; x(x), y(y), Event(vr::Event::EventType::MouseMoved) {};
int x; int x;
@ -165,7 +165,7 @@ namespace vr {
class MouseEntered : public Event { class MouseEntered : public Event {
public: public:
MouseEntered(bool x, bool y) : MouseEntered(int x, int y) :
x(x), y(y), Event(vr::Event::EventType::MouseEntered) {}; x(x), y(y), Event(vr::Event::EventType::MouseEntered) {};
int x; int x;
@ -174,7 +174,7 @@ namespace vr {
class MouseLeft : public Event { class MouseLeft : public Event {
public: public:
MouseLeft(bool x, bool y) : MouseLeft(int x, int y) :
x(x), y(y), Event(vr::Event::EventType::MouseLeft) {}; x(x), y(y), Event(vr::Event::EventType::MouseLeft) {};
int x; int x;

@ -1,11 +1,12 @@
#pragma once #pragma once
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <list> #include <list>
#include "Pub_Sub.hpp"
#include "Event.hpp" #include "Event.hpp"
#include <memory>
#include "Pub_Sub.h"
class Input : public SfEventPublisher { class Input : public VrEventPublisher {
public: public:
Input(); Input();
@ -34,6 +35,6 @@ private:
private: private:
std::list<vr::Event> event_queue; std::list<std::unique_ptr<vr::Event>> event_queue;
}; };

@ -0,0 +1,32 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Event.hpp"
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);
protected:
std::vector<vr::Event::EventType> subscribed_event_types;
};
class VrEventPublisher {
public:
virtual ~VrEventPublisher() {};
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);
private:
std::map<vr::Event::EventType, std::vector<VrEventSubscriber*>> subscribers;
};

@ -1,139 +0,0 @@
#pragma once
#include <SFML/Graphics.hpp>
class SfEventPublisher;
class SfEventSubscriber {
public:
virtual ~SfEventSubscriber() {};
virtual void update(SfEventPublisher* p, sf::Event e) = 0;
};
class SfEventPublisher {
public:
// Allows the subscription to classes of events
enum Event_Class {
JoystickButtonEvent,
JoystickConnectEvent,
JoystickMoveEvent,
KeyEvent,
MouseButtonEvent,
MouseMoveEvent,
MouseWheelEvent,
MouseWheelScrollEvent,
SensorEvent,
SizeEvent,
TextEvent,
TouchEvent,
WindowEvent
};
virtual ~SfEventPublisher() {};
virtual void subscribe(SfEventSubscriber *s, Event_Class c) {
subscribers[c].push_back(s);
};
virtual void unsubscribe(SfEventSubscriber *s, Event_Class c) {
std::remove(subscribers[c].begin(), subscribers[c].end(), s);
};
virtual void notify(sf::Event e) {
// I don't quite like that some event classes contained multiple types of events:
// KeyPressed, and KeyReleased under KeyEvent for example.
// While others are not represented by classes:
// Closed, LostFocus, and GainedFocus have no representing class.
// So, we'll add another "class", WindowEvent which contains those previously mentioned,
// and use those new identifiers to group our events
// This will also make it a bit easier when subscribing to certain events
Event_Class event_class;
if (e.type == sf::Event::Closed ||
e.type == sf::Event::LostFocus ||
e.type == sf::Event::GainedFocus ){
event_class = Event_Class::WindowEvent;
}
// Preserve a little of sfml's default behavior and separate resized event
else if (e.type == sf::Event::Resized) {
event_class = Event_Class::SizeEvent;
}
else if (e.type == sf::Event::TextEntered) {
event_class = Event_Class::TextEvent;
}
else if (e.type == sf::Event::KeyPressed ||
e.type == sf::Event::KeyReleased ){
event_class = Event_Class::KeyEvent;
}
else if (e.type == sf::Event::MouseWheelMoved ||
e.type == sf::Event::MouseWheelScrolled ){
event_class = Event_Class::MouseWheelScrollEvent;
}
else if (e.type == sf::Event::MouseButtonPressed ||
e.type == sf::Event::MouseButtonReleased ){
event_class = Event_Class::MouseButtonEvent;
}
// Is this a good idea, mixing events that contain data, and don't contain data?
else if (e.type == sf::Event::MouseMoved ||
e.type == sf::Event::MouseEntered ||
e.type == sf::Event::MouseLeft ){
event_class = Event_Class::MouseMoveEvent;
}
else if (e.type == sf::Event::JoystickButtonPressed ||
e.type == sf::Event::JoystickButtonReleased ){
event_class = Event_Class::JoystickButtonEvent;
}
else if (e.type == sf::Event::JoystickMoved) {
event_class = Event_Class::JoystickMoveEvent;
}
else if (e.type == sf::Event::JoystickConnected ||
e.type == sf::Event::JoystickDisconnected ){
event_class = Event_Class::JoystickConnectEvent;
}
else if (e.type == sf::Event::TouchBegan ||
e.type == sf::Event::TouchEnded ||
e.type == sf::Event::TouchMoved ){
event_class = Event_Class::TouchEvent;
}
else if (e.type == sf::Event::SensorChanged) {
event_class = Event_Class::SensorEvent;
}
else {
std::cout << "Unable to classify sf::Event into Event_Class";
abort();
}
// get the bucket containing subscribers to that Event_Class
std::vector<SfEventSubscriber*> *event_type_bucket = &subscribers[event_class];
// Send them the event
for (auto s : *event_type_bucket) {
s->update(this, e);
}
};
private:
std::map<Event_Class, std::vector<SfEventSubscriber*>> subscribers;
};

@ -1,5 +1,6 @@
#pragma once
#include "Camera.h" #include "Camera.h"
#include "Pub_Sub.h"
Camera::Camera() { Camera::Camera() {
@ -86,18 +87,16 @@ int Camera::update(double delta_time) {
return 1; return 1;
} }
void Camera::update(SfEventPublisher* p, sf::Event e) void Camera::update(VrEventPublisher* p, vr::Event e) {
{
std::cout << "Cam event" << std::endl;
} }
void Camera::look_at_center() { void Camera::look_at_center() {
direction = CartToNormalizedSphere(sf::Vector3f(75, 75, 75) - position); direction = CartToNormalizedSphere(sf::Vector3f(75, 75, 75) - position);
} }
sf::Vector2f* Camera::get_direction_pointer() { sf::Vector2f* Camera::get_direction_pointer() {
return &direction; return &direction;
} }

@ -1,4 +1,6 @@
#include "Input.h" #include "Input.h"
#include <iostream>
#include <memory>
Input::Input() : Input::Input() :
@ -24,6 +26,7 @@ void Input::consume_sf_events(sf::RenderWindow *window) {
transpose_sf_events(sf_event_queue); transpose_sf_events(sf_event_queue);
sf_event_queue.clear();
} }
void Input::consume_vr_events() { void Input::consume_vr_events() {
@ -32,134 +35,131 @@ void Input::consume_vr_events() {
void Input::set_flags() { void Input::set_flags() {
for (auto e: event_queue) { //for (auto e: event_queue) {
if (e.type == sf::Event::KeyPressed) { // if (e.type == vr::Event::KeyPressed) {
held_keys.push_back(e.key.code); // 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); // else if (e.type == sf::Event::KeyReleased) {
} // //std::remove(held_keys.begin(), held_keys.end(), e.key.code);
} // }
//}
} }
void Input::dispatch_events() { void Input::dispatch_events() {
while (event_queue.size() != 0) { while (event_queue.size() != 0) {
notify(event_queue.front()); notify(*event_queue.front().get());
event_queue.pop_front(); event_queue.pop_front();
} }
} }
void Input::transpose_sf_events(std::list<sf::Event> sf_event_queue) { void Input::transpose_sf_events(std::list<sf::Event> sf_event_queue) {
for (auto sf_event: sf_event_queue) { for (auto sf_event: sf_event_queue) {
vr::Event vr_event;
switch(sf_event.type) { switch(sf_event.type) {
case sf::Event::Closed : { case sf::Event::Closed : {
event_queue.push_back(vr::Closed()); event_queue.push_back(std::make_unique<vr::Closed>(vr::Closed()));
break;
}; };
case sf::Event::Resized: { case sf::Event::Resized: {
event_queue.push_back(vr::Resized()); event_queue.push_back(std::make_unique<vr::Resized>(vr::Resized(sf_event.size.width, sf_event.size.height)));
break;
}; };
case sf::Event::LostFocus: { case sf::Event::LostFocus: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::LostFocus>(vr::LostFocus()));
break;
}; };
case sf::Event::GainedFocus: { case sf::Event::GainedFocus: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::GainedFocus>(vr::GainedFocus()));
break;
}; };
case sf::Event::TextEntered: { case sf::Event::TextEntered: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::TextEntered>(vr::TextEntered(sf_event.text.unicode)));
break;
}; };
case sf::Event::KeyPressed: { case sf::Event::KeyPressed: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::KeyPressed>(vr::KeyPressed(sf_event.key.code, sf_event.key.alt, sf_event.key.control, sf_event.key.shift, sf_event.key.system)));
break;
}; };
case sf::Event::KeyReleased: { case sf::Event::KeyReleased: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::KeyReleased>(vr::KeyReleased(sf_event.key.code, sf_event.key.alt, sf_event.key.control, sf_event.key.shift, sf_event.key.system)));
break;
}; };
// Mouse wheel moved will generate a MouseWheelScrolled event with the defaul vertical wheel
case sf::Event::MouseWheelMoved: { case sf::Event::MouseWheelMoved: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::MouseWheelScrolled>(vr::MouseWheelScrolled(sf::Mouse::VerticalWheel, sf_event.mouseWheelScroll.delta, sf_event.mouseWheelScroll.x, sf_event.mouseWheelScroll.y)));
break;
}; };
case sf::Event::Closed: { case sf::Event::MouseWheelScrolled: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::MouseWheelScrolled>(vr::MouseWheelScrolled(sf_event.mouseWheelScroll.wheel, sf_event.mouseWheelScroll.delta, sf_event.mouseWheelScroll.x, sf_event.mouseWheelScroll.y)));
break;
}; };
case sf::Event::Closed: { case sf::Event::MouseButtonPressed: {
vr_event = vr::Closed(); event_queue.push_back(std::make_unique<vr::MouseButtonPressed>(vr::MouseButtonPressed(sf_event.mouseButton.button, sf_event.mouseButton.x, sf_event.mouseButton.y)));
break;
}; };
case sf::Event::MouseButtonReleased: {
} event_queue.push_back(std::make_unique<vr::MouseButtonReleased>(vr::MouseButtonReleased(sf_event.mouseButton.button, sf_event.mouseButton.x, sf_event.mouseButton.y)));
if (e.type == sf::Event::Closed || break;
e.type == sf::Event::LostFocus || };
e.type == sf::Event::GainedFocus) { case sf::Event::MouseMoved: {
event_queue.push_back(std::make_unique<vr::MouseMoved>(vr::MouseMoved(sf_event.mouseMove.x, sf_event.mouseMove.y)));
event_class = Event_Class::WindowEvent; break;
} };
case sf::Event::MouseEntered: {
// Preserve a little of sfml's default behavior and separate resized event event_queue.push_back(std::make_unique<vr::MouseEntered>(vr::MouseEntered(sf_event.mouseMove.x, sf_event.mouseMove.y)));
else if (e.type == sf::Event::Resized) { break;
event_class = Event_Class::SizeEvent; };
} case sf::Event::MouseLeft: {
event_queue.push_back(std::make_unique<vr::MouseLeft>(vr::MouseLeft(sf_event.mouseMove.x, sf_event.mouseMove.x)));
else if (e.type == sf::Event::TextEntered) { break;
event_class = Event_Class::TextEvent; };
} case sf::Event::JoystickButtonPressed: {
event_queue.push_back(std::make_unique<vr::JoystickButtonPressed>(vr::JoystickButtonPressed(sf_event.joystickButton.joystickId, sf_event.joystickButton.button)));
else if (e.type == sf::Event::KeyPressed || break;
e.type == sf::Event::KeyReleased) { };
case sf::Event::JoystickButtonReleased: {
event_class = Event_Class::KeyEvent; event_queue.push_back(std::make_unique<vr::JoystickButtonReleased>(vr::JoystickButtonReleased(sf_event.joystickButton.joystickId, sf_event.joystickButton.button)));
} break;
};
else if (e.type == sf::Event::MouseWheelMoved || case sf::Event::JoystickMoved: {
e.type == sf::Event::MouseWheelScrolled) { event_queue.push_back(std::make_unique<vr::JoystickMoved>(vr::JoystickMoved(sf_event.joystickMove.axis, sf_event.joystickMove.joystickId, sf_event.joystickMove.position)));
break;
event_class = Event_Class::MouseWheelScrollEvent; };
} case sf::Event::JoystickConnected: {
event_queue.push_back(std::make_unique<vr::JoystickConnected>(vr::JoystickConnected(sf_event.joystickConnect.joystickId)));
else if (e.type == sf::Event::MouseButtonPressed || break;
e.type == sf::Event::MouseButtonReleased) { };
case sf::Event::JoystickDisconnected: {
event_class = Event_Class::MouseButtonEvent; event_queue.push_back(std::make_unique<vr::JoystickDisconnected>(vr::JoystickDisconnected(sf_event.joystickConnect.joystickId)));
} break;
};
// Is this a good idea, mixing events that contain data, and don't contain data? case sf::Event::TouchBegan: {
else if (e.type == sf::Event::MouseMoved || event_queue.push_back(std::make_unique<vr::TouchBegan>(vr::TouchBegan(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
e.type == sf::Event::MouseEntered || break;
e.type == sf::Event::MouseLeft) { };
case sf::Event::TouchMoved: {
event_class = Event_Class::MouseMoveEvent; event_queue.push_back(std::make_unique<vr::TouchMoved>(vr::TouchMoved(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
} break;
};
else if (e.type == sf::Event::JoystickButtonPressed || case sf::Event::TouchEnded: {
e.type == sf::Event::JoystickButtonReleased) { event_queue.push_back(std::make_unique<vr::TouchEnded>(vr::TouchEnded(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
break;
event_class = Event_Class::JoystickButtonEvent; };
} case sf::Event::SensorChanged: {
event_queue.push_back(std::make_unique<vr::SensorChanged>(vr::SensorChanged(sf_event.sensor.type, sf_event.sensor.x, sf_event.sensor.y, sf_event.sensor.z)));
else if (e.type == sf::Event::JoystickMoved) { break;
event_class = Event_Class::JoystickMoveEvent; };
} default: {
std::cout << "Event not recognized";
else if (e.type == sf::Event::JoystickConnected || abort();
e.type == sf::Event::JoystickDisconnected) { break;
event_class = Event_Class::JoystickConnectEvent;
}
else if (e.type == sf::Event::TouchBegan ||
e.type == sf::Event::TouchEnded ||
e.type == sf::Event::TouchMoved) {
event_class = Event_Class::TouchEvent;
} }
else if (e.type == sf::Event::SensorChanged) {
event_class = Event_Class::SensorEvent;
} }
} }
} }

@ -0,0 +1,41 @@
#pragma once
#include <vector>
#include "Event.hpp"
#include "Pub_Sub.h"
void VrEventSubscriber::subscribe(VrEventPublisher* publisher, vr::Event::EventType type) {
publisher->subscribe(this, type);
}
void VrEventSubscriber::subscribe(VrEventPublisher* publisher, std::vector<vr::Event::EventType> type) {
publisher->subscribe(this, type);
}
void VrEventPublisher::subscribe(VrEventSubscriber *subscriber, vr::Event::EventType type) {
subscribers[type].push_back(subscriber);
}
void VrEventPublisher::subscribe(VrEventSubscriber *subscriber, std::vector<vr::Event::EventType> type) {
for (auto t : type)
subscribers[t].push_back(subscriber);
}
void VrEventPublisher::unsubscribe(VrEventSubscriber *s, vr::Event::EventType type) {
std::remove(subscribers[type].begin(), subscribers[type].end(), s);
}
void VrEventPublisher::notify(vr::Event event) {
// get the bucket containing subscribers to that Event_Class
std::vector<VrEventSubscriber*> *event_type_bucket = &subscribers[event.type];
// Send them the event
for (auto s : *event_type_bucket) {
s->update(this, event);
}
}

@ -36,6 +36,7 @@
#include <Camera.h> #include <Camera.h>
#include "Software_Caster.h" #include "Software_Caster.h"
#include "Input.h" #include "Input.h"
#include "Pub_Sub.h"
const int WINDOW_X = 1920; const int WINDOW_X = 1920;
@ -147,12 +148,13 @@ int main() {
Input input_handler; Input input_handler;
input_handler.subscribe(camera, SfEventPublisher::Event_Class::KeyEvent); input_handler.subscribe(camera, vr::Event::EventType::KeyPressed);
window.setKeyRepeatEnabled(false); window.setKeyRepeatEnabled(false);
while (window.isOpen()) { while (window.isOpen()) {
input_handler.consume_events(&window); input_handler.consume_sf_events(&window);
input_handler.set_flags(); input_handler.set_flags();
// Poll for events from the user // Poll for events from the user
sf::Event event; sf::Event event;

Loading…
Cancel
Save