ktx.style.style.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ktx-style Show documentation
Show all versions of ktx-style Show documentation
libGDX Scene2D GUI style building utilities for Kotlin applications.
The newest version!
package ktx.style
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.CheckBox.CheckBoxStyle
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton.ImageButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.ImageTextButton.ImageTextButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle
import com.badlogic.gdx.scenes.scene2d.ui.List.ListStyle
import com.badlogic.gdx.scenes.scene2d.ui.ProgressBar.ProgressBarStyle
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane.ScrollPaneStyle
import com.badlogic.gdx.scenes.scene2d.ui.SelectBox.SelectBoxStyle
import com.badlogic.gdx.scenes.scene2d.ui.Skin
import com.badlogic.gdx.scenes.scene2d.ui.Slider.SliderStyle
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane.SplitPaneStyle
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle
import com.badlogic.gdx.scenes.scene2d.ui.TextField.TextFieldStyle
import com.badlogic.gdx.scenes.scene2d.ui.TextTooltip.TextTooltipStyle
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad.TouchpadStyle
import com.badlogic.gdx.scenes.scene2d.ui.Tree.TreeStyle
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle
import com.badlogic.gdx.utils.GdxRuntimeException
import com.badlogic.gdx.utils.ObjectMap
import kotlin.annotation.AnnotationTarget.CLASS
import kotlin.annotation.AnnotationTarget.FUNCTION
import kotlin.annotation.AnnotationTarget.TYPE
import kotlin.annotation.AnnotationTarget.TYPEALIAS
import kotlin.annotation.AnnotationTarget.TYPE_PARAMETER
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
/** Should annotate builder methods of Scene2D [Skin]. */
@DslMarker
@Target(CLASS, TYPE_PARAMETER, FUNCTION, TYPE, TYPEALIAS)
annotation class SkinDsl
/**
* Name of default resources in [Skin]. Often used as default Scene2D actor style names.
*/
const val defaultStyle = "default"
/**
* @param init will be applied to the [Skin] instance. Inlined.
* @return a new instance of [Skin].
*/
@SkinDsl
@OptIn(ExperimentalContracts::class)
inline fun skin(init: (@SkinDsl Skin).(Skin) -> Unit = {}): Skin {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
val skin = Skin()
skin.init(skin)
return skin
}
/**
* @param atlas will be disposed along with the [Skin].
* @param init will be applied to the [Skin] instance. Inlined.
* @return a new instance of [Skin].
*/
@OptIn(ExperimentalContracts::class)
inline fun skin(atlas: TextureAtlas, init: (@SkinDsl Skin).(Skin) -> Unit = {}): Skin {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
val skin = Skin(atlas)
skin.init(skin)
return skin
}
/**
* Allows to create new UI component styles with the KTX DSL. This method is very similar
* to the [apply] extension method from the standard library, but supports the DSL annotation.
* @param styles will be applied to this [Skin] instance. Inlined.
*/
@OptIn(ExperimentalContracts::class)
inline fun Skin.register(styles: (@SkinDsl Skin).(Skin) -> Unit) {
contract { callsInPlace(styles, InvocationKind.EXACTLY_ONCE) }
styles(this)
}
/**
* Utility function that makes it easier to access [Skin] assets.
* @param name name of the requested resource. Defaults to [defaultStyle].
* @return resource of the specified type with the selected name.
* @throws GdxRuntimeException if unable to find the resource.
*/
inline operator fun Skin.get(name: String = defaultStyle): Resource =
this[name, Resource::class.java]
/**
* Utility function that makes it easier to access [Skin] assets.
* @param name name of the requested resource.
* @return resource of the specified type with the selected name.
* @throws GdxRuntimeException if unable to find the resource.
*/
inline operator fun > Skin.get(name: E): Resource = this[name.toString()]
/**
* Utility function that makes it easier to access [Skin] assets or return null if they don't exist.
* @param name name of the requested resource. Defaults to [defaultStyle].
* @return resource of the specified type with the selected name, or `null` if it doesn't exist.
*/
inline fun Skin.optional(name: String = defaultStyle): Resource? =
this.optional(name, Resource::class.java)
/**
* Utility function that makes it easier to add [Skin] assets.
* @param name name of the passed resource.
* @param resource will be added to the skin and mapped to the selected name.
*/
inline operator fun Skin.set(name: String, resource: Resource) =
this.add(name, resource, Resource::class.java)
/**
* Utility function that makes it easier to add [Skin] assets.
* @param name name of the passed resource.
* @param resource will be added to the skin and mapped to the selected name.
*/
inline operator fun > Skin.set(name: E, resource: Resource) =
this.set(name.toString(), resource)
/**
* Utility function that makes it easier to add [Skin] assets.
* @param name name of the passed resource. Defaults to [defaultStyle].
* @param resource will be added to the skin and mapped to the selected name.
*/
inline fun Skin.add(resource: Resource, name: String = defaultStyle) =
this.add(name, resource, Resource::class.java)
/**
* Utility function that makes it easier to add [Skin] assets under the [defaultStyle] name.
* @param resource will be added to the skin and mapped to the selected name.
*/
inline operator fun Skin.plusAssign(resource: Resource) =
this.add(resource)
/**
* Utility function that makes it easier to remove [Skin] assets.
* @param name name of the passed resource. Defaults to [defaultStyle].
* @throws NullPointerException if unable to find the resource.
*/
inline fun Skin.remove(name: String = defaultStyle) =
this.remove(name, Resource::class.java)
/**
* Utility function that makes it easier to check if [Skin] contains assets.
* @param name name of the resource to look for. Defaults to [defaultStyle].
*/
inline fun Skin.has(name: String = defaultStyle): Boolean =
this.has(name, Resource::class.java)
/**
* Utility function that makes it easier to access all [Skin] assets of a certain type.
* @return map of the resources for the [Resource] type, or `null` if no resources of that type is in the skin.
*/
inline fun Skin.getAll(): ObjectMap? =
this.getAll(Resource::class.java)
/**
* Utility function for adding existing styles to the skin. Mostly for internal use.
* @param name name of the style as it will appear in the [Skin] instance.
* @param style non-null existing style instance.
* @param init will be applied to the style instance. Inlined.
* @return passed style instance (for chaining).
*/
@OptIn(ExperimentalContracts::class)
inline fun