Add icons, set layout, add end dialog

This commit is contained in:
adrienmalin 2018-08-14 02:58:12 +02:00
parent 09c7fef71c
commit 8387482e45
18 changed files with 280 additions and 139 deletions

65
.idea/assetWizardSettings.xml generated Normal file
View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="WizardSettings">
<option name="children">
<map>
<entry key="imageWizard">
<value>
<PersistentState>
<option name="children">
<map>
<entry key="imageAssetPanel">
<value>
<PersistentState>
<option name="children">
<map>
<entry key="actionbar">
<value>
<PersistentState>
<option name="values">
<map>
<entry key="assetType" value="IMAGE" />
<entry key="imageAsset" value="C:\Users\adima\Downloads\ping-pong (2).png" />
<entry key="outputName" value="ic_button" />
</map>
</option>
</PersistentState>
</value>
</entry>
<entry key="launcherLegacy">
<value>
<PersistentState>
<option name="values">
<map>
<entry key="assetType" value="IMAGE" />
<entry key="iconShape" value="NONE" />
<entry key="imageAsset" value="C:\Users\adima\Downloads\ping-pong (1).png" />
</map>
</option>
</PersistentState>
</value>
</entry>
</map>
</option>
<option name="values">
<map>
<entry key="outputIconType" value="ACTIONBAR" />
</map>
</option>
</PersistentState>
</value>
</entry>
</map>
</option>
</PersistentState>
</value>
</entry>
<entry key="vectorWizard">
<value>
<PersistentState />
</value>
</entry>
</map>
</option>
</component>
</project>

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -0,0 +1,35 @@
package adrienmalin.pingpoints
import android.content.DialogInterface
import android.app.AlertDialog
import android.app.Dialog
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.DialogFragment
class EndOfMatchDialog: DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(activity)
val winnerName = arguments?.getString("WINNER_NAME")
val winnerScore = arguments?.getInt("WINNER_SCORE")
val loserScore = arguments?.getInt("LOSER_SCORE")
builder.setTitle(getString(R.string.end_match_dialog_title, winnerName))
.setMessage(getString(R.string.score, winnerScore, loserScore))
.setPositiveButton(
R.string.new_match_button,
DialogInterface.OnClickListener {
dialog, id -> startActivity(Intent(context, MainActivity::class.java))
activity?.finish()
}
)
.setNegativeButton(
R.string.quit_button,
DialogInterface.OnClickListener {
dialog, id -> activity?.finish()
}
)
return builder.create()
}
}

View File

