Untitled

 avatar
unknown
plain_text
2 years ago
5.3 kB
2
Indexable
package ru.spbstu.king_game

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Size
import android.view.MotionEvent
import android.view.View
import androidx.core.content.ContextCompat


class CardView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
    defStyleRes: Int = 0
) : View(context, attrs, defStyleAttr, defStyleRes) {

    val cardOffset = 40

    // 280 320
    val closedCardSize = Size(200, 240)
    val openedCardSize = Size(200, 280)
    val topBounds = Rect(300, 60, 300 + closedCardSize.width, 60 + closedCardSize.height)
    val leftBounds = Rect(200, 1350, 200 + closedCardSize.width, 1350 + closedCardSize.height)
    val rightBounds = Rect(200, 250, 200 + closedCardSize.width, 250 + closedCardSize.height)
    val bottomBounds = Rect(60, 1500, 60 + openedCardSize.width, 1500 + openedCardSize.height)
    val cardCount = 8
    val closedCardDrawable = ContextCompat.getDrawable(context, R.drawable.closed_card_image)!!
    val openedCardDrawable = ContextCompat.getDrawable(context, R.drawable.valet_card)!!

    val textPaint = Paint().apply {
        color = Color.WHITE
        textSize = 40f
        isAntiAlias = true
    }

    init {
        setBackgroundResource(R.drawable.table_background)
    }

    override fun onDraw(canvas: Canvas) {
        for (i in 0 until cardCount) {
            val offset = cardOffset * i
            closedCardDrawable.setBounds(
                topBounds.left + offset,
                topBounds.top,
                topBounds.right + offset,
                topBounds.bottom
            )
            closedCardDrawable.draw(canvas)
        }

        canvas.save()
        canvas.rotate(90f, width / 2f, height / 2f)
        for (i in 0 until cardCount) {
            val offset = cardOffset * i
            closedCardDrawable.setBounds(
                leftBounds.left + offset,
                leftBounds.top,
                leftBounds.right + offset,
                leftBounds.bottom
            )
            closedCardDrawable.draw(canvas)
        }

        for (i in 0 until cardCount) {
            val offset = cardOffset * i
            closedCardDrawable.setBounds(
                rightBounds.left + offset,
                rightBounds.top,
                rightBounds.right + offset,
                rightBounds.bottom
            )
            closedCardDrawable.draw(canvas)
        }
        canvas.restore()

        val bottomOffset = (width - openedCardSize.width) / cardCount
        for (i in 0 until cardCount) {
            val offset = i * bottomOffset
            openedCardDrawable.setBounds(
                bottomBounds.left + offset,
                bottomBounds.top,
                bottomBounds.right + offset,
                bottomBounds.bottom
            )
            openedCardDrawable.draw(canvas)
        }

        canvas.drawText("Ваш ход", 480f, 1430f, textPaint)
        canvas.drawText("Player 1", 15f, 550f, textPaint)
        canvas.drawText("Player 2", topBounds.left.toFloat() + 20f, topBounds.bottom + 70f, textPaint)
        canvas.drawText("Player 3", width - 150f, 550f, textPaint)

        openedCardDrawable.setBounds(450, 1000 - openedCardSize.height / 2, 450 + openedCardSize.width, 1000 + openedCardSize.height / 2)
        openedCardDrawable.draw(canvas)
        openedCardDrawable.setBounds(450 - openedCardSize.width, 1000 - openedCardSize.height, 450, 1000)
        openedCardDrawable.draw(canvas)
        openedCardDrawable.setBounds(450 + openedCardSize.width, 1000 - openedCardSize.height, 450 + openedCardSize.width * 2, 1000)
        openedCardDrawable.draw(canvas)
        openedCardDrawable.setBounds(450, 1000 - openedCardSize.height / 2 - openedCardSize.height, 450 + openedCardSize.width, 1000 - openedCardSize.height / 2)
        openedCardDrawable.draw(canvas)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        val evX = event.x
        val evY = event.y
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                if (currentStatus === GameStatus.Playing) {
                    // If the touch is within the square
                    if (evX >= player.getX() && evX <= player.getX() + player.getBitmap()
                            .getWidth() && evY >= player.getY() && evY <= player.getY() + player.getBitmap()
                            .getHeight()
                    ) {
                        // Activate the drag mode
                        isDragged = true
                        dragX = evX - player.getX()
                        dragY = evY - player.getY()
                    }
                }
                if (isDragged) {
                    // Defines new coordinates for drawing
                    player.setX(evX - dragX)
                    player.setY(evY - dragY)
                    invalidate()
                }
            }
            MotionEvent.ACTION_MOVE -> if (isDragged) {
                player.setX(evX - dragX)
                player.setY(evY - dragY)
                invalidate()
            }
            MotionEvent.ACTION_UP -> isDragged = false
        }
        return true
    }
}
Editor is loading...