Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
612 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module App.Drawer.PieceActor where | ||
|
||
import Prelude | ||
|
||
import App.EaselJS.Point (Point) | ||
import App.EaselJS.Point as Point | ||
import App.EaselJS.Rectangle (Rectangle) | ||
import App.EaselJS.Rectangle as Rectangle | ||
import App.EaselJS.Shape (Shape) | ||
import App.EaselJS.Shape as Shape | ||
import App.Model.Piece (Piece) | ||
import App.Model.Piece as Piece | ||
import Data.Array as Array | ||
import Data.Maybe (Maybe(..)) | ||
import Effect (Effect) | ||
import Effect.Ref (Ref) | ||
import Effect.Ref as Ref | ||
|
||
type Transform = | ||
{ position :: Point | ||
, rotation :: Number | ||
} | ||
|
||
type PieceActor = | ||
{ body :: Piece | ||
, shape :: Shape | ||
, transform :: Ref Transform | ||
, loops :: Ref (Array Piece.Loop) | ||
, merger :: Ref (Maybe Int) | ||
, localBoundary :: Ref Rectangle | ||
, boundary :: Ref Rectangle | ||
} | ||
|
||
create :: Piece -> Effect PieceActor | ||
create body = do | ||
shape <- Shape.create | ||
transform <- Ref.new { position: Point.zero, rotation: 0.0 } | ||
loops <- Ref.new body.loops | ||
merger <- Ref.new Nothing | ||
let boundary' = | ||
Array.foldr Rectangle.addPoint Rectangle.empty | ||
$ Array.catMaybes $ Array.concat body.loops | ||
localBoundary <- Ref.new boundary' | ||
boundary <- Ref.new boundary' | ||
pure { body, shape, transform, loops, merger, localBoundary, boundary } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
module App.Drawer.PieceDrawer where | ||
|
||
import Prelude | ||
|
||
import App.Drawer.PieceActor (PieceActor) | ||
import App.EaselJS.Graphics (Graphics) | ||
import App.EaselJS.Graphics as G | ||
import App.EaselJS.Point (Point) | ||
import App.EaselJS.Rectangle as Rectangle | ||
import App.Model.Piece (Loop) | ||
import App.Utils (bool) | ||
import Data.Array as Array | ||
import Data.Maybe (Maybe(..), maybe) | ||
import Data.Traversable (traverse_) | ||
import Effect (Effect) | ||
import Effect.Ref as Ref | ||
import Web.DOM (Element) | ||
|
||
type PieceDrawer = | ||
{ image :: Maybe Element | ||
, drawsImage :: Boolean | ||
, drawsStroke :: Boolean | ||
, drawsControlLine :: Boolean | ||
, drawsBoundary :: Boolean | ||
, drawsCenter :: Boolean | ||
} | ||
|
||
withImage :: Element -> PieceDrawer | ||
withImage image = | ||
{ image: pure image | ||
, drawsImage: true | ||
, drawsStroke: false | ||
, drawsControlLine: false | ||
, drawsBoundary: false | ||
, drawsCenter: false | ||
} | ||
|
||
draw :: PieceActor -> PieceDrawer -> Effect Unit | ||
draw actor drawer = do | ||
let g = actor.shape.graphics | ||
G.clear g | ||
|
||
bool | ||
(G.beginFill "rgba(127, 191, 255, 0.5)") | ||
(maybe (G.beginFill "#f33") G.beginBitmapFill drawer.image) | ||
drawer.drawsImage g | ||
|
||
when drawer.drawsStroke do | ||
G.setStrokeStyle 2.0 g | ||
G.beginStroke "#faf" g | ||
|
||
traverse_ (\loop -> drawCurve loop g) actor.body.loops | ||
|
||
G.endFill g | ||
G.endStroke g | ||
|
||
when drawer.drawsBoundary do | ||
rect <- Ref.read actor.localBoundary | ||
G.setStrokeStyle 2.0 g | ||
G.beginStroke "#0f0" g | ||
G.drawRect rect g | ||
G.endStroke g | ||
|
||
when drawer.drawsControlLine do | ||
G.setStrokeStyle 1.0 g | ||
G.beginStroke "#663" g | ||
traverse_ (\loop -> drawPolyline loop g) actor.body.loops | ||
G.endStroke g | ||
|
||
when drawer.drawsCenter do | ||
pt <- Rectangle.center <$> Ref.read actor.localBoundary | ||
G.setStrokeStyle 2.0 g | ||
G.beginFill "#3f9" g | ||
G.drawCircle pt 8.0 g | ||
G.endFill g | ||
|
||
|
||
drawCurve :: Loop -> Graphics -> Effect Unit | ||
drawCurve loop g = do | ||
Array.uncons loop # traverse_ \({ head, tail }) -> do | ||
head # traverse_ \pt -> | ||
G.moveTo pt g | ||
f tail | ||
where | ||
f :: Array (Maybe Point) -> Effect Unit | ||
f pts = case Array.take 3 pts of | ||
[Just pt1, Just pt2, Just pt3] -> do | ||
G.bezierCurveTo pt1 pt2 pt3 g | ||
f $ Array.drop 3 pts | ||
[_, _, Just pt3] -> do | ||
G.lineTo pt3 g | ||
f $ Array.drop 3 pts | ||
_ -> | ||
pure unit | ||
|
||
drawPolyline :: Loop -> Graphics -> Effect Unit | ||
drawPolyline loop g = do | ||
Array.uncons loop # traverse_ \({ head, tail }) -> do | ||
head # traverse_ \pt -> | ||
G.moveTo pt g | ||
f tail | ||
where | ||
f :: Array (Maybe Point) -> Effect Unit | ||
f pts = case Array.uncons pts of | ||
Just { head, tail } -> do | ||
head # traverse_ \pt -> G.lineTo pt g | ||
f tail | ||
_ -> | ||
pure unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.