Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move touchscreen input handling to base GUIModalMenu class
  • Loading branch information
stujones11 authored and sfan5 committed Oct 29, 2018
1 parent 0d54399 commit 323c860
Show file tree
Hide file tree
Showing 14 changed files with 425 additions and 272 deletions.
1 change: 1 addition & 0 deletions build/android/jni/Android.mk
Expand Up @@ -165,6 +165,7 @@ LOCAL_SRC_FILES := \
jni/src/guiscalingfilter.cpp \
jni/src/gui/guiVolumeChange.cpp \
jni/src/gui/intlGUIEditBox.cpp \
jni/src/gui/modalMenu.cpp \
jni/src/gui/profilergraph.cpp \
jni/src/gui/touchscreengui.cpp \
jni/src/httpfetch.cpp \
Expand Down
9 changes: 7 additions & 2 deletions src/game.cpp
Expand Up @@ -1288,7 +1288,12 @@ bool Game::createClient(const std::string &playername,
return false;

bool could_connect, connect_aborted;

#ifdef HAVE_TOUCHSCREENGUI
if (g_touchscreengui) {
g_touchscreengui->init(texture_src);
g_touchscreengui->hide();
}
#endif
if (!connectToServer(playername, password, address, port,
&could_connect, &connect_aborted))
return false;
Expand Down Expand Up @@ -1414,7 +1419,7 @@ bool Game::initGui()
#ifdef HAVE_TOUCHSCREENGUI

if (g_touchscreengui)
g_touchscreengui->init(texture_src);
g_touchscreengui->show();

#endif

Expand Down
1 change: 1 addition & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -10,6 +10,7 @@ set(gui_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/guiTable.cpp
${CMAKE_CURRENT_SOURCE_DIR}/guiVolumeChange.cpp
${CMAKE_CURRENT_SOURCE_DIR}/intlGUIEditBox.cpp
${CMAKE_CURRENT_SOURCE_DIR}/modalMenu.cpp
${CMAKE_CURRENT_SOURCE_DIR}/profilergraph.cpp
PARENT_SCOPE
)
28 changes: 27 additions & 1 deletion src/gui/guiConfirmRegistration.cpp
Expand Up @@ -43,6 +43,9 @@ GUIConfirmRegistration::GUIConfirmRegistration(gui::IGUIEnvironment *env,
m_client(client), m_playername(playername), m_password(password),
m_address(address), m_aborted(aborted)
{
#ifdef __ANDROID__
m_touchscreen_visible = false;
#endif
}

GUIConfirmRegistration::~GUIConfirmRegistration()
Expand Down Expand Up @@ -157,6 +160,9 @@ void GUIConfirmRegistration::drawMenu()
driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);

gui::IGUIElement::draw();
#ifdef __ANDROID__
getAndroidUIInput();
#endif
}

