Did a lot of boilerplate on the Event wrapper pt.2

master
MitchellHansen 8 years ago
parent 4cd9bbb4ed
commit 2e0227419c

@ -1,11 +1,27 @@
#pragma once
#include <SFML/Config.hpp>
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Mouse.hpp>
#include <SFML/Window/Joystick.hpp>
#include <SFML/Window/Sensor.hpp>
#include <GL/glew.h>
namespace vr {
// The event class here will both abstract out
// the SFML sf::Event class, but will also provide
// the ability to easily extend the class to contain
// even more event types.
// 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
class Event {
public:
enum EventType
{
Closed,
@ -31,44 +47,274 @@ namespace vr {
TouchMoved,
TouchEnded,
SensorChanged,
NetworkJoystickButtonPressed,
NetworkJoystickButtonReleased,
NetworkJoystickMoved,
NetworkJoystickConnected,
NetworkJoystickDisconnected,
Count
};
Event(EventType type) : type(type) {
};
EventType type;
};
Closed,
Resized,
LostFocus,
GainedFocus,
TextEntered,
KeyPressed,
KeyReleased,
MouseWheelMoved,
MouseWheelScrolled,
MouseButtonPressed,
MouseButtonReleased,
MouseMoved,
MouseEntered,
MouseLeft,
JoystickButtonPressed,
JoystickButtonReleased,
JoystickMoved,
JoystickConnected,
JoystickDisconnected,
TouchBegan,
TouchMoved,
TouchEnded,
SensorChanged,
Count
class Closed : public Event {
public:
Closed() : Event(vr::Event::EventType::Closed) {};
};
class Resized : public Event {
public:
Resized(unsigned int width, unsigned int height) :
width(width), height(height), Event(vr::Event::EventType::Resized) {};
unsigned int width;
unsigned int height;
};
class LostFocus : public Event {
public:
LostFocus() : Event(vr::Event::EventType::LostFocus) { };
};
class GainedFocus : public Event {
public:
GainedFocus() : Event(vr::Event::EventType::GainedFocus) {};
};
class TextEntered : public Event {
public:
TextEntered(sf::Uint32 unicode) :
unicode(unicode), Event(vr::Event::EventType::TextEntered) {};
sf::Uint32 unicode;
};
class KeyPressed : public Event {
public:
KeyPressed(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::KeyPressed) {};
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) :
code(code), alt(alt), control(control), shift(shift), system(system), Event(vr::Event::EventType::KeyReleased) {};
sf::Keyboard::Key code;
bool alt;
bool control;
bool shift;
bool system;
};
// This is depreciated, remove?
class MouseWheelMoved : public Event {
public:
MouseWheelMoved() : Event(vr::Event::EventType::MouseWheelMoved) {};
};
class MouseWheelScrolled : public Event {
public:
MouseWheelScrolled(sf::Mouse::Wheel wheel, bool delta, bool x, bool y) :
wheel(wheel), delta(delta), x(x), y(y), Event(vr::Event::EventType::MouseWheelScrolled) {};
sf::Mouse::Wheel wheel;
float delta;
int x;
int y;
};
class MouseButtonPressed : public Event {
public:
MouseButtonPressed(sf::Mouse::Button button, bool x, bool y) :
button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonPressed) {};
sf::Mouse::Button button;
int x;
int y;
};
class MouseButtonReleased : public Event {
public:
MouseButtonReleased(sf::Mouse::Button button, bool x, bool y) :
button(button), x(x), y(y), Event(vr::Event::EventType::MouseButtonReleased) {};
sf::Mouse::Button button;
int x;
int y;
};
class MouseMoved : public Event {
public:
MouseMoved(bool x, bool y) :
x(x), y(y), Event(vr::Event::EventType::MouseMoved) {};
int x;
int y;
};
class MouseEntered : public Event {
public:
MouseEntered(bool x, bool y) :
x(x), y(y), Event(vr::Event::EventType::MouseEntered) {};
int x;
int y;
};
class MouseLeft : public Event {
public:
MouseLeft(bool x, bool y) :
x(x), y(y), Event(vr::Event::EventType::MouseLeft) {};
int x;
int y;
};
class JoystickButtonPressed : public Event {
public:
JoystickButtonPressed(unsigned int joystickId, unsigned int button) :
joystickId(joystickId), button(button), Event(vr::Event::EventType::JoystickButtonPressed) {};
unsigned int joystickId;
unsigned int button;
};
class JoystickButtonReleased : public Event {
public:
JoystickButtonReleased(unsigned int joystickId, unsigned int button) :
joystickId(joystickId), button(button), Event(vr::Event::EventType::JoystickButtonReleased) {};
unsigned int joystickId;
unsigned int button;
};
class JoystickMoved : public Event {
public:
JoystickMoved(sf::Joystick::Axis axis, unsigned int joystickId, float position) :
axis(axis), joystickId(joystickId), position(position), Event(vr::Event::EventType::JoystickMoved) {};
sf::Joystick::Axis axis;
unsigned int joystickId;
float position;
};
class JoystickConnected : public Event {
public:
JoystickConnected(unsigned int joystickId) :
joystickId(joystickId), Event(vr::Event::EventType::JoystickConnected) {};
unsigned int joystickId;
};
class JoystickDisconnected : public Event {
public:
JoystickDisconnected(unsigned int joystickId) :
joystickId(joystickId), Event(vr::Event::EventType::JoystickDisconnected) {};
unsigned int joystickId;
};
class TouchBegan : public Event {
public:
TouchBegan(unsigned int finger, int x, int y) :
finger(finger), x(x), y(y), Event(vr::Event::EventType::TouchBegan) {};
unsigned int finger;
int x;
int y;
};
class TouchMoved : public Event {
public:
TouchMoved(unsigned int finger, int x, int y) :
finger(finger), x(x), y(y), Event(vr::Event::EventType::TouchMoved) {};
unsigned int finger;
int x;
int y;
};
class TouchEnded : public Event {
public:
TouchEnded(unsigned int finger, int x, int y) :
finger(finger), x(x), y(y), Event(vr::Event::EventType::TouchEnded) {};
unsigned int finger;
int x;
int y;
};
class SensorChanged : public Event {
public:
SensorChanged(sf::Sensor::Type type, float x, float y, float z) :
type(type), x(x), y(y), z(z), Event(vr::Event::EventType::SensorChanged) {};
sf::Sensor::Type type;
float x;
float y;
float z;
};
// I'm moving this to it's own event type because
// I don't want to have to mess with the static
// sf::Joystick class and how it hits a good half
// dozen impl files.
class NetworkJoystickButtonPressed : public Event {
public:
NetworkJoystickButtonPressed(unsigned int joystickId, unsigned int button) :
joystickId(joystickId), button(button), Event(vr::Event::EventType::NetworkJoystickButtonPressed) {};
unsigned int joystickId;
unsigned int button;
};
class NetworkJoystickButtonReleased : public Event {
public:
NetworkJoystickButtonReleased(unsigned int joystickId, unsigned int button) :
joystickId(joystickId), button(button), Event(vr::Event::EventType::NetworkJoystickButtonReleased) {};
unsigned int joystickId;
unsigned int button;
};
class NetworkJoystickMoved : public Event {
public:
NetworkJoystickMoved(sf::Joystick::Axis axis, unsigned int joystickId, float position) :
axis(axis), joystickId(joystickId), position(position), Event(vr::Event::EventType::NetworkJoystickMoved) {};
sf::Joystick::Axis axis;
unsigned int joystickId;
float position;
};
class NetworkJoystickConnected : public Event {
public:
NetworkJoystickConnected(unsigned int joystickId) :
joystickId(joystickId), Event(vr::Event::EventType::NetworkJoystickConnected) {};
unsigned int joystickId;
};
class NetworkJoystickDisconnected : public Event {
public:
NetworkJoystickDisconnected(unsigned int joystickId) :
joystickId(joystickId), Event(vr::Event::EventType::NetworkJoystickDisconnected) {};
unsigned int joystickId;
};
}
}

