Browse Source

Platform: various minor EmscriptenApplication cleanup.

pull/300/head
Vladimír Vondruš 7 years ago
parent
commit
2952a19287
  1. 32
      src/Magnum/Platform/EmscriptenApplication.cpp
  2. 43
      src/Magnum/Platform/EmscriptenApplication.h

32
src/Magnum/Platform/EmscriptenApplication.cpp

@ -28,7 +28,6 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#include <emscripten/html5.h> #include <emscripten/html5.h>
#include <Corrade/Containers/ArrayView.h> #include <Corrade/Containers/ArrayView.h>
#include <Corrade/Utility/Arguments.h> #include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
@ -98,11 +97,11 @@ namespace {
/* Predicate for Entry "less than" to use with std::lower_bound */ /* Predicate for Entry "less than" to use with std::lower_bound */
struct EntryCompare { struct EntryCompare {
bool operator()(const Entry& a, const char* const& b) { bool operator()(const Entry& a, const char* const b) {
return std::strcmp(a.key, b) < 0; return std::strcmp(a.key, b) < 0;
} }
bool operator()(const char*& a, const Entry& b) { bool operator()(const char* const a, const Entry& b) {
return std::strcmp(a, b.key) < 0; return std::strcmp(a, b.key) < 0;
} }
}; };
@ -115,8 +114,8 @@ namespace {
@param code Keyboard layout independent key string, e.g. 'KeyA' or 'Minus'. @param code Keyboard layout independent key string, e.g. 'KeyA' or 'Minus'.
Note that the y key on some layouts may result in 'KeyZ'. Note that the y key on some layouts may result in 'KeyZ'.
*/ */
Key toKey(const EM_UTF8* key, const EM_UTF8* code) { Key toKey(const EM_UTF8* const key, const EM_UTF8* const code) {
const size_t keyLength = std::strlen(key); const std::size_t keyLength = std::strlen(key);
if(keyLength == 0) return Key::Unknown; if(keyLength == 0) return Key::Unknown;
/* We use key for a-z as it gives us a keyboard layout respecting /* We use key for a-z as it gives us a keyboard layout respecting
@ -129,7 +128,7 @@ namespace {
/* We use code for 0-9 as it allows us to differentiate towards Numpad digits. /* We use code for 0-9 as it allows us to differentiate towards Numpad digits.
For digits independent of numpad or not, key is e.g. '0' for Zero */ For digits independent of numpad or not, key is e.g. '0' for Zero */
const size_t codeLength = std::strlen(code); const std::size_t codeLength = std::strlen(code);
if(Utility::String::viewBeginsWith({code, codeLength}, "Digit")) { if(Utility::String::viewBeginsWith({code, codeLength}, "Digit")) {
return Key(code[5]); return Key(code[5]);
@ -146,20 +145,15 @@ namespace {
/* Numpad0 - Numpad9 */ /* Numpad0 - Numpad9 */
const Int num = numKey[0] - '0'; const Int num = numKey[0] - '0';
if(num >= 0 && num <= 9) { if(num >= 0 && num <= 9) return Key(num + Int(Key::NumZero));
return Key(num + Int(Key::NumZero));
}
return Key::Unknown; return Key::Unknown;
} }
const auto mapping = Containers::arrayView(KeyMapping, const Containers::ArrayView<const Entry> mapping = KeyMapping;
Containers::arraySize(KeyMapping)); const Entry* found = std::lower_bound(mapping.begin(), mapping.end(), code, EntryCompare{});
const Entry* found = if(found != mapping.end() && std::strcmp(found->key, code) == 0)
std::lower_bound(mapping.begin(), mapping.end(), code, EntryCompare{});
if(found != mapping.end() && std::strcmp(found->key, code) == 0) {
return found->value; return found->value;
}
/* F1 - F12 */ /* F1 - F12 */
if(code[0] == 'F') { if(code[0] == 'F') {
@ -175,7 +169,6 @@ namespace {
return Key::Unknown; return Key::Unknown;
} }
} }
#ifdef MAGNUM_TARGET_GL #ifdef MAGNUM_TARGET_GL
@ -573,7 +566,7 @@ Vector2 EmscriptenApplication::MouseScrollEvent::offset() const {
DOM_DELTA_PAGE => 1 page = 80 steps DOM_DELTA_PAGE => 1 page = 80 steps
*/ */
const Float f = (_event->deltaMode == DOM_DELTA_PIXEL) ? -0.01f : const Float f = (_event->deltaMode == DOM_DELTA_PIXEL) ? -0.01f :
((_event->deltaMode == DOM_DELTA_LINE) ? -1.0f/3.0f : -80.0f); ((_event->deltaMode == DOM_DELTA_LINE) ? -1.0f/3.0f : -80.0f);
return {f*Float(_event->deltaX), f*Float(_event->deltaY)}; return {f*Float(_event->deltaX), f*Float(_event->deltaY)};
} }
@ -597,9 +590,8 @@ Key EmscriptenApplication::KeyEvent::key() const {
std::string EmscriptenApplication::KeyEvent::keyName() const { std::string EmscriptenApplication::KeyEvent::keyName() const {
if((_event->key[0] >= 'a' && _event->key[0] <= 'z') || if((_event->key[0] >= 'a' && _event->key[0] <= 'z') ||
(_event->key[0] >= 'A' && _event->key[0] <= 'Z')) { (_event->key[0] >= 'A' && _event->key[0] <= 'Z')) return _event->key;
return _event->key;
}
return _event->code; return _event->code;
} }

43
src/Magnum/Platform/EmscriptenApplication.h

@ -33,7 +33,6 @@
#endif #endif
#include <string> #include <string>
#include <Corrade/Containers/ArrayView.h> #include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/Pointer.h> #include <Corrade/Containers/Pointer.h>
@ -129,7 +128,6 @@ class EmscriptenApplication {
class MouseScrollEvent; class MouseScrollEvent;
class KeyEvent; class KeyEvent;
class TextInputEvent; class TextInputEvent;
class TextEditingEvent;
/** /**
* @brief Execute main loop * @brief Execute main loop
@ -474,7 +472,6 @@ class EmscriptenApplication {
/* These are saved from command-line arguments */ /* These are saved from command-line arguments */
bool _verboseLog{}; bool _verboseLog{};
Vector2 _commandLineDpiScaling; Vector2 _commandLineDpiScaling;
}; };
CORRADE_ENUMSET_OPERATORS(EmscriptenApplication::Flags) CORRADE_ENUMSET_OPERATORS(EmscriptenApplication::Flags)
@ -494,7 +491,7 @@ class EmscriptenApplication::GLConfiguration {
* @see @ref Flags, @ref setFlags(), @ref Context::Flag * @see @ref Flags, @ref setFlags(), @ref Context::Flag
* @requires_gles Context flags are not available in WebGL. * @requires_gles Context flags are not available in WebGL.
*/ */
enum class Flag: int { enum class Flag: Int {
/** /**
* Premultiplied alpha * Premultiplied alpha
* *
@ -708,7 +705,6 @@ Double-buffered RGBA canvas with depth and stencil buffers.
*/ */
class EmscriptenApplication::Configuration { class EmscriptenApplication::Configuration {
public: public:
/** /**
* @brief Window flag * @brief Window flag
* *
@ -763,7 +759,7 @@ class EmscriptenApplication::Configuration {
* is @cpp size*dpiScaling @ce and @ref EmscriptenApplication::dpiScaling() * is @cpp size*dpiScaling @ce and @ref EmscriptenApplication::dpiScaling()
* is @p dpiScaling. * is @p dpiScaling.
*/ */
Configuration& setSize(const Vector2i& size, const Vector2& dpiScaling={}) { Configuration& setSize(const Vector2i& size, const Vector2& dpiScaling = {}) {
_size = size; _size = size;
_dpiScaling = dpiScaling; _dpiScaling = dpiScaling;
return *this; return *this;
@ -967,8 +963,6 @@ class EmscriptenApplication::InputEvent {
@see @ref MouseMoveEvent, @ref mousePressEvent(), @ref mouseReleaseEvent() @see @ref MouseMoveEvent, @ref mousePressEvent(), @ref mouseReleaseEvent()
*/ */
class EmscriptenApplication::MouseEvent: public EmscriptenApplication::InputEvent { class EmscriptenApplication::MouseEvent: public EmscriptenApplication::InputEvent {
friend EmscriptenApplication;
public: public:
/** /**
* @brief Mouse button * @brief Mouse button
@ -996,9 +990,11 @@ class EmscriptenApplication::MouseEvent: public EmscriptenApplication::InputEven
Modifiers modifiers() const; Modifiers modifiers() const;
private: private:
friend EmscriptenApplication;
explicit MouseEvent(const EmscriptenMouseEvent* event): _event(event) {} explicit MouseEvent(const EmscriptenMouseEvent* event): _event(event) {}
const EmscriptenMouseEvent* _event; const EmscriptenMouseEvent* const _event;
}; };
/** /**
@ -1007,8 +1003,6 @@ class EmscriptenApplication::MouseEvent: public EmscriptenApplication::InputEven
@see @ref MouseEvent, @ref mouseMoveEvent() @see @ref MouseEvent, @ref mouseMoveEvent()
*/ */
class EmscriptenApplication::MouseMoveEvent: public EmscriptenApplication::InputEvent { class EmscriptenApplication::MouseMoveEvent: public EmscriptenApplication::InputEvent {
friend EmscriptenApplication;
public: public:
/** /**
* @brief Mouse button * @brief Mouse button
@ -1043,9 +1037,11 @@ class EmscriptenApplication::MouseMoveEvent: public EmscriptenApplication::Input
Modifiers modifiers() const; Modifiers modifiers() const;
private: private:
explicit MouseMoveEvent(const EmscriptenMouseEvent* event): _event(event) {} friend EmscriptenApplication;
explicit MouseMoveEvent(const EmscriptenMouseEvent* event): _event{event} {}
const EmscriptenMouseEvent* _event; const EmscriptenMouseEvent* const _event;
}; };
/** /**
@ -1054,8 +1050,6 @@ class EmscriptenApplication::MouseMoveEvent: public EmscriptenApplication::Input
@see @ref MouseEvent, @ref MouseMoveEvent, @ref mouseScrollEvent() @see @ref MouseEvent, @ref MouseMoveEvent, @ref mouseScrollEvent()
*/ */
class EmscriptenApplication::MouseScrollEvent: public EmscriptenApplication::InputEvent { class EmscriptenApplication::MouseScrollEvent: public EmscriptenApplication::InputEvent {
friend EmscriptenApplication;
public: public:
/** /**
@ -1072,10 +1066,11 @@ class EmscriptenApplication::MouseScrollEvent: public EmscriptenApplication::Inp
Modifiers modifiers() const; Modifiers modifiers() const;
private: private:
explicit MouseScrollEvent(const EmscriptenWheelEvent* event): friend EmscriptenApplication;
InputEvent{}, _event{event} {}
explicit MouseScrollEvent(const EmscriptenWheelEvent* event): _event{event} {}
const EmscriptenWheelEvent* _event; const EmscriptenWheelEvent* const _event;
}; };
/** /**
@ -1084,10 +1079,7 @@ class EmscriptenApplication::MouseScrollEvent: public EmscriptenApplication::Inp
@see @ref keyPressEvent(), @ref keyReleaseEvent() @see @ref keyPressEvent(), @ref keyReleaseEvent()
*/ */
class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent { class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent {
friend EmscriptenApplication;
public: public:
/** /**
* @brief Key * @brief Key
* *
@ -1277,10 +1269,11 @@ class EmscriptenApplication::KeyEvent: public EmscriptenApplication::InputEvent
Modifiers modifiers() const; Modifiers modifiers() const;
private: private:
explicit KeyEvent(const EmscriptenKeyboardEvent* event): _event(event) {} friend EmscriptenApplication;
const EmscriptenKeyboardEvent* _event; explicit KeyEvent(const EmscriptenKeyboardEvent* event): _event{event} {}
const EmscriptenKeyboardEvent* const _event;
}; };
CORRADE_ENUMSET_OPERATORS(EmscriptenApplication::MouseMoveEvent::Buttons) CORRADE_ENUMSET_OPERATORS(EmscriptenApplication::MouseMoveEvent::Buttons)
@ -1291,8 +1284,6 @@ CORRADE_ENUMSET_OPERATORS(EmscriptenApplication::MouseMoveEvent::Buttons)
@see @ref textInputEvent() @see @ref textInputEvent()
*/ */
class EmscriptenApplication::TextInputEvent { class EmscriptenApplication::TextInputEvent {
friend EmscriptenApplication;
public: public:
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
TextInputEvent(const TextInputEvent&) = delete; TextInputEvent(const TextInputEvent&) = delete;
@ -1323,6 +1314,8 @@ class EmscriptenApplication::TextInputEvent {
Containers::ArrayView<const char> text() const { return _text; } Containers::ArrayView<const char> text() const { return _text; }
private: private:
friend EmscriptenApplication;
explicit TextInputEvent(Containers::ArrayView<const char> text): _text{text}, _accepted{false} {} explicit TextInputEvent(Containers::ArrayView<const char> text): _text{text}, _accepted{false} {}
const Containers::ArrayView<const char> _text; const Containers::ArrayView<const char> _text;

Loading…
Cancel
Save