From ae0ac23910f7536519526df3ece30b038969db29 Mon Sep 17 00:00:00 2001 From: sheaf Date: Wed, 12 Aug 2020 23:23:19 +0200 Subject: [PATCH] =?UTF-8?q?draw=20B=C3=A9zier=20curves=20and=20connecting?= =?UTF-8?q?=20lines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/MetaBrush/Render/Document.hs | 50 ++++++++++++++++++++++++---- src/app/MetaBrush/UI/Viewport.hs | 1 + 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/app/MetaBrush/Render/Document.hs b/src/app/MetaBrush/Render/Document.hs index c17f00e..f8b87ff 100644 --- a/src/app/MetaBrush/Render/Document.hs +++ b/src/app/MetaBrush/Render/Document.hs @@ -66,9 +66,11 @@ renderStroke cols zoom ( Stroke strokePts ) = go True strokePts drawPoints, drawNextPoints, drawPath, drawNextPath :: Cairo.Render () ( drawNextPoints, drawNextPath ) = go False ( p2 : ps ) drawPoints = do - when drawFirstPoint ( drawPoint cols zoom p0 ) drawControl cols zoom p1 - drawPoint cols zoom p2 + drawLine cols zoom p0 p1 + drawLine cols zoom p1 p2 + when drawFirstPoint ( drawPoint cols zoom p0 ) + drawPoint cols zoom p2 drawNextPoints drawPath = do drawQuadraticBezier cols zoom ( Quadratic.Bezier p0 p1 p2 ) @@ -85,7 +87,7 @@ drawPoint ( Colours { pathPoint, pathPointOutline } ) zoom ( Point2D x y ) = do Cairo.save Cairo.translate x y - Cairo.scale ( 4 / zoom ) ( 4 / zoom ) + Cairo.scale ( 3 / zoom ) ( 3 / zoom ) Cairo.moveTo 1 0 Cairo.lineTo 0.5 hsqrt3 @@ -95,7 +97,7 @@ drawPoint ( Colours { pathPoint, pathPointOutline } ) zoom ( Point2D x y ) = do Cairo.lineTo 0.5 (-hsqrt3) Cairo.closePath - Cairo.setLineWidth 1 + Cairo.setLineWidth 0.8 withRGBA pathPointOutline Cairo.setSourceRGBA Cairo.strokePreserve @@ -108,11 +110,11 @@ drawControl ( Colours { controlPoint, controlPointOutline } ) zoom ( Point2D x y Cairo.save Cairo.translate x y - Cairo.scale ( 4 / zoom ) ( 4 / zoom ) + Cairo.scale ( 3 / zoom ) ( 3 / zoom ) Cairo.arc 0 0 1 0 ( 2 * pi ) - Cairo.setLineWidth 1 + Cairo.setLineWidth 0.8 withRGBA controlPointOutline Cairo.setSourceRGBA Cairo.strokePreserve @@ -121,5 +123,39 @@ drawControl ( Colours { controlPoint, controlPointOutline } ) zoom ( Point2D x y Cairo.restore +drawLine :: Colours -> Double -> Point2D Double -> Point2D Double -> Cairo.Render () +drawLine ( Colours { controlPoint } ) zoom ( Point2D x1 y1 ) ( Point2D x2 y2 ) = do + + Cairo.save + Cairo.moveTo x1 y1 + Cairo.lineTo x2 y2 + + Cairo.setLineWidth ( 3 / zoom ) + withRGBA controlPoint Cairo.setSourceRGBA + Cairo.stroke + + Cairo.restore + drawQuadraticBezier :: Colours -> Double -> Quadratic.Bezier ( Point2D Double ) -> Cairo.Render () -drawQuadraticBezier _ _ _ = pure () +drawQuadraticBezier ( Colours { path } ) zoom + ( Quadratic.Bezier + { p0 = Point2D x0 y0 + , p1 = Point2D x1 y1 + , p2 = Point2D x2 y2 + } + ) + = do + + Cairo.save + + Cairo.moveTo x0 y0 + Cairo.curveTo + ( ( 2 * x1 + x0 ) / 3 ) ( ( 2 * y1 + y0 ) / 3 ) + ( ( 2 * x1 + x2 ) / 3 ) ( ( 2 * y1 + y2 ) / 3 ) + x2 y2 + + Cairo.setLineWidth ( 6 / zoom ) + withRGBA path Cairo.setSourceRGBA + Cairo.stroke + + Cairo.restore diff --git a/src/app/MetaBrush/UI/Viewport.hs b/src/app/MetaBrush/UI/Viewport.hs index 06c0714..0cdb5a1 100644 --- a/src/app/MetaBrush/UI/Viewport.hs +++ b/src/app/MetaBrush/UI/Viewport.hs @@ -114,6 +114,7 @@ createViewport colours activeDocumentTVar openDocumentsTVar viewportGrid = do GTK.setContainerChild viewportOverlay viewportDrawingArea GTK.widgetAddEvents viewportDrawingArea [ GDK.EventMaskPointerMotionMask + , GDK.EventMaskButtonPressMask, GDK.EventMaskButtonReleaseMask , GDK.EventMaskScrollMask, GDK.EventMaskSmoothScrollMask ]