Android: simplify input dialog
This commit is contained in:
parent
9e83c219e7
commit
da679d619e
@ -37,6 +37,7 @@ import com.multicraft.game.MainActivity.Companion.radius
|
|||||||
import com.multicraft.game.databinding.InputTextBinding
|
import com.multicraft.game.databinding.InputTextBinding
|
||||||
import com.multicraft.game.databinding.MultilineInputBinding
|
import com.multicraft.game.databinding.MultilineInputBinding
|
||||||
import com.multicraft.game.helpers.*
|
import com.multicraft.game.helpers.*
|
||||||
|
import com.multicraft.game.helpers.ApiLevelHelper.isOreo
|
||||||
import org.libsdl.app.SDLActivity
|
import org.libsdl.app.SDLActivity
|
||||||
|
|
||||||
class GameActivity : SDLActivity() {
|
class GameActivity : SDLActivity() {
|
||||||
@ -65,7 +66,7 @@ class GameActivity : SDLActivity() {
|
|||||||
return getContext().applicationInfo.nativeLibraryDir + "/libMultiCraft.so"
|
return getContext().applicationInfo.nativeLibraryDir + "/libMultiCraft.so"
|
||||||
}
|
}
|
||||||
|
|
||||||
public override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
|
||||||
hasKeyboard = resources.configuration.hardKeyboardHidden == HARDKEYBOARDHIDDEN_NO
|
hasKeyboard = resources.configuration.hardKeyboardHidden == HARDKEYBOARDHIDDEN_NO
|
||||||
@ -103,10 +104,9 @@ class GameActivity : SDLActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
fun showDialog(
|
fun showDialog(hint: String?, current: String?, editType: Int) {
|
||||||
@Suppress("UNUSED_PARAMETER") s: String?,
|
messageReturnValue = ""
|
||||||
hint: String?, current: String?, editType: Int
|
messageReturnCode = -1
|
||||||
) {
|
|
||||||
if (editType == 1)
|
if (editType == 1)
|
||||||
runOnUiThread { showMultiLineDialog(hint, current) }
|
runOnUiThread { showMultiLineDialog(hint, current) }
|
||||||
else
|
else
|
||||||
@ -117,9 +117,10 @@ class GameActivity : SDLActivity() {
|
|||||||
isInputActive = true
|
isInputActive = true
|
||||||
val builder = AlertDialog.Builder(this, R.style.FullScreenDialogStyle)
|
val builder = AlertDialog.Builder(this, R.style.FullScreenDialogStyle)
|
||||||
val binding = InputTextBinding.inflate(layoutInflater)
|
val binding = InputTextBinding.inflate(layoutInflater)
|
||||||
val hintText = hint?.ifEmpty {
|
var hintText: String = hint?.ifEmpty {
|
||||||
resources.getString(if (editType == 3) R.string.input_password else R.string.input_text)
|
resources.getString(if (editType == 3) R.string.input_password else R.string.input_text)
|
||||||
}
|
}.toString()
|
||||||
|
hintText = hintText.replace(":$".toRegex(), "")
|
||||||
binding.input.hint = hintText
|
binding.input.hint = hintText
|
||||||
builder.setView(binding.root)
|
builder.setView(binding.root)
|
||||||
val alertDialog = builder.create()
|
val alertDialog = builder.create()
|
||||||
@ -129,16 +130,19 @@ class GameActivity : SDLActivity() {
|
|||||||
editText.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
|
editText.imeOptions = EditorInfo.IME_FLAG_NO_FULLSCREEN
|
||||||
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
|
val imm = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
|
||||||
var inputType = InputType.TYPE_CLASS_TEXT
|
var inputType = InputType.TYPE_CLASS_TEXT
|
||||||
if (editType == 3)
|
if (editType == 3) {
|
||||||
inputType = inputType or InputType.TYPE_TEXT_VARIATION_PASSWORD
|
inputType = inputType or InputType.TYPE_TEXT_VARIATION_PASSWORD
|
||||||
|
if (isOreo())
|
||||||
|
editText.importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO
|
||||||
|
}
|
||||||
editText.inputType = inputType
|
editText.inputType = inputType
|
||||||
editText.setSelection(editText.text?.length ?: 0)
|
editText.setSelection(editText.text?.length ?: 0)
|
||||||
// for Android OS
|
// for Android OS
|
||||||
editText.setOnEditorActionListener { _: TextView?, KeyCode: Int, _: KeyEvent? ->
|
editText.setOnEditorActionListener { _: TextView?, KeyCode: Int, _: KeyEvent? ->
|
||||||
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
return@setOnEditorActionListener true
|
return@setOnEditorActionListener true
|
||||||
@ -149,8 +153,8 @@ class GameActivity : SDLActivity() {
|
|||||||
editText.setOnKeyListener { _: View?, KeyCode: Int, _: KeyEvent? ->
|
editText.setOnKeyListener { _: View?, KeyCode: Int, _: KeyEvent? ->
|
||||||
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
return@setOnKeyListener true
|
return@setOnKeyListener true
|
||||||
@ -159,15 +163,15 @@ class GameActivity : SDLActivity() {
|
|||||||
}
|
}
|
||||||
binding.input.setEndIconOnClickListener {
|
binding.input.setEndIconOnClickListener {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
}
|
}
|
||||||
binding.rl.setOnClickListener {
|
binding.rl.setOnClickListener {
|
||||||
window.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
window.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
||||||
messageReturnValue = current.toString()
|
messageReturnValue = current.toString()
|
||||||
messageReturnCode = -1
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
}
|
}
|
||||||
@ -180,7 +184,7 @@ class GameActivity : SDLActivity() {
|
|||||||
alertDialog.setOnCancelListener {
|
alertDialog.setOnCancelListener {
|
||||||
window.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
window.setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_HIDDEN)
|
||||||
messageReturnValue = current.toString()
|
messageReturnValue = current.toString()
|
||||||
messageReturnCode = -1
|
messageReturnCode = 0
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -189,9 +193,10 @@ class GameActivity : SDLActivity() {
|
|||||||
isInputActive = true
|
isInputActive = true
|
||||||
val builder = AlertDialog.Builder(this, R.style.FullScreenDialogStyle)
|
val builder = AlertDialog.Builder(this, R.style.FullScreenDialogStyle)
|
||||||
val binding = MultilineInputBinding.inflate(layoutInflater)
|
val binding = MultilineInputBinding.inflate(layoutInflater)
|
||||||
val hintText = hint?.ifEmpty {
|
var hintText: String = hint?.ifEmpty {
|
||||||
resources.getString(R.string.input_text)
|
resources.getString(R.string.input_text)
|
||||||
}
|
}.toString()
|
||||||
|
hintText = hintText.replace(":$".toRegex(), "")
|
||||||
binding.multiInput.hint = hintText
|
binding.multiInput.hint = hintText
|
||||||
builder.setView(binding.root)
|
builder.setView(binding.root)
|
||||||
val alertDialog = builder.create()
|
val alertDialog = builder.create()
|
||||||
@ -205,8 +210,8 @@ class GameActivity : SDLActivity() {
|
|||||||
editText.setOnEditorActionListener { _: TextView?, KeyCode: Int, _: KeyEvent? ->
|
editText.setOnEditorActionListener { _: TextView?, KeyCode: Int, _: KeyEvent? ->
|
||||||
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
return@setOnEditorActionListener true
|
return@setOnEditorActionListener true
|
||||||
@ -217,8 +222,8 @@ class GameActivity : SDLActivity() {
|
|||||||
editText.setOnKeyListener { _: View?, KeyCode: Int, _: KeyEvent? ->
|
editText.setOnKeyListener { _: View?, KeyCode: Int, _: KeyEvent? ->
|
||||||
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
if (KeyCode == KeyEvent.KEYCODE_ENTER || KeyCode == KeyEvent.KEYCODE_ENDCALL) {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
return@setOnKeyListener true
|
return@setOnKeyListener true
|
||||||
@ -227,8 +232,8 @@ class GameActivity : SDLActivity() {
|
|||||||
}
|
}
|
||||||
binding.multiInput.setEndIconOnClickListener {
|
binding.multiInput.setEndIconOnClickListener {
|
||||||
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
imm.hideSoftInputFromWindow(editText.windowToken, 0)
|
||||||
messageReturnCode = 0
|
|
||||||
messageReturnValue = editText.text.toString()
|
messageReturnValue = editText.text.toString()
|
||||||
|
messageReturnCode = 0
|
||||||
alertDialog.dismiss()
|
alertDialog.dismiss()
|
||||||
isInputActive = false
|
isInputActive = false
|
||||||
}
|
}
|
||||||
|
@ -2141,7 +2141,7 @@ void Game::openConsole(float scale, const wchar_t *line)
|
|||||||
|
|
||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
#if defined(__ANDROID__) || defined(__IOS__)
|
||||||
if (!porting::hasRealKeyboard()) {
|
if (!porting::hasRealKeyboard()) {
|
||||||
porting::showInputDialog(gettext("OK"), "", "", 2);
|
porting::showInputDialog("", "", 2);
|
||||||
m_android_chat_open = true;
|
m_android_chat_open = true;
|
||||||
} else {
|
} else {
|
||||||
#endif
|
#endif
|
||||||
|
@ -261,7 +261,7 @@ bool GUIConfirmRegistration::OnEvent(const SEvent &event)
|
|||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
#if defined(__ANDROID__) || defined(__IOS__)
|
||||||
bool GUIConfirmRegistration::getAndroidUIInput()
|
bool GUIConfirmRegistration::getAndroidUIInput()
|
||||||
{
|
{
|
||||||
if (!hasAndroidUIInput() || m_jni_field_name != "password")
|
if (m_jni_field_name.empty() || m_jni_field_name != "password")
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// still waiting
|
// still waiting
|
||||||
|
@ -3595,7 +3595,7 @@ void GUIFormSpecMenu::legacySortElements(core::list<IGUIElement *>::Iterator fro
|
|||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
#if defined(__ANDROID__) || defined(__IOS__)
|
||||||
bool GUIFormSpecMenu::getAndroidUIInput()
|
bool GUIFormSpecMenu::getAndroidUIInput()
|
||||||
{
|
{
|
||||||
if (!hasAndroidUIInput())
|
if (m_jni_field_name.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// still waiting
|
// still waiting
|
||||||
@ -3616,6 +3616,17 @@ bool GUIFormSpecMenu::getAndroidUIInput()
|
|||||||
|
|
||||||
std::string text = porting::getInputDialogValue();
|
std::string text = porting::getInputDialogValue();
|
||||||
((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
|
((gui::IGUIEditBox *)element)->setText(utf8_to_wide(text).c_str());
|
||||||
|
|
||||||
|
// Create event
|
||||||
|
gui::IGUIElement *focus = Environment->getFocus();
|
||||||
|
if (focus) {
|
||||||
|
SEvent e;
|
||||||
|
e.EventType = EET_GUI_EVENT;
|
||||||
|
e.GUIEvent.Caller = focus;
|
||||||
|
e.GUIEvent.Element = 0;
|
||||||
|
e.GUIEvent.EventType = gui::EGET_EDITBOX_CHANGED;
|
||||||
|
element->OnEvent(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ GUIModalMenu::GUIModalMenu(gui::IGUIEnvironment* env, gui::IGUIElement* parent,
|
|||||||
IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
|
IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
|
||||||
core::rect<s32>(0, 0, 100, 100)),
|
core::rect<s32>(0, 0, 100, 100)),
|
||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
#if defined(__ANDROID__) || defined(__IOS__)
|
||||||
m_jni_field_name(""),
|
m_jni_field_name(),
|
||||||
#endif
|
#endif
|
||||||
m_menumgr(menumgr),
|
m_menumgr(menumgr),
|
||||||
m_remap_dbl_click(remap_dbl_click)
|
m_remap_dbl_click(remap_dbl_click)
|
||||||
@ -287,14 +287,6 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
|
|||||||
return retval;
|
return retval;
|
||||||
|
|
||||||
m_jni_field_name = field_name;
|
m_jni_field_name = field_name;
|
||||||
/*~ Imperative, as in "Enter/type in text".
|
|
||||||
Don't forget the space. */
|
|
||||||
std::string message = gettext("Enter ");
|
|
||||||
std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
|
|
||||||
if (label.empty())
|
|
||||||
label = "text";
|
|
||||||
message += gettext(label.c_str());
|
|
||||||
message += ":";
|
|
||||||
|
|
||||||
// single line text input
|
// single line text input
|
||||||
int type = 2;
|
int type = 2;
|
||||||
@ -307,7 +299,7 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
|
|||||||
if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
|
if (((gui::IGUIEditBox *)hovered)->isPasswordBox())
|
||||||
type = 3;
|
type = 3;
|
||||||
|
|
||||||
porting::showInputDialog(gettext("OK"), "",
|
porting::showInputDialog(wide_to_utf8(getLabelByID(hovered->getID())),
|
||||||
wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
|
wide_to_utf8(((gui::IGUIEditBox *)hovered)->getText()), type);
|
||||||
return retval;
|
return retval;
|
||||||
}
|
}
|
||||||
@ -385,24 +377,3 @@ bool GUIModalMenu::preprocessEvent(const SEvent &event)
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
|
||||||
bool GUIModalMenu::hasAndroidUIInput()
|
|
||||||
{
|
|
||||||
// no dialog shown
|
|
||||||
if (m_jni_field_name.empty())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// still waiting
|
|
||||||
if (porting::getInputDialogState() == -1)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
// no value abort dialog processing
|
|
||||||
if (porting::getInputDialogState() != 0) {
|
|
||||||
m_jni_field_name.clear();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
@ -56,7 +56,6 @@ public:
|
|||||||
virtual bool pausesGame() { return false; } // Used for pause menu
|
virtual bool pausesGame() { return false; } // Used for pause menu
|
||||||
#if defined(__ANDROID__) || defined(__IOS__)
|
#if defined(__ANDROID__) || defined(__IOS__)
|
||||||
virtual bool getAndroidUIInput() { return false; }
|
virtual bool getAndroidUIInput() { return false; }
|
||||||
bool hasAndroidUIInput();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -211,22 +211,19 @@ void initializePathsAndroid()
|
|||||||
activityObj, mt_getAbsPath, "getCacheDir");
|
activityObj, mt_getAbsPath, "getCacheDir");
|
||||||
}
|
}
|
||||||
|
|
||||||
void showInputDialog(const std::string &acceptButton, const std::string &hint,
|
void showInputDialog(const std::string &hint, const std::string ¤t, int editType)
|
||||||
const std::string ¤t, int editType)
|
|
||||||
{
|
{
|
||||||
jmethodID showdialog = jnienv->GetMethodID(activityClass, "showDialog",
|
jmethodID showdialog = jnienv->GetMethodID(activityClass, "showDialog",
|
||||||
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
|
"(Ljava/lang/String;Ljava/lang/String;I)V");
|
||||||
|
|
||||||
FATAL_ERROR_IF(showdialog == nullptr,
|
FATAL_ERROR_IF(showdialog == nullptr,
|
||||||
"porting::showInputDialog unable to find java show dialog method");
|
"porting::showInputDialog unable to find java show dialog method");
|
||||||
|
|
||||||
jstring jacceptButton = jnienv->NewStringUTF(acceptButton.c_str());
|
|
||||||
jstring jhint = jnienv->NewStringUTF(hint.c_str());
|
jstring jhint = jnienv->NewStringUTF(hint.c_str());
|
||||||
jstring jcurrent = jnienv->NewStringUTF(current.c_str());
|
jstring jcurrent = jnienv->NewStringUTF(current.c_str());
|
||||||
jint jeditType = editType;
|
jint jeditType = editType;
|
||||||
|
|
||||||
jnienv->CallVoidMethod(activityObj, showdialog,
|
jnienv->CallVoidMethod(activityObj, showdialog, jhint, jcurrent, jeditType);
|
||||||
jacceptButton, jhint, jcurrent, jeditType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void openURIAndroid(const std::string &url)
|
void openURIAndroid(const std::string &url)
|
||||||
|
@ -41,20 +41,17 @@ void cleanupAndroid();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes path_* variables for Android
|
* Initializes path_* variables for Android
|
||||||
* @param env Android JNI environment
|
|
||||||
*/
|
*/
|
||||||
void initializePathsAndroid();
|
void initializePathsAndroid();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* show text input dialog in java
|
* show text input dialog in java
|
||||||
* @param acceptButton text to display on accept button
|
|
||||||
* @param hint hint to show
|
* @param hint hint to show
|
||||||
* @param current initial value to display
|
* @param current initial value to display
|
||||||
* @param editType type of texfield
|
* @param editType type of texfield
|
||||||
* (1==multiline text input; 2==single line text input; 3=password field)
|
* (1 == multiline text input; 2 == single line text input; 3 == password field)
|
||||||
*/
|
*/
|
||||||
void showInputDialog(const std::string &acceptButton,
|
void showInputDialog(const std::string &hint, const std::string ¤t, int editType);
|
||||||
const std::string &hint, const std::string ¤t, int editType);
|
|
||||||
|
|
||||||
void openURIAndroid(const std::string &url);
|
void openURIAndroid(const std::string &url);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user