Fix Android input box function. Add dpi to command line options in PCMain.

This commit is contained in:
Henrik Rydgård committed 2014-01-21 15:57:15 +01:00
1 parent 80a424fd18
commit ca90b79bf8
2 files changed
+115 -76

No files matched your search

@@ -30,6 +30,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Vibrator;
import android.text.InputType;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
@@ -116,8 +117,6 @@ public class NativeActivity extends Activity {
private AudioManager audioManager;
private Vibrator vibrator;
public static boolean inputBoxCancelled;
// Allow for two connected gamepads but just consider them the same for now.
// Actually this is not entirely true, see the code.
@@ -636,17 +635,18 @@ public class NativeActivity extends Activity {
return true;
}
}
public String inputBox(String title, String defaultText, String defaultAction) {
// The return value is sent elsewhere. TODO in java, in SendMessage in C++.
public void inputBox(String title, String defaultText, String defaultAction) {
final FrameLayout fl = new FrameLayout(this);
final EditText input = new EditText(this);
input.setGravity(Gravity.CENTER);
inputBoxCancelled = false;
FrameLayout.LayoutParams editBoxLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
FrameLayout.LayoutParams editBoxLayout = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
editBoxLayout.setMargins(2, 20, 2, 20);
fl.addView(input, editBoxLayout);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setText(defaultText);
input.selectAll();
@@ -655,79 +655,93 @@ public class NativeActivity extends Activity {
.setTitle(title)
.setPositiveButton(defaultAction, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int which) {
NativeApp.sendMessage("inputbox_completed", input.getText().toString());
d.dismiss();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface d, int which) {
NativeApp.sendMessage("inputbox_failed", "");
d.cancel();
NativeActivity.inputBoxCancelled = false;
}
}).create();
dlg.setCancelable(false);
dlg.setCancelable(true);
dlg.show();
if (inputBoxCancelled)
return null;
NativeApp.sendMessage("INPUTBOX:" + title, input.getText().toString());
return input.getText().toString();
}
public boolean processCommand(String command, String params) {
if (command.equals("launchBrowser")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(params));
startActivity(i);
return true;
} else if (command.equals("launchEmail")) {
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText;
uriText = "mailto:email@gmail.com" +
"?subject=Your app is..." +
"&body=great! Or?";
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "E-mail the app author!"));
return true;
} else if (command.equals("sharejpeg")) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + params));
startActivity(Intent.createChooser(share, "Share Picture"));
} else if (command.equals("sharetext")) {
Intent sendIntent = new Intent();
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, params);
sendIntent.setAction(Intent.ACTION_SEND);
startActivity(sendIntent);
} else if (command.equals("showTwitter")) {
String twitter_user_name = params;
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + twitter_user_name)));
} catch (Exception e) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
}
} else if (command.equals("launchMarket")) {
// Don't need this, can just use launchBrowser with a market:
// http://stackoverflow.com/questions/3442366/android-link-to-market-from-inside-another-app
// http://developer.android.com/guide/publishing/publishing.html#marketintent
return false;
} else if (command.equals("toast")) {
Toast toast = Toast.makeText(this, params, Toast.LENGTH_SHORT);
toast.show();
return true;
} else if (command.equals("showKeyboard")) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
// No idea what the point of the ApplicationWindowToken is or if it matters where we get it from...
inputMethodManager.toggleSoftInputFromWindow(mGLSurfaceView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
return true;
} else if (command.equals("hideKeyboard")) {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(mGLSurfaceView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
return true;
} else if (command.equals("inputBox")) {
inputBox(params, "", "OK");
return true;
} else if (command.equals("vibrate")) {
if (command.equals("launchBrowser")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(params));
startActivity(i);
return true;
} else if (command.equals("launchEmail")) {
Intent send = new Intent(Intent.ACTION_SENDTO);
String uriText;
uriText = "mailto:email@gmail.com" + "?subject=Your app is..."
+ "&body=great! Or?";
uriText = uriText.replace(" ", "%20");
Uri uri = Uri.parse(uriText);
send.setData(uri);
startActivity(Intent.createChooser(send, "E-mail the app author!"));
return true;
} else if (command.equals("sharejpeg")) {
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + params));
startActivity(Intent.createChooser(share, "Share Picture"));
} else if (command.equals("sharetext")) {
Intent sendIntent = new Intent();
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_TEXT, params);
sendIntent.setAction(Intent.ACTION_SEND);
startActivity(sendIntent);
} else if (command.equals("showTwitter")) {
String twitter_user_name = params;
try {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("twitter://user?screen_name="
+ twitter_user_name)));
} catch (Exception e) {
startActivity(new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
}
} else if (command.equals("launchMarket")) {
// Don't need this, can just use launchBrowser with a market:
// http://stackoverflow.com/questions/3442366/android-link-to-market-from-inside-another-app
// http://developer.android.com/guide/publishing/publishing.html#marketintent
return false;
} else if (command.equals("toast")) {
Toast toast = Toast.makeText(this, params, Toast.LENGTH_SHORT);
toast.show();
return true;
} else if (command.equals("showKeyboard")) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// No idea what the point of the ApplicationWindowToken is or if it
// matters where we get it from...
inputMethodManager.toggleSoftInputFromWindow(
mGLSurfaceView.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
return true;
} else if (command.equals("hideKeyboard")) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(
mGLSurfaceView.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
return true;
} else if (command.equals("inputbox")) {
String title = "Input";
String defString = "";
String[] param = params.split(":");
if (param[0].length() > 0)
title = param[0];
if (param.length > 1)
defString = param[1];
Log.i(TAG, "Launching inputbox: " + title + " " + defString);
inputBox(title, defString, "OK");
return true;
} else if (command.equals("vibrate")) {
if (vibrator != null) {
int milliseconds = -1;
if (params != "") {
@@ -736,11 +750,13 @@ public class NativeActivity extends Activity {
} catch (NumberFormatException e) {
}
}
// Special parameters to perform standard haptic feedback operations
// Special parameters to perform standard haptic feedback
// operations
// -1 = Standard keyboard press feedback
// -2 = Virtual key press
// -3 = Long press feedback
// Note that these three do not require the VIBRATE Android permission.
// Note that these three do not require the VIBRATE Android
// permission.
switch (milliseconds) {
case -1:
mGLSurfaceView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
@@ -756,8 +772,8 @@ public class NativeActivity extends Activity {
break;
}
}
return true;
}
return true;
}
return false;
}
}
+28 -5
View File
@@ -148,7 +148,8 @@ int8_t EGL_Init() {
g_Window = (NativeWindowType)sysInfo.info.x11.window;
#endif
g_eglSurface = eglCreateWindowSurface(g_eglDisplay, g_eglConfig, g_Window, 0);
if (g_eglSurface == EGL_NO_SURFACE) EGL_ERROR("Unable to create EGL surface!", true);
if (g_eglSurface == EGL_NO_SURFACE)
EGL_ERROR("Unable to create EGL surface!", true);
if (eglMakeCurrent(g_eglDisplay, g_eglSurface, g_eglSurface, g_eglContext) != EGL_TRUE)
EGL_ERROR("Unable to make GLES context current.", true);
@@ -323,10 +324,10 @@ extern void mixaudio(void *userdata, Uint8 *stream, int len) {
}
// returns -1 on failure
int parseInt(const char *str) {
static int parseInt(const char *str) {
int val;
int retval = sscanf(str, "%d", &val);
printf("%i = sscanf %s\n", retval, str);
printf("%i = scanf %s\n", retval, str);
if (retval != 1) {
return -1;
} else {
@@ -334,6 +335,17 @@ int parseInt(const char *str) {
}
}
static float parseFloat(const char *str) {
float val;
int retval = sscanf(str, "%f", &val);
printf("%i = sscanf %s\n", retval, str);
if (retval != 1) {
return -1.0f;
} else {
return val;
}
}
void ToggleFullScreenIfFlagSet() {
if (g_ToggleFullScreenNextFrame) {
g_ToggleFullScreenNextFrame = false;
@@ -396,6 +408,8 @@ int main(int argc, char *argv[]) {
mode = SDL_OPENGL;
int set_xres = -1;
int set_yres = -1;
float set_dpi = 1.0f;
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i],"--fullscreen"))
mode |= SDL_FULLSCREEN;
@@ -404,10 +418,15 @@ int main(int argc, char *argv[]) {
} else if (set_yres == -2) {
set_yres = parseInt(argv[i]);
}
if (set_dpi == -2)
set_dpi = parseFloat(argv[i]);
if (!strcmp(argv[i],"--xres"))
set_xres = -2;
if (!strcmp(argv[i],"--yres"))
set_yres = -2;
if (!strcmp(argv[i],"--dpi"))
set_dpi = -2;
}
#endif
if (mode & SDL_FULLSCREEN) {
@@ -436,9 +455,13 @@ int main(int argc, char *argv[]) {
if (set_yres > 0) {
pixel_yres = set_yres;
}
float dpi_scale = 1.0f;
if (set_dpi > 0) {
dpi_scale = set_dpi;
}
dp_xres = (float)pixel_xres;
dp_yres = (float)pixel_yres;
dp_xres = (float)pixel_xres * dpi_scale;
dp_yres = (float)pixel_yres * dpi_scale;
g_Screen = SDL_SetVideoMode(pixel_xres, pixel_yres, 0, mode);
if (g_Screen == NULL) {