Skip to content

Commit

Permalink
add method to translate mouse position to document position
Browse files Browse the repository at this point in the history
  • Loading branch information
adriandieter committed Jan 6, 2023
1 parent 30fc92f commit c1ce841
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
24 changes: 13 additions & 11 deletions orx-g-code/src/demo/kotlin/DemoInteractivePlot.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import org.openrndr.MouseEvent
import org.openrndr.application
import org.openrndr.extensions.Screenshots
import org.openrndr.extra.gcode.Origin
import org.openrndr.extra.gcode.Plot
import org.openrndr.extra.gcode.basicGrblSetup
import org.openrndr.math.Vector2
Expand All @@ -15,37 +15,39 @@ fun main() = application {
program {
extend(Screenshots())

val plot = Plot(dimensions = Vector2(210.0, 297.0), manualRedraw = false)
val plot = Plot(
dimensions = Vector2(210.0, 297.0),
manualRedraw = false,
origin = Origin.CENTER
)
extend(plot) {
generator = basicGrblSetup()

// Set output files to be exported to tmp
// "g" to export g-code.
folder = "/tmp"
}

val drawingArea = plot.docBounds.offsetEdges(-10.0)

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

val drawingArea = plot.docBounds.offsetEdges(-10.0)

val cb = ContourBuilder(true)

// Handle mouse events and restrict drawing to drawing area
mouse.buttonDown.listen {
val p = it.documentPosition()
val p = plot.toDocumentSpace(it.position)
if (drawingArea.contains(p)) {
cb.moveTo(p)
} else {
cb.moveTo(drawingArea.contour.nearest(p).position)
}
}
mouse.dragged.listen {
val p = it.documentPosition()
val p = plot.toDocumentSpace(it.position)
if (drawingArea.contains(p)) {
cb.moveOrLineTo(p)
} else {
Expand Down
32 changes: 24 additions & 8 deletions orx-g-code/src/jvmMain/kotlin/Plot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ typealias DrawFunction = CompositionDrawer.() -> Unit
* Configuration:
* When [manualRedraw] is true, the programs presentation mode is set to Manual on startup.
* "r" to trigger redraw.
* When [renderMode] is set to manual, the plot will not be rendered to the programms drawer.
* Then [render] has to be called to draw the plot. [origin]
*/
class Plot(
// Document
dimensions: Vector2, // Document size in mm
var name: String? = null,
var origin: Origin = Origin.BOTTOM_LEFT,
val origin: Origin = Origin.BOTTOM_LEFT,

// G-code
var generator: Generator = noopGenerator(),
Expand Down Expand Up @@ -75,7 +77,11 @@ class Plot(

override var enabled: Boolean = true

val docBounds = Rectangle(0.0, 0.0, dimensions.x, dimensions.y)
val docBounds = when (origin) {
Origin.CENTER -> Rectangle(-dimensions.times(.5), dimensions.x, dimensions.y)
Origin.BOTTOM_LEFT,
Origin.TOP_LEFT -> Rectangle(0.0, 0.0, dimensions.x, dimensions.y)
}

val layers: MutableMap<String, Composition> = mutableMapOf()
private var order: List<String> = listOf()
Expand Down Expand Up @@ -174,10 +180,7 @@ class Plot(
drawer.isolated {
strokeWeight = 0.0
fill = backgroundColor
when (origin) {
Origin.CENTER -> rectangle(docBounds.dimensions * -.5, docBounds.width, docBounds.height)
else -> rectangle(0.0, 0.0, docBounds.width, docBounds.height)
}
rectangle(docBounds)
}

// Layers
Expand All @@ -186,7 +189,7 @@ class Plot(
}

/**
* Drawer scaled to document space.
* Drawer scaled to document space, to fit to the window.
*/
fun scaled(drawer: Drawer, drawFunction: (Drawer) -> Unit) = drawer.isolated {
when (origin) {
Expand All @@ -209,10 +212,23 @@ class Plot(
drawFunction(this)
}

/**
* Scales and translates the given position from screen space to document space.
* Can be used to translate mouse events to draw to the plot.
*/
fun toDocumentSpace(p: Vector2): Vector2 {
val s = 1.0 / scale
return when (origin) {
Origin.BOTTOM_LEFT -> Vector2(p.x * s, docBounds.height - p.y * s)
Origin.TOP_LEFT -> p.times(s)
Origin.CENTER -> p.times(Vector2(s,-s)).plus(Vector2(-docBounds.width, docBounds.height) *.5)
}
}

/**
* Double [v] scaled from document space to screen space.
*/
fun scaled(v: Double) = v.times(scale)
fun scaled(v: Double) = v * scale

/**
* Scale from document space to screen space.
Expand Down

0 comments on commit c1ce841

Please sign in to comment.