Skip to content

Commit d8c73af

Browse files
authored
[orx-camera] Re-add Camera2D (#275)
1 parent 819537c commit d8c73af

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

orx-camera/build.gradle.kts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
1+
import ScreenshotsHelper.collectScreenshots
2+
13
plugins {
24
org.openrndr.extra.convention.`kotlin-multiplatform`
35
}
46

57
kotlin {
8+
jvm {
9+
@Suppress("UNUSED_VARIABLE")
10+
val demo by compilations.getting {
11+
// TODO: Move demos to /jvmDemo
12+
defaultSourceSet {
13+
kotlin.srcDir("src/demo/kotlin")
14+
}
15+
collectScreenshots { }
16+
}
17+
testRuns["test"].executionTask {
18+
useJUnitPlatform {
19+
includeEngines("spek2")
20+
}
21+
}
22+
}
23+
624
sourceSets {
725
@Suppress("UNUSED_VARIABLE")
826
val commonMain by getting {
@@ -15,5 +33,12 @@ kotlin {
1533
implementation(libs.kotlin.reflect)
1634
}
1735
}
36+
37+
@Suppress("UNUSED_VARIABLE")
38+
val jvmDemo by getting {
39+
dependencies {
40+
implementation(project(":orx-camera"))
41+
}
42+
}
1843
}
19-
}
44+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.openrndr.extra.camera
2+
3+
import org.openrndr.Extension
4+
import org.openrndr.Program
5+
import org.openrndr.draw.Drawer
6+
import org.openrndr.math.Matrix44
7+
import org.openrndr.math.transforms.buildTransform
8+
9+
/**
10+
* The [Camera2D] extension enables:
11+
* - **panning** the view by moving the mouse while a mouse button is pressed
12+
* - **zooming** in and out by using the mouse wheel
13+
*
14+
* Usage: `extend(Camera2D())`
15+
*/
16+
class Camera2D : Extension {
17+
override var enabled = true
18+
var view = Matrix44.IDENTITY
19+
override fun setup(program: Program) {
20+
program.mouse.dragged.listen {
21+
view = buildTransform { translate(it.dragDisplacement) } * view
22+
}
23+
program.mouse.scrolled.listen {
24+
val scaleFactor = 1.0 - it.rotation.y * 0.03
25+
view = buildTransform {
26+
translate(it.position)
27+
scale(scaleFactor)
28+
translate(-it.position)
29+
} * view
30+
}
31+
}
32+
33+
override fun beforeDraw(drawer: Drawer, program: Program) {
34+
drawer.view = view
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.openrndr.application
2+
import org.openrndr.color.ColorRGBa
3+
import org.openrndr.draw.loadFont
4+
import org.openrndr.extra.camera.Camera2D
5+
6+
/**
7+
* # Camera2D demo
8+
*
9+
* click and drag the mouse for panning, use the mouse wheel for zooming
10+
*/
11+
fun main() = application {
12+
program {
13+
val font = loadFont("demo-data/fonts/IBMPlexMono-Regular.ttf", 72.0)
14+
15+
extend(Camera2D())
16+
extend {
17+
drawer.circle(drawer.bounds.center, 300.0)
18+
19+
drawer.fontMap = font
20+
drawer.fill = ColorRGBa.PINK
21+
drawer.text("click and drag mouse", 50.0, 400.0)
22+
drawer.text("use mouse wheel", 50.0, 500.0)
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)