All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.bselzer.gw2.v2.model.tile.response.Tile.kt Maven / Gradle / Ivy

There is a newer version: 3.11.0
Show newest version
package com.bselzer.gw2.v2.model.tile.response

import com.bselzer.gw2.v2.model.tile.position.GridPosition
import com.bselzer.gw2.v2.model.tile.position.TexturePosition
import com.bselzer.gw2.v2.model.tile.request.TileRequest
import com.bselzer.ktx.geometry.dimension.bi.Dimension2D
import com.bselzer.ktx.geometry.dimension.bi.position.Point2D
import com.bselzer.ktx.serialization.serializer.Dimension2DSerializer
import kotlinx.serialization.Serializable

/**
 * Represents a tile within the grid.
 */
@Serializable
data class Tile(
    /**
     * The relative position within the grid.
     */
    val gridPosition: GridPosition,

    /**
     * The width and height.
     */
    @Serializable(with = Dimension2DSerializer::class)
    val size: Dimension2D,

    /**
     * The zoom level.
     */
    val zoom: Int,

    /**
     * The image content.
     */
    val content: ByteArray = ByteArray(0)
) {
    /**
     * Creates a new instance using the data from the [request].
     */
    constructor(
        request: TileRequest,
        content: ByteArray = ByteArray(0)
    ) : this(
        gridPosition = request.gridPosition,
        size = request.size,
        zoom = request.zoom,
        content = content
    )

    /**
     * The absolute position within the texture.
     */
    val texturePosition: TexturePosition = TexturePosition(
        value = Point2D(
            x = gridPosition.x * size.width,
            y = gridPosition.y * size.height
        )
    )

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || this::class != other::class) return false

        other as Tile

        if (gridPosition != other.gridPosition) return false
        if (zoom != other.zoom) return false

        return true
    }

    override fun hashCode(): Int {
        var result = gridPosition.hashCode()
        result = 31 * result + zoom
        return result
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy