|
| 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 | +} |
0 commit comments