From efca7db6c7e5779d637ee33587f38ea2e006dedf Mon Sep 17 00:00:00 2001 From: skaupper Date: Fri, 24 Feb 2017 15:11:02 +0100 Subject: [PATCH] remove application event (we do not need mobile events) add string representations TODO: add comparison operators for bkengine::Keys --- CMakeLists.txt | 2 + include/bkengine/Event.h | 11 +- include/bkengine/Keys.h | 251 ++++++++++++++++++++------------------ src/Event.cpp | 42 +++++++ src/Keys.cpp | 142 +++++++++++++++++++++ src/SDLEventInterface.cpp | 16 ++- 6 files changed, 335 insertions(+), 129 deletions(-) create mode 100644 src/Event.cpp create mode 100644 src/Keys.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b0a6713..2f43996 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,6 +36,8 @@ SET(SOURCES src/Core.cpp src/Misc.cpp src/Fonts.cpp src/Entity.cpp + src/Event.cpp + src/Keys.cpp src/SDLEventInterface.cpp) IF(ENABLE_TESTS) diff --git a/include/bkengine/Event.h b/include/bkengine/Event.h index 46c400d..dab3ca5 100644 --- a/include/bkengine/Event.h +++ b/include/bkengine/Event.h @@ -23,7 +23,6 @@ namespace bkengine enum class EventType { UNKNOWN, - APPLICATION, KEYBOARD, MOUSE, MOTION, @@ -60,10 +59,6 @@ namespace bkengine WheelDirection direction; }; - struct ApplicationEvent { - // TODO: implement - }; - struct WindowEvent { // TODO: implement }; @@ -71,6 +66,11 @@ namespace bkengine class Event { public: + Event() {} + virtual ~Event() {} + Event(const Event &event); + Event &operator=(const Event &event); + EventType type = EventType::UNKNOWN; uint32_t timeStamp = std::chrono::duration_cast (std::chrono::system_clock::now().time_since_epoch()).count(); @@ -81,7 +81,6 @@ namespace bkengine MouseEvent mouse; MotionEvent motion; WheelEvent wheel; - ApplicationEvent application; WindowEvent window; }; }; diff --git a/include/bkengine/Keys.h b/include/bkengine/Keys.h index a463555..c3a9c37 100644 --- a/include/bkengine/Keys.h +++ b/include/bkengine/Keys.h @@ -1,149 +1,158 @@ #ifndef KEYS_H #define KEYS_H +#include + namespace bkengine { - enum class Keys { - UNKNOWN, + class Keys + { + private: + std::string keyString; + + public: + Keys() : Keys("UNKNOWN") {} + Keys(const std::string &keyString) : keyString(keyString) {} + + static Keys UNKNOWN; /* Control keys */ - RETURN, - ESCAPE, - BACKSPACE, - TAB, - SPACE, - CAPSLOCK, + static Keys RETURN; + static Keys ESCAPE; + static Keys BACKSPACE; + static Keys TAB; + static Keys SPACE; + static Keys CAPSLOCK; /* Number keys */ - ZERO, - ONE, - TWO, - THREE, - FOUR, - FIVE, - SIX, - SEVEN, - EIGHT, - NINE, + static Keys ZERO; + static Keys ONE; + static Keys TWO; + static Keys THREE; + static Keys FOUR; + static Keys FIVE; + static Keys SIX; + static Keys SEVEN; + static Keys EIGHT; + static Keys NINE; /* Letter keys */ - A, - B, - C, - D, - E, - F, - G, - H, - I, - J, - K, - L, - M, - N, - O, - P, - Q, - R, - S, - T, - U, - V, - W, - X, - Y, - Z, + static Keys A; + static Keys B; + static Keys C; + static Keys D; + static Keys E; + static Keys F; + static Keys G; + static Keys H; + static Keys I; + static Keys J; + static Keys K; + static Keys L; + static Keys M; + static Keys N; + static Keys O; + static Keys P; + static Keys Q; + static Keys R; + static Keys S; + static Keys T; + static Keys U; + static Keys V; + static Keys W; + static Keys X; + static Keys Y; + static Keys Z; /* Function keys */ - F1, - F2, - F3, - F4, - F5, - F6, - F7, - F8, - F9, - F10, - F11, - F12, - F13, - F14, - F15, - F16, - F17, - F18, - F19, - F20, - F21, - F22, - F23, - F24, + static Keys F1; + static Keys F2; + static Keys F3; + static Keys F4; + static Keys F5; + static Keys F6; + static Keys F7; + static Keys F8; + static Keys F9; + static Keys F10; + static Keys F11; + static Keys F12; + static Keys F13; + static Keys F14; + static Keys F15; + static Keys F16; + static Keys F17; + static Keys F18; + static Keys F19; + static Keys F20; + static Keys F21; + static Keys F22; + static Keys F23; + static Keys F24; /* Arrow keys */ - RIGHT, - LEFT, - DOWN, - UP, + static Keys RIGHT; + static Keys LEFT; + static Keys DOWN; + static Keys UP; - PRINTSCREEN, - SCROLLLOCK, - PAUSE, - INSERT, - HOME, - PAGEUP, - DELETE, - END, - PAGEDOWN, + static Keys PRINTSCREEN; + static Keys SCROLLLOCK; + static Keys PAUSE; + static Keys INSERT; + static Keys HOME; + static Keys PAGEUP; + static Keys DELETE; + static Keys END; + static Keys PAGEDOWN; /* Numpad keys */ - NUMLOCKCLEAR, - NP_DIVIDE, - NP_MULTIPLY, - NP_MINUS, - NP_PLUS, - NP_ENTER, - NP_ONE, - NP_TWO, - NP_THREE, - NP_FOUR, - NP_FIVE, - NP_SIX, - NP_SEVEN, - NP_EIGHT, - NP_NINE, - NP_ZERO, - NP_SEPARATOR, + static Keys NUMLOCKCLEAR; + static Keys NP_DIVIDE; + static Keys NP_MULTIPLY; + static Keys NP_MINUS; + static Keys NP_PLUS; + static Keys NP_ENTER; + static Keys NP_ONE; + static Keys NP_TWO; + static Keys NP_THREE; + static Keys NP_FOUR; + static Keys NP_FIVE; + static Keys NP_SIX; + static Keys NP_SEVEN; + static Keys NP_EIGHT; + static Keys NP_NINE; + static Keys NP_ZERO; + static Keys NP_SEPARATOR; /* Modifier keys */ - LCTRL, - LSHIFT, - LALT, - LGUI, - RCTRL, - RSHIFT, - RALT, - RGUI, + static Keys LCTRL; + static Keys LSHIFT; + static Keys LALT; + static Keys LGUI; + static Keys RCTRL; + static Keys RSHIFT; + static Keys RALT; + static Keys RGUI; /* Media keys */ - MUTE, - VOLUMEUP, - VOLUMEDOWN, - AUDIONEXT, - AUDIOPREV, - AUDIOSTOP, - AUDIOPLAY, - AUDIOMUTE, + static Keys MUTE; + static Keys VOLUMEUP; + static Keys VOLUMEDOWN; + static Keys AUDIONEXT; + static Keys AUDIOPREV; + static Keys AUDIOSTOP; + static Keys AUDIOPLAY; + static Keys AUDIOMUTE; /* Special keys */ - APPLICATION, - MENU, - BRIGHTNESSDOWN, - BRIGHTNESSUP, - SLEEP, - POWER + static Keys APPLICATION; + static Keys MENU; + static Keys BRIGHTNESSDOWN; + static Keys BRIGHTNESSUP; + static Keys SLEEP; + static Keys POWER; }; - enum class Buttons { LEFT, MIDDLE, diff --git a/src/Event.cpp b/src/Event.cpp new file mode 100644 index 0000000..7f67c12 --- /dev/null +++ b/src/Event.cpp @@ -0,0 +1,42 @@ +#include "Event.h" + +using namespace bkengine; + +static void copy(Event &e1, const Event &e2) +{ + e1.type = e2.type; + e1.timeStamp = e2.timeStamp; + e1.windowId = e2.windowId; + switch(e1.type) { + case EventType::KEYBOARD: + e1.keyboard = e2.keyboard; + break; + + case EventType::MOUSE: + e1.mouse = e2.mouse; + break; + + case EventType::MOTION: + e1.motion = e2.motion; + break; + + case EventType::WHEEL: + e1.wheel = e2.wheel; + break; + + case EventType::UNKNOWN: + case EventType::QUIT: + break; + } +} + +Event::Event(const Event &event) +{ + copy(*this, event); +} + +Event &Event::operator=(const Event &event) +{ + copy(*this, event); + return *this; +} diff --git a/src/Keys.cpp b/src/Keys.cpp new file mode 100644 index 0000000..597f85d --- /dev/null +++ b/src/Keys.cpp @@ -0,0 +1,142 @@ +#include "Keys.h" + +using namespace bkengine; + +Keys Keys::UNKNOWN("UNKNOWN"); + +/* Control keys */ +Keys Keys::RETURN("RETURN"); +Keys Keys::ESCAPE("ESCAPE"); +Keys Keys::BACKSPACE("BACKSPACE"); +Keys Keys::TAB("TAB"); +Keys Keys::SPACE("SPACE"); +Keys Keys::CAPSLOCK("CAPSLOCK"); + +/* Number keys */ +Keys Keys::ZERO("ZERO"); +Keys Keys::ONE("ONE"); +Keys Keys::TWO("TWO"); +Keys Keys::THREE("THREE"); +Keys Keys::FOUR("FOUR"); +Keys Keys::FIVE("FIVE"); +Keys Keys::SIX("SIX"); +Keys Keys::SEVEN("SEVEN"); +Keys Keys::EIGHT("EIGHT"); +Keys Keys::NINE("NINE"); + +/* Letter keys */ +Keys Keys::A("A"); +Keys Keys::B("B"); +Keys Keys::C("C"); +Keys Keys::D("D"); +Keys Keys::E("E"); +Keys Keys::F("F"); +Keys Keys::G("G"); +Keys Keys::H("H"); +Keys Keys::I("I"); +Keys Keys::J("J"); +Keys Keys::K("K"); +Keys Keys::L("L"); +Keys Keys::M("M"); +Keys Keys::N("N"); +Keys Keys::O("O"); +Keys Keys::P("P"); +Keys Keys::Q("Q"); +Keys Keys::R("R"); +Keys Keys::S("S"); +Keys Keys::T("T"); +Keys Keys::U("U"); +Keys Keys::V("V"); +Keys Keys::W("W"); +Keys Keys::X("X"); +Keys Keys::Y("Y"); +Keys Keys::Z("Z"); + +/* Function keys */ +Keys Keys::F1("F1"); +Keys Keys::F2("F2"); +Keys Keys::F3("F3"); +Keys Keys::F4("F4"); +Keys Keys::F5("F5"); +Keys Keys::F6("F6"); +Keys Keys::F7("F7"); +Keys Keys::F8("F8"); +Keys Keys::F9("F9"); +Keys Keys::F10("F10"); +Keys Keys::F11("F11"); +Keys Keys::F12("F12"); +Keys Keys::F13("F13"); +Keys Keys::F14("F14"); +Keys Keys::F15("F15"); +Keys Keys::F16("F16"); +Keys Keys::F17("F17"); +Keys Keys::F18("F18"); +Keys Keys::F19("F19"); +Keys Keys::F20("F20"); +Keys Keys::F21("F21"); +Keys Keys::F22("F22"); +Keys Keys::F23("F23"); +Keys Keys::F24("F24"); + +/* Arrow keys */ +Keys Keys::RIGHT("RIGHT"); +Keys Keys::LEFT("LEFT"); +Keys Keys::DOWN("DOWN"); +Keys Keys::UP("UP"); + +Keys Keys::PRINTSCREEN("PRINTSCREEN"); +Keys Keys::SCROLLLOCK("SCROLLLOCK"); +Keys Keys::PAUSE("PAUSE"); +Keys Keys::INSERT("INSERT"); +Keys Keys::HOME("HOME"); +Keys Keys::PAGEUP("PAGEUP"); +Keys Keys::DELETE("DELETE"); +Keys Keys::END("END"); +Keys Keys::PAGEDOWN("PAGEDOWN"); + +/* Numpad keys */ +Keys Keys::NUMLOCKCLEAR("NUMLOCKCLEAR"); +Keys Keys::NP_DIVIDE("NP_DIVIDE"); +Keys Keys::NP_MULTIPLY("NP_MULTIPLY"); +Keys Keys::NP_MINUS("NP_MINUS"); +Keys Keys::NP_PLUS("NP_PLUS"); +Keys Keys::NP_ENTER("NP_ENTER"); +Keys Keys::NP_ONE("NP_ONE"); +Keys Keys::NP_TWO("NP_TWO"); +Keys Keys::NP_THREE("NP_THREE"); +Keys Keys::NP_FOUR("NP_FOUR"); +Keys Keys::NP_FIVE("NP_FIVE"); +Keys Keys::NP_SIX("NP_SIX"); +Keys Keys::NP_SEVEN("NP_SEVEN"); +Keys Keys::NP_EIGHT("NP_EIGHT"); +Keys Keys::NP_NINE("NP_NINE"); +Keys Keys::NP_ZERO("NP_ZERO"); +Keys Keys::NP_SEPARATOR("NP_SEPARATOR"); + +/* Modifier keys */ +Keys Keys::LCTRL("LCTRL"); +Keys Keys::LSHIFT("LSHIFT"); +Keys Keys::LALT("LALT"); +Keys Keys::LGUI("LGUI"); +Keys Keys::RCTRL("RCTRL"); +Keys Keys::RSHIFT("RSHIFT"); +Keys Keys::RALT("RALT"); +Keys Keys::RGUI("RGUI"); + +/* Media keys */ +Keys Keys::MUTE("MUTE"); +Keys Keys::VOLUMEUP("VOLUMEUP"); +Keys Keys::VOLUMEDOWN("VOLUMEDOWN"); +Keys Keys::AUDIONEXT("AUDIONEXT"); +Keys Keys::AUDIOPREV("AUDIOPREV"); +Keys Keys::AUDIOSTOP("AUDIOSTOP"); +Keys Keys::AUDIOPLAY("AUDIOPLAY"); +Keys Keys::AUDIOMUTE("AUDIOMUTE"); + +/* Special keys */ +Keys Keys::APPLICATION("APPLICATION"); +Keys Keys::MENU("MENU"); +Keys Keys::BRIGHTNESSDOWN("BRIGHTNESSDOWN"); +Keys Keys::BRIGHTNESSUP("BRIGHTNESSUP"); +Keys Keys::SLEEP("SLEEP"); +Keys Keys::POWER("POWER"); diff --git a/src/SDLEventInterface.cpp b/src/SDLEventInterface.cpp index 22b09a8..c01bb78 100644 --- a/src/SDLEventInterface.cpp +++ b/src/SDLEventInterface.cpp @@ -161,6 +161,7 @@ Event SDLEventInterface::poll() switch (event.type) { case SDL_KEYDOWN: case SDL_KEYUP: + { KeyboardEvent keyboardEvent; e.type = EventType::KEYBOARD; e.windowId = event.key.windowID; @@ -170,9 +171,11 @@ Event SDLEventInterface::poll() keyboardEvent.repeat = (event.key.repeat != 0); e.keyboard = keyboardEvent; break; + } case SDL_MOUSEBUTTONDOWN: case SDL_MOUSEBUTTONUP: + { MouseEvent mouseEvent; e.type = EventType::MOUSE; e.windowId = event.button.windowID; @@ -204,19 +207,23 @@ Event SDLEventInterface::poll() ButtonState::UP); e.mouse = mouseEvent; break; + } case SDL_MOUSEMOTION: + { MotionEvent motionEvent; e.type = EventType::MOTION; e.windowId = event.motion.windowID; - mouseEvent.x = event.motion.x; - mouseEvent.y = event.motion.y; + motionEvent.x = event.motion.x; + motionEvent.y = event.motion.y; motionEvent.relativeX = event.motion.xrel; motionEvent.relativeY = event.motion.yrel; e.motion = motionEvent; break; + } case SDL_MOUSEWHEEL: + { WheelEvent wheelEvent; e.type = EventType::WHEEL; e.windowId = event.motion.windowID; @@ -226,16 +233,21 @@ Event SDLEventInterface::poll() WheelDirection::NORMAL : WheelDirection::FLIPPED); e.wheel = wheelEvent; break; + } case SDL_QUIT: + { e.type = EventType::QUIT; break; + } default: + { e.type = EventType::UNKNOWN; /* This one would spam a lot! */ // Logger::LogDebug("SDLEventInterface::poll(): SDL event " + std::to_string(event.type) + " occurred which is not mapped by the interface"); break; + } } return e;