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/html5.h>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/Debug.h>
@ -98,11 +97,11 @@ namespace {
/* Predicate for Entry "less than" to use with std::lower_bound */
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;
}
bool operator()(const char*& a, const Entry& b) {
bool operator()(const char* const a, const Entry& b) {
return std::strcmp(a, b.key) < 0;
}
};
@ -115,8 +114,8 @@ namespace {
@param code Keyboard layout independent key string, e.g. 'KeyA' or 'Minus'.
Note that the y key on some layouts may result in 'KeyZ'.
*/
Key toKey(const EM_UTF8* key, const EM_UTF8* code) {
const size_t keyLength = std::strlen(key);
Key toKey(const EM_UTF8* const key, const EM_UTF8* const code) {
const std::size_t keyLength = std::strlen(key);
if(keyLength == 0) return Key::Unknown;
/* 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.
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")) {
return Key(code[5]);
@ -146,20 +145,15 @@ namespace {
/* Numpad0 - Numpad9 */
const Int num = numKey[0] - '0';
if(num >= 0 && num <= 9) {
return Key(num + Int(Key::NumZero));
}
if(num >= 0 && num <= 9) return Key(num + Int(Key::NumZero));
return Key::Unknown;
}
const auto mapping = Containers::arrayView(KeyMapping,
Containers::arraySize(KeyMapping));
const Entry* found =
std::lower_bound(mapping.begin(), mapping.end(), code, EntryCompare{});
if(found != mapping.end() && std::strcmp(found->key, code) == 0) {
const Containers::ArrayView<const Entry> mapping = KeyMapping;
const Entry* found = std::lower_bound(mapping.begin(), mapping.end(), code, EntryCompare{});
if(found != mapping.end() && std::strcmp(found->key, code) == 0)
return found->value;
}
/* F1 - F12 */
if(code[0] == 'F') {
@ -175,7 +169,6 @@ namespace {
return Key::Unknown;
}
}
#ifdef MAGNUM_TARGET_GL
@ -573,7 +566,7 @@ Vector2 EmscriptenApplication::MouseScrollEvent::offset() const {
DOM_DELTA_PAGE => 1 page = 80 steps
*/
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)};
}
@ -597,9 +590,8 @@ Key EmscriptenApplication::KeyEvent::key() const {
std::string EmscriptenApplication::KeyEvent::keyName() const {
if((_event->key[0] >= 'a' && _event->key[0] <= 'z') ||
(_event->key[0] >= 'A' && _event->key[0] <= 'Z')) {
return _event->key;
}
(_event->key[0] >= 'A' && _event->key[0] <= 'Z')) return _event->key;
return _event->code;
}

43
src/Magnum/Platform/EmscriptenApplication.h

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

Loading…
Cancel
Save