Skip to content

Commit

Permalink
image scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mnemotechnician committed Jan 20, 2022
1 parent 5f89900 commit 1659190
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.mnemotechnician.mkui

import arc.util.*
import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
Expand Down Expand Up @@ -37,20 +38,23 @@ inline fun Table.textButton(crossinline provider: () -> String, style: TextButto
/** Adds an image button with an optional onclick listener, returns the created cell */
inline fun Table.imageButton(image: Drawable, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(image, style)
b.image.setScaling(Scaling.bounded)
b.clicked { b.onclick() }
return add(b)
}

/** Adds an image button with an optional onclick listener, returns the created cell */
inline fun Table.imageButton(image: TextureRegion, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(image, style)
b.image.setScaling(Scaling.bounded)
b.clicked { b.onclick() }
return add(b)
}

/** Adds an image button with a dynamic image and an optional onclick listener, returns the created cell */
inline fun Table.imageButton(crossinline provider: () -> TextureRegion, style: ImageButton.ImageButtonStyle = Styles.defaulti, crossinline onclick: ImageButton.() -> Unit = {}): Cell<ImageButton> {
val b = ImageButton(provider(), style)
b.image.setScaling(Scaling.bounded)
b.clicked { b.onclick() }
b.update { b.image.setDrawable(provider()) }
return add(b)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.mnemotechnician.mkui

import arc.util.*
import arc.scene.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
Expand All @@ -23,7 +24,17 @@ fun Cell<Label>.scaleFont(scale: Float) = this.apply {
get().setFontScale(scale)
};

/** Changes the font size of the wrapped text button and returns the created cell */
/** Changes the font size of the wrapped text button and returns the cell */
fun Cell<TextButton>.scaleButtonFont(scale: Float) = this.apply {
get().label.setFontScale(scale)
};

/** Changes Scaling of the wrapped image and returns the cell */
fun Cell<Image>.scaleImage(scaling: Scaling) = this.apply {
get().setScaling(scaling)
};

/** Sets scaling of the image inside the image button and returns the cell */
fun Cell<ImageButton>.scaleButtonImage(scaling: Scaling) = this.apply {
get().image.setScaling(scaling)
}
10 changes: 7 additions & 3 deletions lib/src/main/kotlin/com/github/mnemotechnician/mkui/Elements.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.mnemotechnician.mkui

import arc.util.*
import arc.scene.ui.*
import arc.scene.ui.layout.*
import arc.scene.style.*
Expand All @@ -26,20 +27,23 @@ inline fun Table.addLabel(crossinline provider: () -> CharSequence, style: Label
}

/** Adds a constant image to the table and returns the created cell */
inline fun Table.addImage(drawable: Drawable): Cell<Image> {
inline fun Table.addImage(drawable: Drawable, scaling: Scaling = Scaling.bounded): Cell<Image> {
val i = Image(drawable)
i.setScaling(scaling)
return add(i)
}

/** Adds a constant image to the table and returns the created cell */
inline fun Table.addImage(drawable: TextureRegion): Cell<Image> {
inline fun Table.addImage(drawable: TextureRegion, scaling: Scaling = Scaling.bounded): Cell<Image> {
val i = Image(drawable)
i.setScaling(scaling)
return add(i)
}

/** Adds a dynamic image to the table and returns the created cell */
inline fun Table.addImage(crossinline provider: () -> TextureRegion): Cell<Image> {
inline fun Table.addImage(crossinline provider: () -> TextureRegion, scaling: Scaling = Scaling.bounded): Cell<Image> {
val i = Image(provider())
i.setScaling(scaling)
i.update { provider() }
return add(i)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package com.github.mnemotechnician.mkui.windows
import arc.scene.ui.layout.*
import com.github.mnemotechnician.mkui.*

/** A class that represents a floating on-screen window that the user can drag and interact with. */
/**
* A class that represents a floating on-screen window that the user can drag and interact with.
*/
abstract class Window {

/** The root of the window */
/** The root of the window. Should not be modified nor accessed. */
lateinit internal var rootTable: Table

/** The table this window is assigned to. Initialized when the window is created. */
Expand All @@ -27,22 +29,34 @@ abstract class Window {
/** Whether this window can be closed by the user. Should be overriden. */
abstract var closeable: Boolean

/** Called when the window is being created. At this point the window has a Table assigned to it, which should be inflated by this function. */
/**
* Called when the window is being created.
*
* At this point the window has a Table assigned to it, which should be inflated by this function.
*/
abstract fun onCreate()

/** The window has already been created, this function is called on every tick. You should avoid modifying the table from this function: that can cause a performance loss. */
/**
* This function is called on every tick after creating the table.
*
* Avoid modifying the table in this function: that can cause a performance loss.
*/
open fun onUpdate() {
}

/** Called whenever the window is being dragged by the user */
/**
* Called when the window is being dragged by the user.
*
* This function is called continuously.
*/
open fun onDrag() {
}

/** Called whenever this window is being toggled by the user */
open fun onToggle(collapsed: Boolean) {
}

/** Called whenever the window is being destroyed */
/** Called when the window is being destroyed. Usually this means that the user closed the window. */
open fun onDestroy() {
}

Expand Down

0 comments on commit 1659190

Please sign in to comment.