-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathDemoProxyProgram02.kt
64 lines (60 loc) · 1.7 KB
/
DemoProxyProgram02.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import org.openrndr.*
import org.openrndr.color.ColorRGBa
import org.openrndr.extra.viewbox.ViewBox
import org.openrndr.extra.viewbox.viewBox
import org.openrndr.math.Vector2
import kotlin.math.cos
/**
* Demonstrates how to use two proxy programs and
* toggle between them by clicking the mouse.
*
* programA draws a circle and can be moved by pressing the
* arrow keys.
*
* programB draws a ring located at the current mouse
* position.
*
* Note that programA keeps listening to the key events
* even if programB is currently displayed.
*/
fun Program.programA() {
var pos = drawer.bounds.center
extend {
drawer.fill = ColorRGBa.PINK
drawer.circle(pos, cos(seconds) * 50.0 + 50.0)
}
keyboard.keyDown.listen {
when (it.key) {
KEY_ARROW_UP -> pos -= Vector2.UNIT_Y * 20.0
KEY_ARROW_DOWN -> pos += Vector2.UNIT_Y * 20.0
KEY_ARROW_LEFT -> pos -= Vector2.UNIT_X * 20.0
KEY_ARROW_RIGHT -> pos += Vector2.UNIT_X * 20.0
}
}
}
fun Program.programB() {
extend {
drawer.fill = ColorRGBa.TRANSPARENT
drawer.stroke = ColorRGBa.CYAN
drawer.strokeWeight = 50.0
drawer.circle(mouse.position, 200.0)
}
}
fun main() = application {
configure {
width = 720
height = 360
}
program {
val proxyBoxes = mutableListOf<ViewBox>()
proxyBoxes.add(viewBox(drawer.bounds).apply { programA() })
proxyBoxes.add(viewBox(drawer.bounds).apply { programB() })
var currentBox = 1
extend {
proxyBoxes[currentBox].draw()
}
mouse.buttonDown.listen {
currentBox = (currentBox + 1) % proxyBoxes.size
}
}
}