Browse Source

Platform: use StringView in *Application::Text*Event.

Which allows to get rid of a now-unneeded ArrayView include in the
header. Also, now that we're returning a StringView, it's useful to
have the view always null-terminated. In SDL2 and Emscripten it was
already like that, GLFW needed a minor change.
ktx1-detection
Vladimír Vondruš 4 years ago
parent
commit
70d286774f
  1. 8
      src/Magnum/Platform/EmscriptenApplication.cpp
  2. 14
      src/Magnum/Platform/EmscriptenApplication.h
  3. 7
      src/Magnum/Platform/GlfwApplication.cpp
  4. 14
      src/Magnum/Platform/GlfwApplication.h
  5. 5
      src/Magnum/Platform/Sdl2Application.cpp
  6. 27
      src/Magnum/Platform/Sdl2Application.h
  7. 3
      src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp
  8. 3
      src/Magnum/Platform/Test/GlfwApplicationTest.cpp
  9. 3
      src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp

8
src/Magnum/Platform/EmscriptenApplication.cpp

@ -631,11 +631,11 @@ void EmscriptenApplication::setupCallbacks(bool resizable) {
emscripten_set_keydown_callback(keyboardListeningElement, this, false, emscripten_set_keydown_callback(keyboardListeningElement, this, false,
([](int, const EmscriptenKeyboardEvent* event, void* userData) -> Int { ([](int, const EmscriptenKeyboardEvent* event, void* userData) -> Int {
EmscriptenApplication& app = *static_cast<EmscriptenApplication*>(userData); EmscriptenApplication& app = *static_cast<EmscriptenApplication*>(userData);
const std::size_t keyLen = std::strlen(event->key); const Containers::StringView key = event->key;
/* If the key name is a single letter or a start of an UTF-8 /* If the key name is a single letter or a start of an UTF-8
sequence, pass it to the text input even tas well */ sequence, pass it to the text input event as well */
if(app.isTextInputActive() && (std::strlen(event->key) == 1 || (std::strlen(event->key) >= 1 && UnsignedByte(event->key[0]) > 127))) { if(app.isTextInputActive() && key.size() == 1 || (key.size() >= 1 && UnsignedByte(key[0]) > 127)) {
TextInputEvent e{*event, {event->key, keyLen}}; TextInputEvent e{*event, key};
app.textInputEvent(e); app.textInputEvent(e);
return e.isAccepted(); return e.isAccepted();
} }

14
src/Magnum/Platform/EmscriptenApplication.h

@ -34,7 +34,6 @@
*/ */
#endif #endif
#include <Corrade/Containers/ArrayView.h>
/* Needed by the MAGNUM_EMSCRIPTENAPPLICATION_MAIN() macro */ /* Needed by the MAGNUM_EMSCRIPTENAPPLICATION_MAIN() macro */
/** @todo use an Optional */ /** @todo use an Optional */
#include <Corrade/Containers/Pointer.h> #include <Corrade/Containers/Pointer.h>
@ -1897,8 +1896,13 @@ class EmscriptenApplication::TextInputEvent {
/** @copydoc EmscriptenApplication::InputEvent::setAccepted() */ /** @copydoc EmscriptenApplication::InputEvent::setAccepted() */
void setAccepted(bool accepted = true) { _accepted = accepted; } void setAccepted(bool accepted = true) { _accepted = accepted; }
/** @brief Input text in UTF-8 */ /**
Containers::ArrayView<const char> text() const { return _text; } * @brief Input text
*
* The returned view is in UTF-8 and is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}.
*/
Containers::StringView text() const { return _text; }
/** @brief Underlying Emscripten event */ /** @brief Underlying Emscripten event */
const EmscriptenKeyboardEvent& event() const { return _event; } const EmscriptenKeyboardEvent& event() const { return _event; }
@ -1906,10 +1910,10 @@ class EmscriptenApplication::TextInputEvent {
private: private:
friend EmscriptenApplication; friend EmscriptenApplication;
explicit TextInputEvent(const EmscriptenKeyboardEvent& event, Containers::ArrayView<const char> text): _event(event), _text{text}, _accepted{false} {} explicit TextInputEvent(const EmscriptenKeyboardEvent& event, Containers::StringView text): _event(event), _text{text}, _accepted{false} {}
const EmscriptenKeyboardEvent& _event; const EmscriptenKeyboardEvent& _event;
const Containers::ArrayView<const char> _text; const Containers::StringView _text;
bool _accepted; bool _accepted;
}; };

7
src/Magnum/Platform/GlfwApplication.cpp

@ -654,9 +654,10 @@ void GlfwApplication::setupCallbacks() {
if(!(app._flags & Flag::TextInputActive)) return; if(!(app._flags & Flag::TextInputActive)) return;
char utf8[4]{}; /* One extra byte to ensure it gets always null-terminated */
const std::size_t size = Utility::Unicode::utf8(codepoint, utf8); char utf8[4 + 1]{};
TextInputEvent e{{utf8, size}}; const std::size_t size = Utility::Unicode::utf8(codepoint, Containers::staticArrayView(utf8).prefix<4>());
TextInputEvent e{{utf8, size, Containers::StringViewFlag::NullTerminated}};
app.textInputEvent(e); app.textInputEvent(e);
}); });
} }

14
src/Magnum/Platform/GlfwApplication.h

@ -32,7 +32,6 @@
* @brief Class @ref Magnum::Platform::GlfwApplication, macro @ref MAGNUM_GLFWAPPLICATION_MAIN() * @brief Class @ref Magnum::Platform::GlfwApplication, macro @ref MAGNUM_GLFWAPPLICATION_MAIN()
*/ */
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h> /** @todo PIMPL Configuration instead? */ #include <Corrade/Containers/String.h> /** @todo PIMPL Configuration instead? */
@ -2050,15 +2049,20 @@ class GlfwApplication::TextInputEvent {
*/ */
void setAccepted(bool accepted = true) { _accepted = accepted; } void setAccepted(bool accepted = true) { _accepted = accepted; }
/** @brief Input text in UTF-8 */ /**
Containers::ArrayView<const char> text() const { return _text; } * @brief Input text
*
* The returned view is in UTF-8 and is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}.
*/
Containers::StringView text() const { return _text; }
private: private:
friend GlfwApplication; friend GlfwApplication;
explicit TextInputEvent(Containers::ArrayView<const char> text): _text{text}, _accepted{false} {} explicit TextInputEvent(Containers::StringView text): _text{text}, _accepted{false} {}
const Containers::ArrayView<const char> _text; const Containers::StringView _text;
bool _accepted; bool _accepted;
}; };

5
src/Magnum/Platform/Sdl2Application.cpp

@ -27,7 +27,6 @@
#include "Sdl2Application.h" #include "Sdl2Application.h"
#include <cstring> /** @todo remove, needed for std::strlen() in TextInputEvent */
#ifdef CORRADE_TARGET_CLANG_CL #ifdef CORRADE_TARGET_CLANG_CL
/* SDL does #pragma pack(push,8) and #pragma pack(pop,8) in different headers /* SDL does #pragma pack(push,8) and #pragma pack(pop,8) in different headers
(begin_code.h and end_code.h) and clang-cl doesn't like that, even though it (begin_code.h and end_code.h) and clang-cl doesn't like that, even though it
@ -963,12 +962,12 @@ bool Sdl2Application::mainLoopIteration() {
} }
case SDL_TEXTINPUT: { case SDL_TEXTINPUT: {
TextInputEvent e{event, {event.text.text, std::strlen(event.text.text)}}; TextInputEvent e{event, event.text.text};
textInputEvent(e); textInputEvent(e);
} break; } break;
case SDL_TEXTEDITING: { case SDL_TEXTEDITING: {
TextEditingEvent e{event, {event.edit.text, std::strlen(event.text.text)}, event.edit.start, event.edit.length}; TextEditingEvent e{event, event.edit.text, event.edit.start, event.edit.length};
textEditingEvent(e); textEditingEvent(e);
} break; } break;

27
src/Magnum/Platform/Sdl2Application.h

@ -31,7 +31,6 @@
* @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN() * @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN()
*/ */
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/EnumSet.h> #include <Corrade/Containers/EnumSet.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h> /** @todo PIMPL Configuration instead? */ #include <Corrade/Containers/String.h> /** @todo PIMPL Configuration instead? */
@ -2798,8 +2797,13 @@ class Sdl2Application::TextInputEvent {
*/ */
void setAccepted(bool accepted = true) { _accepted = accepted; } void setAccepted(bool accepted = true) { _accepted = accepted; }
/** @brief Input text in UTF-8 */ /**
Containers::ArrayView<const char> text() const { return _text; } * @brief Input text
*
* The returned view is in UTF-8 and is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}.
*/
Containers::StringView text() const { return _text; }
/** /**
* @brief Underlying SDL event * @brief Underlying SDL event
@ -2812,10 +2816,10 @@ class Sdl2Application::TextInputEvent {
private: private:
friend Sdl2Application; friend Sdl2Application;
explicit TextInputEvent(const SDL_Event& event, Containers::ArrayView<const char> text): _event(event), _text{text}, _accepted{false} {} explicit TextInputEvent(const SDL_Event& event, Containers::StringView text): _event(event), _text{text}, _accepted{false} {}
const SDL_Event& _event; const SDL_Event& _event;
const Containers::ArrayView<const char> _text; const Containers::StringView _text;
bool _accepted; bool _accepted;
}; };
@ -2851,8 +2855,13 @@ class Sdl2Application::TextEditingEvent {
*/ */
void setAccepted(bool accepted = true) { _accepted = accepted; } void setAccepted(bool accepted = true) { _accepted = accepted; }
/** @brief Input text in UTF-8 */ /**
Containers::ArrayView<const char> text() const { return _text; } * @brief Input text
*
* The returned view is in UTF-8 and is always
* @relativeref{Corrade,Containers::StringViewFlag::NullTerminated}.
*/
Containers::StringView text() const { return _text; }
/** @brief Location to begin editing from */ /** @brief Location to begin editing from */
Int start() const { return _start; } Int start() const { return _start; }
@ -2871,10 +2880,10 @@ class Sdl2Application::TextEditingEvent {
private: private:
friend Sdl2Application; friend Sdl2Application;
explicit TextEditingEvent(const SDL_Event& event, Containers::ArrayView<const char> text, Int start, Int length): _event(event), _text{text}, _start{start}, _length{length}, _accepted{false} {} explicit TextEditingEvent(const SDL_Event& event, Containers::StringView text, Int start, Int length): _event(event), _text{text}, _start{start}, _length{length}, _accepted{false} {}
const SDL_Event& _event; const SDL_Event& _event;
const Containers::ArrayView<const char> _text; const Containers::StringView _text;
const Int _start; const Int _start;
const Int _length; const Int _length;
bool _accepted; bool _accepted;

3
src/Magnum/Platform/Test/EmscriptenApplicationTest.cpp

@ -26,7 +26,6 @@
*/ */
#include <Corrade/Utility/Arguments.h> #include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/DebugStl.h>
#include "Magnum/Platform/EmscriptenApplication.h" #include "Magnum/Platform/EmscriptenApplication.h"
#include "Magnum/GL/Renderer.h" #include "Magnum/GL/Renderer.h"
@ -140,7 +139,7 @@ struct EmscriptenApplicationTest: Platform::Application {
} }
void textInputEvent(TextInputEvent& event) override { void textInputEvent(TextInputEvent& event) override {
Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; Debug{} << "text input event:" << event.text();
event.setAccepted(); event.setAccepted();
} }

3
src/Magnum/Platform/Test/GlfwApplicationTest.cpp

@ -26,7 +26,6 @@
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/PluginManager/Manager.h> #include <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Arguments.h> #include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/Resource.h> #include <Corrade/Utility/Resource.h>
#include "Magnum/ImageView.h" #include "Magnum/ImageView.h"
@ -102,7 +101,7 @@ struct GlfwApplicationTest: Platform::Application {
} }
void textInputEvent(TextInputEvent& event) override { void textInputEvent(TextInputEvent& event) override {
Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; Debug{} << "text input event:" << event.text();
} }
}; };

3
src/Magnum/Platform/Test/Sdl2ApplicationTest.cpp

@ -26,7 +26,6 @@
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/PluginManager/Manager.h> #include <Corrade/PluginManager/Manager.h>
#include <Corrade/Utility/Arguments.h> #include <Corrade/Utility/Arguments.h>
#include <Corrade/Utility/DebugStl.h>
#include <Corrade/Utility/Resource.h> #include <Corrade/Utility/Resource.h>
#include "Magnum/ImageView.h" #include "Magnum/ImageView.h"
@ -142,7 +141,7 @@ struct Sdl2ApplicationTest: Platform::Application {
} }
void textInputEvent(TextInputEvent& event) override { void textInputEvent(TextInputEvent& event) override {
Debug{} << "text input event:" << std::string{event.text(), event.text().size()}; Debug{} << "text input event:" << event.text();
} }
/* Should fire on currently not handled events, such as minimize/maximize /* Should fire on currently not handled events, such as minimize/maximize

Loading…
Cancel
Save