Skip to content

Commit 196ccd9

Browse files
authored
Merge pull request #357 from alyssaruth/christmas-1
Christmas
2 parents 604860a + d3acfb9 commit 196ccd9

226 files changed

Lines changed: 1860 additions & 131 deletions

File tree

Some content is hidden

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

‎build.gradle.kts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ detekt {
3030
ktfmt { kotlinLangStyle() }
3131

3232
dependencies {
33-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0")
3433
implementation("org.apache.commons:commons-math3:3.6.1")
3534
implementation("com.miglayout:miglayout-swing:11.0")
3635
implementation("org.jfree:jfreechart:1.5.4")
@@ -52,6 +51,7 @@ dependencies {
5251
testImplementation("io.github.alyssaruth:swing-test:5.1.0")
5352
testImplementation("org.junit.jupiter:junit-jupiter:5.14.3")
5453
testImplementation("com.squareup.okhttp3:mockwebserver:4.12.0")
54+
testImplementation("com.github.romankh3:image-comparison:4.4.0")
5555
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
5656
}
5757

@@ -82,6 +82,7 @@ kover {
8282
instrumentation {
8383
disabledForTestTasks.add("test")
8484
disabledForTestTasks.add("updateScreenshots")
85+
disabledForTestTasks.add("verifyScreenshots")
8586
disabledForTestTasks.add("integrationAndE2E")
8687
}
8788
}
@@ -101,6 +102,14 @@ tasks.register("unitTest", Test::class) {
101102
classpath = derivedTestClasspath
102103
}
103104

105+
tasks.register("verifyScreenshots", Test::class) {
106+
group = "verification"
107+
useJUnitPlatform { includeTags = setOf("screenshot") }
108+
109+
testClassesDirs = derviedTestClassesDirs
110+
classpath = derivedTestClasspath
111+
}
112+
104113
tasks.register("updateScreenshots", Test::class) {
105114
group = "verification"
106115
useJUnitPlatform { includeTags = setOf("screenshot") }

‎src/main/kotlin/dartzee/bean/InteractiveDartboard.kt‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class InteractiveDartboard(colourWrapper: IDartboardPainter = getColourWrapperFr
1818
addMouseMotionListener(this)
1919
}
2020

21+
override fun resolveClashingOverrides() {
22+
hoveredSegment?.let { overriddenSegmentColours[it] = hoverColour(it) }
23+
}
24+
2125
fun highlightDartboard(pt: Point) {
2226
if (!allowInteraction) {
2327
return
@@ -38,15 +42,15 @@ class InteractiveDartboard(colourWrapper: IDartboardPainter = getColourWrapperFr
3842

3943
if (newHoveredSegment != null && isValidSegmentForHover(newHoveredSegment)) {
4044
hoveredSegment = newHoveredSegment
41-
overrideSegmentColour(
42-
newHoveredSegment,
43-
getHighlightedColour(defaultColourForSegment(newHoveredSegment)),
44-
)
45+
overrideSegmentColour(newHoveredSegment, hoverColour(newHoveredSegment))
4546
} else {
4647
hoveredSegment = null
4748
}
4849
}
4950

51+
private fun hoverColour(segment: DartboardSegment) =
52+
getHighlightedColour(defaultColourForSegment(segment))
53+
5054
private fun isValidSegmentForHover(segment: DartboardSegment): Boolean {
5155
val statuses = segmentStatuses
5256
if (statuses != null) {

‎src/main/kotlin/dartzee/bean/PresentationDartboard.kt‎

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ import dartzee.logging.KEY_DURATION
66
import dartzee.`object`.ComputedPoint
77
import dartzee.`object`.DartboardSegment
88
import dartzee.`object`.IDartboard
9+
import dartzee.`object`.SegmentType
10+
import dartzee.screen.game.SegmentStatus
911
import dartzee.screen.game.SegmentStatuses
1012
import dartzee.screen.game.getSegmentStatus
1113
import dartzee.theme.GREY_COLOUR_WRAPPER
1214
import dartzee.theme.IDartboardPainter
13-
import dartzee.theme.getBaseFont
1415
import dartzee.utils.DurationTimer
1516
import dartzee.utils.InjectedThings
1617
import dartzee.utils.InjectedThings.logger
18+
import dartzee.utils.ResourceCache
1719
import dartzee.utils.UPPER_BOUND_DOUBLE_RATIO
1820
import dartzee.utils.UPPER_BOUND_OUTSIDE_BOARD_RATIO
1921
import dartzee.utils.computeEdgePoints
@@ -39,9 +41,10 @@ open class PresentationDartboard(
3941
private val renderThemeBanner: Boolean = false,
4042
) : JComponent(), IDartboard {
4143
var segmentStatuses: SegmentStatuses? = null
42-
private val overriddenSegmentColours = mutableMapOf<DartboardSegment, Color>()
44+
protected val overriddenSegmentColours = mutableMapOf<DartboardSegment, Color>()
4345
private val dirtySegments = mutableSetOf<DartboardSegment>()
4446
private var lastPaintImage: BufferedImage? = null
47+
var paintExtraComponents: (Graphics2D) -> Unit = {}
4548

4649
override fun computeRadius() = computeRadius(width, height)
4750

@@ -69,6 +72,26 @@ open class PresentationDartboard(
6972
pt.setLocation(x, y)
7073
}
7174

75+
fun overrideScoringSegmentColours(segments: Map<DartboardSegment, Color>) {
76+
dirtySegments.addAll(overriddenSegmentColours.keys)
77+
overriddenSegmentColours.clear()
78+
79+
val scoring =
80+
segments.filter { (segment, _) ->
81+
segmentStatuses == null ||
82+
segmentStatuses.getSegmentStatus(segment) == SegmentStatus.SCORING
83+
}
84+
85+
overriddenSegmentColours.putAll(scoring)
86+
dirtySegments.addAll(scoring.keys)
87+
88+
resolveClashingOverrides()
89+
90+
repaint()
91+
}
92+
93+
open fun resolveClashingOverrides() {}
94+
7295
fun overrideSegmentColour(segment: DartboardSegment, colour: Color) {
7396
overriddenSegmentColours[segment] = colour
7497
dirtySegments.add(segment)
@@ -96,6 +119,11 @@ open class PresentationDartboard(
96119
repaint()
97120
}
98121

122+
fun colorAt(pt: Point): Color? {
123+
val rgb = lastPaintImage?.getRGB(pt.x, pt.y)
124+
return rgb?.let { Color(rgb, true) }
125+
}
126+
99127
override fun paintComponent(g: Graphics) {
100128
super.paintComponent(g)
101129

@@ -110,6 +138,8 @@ open class PresentationDartboard(
110138
paintOuterBoard(g2d)
111139
getAllSegmentsForDartzee().paintAll(bi)
112140

141+
paintExtraComponents(g2d)
142+
113143
if (renderThemeBanner) {
114144
renderBanner(g2d)
115145
}
@@ -147,7 +177,7 @@ open class PresentationDartboard(
147177
details.textCenter,
148178
details.fontHeight,
149179
svgBounds.height,
150-
getBaseFont(),
180+
InjectedThings.theme?.bannerFont ?: ResourceCache.BASE_FONT,
151181
theme.fontColor,
152182
details.text,
153183
details.maxWidth,
@@ -168,10 +198,12 @@ open class PresentationDartboard(
168198
}
169199

170200
private fun paintSegment(segment: DartboardSegment, bi: BufferedImage) {
171-
val colour = overriddenSegmentColours[segment] ?: defaultColourForSegment(segment)
172-
colourSegment(segment, colour, bi)
201+
colourSegment(segment, colourForSegment(segment), bi)
173202
}
174203

204+
private fun colourForSegment(segment: DartboardSegment): Color =
205+
overriddenSegmentColours[segment] ?: defaultColourForSegment(segment)
206+
175207
protected fun defaultColourForSegment(segment: DartboardSegment): Color {
176208
val default = colourWrapper.getColour(segment)
177209
val status = segmentStatuses ?: return default
@@ -185,7 +217,9 @@ open class PresentationDartboard(
185217

186218
private fun colourSegment(segment: DartboardSegment, color: Color, bi: BufferedImage) {
187219
if (segment.isMiss()) {
188-
paintOuterRing(bi.createGraphics(), color)
220+
val g = bi.createGraphics()
221+
paintOuterRing(g, color)
222+
paintScoreLabels(g)
189223
return
190224
}
191225

@@ -219,8 +253,6 @@ open class PresentationDartboard(
219253
g.paint = color
220254
val ring = createOuterRing()
221255
g.fill(ring)
222-
223-
paintScoreLabels(g)
224256
}
225257

226258
private fun createOuterRing(): Shape {
@@ -247,17 +279,30 @@ open class PresentationDartboard(
247279
return area
248280
}
249281

250-
private fun paintScoreLabels(g: Graphics2D) {
282+
private fun paintScoreLabels(g: Graphics2D, wrapper: IDartboardPainter = colourWrapper) {
251283
if (!renderScoreLabels) return
252284

253285
val radius = computeRadius()
254286
val outerRadius = UPPER_BOUND_OUTSIDE_BOARD_RATIO * radius
255287
val lblHeight = ((outerRadius - radius) / 2).roundToInt()
256288

257-
(1..20).forEach { paintScoreLabel(it, g, lblHeight) }
289+
(1..20).forEach { paintScoreLabel(it, g, lblHeight, wrapper) }
290+
}
291+
292+
fun repaintScoreLabels(wrapper: IDartboardPainter) {
293+
val graphics = lastPaintImage?.graphics as? Graphics2D ?: return
294+
val outerRingColour = colourForSegment(DartboardSegment(SegmentType.MISS, 20))
295+
296+
paintOuterRing(graphics, outerRingColour)
297+
paintScoreLabels(graphics, wrapper)
258298
}
259299

260-
private fun paintScoreLabel(score: Int, g: Graphics2D, lblHeight: Int) {
300+
private fun paintScoreLabel(
301+
score: Int,
302+
g: Graphics2D,
303+
lblHeight: Int,
304+
colourWrapper: IDartboardPainter,
305+
) {
261306
val angle = getAnglesForScore(score).toList().average()
262307
val radiusForLabel = computeRadius() + lblHeight
263308
val avgPoint = translatePoint(computeCenter(), radiusForLabel, angle)
@@ -268,7 +313,7 @@ open class PresentationDartboard(
268313
lblHeight,
269314
lblHeight,
270315
colourWrapper.font,
271-
colourWrapper.fontColor,
316+
colourWrapper.getFontColour(score),
272317
score.toString(),
273318
)
274319
}

‎src/main/kotlin/dartzee/bean/RenderUtils.kt‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ fun paintLabel(
2626
lbl.foreground = color
2727
lbl.horizontalAlignment = SwingConstants.CENTER
2828
lbl.font = font
29+
lbl.putClientProperty(
30+
RenderingHints.KEY_TEXT_ANTIALIASING,
31+
RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB,
32+
)
2933

3034
// Work out the width for this label, based on the text
3135
val metrics = Canvas().getFontMetrics(font)
@@ -35,7 +39,6 @@ fun paintLabel(
3539
val lblX = center.getX().toInt() - lblWidth / 2
3640
val lblY = center.getY().toInt() - labelHeight / 2
3741

38-
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON)
3942
g.translate(lblX, lblY)
4043
lbl.paint(g)
4144
g.translate(-lblX, -lblY)

‎src/main/kotlin/dartzee/core/bean/NumberField.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import javax.swing.JFormattedTextField
44
import javax.swing.text.NumberFormatter
55

66
/** Text field to contain integer values */
7-
class NumberField constructor(min: Int? = null, max: Int? = null, val testId: String = "") :
7+
class NumberField(min: Int? = null, max: Int? = null, val testId: String = "") :
88
JFormattedTextField(NumberFormatter()) {
99
init {
1010
if (min != null) {

‎src/main/kotlin/dartzee/core/util/DartboardDodgyUtil.kt‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ private fun GameplayDartboard.doDodgyOnEdt(animation: Animation) {
6767
dodgyLabel.icon = img
6868
dodgyLabel.setSize(img.iconWidth, img.iconHeight)
6969

70-
val x = (getWidth() - img.iconWidth) / 2
70+
val x = (getWidth() - img.iconWidth) / 2 + (getWidth() * animation.xOffsetPercent).toInt()
7171
val y = getHeight() - img.iconHeight
7272
dodgyLabel.setLocation(x, y)
7373
add(dodgyLabel)
@@ -81,8 +81,8 @@ private fun GameplayDartboard.doDodgyOnEdt(animation: Animation) {
8181
playDodgySound(animation.wavResource)
8282
}
8383

84-
private fun GameplayDartboard.playDodgySound(soundName: String) {
85-
if (!preferenceService.get(Preferences.showAnimations)) {
84+
private fun GameplayDartboard.playDodgySound(soundName: String?) {
85+
if (!preferenceService.get(Preferences.showAnimations) || soundName == null) {
8686
return
8787
}
8888

@@ -138,10 +138,12 @@ private fun GameplayDartboard.initialiseAudioClip(
138138
return myClip
139139
}
140140

141-
private fun GameplayDartboard.resetDodgy() {
142-
removeDodgyLabels()
143-
repaint()
144-
revalidate()
141+
fun GameplayDartboard.resetDodgy() {
142+
runOnEventThread {
143+
removeDodgyLabels()
144+
repaint()
145+
revalidate()
146+
}
145147
}
146148

147149
private fun GameplayDartboard.removeDodgyLabels() {

‎src/main/kotlin/dartzee/core/util/MathsUtil.kt‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dartzee.core.util
22

33
import kotlin.math.ceil
44
import kotlin.math.pow
5+
import kotlin.math.roundToInt
56

67
fun Int.ceilDiv(other: Int) = ceil(this.toDouble() / other).toInt()
78

@@ -17,11 +18,30 @@ fun <T> Pair<Double, Double>.mapStepped(stepSize: Double, mapFunction: (Double)
1718
return ret
1819
}
1920

21+
tailrec fun toRomanNumerals(value: Int, numeralsSoFar: String = ""): String =
22+
if (value < 1) numeralsSoFar.ifEmpty { "-" }
23+
else if (value >= 1000) toRomanNumerals(value - 1000, numeralsSoFar + "M")
24+
else if (value >= 900) toRomanNumerals(value - 900, numeralsSoFar + "CM")
25+
else if (value >= 500) toRomanNumerals(value - 500, numeralsSoFar + "D")
26+
else if (value >= 400) toRomanNumerals(value - 400, numeralsSoFar + "CD")
27+
else if (value >= 100) toRomanNumerals(value - 100, numeralsSoFar + "C")
28+
else if (value >= 90) toRomanNumerals(value - 90, numeralsSoFar + "XC")
29+
else if (value >= 50) toRomanNumerals(value - 50, numeralsSoFar + "L")
30+
else if (value >= 40) toRomanNumerals(value - 40, numeralsSoFar + "XL")
31+
else if (value >= 10) toRomanNumerals(value - 10, numeralsSoFar + "X")
32+
else if (value >= 9) numeralsSoFar + "IX"
33+
else if (value >= 5) toRomanNumerals(value - 5, numeralsSoFar + "V")
34+
else if (value >= 4) numeralsSoFar + "IV" else toRomanNumerals(value - 1, numeralsSoFar + "I")
35+
2036
object MathsUtil {
2137
fun round(number: Double, decimalPlaces: Int): Double {
38+
if (number.isNaN()) {
39+
return number
40+
}
41+
2242
val powerOfTen = 10.0.pow(decimalPlaces.toDouble())
2343

24-
val rounded = Math.round(powerOfTen * number)
44+
val rounded = (powerOfTen * number).roundToInt()
2545

2646
return rounded / powerOfTen
2747
}

‎src/main/kotlin/dartzee/object/IDartboard.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dartzee.`object`
22

33
import dartzee.utils.AimPoint
4+
import dartzee.utils.RADIUS_RATIO
45
import dartzee.utils.computePointsForSegment
56
import dartzee.utils.factorySegmentForPoint
67
import dartzee.utils.getAngleForPoint
@@ -11,7 +12,7 @@ interface IDartboard {
1112

1213
fun computeCenter(): Point
1314

14-
fun computeRadius(width: Int, height: Int) = 0.7 * minOf(width, height) / 2.0
15+
fun computeRadius(width: Int, height: Int) = RADIUS_RATIO * minOf(width, height) / 2.0
1516

1617
fun getPointsForSegment(segment: DartboardSegment) =
1718
computePointsForSegment(segment, computeCenter(), computeRadius())

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ 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.ISliderParent
15+
import dartzee.theme.PopupSlider
1416
import dartzee.utils.DevUtilities
1517
import dartzee.utils.InjectedThings
1618
import dartzee.utils.InjectedThings.gameLauncher
@@ -40,9 +42,14 @@ private const val CMD_DUMP_LOGS = "dump logs"
4042

4143
val APP_SIZE = Dimension(1000, 700)
4244

43-
class DartsApp(commandBar: CheatBar) : AbstractDevScreen(commandBar), WindowListener {
45+
class DartsApp(commandBar: CheatBar) :
46+
AbstractDevScreen(commandBar), WindowListener, ISliderParent {
4447
override val windowName = "Main Window"
4548
var currentScreen: EmbeddedScreen = ScreenCache.get<MenuScreen>()
49+
private val slider =
50+
InjectedThings.theme?.slideImage?.let {
51+
PopupSlider(this.contentPane as JComponent, ::shouldAnimate, it)
52+
}
4653

4754
init {
4855
title = "Darts"
@@ -55,8 +62,12 @@ class DartsApp(commandBar: CheatBar) : AbstractDevScreen(commandBar), WindowList
5562
defaultCloseOperation = DO_NOTHING_ON_CLOSE
5663

5764
addWindowListener(this)
65+
66+
slider?.start()
5867
}
5968

69+
override fun shouldAnimate() = currentScreen !is MenuScreen
70+
6071
fun init() {
6172
setIcon()
6273

0 commit comments

Comments
 (0)