pp_shape: Shape object for generating various grobs

View source: R/pp_shape.R

pp_shapeR Documentation

Shape object for generating various grobs

Description

pp_shape() creates an R6 object with methods for creating various shape based grobs.

Usage

pp_shape(label = "rect", theta = 90, radius = 0.2, back = FALSE)

Arguments

label

Label of the shape. One of

“circle”

Circle.

“convexN”

An N-sided convex polygon. theta controls which direction the first vertex is drawn.

“concaveN”

A “star” (concave) polygon with N “points”. theta controls which direction the first point is drawn. radius controls the distance of the “inner” vertices from the center.

“halma”

A 2D outline of a “Halma pawn”.

“kite”

“Kite” quadrilateral shape.

“meeple”

A 2D outline of a “meeple”.

“oval”

Oval.

“pyramid”

An “Isosceles” triangle whose base is the bottom of the viewport. Typically used to help draw the face of the “pyramid” piece.

“rect”

Rectangle.

“roundrect”

“Rounded” rectangle. radius controls curvature of corners.

theta

convex and concave polygon shapes use this to determine where the first point is drawn.

radius

concave polygon and roundrect use this to control appearance of the shape.

back

Whether the shape should be reflected across a vertical line in the middle of the viewport.

Details

pp_shape objects serve the following purposes:

  1. Make it easier for developers to customize game piece appearances either through a "grob_fn" or "op_grob_fn" styles in pp_cfg() or manipulate a piece post drawing via functions like grid::grid.edit().

  2. Used internally to generate piecepackr's built-in game piece grobs.

pp_shape R6 Class Method Arguments

mat_width

Numeric vector of mat widths.

clip

“clip grob” to perform polyclip operation with. See gridGeometry::grid.polyclip() for more info.

op

Polyclip operation to perform. See gridGeometry::grid.polyclip() for more info.

pattern

Pattern to fill in shape with. See gridpattern::patternGrob() for more info.

...

Passed to gridpattern::patternGrob().

name

Grid grob name value.

gp

Grid gpar list. See grid::gpar() for more info.

vp

Grid viewport or NULL.

pp_shape R6 Class Methods

checkers(name = NULL, gp = gpar(), vp = NULL)

Returns a grob of checkers for that shape.

gridlines(name = NULL, gp = gpar(), vp = NULL)

Returns a grob of gridlines for that shape.

hexlines(name = NULL, gp = gpar(), vp = NULL)

Returns a grob of hexlines for that shape.

mat(mat_width = 0, name = NULL, gp = gpar(), vp = NULL)

Returns a grob for a matting “mat” for that shape.

pattern(pattern = "stripe", ..., name = NULL, gp = gpar(), vp = NULL)

Fills in the shape's npc_coords with a pattern. See gridpattern::patternGrob() for more information.

polyclip(clip, op = "intersection", name = NULL, gp = gpar(), vp = NULL)

Returns a grob that is an “intersection”, “minus”, “union”, or “xor” of another grob. Note unlike gridGeometry::polyclipGrob it can directly work with a pieceGrob "clip grob" argument.

shape(name = NULL, gp = gpar(), vp = NULL)

Returns a grob of the shape.

pp_shape R6 Class Active Bindings

label

The shape's label.

theta

The shape's theta.

radius

The shape's radius.

back

A boolean of whether this is the shape's “back” side.

npc_coords

A named list of “npc” coordinates along the perimeter of the shape.

Examples

 if (require("grid", quietly = TRUE)) {
     gp <- gpar(col="black", fill="yellow")
     rect <- pp_shape(label="rect")
     convex6 <- pp_shape(label="convex6")
     circle <- pp_shape(label="circle")

     pushViewport(viewport(x=0.25, y=0.75, width=1/2, height=1/2))
     grid.draw(rect$shape(gp=gp))
     grid.draw(rect$gridlines(gp=gpar(col="blue", lex=4)))
     grid.draw(rect$hexlines(gp=gpar(col="green")))
     popViewport()

     pushViewport(viewport(x=0.75, y=0.75, width=1/2, height=1/2))
     grid.draw(convex6$shape(gp=gp))
     grid.draw(convex6$checkers(gp=gpar(fill="blue")))
     popViewport()

     pushViewport(viewport(x=0.25, y=0.25, width=1/2, height=1/2))
     grid.draw(circle$shape(gp=gp))
     grid.draw(circle$mat(mat_width=0.2, gp=gpar(fill="blue")))
     popViewport()

     pushViewport(viewport(x=0.75, y=0.25, width=1/2, height=1/2))
     grid.draw(rect$shape(gp=gp))
     grid.draw(rect$mat(mat_width=c(0.2, 0.1, 0.3, 0.4), gp=gpar(fill="blue")))
     popViewport()
 }
 if (require("grid", quietly = TRUE)) {
     grid.newpage()
     gp <- gpar(col="black", fill="yellow")

     vp <- viewport(x=1/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("halma")$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("pyramid")$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("kite")$shape(gp=gp, vp=vp))
     vp <- viewport(x=1/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("meeple")$shape(gp=gp, vp=vp))
 }
 if (require("grid", quietly = TRUE)) {
     grid.newpage()
     vp <- viewport(x=1/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("convex3", 0)$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("convex4", 90)$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("convex5", 180)$shape(gp=gp, vp=vp))
     vp <- viewport(x=1/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("convex6", 270)$shape(gp=gp, vp=vp))
 }
 if (require("grid", quietly = TRUE)) {
     grid.newpage()
     vp <- viewport(x=1/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("concave3", 0, 0.1)$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=1/4, width=1/2, height=1/2)
     grid.draw(pp_shape("concave4", 90, 0.2)$shape(gp=gp, vp=vp))
     vp <- viewport(x=3/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("concave5", 180, 0.3)$shape(gp=gp, vp=vp))
     vp <- viewport(x=1/4, y=3/4, width=1/2, height=1/2)
     grid.draw(pp_shape("concave6", 270)$shape(gp=gp, vp=vp))
 }
 if (require("grid", quietly = TRUE) &&
     requireNamespace("gridpattern", quietly = TRUE)) {
     grid.newpage()
     hex <- pp_shape("convex6")
     gp <- gpar(fill = c("blue", "yellow", "red"), col = "black")
     grid.draw(hex$pattern("polygon_tiling", gp = gp, spacing = 0.1,
                           type = "truncated_trihexagonal"))
     gp <- gpar(fill = "black", col = NA)
     grid.draw(hex$mat(mat_width = 0.025, gp = gp))
 }

piecepackr documentation built on Sept. 11, 2024, 9:09 p.m.