Skip to content

Commit c1ce841

Browse files
committed
add method to translate mouse position to document position
1 parent 30fc92f commit c1ce841

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

orx-g-code/src/demo/kotlin/DemoInteractivePlot.kt

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import org.openrndr.MouseEvent
21
import org.openrndr.application
32
import org.openrndr.extensions.Screenshots
3+
import org.openrndr.extra.gcode.Origin
44
import org.openrndr.extra.gcode.Plot
55
import org.openrndr.extra.gcode.basicGrblSetup
66
import org.openrndr.math.Vector2
@@ -15,37 +15,39 @@ fun main() = application {
1515
program {
1616
extend(Screenshots())
1717

18-
val plot = Plot(dimensions = Vector2(210.0, 297.0), manualRedraw = false)
18+
val plot = Plot(
19+
dimensions = Vector2(210.0, 297.0),
20+
manualRedraw = false,
21+
origin = Origin.CENTER
22+
)
1923
extend(plot) {
2024
generator = basicGrblSetup()
2125

2226
// Set output files to be exported to tmp
2327
// "g" to export g-code.
2428
folder = "/tmp"
25-
}
26-
27-
val drawingArea = plot.docBounds.offsetEdges(-10.0)
2829

29-
// Converts mouse events position to document space
30-
fun MouseEvent.documentPosition(): Vector2 {
31-
val s = 1 / plot.scale()
32-
return Vector2(position.x * s, plot.docBounds.height - position.y * s)
30+
draw {
31+
rectangle(docBounds.offsetEdges(-9.0))
32+
println(docBounds)
33+
}
3334
}
3435

36+
val drawingArea = plot.docBounds.offsetEdges(-10.0)
3537

3638
val cb = ContourBuilder(true)
3739

3840
// Handle mouse events and restrict drawing to drawing area
3941
mouse.buttonDown.listen {
40-
val p = it.documentPosition()
42+
val p = plot.toDocumentSpace(it.position)
4143
if (drawingArea.contains(p)) {
4244
cb.moveTo(p)
4345
} else {
4446
cb.moveTo(drawingArea.contour.nearest(p).position)
4547
}
4648
}
4749
mouse.dragged.listen {
48-
val p = it.documentPosition()
50+
val p = plot.toDocumentSpace(it.position)
4951
if (drawingArea.contains(p)) {
5052
cb.moveOrLineTo(p)
5153
} else {

orx-g-code/src/jvmMain/kotlin/Plot.kt

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ typealias DrawFunction = CompositionDrawer.() -> Unit
3939
* Configuration:
4040
* When [manualRedraw] is true, the programs presentation mode is set to Manual on startup.
4141
* "r" to trigger redraw.
42+
* When [renderMode] is set to manual, the plot will not be rendered to the programms drawer.
43+
* Then [render] has to be called to draw the plot. [origin]
4244
*/
4345
class Plot(
4446
// Document
4547
dimensions: Vector2, // Document size in mm
4648
var name: String? = null,
47-
var origin: Origin = Origin.BOTTOM_LEFT,
49+
val origin: Origin = Origin.BOTTOM_LEFT,
4850

4951
// G-code
5052
var generator: Generator = noopGenerator(),
@@ -75,7 +77,11 @@ class Plot(
7577

7678
override var enabled: Boolean = true
7779

78-
val docBounds = Rectangle(0.0, 0.0, dimensions.x, dimensions.y)
80+
val docBounds = when (origin) {
81+
Origin.CENTER -> Rectangle(-dimensions.times(.5), dimensions.x, dimensions.y)
82+
Origin.BOTTOM_LEFT,
83+
Origin.TOP_LEFT -> Rectangle(0.0, 0.0, dimensions.x, dimensions.y)
84+
}
7985

8086
val layers: MutableMap<String, Composition> = mutableMapOf()
8187
private var order: List<String> = listOf()
@@ -174,10 +180,7 @@ class Plot(
174180
drawer.isolated {
175181
strokeWeight = 0.0
176182
fill = backgroundColor
177-
when (origin) {
178-
Origin.CENTER -> rectangle(docBounds.dimensions * -.5, docBounds.width, docBounds.height)
179-
else -> rectangle(0.0, 0.0, docBounds.width, docBounds.height)
180-
}
183+
rectangle(docBounds)
181184
}
182185

183186
// Layers
@@ -186,7 +189,7 @@ class Plot(
186189
}
187190

188191
/**
189-
* Drawer scaled to document space.
192+
* Drawer scaled to document space, to fit to the window.
190193
*/
191194
fun scaled(drawer: Drawer, drawFunction: (Drawer) -> Unit) = drawer.isolated {
192195
when (origin) {
@@ -209,10 +212,23 @@ class Plot(
209212
drawFunction(this)
210213
}
211214

215+
/**
216+
* Scales and translates the given position from screen space to document space.
217+
* Can be used to translate mouse events to draw to the plot.
218+
*/
219+
fun toDocumentSpace(p: Vector2): Vector2 {
220+
val s = 1.0 / scale
221+
return when (origin) {
222+
Origin.BOTTOM_LEFT -> Vector2(p.x * s, docBounds.height - p.y * s)
223+
Origin.TOP_LEFT -> p.times(s)
224+
Origin.CENTER -> p.times(Vector2(s,-s)).plus(Vector2(-docBounds.width, docBounds.height) *.5)
225+
}
226+
}
227+
212228
/**
213229
* Double [v] scaled from document space to screen space.
214230
*/
215-
fun scaled(v: Double) = v.times(scale)
231+
fun scaled(v: Double) = v * scale
216232

217233
/**
218234
* Scale from document space to screen space.

0 commit comments

Comments
 (0)