Skip to content

Commit

Permalink
Merge pull request #1146 from BlisterB/master
Browse files Browse the repository at this point in the history
Android: Fix fast forward button and text centered in buttons
  • Loading branch information
Ghabry authored Jun 27, 2017
2 parents a1fb198 + f89ac39 commit 143a5c7
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.easyrpg.player.button_mapping;

import android.content.Context;
import android.graphics.Canvas;

import org.easyrpg.player.settings.SettingsManager;
import org.libsdl.app.SDLActivity;

public class FastForwardingButton extends VirtualButton {
boolean alreadyActivated;

protected FastForwardingButton(Context context, int keyCode, double posX, double posY, int size) {
super(context, keyCode, posX, posY, size);
}

@Override
public void onPressed() {
if (!debug_mode) {
if (!isPressed) {
isPressed = true;

SDLActivity.onNativeKeyDown(this.keyCode);
// Vibration
if (SettingsManager.isVibrationEnabled() && vibrator != null) {
vibrator.vibrate(SettingsManager.getVibrationDuration());
}
}
}
}

@Override
public void onReleased() {
if (!debug_mode) {
if (isPressed) {
isPressed = false;

if (!isTapMode() || (isTapMode() && alreadyActivated)) {
SDLActivity.onNativeKeyUp(this.keyCode);
alreadyActivated = false;
} else {
alreadyActivated = true;
}
}
}

}

@Override
protected void onDraw(Canvas canvas) {
setProperTransparency(canvas);

// Draw the rectangle surrounding the button's letter
int border = 5;
canvas.drawRect(border, border, realSize - border, realSize - border, painter);

// Draw the symbol, centered in the rectangle
drawCenter(canvas, painter, String.valueOf(charButton));
}

private boolean isTapMode() {
return (SettingsManager.getFastForwardMode() == 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.os.Vibrator;
import android.view.KeyEvent;
import android.view.MotionEvent;
Expand All @@ -35,7 +34,7 @@ public class VirtualButton extends View {

public static VirtualButton Create(Context context, int keyCode, double posX, double posY, int size) {
if (keyCode == KEY_FAST_FORWARD) {
return new VirtualButtonRectangle(context, keyCode, posX, posY, size);
return new FastForwardingButton(context, keyCode, posX, posY, size);
}

return new VirtualButton(context, keyCode, posX, posY, size);
Expand Down Expand Up @@ -70,26 +69,40 @@ protected VirtualButton(Context context, int keyCode, double posX, double posY,

@Override
protected void onDraw(Canvas canvas) {
setProperTransparency(canvas);

// Draw the circle surrounding the button's letter
int border = 5;
canvas.drawCircle(realSize / 2, realSize / 2, realSize / 2 - border, painter);

// Draw the letter, centered in the circle
drawCenter(canvas, painter, String.valueOf(charButton));
}

protected void setProperTransparency(Canvas canvas) {
if (!debug_mode) {
painter.setAlpha(255 - SettingsManager.getLayoutTransparency());
}
}

// Draw
// The circle
canvas.drawCircle(realSize / 2, realSize / 2, realSize / 2 - 5, painter);

// The letter
// Anticipate the size of the letter
/** Draw "text" centered in "canvas" */
protected void drawCenter(Canvas canvas, Paint paint, String text) {
// Set the text size
painter.setTextSize(Helper.getPixels(this, (int) (originalLetterSize * ((float) resizeFactor / 100))));
painter.getTextBounds("" + charButton, 0, 1, letterBound);

// Draw the letter, centered in the circle
canvas.drawText("" + charButton, (realSize - letterBound.width()) / 2,
letterBound.height() + (realSize - letterBound.height()) / 2, painter);
// Draw the text
Rect bound = new Rect();
canvas.getClipBounds(bound);
int cHeight = bound.height();
int cWidth = bound.width();
paint.setTextAlign(Paint.Align.LEFT);
paint.getTextBounds(text, 0, text.length(), bound);
float x = cWidth / 2f - bound.width() / 2f - bound.left;
float y = cHeight / 2f + bound.height() / 2f - bound.bottom;
canvas.drawText(text, x, y, paint);
}

public int getFuturSize() {
// Resize
realSize = (int) ((float) originalSize * resizeFactor / 100);

return realSize;
Expand Down Expand Up @@ -197,11 +210,7 @@ public char getAppropriateChar(int keyCode) {
} else if (keyCode == KEY_PLUS) {
charButton = '+';
} else if (keyCode == KEY_FAST_FORWARD) {
if (Build.VERSION.SDK_INT >= 16) { // Android 4.1
charButton = '⏩';
} else {
charButton = '>';
}
charButton = '»';
} else {
charButton = '?';
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ public void onDrawerSlide(View view, float arg1) {

// Add buttons
addButtons();

// Set speed multiplier
setFastForwardMultiplier(SettingsManager.getFastForwardMultiplier());
}

@Override
Expand Down Expand Up @@ -301,6 +304,8 @@ public void onClick(DialogInterface dialog, int id) {

public static native void endGame();

public static native void setFastForwardMultiplier(int m);

protected String[] getArguments() {
return getIntent().getStringArrayExtra(TAG_COMMAND_LINE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ enum SettingsEnum {
LAYOUT_SIZE("PREF_SIZE_EVERY_BUTTONS"),
MAIN_DIRECTORY("PREF_DIRECTORY"),
GAMES_DIRECTORY("PREF_GAME_DIRECTORIES"),
FORCED_LANDSCAPE("PREF_FORCED_LANDSCAPE");
FORCED_LANDSCAPE("PREF_FORCED_LANDSCAPE"),
FAST_FORWARD_MODE("FAST_FORWARD_MODE"),
FAST_FORWARD_MULTIPLIER("FAST_FORWARD_MULTIPLIER");

private String label;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatSpinner;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
Expand All @@ -24,11 +27,12 @@

public class SettingsInputActivity extends AppCompatActivity implements View.OnClickListener {
private CheckBox enableVibrationCheckBox, enableVibrateWhenSlidingCheckbox, ignoreLayoutSizeCheckbox;
private AppCompatSpinner chooseFastForwardModeSpinner;
private ButtonMappingManager buttonMappingManager;
private Button addInputLayoutButton;
private LinearLayout inputLayoutList;
private SeekBar layoutTransparencyLayout, layoutSizeSeekBar;
private TextView layoutTransparencyTextView, layoutSizeTextView;
private SeekBar layoutTransparencyLayout, layoutSizeSeekBar, fastForwardMultiplierSeekBar;
private TextView layoutTransparencyTextView, layoutSizeTextView, fastForwardMultiplierTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -48,6 +52,7 @@ public void onCreate(Bundle savedInstanceState) {
enableVibrateWhenSlidingCheckbox.setChecked(SettingsManager.isVibrateWhenSlidingDirectionEnabled());
enableVibrateWhenSlidingCheckbox.setOnClickListener(this);

configureFastForwardButton();
configureLayoutTransparencySystem();
configureLayoutSizeSystem();
updateInputLayoutList();
Expand All @@ -73,6 +78,46 @@ public void onClick(View v) {
}
}

private void configureFastForwardButton() {
chooseFastForwardModeSpinner = (AppCompatSpinner) findViewById(R.id.settings_fast_forward_mode);
chooseFastForwardModeSpinner.setSelection(SettingsManager.getFastForwardMode());
chooseFastForwardModeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
SettingsManager.setFastForwardMode(i);
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

fastForwardMultiplierSeekBar = (SeekBar) findViewById(R.id.settings_fast_forward_multiplier);
fastForwardMultiplierSeekBar.setProgress(SettingsManager.getFastForwardMultiplier() - 2);
fastForwardMultiplierSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// The seekbar has values 0-8, we want 2-10
SettingsManager.setFastForwardMultiplier(seekBar.getProgress() + 2);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
fastForwardMultiplierTextView.setText(getString(R.string.fast_forward_factor) + " " + (fastForwardMultiplierSeekBar.getProgress() + 2) + "x");
}
});

// The textview displays the current multiplier value
fastForwardMultiplierTextView = (TextView) findViewById(R.id.settings_fast_forward_multiplier_text_view);
fastForwardMultiplierTextView.setText(getString(R.string.fast_forward_factor) + " " + (fastForwardMultiplierSeekBar.getProgress() + 2) + "x");
}

private void configureLayoutTransparencySystem() {
// The seekbar permit to modify this value
layoutTransparencyLayout = (SeekBar) findViewById(R.id.settings_layout_transparency);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import java.util.List;

import static org.easyrpg.player.settings.SettingsEnum.AUDIO_ENABLED;
import static org.easyrpg.player.settings.SettingsEnum.FAST_FORWARD_MODE;
import static org.easyrpg.player.settings.SettingsEnum.FAST_FORWARD_MULTIPLIER;
import static org.easyrpg.player.settings.SettingsEnum.FORCED_LANDSCAPE;
import static org.easyrpg.player.settings.SettingsEnum.GAMES_DIRECTORY;
import static org.easyrpg.player.settings.SettingsEnum.IGNORE_LAYOUT_SIZE_SETTINGS;
Expand All @@ -30,9 +32,12 @@ public class SettingsManager {
private static SharedPreferences.Editor editor;
private static Context context;

private static boolean vibrationEnabled, vibrateWhenSlidingDirectionEnabled,
audioEnabled, ignoreLayoutSizePreferencesEnabled, forcedLandscape;
private static int layoutTransparency, layoutSize;
private static boolean vibrationEnabled;
private static boolean vibrateWhenSlidingDirectionEnabled;
private static boolean audioEnabled;
private static boolean ignoreLayoutSizePreferencesEnabled;
private static boolean forcedLandscape;
private static int layoutTransparency, layoutSize, fastForwardMode, fastForwardMultiplier;
private static String easyRPGFolder;
private static List<String> gamesFolderList = new ArrayList<>();

Expand All @@ -59,6 +64,8 @@ private static void loadSettings() {
easyRPGFolder = sharedPref.getString(MAIN_DIRECTORY.toString(),
Environment.getExternalStorageDirectory().getPath() + "/easyrpg");
forcedLandscape = sharedPref.getBoolean(FORCED_LANDSCAPE.toString(), false);
fastForwardMode = sharedPref.getInt(FAST_FORWARD_MODE.toString(), 0);
fastForwardMultiplier = sharedPref.getInt(FAST_FORWARD_MULTIPLIER.toString(), 3);

// Fetch the games directories :
gamesFolderList = new ArrayList<>();
Expand Down Expand Up @@ -139,6 +146,26 @@ public static void setVibrationEnabled(boolean b) {
editor.commit();
}

public static int getFastForwardMode() {
return fastForwardMode;
}

public static void setFastForwardMode(int i) {
fastForwardMode = i;
editor.putInt(SettingsEnum.FAST_FORWARD_MODE.toString(), i);
editor.commit();
}

public static int getFastForwardMultiplier() {
return fastForwardMultiplier;
}

public static void setFastForwardMultiplier(int i) {
fastForwardMultiplier = i;
editor.putInt(SettingsEnum.FAST_FORWARD_MULTIPLIER.toString(), i);
editor.commit();
}

public static boolean isVibrateWhenSlidingDirectionEnabled() {
return vibrateWhenSlidingDirectionEnabled;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/

#include "org_easyrpg_player_player_EasyRpgPlayerActivity.h"
#include "graphics.h"
#include "player.h"

#ifdef __cplusplus
Expand All @@ -42,6 +41,12 @@ JNIEXPORT void JNICALL Java_org_easyrpg_player_player_EasyRpgPlayerActivity_endG
Player::exit_flag = true;
}

JNIEXPORT void JNICALL Java_org_easyrpg_player_player_EasyRpgPlayerActivity_setFastForwardMultiplier
(JNIEnv *, jclass, jint ji)
{
Player::speed_modifier = ji;
}

#ifdef __cplusplus
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 143a5c7

Please sign in to comment.