@ -2,6 +2,7 @@
#include <SFML/Graphics.hpp>
#include <list>
#include "Pub_Sub.hpp"
#include "Event.hpp"
class Input : public SfEventPublisher {
@ -13,12 +14,18 @@ public:
// Keep track of keys that are not released
// Keep track of mouse up and downs in conjunction with dragging
// Keep track of joystick buttons
void consume_events(sf::RenderWindow *window);
void consume_sf_events(sf::RenderWindow *window);
void consume_vr_events();
void set_flags();
void dispatch_events();
private:
void transpose_sf_events(std::list<sf::Event> event_queue);
// Network controller class
std::vector<sf::Keyboard::Key> held_keys;
std::vector<sf::Mouse::Button> held_mouse_buttons;
@ -26,5 +33,7 @@ private:
std::vector<bool> mouse_flags;
private:
std::list<sf::Event> event_queue;
std::list<vr::Event> event_queue;
};

@ -13,13 +13,21 @@ Input::~Input()
}
void Input::consume_events(sf::RenderWindow *window) {
void Input::consume_sf_events(sf::RenderWindow *window) {
std::list<sf::Event> sf_event_queue;
sf::Event e;
while (window->pollEvent(e)) {
event_queue.push_back(e);
sf_event_queue.push_back(e);
}
transpose_sf_events(sf_event_queue);
}
void Input::consume_vr_events() {
}
void Input::set_flags() {
@ -41,3 +49,117 @@ void Input::dispatch_events() {
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());
};
case sf::Event::Resized: {
event_queue.push_back(vr::Resized());
};
case sf::Event::LostFocus: {
vr_event = vr::Closed();
};
case sf::Event::GainedFocus: {
vr_event = vr::Closed();
};
case sf::Event::TextEntered: {
vr_event = vr::Closed();
};
case sf::Event::KeyPressed: {
vr_event = vr::Closed();
};
case sf::Event::KeyReleased: {
vr_event = vr::Closed();
};
case sf::Event::MouseWheelMoved: {
vr_event = vr::Closed();
};
case sf::Event::Closed: {
vr_event = vr::Closed();
};
case sf::Event::Closed: {
vr_event = vr::Closed();
};
}
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;
}
}
}

Loading…
Cancel
Save