Skip to content

Commit

Permalink
Android: Add selection dialog (drop down/combo box) (#13814)
Browse files Browse the repository at this point in the history
- The handling of IGUIComboBox uses the new setAndSendSelected() method.
- getDialogState() is now getInputDialogState() and returns the state of the input dialog.
- getLastDialogType() is added and returns current/last shown dialog's type.
- getInputDialogState() now returns an enum instead of int.
- getAndroidUIInput() now returns void instead of bool.
- New data types (enum) are added:
  (1) GameActivity.DialogType (Java) and porting::AndroidDialogType (C++)
  (2) GameActivity.DialogState (Java) and porting::AndroidDialogState (C++)
- When showing a text input dialog, there is no custom accept button text any more.
- showDialog()/showDialogUI() for text input is now showTextInputDialog()/showTextInputDialogUI().
- showInputDialog()/showDialogUI() for text input is now showTextInputDialog()/showTextInputDialogUI().
- getDialogValue()/getInputDialogValue() is now getDialogMessage()/getInputDialogMessage().


Co-authored-by: Gregor Parzefall <82708541+grorp@users.noreply.github.com>
  • Loading branch information
srifqi and grorp committed Jan 7, 2024
1 parent bd42cc2 commit 171f911
Show file tree
Hide file tree
Showing 10 changed files with 259 additions and 106 deletions.
59 changes: 48 additions & 11 deletions android/app/src/main/java/net/minetest/minetest/GameActivity.java
Expand Up @@ -51,8 +51,13 @@ public class GameActivity extends NativeActivity {
System.loadLibrary("minetest");
}

private int messageReturnCode = -1;
enum DialogType { TEXT_INPUT, SELECTION_INPUT }
enum DialogState { DIALOG_SHOWN, DIALOG_INPUTTED, DIALOG_CANCELED }

private DialogType lastDialogType = DialogType.TEXT_INPUT;
private DialogState inputDialogState = DialogState.DIALOG_CANCELED;
private String messageReturnValue = "";
private int selectionReturnValue = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -85,11 +90,17 @@ public void onBackPressed() {
// Ignore the back press so Minetest can handle it
}

public void showDialog(String acceptButton, String hint, String current, int editType) {
runOnUiThread(() -> showDialogUI(hint, current, editType));
public void showTextInputDialog(String hint, String current, int editType) {
runOnUiThread(() -> showTextInputDialogUI(hint, current, editType));
}

public void showSelectionInputDialog(String[] optionList, int selectedIdx) {
runOnUiThread(() -> showSelectionInputDialogUI(optionList, selectedIdx));
}

private void showDialogUI(String hint, String current, int editType) {
private void showTextInputDialogUI(String hint, String current, int editType) {
lastDialogType = DialogType.TEXT_INPUT;
inputDialogState = DialogState.DIALOG_SHOWN;
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
Expand All @@ -114,7 +125,7 @@ else if (editType == 3)
// For multi-line, do not submit the text after pressing Enter key
if (keyCode == KeyEvent.KEYCODE_ENTER && editType != 1) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
messageReturnCode = 0;
inputDialogState = DialogState.DIALOG_INPUTTED;
messageReturnValue = editText.getText().toString();
alertDialog.dismiss();
return true;
Expand All @@ -128,29 +139,55 @@ else if (editType == 3)
doneButton.setText(R.string.ime_dialog_done);
doneButton.setOnClickListener((view -> {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
messageReturnCode = 0;
inputDialogState = DialogState.DIALOG_INPUTTED;
messageReturnValue = editText.getText().toString();
alertDialog.dismiss();
}));
}
alertDialog.setOnCancelListener(dialog -> {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
inputDialogState = DialogState.DIALOG_CANCELED;
messageReturnValue = current;
messageReturnCode = -1;
});
alertDialog.show();
editText.requestFocusTryShow();
}

public int getDialogState() {
return messageReturnCode;
public void showSelectionInputDialogUI(String[] optionList, int selectedIdx) {
lastDialogType = DialogType.SELECTION_INPUT;
inputDialogState = DialogState.DIALOG_SHOWN;
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setSingleChoiceItems(optionList, selectedIdx, (dialog, selection) -> {
inputDialogState = DialogState.DIALOG_INPUTTED;
selectionReturnValue = selection;
dialog.dismiss();
});
builder.setOnCancelListener(dialog -> {
inputDialogState = DialogState.DIALOG_CANCELED;
selectionReturnValue = selectedIdx;
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}

public int getLastDialogType() {
return lastDialogType.ordinal();
}

public String getDialogValue() {
messageReturnCode = -1;
public int getInputDialogState() {
return inputDialogState.ordinal();
}

public String getDialogMessage() {
inputDialogState = DialogState.DIALOG_CANCELED;
return messageReturnValue;
}

public int getDialogSelection() {
inputDialogState = DialogState.DIALOG_CANCELED;
return selectionReturnValue;
}

public float getDensity() {
return getResources().getDisplayMetrics().density;
}
Expand Down
16 changes: 10 additions & 6 deletions src/client/game.cpp
Expand Up @@ -2277,7 +2277,7 @@ void Game::openConsole(float scale, const wchar_t *line)
assert(scale > 0.0f && scale <= 1.0f);

#ifdef __ANDROID__
porting::showInputDialog(gettext("ok"), "", "", 2);
porting::showTextInputDialog("", "", 2);
m_android_chat_open = true;
#else
if (gui_chat_console->isOpenInhibited())
Expand All @@ -2293,15 +2293,19 @@ void Game::openConsole(float scale, const wchar_t *line)
#ifdef __ANDROID__
void Game::handleAndroidChatInput()
{
if (m_android_chat_open && porting::getInputDialogState() == 0) {
std::string text = porting::getInputDialogValue();
client->typeChatMessage(utf8_to_wide(text));
m_android_chat_open = false;
// It has to be a text input
if (m_android_chat_open && porting::getLastInputDialogType() == porting::TEXT_INPUT) {
porting::AndroidDialogState dialogState = porting::getInputDialogState();
if (dialogState == porting::DIALOG_INPUTTED) {
std::string text = porting::getInputDialogMessage();
client->typeChatMessage(utf8_to_wide(text));
}
if (dialogState != porting::DIALOG_SHOWN)
m_android_chat_open = false;
}
}
#endif


void Game::toggleFreeMove()
{
bool free_move = !g_settings->getBool("free_move");
Expand Down
62 changes: 37 additions & 25 deletions src/gui/guiFormSpecMenu.cpp
Expand Up @@ -3497,46 +3497,58 @@ void GUIFormSpecMenu::legacySortElements(std::list<IGUIElement *>::iterator from
}

#ifdef __ANDROID__
bool GUIFormSpecMenu::getAndroidUIInput()
void GUIFormSpecMenu::getAndroidUIInput()
{
if (!hasAndroidUIInput())
return false;
porting::AndroidDialogState dialogState = getAndroidUIInputState();
if (dialogState == porting::DIALOG_SHOWN) {
return;
} else if (dialogState == porting::DIALOG_CANCELED) {
m_jni_field_name.clear();
return;
}

// still waiting
if (porting::getInputDialogState() == -1)
return true;
porting::AndroidDialogType dialog_type = porting::getLastInputDialogType();

std::string fieldname = m_jni_field_name;
m_jni_field_name.clear();

for (const FieldSpec &field : m_fields) {
if (field.fname != fieldname)
continue;
continue; // Iterate until found

IGUIElement *element = getElementFromId(field.fid, true);

if (!element || element->getType() != irr::gui::EGUIET_EDIT_BOX)
return false;
if (!element)
return;

gui::IGUIEditBox *editbox = (gui::IGUIEditBox *)element;
std::string text = porting::getInputDialogValue();
editbox->setText(utf8_to_wide(text).c_str());
auto element_type = element->getType();
if (dialog_type == porting::TEXT_INPUT && element_type == irr::gui::EGUIET_EDIT_BOX) {
gui::IGUIEditBox *editbox = (gui::IGUIEditBox *)element;
std::string text = porting::getInputDialogMessage();
editbox->setText(utf8_to_wide(text).c_str());

bool enter_after_edit = false;
auto iter = field_enter_after_edit.find(fieldname);
if (iter != field_enter_after_edit.end()) {
enter_after_edit = iter->second;
}
if (enter_after_edit && editbox->getParent()) {
SEvent enter;
enter.EventType = EET_GUI_EVENT;
enter.GUIEvent.Caller = editbox;
enter.GUIEvent.Element = nullptr;
enter.GUIEvent.EventType = gui::EGET_EDITBOX_ENTER;
editbox->getParent()->OnEvent(enter);
bool enter_after_edit = false;
auto iter = field_enter_after_edit.find(fieldname);
if (iter != field_enter_after_edit.end()) {
enter_after_edit = iter->second;
}
if (enter_after_edit && editbox->getParent()) {
SEvent enter;
enter.EventType = EET_GUI_EVENT;
enter.GUIEvent.Caller = editbox;
enter.GUIEvent.Element = nullptr;
enter.GUIEvent.EventType = gui::EGET_EDITBOX_ENTER;
editbox->getParent()->OnEvent(enter);
}
} else if (dialog_type == porting::SELECTION_INPUT &&
element_type == irr::gui::EGUIET_COMBO_BOX) {
auto dropdown = (gui::IGUIComboBox *) element;
int selected = porting::getInputDialogSelection();
dropdown->setAndSendSelected(selected);
}

return; // Early-return after found
}
return false;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/gui/guiFormSpecMenu.h
Expand Up @@ -286,7 +286,7 @@ class GUIFormSpecMenu : public GUIModalMenu
core::rect<s32> getAbsoluteRect();

#ifdef __ANDROID__
bool getAndroidUIInput();
void getAndroidUIInput();
#endif

protected:
Expand Down
23 changes: 14 additions & 9 deletions src/gui/guiPasswordChange.cpp
Expand Up @@ -264,14 +264,19 @@ std::string GUIPasswordChange::getNameByID(s32 id)
}

#ifdef __ANDROID__
bool GUIPasswordChange::getAndroidUIInput()
void GUIPasswordChange::getAndroidUIInput()
{
if (!hasAndroidUIInput())
return false;
porting::AndroidDialogState dialogState = getAndroidUIInputState();
if (dialogState == porting::DIALOG_SHOWN) {
return;
} else if (dialogState == porting::DIALOG_CANCELED) {
m_jni_field_name.clear();
return;
}

// still waiting
if (porting::getInputDialogState() == -1)
return true;
// It has to be a text input
if (porting::getLastInputDialogType() != porting::TEXT_INPUT)
return;

gui::IGUIElement *e = nullptr;
if (m_jni_field_name == "old_password")
Expand All @@ -283,10 +288,10 @@ bool GUIPasswordChange::getAndroidUIInput()
m_jni_field_name.clear();

if (!e || e->getType() != irr::gui::EGUIET_EDIT_BOX)
return false;
return;

std::string text = porting::getInputDialogValue();
std::string text = porting::getInputDialogMessage();
e->setText(utf8_to_wide(text).c_str());
return false;
return;
}
#endif
2 changes: 1 addition & 1 deletion src/gui/guiPasswordChange.h
Expand Up @@ -45,7 +45,7 @@ class GUIPasswordChange : public GUIModalMenu

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

protected:
Expand Down
45 changes: 29 additions & 16 deletions src/gui/modalMenu.cpp
Expand Up @@ -268,11 +268,34 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
type = 3;

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

if (event.EventType == EET_GUI_EVENT) {
if (event.GUIEvent.EventType == gui::EGET_LISTBOX_OPENED) {
gui::IGUIComboBox *dropdown = (gui::IGUIComboBox *) event.GUIEvent.Caller;

std::string field_name = getNameByID(dropdown->getID());
if (field_name.empty())
return false;

m_jni_field_name = field_name;

s32 selected_idx = dropdown->getSelected();
s32 option_size = dropdown->getItemCount();
std::string list_of_options[option_size];

for (s32 i = 0; i < option_size; i++) {
list_of_options[i] = wide_to_utf8(dropdown->getItem(i));
}

porting::showComboBoxDialog(list_of_options, option_size, selected_idx);
return true; // Prevent the Irrlicht dropdown from opening.
}
}
#endif

// Convert touch events into mouse events.
Expand Down Expand Up @@ -347,22 +370,12 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
}

#ifdef __ANDROID__
bool GUIModalMenu::hasAndroidUIInput()
porting::AndroidDialogState GUIModalMenu::getAndroidUIInputState()
{
// no dialog shown
// No dialog is shown
if (m_jni_field_name.empty())
return false;

// still waiting
if (porting::getInputDialogState() == -1)
return true;
return porting::DIALOG_CANCELED;

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

return true;
return porting::getInputDialogState();
}
#endif
7 changes: 5 additions & 2 deletions src/gui/modalMenu.h
Expand Up @@ -22,6 +22,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "util/string.h"
#ifdef __ANDROID__
#include <porting_android.h>
#endif

enum class PointerType {
Mouse,
Expand Down Expand Up @@ -59,8 +62,8 @@ class GUIModalMenu : public gui::IGUIElement
virtual bool OnEvent(const SEvent &event) { return false; };
virtual bool pausesGame() { return false; } // Used for pause menu
#ifdef __ANDROID__
virtual bool getAndroidUIInput() { return false; }
bool hasAndroidUIInput();
virtual void getAndroidUIInput() {};
porting::AndroidDialogState getAndroidUIInputState();
#endif

PointerType getPointerType() { return m_pointer_type; };
Expand Down

0 comments on commit 171f911

Please sign in to comment.