@ -2,8 +2,24 @@ package adrienmalin.pingpoints
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.text.Html
import android.view.View import android.view.View
import android.widget.Button import android.widget.Button
import android.os.Build
import android.text.Spanned
import android.text.TextUtils.join
import kotlin.math.abs
@SuppressWarnings("deprecation")
fun fromHtml(html: String): Spanned {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY)
} else {
Html.fromHtml(html)
}
}
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@ -14,16 +30,12 @@ class MainActivity : AppCompatActivity() {
var server: Int = 0 var server: Int = 0
var notServer: Int = 1 var notServer: Int = 1
var buttonPlayers: Array<Button> = emptyArray()
var serviceTexts: Array<Array<String>> = arrayOf(
arrayOf("""_o/°""", ""),
arrayOf("", """°\o_""")
)
var textScore: android.widget.TextView? = null var textScore: android.widget.TextView? = null
var textService: android.widget.TextView? = null
var stringScore:String = "" var stringScore:String = ""
var textService: android.widget.TextView? = null
var stringService:String = "" var stringService:String = ""
var buttons: Array<Button> = emptyArray()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -37,12 +49,7 @@ class MainActivity : AppCompatActivity() {
textScore = findViewById(R.id.textScore) textScore = findViewById(R.id.textScore)
textService = findViewById(R.id.textService) textService = findViewById(R.id.textService)
buttons = arrayOf(
stringScore = getString(R.string.score)
stringService = getString(R.string.service)
buttonPlayers = arrayOf(
findViewById(R.id.buttonPlayer1), findViewById(R.id.buttonPlayer1),
findViewById(R.id.buttonPlayer2) findViewById(R.id.buttonPlayer2)
) )
@ -50,14 +57,23 @@ class MainActivity : AppCompatActivity() {
update_ui() update_ui()
} }
fun updateScore(scoringPlayerId: Int) { fun update_ui() {
players[scoringPlayerId].score ++
if (players.sumBy { it.score } % 2 == 0) { textScore?.text = getString(R.string.score, players[server].score, players[notServer].score)
server = notServer.also { notServer = server }
textService?.text = getString(R.string.service, players[server].name)
for ((button, player) in buttons.zip(players)) {
button.text = fromHtml(getString(R.string.button_text, player.name, player.score))
} }
update_ui() if (server == 0) {
buttons[0].setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_button, 0, 0, 0)
buttons[1].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
} else {
buttons[0].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0)
buttons[1].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_button, 0)
}
} }
fun onClickPlayer1(view: View) { fun onClickPlayer1(view: View) {
@ -68,27 +84,42 @@ class MainActivity : AppCompatActivity() {
updateScore(1) updateScore(1)
} }
fun update_ui(){ fun updateScore(scoringPlayerId: Int) {
players[scoringPlayerId].score++
textScore?.text = "$stringScore ${players[server].score} - ${players[notServer].score}" if (players.sumBy { it.score } % 2 == 0) {
server = notServer.also { notServer = server }
textService?.text = "$stringService ${players[server].name}"
for ((player, serviceText) in players.zip(serviceTexts[server])) {
player.serviceText = serviceText
} }
for ((button, player) in buttonPlayers.zip(players)) { update_ui()
button.text = """
|${player.name} if (
|${player.score} (players.map { it -> it.score } .max() ?: 0 >= 11) and
|${player.serviceText}""".trimMargin() (abs(players[0].score - players[1].score) >= 2)
) {
endOfMatch()
} }
} }
fun endOfMatch() {
val (loser, winner) = players.sortedBy { it.score }
var endOfMatchDialog: EndOfMatchDialog = EndOfMatchDialog()
val bundle = Bundle()
bundle.putString("WINNER_NAME", winner.name)
bundle.putInt("WINNER_SCORE", winner.score)
bundle.putInt("LOSER_SCORE", loser.score)
endOfMatchDialog.arguments = bundle
endOfMatchDialog.show(
supportFragmentManager,
join(" ", arrayOf(winner.name, winner.score.toString(), "-", loser.name, loser.score.toString()))
)
}
} }
class Player( data class Player(
var name: String = "", var name: String = "",
var score: Int = 0, var score: Int = 0,
var serviceText: String = "" var serviceText: String = ""

View File

@ -7,7 +7,7 @@ import android.app.Dialog
import android.content.DialogInterface import android.content.DialogInterface
class Dialog : DialogFragment() { class StarterNameDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Use the Builder class for convenient dialog construction // Use the Builder class for convenient dialog construction
val builder = AlertDialog.Builder(activity) val builder = AlertDialog.Builder(activity)
@ -19,7 +19,7 @@ class Dialog : DialogFragment() {
.setNegativeButton(R.string.quit, DialogInterface.OnClickListener { dialog, id -> .setNegativeButton(R.string.quit, DialogInterface.OnClickListener { dialog, id ->
// User cancelled the dialog // User cancelled the dialog
}) })
.setView(view) .setView(R.layout.dialog)
// Create the AlertDialog object and return it // Create the AlertDialog object and return it
return builder.create() return builder.create()
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1005 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 665 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

View File

@ -7,56 +7,54 @@
tools:context=".MainActivity" tools:context=".MainActivity"
tools:layout_editor_absoluteY="73dp"> tools:layout_editor_absoluteY="73dp">
<TextView <LinearLayout
android:id="@+id/textService" android:id="@+id/linearLayoutText"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginRight="8dp" android:layout_marginRight="8dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:text="@string/service" android:orientation="horizontal"
android:textAppearance="@android:style/TextAppearance.Material.Large"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textScore" /> app:layout_constraintTop_toTopOf="parent">
<TextView <TextView
android:id="@+id/textScore" android:id="@+id/textScore"
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_weight="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="@string/score" android:text="@string/score"
android:textAppearance="@android:style/TextAppearance.Material.Large" android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="18dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:text="@string/service"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toTopOf="@+id/linearLayoutButtons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textScore"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
<LinearLayout <LinearLayout
android:id="@+id/linearLayout" android:id="@+id/linearLayoutButtons"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="0dp" android:layout_height="0dp"
android:layout_marginBottom="8dp" android:layout_marginBottom="8dp"
@ -69,7 +67,7 @@
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textService"> app:layout_constraintTop_toBottomOf="@+id/linearLayoutText">
<Button <Button
android:id="@+id/buttonPlayer1" android:id="@+id/buttonPlayer1"
@ -83,9 +81,11 @@
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_weight="1" android:layout_weight="1"
android:bufferType="spannable"
android:onClick="onClickPlayer1" android:onClick="onClickPlayer1"
android:text="Button" android:text="Button"
android:textAllCaps="false" android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textSize="24sp" android:textSize="24sp"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textScore" app:layout_constraintBottom_toTopOf="@+id/textScore"
@ -106,9 +106,11 @@
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" android:layout_marginTop="8dp"
android:layout_weight="1" android:layout_weight="1"
android:bufferType="spannable"
android:onClick="onClickPlayer2" android:onClick="onClickPlayer2"
android:text="Button" android:text="Button"
android:textAllCaps="false" android:textAllCaps="false"
android:textAppearance="@style/TextAppearance.AppCompat.Button"
android:textSize="24sp" android:textSize="24sp"
android:textStyle="bold" android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/textScore" app:layout_constraintBottom_toTopOf="@+id/textScore"
@ -116,4 +118,5 @@
app:layout_constraintStart_toEndOf="@+id/buttonPlayer1" app:layout_constraintStart_toEndOf="@+id/buttonPlayer1"
app:layout_constraintTop_toBottomOf="@+id/textService" /> app:layout_constraintTop_toBottomOf="@+id/textService" />
</LinearLayout> </LinearLayout>
</android.support.constraint.ConstraintLayout> </android.support.constraint.ConstraintLayout>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 6.3 KiB

After

Width:  |  Height:  |  Size: 7.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.0 KiB

After

Width:  |  Height:  |  Size: 11 KiB

View File

@ -5,11 +5,14 @@
<string name="dialog_title">Nouvelle partie</string> <string name="dialog_title">Nouvelle partie</string>
<string name="dialog_message">Qui commence ?</string> <string name="dialog_message">Qui commence ?</string>
<string name="info">Cliquez sur le joueur qui a marqué</string> <string name="info">Cliquez sur le joueur qui a marqué</string>
<string name="service">Service :</string> <string name="service">Service : %1s</string>
<string name="score">Score :</string> <string name="score">Score : %1d - %2d</string>
<string name="quit">Quitter</string> <string name="quit">Quitter</string>
<string-array name="players_names"> <string-array name="players_names">
<item>Joueur 1</item> <item>Joueur 1</item>
<item>Joueur 2</item> <item>Joueur 2</item>
</string-array> </string-array>
<string name="end_match_dialog_title">Bravo %1s !</string>
<string name="new_match_button">Nouvelle partie</string>
<string name="quit_button">Quitter</string>
</resources> </resources>

View File

@ -5,10 +5,14 @@
<string name="dialog_title">New match</string> <string name="dialog_title">New match</string>
<string name="dialog_message">Who starts?</string> <string name="dialog_message">Who starts?</string>
<string name="info">Click on the scoring player</string> <string name="info">Click on the scoring player</string>
<string name="service">Service:</string> <string name="service">Service: %1s</string>
<string name="score">Score:</string> <string name="score">Score: %1d - %2d</string>
<string name="button_text" translatable="false">%1s &lt;br /&gt; &lt;big&gt; &lt;big&gt; %2d &lt;/big&gt; &lt;/big&gt;</string>
<string name="end_match_dialog_title">Congratulations, %1s!</string>
<string-array name="players_names"> <string-array name="players_names">
<item>Player 1</item> <item>Player 1</item>
<item>Player 2</item> <item>Player 2</item>
</string-array> </string-array>
<string name="new_match_button">New match</string>
<string name="quit_button">Quit</string>
</resources> </resources>