Skip to content

Commit 40cd970

Browse files
committed
Update docs
1 parent cd022d3 commit 40cd970

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

docs/.bundle/config

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
BUNDLE_PATH: "/home/funpro/.bundle/"

docs/interaction/Events.markdown

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
# File generated by dokgen. Do not edit.
3+
# Edit 'src/main/kotlin/docs/40_Interaction/C90_Events.kt' instead.
4+
layout: default
5+
title: Events
6+
parent: Interaction
7+
last_modified_at: 2024.03.05 16:52:37 +0100
8+
nav_order: 90
9+
has_children: false
10+
---
11+
12+
# Events
13+
14+
Events are notifications sent by a sender to one or more subscribers indicating
15+
that an action has taken place.
16+
17+
Mouse and keyboard events are among the most commonly used events in OPENRNDR.
18+
Before we get to those in the next chapter, let's examine how to create
19+
our own custom events.
20+
21+
We will often use events in class instances, so let's create
22+
a simple class called `Thing`. This class will include an update method
23+
that will emit an event every 60 times it's called. Anyone listening to this
24+
event will receive it.
25+
26+
```kotlin
27+
class Thing {
28+
val timeEvent = Event<Boolean>()
29+
30+
private var frame = 0
31+
32+
fun update() {
33+
if (++frame % 60 == 0) {
34+
timeEvent.trigger(Random.bool())
35+
timeEvent.deliver()
36+
}
37+
}
38+
}
39+
```
40+
41+
### Sending an event
42+
43+
Notice how events carry a payload, in this case `Boolean`. This is convenient
44+
because it allows us to transmit information together with the event.
45+
Mouse and Keyboard events contain details about the mouse position or
46+
the key pressed. In this program we are free to choose any type, so lets
47+
just broadcasting a message containing a random boolean value.
48+
49+
Another thing to observe is that `timeEvent` is a public variable. If it
50+
was private we couldn't listen to it from outside
51+
by calling `thing.timeEvent.listen { ... }`.
52+
53+
At some point in our program execution we need to call `.trigger()`
54+
to queue an event. We can call it as many times as needed.
55+
56+
Finally, we call `.deliver()` to deliver the queued events to those
57+
listening to them.
58+
59+
### Listening to an event
60+
61+
The following small program shows how to listen to an event emitted by a class.
62+
63+
First, let's create one instance of the class (`Thing` in this case).
64+
65+
Next, listen to an event this instance can emit (`timeEvent` here).
66+
67+
68+
```kotlin
69+
fun main() = application {
70+
program {
71+
val thing = Thing()
72+
extend {
73+
thing.update()
74+
}
75+
thing.timeEvent.listen {
76+
println("timeEvent triggered! It contains a: $it")
77+
}
78+
}
79+
}
80+
```
81+
82+
There are multiple approaches to including events in our class. For example, a
83+
class could have separate `loadStart` and `loadComplete` events, or instead,
84+
have just one `loadEvent` and have the payload describe if it was
85+
a `start` or a `complete` event. Having two separate events arguably produces
86+
more readable code.
87+
88+
[edit on GitHub](https://github.com/openrndr/openrndr-guide/blob/main/src/main/kotlin/docs/40_Interaction/C90_Events.kt){: .btn .btn-github }

0 commit comments

Comments
 (0)