void GUIConfirmRegistration::closeMenu(bool goNext)
Expand Down Expand Up @@ -193,10 +199,14 @@ bool GUIConfirmRegistration::processInput()
bool GUIConfirmRegistration::OnEvent(const SEvent &event)
{
if (event.EventType == EET_KEY_INPUT_EVENT) {
if (event.KeyInput.Key == KEY_ESCAPE && event.KeyInput.PressedDown) {
// clang-format off
if ((event.KeyInput.Key == KEY_ESCAPE ||
event.KeyInput.Key == KEY_CANCEL) &&
event.KeyInput.PressedDown) {
closeMenu(false);
return true;
}
// clang-format on
if (event.KeyInput.Key == KEY_RETURN && event.KeyInput.PressedDown) {
acceptInput();
if (processInput())
Expand Down Expand Up @@ -239,3 +249,19 @@ bool GUIConfirmRegistration::OnEvent(const SEvent &event)

return false;
}

#ifdef __ANDROID__
bool GUIConfirmRegistration::getAndroidUIInput()
{
if (!hasAndroidUIInput() || m_jni_field_name != "password")
return false;

std::string text = porting::getInputDialogValue();
gui::IGUIElement *e = getElementFromId(ID_confirmPassword);
if (e)
e->setText(utf8_to_wide(text).c_str());

m_jni_field_name.clear();
return false;
}
#endif
6 changes: 6 additions & 0 deletions src/gui/guiConfirmRegistration.h
Expand Up @@ -50,8 +50,14 @@ class GUIConfirmRegistration : public GUIModalMenu
bool processInput();

bool OnEvent(const SEvent &event);
#ifdef __ANDROID__
bool getAndroidUIInput();
#endif

private:
std::wstring getLabelByID(s32 id) { return L""; }
std::string getNameByID(s32 id) { return "password"; }

Client *m_client = nullptr;
const std::string &m_playername;
const std::string &m_password;
Expand Down
178 changes: 5 additions & 173 deletions src/gui/guiFormSpecMenu.cpp
Expand Up @@ -97,9 +97,6 @@ GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
m_text_dst(tdst),
m_joystick(joystick),
m_remap_dbl_click(remap_dbl_click)
#ifdef __ANDROID__
, m_JavaDialogFieldName("")
#endif
{
current_keys_pending.key_down = false;
current_keys_pending.key_up = false;
Expand Down Expand Up @@ -2265,23 +2262,11 @@ void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
#ifdef __ANDROID__
bool GUIFormSpecMenu::getAndroidUIInput()
{
/* no dialog shown */
if (m_JavaDialogFieldName == "") {
if (!hasAndroidUIInput())
return false;
}

/* still waiting */
if (porting::getInputDialogState() == -1) {
return true;
}

std::string fieldname = m_JavaDialogFieldName;
m_JavaDialogFieldName = "";

/* no value abort dialog processing */
if (porting::getInputDialogState() != 0) {
return false;
}
std::string fieldname = m_jni_field_name;
m_jni_field_name.clear();

for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
iter != m_fields.end(); ++iter) {
Expand All @@ -2301,8 +2286,7 @@ bool GUIFormSpecMenu::getAndroidUIInput()

std::string text = porting::getInputDialogValue();

((gui::IGUIEditBox*) tochange)->
setText(utf8_to_wide(text).c_str());
((gui::IGUIEditBox *)tochange)->setText(utf8_to_wide(text).c_str());
}
return false;
}
Expand Down Expand Up @@ -3043,158 +3027,6 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
}
}

#ifdef __ANDROID__
// display software keyboard when clicking edit boxes
if (event.EventType == EET_MOUSE_INPUT_EVENT
&& event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
gui::IGUIElement *hovered =
Environment->getRootGUIElement()->getElementFromPoint(
core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
bool retval = hovered->OnEvent(event);
if (retval)
Environment->setFocus(hovered);

std::string field_name = getNameByID(hovered->getID());
/* read-only field */
if (field_name.empty())
return retval;

m_JavaDialogFieldName = field_name;
std::string message = gettext("Enter ");
std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
if (label.empty())
label = "text";
message += gettext(label) + ":";

/* single line text input */
int type = 2;

/* multi line text input */
if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled())
type = 1;

/* passwords are always single line */
if (((gui::IGUIEditBox*) hovered)->isPasswordBox())
type = 3;

porting::showInputDialog(gettext("ok"), "",
wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
type);
return retval;
}
}

