Skip to content

Commit

Permalink
#194 Created first optMap method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich2 committed Oct 7, 2022
1 parent 030cef5 commit 5ad271a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Dev/src/pzug/ZugGui.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ case class ZugGui(canv: CanvasPlatform, scenIn: ZugScen) extends HGridSysGui("Zu
val polyFills: Arr[PolygonFill] =
terrs.rowsCombine.map{ (hcrv: HCenRowValue[ZugTerr]) => hcrv.hVertPolygon.toPolygon(_.toPt2Reg).fill(hcrv.value.colour) }

val lines: Arr[LineSegDraw] = terrs.sideFlatMap((hs, _) => Arr(hs.draw()), (hs, t1, t2 ) => ife(t1 == t2, Arr(hs.draw(t1.contrastBW)), Arr()))
val lines: Arr[LineSegDraw] = terrs.sideFlatMap((hs, _) => Arr(hs.drawDepr()), (hs, t1, t2 ) => ife(t1 == t2, Arr(hs.drawDepr(t1.contrastBW)), Arr()))

def lunits: GraphicElems = scen.lunits.gridHeadsFlatMap{ (hc, squad) =>
val uc = UnitCounters.infantry(1.2, HSquad(hc, squad), squad.colour, terrs(hc).colour).slate(hc.toPt2Reg)
Expand Down
7 changes: 4 additions & 3 deletions Tiling/ExsSrc/gFour/GFourGui.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ case class GFourGui(canv: CanvasPlatform, scenStart: FourScen) extends HGridSysG
implicit val proj: HSysProjection = gridSys.projection(mainPanel)
//proj.setView(viewIn)
val lines: Arr[LineSegDraw] = //terrs.sideFlatMap((hs, _) => Arr(hs.draw()), (hs, t1, t2 ) => ife(t1 == t2, Arr(hs.draw(t1.contrastBW)), Arr()))
terrs.linksFlatMap{(hs, t1, t2 ) => ife(t1 == t2, Arr(hs.draw(t1.contrastBW)), Arr[LineSegDraw]()) }
//terrs.linksFlatMap{(hs, t1, t2 ) => ife(t1 == t2, Arr(hs.draw(t1.contrastBW)), Arr[LineSegDraw]()) }
terrs.projLinksOptMap{(hs, t1, t2 ) => ife(t1 == t2, Some(hs.drawDepr(t1.contrastBW)), None) }

val rows: Arr[HCenRowValue[Terr]] = terrs.rowsCombine
debvar(rows.length)
Expand All @@ -26,7 +27,7 @@ case class GFourGui(canv: CanvasPlatform, scenStart: FourScen) extends HGridSysG
Rect(1.6, 1.2, hc.toPt2Reg).fillDrawTextActive(p.colour, p, p.team.name + "\n" + hc.rcStr, 24, 2.0)
}

def unitRects: Arr[PolygonCompound] = units.projSomeHcPtMap { (p, hc, pt) =>
def unitGraphics: Arr[PolygonCompound] = units.projSomeHcPtMap { (p, hc, pt) =>
Rect(160, 120, pt).fillDrawTextActive(p.colour, p, p.team.name + "\n" + hc.rcStr, 24, 2.0) }

def texts: Arr[TextGraphic] = units.projNoneHcPtMap{ (hc, pt) => pt.textAt(hc.rcStr, 14, terrs(hc).contrastBW) }
Expand Down Expand Up @@ -63,6 +64,6 @@ case class GFourGui(canv: CanvasPlatform, scenStart: FourScen) extends HGridSysG
statusText = s"Game Four. Scenario has ${gridSys.numTiles} tiles."
thisTop()

def frame: GraphicElems = (hexs ++ lines).slate(-focus).scale(cPScale) ++ unitRects ++ texts
def frame: GraphicElems = (hexs ++ lines).slate(-focus).scale(cPScale) ++ unitGraphics ++ texts
repaint()
}
2 changes: 1 addition & 1 deletion Tiling/src/egrid/EGridFlatGui.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ case class EGridFlatGui(canv: CanvasPlatform, scen: EScenFlat, viewIn: HGView) e
val terrs: HCenLayer[WTile] = scen.terrs
val sTerrs: HSideBoolLayer = scen.sTerrs
def tiles: Arr[PolygonCompound] = gridSys.map{ hc => hc.polygonReg.fillActive(terrs(hc).colour, hc.polygonReg) }
def sides: GraphicElems = sTerrs.truesMap{hs => Rectangle.fromAxisRatio(hs.lineSeg, 0.3).fill(Colour.DarkBlue) }
def sides: GraphicElems = sTerrs.truesMap{hs => Rectangle.fromAxisRatio(hs.lineSegDepr, 0.3).fill(Colour.DarkBlue) }

def tileStrs: Arr[PolygonCompound] = gridSys.map{ hc =>
hc.polygonReg.fillTextActive(terrs(hc).colour, hc.polygonReg, hc.rcStr32 --- hc.rcStr, 12, terrs(hc).contrastBW)
Expand Down
41 changes: 37 additions & 4 deletions Tiling/src/prid/phex/HCenLayer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import reflect.ClassTag
class HCenLayer[A <: AnyRef](val unsafeArray: Array[A]) extends AnyVal with TCenLayer[A]
{ override type ThisT = HCenLayer[A]
override def typeStr: String = "HCenDGrid"
def apply(hc: HCen)(implicit grider: HGridSys): A = unsafeArray(grider.arrIndex(hc))
def apply(hc: HCen)(implicit gridSys: HGridSys): A = unsafeArray(gridSys.arrIndex(hc))
def apply(gridSys: HGridSys, hc: HCen): A = unsafeArray(gridSys.arrIndex(hc))
def rc(r: Int, c: Int)(implicit grid: HGridSys): A = unsafeArray(grid.arrIndex(r, c))
def set(hc: HCen, value: A)(implicit gridSys: HGridSys): Unit = { unsafeArray(gridSys.arrIndex(hc)) = value }
def set(r: Int, c: Int, value: A)(implicit gridSys: HGridSys): Unit = { unsafeArray(gridSys.arrIndex(r, c)) = value }
Expand Down Expand Up @@ -140,9 +141,41 @@ class HCenLayer[A <: AnyRef](val unsafeArray: Array[A]) extends AnyVal with TCen
def projSideFlatMap[BB <: SeqImut[_]](proj: HSysProjection)(f1: (HSide, A) => BB, f2: (HSide, A, A) => BB)(implicit build: ArrFlatBuilder[BB]): BB =
proj.gChild.sidesFlatMap { hs =>
hs.tiles match {
case (c1, c2) if proj.gChild.hCenExists(c1) & proj.gChild.hCenExists(c2) => f2(hs, apply(c1)(proj.parent), apply(c2)(proj.parent))
case (c1, _) if proj.gChild.hCenExists(c1) => f1(hs, apply(c1)(proj.parent))
case (_, c2) => f1(hs, apply(c2)(proj.parent))
case (c1, c2) if proj.gChild.hCenExists(c1) & proj.gChild.hCenExists(c2) => f2(hs, apply(proj.parent, c1), apply(proj.parent, c2))
case (c1, _) if proj.gChild.hCenExists(c1) => f1(hs, apply(proj.parent, c1))
case (_, c2) => f1(hs, apply(proj.parent, c2))
}
}

/** Maps the sides to an immutable Array, using the data of this HCenArr. It takes two functions, one for the edges of the grid, that takes the
* [[HSide]] and the single adjacent hex tile data value and one for the inner sides of the grid that takes the [[HSide]] and the two adjacent hex
* tile data values. */
def projLinksFlatMap[BB <: SeqImut[_]](f2: (HSide, A, A) => BB)(implicit proj: HSysProjection, build: ArrFlatBuilder[BB]): BB =
projLinksFlatMap(proj)(f2)

/** Maps the sides to an immutable Array, using the data of this HCenArr. It takes two functions, one for the edges of the grid, that takes the
* [[HSide]] and the single adjacent hex tile data value and one for the inner sides of the grid that takes the [[HSide]] and the two adjacent hex
* tile data values. */
def projLinksFlatMap[BB <: SeqImut[_]](proj: HSysProjection)(f2: (HSide, A, A) => BB)(implicit build: ArrFlatBuilder[BB]): BB =
proj.gChild.linksFlatMap { hs =>
hs.tiles match {
case (c1, c2) if proj.gChild.hCenExists(c1) & proj.gChild.hCenExists(c2) => f2(hs, apply(proj.parent, c1), apply(proj.parent, c2))
}
}

/** Maps the sides to an immutable Array, using the data of this HCenArr. It takes two functions, one for the edges of the grid, that takes the
* [[HSide]] and the single adjacent hex tile data value and one for the inner sides of the grid that takes the [[HSide]] and the two adjacent hex
* tile data values. */
def projLinksOptMap[B, BB <: SeqImut[B]](f2: (HSide, A, A) => Option[B])(implicit proj: HSysProjection, build: ArrBuilder[B, BB]): BB =
projLinksOptMap(proj)(f2)

/** Maps the sides to an immutable Array, using the data of this HCenArr. It takes two functions, one for the edges of the grid, that takes the
* [[HSide]] and the single adjacent hex tile data value and one for the inner sides of the grid that takes the [[HSide]] and the two adjacent hex
* tile data values. */
def projLinksOptMap[B, BB <: SeqImut[B]](proj: HSysProjection)(f2: (HSide, A, A) => Option[B])(implicit build: ArrBuilder[B, BB]): BB =
proj.gChild.linksOptMap { hs =>
hs.tiles match {
case (c1, c2) if proj.gChild.hCenExists(c1) & proj.gChild.hCenExists(c2) => f2(hs, apply(proj.parent, c1), apply(proj.parent, c2))
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tiling/src/prid/phex/HGridSys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,13 @@ trait HGridSys extends Any with TGridSys
build.buffToBB(buff)
}

/** OptMaps over each inner hex Side's coordinate [[HSide]]. */
final def linksOptMap[B, ArrB <: SeqImut[B]](f: HSide => Option[B])(implicit build: ArrBuilder[B, ArrB]): ArrB = {
val buff = build.newBuff()
linksForeach { hs => f(hs).foreach(build.buffGrow(buff, _)) }
build.buffToBB(buff)
}

/** The [[HSide]] hex side coordinates. */
final def sides: HSideArr = sidesMap(hs => hs)

Expand Down
10 changes: 6 additions & 4 deletions Tiling/src/prid/phex/HSide.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ class HSide(val r: Int, val c: Int) extends HCenOrSide with TSide
case _ => excep("Invalid hex side.")
}

/** Returns a [[LineSeg]] for a regular Hex tile grid. */
def lineSeg(implicit grider: HGridSys): LineSeg = lineSegHC.oldLineSeg
/** May want to remove this method because it doesn't use projection. Will have to think about it. Returns a [[LineSeg]] for a regular Hex tile
* grid. */
def lineSegDepr(implicit grider: HGridSys): LineSeg = lineSegHC.oldLineSeg

/** Draws a [[LineSeg]], returning a [[LineSegDraw]] for a regular Hex tile grid. */
def draw(colour: Colour= Black, lineWidth: Double = 2)(implicit grider: HGridSys) : LineSegDraw = lineSeg.draw(colour, lineWidth)
/** May want to remove this method because it doesn't use projection. Will have to think about it. Draws a [[LineSeg]], returning a [[LineSegDraw]]
* for a regular Hex tile grid. */
def drawDepr(colour: Colour= Black, lineWidth: Double = 2)(implicit grider: HGridSys) : LineSegDraw = lineSegDepr.draw(colour, lineWidth)
}

/** Companion object for the HSide class, provides an apply factory method that throws an exception for an invalid Hex side coordinate. */
Expand Down

0 comments on commit 5ad271a

Please sign in to comment.