v0.4.3: fixed some crash bugs with wndtextinput

This commit is contained in:
Evan Debenham 2016-10-07 00:11:39 -04:00
parent 4ffd6f9c83
commit 7e0fc9f2f0

View File

@ -59,7 +59,8 @@ public class WndTextInput extends Window {
this( title, initialValue, multiLine ? MAX_LEN_MULTI : MAX_LEN_SINGLE, multiLine, posTxt, negTxt); this( title, initialValue, multiLine ? MAX_LEN_MULTI : MAX_LEN_SINGLE, multiLine, posTxt, negTxt);
} }
public WndTextInput(String title, String initialValue, int maxLength, final boolean multiLine, String posTxt, String negTxt){ public WndTextInput(final String title, final String initialValue, final int maxLength,
final boolean multiLine, final String posTxt, final String negTxt){
super(); super();
//need to offset to give space for the soft keyboard //need to offset to give space for the soft keyboard
@ -76,101 +77,101 @@ public class WndTextInput extends Window {
width = WIDTH; width = WIDTH;
} }
RenderedTextMultiline txtTitle = PixelScene.renderMultiline( title, 9 );
txtTitle.maxWidth( width );
txtTitle.hardlight( Window.TITLE_COLOR );
txtTitle.setPos( (width - txtTitle.width()) /2, 0);
add(txtTitle);
float pos = txtTitle.bottom() + MARGIN;
textInput = new EditText(ShatteredPixelDungeon.instance);
textInput.setText( initialValue );
textInput.setTypeface( RenderedText.getFont() );
textInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
textInput.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES );
//this accounts for the game resolution differing from the display resolution in power saver mode
final float scaledZoom;
scaledZoom = camera.zoom * (Game.dispWidth / (float)Game.width);
//sets different visual style depending on whether this is a single or multi line input.
final float inputHeight;
if (multiLine) {
textInput.setSingleLine(false);
//This is equivalent to PixelScene.renderText(6)
textInput.setTextSize( TypedValue.COMPLEX_UNIT_PX, 6*scaledZoom);
//8 lines of text (+1 line for padding)
inputHeight = 9*textInput.getLineHeight() / scaledZoom;
} else {
//sets to single line and changes enter key input to be the same as the positive button
textInput.setSingleLine();
textInput.setOnEditorActionListener( new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
onSelect(true);
hide();
return true;
}
});
//doesn't let the keyboard take over the whole UI
textInput.setImeOptions( EditorInfo.IME_FLAG_NO_EXTRACT_UI );
//centers text
textInput.setGravity(Gravity.CENTER);
//This is equivalent to PixelScene.renderText(9)
textInput.setTextSize( TypedValue.COMPLEX_UNIT_PX, 9*scaledZoom);
//1 line of text (+1 line for padding)
inputHeight = 2*textInput.getLineHeight() / scaledZoom;
}
//We haven't added the textInput yet, but we can anticipate its height at this point.
pos += inputHeight + MARGIN;
RedButton positiveBtn = new RedButton( posTxt ) {
@Override
protected void onClick() {
onSelect( true );
hide();
}
};
if (negTxt != null)
positiveBtn.setRect( MARGIN, pos, (width - MARGIN * 3) / 2, BUTTON_HEIGHT );
else
positiveBtn.setRect( MARGIN, pos, width - MARGIN * 2, BUTTON_HEIGHT );
add( positiveBtn );
if (negTxt != null){
RedButton negativeBtn = new RedButton( negTxt ) {
@Override
protected void onClick() {
onSelect( false );
hide();
}
};
negativeBtn.setRect( positiveBtn.right() + MARGIN, pos, (width - MARGIN * 3) / 2, BUTTON_HEIGHT );
add( negativeBtn );
}
pos += BUTTON_HEIGHT + MARGIN;
//The layout of the TextEdit is in display pixel space, not ingame pixel space
// resize the window first so we can know the screen-space coordinates for the text input.
resize( width, (int)pos );
final int inputTop = (int)(camera.cameraToScreen(0, txtTitle.bottom() + MARGIN).y * (Game.dispWidth / (float)Game.width));
//The text input exists in a separate view ontop of the normal game view.
// It visually appears to be a part of the game window but is infact a separate
// UI element from the game entirely.
ShatteredPixelDungeon.instance.runOnUiThread(new Runnable() { ShatteredPixelDungeon.instance.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
RenderedTextMultiline txtTitle = PixelScene.renderMultiline( title, 9 );
txtTitle.maxWidth( width );
txtTitle.hardlight( Window.TITLE_COLOR );
txtTitle.setPos( (width - txtTitle.width()) /2, 0);
add(txtTitle);
float pos = txtTitle.bottom() + MARGIN;
textInput = new EditText(ShatteredPixelDungeon.instance);
textInput.setText( initialValue );
textInput.setTypeface( RenderedText.getFont() );
textInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
textInput.setInputType( InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES );
//this accounts for the game resolution differing from the display resolution in power saver mode
final float scaledZoom;
scaledZoom = camera.zoom * (Game.dispWidth / (float)Game.width);
//sets different visual style depending on whether this is a single or multi line input.
final float inputHeight;
if (multiLine) {
textInput.setSingleLine(false);
//This is equivalent to PixelScene.renderText(6)
textInput.setTextSize( TypedValue.COMPLEX_UNIT_PX, 6*scaledZoom);
//8 lines of text (+1 line for padding)
inputHeight = 9*textInput.getLineHeight() / scaledZoom;
} else {
//sets to single line and changes enter key input to be the same as the positive button
textInput.setSingleLine();
textInput.setOnEditorActionListener( new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
onSelect(true);
hide();
return true;
}
});
//doesn't let the keyboard take over the whole UI
textInput.setImeOptions( EditorInfo.IME_FLAG_NO_EXTRACT_UI );
//centers text
textInput.setGravity(Gravity.CENTER);
//This is equivalent to PixelScene.renderText(9)
textInput.setTextSize( TypedValue.COMPLEX_UNIT_PX, 9*scaledZoom);
//1 line of text (+1 line for padding)
inputHeight = 2*textInput.getLineHeight() / scaledZoom;
}
//We haven't added the textInput yet, but we can anticipate its height at this point.
pos += inputHeight + MARGIN;
RedButton positiveBtn = new RedButton( posTxt ) {
@Override
protected void onClick() {
onSelect( true );
hide();
}
};
if (negTxt != null)
positiveBtn.setRect( MARGIN, pos, (width - MARGIN * 3) / 2, BUTTON_HEIGHT );
else
positiveBtn.setRect( MARGIN, pos, width - MARGIN * 2, BUTTON_HEIGHT );
add( positiveBtn );
if (negTxt != null){
RedButton negativeBtn = new RedButton( negTxt ) {
@Override
protected void onClick() {
onSelect( false );
hide();
}
};
negativeBtn.setRect( positiveBtn.right() + MARGIN, pos, (width - MARGIN * 3) / 2, BUTTON_HEIGHT );
add( negativeBtn );
}
pos += BUTTON_HEIGHT + MARGIN;
//The layout of the TextEdit is in display pixel space, not ingame pixel space
// resize the window first so we can know the screen-space coordinates for the text input.
resize( width, (int)pos );
final int inputTop = (int)(camera.cameraToScreen(0, txtTitle.bottom() + MARGIN).y * (Game.dispWidth / (float)Game.width));
//The text input exists in a separate view ontop of the normal game view.
// It visually appears to be a part of the game window but is infact a separate
// UI element from the game entirely.
FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams( FrameLayout.LayoutParams layout = new FrameLayout.LayoutParams(
(int)((width - MARGIN*2)*scaledZoom), (int)((width - MARGIN*2)*scaledZoom),
(int)(inputHeight * scaledZoom), (int)(inputHeight * scaledZoom),