From d0aad3ee1e68c91b55e9d0711a82844e676f9f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Sun, 28 Nov 2021 20:14:15 +0100 Subject: [PATCH] Platform: Add initial Clipboard support (only text is supported) --- include/Nazara/Platform.hpp | 1 + include/Nazara/Platform/Clipboard.hpp | 31 +++++++++++++++ include/Nazara/Platform/Clipboard.inl | 12 ++++++ src/Nazara/Platform/Clipboard.cpp | 25 ++++++++++++ src/Nazara/Platform/Keyboard.cpp | 16 ++++---- src/Nazara/Platform/Mouse.cpp | 16 ++++---- src/Nazara/Platform/SDL2/InputImpl.cpp | 54 +++++++++++++++++++------- src/Nazara/Platform/SDL2/InputImpl.hpp | 6 ++- 8 files changed, 130 insertions(+), 31 deletions(-) create mode 100644 include/Nazara/Platform/Clipboard.hpp create mode 100644 include/Nazara/Platform/Clipboard.inl create mode 100644 src/Nazara/Platform/Clipboard.cpp diff --git a/include/Nazara/Platform.hpp b/include/Nazara/Platform.hpp index 392e475a7..d7f1da654 100644 --- a/include/Nazara/Platform.hpp +++ b/include/Nazara/Platform.hpp @@ -29,6 +29,7 @@ #ifndef NAZARA_GLOBAL_PLATFORM_HPP #define NAZARA_GLOBAL_PLATFORM_HPP +#include #include #include #include diff --git a/include/Nazara/Platform/Clipboard.hpp b/include/Nazara/Platform/Clipboard.hpp new file mode 100644 index 000000000..6160503ff --- /dev/null +++ b/include/Nazara/Platform/Clipboard.hpp @@ -0,0 +1,31 @@ +// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Platform module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_PLATFORM_CLIPBOARD_HPP +#define NAZARA_PLATFORM_CLIPBOARD_HPP + +#include +#include +#include +#include + +namespace Nz +{ + class NAZARA_PLATFORM_API Clipboard + { + public: + Clipboard() = delete; + ~Clipboard() = delete; + + static ClipboardContentType GetContentType(); + static std::string GetString(); + static void SetString(const std::string& str); + }; +} + +#include + +#endif // NAZARA_PLATFORM_CLIPBOARD_HPP diff --git a/include/Nazara/Platform/Clipboard.inl b/include/Nazara/Platform/Clipboard.inl new file mode 100644 index 000000000..6b3f1c283 --- /dev/null +++ b/include/Nazara/Platform/Clipboard.inl @@ -0,0 +1,12 @@ +// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Platform module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +namespace Nz +{ +} + +#include diff --git a/src/Nazara/Platform/Clipboard.cpp b/src/Nazara/Platform/Clipboard.cpp new file mode 100644 index 000000000..afa8e999f --- /dev/null +++ b/src/Nazara/Platform/Clipboard.cpp @@ -0,0 +1,25 @@ +// Copyright (C) 2021 Jérôme "Lynix" Leclercq (lynix680@gmail.com) +// This file is part of the "Nazara Engine - Platform module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +namespace Nz +{ + ClipboardContentType Clipboard::GetContentType() + { + return InputImpl::GetClipboardContentType(); + } + + std::string Clipboard::GetString() + { + return InputImpl::GetClipboardString(); + } + + void Clipboard::SetString(const std::string& str) + { + return InputImpl::SetClipboardString(str); + } +} diff --git a/src/Nazara/Platform/Keyboard.cpp b/src/Nazara/Platform/Keyboard.cpp index e2f12db51..1c6cb1e13 100644 --- a/src/Nazara/Platform/Keyboard.cpp +++ b/src/Nazara/Platform/Keyboard.cpp @@ -10,41 +10,41 @@ namespace Nz { std::string Keyboard::GetKeyName(Scancode scancode) { - return EventImpl::GetKeyName(scancode); + return InputImpl::GetKeyName(scancode); } std::string Keyboard::GetKeyName(VKey key) { - return EventImpl::GetKeyName(key); + return InputImpl::GetKeyName(key); } bool Keyboard::IsKeyPressed(Scancode scancode) { - return EventImpl::IsKeyPressed(scancode); + return InputImpl::IsKeyPressed(scancode); } bool Keyboard::IsKeyPressed(VKey key) { - return EventImpl::IsKeyPressed(key); + return InputImpl::IsKeyPressed(key); } void Keyboard::StartTextInput() { - EventImpl::StartTextInput(); + InputImpl::StartTextInput(); } void Keyboard::StopTextInput() { - EventImpl::StopTextInput(); + InputImpl::StopTextInput(); } Keyboard::Scancode Keyboard::ToScanCode(VKey key) { - return EventImpl::ToScanCode(key); + return InputImpl::ToScanCode(key); } Keyboard::VKey Keyboard::ToVirtualKey(Scancode scancode) { - return EventImpl::ToVirtualKey(scancode); + return InputImpl::ToVirtualKey(scancode); } } diff --git a/src/Nazara/Platform/Mouse.cpp b/src/Nazara/Platform/Mouse.cpp index 70242df3c..4983d4a93 100644 --- a/src/Nazara/Platform/Mouse.cpp +++ b/src/Nazara/Platform/Mouse.cpp @@ -11,27 +11,27 @@ namespace Nz { Vector2i Mouse::GetPosition() { - return EventImpl::GetMousePosition(); + return InputImpl::GetMousePosition(); } Vector2i Mouse::GetPosition(const Window& relativeTo) { - return EventImpl::GetMousePosition(relativeTo); + return InputImpl::GetMousePosition(relativeTo); } bool Mouse::IsButtonPressed(Button button) { - return EventImpl::IsMouseButtonPressed(button); + return InputImpl::IsMouseButtonPressed(button); } bool Mouse::SetRelativeMouseMode(bool relativeMouseMode) { - return EventImpl::SetRelativeMouseMode(relativeMouseMode); + return InputImpl::SetRelativeMouseMode(relativeMouseMode); } void Mouse::SetPosition(const Vector2i& position) { - EventImpl::SetMousePosition(position.x, position.y); + InputImpl::SetMousePosition(position.x, position.y); } void Mouse::SetPosition(const Vector2i& position, const Window& relativeTo, bool ignoreEvent) @@ -39,12 +39,12 @@ namespace Nz if (ignoreEvent && position.x > 0 && position.y > 0) relativeTo.IgnoreNextMouseEvent(position.x, position.y); - EventImpl::SetMousePosition(position.x, position.y, relativeTo); + InputImpl::SetMousePosition(position.x, position.y, relativeTo); } void Mouse::SetPosition(int x, int y) { - EventImpl::SetMousePosition(x, y); + InputImpl::SetMousePosition(x, y); } void Mouse::SetPosition(int x, int y, const Window& relativeTo, bool ignoreEvent) @@ -52,6 +52,6 @@ namespace Nz if (ignoreEvent && x > 0 && y > 0) relativeTo.IgnoreNextMouseEvent(x, y); - EventImpl::SetMousePosition(x, y, relativeTo); + InputImpl::SetMousePosition(x, y, relativeTo); } } diff --git a/src/Nazara/Platform/SDL2/InputImpl.cpp b/src/Nazara/Platform/SDL2/InputImpl.cpp index 203046664..32a1b4441 100644 --- a/src/Nazara/Platform/SDL2/InputImpl.cpp +++ b/src/Nazara/Platform/SDL2/InputImpl.cpp @@ -3,10 +3,12 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include #include +#include #include #include #include @@ -14,7 +16,26 @@ namespace Nz { - std::string EventImpl::GetKeyName(Keyboard::Scancode key) + ClipboardContentType InputImpl::GetClipboardContentType() + { + if (SDL_HasClipboardText()) + return ClipboardContentType::Text; + + return ClipboardContentType::Unknown; + } + + std::string InputImpl::GetClipboardString() + { + char* str = SDL_GetClipboardText(); + if (!str) + return {}; + + CallOnExit freeStr([=] { SDL_free(str); }); + + return std::string(str); + } + + std::string InputImpl::GetKeyName(Keyboard::Scancode key) { SDL_Scancode scancode = SDLHelper::ToSDL(key); @@ -27,7 +48,7 @@ namespace Nz return name; } - std::string EventImpl::GetKeyName(Keyboard::VKey key) + std::string InputImpl::GetKeyName(Keyboard::VKey key) { SDL_Keycode vkey = SDLHelper::ToSDL(key); @@ -40,7 +61,7 @@ namespace Nz return name; } - Vector2i EventImpl::GetMousePosition() + Vector2i InputImpl::GetMousePosition() { Vector2i pos; SDL_GetGlobalMouseState(&pos.x, &pos.y); @@ -48,7 +69,7 @@ namespace Nz return pos; } - Vector2i EventImpl::GetMousePosition(const Window& relativeTo) + Vector2i InputImpl::GetMousePosition(const Window& relativeTo) { auto windowPos = relativeTo.GetPosition(); auto mousePos = GetMousePosition(); @@ -56,17 +77,17 @@ namespace Nz return mousePos - windowPos; } - bool EventImpl::IsKeyPressed(Keyboard::Scancode key) + bool InputImpl::IsKeyPressed(Keyboard::Scancode key) { return SDL_GetKeyboardState(nullptr)[SDLHelper::ToSDL(key)]; } - bool EventImpl::IsKeyPressed(Keyboard::VKey key) + bool InputImpl::IsKeyPressed(Keyboard::VKey key) { return IsKeyPressed(ToScanCode(key)); } - bool EventImpl::IsMouseButtonPressed(Mouse::Button button) + bool InputImpl::IsMouseButtonPressed(Mouse::Button button) { static int vButtons[Mouse::Max + 1] = { SDL_BUTTON_LMASK, // Button::Left @@ -79,18 +100,23 @@ namespace Nz return (SDL_GetGlobalMouseState(nullptr, nullptr) & vButtons[button]) != 0; } - bool EventImpl::SetRelativeMouseMode(bool relativeMouseMode) + void InputImpl::SetClipboardString(const std::string& str) + { + SDL_SetClipboardText(str.c_str()); + } + + bool InputImpl::SetRelativeMouseMode(bool relativeMouseMode) { return SDL_SetRelativeMouseMode((relativeMouseMode) ? SDL_TRUE : SDL_FALSE) == 0; } - void EventImpl::SetMousePosition(int x, int y) + void InputImpl::SetMousePosition(int x, int y) { if (SDL_WarpMouseGlobal(x, y) != 0) NazaraWarning(SDL_GetError()); } - void EventImpl::SetMousePosition(int x, int y, const Window& relativeTo) + void InputImpl::SetMousePosition(int x, int y, const Window& relativeTo) { SDL_Window* handle = static_cast(relativeTo.GetImpl())->GetHandle(); if (handle) @@ -99,22 +125,22 @@ namespace Nz NazaraError("Invalid window handle"); } - void EventImpl::StartTextInput() + void InputImpl::StartTextInput() { SDL_StartTextInput(); } - void EventImpl::StopTextInput() + void InputImpl::StopTextInput() { SDL_StopTextInput(); } - Keyboard::Scancode EventImpl::ToScanCode(Keyboard::VKey key) + Keyboard::Scancode InputImpl::ToScanCode(Keyboard::VKey key) { return SDLHelper::FromSDL(SDL_GetScancodeFromKey(SDLHelper::ToSDL(key))); } - Keyboard::VKey EventImpl::ToVirtualKey(Keyboard::Scancode scancode) + Keyboard::VKey InputImpl::ToVirtualKey(Keyboard::Scancode scancode) { return SDLHelper::FromSDL(SDL_GetKeyFromScancode(SDLHelper::ToSDL(scancode))); } diff --git a/src/Nazara/Platform/SDL2/InputImpl.hpp b/src/Nazara/Platform/SDL2/InputImpl.hpp index a4381a867..101c525f9 100644 --- a/src/Nazara/Platform/SDL2/InputImpl.hpp +++ b/src/Nazara/Platform/SDL2/InputImpl.hpp @@ -8,15 +8,18 @@ #define NAZARA_PLATFORM_SDL2_INPUTIMPL_HPP #include +#include #include #include #include namespace Nz { - class EventImpl + class InputImpl { public: + static ClipboardContentType GetClipboardContentType(); + static std::string GetClipboardString(); static std::string GetKeyName(Keyboard::Scancode scancode); static std::string GetKeyName(Keyboard::VKey key); static Vector2i GetMousePosition(); @@ -24,6 +27,7 @@ namespace Nz static bool IsKeyPressed(Keyboard::Scancode key); static bool IsKeyPressed(Keyboard::VKey key); static bool IsMouseButtonPressed(Mouse::Button button); + static void SetClipboardString(const std::string& str); static bool SetRelativeMouseMode(bool relativeMouseMode); static void SetMousePosition(int x, int y); static void SetMousePosition(int x, int y, const Window& relativeTo);