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} ${OPENGL_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})
# Setup to use C++11

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

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

@ -1,11 +1,12 @@
#pragma once
#include <SFML/Graphics.hpp>
#include <list>
#include "Pub_Sub.hpp"
#include "Event.hpp"
#include <memory>
#include "Pub_Sub.h"
class Input : public SfEventPublisher {
class Input : public VrEventPublisher {
public:
Input();
@ -34,6 +35,6 @@ 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 "Pub_Sub.h"
Camera::Camera() {
@ -86,18 +87,16 @@ int Camera::update(double delta_time) {
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() {
direction = CartToNormalizedSphere(sf::Vector3f(75, 75, 75) - position);
}
sf::Vector2f* Camera::get_direction_pointer() {
return &direction;
}

@ -1,4 +1,6 @@
#include "Input.h"
#include <iostream>
#include <memory>
Input::Input() :
@ -24,6 +26,7 @@ void Input::consume_sf_events(sf::RenderWindow *window) {
transpose_sf_events(sf_event_queue);
sf_event_queue.clear();
}
void Input::consume_vr_events() {
@ -32,134 +35,131 @@ void Input::consume_vr_events() {
void Input::set_flags() {
for (auto e: event_queue) {
if (e.type == sf::Event::KeyPressed) {
held_keys.push_back(e.key.code);
}
else if (e.type == sf::Event::KeyReleased) {
std::remove(held_keys.begin(), held_keys.end(), e.key.code);
}
}
//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());
notify(*event_queue.front().get());
event_queue.pop_front();
}
}
void Input::transpose_sf_events(std::list<sf::Event> sf_event_queue) {
for (auto sf_event: sf_event_queue) {
vr::Event vr_event;
switch(sf_event.type) {
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: {
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: {
vr_event = vr::Closed();
event_queue.push_back(std::make_unique<vr::LostFocus>(vr::LostFocus()));
break;
};
case sf::Event::GainedFocus: {
vr_event = vr::Closed();
event_queue.push_back(std::make_unique<vr::GainedFocus>(vr::GainedFocus()));
break;
};
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: {
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: {
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: {
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: {
vr_event = vr::Closed();
case sf::Event::MouseWheelScrolled: {
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: {
vr_event = vr::Closed();
case sf::Event::MouseButtonPressed: {
event_queue.push_back(std::make_unique<vr::MouseButtonPressed>(vr::MouseButtonPressed(sf_event.mouseButton.button, sf_event.mouseButton.x, sf_event.mouseButton.y)));
break;
};
}
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;
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)));
break;
};
case sf::Event::MouseMoved: {
event_queue.push_back(std::make_unique<vr::MouseMoved>(vr::MouseMoved(sf_event.mouseMove.x, sf_event.mouseMove.y)));
break;
};
case sf::Event::MouseEntered: {
event_queue.push_back(std::make_unique<vr::MouseEntered>(vr::MouseEntered(sf_event.mouseMove.x, sf_event.mouseMove.y)));
break;
};
case sf::Event::MouseLeft: {
event_queue.push_back(std::make_unique<vr::MouseLeft>(vr::MouseLeft(sf_event.mouseMove.x, sf_event.mouseMove.x)));
break;
};
case sf::Event::JoystickButtonPressed: {
event_queue.push_back(std::make_unique<vr::JoystickButtonPressed>(vr::JoystickButtonPressed(sf_event.joystickButton.joystickId, sf_event.joystickButton.button)));
break;
};
case sf::Event::JoystickButtonReleased: {
event_queue.push_back(std::make_unique<vr::JoystickButtonReleased>(vr::JoystickButtonReleased(sf_event.joystickButton.joystickId, sf_event.joystickButton.button)));
break;
};
case sf::Event::JoystickMoved: {
event_queue.push_back(std::make_unique<vr::JoystickMoved>(vr::JoystickMoved(sf_event.joystickMove.axis, sf_event.joystickMove.joystickId, sf_event.joystickMove.position)));
break;
};
case sf::Event::JoystickConnected: {
event_queue.push_back(std::make_unique<vr::JoystickConnected>(vr::JoystickConnected(sf_event.joystickConnect.joystickId)));
break;
};
case sf::Event::JoystickDisconnected: {
event_queue.push_back(std::make_unique<vr::JoystickDisconnected>(vr::JoystickDisconnected(sf_event.joystickConnect.joystickId)));
break;
};
case sf::Event::TouchBegan: {
event_queue.push_back(std::make_unique<vr::TouchBegan>(vr::TouchBegan(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
break;
};
case sf::Event::TouchMoved: {
event_queue.push_back(std::make_unique<vr::TouchMoved>(vr::TouchMoved(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
break;
};
case sf::Event::TouchEnded: {
event_queue.push_back(std::make_unique<vr::TouchEnded>(vr::TouchEnded(sf_event.touch.finger, sf_event.touch.x, sf_event.touch.y)));
break;
};
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)));
break;
};
default: {
std::cout << "Event not recognized";
abort();
break;
}
}
}
}

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

Loading…
Cancel
Save