Commit 315f0bcd authored by h2b's avatar h2b

Factory methods improved for consistency and usability.

parent 66f4d31d
......@@ -48,13 +48,13 @@ import Floating._
* @author h2b
*/
class GridLayout private (val rows: Int, val cols: Int, val cell: Cell,
val floating: Floating) extends Cell(cols*cell.width, rows*cell.height, cell.origin) with
val floating: Floating) extends Cell(cols*cell.width, rows*cell.height, cell.origin) with
Iterable[Cell] {
/**
* A copy of this grid layout at a new origin.
*/
override def at (origin: Pixel): GridLayout =
override def at (origin: Pixel): GridLayout =
new GridLayout(rows, cols, cell at origin, floating)
def iterator = floating match {
......@@ -75,7 +75,7 @@ class GridLayout private (val rows: Int, val cols: Int, val cell: Cell,
result
}
}
override def toString = s"GridLayout($rows, $cols, $cell, $floating)"
}
......@@ -99,35 +99,31 @@ object GridLayout {
/**
* Constructs a new grid layout of the specified number of rows and columns
* on a rectangular area of specified width and height.
* within the given cell.
*
* @param origin the upper-left corner of this layout, defaults to `(0, 0)`
* @param floating instructs the iterator to visit the cells either in
* horizontal (first left to right, then top to bottom) or vertical (vice
* versa) order (defaults to horizontal)
*
* @throws ArithmeticException if `rows` or `cols` is zero
*/
def onRectangle (rows: Int, cols: Int, width: Int, height: Int,
floating: Floating = DefaultFloating, origin: Pixel = Cell.DefaultOrigin): GridLayout = {
val cell = Cell(width/cols, height/rows, origin)
new GridLayout(rows, cols, cell, floating)
def withinCell (cell: Cell, rows: Int, cols: Int, floating: Floating = DefaultFloating): GridLayout = {
val inner = Cell(cell.width/cols, cell.height/rows, cell.origin)
new GridLayout(rows, cols, inner, floating)
}
/**
* Constructs a new grid layout of the specified number of rows and columns
* on the whole screen.
*
* @param origin the upper-left corner of this layout, defaults to `(0, 0)`
* @param floating instructs the iterator to visit the cells either in
* horizontal (first left to right, then top to bottom) or vertical (vice
* versa) order (defaults to horizontal)
*
* @throws ArithmeticException if `rows` or `cols` is zero
*/
def onScreen (rows: Int, cols: Int, floating: Floating = DefaultFloating,
origin: Pixel = Cell.DefaultOrigin): GridLayout = {
onRectangle(rows, cols, Screen.width, Screen.height, floating, origin)
def onScreen (rows: Int, cols: Int, floating: Floating = DefaultFloating): GridLayout = {
withinCell(Cell(Screen.width, Screen.height), rows, cols, floating)
}
}
......@@ -27,8 +27,8 @@ import de.h2b.scala.lib.simgraf.Pixel
@RunWith(classOf[JUnitRunner])
class GridLayoutTest extends FunSuite {
val hlayout = GridLayout.onRectangle(2, 3, 300, 200, origin=Pixel(50, 150))
val vlayout = GridLayout.onRectangle(2, 3, 300, 200, floating=Vertical) at Pixel(50, 150)
val hlayout = GridLayout.withinCell(Cell(300, 200, Pixel(50, 150)), 2, 3)
val vlayout = GridLayout.withinCell(Cell(300, 200), 2, 3, floating=Vertical) at Pixel(50, 150)
val cell = Cell(100, 100)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment