Skip to content

Commit

Permalink
#194 Created optMap and optFlatMap methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich2 committed Aug 16, 2022
1 parent b3df4e1 commit 4cdfb59
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 39 deletions.
14 changes: 0 additions & 14 deletions Geom/src/geom/packageGeom.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,6 @@ package object geom
implicit class MetreExtensionsImplicit(thisMetres: Length)
{ def / (operand: Length): Double = thisMetres.metresNum / operand.metresNum
}

implicit class OptionGeomImplicit[A](thisOption: Option[A])
{ def canvObjsPair(f: A => (Seq[GraphicAffineElem], Seq[GraphicAffineElem])): (Seq[GraphicAffineElem], Seq[GraphicAffineElem]) = thisOption match
{
case Some(a) => f(a)
case None => (Seq(), Seq())
}

/** folds to [[GraphicElems]], an empty Array in the case of none, an Arr of 1 [[GraphicElem]] in the case of [[Some]]. */
def foldToGraphic(f: A => GraphicElem): GraphicElems = thisOption.fld(Arr(), a => Arr(f(a)))

/** folds to [[GraphicElems]], an empty Array in the case of none, the result of the function in the case of [[Some]]. */
def foldToGraphics(f: A => GraphicElems): GraphicElems = thisOption.fld(Arr(), f(_))
}

implicit class IterableExtensions[A](val thisIter: Iterable[A]) extends AnyVal
{
Expand Down
8 changes: 6 additions & 2 deletions Tiling/ExsSrc/gOne/GOneGui.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ case class GOneGui(canv: CanvasPlatform, scenStart: OneScen, viewIn: HGView) ext
}

/** [[TextGraphic]]s to display the [[HCen]] coordinate in the tiles that have no unit counters. */
def hexStrs: Arr[TextGraphic] = players.hcNonesMap(hc => TextGraphic(hc.strComma, 20, hc.toPt2))
def hexStrs: Arr[TextGraphic] = players.hcNonesOptMap{ hc => proj.transCoord(hc).map(TextGraphic(hc.strComma, 20, _)) }

/** This makes the tiles active. They respond to mouse clicks. It does not paint or draw the tiles. */
val tiles: Arr[PolygonActive] = gridSys.activeTiles
Expand Down Expand Up @@ -84,6 +84,10 @@ case class GOneGui(canv: CanvasPlatform, scenStart: OneScen, viewIn: HGView) ext

def moveGraphics2: GraphicElems = moveGraphics.slate(-focus).scale(cPScale).flatMap(_.arrow)
def frame: GraphicElems = (tiles ++ units ++ hexStrs).slate(-focus).scale(cPScale) +% innerSidesDraw +% outerSidesDraw ++ moveGraphics2
proj.frame = () => frame
proj.getFrame = () => frame
proj.setStatusText = {str =>
statusText = str
thisTop()
}
repaint()
}
16 changes: 3 additions & 13 deletions Tiling/src/egrid/GridWorldGui.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,14 @@ class GridWorldGui(val canv: CanvasPlatform, scenIn: EScenWarm, viewIn: HGView)

def rcTexts = ifGScale(20.5, optTexts)

def optTexts = terrs.hcFlatMap{ (hc, terr) =>
proj.transCoord(hc).foldToGraphics{ pt =>
def optTexts = terrs.hcOptFlatMap{ (hc, terr) =>
proj.transCoord(hc).map{ pt =>
val strs: StringArr = StringArr(hc.rcStr32).appendOption(proj.hCoordOptStr(hc)) +% hc.strComma
TextGraphic.lines(strs, 12, pt, terr.contrastBW)
}
}

// val hexs1 = gridSys.map{ hc =>
// val col = terrs(hc).colour
// val p = hc.hVertPolygon.map(gridSys.hCoordLL(_)).toMetres3.fromLatLongFocus(focus)//.map(_.xy)
// (p, col)
// }
//
// val hexs2 = hexs1.map{ (p, col: Colour) => p.map(_.xy / scale).fill(col) }

val tiles = gridSys.flatMap{ hc =>
proj.transTile(hc).foldToGraphic{ poly =>poly.fill(terrs(hc).colour) }
}
val tiles = gridSys.optMap{ hc => proj.transTile(hc).map(poly => poly.fill(terrs(hc).colour)) }

def innerSides = proj.innerSidesDraw(2, White) //lines3.map(_.xyLineSeg(scale).draw(White))
def innerSidesDraw = ifGScale(5, Arr(innerSides))
Expand Down
11 changes: 11 additions & 0 deletions Tiling/src/prid/phex/HCenDGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ class HCenDGrid[A <: AnyRef](val unsafeArray: Array[A]) extends AnyVal with TCen
build.buffToBB(buff)
}

/** [[HCen]] with optFlatmap. Applies the function to each [[HCen]] coordinate with the corresponding element in the underlying array. Note the
* function signature follows the foreach based convention of putting the collection element 2nd or last as seen for example in fold methods' (accumulator,
* element) => B signature. */
def hcOptFlatMap[BB <: SeqImut[_]](f: (HCen, A) => Option[BB])(implicit grid: HGridSys, build: ArrFlatBuilder[BB]): BB = {
val buff = build.newBuff()
grid.iForeach { (hc, i) =>
f(hc, apply(hc)).foreach(build.buffGrowArr(buff, _))
}
build.buffToBB(buff)
}

/** Completes the given row from the given starting c column value to the end of the row. An exception is
* thrown if the tile values don't match with the end of the row. */
final def completeRow(row: Int, cStart: Int, tileValues: Multiple[A]*)(implicit grid: HGrid): HCen =
Expand Down
14 changes: 14 additions & 0 deletions Tiling/src/prid/phex/HCenOptDGrid.scala
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ class HCenOptDGrid[A <: AnyRef](val unsafeArr: Array[A]) extends AnyVal with TCe
build.buffToBB(buff)
}

