mirror of
https://github.com/vcmi/vcmi.git
synced 2024-12-10 09:50:17 +02:00
93 lines
1.9 KiB
C++
93 lines
1.9 KiB
C++
|
/*
|
||
|
* InputSourceText.cpp, part of VCMI engine
|
||
|
*
|
||
|
* Authors: listed in file AUTHORS in main folder
|
||
|
*
|
||
|
* License: GNU General Public License v2.0 or later
|
||
|
* Full text of license available in license.txt file, in main folder
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
#include "StdInc.h"
|
||
|
#include "InputSourceText.h"
|
||
|
|
||
|
#include "../CMT.h"
|
||
|
#include "../gui/CGuiHandler.h"
|
||
|
#include "../gui/EventDispatcher.h"
|
||
|
|
||
|
#include "../../lib/Rect.h"
|
||
|
|
||
|
#include <SDL_events.h>
|
||
|
#include <SDL_render.h>
|
||
|
|
||
|
#ifdef VCMI_APPLE
|
||
|
# include <dispatch/dispatch.h>
|
||
|
#endif
|
||
|
|
||
|
#ifdef VCMI_IOS
|
||
|
# include "ios/utils.h"
|
||
|
#endif
|
||
|
|
||
|
void InputSourceText::handleEventTextInput(const SDL_TextInputEvent & text)
|
||
|
{
|
||
|
GH.events().dispatchTextInput(text.text);
|
||
|
}
|
||
|
|
||
|
void InputSourceText::handleEventTextEditing(const SDL_TextEditingEvent & text)
|
||
|
{
|
||
|
GH.events().dispatchTextEditing(text.text);
|
||
|
}
|
||
|
|
||
|
void InputSourceText::startTextInput(const Rect & whereInput)
|
||
|
{
|
||
|
#ifdef VCMI_APPLE
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
#endif
|
||
|
|
||
|
// TODO ios: looks like SDL bug actually, try fixing there
|
||
|
auto renderer = SDL_GetRenderer(mainWindow);
|
||
|
float scaleX, scaleY;
|
||
|
SDL_Rect viewport;
|
||
|
SDL_RenderGetScale(renderer, &scaleX, &scaleY);
|
||
|
SDL_RenderGetViewport(renderer, &viewport);
|
||
|
|
||
|
#ifdef VCMI_IOS
|
||
|
const auto nativeScale = iOS_utils::screenScale();
|
||
|
scaleX /= nativeScale;
|
||
|
scaleY /= nativeScale;
|
||
|
#endif
|
||
|
|
||
|
SDL_Rect rectInScreenCoordinates;
|
||
|
rectInScreenCoordinates.x = (viewport.x + whereInput.x) * scaleX;
|
||
|
rectInScreenCoordinates.y = (viewport.y + whereInput.y) * scaleY;
|
||
|
rectInScreenCoordinates.w = whereInput.w * scaleX;
|
||
|
rectInScreenCoordinates.h = whereInput.h * scaleY;
|
||
|
|
||
|
SDL_SetTextInputRect(&rectInScreenCoordinates);
|
||
|
|
||
|
if (SDL_IsTextInputActive() == SDL_FALSE)
|
||
|
{
|
||
|
SDL_StartTextInput();
|
||
|
}
|
||
|
|
||
|
#ifdef VCMI_APPLE
|
||
|
});
|
||
|
#endif
|
||
|
}
|
||
|
|
||
|
void InputSourceText::stopTextInput()
|
||
|
{
|
||
|
#ifdef VCMI_APPLE
|
||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||
|
#endif
|
||
|
|
||
|
if (SDL_IsTextInputActive() == SDL_TRUE)
|
||
|
{
|
||
|
SDL_StopTextInput();
|
||
|
}
|
||
|
|
||
|
#ifdef VCMI_APPLE
|
||
|
});
|
||
|
#endif
|
||
|
}
|