grid.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. --[[
  2. @classmod Grid
  3. ]]
  4. return pl.class {
  5. _name = "Grid",
  6. _init = function (self, s, w, h)
  7. self.show = true
  8. self.sz = s
  9. self.x, self.y = 0, 0
  10. self.selection = {}
  11. self.w, self.h = w, h
  12. self.show_coords = false
  13. end,
  14. reset = function (self)
  15. self.sz = Sled.tile_size
  16. self.x, self.y = 0, 0
  17. end,
  18. snap = function (self, x, y)
  19. return math.floor(x/self.sz)*self.sz, y and math.floor(y/self.sz)*self.sz
  20. end,
  21. highlight = function (self, x, y, c)
  22. self.selection_color = c or {1, 1, 1}
  23. local tx, ty = self:snap(x, y)
  24. self.tx, self.ty = tx, ty
  25. end,
  26. get_highlighted_tile_pos = function (self)
  27. return self.tx, self.ty
  28. end,
  29. update = function (self)
  30. if not Cursor then return end
  31. end,
  32. draw_coordinates = function (self)
  33. if not self.show_coords then return end
  34. local tx, ty = self.tx, self.ty
  35. local w, h = self.w, self.h
  36. _lg.setLineWidth(.1)
  37. _lg.setColor(.1, .1, .1, 1)
  38. _lg.line(tx, self.x, tx, ty)
  39. _lg.line(tx, ty, self.x, ty)
  40. end,
  41. resize = function (self, w, h)
  42. self.w, self.h = w, h
  43. end,
  44. move = function (self, x, y)
  45. self.x, self.y = x, y
  46. end,
  47. draw = function (self)
  48. if self.show then
  49. local w, h = self.w, self.h
  50. _lg.setColor(.2, .2, .2, .5)
  51. _lg.setLineWidth(.1)
  52. for y = self.y, self.y + h, self.sz do
  53. _lg.line(self.x, y, self.x + w, y)
  54. end
  55. for x = self.x, self.x + w, self.sz do
  56. _lg.line(x, self.y, x, self.y + h)
  57. end
  58. end
  59. if self.tx and self.ty then
  60. self:draw_coordinates ()
  61. _lg.setColor(.5, .5, .5)
  62. _lg.rectangle("line", self.tx, self.ty, Sled.tile_size, Sled.tile_size)
  63. end
  64. end
  65. }