cursor.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --[[
  2. @classmod Cursor
  3. ]]
  4. local lume = require (LIB_PATH .. "lume")
  5. return pl.class {
  6. _name = "Cursor",
  7. _init = function (self, s, grid)
  8. self.x, self.y = 0, 0
  9. self.rx, self.ry = 0, 0
  10. self.dx = 0
  11. self.dy = 0
  12. self.w = s
  13. self.h = s
  14. self.show = true
  15. self.color = {.1, .1, .1}
  16. self.grid = grid
  17. self.mode = "focused"
  18. end,
  19. moved = function (self)
  20. return lume.all({self.dx, self.old_dx, self.dy, self.old_dy},
  21. function (v) return v == 0 end)
  22. end,
  23. update = function (self)
  24. if Sled.state == "splash" then return end
  25. local rx, ry = _lm.getPosition()
  26. self.rx, self.ry = rx, ry
  27. local mx, my = Sled.current_screen:get_mouse_position ()
  28. mx, my = Sled.grid:snap(mx, my)
  29. self.x, self.y = mx, my
  30. self.ox, self.oy = self.ox or rx, self.oy or ry
  31. self.dx, self.dy = self.ox- rx, self.oy - ry
  32. self.x, self.y = mx, my
  33. local bx, by, bw, bh
  34. if Sled.state == "map" then
  35. self.w, self.h = Sled.tile_size, Sled.tile_size
  36. Sled.grid.sz = Sled.tile_size
  37. bx, by = 0, 0
  38. bw, bh = Sled.grid.w, Sled.grid.h
  39. elseif Sled.state == "focused" then
  40. self.w, self.h = 1, 1
  41. Sled.grid.sz = 1
  42. bx, by = Sled.grid.tx, Sled.grid.ty
  43. bw, bh = 8, 8
  44. end
  45. if self.x >= bx and self.y >=by and self.x < bw+bx and self.y < bh+by then
  46. self.in_canvas = true
  47. else
  48. self.in_canvas = false
  49. end
  50. self.ox, self.oy = rx, ry
  51. self.old_dx, self.old_dy = self:get_delta()
  52. end,
  53. draw = function (self)
  54. _lg.setColor(1, 1, 1)
  55. if self.show then
  56. if self.in_canvas then
  57. _lg.setColor(.1, .8, .5 ,1)
  58. else
  59. _lg.setColor(.1,.1,.1, .1)
  60. end
  61. _lg.setLineWidth(.1)
  62. _lg.rectangle("line", self.x, self.y, self.w, self.h)
  63. end
  64. end,
  65. get_position = function (self)
  66. return _lm.getPosition()
  67. end,
  68. get_delta = function (self)
  69. return self.dx, self.dy
  70. end
  71. }