Skip to content

Commit

Permalink
Add arrow buttons for choosing tables, including keyboard support (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
dozingcat authored Oct 22, 2022
1 parent c1a823e commit 2b6debb
Show file tree
Hide file tree
Showing 17 changed files with 181 additions and 93 deletions.
50 changes: 41 additions & 9 deletions app/src/main/java/com/dozingcatsoftware/bouncy/BouncyActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

Expand All @@ -57,10 +58,12 @@ public class BouncyActivity extends Activity {

View buttonPanel;
View highScorePanel;
View selectTableRow;
ImageButton nextTableButton;
ImageButton previousTableButton;
Button startGameButton;
Button resumeGameButton;
Button endGameButton;
Button switchTableButton;
Button aboutButton;
Button preferencesButton;
Button showHighScoreButton;
Expand Down Expand Up @@ -153,11 +156,13 @@ public class BouncyActivity extends Activity {
scoreView.setHighScores(highScores);

buttonPanel = findViewById(R.id.buttonPanel);
selectTableRow = findViewById(R.id.selectTableRow);
highScorePanel = findViewById(R.id.highScorePanel);
nextTableButton = findViewById(R.id.nextTableButton);
previousTableButton = findViewById(R.id.previousTableButton);
startGameButton = findViewById(R.id.startGameButton);
resumeGameButton = findViewById(R.id.resumeGameButton);
endGameButton = findViewById(R.id.endGameButton);
switchTableButton = findViewById(R.id.switchTableButton);
aboutButton = findViewById(R.id.aboutButton);
preferencesButton = findViewById(R.id.preferencesButton);
unlimitedBallsToggle = findViewById(R.id.unlimitedBallsToggle);
Expand All @@ -177,7 +182,7 @@ public class BouncyActivity extends Activity {
// (after checking that the event was within the button bounds). This is likely
// fragile but seems to be working ok.
List<View> allButtons = Arrays.asList(
startGameButton, resumeGameButton, endGameButton, switchTableButton,
nextTableButton, previousTableButton, startGameButton, resumeGameButton, endGameButton,
aboutButton, preferencesButton, unlimitedBallsToggle, showHighScoreButton, hideHighScoreButton);
for (View button : allButtons) {
button.setOnTouchListener((view, motionEvent) -> {
Expand Down Expand Up @@ -275,6 +280,19 @@ public void receivedOrientationValues(float azimuth, float pitch, float roll) {
return true;
}
}
// When showing the main menu, switch tables with the flipper buttons.
if (!field.getGameState().isGameInProgress() && buttonPanel.getVisibility() == View.VISIBLE) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
doPreviousTable(null);
startGameButton.requestFocus();
return true;
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
doNextTable(null);
startGameButton.requestFocus();
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Expand Down Expand Up @@ -315,11 +333,11 @@ void updateButtons() {
}
else {
buttonPanel.setVisibility(View.VISIBLE);
selectTableRow.setVisibility(View.GONE);
unlimitedBallsToggle.setVisibility(View.GONE);
startGameButton.setVisibility(View.GONE);
resumeGameButton.setVisibility(View.VISIBLE);
endGameButton.setVisibility(View.VISIBLE);
switchTableButton.setVisibility(View.GONE);
unlimitedBallsToggle.setVisibility(View.GONE);
resumeGameButton.requestFocus();
}
}
Expand All @@ -334,11 +352,11 @@ else if (highScorePanel.getVisibility() == View.VISIBLE) {
}
else {
buttonPanel.setVisibility(View.VISIBLE);
selectTableRow.setVisibility(View.VISIBLE);
unlimitedBallsToggle.setVisibility(View.VISIBLE);
startGameButton.setVisibility(View.VISIBLE);
resumeGameButton.setVisibility(View.GONE);
endGameButton.setVisibility(View.GONE);
switchTableButton.setVisibility(View.VISIBLE);
unlimitedBallsToggle.setVisibility(View.VISIBLE);
startGameButton.requestFocus();
}
}
Expand Down Expand Up @@ -592,8 +610,8 @@ public void scoreViewClicked(View view) {
}
}

public void doSwitchTable(View view) {
currentLevel = (currentLevel == numberOfLevels) ? 1 : currentLevel + 1;
void switchToTable(int tableNum) {
this.currentLevel = tableNum;
synchronized (field) {
resetFieldForCurrentLevel();
}
Expand All @@ -604,6 +622,20 @@ public void doSwitchTable(View view) {
fieldDriver.resetFrameRate();
}

public void doSwitchTable(View view) {
doNextTable(view);
}

public void doNextTable(View view) {
int nextTableNum = (currentLevel == numberOfLevels) ? 1 : currentLevel + 1;
switchToTable(nextTableNum);
}

public void doPreviousTable(View view) {
int prevTableNum = (currentLevel == 1) ? numberOfLevels : currentLevel - 1;
switchToTable(prevTableNum);
}

void resetFieldForCurrentLevel() {
field.resetForLayoutMap(FieldLayoutReader.layoutMapForLevel(this, currentLevel));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="60dp"
android:tint="#F0F0F0" android:viewportHeight="24"
android:viewportWidth="24" android:width="60dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M14,7l-5,5 5,5V7z"/>
</vector>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:autoMirrored="true" android:height="60dp"
android:tint="#F0F0F0" android:viewportHeight="24"
android:viewportWidth="24" android:width="60dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M10,17l5,-5 -5,-5v10z"/>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
202 changes: 118 additions & 84 deletions app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,97 +29,131 @@
android:layout_height="fill_parent"
android:visibility="gone" />

<TableLayout
android:id="@+id/buttonPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#A448"
android:orientation="vertical"
android:padding="20dp">

<Button
android:id="@+id/startGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="doStartGame"
android:focusableInTouchMode="true"
android:text="@string/start_game_button_label" />

<Button
android:id="@+id/resumeGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="doStartGame"
android:focusableInTouchMode="true"
android:text="@string/resume_game_button_label"
android:visibility="gone" />

<Button
android:id="@+id/endGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="doEndGame"
android:focusableInTouchMode="true"
android:text="@string/end_game_button_label"
android:visibility="gone" />

<Button
android:id="@+id/highScoreButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="showHighScore"
android:focusableInTouchMode="true"
android:text="@string/show_high_score_button_label" />

<Button
android:id="@+id/switchTableButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:maxLines="1"
android:onClick="doSwitchTable"
android:focusableInTouchMode="true"
android:text="@string/change_table_button_label" />

<Button
android:id="@+id/aboutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:onClick="doAbout"
android:focusableInTouchMode="true"
android:text="@string/help_button_label" />

<Button
android:id="@+id/preferencesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:onClick="doPreferences"
android:focusableInTouchMode="true"
android:text="@string/preferences_button_label" />

<CheckBox
android:id="@+id/unlimitedBallsToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:text="@string/unlimited_balls_label" />
</TableLayout>
<LinearLayout
android:id="@+id/buttonPanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:background="@layout/rounded_border"
android:orientation="vertical"
android:paddingVertical="32dp"
android:paddingHorizontal="16dp">

<LinearLayout
android:id="@+id/selectTableRow"
android:orientation="horizontal"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<ImageButton
android:id="@+id/previousTableButton"
android:src="@drawable/ic_baseline_arrow_left_60"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doPreviousTable"
android:focusableInTouchMode="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="#F0F0F0"
android:text="Select table" />
<ImageButton
android:id="@+id/nextTableButton"
android:src="@drawable/ic_baseline_arrow_right_60"
android:background="#0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doNextTable"
android:focusableInTouchMode="true" />
</LinearLayout>

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginHorizontal="16dp"
android:orientation="vertical">

<CheckBox
android:id="@+id/unlimitedBallsToggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5px"
android:focusableInTouchMode="true"
android:text="@string/unlimited_balls_label" />

<Button
android:id="@+id/startGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:onClick="doStartGame"
android:focusableInTouchMode="true"
android:text="@string/start_game_button_label" />

<Button
android:id="@+id/resumeGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="doStartGame"
android:focusableInTouchMode="true"
android:text="@string/resume_game_button_label"
android:visibility="gone" />

<Button
android:id="@+id/endGameButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="doEndGame"
android:focusableInTouchMode="true"
android:text="@string/end_game_button_label"
android:visibility="gone" />

<Button
android:id="@+id/highScoreButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:onClick="showHighScore"
android:focusableInTouchMode="true"
android:text="@string/show_high_score_button_label" />

<Button
android:id="@+id/aboutButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:onClick="doAbout"
android:focusableInTouchMode="true"
android:text="@string/help_button_label" />

<Button
android:id="@+id/preferencesButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10sp"
android:onClick="doPreferences"
android:focusableInTouchMode="true"
android:text="@string/preferences_button_label" />
</TableLayout>
</LinearLayout>

<TableLayout
android:id="@+id/highScorePanel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#A448"
android:background="@layout/rounded_border"
android:orientation="vertical"
android:padding="20dp"
android:padding="32dp"
android:visibility="gone">

<TextView
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/rounded_border.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#A448"/>
<corners android:radius="20dp" />
<stroke
android:width="2dp"
android:color="#3bbdfa"
/>
</shape>
</item>
</selector>

0 comments on commit 2b6debb

Please sign in to comment.