import "luxe: world" for Entity, Text, Transform
import "luxe: assets" for Assets

import "globals" for Globals
import "math/vector" for Vector

class Tooltip{
  depth{100}
  color{[1, 1, 1, 1]}

  construct new(app){
    _ent = Entity.create(app.ui)
    _mat_font = Assets.material("luxe: material/font")
    Text.create(_ent, _mat_font, 8, "assets/fonts/BabyBlocks", color)

    Transform.create(_ent)
    Transform.set_pos(_ent, 64, 128, depth)
    Transform.set_snap(_ent, 1, 1, 0)

    Globals["Tooltip"] = this
  }

  tick(){
    var pos = Vector.new(Globals["UiMouse"])
    if(_active && pos){
      if(_x){
        pos.x = _x
      }
      if(_y){
        pos.y = _y
      }
      Transform.set_pos(_ent, pos.x, pos.y, depth)
    }
  }

  set(text) {
    set(text, null)
  }

  fix_y(value){
    _y = value
  }

  fix_x(value){
    _x = value
  }

  set(text, source){
    Text.set_text_buffer(_ent, text)
    Text.commit(_ent)
    _active = true
    _source = source
    _x = null
    _y = null
  }

  clear(){
    clear(_source)
  }

  clear(source){
    if(_source != source) {
      return
    }
    Text.set_text_buffer(_ent, "")
    Text.commit(_ent)
    _active = false
  }
}