/
C130_Live_coding.kt
140 lines (118 loc) · 3.88 KB
/
C130_Live_coding.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@file:Suppress("UNUSED_EXPRESSION")
@file:Title("Live coding")
@file:ParentTitle("Use cases")
@file:Order("130")
@file:URL("useCases/liveCoding")
package docs.`95_Use_cases`
import org.openrndr.Program
import org.openrndr.application
import org.openrndr.dokgen.annotations.*
import org.openrndr.draw.persistent
import org.openrndr.extra.olive.Olive
import org.openrndr.extra.olive.Once
import org.openrndr.extra.olive.oliveProgram
import org.openrndr.extra.camera.Orbital
import org.openrndr.ffmpeg.VideoPlayerFFMPEG
fun main() {
@Text """
# Live coding with orx-olive
By using Kotlin's ability to run script files we can build a live coding
environment. The `orx-olive` library
simplifies the work to be done to set up a live coding environment. Code
and additional documentation for the library can be found in the
[Github repository](https://github.com/openrndr/orx/tree/master/orx-jvm/orx-olive).
## Prerequisites
Assuming you are working on an
[`openrndr-template`](https://github.com/openrndr/openrndr-template) based
project, all you have to do is enable `orx-olive` in the `orxFeatures`
set in `build.gradle.kts` and reimport the gradle project.
## Basic example
"""
@Code
application {
configure {
width = 768
height = 576
}
program {
extend(Olive<Program>())
}
}
@Text
"""
When running this script you will see a file called `live.kts` appear
in `src/main/kotlin`. When you edit
this file you will see that changes are automatically detected
(after save) and that the program reloads.
## Interaction with extensions
The Olive extension works well together with other extensions, but only
those which are installed before the Olive extension. In the following
example we see the use of `Orbital` in combination with `Olive`.
"""
@Code
application {
program {
extend(Orbital())
extend(Olive<Program>())
}
}
@Text """
## Adding script drag/drop support
A simple trick to turn your live coding host program into a versatile
live coding environment is to add file drop support. With this enabled
one can drag a .kts file onto the window and drop it to load the script file.
"""
@Code
application {
program {
extend(Olive<Program>()) {
this@program.window.drop.listen {
this.script = it.files.first()
}
}
}
}
@Text """
## Adding persistent state
Sometimes you want to keep parts of your application persistent, that
means its state will survive a script reload.
In the following example we show how you can prepare the host program
to contain a persistent camera device.
"""
@Code
application {
oliveProgram {
val camera by Once {
persistent {
VideoPlayerFFMPEG.fromDevice()
}
}
camera.play()
extend {
camera.colorBuffer?.let {
drawer.image(it,0.0, 0.0, 128.0, 96.0)
}
}
}
}
@Text
"""
Note that when you create a custom host program you also have to adjust
script files to include the program type. For example `live.kts` would
become like this.
```kotlin
@file:Suppress("UNUSED_LAMBDA_EXPRESSION")
import org.openrndr.color.ColorRGBa
import org.openrndr.draw.*
{ program: PersistentProgram ->
program.apply {
extend {
camera.next()
drawer.drawStyle.colorMatrix = tint(ColorRGBa.GREEN) * grayscale(0.0, 0.0, 1.0)
camera.draw(drawer)
}
}
}
```
"""
}