From a2455af788bf1c155ad818020cba70094ba099e1 Mon Sep 17 00:00:00 2001 From: Felix Kramer Date: Tue, 18 Sep 2018 17:09:37 +0200 Subject: [PATCH] - Main game layout updated - User can now choose what was announced in an extra "window" - Choosing activity adapts itself based on the choices made by the user - After confirming the announcement is shown on the main game layout - Some comments added for better documentation --- app/src/main/AndroidManifest.xml | 7 +- .../schafkopfzaehler_2/ChooseGame.java | 293 ++++++++++++++++++ .../schafkopfzaehler_2/MainGameActivity.java | 58 +++- .../schafkopfzaehler_2/StartActivity.java | 16 +- app/src/main/res/layout/choosegame.xml | 246 +++++++++++++++ app/src/main/res/layout/main_game_layout.xml | 51 ++- app/src/main/res/values/strings.xml | 22 +- 7 files changed, 657 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/project/schafkopfzaehler_2/ChooseGame.java create mode 100644 app/src/main/res/layout/choosegame.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4471289..f08a855 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -24,7 +24,12 @@ android:name=".MainGameActivity" android:label="@string/title_activity_main_game" android:screenOrientation="portrait" - android:theme="@style/AppTheme.NoActionBar"> + android:theme="@style/AppTheme.NoActionBar" /> + \ No newline at end of file diff --git a/app/src/main/java/project/schafkopfzaehler_2/ChooseGame.java b/app/src/main/java/project/schafkopfzaehler_2/ChooseGame.java new file mode 100644 index 0000000..e7dc58a --- /dev/null +++ b/app/src/main/java/project/schafkopfzaehler_2/ChooseGame.java @@ -0,0 +1,293 @@ +package project.schafkopfzaehler_2; + +import android.content.Intent; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.util.Log; +import android.view.View; +import android.widget.Button; +import android.widget.RadioButton; +import android.widget.RadioGroup; +import android.widget.TextView; + +public class ChooseGame extends AppCompatActivity { + + RadioGroup style, call, soloPlay; // Radio groups init + RadioButton heart; // Heart button init for first invisibility + Button confirmChoice; // Confirm button init + TextView choice; // Choice view init for first choice + String playStyle, playCall, playerName; // Init for choice manipulation + private String names[] = {"", "", "", ""}; // Player names init + + private RadioGroup.OnCheckedChangeListener startChangeListener = new RadioGroup.OnCheckedChangeListener() { + @Override + public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { + if (radioGroup == style) { + playStyle = getPlayStyle(checkedId); + playCall = getPlayCall(call.getCheckedRadioButtonId()); + playerName = getSoloPlayer(soloPlay.getCheckedRadioButtonId()); + } else if (radioGroup == call) { + playStyle = getPlayStyle(style.getCheckedRadioButtonId()); + playCall = getPlayCall(checkedId); + playerName = getSoloPlayer(soloPlay.getCheckedRadioButtonId()); + } else if (radioGroup == soloPlay) { + playStyle = getPlayStyle(style.getCheckedRadioButtonId()); + playCall = getPlayCall(call.getCheckedRadioButtonId()); + playerName = getSoloPlayer(checkedId); + } + + configureChoice(playStyle, playCall, playerName); + + } + }; + + private View.OnClickListener startClickListener = new View.OnClickListener() { + + @Override + public void onClick (View v) { + + if ( v == confirmChoice ) { + // Send choice to the previous activity + // and then finishes the current activity + Intent data = new Intent(); + data.putExtra("choice", choice.getText().toString()); + setResult(RESULT_OK, data); + finish(); + } + + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.choosegame); + + // Get player names from the activity that started this one + Intent intent = getIntent(); + names = intent.getStringArrayExtra("playerNames"); + + // Fill in the player names in the solo choice + soloPlay = findViewById(R.id.soloplay); + for (int i = 0; i < soloPlay.getChildCount(); i++) { + ((RadioButton) soloPlay.getChildAt(i)).setText(names[i]); + } + + // Start click listener on confirm button + confirmChoice = findViewById(R.id.gameChoosed); + confirmChoice.setOnClickListener(startClickListener); + + // Assign variables radio group and start the change listener + style = findViewById(R.id.playstyle); + style.setOnCheckedChangeListener(startChangeListener); + call = findViewById(R.id.calls); + call.setOnCheckedChangeListener(startChangeListener); + soloPlay.setOnCheckedChangeListener(startChangeListener); + + // At first Sauspiel & Eichel is checked, that means that the + // heart can not be called and no solo is played. Therefore + // both options has to be invisible at first + heart = findViewById(R.id.herz); + heart.setVisibility(View.GONE); + TextView titleSolo = findViewById(R.id.title_soloplayer); + titleSolo.setVisibility(View.GONE); + soloPlay.setVisibility(View.GONE); + choice = findViewById(R.id.choice); + choice.setText(getString(R.string.sauspiel1)); + + } + + private String getPlayStyle (int checked) { + + // Certain play styles do not allow a call + // In these cases the call view should be invisible + TextView titleCall = findViewById(R.id.title_playcall); + TextView titleSolo = findViewById(R.id.title_soloplayer); + + // Return value + String returnStyle = ""; + + // Find which radio button is selected + switch (checked) { + case R.id.sauspiel: + // Sauspiel + + // Toggle visibility + titleCall.setVisibility(View.VISIBLE); + titleCall.setText(getString(R.string.call)); + call.setVisibility(View.VISIBLE); + heart.setVisibility(View.GONE); + titleSolo.setVisibility(View.GONE); + soloPlay.setVisibility(View.GONE); + // ----------------- + + Log.d("ChooseGame", "Sauspiel selected"); + + returnStyle = "Sauspiel"; + + break; + + case R.id.wenz: + // Wenz + + // Toggle visibility + titleCall.setVisibility(View.GONE); + call.setVisibility(View.GONE); + titleSolo.setVisibility(View.GONE); + soloPlay.setVisibility(View.GONE); + // ----------------- + + Log.d("ChooseGame", "Wenz selected"); + + returnStyle = "Wenz"; + + break; + + case R.id.solo: + // Solo + + // Toggle visibility + titleCall.setVisibility(View.VISIBLE); + titleCall.setText(getString(R.string.soloCall)); + call.setVisibility(View.VISIBLE); + titleSolo.setVisibility(View.VISIBLE); + soloPlay.setVisibility(View.VISIBLE); + heart.setVisibility(View.VISIBLE); + // ----------------- + + Log.d("ChooseGame", "Solo selected"); + + returnStyle = "Solo"; + + break; + + default: + Log.d("ChooseGame", "!! ERROR - Nothing is selected"); + break; + + } + + return returnStyle; + + } + + private String getPlayCall (int checked) { + + Log.d("ChooseGame", "Call changed"); + + // Return value + String returnCall = ""; + + switch (checked) { + + case R.id.eichel: + + Log.d("ChooseGame", "Eichel selected"); + returnCall = "Eichel"; + + break; + + case R.id.blatt: + + Log.d("ChooseGame", "Blatt selected"); + returnCall = "Blatt"; + + break; + + case R.id.schelle: + + Log.d("ChooseGame", "Schelle selected"); + returnCall = "Schelle"; + + break; + + case R.id.herz: + + Log.d("ChooseGame", "Herz selected"); + returnCall = "Herz"; + + break; + + } + + return returnCall; + } + + private String getSoloPlayer (int checked) { + + Log.d("ChooseGame", "Name changed"); + + // Return value + String name = ""; + + switch (checked) { + + case R.id.player1: + name = ((RadioButton) soloPlay.getChildAt(0)).getText().toString(); + break; + + case R.id.player2: + name = ((RadioButton) soloPlay.getChildAt(1)).getText().toString(); + break; + + case R.id.player3: + name = ((RadioButton) soloPlay.getChildAt(2)).getText().toString(); + break; + + case R.id.player4: + name = ((RadioButton) soloPlay.getChildAt(3)).getText().toString(); + break; + + default: + // Do nothing + break; + } + + return name; + } + + private void configureChoice (String style, String call, String name) { + + if ( style.equals("Wenz") ) { + + choice.setText(style); + + } else if ( style.equals("Sauspiel") ) { + + switch (call) { + case "Eichel": + choice.setText(getString(R.string.sauspiel1)); + break; + case "Blatt": + choice.setText(getString(R.string.sauspiel2)); + break; + case "Schelle": + choice.setText(getString(R.string.sauspiel3)); + break; + } + + } else if ( style.equals("Solo") ) { + + switch (call) { + case "Eichel": + choice.setText(getString(R.string.solo1)); + choice.append(" " + name); + break; + case "Blatt": + choice.setText(getString(R.string.solo2)); + choice.append(" " + name); + break; + case "Schelle": + choice.setText(getString(R.string.solo3)); + choice.append(" " + name); + break; + case "Herz": + choice.setText(getString(R.string.solo4)); + choice.append(" " + name); + break; + } + } + + + } +} diff --git a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java index 6c8f1d8..24645f3 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/MainGameActivity.java @@ -9,10 +9,13 @@ import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.widget.Button; +import android.widget.TextView; public class MainGameActivity extends AppCompatActivity { - private Button p1, p2, p3, p4; + private Button p1, p2, p3, p4, chooseGame; // Button init + private TextView choice; + private String playerNames[] = {"", "", "", ""}; // Player names init private View.OnClickListener startClickListener = new View.OnClickListener() { @@ -21,6 +24,9 @@ public class MainGameActivity extends AppCompatActivity { if (v == p1 || v == p2 || v == p3 || v == p4) { playerButtonClicked(v); } + if (v == chooseGame) { + gameChoosing(); + } } }; @@ -28,29 +34,27 @@ public class MainGameActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("MainGame", "onCreate"); - setContentView(R.layout.main_game_layout); + // Get player names from the activity that started this one Intent intent = getIntent(); - String playerNames[] = intent.getStringArrayExtra("playerNames"); + playerNames = intent.getStringArrayExtra("playerNames"); - Log.d("MainGame", "Find Buttons"); p1 = findViewById(R.id.p1_game); p2 = findViewById(R.id.p2_game); p3 = findViewById(R.id.p3_game); p4 = findViewById(R.id.p4_game); + chooseGame = findViewById(R.id.chooseGame); + choice = findViewById(R.id.announcement); - Log.d("MainGame", "Set player names into buttons"); // Set player names into buttons p1.setText(playerNames[0]); - Log.d("MainGame", "Zero works -> one"); p2.setText(playerNames[1]); - Log.d("MainGame", "one works -> two"); p3.setText(playerNames[2]); - Log.d("MainGame", "Two works -> three"); p4.setText(playerNames[3]); - Log.d("MainGame", "Three works"); + // Start listening if buttons are clicked + chooseGame.setOnClickListener(startClickListener); p1.setOnClickListener(startClickListener); p2.setOnClickListener(startClickListener); p3.setOnClickListener(startClickListener); @@ -58,10 +62,46 @@ public class MainGameActivity extends AppCompatActivity { } + @Override + protected void onPause() { + super.onPause(); + } + + @Override + protected void onResume() { + super.onResume(); + } + + @Override + protected void onRestart() { + super.onRestart(); + } + + @Override + protected void onStart() { + super.onStart(); + } + private void playerButtonClicked (View v) { Log.d("MainGame", v + " clicked..."); } + private void gameChoosing () { + Intent choose = new Intent(this, ChooseGame.class); + choose.putExtra("playerNames", playerNames); + startActivityForResult(choose, 0); + //startActivity(choose); + } + + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode == 0) { + if (resultCode == RESULT_OK) { + String announcement = data.getStringExtra("choice"); + choice.setText(announcement); + } + } + } + } diff --git a/app/src/main/java/project/schafkopfzaehler_2/StartActivity.java b/app/src/main/java/project/schafkopfzaehler_2/StartActivity.java index 6960be1..396aa75 100644 --- a/app/src/main/java/project/schafkopfzaehler_2/StartActivity.java +++ b/app/src/main/java/project/schafkopfzaehler_2/StartActivity.java @@ -10,14 +10,14 @@ import android.widget.EditText; public class StartActivity extends AppCompatActivity { - private Button startButton; - private Button nextButton; - private String players[] = {"", "", "", ""}; + private Button startButton, nextButton; // Button init + private String players[] = {"", "", "", ""}; // Player names array init private View.OnClickListener startClickListener = new View.OnClickListener() { @Override public void onClick (View v) { + // Distinguish between the start button and the next button if (v == startButton) { startButtonClicked(); } @@ -95,12 +95,10 @@ public class StartActivity extends AppCompatActivity { players[3] = player4.getText().toString(); } - Log.d("Start", "nextButtonClicked --> Intent follows"); - - Intent intent = new Intent(this, MainGameActivity.class); - intent.putExtra("playerNames", players); - startActivity(intent); - + // Start main game activity and add the player names to the intent + Intent intent = new Intent(this, MainGameActivity.class); + intent.putExtra("playerNames", players); + startActivity(intent); } diff --git a/app/src/main/res/layout/choosegame.xml b/app/src/main/res/layout/choosegame.xml new file mode 100644 index 0000000..81bb813 --- /dev/null +++ b/app/src/main/res/layout/choosegame.xml @@ -0,0 +1,246 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +