isac-java/src/java/isac/gui/mawen/editor/Box.scala
author Walther Neuper <wneuper@ist.tugraz.at>
Wed, 06 Sep 2017 13:45:58 +0200
changeset 5199 3799cb07f123
parent 5187 5f4cc0eadbaa
child 5203 8fcdb69044d9
permissions -rw-r--r--
reformat package isac.gui.mawen.editor
s1520454056@5057
     1
package isac.gui.mawen.editor
s1520454056@5057
     2
s1520454056@5144
     3
import java.awt.Rectangle
s1520454056@5057
     4
import java.awt.Graphics
wneuper@5159
     5
import isac.gui.mawen.syntax.Ast
s1520454056@5093
     6
import isac.gui.mawen.syntax.Ast._
s1520454056@5057
     7
wneuper@5165
     8
/**
wneuper@5165
     9
 * Holds all data required to draw the <code>Ast</code>.
wneuper@5165
    10
 * The "Empty*Box"es initalise respective data before calculation.
wneuper@5165
    11
 */
s1520454056@5162
    12
object Box {
s1520454056@5057
    13
  
s1520454056@5162
    14
  sealed abstract class Box(val ast: Ast)
s1520454056@5164
    15
  class EmptyBox(val level: Int, val x0: Int, val y0: Int, override val ast: Ast) extends Box(ast) {
s1520454056@5164
    16
    def apply (level: Int, x0: Int, y0: Int, ast: Ast) = new EmptyBox(level, x0, y0, ast)
s1520454056@5164
    17
    def toEmptyBox() = new EmptyBox(level, x0, y0, ast)
s1520454056@5164
    18
    def toEmptyBox(ast: Ast) = new EmptyBox(level, x0, y0, ast)
s1520454056@5164
    19
    def toEmptyBox(x: Int, y: Int, ast: Ast) = new EmptyBox(level, x, y, ast)
s1520454056@5164
    20
  }
s1520454056@5162
    21
  case class EmptyLeafOpBox(override val level: Int, override val x0: Int, override val y0: Int, override val ast: Ast) extends EmptyBox(level, x0, y0, ast)
s1520454056@5162
    22
  case class EmptyUniOpBox(override val level: Int, override val x0: Int, override val y0: Int, param: Ast, override val ast: Ast) extends EmptyBox(level, x0, y0, ast)
s1520454056@5163
    23
  case class EmptyBinOpBox(override val level: Int, override val x0: Int, override val y0: Int, operator: Ast, paraml: Ast, paramr: Ast, override val ast: Ast) extends EmptyBox(level, x0, y0, ast)
wneuper@5159
    24
s1520454056@5164
    25
  case class DrawBox(name: String, val level: Int, val x: Int, val y: Int, val width: Int, val height: Int, x0 : Int, y0 : Int, override val ast: Ast) extends Box(ast) {
s1520454056@5162
    26
    var children : List[DrawBox] = List.empty[DrawBox]
s1520454056@5162
    27
    var isBoxed: Boolean  = false
s1520454056@5162
    28
    var colorCode : Int = 0
s1520454056@5162
    29
    var boxedElement : Int = 0
s1520454056@5101
    30
  }
s1520454056@5162
    31
  def isLeaf(b: DrawBox) : Boolean = b.children.length == 0
s1520454056@5162
    32
  
s1520454056@5162
    33
  def Contains(parent: DrawBox, box: DrawBox) : Boolean  =
s1520454056@5162
    34
    box == parent || parent.children.exists( y => Contains(y, box))
s1520454056@5162
    35
s1520454056@5162
    36
  def draw(b: DrawBox, g: Graphics) : Unit = draw(b, g, true) 
s1520454056@5162
    37
  def draw(b: DrawBox, g: Graphics, withChildren: Boolean) : Unit = {
s1520454056@5162
    38
    g.drawRect(b.x, b.y - b.height, b.width, b.height)
s1520454056@5162
    39
    if (withChildren) b.children.foreach(draw(_,g))
s1520454056@5162
    40
  }
s1520454056@5162
    41
  def drawBox(b: DrawBox, g: Graphics) {
s1520454056@5162
    42
    if (b.isBoxed) {
s1520454056@5162
    43
      g.setColor(Settings.CollorMapping(b.colorCode))
s1520454056@5187
    44
      g.drawRect(b.x, b.y - b.height, b.width, b.height)
s1520454056@5144
    45
      g.setColor(java.awt.Color.BLACK)
s1520454056@5144
    46
    }
s1520454056@5144
    47
  }
s1520454056@5162
    48
  def BoxIn(b: DrawBox, rect: Rectangle) : Boolean = 
s1520454056@5162
    49
    rect.contains(b.x, b.y) || rect.contains(b.x + b.width, b.y) ||
s1520454056@5162
    50
    rect.contains(b.x, b.y + b.height) || rect.contains(b.x + b.width, b.y + b.height)
s1520454056@5057
    51
}