Skip to content

Commit

Permalink
Endgame (#27)
Browse files Browse the repository at this point in the history
* Fix Tab not resizing

* Fix player board custom name recognition

* Complete logic for triggering and managing EndGame screen

* Clean up End Game screen

* Add End Game button to scoreboard and load button to end game screen
  • Loading branch information
askariya authored Aug 14, 2024
1 parent 1f9a7d1 commit e9f3800
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 35 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name">
android:screenOrientation="portrait"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class MainActivity : AppCompatActivity() {
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
setHeaderToolbar(position)
setHeaderToolbarColor(position)
}
})
}
Expand All @@ -92,6 +92,13 @@ class MainActivity : AppCompatActivity() {
return true
}

override fun onPrepareOptionsMenu(menu: Menu): Boolean {
val submitButton = menu.findItem(R.id.submit_button)
submitButton.isVisible = !(GameStateManager.gameOver.value ?: false)
return super.onPrepareOptionsMenu(menu)
}


// Handle toolbar button clicks
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
Expand Down Expand Up @@ -119,7 +126,7 @@ class MainActivity : AppCompatActivity() {
}
}

private fun setHeaderToolbar(position: Int)
private fun setHeaderToolbarColor(position: Int)
{
when (position) {
0 -> binding.headerToolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.player1_colour))
Expand All @@ -133,7 +140,6 @@ class MainActivity : AppCompatActivity() {
supportFragmentManager.beginTransaction()
.replace(R.id.endgame_fragment_container, endGameFragment)
.commit()

// Hide ViewPager2 and TabLayout
viewPager.visibility = View.GONE
tabLayout.visibility = View.GONE
Expand Down Expand Up @@ -191,10 +197,13 @@ class MainActivity : AppCompatActivity() {
if (gameOver) {
showEndGameFragment()
updateActionBarTitle("Game Over")
//TODO Hide the save button
setHeaderToolbarColor(2)
invalidateOptionsMenu()
}
else {
hideEndGameFragment()
viewPager.currentItem = 0
invalidateOptionsMenu()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,13 @@ class PlayerBoardFragment : Fragment() {
}

private fun onResetButtonPressed() {
val currentPlayer = if (playerId == 1) GameStateManager.player1Name.value
else GameStateManager.player2Name.value
// Only prompt if the board has been modified.
if (viewModel.hasBoardBeenModified) {
val message = """
Are you sure you want to reset Player $playerId's board?<br><br>
<i>All selected buttons will be unselected and Player $playerId's
Are you sure you want to reset $currentPlayer's board?<br><br>
<i>All selected buttons will be unselected and $currentPlayer's
current score will be reset.</i>
""".trimIndent()
DialogUtils.showConfirmationDialog(requireContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.example.lostcitiesscorecalculator.R
import com.example.lostcitiesscorecalculator.databinding.FragmentEndGameBinding
import com.example.lostcitiesscorecalculator.databinding.FragmentScoreboardBinding
import com.example.lostcitiesscorecalculator.ui.utils.GameStateManager

class EndGameFragment : Fragment() {
Expand All @@ -25,16 +25,79 @@ class EndGameFragment : Fragment() {
_binding = FragmentEndGameBinding.inflate(inflater, container, false)
val root: View = binding.root

handleWinner()

val fragment = ScoreboardFragment() // Replace with your fragment class
childFragmentManager.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit()

// set the restartButton functionality
val restartGameButton : Button = binding.restartGameButton
restartGameButton.setOnClickListener{
onRestartGameButtonPressed()
}

// set the reloadButton functionality
val reloadGameButton : Button = binding.reloadGameButton
reloadGameButton.setOnClickListener{
onReloadGameButtonPressed()
}

return root
}

private fun handleWinner(){
val gameResult = getWinnerIndex()
when (gameResult) {
0 ->
{
setHeaderColor(R.color.player1_colour)
setFooterColor(R.color.player1_colour)
setHeaderText("${GameStateManager.player1Name.value} Wins")
}
1 ->
{
setHeaderColor(R.color.player2_colour)
setFooterColor(R.color.player2_colour)
setHeaderText("${GameStateManager.player2Name.value} Wins")
}
else -> {
setHeaderColor(R.color.color_secondary)
setFooterColor(R.color.color_secondary)
setHeaderText("Draw")
}
}
}

private fun getWinnerIndex(): Int
{
val player1Pts = GameStateManager.player1TotalPoints.value ?: 0
val player2Pts = GameStateManager.player2TotalPoints.value ?: 0

return if (player1Pts > player2Pts ) 0
else if (player2Pts > player1Pts) 1
else 2
}

private fun setHeaderColor(color: Int)
{
binding.endGameMessage.setBackgroundColor(ContextCompat.getColor(requireContext(), color))
}
private fun setFooterColor(color: Int)
{
binding.endGameFooter.setBackgroundColor(ContextCompat.getColor(requireContext(), color))
}

private fun setHeaderText(text: String)
{
binding.endGameMessage.text = text
}

private fun onRestartGameButtonPressed() {
GameStateManager.restartGame(requireContext())
}
private fun onReloadGameButtonPressed() {
GameStateManager.loadGame(requireContext())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.example.lostcitiesscorecalculator.LostCitiesScoreCalculatorApplication
import com.example.lostcitiesscorecalculator.R
import com.example.lostcitiesscorecalculator.databinding.FragmentScoreboardBinding
import com.example.lostcitiesscorecalculator.ui.utils.DialogUtils
import com.example.lostcitiesscorecalculator.ui.utils.GameStateManager

class ScoreboardFragment : Fragment() {
Expand Down Expand Up @@ -48,13 +48,19 @@ class ScoreboardFragment : Fragment() {
onRestartGameButtonPressed()
}

// set the endGameButton functionality
val endGameButton : Button = binding.endGameButton
endGameButton.setOnClickListener{
onEndGameButtonPressed()
}

GameStateManager.gameOver.observe(viewLifecycleOwner, endGameObserver)
GameStateManager.player1CurrentPoints.observe(viewLifecycleOwner, player1CurrentScoreObserver)
GameStateManager.player2CurrentPoints.observe(viewLifecycleOwner, player2CurrentScoreObserver)
GameStateManager.player1TotalPoints.observe(viewLifecycleOwner, player1TotalScoreObserver)
GameStateManager.player2TotalPoints.observe(viewLifecycleOwner, player2TotalScoreObserver)
GameStateManager.roundScores.observe(viewLifecycleOwner, roundScoreObserver)
GameStateManager.roundScores.observe(viewLifecycleOwner, roundScoreObserver)

GameStateManager.player1Name.observe(viewLifecycleOwner, player1NameObserver)
GameStateManager.player2Name.observe(viewLifecycleOwner, player2NameObserver)

Expand All @@ -72,6 +78,20 @@ class ScoreboardFragment : Fragment() {
private fun onRestartGameButtonPressed() {
GameStateManager.restartGame(requireContext())
}
private fun onEndGameButtonPressed() {
val message = """
Are you sure you want to end the game?<br><br>
<i>All player scores and round history will be finalized.</i>
""".trimIndent()
DialogUtils.showConfirmationDialog(requireContext(),
"End Game",
message,
"Yes",
"No")
{
GameStateManager.endGame(requireContext())
}
}

private fun addNewRound(roundCount: Int, player1RoundScore: Int, player2RoundScore: Int) {
// Create TextView for Round number
Expand Down Expand Up @@ -105,6 +125,7 @@ class ScoreboardFragment : Fragment() {
setTextColor(textColor)
setBackgroundColor(colorPrimaryVariant)
setTypeface(typeface, android.graphics.Typeface.BOLD)
setPadding(12,12,12,12)
gravity = android.view.Gravity.CENTER
}

Expand All @@ -120,6 +141,7 @@ class ScoreboardFragment : Fragment() {
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize)
setTextColor(textColor)
setBackgroundColor(backgroundColor)
setPadding(12,12,12,12)
gravity = android.view.Gravity.CENTER
}

Expand All @@ -135,6 +157,7 @@ class ScoreboardFragment : Fragment() {
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize)
setTextColor(textColor)
setBackgroundColor(backgroundColor)
setPadding(12,12,12,12)
gravity = android.view.Gravity.CENTER
}

Expand All @@ -151,6 +174,35 @@ class ScoreboardFragment : Fragment() {
binding.emptyView.visibility = if (binding.scoreGridLayout.childCount <= 0) View.VISIBLE else View.GONE
}

private val endGameObserver = Observer<Boolean> { gameOver ->
if (gameOver)
{
binding.scoreBoardContent.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.dark_grey))
binding.scoreCurrentHeader.visibility = View.GONE
binding.scoreboardFooter.visibility = View.GONE
binding.totalScoreLabel.text = getString(R.string.final_label)
binding.totalScoreLabel.textSize = 30F
binding.totalScoreLabel.setPadding(15,15,15,15)
binding.player1TotalScoreView.textSize = 30F
binding.player1TotalScoreView.setPadding(15,15,15,15)
binding.player2TotalScoreView.textSize = 30F
binding.player2TotalScoreView.setPadding(15,15,15,15)
}
else
{
binding.scoreBoardContent.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.background_color_dark))
binding.scoreCurrentHeader.visibility = View.VISIBLE
binding.scoreboardFooter.visibility = View.VISIBLE
binding.totalScoreLabel.text = getString(R.string.total_label)
binding.totalScoreLabel.textSize = 20F
binding.totalScoreLabel.setPadding(10,10,10,10)
binding.player1TotalScoreView.textSize = 20F
binding.player1TotalScoreView.setPadding(10,10,10,10)
binding.player2TotalScoreView.textSize = 20F
binding.player2TotalScoreView.setPadding(10,10,10,10)
}
}

private val player1CurrentScoreObserver = Observer<Int> { score ->
val player1Score = binding.player1CurrentScoreView
player1Score.text = score.toString()
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="?attr/actionBarSize"
app:layout_constraintTop_toBottomOf="@id/header_toolbar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabMaxWidth="0dp"
style="@style/CustomTabLayoutStyle"
app:tabBackground="@drawable/tab_colour_selector"/>

Expand Down
56 changes: 44 additions & 12 deletions app/src/main/res/layout/fragment_end_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,56 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.scoreboard.EndGameFragment"
android:background="@color/background_color_dark">
android:background="@color/dark_grey">
<LinearLayout
android:id="@+id/end_game_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:background="@color/dark_red"
android:id="@+id/end_game_message"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/label_endgame_title"
android:textSize="40sp"
android:textColor="@android:color/black" />
android:textSize="35sp"
android:textColor="@android:color/white" />
</LinearLayout>

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/end_game_header"
app:layout_constraintBottom_toTopOf="@id/end_game_footer"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout="@layout/fragment_scoreboard"
android:layout_marginBottom="50dp"/>

<GridLayout
android:id="@+id/end_game_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:padding="5dp">
<Button
android:id="@+id/reload_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="@string/label_load_game"
android:textSize="20sp"
android:backgroundTint="@color/dark_grey"
style="@style/StandardButtonStyle"
android:layout_gravity="start"/>
<Button
android:id="@+id/restart_game_button"
android:layout_width="wrap_content"
Expand All @@ -29,7 +62,6 @@
android:textSize="20sp"
android:backgroundTint="@color/dark_red"
style="@style/StandardButtonStyle"
android:layout_gravity="center"/>
</LinearLayout>

android:layout_gravity="end"/>
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Loading

0 comments on commit e9f3800

Please sign in to comment.