bjornbytes / lovr-docs

Documentation for LÖVR
44 stars 34 forks source link

Add Small 2D Button Example #122

Closed bjornbytes closed 1 year ago

bjornbytes commented 1 year ago

e.g.

function lovr.draw(pass)
  local width, height = lovr.system.getWindowDimensions()
  local projection = mat4():orthographic(0, width, 0, height, -10, 10)
  pass:setProjection(1, projection)
  pass:setViewPose(1, mat4():identity())
  pass:setDepthTest()

  local button = { x = 30, y = 30, w = 150, h = 60 }

  local mx, my = lovr.system.getMousePosition()
  local pressed = lovr.system.isMouseDown(1)
  local hovered = mx > button.x and mx < button.x + button.w and my > button.y and my < button.y + button.h

  pass:setColor(hovered and (pressed and { .25, .25, .27 } or { .2, .2, .22 }) or { .15, .15, .17 })
  pass:plane(button.x + button.w / 2, button.y + button.h / 2, 0, button.w, button.h)

  local font = lovr.graphics.getDefaultFont()
  font:setPixelDensity(1) -- set units to pixels instead of meters

  pass:setColor(1, 1, 1)
  pass:text('Hi!!!', button.x + button.w / 2, button.y + button.h / 2)
end