Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Roman Elizarov | fbb36d3 | 2017-01-23 16:03:54 +0300 | [diff] [blame] | 17 | package examples |
| 18 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 19 | import javafx.application.Application |
| 20 | import javafx.scene.Node |
| 21 | import javafx.scene.Scene |
| 22 | import javafx.scene.control.Button |
| 23 | import javafx.scene.layout.FlowPane |
| 24 | import javafx.scene.layout.Pane |
| 25 | import javafx.scene.paint.Color |
| 26 | import javafx.scene.shape.Circle |
| 27 | import javafx.scene.shape.Rectangle |
| 28 | import javafx.stage.Stage |
Roman Elizarov | fbb36d3 | 2017-01-23 16:03:54 +0300 | [diff] [blame] | 29 | import kotlinx.coroutines.experimental.CoroutineScope |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 30 | import kotlinx.coroutines.experimental.Job |
| 31 | import kotlinx.coroutines.experimental.delay |
| 32 | import kotlinx.coroutines.experimental.javafx.JavaFx |
| 33 | import kotlinx.coroutines.experimental.launch |
Roman Elizarov | 49ebab9 | 2017-01-24 12:20:06 +0300 | [diff] [blame] | 34 | import java.text.SimpleDateFormat |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 35 | import java.util.* |
| 36 | |
| 37 | fun main(args: Array<String>) { |
| 38 | Application.launch(FxTestApp::class.java, *args) |
| 39 | } |
| 40 | |
Roman Elizarov | b533112 | 2017-02-08 16:33:07 +0300 | [diff] [blame] | 41 | fun log(msg: String) = println("${SimpleDateFormat("yyyyMMdd-HHmmss.sss").format(Date())} [${Thread.currentThread().name}] $msg") |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 42 | |
| 43 | class FxTestApp : Application() { |
| 44 | val buttons = FlowPane().apply { |
| 45 | children += Button("Rect").apply { |
| 46 | setOnAction { doRect() } |
| 47 | } |
| 48 | children += Button("Circle").apply { |
| 49 | setOnAction { doCircle() } |
| 50 | } |
| 51 | children += Button("Clear").apply { |
| 52 | setOnAction { doClear() } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | val root = Pane().apply { |
| 57 | children += buttons |
| 58 | } |
| 59 | |
| 60 | val scene = Scene(root, 600.0, 400.0) |
| 61 | |
Roman Elizarov | 23f864e | 2017-03-03 19:57:47 +0300 | [diff] [blame] | 62 | override fun start(stage: Stage) { |
| 63 | stage.title = "Hello world!" |
| 64 | stage.scene = scene |
| 65 | stage.show() |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | val random = Random() |
| 69 | val animations = arrayListOf<Job>() |
| 70 | var animationIndex = 0 |
| 71 | |
Roman Elizarov | fbb36d3 | 2017-01-23 16:03:54 +0300 | [diff] [blame] | 72 | private fun animation(node: Node, block: suspend CoroutineScope.() -> Unit) { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 73 | root.children += node |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 74 | val job = launch(JavaFx, block = block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 75 | animations += job |
Roman Elizarov | e780347 | 2017-02-16 09:52:31 +0300 | [diff] [blame] | 76 | job.invokeOnCompletion { root.children -= node } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | fun doRect() { |
| 80 | val node = Rectangle(20.0, 20.0).apply { |
| 81 | fill = Color.RED |
| 82 | } |
| 83 | val index = ++animationIndex |
| 84 | val speed = 5.0 |
| 85 | animation(node) { |
| 86 | log("Started new 'rect' coroutine #$index") |
| 87 | var vx = speed |
| 88 | var vy = speed |
| 89 | var counter = 0 |
| 90 | while (true) { |
| 91 | JavaFx.awaitPulse() |
| 92 | node.x += vx |
| 93 | node.y += vy |
| 94 | val xRange = 0.0 .. scene.width - node.width |
| 95 | val yRange = 0.0 .. scene.height - node.height |
| 96 | if (node.x !in xRange ) { |
| 97 | node.x = node.x.coerceIn(xRange) |
| 98 | vx = -vx |
| 99 | } |
| 100 | if (node.y !in yRange) { |
| 101 | node.y = node.y.coerceIn(yRange) |
| 102 | vy = -vy |
| 103 | } |
| 104 | if (counter++ > 100) { |
| 105 | counter = 0 |
| 106 | delay(1000) // pause a bit |
| 107 | log("Delayed #$index for a while, resume and turn") |
| 108 | val t = vx |
| 109 | vx = vy |
| 110 | vy = -t |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fun doCircle() { |
| 117 | val node = Circle(20.0).apply { |
| 118 | fill = Color.BLUE |
| 119 | } |
| 120 | val index = ++animationIndex |
| 121 | val acceleration = 0.1 |
| 122 | val maxSpeed = 5.0 |
| 123 | animation(node) { |
| 124 | log("Started new 'circle' coroutine #$index") |
| 125 | var sx = random.nextDouble() * maxSpeed |
| 126 | var sy = random.nextDouble() * maxSpeed |
| 127 | while (true) { |
| 128 | JavaFx.awaitPulse() |
| 129 | val dx = root.width / 2 - node.translateX |
| 130 | val dy = root.height / 2 - node.translateY |
| 131 | val dn = Math.sqrt(dx * dx + dy * dy) |
| 132 | sx += dx / dn * acceleration |
| 133 | sy += dy / dn * acceleration |
| 134 | val sn = Math.sqrt(sx * sx + sy * sy) |
| 135 | val trim = sn.coerceAtMost(maxSpeed) |
| 136 | sx = sx / sn * trim |
| 137 | sy = sy / sn * trim |
| 138 | node.translateX += sx |
| 139 | node.translateY += sy |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | fun doClear() { |
| 145 | animations.forEach { it.cancel() } |
| 146 | } |
| 147 | } |