main.lua 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. -- virtual keyboard is both a demo app for chui and a useable keyboard module
  2. package.path = package.path .. ";../?.lua" -- needed for chui.lua to be located in parent directory
  3. local chui = require'chui'
  4. local keyboard = require'vqwerty' -- pressed keys are received with lovr.textinput & lovr.keypressed
  5. keyboard.pose:set(mat4(0, 1.6, -0.35):scale(0.03))
  6. -- chui lib doesn't have a rich input field, only the label widget
  7. -- we expand the lable with basic letter entry and backspace letter removal
  8. -- minimal text interactions, edit cursor fixed to the end
  9. local textbox_panel = chui.panel()
  10. textbox_panel.pose:set(mat4(0, 2, -2):scale(0.6))
  11. local textbox = textbox_panel:label{ span=4 } -- centered text
  12. textbox.textinput = function(self, char)
  13. self.text = self.text .. char
  14. end
  15. textbox.keypressed = function(self, key)
  16. if key == 'backspace' then
  17. self.text = self.text:sub(1, math.max(0, #self.text - 1))
  18. end
  19. end
  20. textbox_panel:layout()
  21. lovr.graphics.setBackgroundColor(1,1,1)
  22. function lovr.textinput(char)
  23. textbox:textinput(char)
  24. end
  25. function lovr.keypressed(key)
  26. textbox:keypressed(key)
  27. end
  28. function lovr.update(dt)
  29. keyboard:update(dt) -- allow keyboard panel to process interactions
  30. end
  31. function lovr.draw(pass)
  32. chui.draw(pass, true) -- draw all collected panels, as well as interaction pointer
  33. end