Skip to content

Commit 05cf3fb

Browse files
committed
marta!
1 parent 0ca9890 commit 05cf3fb

51 files changed

Lines changed: 212 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/main/kotlin/dartzee/screen/DartsApp.kt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import dartzee.logging.KEY_CURRENT_SCREEN
1111
import dartzee.logging.LoggingCode
1212
import dartzee.main.exitApplication
1313
import dartzee.`object`.DartsClient
14+
import dartzee.theme.PopupSlider
1415
import dartzee.utils.DevUtilities
1516
import dartzee.utils.InjectedThings
1617
import dartzee.utils.InjectedThings.gameLauncher
@@ -43,6 +44,7 @@ val APP_SIZE = Dimension(1000, 700)
4344
class DartsApp(commandBar: CheatBar) : AbstractDevScreen(commandBar), WindowListener {
4445
override val windowName = "Main Window"
4546
var currentScreen: EmbeddedScreen = ScreenCache.get<MenuScreen>()
47+
private val slider = InjectedThings.theme?.slideImage?.let { PopupSlider(this, it) }
4648

4749
init {
4850
title = "Darts"
@@ -55,6 +57,8 @@ class DartsApp(commandBar: CheatBar) : AbstractDevScreen(commandBar), WindowList
5557
defaultCloseOperation = DO_NOTHING_ON_CLOSE
5658

5759
addWindowListener(this)
60+
61+
slider?.start()
5862
}
5963

6064
fun init() {

‎src/main/kotlin/dartzee/screen/preference/PreferencesPanelDartboard.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class PreferencesPanelDartboard : AbstractPreferencesPanel(), ColourSelectionLis
4444
textPane.text =
4545
"${InjectedThings.theme?.name} theme is currently applied.\n\nTo apply your own dartboard colours, select a theme without its own dartboard theming."
4646
textPane.background = Color.decode("#FFEE8C")
47+
textPane.foreground = Color.BLACK
4748
textPane.isEditable = false
4849
textPane.alignCentrally()
4950
textPane.setFontSize(16)

‎src/main/kotlin/dartzee/theme/Christmas.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ val Themes.CHRISTMAS: Theme
8282
menuAnimation = Snowfall(),
8383
resultConverter = { toRomanNumerals(it) + " " },
8484
dartboardFlasher = true,
85+
slideImage = "/theme/christmas/horrific/marta.png",
8586
)
8687

8788
private fun findChristmas(year: Int) =
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package dartzee.theme
2+
3+
import dartzee.core.util.runOnEventThread
4+
import dartzee.screen.DartsApp
5+
import dartzee.screen.MenuScreen
6+
import dartzee.utils.rotateImageByDegrees
7+
import java.awt.Dimension
8+
import java.awt.Point
9+
import javax.swing.ImageIcon
10+
import javax.swing.JLabel
11+
import kotlin.random.Random
12+
13+
enum class SlideMode {
14+
BOTTOM,
15+
TOP,
16+
LEFT,
17+
RIGHT,
18+
}
19+
20+
class PopupSlider(
21+
private val app: DartsApp,
22+
imagePath: String,
23+
private val listener: PopupListener? = null,
24+
private val phasePause: Long = 1500,
25+
) {
26+
private val img = ImageIcon(javaClass.getResource(imagePath))
27+
private val lbl = JLabel(img)
28+
private val imgMap =
29+
mapOf(
30+
SlideMode.BOTTOM to img,
31+
SlideMode.TOP to rotateImageByDegrees(img, 180.0),
32+
SlideMode.LEFT to rotateImageByDegrees(img, 90.0),
33+
SlideMode.RIGHT to rotateImageByDegrees(img, 270.0),
34+
)
35+
36+
fun start() {
37+
val t = Thread {
38+
while (true) {
39+
Thread.sleep(1000)
40+
val result = Random.nextInt(100)
41+
if (result >= 90 && app.currentScreen !is MenuScreen) {
42+
animate(SlideMode.entries.random())
43+
}
44+
}
45+
}
46+
47+
t.name = "PopupSlider"
48+
t.start()
49+
}
50+
51+
fun animate(mode: SlideMode) {
52+
val icon = imgMap.getValue(mode)
53+
val lblSizes = Dimension(icon.iconWidth, icon.iconHeight)
54+
runOnEventThread {
55+
app.remove(lbl)
56+
lbl.icon = icon
57+
lbl.size = lblSizes
58+
}
59+
60+
val centerX = (app.width / 2) - lblSizes.width / 2
61+
val centerY = (app.height / 2) - lblSizes.height / 2
62+
when (mode) {
63+
SlideMode.BOTTOM ->
64+
animate(Point(centerX, app.height), Point(centerX, app.height - lblSizes.height))
65+
SlideMode.TOP -> animate(Point(centerX, 0 - lblSizes.height), Point(centerX, 0))
66+
SlideMode.LEFT -> animate(Point(0 - lblSizes.width, centerY), Point(0, centerY))
67+
SlideMode.RIGHT ->
68+
animate(Point(app.width, centerY), Point(app.width - lblSizes.width, centerY))
69+
}
70+
}
71+
72+
private fun animate(start: Point, end: Point) {
73+
runOnEventThread { app.add(lbl, 0) }
74+
75+
animatePhase(start, end, "A")
76+
77+
Thread.sleep(phasePause)
78+
79+
animatePhase(end, start, "B")
80+
81+
runOnEventThread {
82+
app.remove(lbl)
83+
app.repaint()
84+
}
85+
}
86+
87+
private fun animatePhase(start: Point, end: Point, phaseName: String) {
88+
val xDiff = (end.x - start.x) / 20
89+
val yDiff = (end.y - start.y) / 20
90+
91+
(0..20).forEach { step ->
92+
runOnEventThread {
93+
lbl.setLocation(start.x + (step * xDiff), start.y + (step * yDiff))
94+
listener?.popupMoved(phaseName, step)
95+
}
96+
97+
Thread.sleep(20)
98+
}
99+
}
100+
}
101+
102+
interface PopupListener {
103+
fun popupMoved(phase: String, step: Int)
104+
}

‎src/main/kotlin/dartzee/theme/Theme.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ data class Theme(
3838
val resultConverter: ((Int) -> String)? = null,
3939
val menuAnimation: AbstractMenuAnimation? = null,
4040
val dartboardFlasher: Boolean = false,
41+
val slideImage: String? = null,
4142
) {
4243
val name = id.name
4344
private val resourcePath = name.lowercase()

‎src/main/kotlin/dartzee/utils/PlayerImageUtils.kt‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,47 @@ import dartzee.core.bean.toBufferedImage
55
import dartzee.db.PlayerEntity
66
import dartzee.db.PlayerImageEntity
77
import java.awt.Color
8+
import java.awt.geom.AffineTransform
89
import java.awt.image.BufferedImage
910
import java.io.ByteArrayOutputStream
1011
import javax.imageio.ImageIO
1112
import javax.swing.ImageIcon
1213
import kotlin.math.abs
14+
import kotlin.math.cos
15+
import kotlin.math.floor
1316
import kotlin.math.pow
17+
import kotlin.math.sin
1418

1519
const val PLAYER_IMAGE_WIDTH = 150
1620
const val PLAYER_IMAGE_HEIGHT = 150
1721

22+
fun rotateImageByDegrees(icon: ImageIcon, angle: Double): ImageIcon {
23+
val img = icon.toBufferedImage(BufferedImage.TYPE_INT_ARGB)
24+
25+
val rads = Math.toRadians(angle)
26+
val sin = abs(sin(rads))
27+
val cos = abs(cos(rads))
28+
val w = img.width
29+
val h = img.height
30+
val newWidth = floor(w * cos + h * sin).toInt()
31+
val newHeight = floor(h * cos + w * sin).toInt()
32+
33+
val rotated = BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB)
34+
val g2d = rotated.createGraphics()
35+
val at = AffineTransform()
36+
at.translate(((newWidth - w) / 2).toDouble(), ((newHeight - h) / 2).toDouble())
37+
38+
val x = w / 2
39+
val y = h / 2
40+
41+
at.rotate(rads, x.toDouble(), y.toDouble())
42+
g2d.transform = at
43+
g2d.drawImage(img, 0, 0, null)
44+
g2d.dispose()
45+
46+
return ImageIcon(rotated)
47+
}
48+
1849
fun splitAvatar(
1950
playerOne: PlayerEntity,
2051
playerTwo: PlayerEntity,
182 KB
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package dartzee.theme
2+
3+
import dartzee.core.bean.CheatBar
4+
import dartzee.helper.AbstractTest
5+
import dartzee.screen.DartsApp
6+
import dartzee.screen.EmbeddedScreen
7+
import dartzee.toLabel
8+
import io.github.alyssaruth.swingtest.flushEdt
9+
import io.github.alyssaruth.swingtest.shouldMatchImage
10+
import java.awt.Color
11+
import java.awt.image.BufferedImage
12+
import javax.swing.ImageIcon
13+
import org.junit.jupiter.api.DynamicTest
14+
import org.junit.jupiter.api.DynamicTest.dynamicTest
15+
import org.junit.jupiter.api.Tag
16+
import org.junit.jupiter.api.TestFactory
17+
18+
class PopupSliderTest : AbstractTest() {
19+
@TestFactory
20+
@Tag("screenshot")
21+
fun screenshots(): Collection<DynamicTest> {
22+
return SlideMode.entries.map { mode ->
23+
dynamicTest("Should match snapshot - $mode") { snapshotPopup(mode) }
24+
}
25+
}
26+
27+
private fun snapshotPopup(mode: SlideMode) {
28+
val app = DartsApp(CheatBar())
29+
30+
app.switchScreen(BlankScreen())
31+
app.isVisible = true
32+
33+
val listener: PopupListener =
34+
object : PopupListener {
35+
override fun popupMoved(phase: String, step: Int) {
36+
if (step % 5 == 0) {
37+
val img = BufferedImage(app.width, app.height, BufferedImage.TYPE_INT_ARGB)
38+
app.paint(img.graphics)
39+
ImageIcon(img).toLabel().shouldMatchImage("$mode-$phase-$step")
40+
}
41+
}
42+
}
43+
44+
val slider =
45+
PopupSlider(app, "/theme/christmas/horrific/marta.png", listener, phasePause = 0)
46+
slider.animate(mode)
47+
flushEdt()
48+
}
49+
50+
private class BlankScreen : EmbeddedScreen() {
51+
override fun initialise() {
52+
background = Color.BLACK
53+
}
54+
55+
init {
56+
removeAll()
57+
}
58+
59+
override fun getScreenName() = "Blank"
60+
}
61+
}

‎src/test/kotlin/dartzee/utils/PlayerImageUtilsTest.kt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ class PlayerImageUtilsTest : AbstractTest() {
6767
ImageIcon(result).toLabel().shouldMatchImage("outer-wilds-avatar")
6868
}
6969

70+
@Test
71+
@Tag("screenshot")
72+
fun `Should be able to rotate an image`() {
73+
val original = ImageIcon(javaClass.getResource("/theme/christmas/horrific/marta.png"))
74+
75+
rotateImageByDegrees(original, 90.0).toLabel().shouldMatchImage("marta-90")
76+
rotateImageByDegrees(original, 135.0).toLabel().shouldMatchImage("marta-135")
77+
}
78+
7079
@Test
7180
@Tag("screenshot")
7281
fun `Should combine player flags`() {
4.67 KB

0 commit comments

Comments
 (0)