Element.scala
author Chengsong
Fri, 15 Mar 2019 10:46:46 +0000
changeset 3 f15dccc42c7b
parent 0 902326e1615a
permissions -rwxr-xr-x
removing PRED why should it still be there after so many rounds of experiments? It should have been removed long ago since its existence will always make the compiler report errors. mysterious.:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
Chengsong
parents:
diff changeset
     1
import Element.elem
Chengsong
parents:
diff changeset
     2
abstract class Element{
Chengsong
parents:
diff changeset
     3
  def contents: Array[String]
Chengsong
parents:
diff changeset
     4
Chengsong
parents:
diff changeset
     5
  def height: Int = contents.length
Chengsong
parents:
diff changeset
     6
  def width: Int = contents(0).length
Chengsong
parents:
diff changeset
     7
Chengsong
parents:
diff changeset
     8
  
Chengsong
parents:
diff changeset
     9
Chengsong
parents:
diff changeset
    10
  def above(that: Element): Element = {
Chengsong
parents:
diff changeset
    11
    //new ArrayElement(this.contents ++ that.contents)
Chengsong
parents:
diff changeset
    12
    val this1 = this widen that.width
Chengsong
parents:
diff changeset
    13
    val that1 = that widen this.width
Chengsong
parents:
diff changeset
    14
    elem(this1.contents ++ that1.contents)
Chengsong
parents:
diff changeset
    15
  }
Chengsong
parents:
diff changeset
    16
  def left_align(that: Element): Element = {
Chengsong
parents:
diff changeset
    17
    if (this.width == that.width){
Chengsong
parents:
diff changeset
    18
      this above that
Chengsong
parents:
diff changeset
    19
    }
Chengsong
parents:
diff changeset
    20
    else if (this.width < that.width) {
Chengsong
parents:
diff changeset
    21
      (this beside elem(' ', that.width - this.width, this.height)) above that
Chengsong
parents:
diff changeset
    22
    }
Chengsong
parents:
diff changeset
    23
    else {
Chengsong
parents:
diff changeset
    24
      this above (that beside elem(' ', this.width - that.width, that.height))
Chengsong
parents:
diff changeset
    25
    }
Chengsong
parents:
diff changeset
    26
  }
Chengsong
parents:
diff changeset
    27
  def up_align(that: Element): Element = {
Chengsong
parents:
diff changeset
    28
    if (this.height == that.height){
Chengsong
parents:
diff changeset
    29
      this beside that
Chengsong
parents:
diff changeset
    30
    }
Chengsong
parents:
diff changeset
    31
    else if (this.height < that.height) {
Chengsong
parents:
diff changeset
    32
      (this above elem(' ', this.width, that.height - this.height)) beside that
Chengsong
parents:
diff changeset
    33
    }
Chengsong
parents:
diff changeset
    34
    else {
Chengsong
parents:
diff changeset
    35
      this beside (that above elem(' ', that.width, this.height - that.height))
Chengsong
parents:
diff changeset
    36
    }  
Chengsong
parents:
diff changeset
    37
  }
Chengsong
parents:
diff changeset
    38
  def beside(that: Element): Element = {
Chengsong
parents:
diff changeset
    39
    val this1 = this heighten that.height
Chengsong
parents:
diff changeset
    40
    val that1 = that heighten this.height
Chengsong
parents:
diff changeset
    41
    elem(
Chengsong
parents:
diff changeset
    42
      for ((line1, line2) <- this1.contents zip that1.contents)
Chengsong
parents:
diff changeset
    43
      yield line1 + line2)
Chengsong
parents:
diff changeset
    44
  }
Chengsong
parents:
diff changeset
    45
  def widen(w: Int): Element = 
Chengsong
parents:
diff changeset
    46
    if(w <= width) this
Chengsong
parents:
diff changeset
    47
    else {
Chengsong
parents:
diff changeset
    48
      val left = Element.elem(' ', (w - width) / 2, height)
Chengsong
parents:
diff changeset
    49
      var right = Element.elem(' ', w - width - left.width, height)
Chengsong
parents:
diff changeset
    50
      left beside this beside right
Chengsong
parents:
diff changeset
    51
    }
Chengsong
parents:
diff changeset
    52
  def heighten(h: Int): Element = 
Chengsong
parents:
diff changeset
    53
    if (h <= height) this
Chengsong
parents:
diff changeset
    54
    else {
Chengsong
parents:
diff changeset
    55
      val top = Element.elem(' ', width, (h - height) / 2)
Chengsong
parents:
diff changeset
    56
      val bot = Element.elem(' ', width, h - height - top.height)
Chengsong
parents:
diff changeset
    57
      top above this above bot
Chengsong
parents:
diff changeset
    58
    }
Chengsong
parents:
diff changeset
    59
  override def toString = contents mkString "\n"
Chengsong
parents:
diff changeset
    60
}
Chengsong
parents:
diff changeset
    61
object Element {
Chengsong
parents:
diff changeset
    62
  private class ArrayElement(
Chengsong
parents:
diff changeset
    63
    val contents: Array[String]
Chengsong
parents:
diff changeset
    64
  ) extends Element
Chengsong
parents:
diff changeset
    65
Chengsong
parents:
diff changeset
    66
  private class LineElement(s: String) extends Element {
Chengsong
parents:
diff changeset
    67
    val contents = Array(s)
Chengsong
parents:
diff changeset
    68
    override def width = s.length
Chengsong
parents:
diff changeset
    69
    override def height = 1
Chengsong
parents:
diff changeset
    70
  }
Chengsong
parents:
diff changeset
    71
Chengsong
parents:
diff changeset
    72
  private class UniformElement(
Chengsong
parents:
diff changeset
    73
    ch: Char,
Chengsong
parents:
diff changeset
    74
    override val width: Int,
Chengsong
parents:
diff changeset
    75
    override val height: Int
Chengsong
parents:
diff changeset
    76
  ) extends Element {
Chengsong
parents:
diff changeset
    77
    private val line = ch.toString * width
Chengsong
parents:
diff changeset
    78
    def contents = Array.fill(height)(line)
Chengsong
parents:
diff changeset
    79
  }
Chengsong
parents:
diff changeset
    80
Chengsong
parents:
diff changeset
    81
  def elem(contents: Array[String]): Element =
Chengsong
parents:
diff changeset
    82
    new ArrayElement(contents)
Chengsong
parents:
diff changeset
    83
    
Chengsong
parents:
diff changeset
    84
  def elem(chr: Char, width: Int, height: Int): Element =
Chengsong
parents:
diff changeset
    85
    new UniformElement(chr, width, height)
Chengsong
parents:
diff changeset
    86
    
Chengsong
parents:
diff changeset
    87
  def elem(line: String): Element =
Chengsong
parents:
diff changeset
    88
      new LineElement(line)
Chengsong
parents:
diff changeset
    89
}