if (event.EventType == EET_TOUCH_INPUT_EVENT)
{
SEvent translated;
memset(&translated, 0, sizeof(SEvent));
translated.EventType = EET_MOUSE_INPUT_EVENT;
gui::IGUIElement* root = Environment->getRootGUIElement();

if (!root) {
errorstream
<< "GUIFormSpecMenu::preprocessEvent unable to get root element"
<< std::endl;
return false;
}
gui::IGUIElement* hovered = root->getElementFromPoint(
core::position2d<s32>(
event.TouchInput.X,
event.TouchInput.Y));

translated.MouseInput.X = event.TouchInput.X;
translated.MouseInput.Y = event.TouchInput.Y;
translated.MouseInput.Control = false;

bool dont_send_event = false;

if (event.TouchInput.touchedCount == 1) {
switch (event.TouchInput.Event) {
case ETIE_PRESSED_DOWN:
m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
translated.MouseInput.ButtonStates = EMBSM_LEFT;
m_down_pos = m_pointer;
break;
case ETIE_MOVED:
m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
translated.MouseInput.Event = EMIE_MOUSE_MOVED;
translated.MouseInput.ButtonStates = EMBSM_LEFT;
break;
case ETIE_LEFT_UP:
translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
translated.MouseInput.ButtonStates = 0;
hovered = root->getElementFromPoint(m_down_pos);
/* we don't have a valid pointer element use last
* known pointer pos */
translated.MouseInput.X = m_pointer.X;
translated.MouseInput.Y = m_pointer.Y;

/* reset down pos */
m_down_pos = v2s32(0,0);
break;
default:
dont_send_event = true;
//this is not supposed to happen
errorstream
<< "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
<< event.TouchInput.Event << std::endl;
}
} else if ( (event.TouchInput.touchedCount == 2) &&
(event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
hovered = root->getElementFromPoint(m_down_pos);

translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
translated.MouseInput.X = m_pointer.X;
translated.MouseInput.Y = m_pointer.Y;

if (hovered) {
hovered->OnEvent(translated);
}

translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
translated.MouseInput.ButtonStates = EMBSM_LEFT;


if (hovered) {
hovered->OnEvent(translated);
}
dont_send_event = true;
}
/* ignore unhandled 2 touch events ... accidental moving for example */
else if (event.TouchInput.touchedCount == 2) {
dont_send_event = true;
}
else if (event.TouchInput.touchedCount > 2) {
errorstream
<< "GUIFormSpecMenu::preprocessEvent to many multitouch events "
<< event.TouchInput.touchedCount << " ignoring them" << std::endl;
}

if (dont_send_event) {
return true;
}

/* check if translated event needs to be preprocessed again */
if (preprocessEvent(translated)) {
return true;
}
if (hovered) {
grab();
bool retval = hovered->OnEvent(translated);

if (event.TouchInput.Event == ETIE_LEFT_UP) {
/* reset pointer */
m_pointer = v2s32(0,0);
}
drop();
return retval;
}
}
#endif

if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
/* TODO add a check like:
if (event.JoystickEvent != joystick_we_listen_for)
Expand All @@ -3214,7 +3046,7 @@ bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
return handled;
}

return false;
return GUIModalMenu::preprocessEvent(event);
}

/******************************************************************************/
Expand Down
11 changes: 2 additions & 9 deletions src/gui/guiFormSpecMenu.h
Expand Up @@ -372,6 +372,8 @@ class GUIFormSpecMenu : public GUIModalMenu
{
return padding + offset + AbsoluteRect.UpperLeftCorner;
}
std::wstring getLabelByID(s32 id);
std::string getNameByID(s32 id);
v2s32 getElementBasePos(bool absolute,
const std::vector<std::string> *v_pos);

Expand Down Expand Up @@ -411,8 +413,6 @@ class GUIFormSpecMenu : public GUIModalMenu
bool m_selected_dragging = false;
ItemStack m_selected_swap;

v2s32 m_pointer;
v2s32 m_old_pointer; // Mouse position after previous mouse event
gui::IGUIStaticText *m_tooltip_element = nullptr;

u64 m_tooltip_show_delay;
Expand Down Expand Up @@ -535,13 +535,6 @@ class GUIFormSpecMenu : public GUIModalMenu
int m_btn_height;
gui::IGUIFont *m_font = nullptr;

std::wstring getLabelByID(s32 id);
std::string getNameByID(s32 id);
#ifdef __ANDROID__
v2s32 m_down_pos;
std::string m_JavaDialogFieldName;
#endif

/* If true, remap a double-click (or double-tap) action to ESC. This is so
* that, for example, Android users can double-tap to close a formspec.
*
Expand Down
4 changes: 4 additions & 0 deletions src/gui/guiKeyChangeMenu.h
Expand Up @@ -58,6 +58,10 @@ class GUIKeyChangeMenu : public GUIModalMenu

bool pausesGame() { return true; }

protected:
std::wstring getLabelByID(s32 id) { return L""; }
std::string getNameByID(s32 id) { return ""; }

private:
void init_keys();

Expand Down

0 comments on commit 323c860

Please sign in to comment.