Platform: Add initial Clipboard support (only text is supported)

This commit is contained in:
Jérôme Leclercq 2021-11-28 20:14:15 +01:00
parent 2cdd7d0b44
commit d0aad3ee1e
8 changed files with 130 additions and 31 deletions

View File

@ -29,6 +29,7 @@
#ifndef NAZARA_GLOBAL_PLATFORM_HPP
#define NAZARA_GLOBAL_PLATFORM_HPP
#include <Nazara/Platform/Clipboard.hpp>
#include <Nazara/Platform/Config.hpp>
#include <Nazara/Platform/Cursor.hpp>
#include <Nazara/Platform/CursorController.hpp>

View File

@ -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 <Nazara/Prerequisites.hpp>
#include <Nazara/Platform/Config.hpp>
#include <Nazara/Platform/Enums.hpp>
#include <string>
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 <Nazara/Platform/Clipboard.inl>
#endif // NAZARA_PLATFORM_CLIPBOARD_HPP

View File

@ -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 <Nazara/Platform/Clipboard.hpp>
#include <Nazara/Platform/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Platform/DebugOff.hpp>

View File

@ -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 <Nazara/Platform/Clipboard.hpp>
#include <Nazara/Platform/SDL2/InputImpl.hpp>
#include <Nazara/Platform/Debug.hpp>
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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -3,10 +3,12 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Platform/SDL2/InputImpl.hpp>
#include <Nazara/Core/CallOnExit.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Platform/Window.hpp>
#include <Nazara/Platform/SDL2/SDLHelper.hpp>
#include <Nazara/Platform/SDL2/WindowImpl.hpp>
#include <SDL2/SDL_clipboard.h>
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h>
#include <SDL2/SDL_mouse.h>
@ -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<const WindowImpl*>(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)));
}

View File

@ -8,15 +8,18 @@
#define NAZARA_PLATFORM_SDL2_INPUTIMPL_HPP
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Platform/Enums.hpp>
#include <Nazara/Platform/Keyboard.hpp>
#include <Nazara/Platform/Mouse.hpp>
#include <string>
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);