/** maps the [[HCen]] Coordinate values to an Option of type B, but only where the element is a None value. It collects the Some values returned by
* the parameter function. Returns an immutable Array based collection of type ArrB, the second type parameter. */
def hcNonesOptMap[B, ArrB <: SeqImut[B]](f: HCen => Option[B])(implicit grider: HGridSys, build: ArrBuilder[B, ArrB]): ArrB = {
val buff = build.newBuff()

grider.foreach { r =>
val a: A = unsafeArr(grider.arrIndex(r))
if (a == null) {
f(r).foreach(build.buffGrow(buff, _))
}
}
build.buffToBB(buff)
}

/** [[HCen]] with element flatMap, but only coordinates where there some value. Maps and flattens each [[HCen]] coordinate with its associated
* element of type A. It ignores the None values. Note the function signature follows the foreach based convention of putting the collection element
* 2nd or last as seen for example in fold methods' (accumulator, element) => B signature. */
Expand Down
8 changes: 8 additions & 0 deletions Tiling/src/prid/phex/HGridSys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ trait HGridSys extends Any with TGridSys
res
}

/** Maps from all hex tile centre coordinates to an Arr of type ArrT. The elements of this array can not be accessed from this grid class as the
* TileGrid structure is lost in the flatMap operation. */
final def optMap[B, ArrB <: SeqImut[B]](f: HCen => Option[B])(implicit build: ArrBuilder[B, ArrB]): ArrB = {
val buff = build.newBuff(numTiles)
foreach { hCen => f(hCen).foreach(build.buffGrow(buff, _)) }
build.buffToBB(buff)
}

/** flatMaps from all hex tile centre coordinates to an Arr of type ArrT. The elements of this array can not be accessed from this grid class as the
* TileGrid structure is lost in the flatMap operation. */
final def flatMap[ArrT <: SeqImut[_]](f: HCen => ArrT)(implicit build: ArrFlatBuilder[ArrT]): ArrT =
Expand Down
3 changes: 2 additions & 1 deletion Tiling/src/prid/phex/HSysProjection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import geom._, pgui._
trait HSysProjection
{ type GridT <: HGridSys
def gridSys: GridT
var frame: () => GraphicElems = () => Arr()
var getFrame: () => GraphicElems = () => Arr()
var setStatusText: String => Unit = s => {}
val buttons: Arr[PolygonCompound]
def tiles: Polygon = ???

Expand Down
21 changes: 12 additions & 9 deletions Tiling/src/prid/phex/HSysProjectionFlat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import geom._, pgui._
final case class HSysProjectionFlat(gridSys: HGridSys, panel: Panel) extends HSysProjection
{ type GridT = HGridSys

var cPScale: Double = 10
/** The number of pixels per column unit. */
var cPScale: Double = 40

/** The number of pixels per tile from side to opposite side. */
def tileScale: Double = cPScale * 4

def tilePScaleStr = s"scale = ${tileScale.str2} pixels per tile"
var focus: Vec2 = Vec2(0, 0)
override def sides: LineSegArr = gridSys.sideLines.slate(-focus).scale(cPScale)
override def innerSides: LineSegArr = gridSys.innerSideLines.slate(-focus).scale(cPScale)
override def outerSides: LineSegArr = gridSys.outerSideLines.slate(-focus).scale(cPScale)

override def transCoord(hc: HCoord): Option[Pt2] = ???
override def transCoord(hc: HCoord): Option[Pt2] = Some(gridSys.hCoordToPt2(hc).slate(-focus).scale(cPScale))

override def transTile(hc: HCen): Option[Polygon] = ???

Expand All @@ -27,19 +33,16 @@ final case class HSysProjectionFlat(gridSys: HGridSys, panel: Panel) extends HSy
case _ =>
}


def zoomIn: PolygonCompound = clickButton("+") { _ =>
cPScale *= 1.1
panel.repaint(frame())
//statusText = tilePScaleStr
//thisTop()
panel.repaint(getFrame())
setStatusText(tilePScaleStr)
}

def zoomOut: PolygonCompound = clickButton("-") { _ =>
cPScale /= 1.1
panel.repaint(frame())
//statusText = tilePScaleStr
//thisTop()
panel.repaint(getFrame())
setStatusText(tilePScaleStr)
}

override val buttons: Arr[PolygonCompound] = Arr(zoomIn, zoomOut)
Expand Down

0 comments on commit 4cdfb59

Please sign in to comment.