Advertisement
PaleoCrafter

Untitled

May 31st, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.85 KB | None | 0 0
  1. trait Component[+A <: Component[A]] extends Publisher {
  2.   this: A =>
  3.  
  4.   def init(channel: Publisher, context: Context): Unit = {
  5.     listenTo(channel)
  6.     this.context = context
  7.   }
  8.  
  9.   def update(mousePos: Point): Unit
  10.  
  11.   def dispose(): Unit = {
  12.     if (context != null)
  13.       deafTo(context)
  14.   }
  15.  
  16.   def bounds: Rectangle = {
  17.     if (_bounds == null || _bounds.width != width || _bounds.height != height || _bounds.start != position)
  18.       _bounds = Rectangle(position, width, height)
  19.     _bounds
  20.   }
  21.  
  22.   def screenBounds: Rectangle = {
  23.     if (_screenBounds == null || _screenBounds.width != width || _screenBounds.height != height || _screenBounds.start != screen)
  24.       _screenBounds = Rectangle(screen, width, height)
  25.     _screenBounds
  26.   }
  27.  
  28.   def x = position.x
  29.  
  30.   def y = position.y
  31.  
  32.   def width = size.width
  33.  
  34.   def height = size.height
  35.  
  36.   def hovered(mousePosition: Point) = screenBounds contains mousePosition
  37.  
  38.   def skin: Skin = Skin(this)
  39.  
  40.   def defaultSkin: Skin
  41.  
  42.   lazy val mc: Minecraft = FMLClientHandler.instance.getClient
  43.   val utils = GuiUtils
  44.   var position = Point(0, 0)
  45.   var screen = Point(0, 0)
  46.   var size = Size(0, 0)
  47.   var context: Context = _
  48.   var enabled: Boolean = true
  49.   var visible: Boolean = true
  50.   var parent: Panel = _
  51.   var name: String = _
  52.   var tooltip: String = _
  53.   var identifier: String = getClass.getSimpleName
  54.   private var _bounds: Rectangle = Rectangle(position, width, height)
  55.   private var _screenBounds: Rectangle = Rectangle(screen, width, height)
  56.  
  57.   object Skin {
  58.     private var defaults = HashMap.empty[String, Skin]
  59.     private var activeSkins = HashMap.empty[String, Skin]
  60.  
  61.     def apply[C <: Component[C]](comp: Component[C]): Skin = {
  62.       if (!activeSkins.contains(comp.identifier))
  63.         if (defaults.contains(comp.identifier)) {
  64.           activeSkins += comp.identifier -> defaults(comp.identifier)
  65.         } else {
  66.           defaults += comp.identifier -> comp.defaultSkin.asInstanceOf[Skin]
  67.           activeSkins += comp.identifier -> comp.defaultSkin.asInstanceOf[Skin]
  68.         }
  69.       activeSkins(comp.identifier)
  70.     }
  71.  
  72.  
  73.     def load(): Unit = {
  74.  
  75.     }
  76.  
  77.     def setDefault(id: String, skin: Skin): Unit = {
  78.       defaults += id -> skin
  79.     }
  80.  
  81.     def set(id: String, skin: Skin): Unit = {
  82.       activeSkins += id -> skin
  83.     }
  84.   }
  85.  
  86.   trait Skin {
  87.     val utils = GuiUtils
  88.  
  89.     protected def drawBackground(mousePos: Point, component: A): Unit = {
  90.       if (background != null) {
  91.         background.size = component.size
  92.         background.draw(mousePos, component.screen)
  93.       }
  94.     }
  95.  
  96.     protected def drawForeground(mousePos: Point, component: A): Unit
  97.  
  98.     def draw(mousePos: Point, component: A): Unit = {
  99.       drawBackground(mousePos, component)
  100.       drawForeground(mousePos, component)
  101.     }
  102.  
  103.     var background: Drawable = null
  104.   }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement