@@ -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 */
4345class 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