inspector.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. local inspector = {}
  2. function inspector:createOffsetFunction(x, ...)
  3. local rest = {...}
  4. return function()
  5. return self.x + x, unpack(rest)
  6. end
  7. end
  8. function inspector:toggleActive()
  9. self.active = not self.active
  10. lib.flux.to(self, .25, {x = self.active and 0 or -self.config.width}):ease('backout')
  11. end
  12. function inspector:smoothX()
  13. local targetX = self.active and 0 or -self.config.width
  14. --self.x = util.lerp(self.x, targetX, 16 * lib.tick.rate)
  15. end
  16. function inspector:updateCursor(mouse, components)
  17. local mx, my = unpack(mouse)
  18. local contains = false
  19. contains = contains or self.dropdown:contains(mx, my)
  20. for i = 1, #components do
  21. contains = contains or f.try(components[i].contains, components[i], mx, my)
  22. end
  23. --love.mouse.setCursor(contains and lib.gooey.controller.cursors.hand or lib.gooey.controller.cursors.default)
  24. end
  25. function inspector:setupComponents(editing)
  26. local subject = util.get(app, editing)
  27. if subject.editor then
  28. local components = {}
  29. local y = 44
  30. for i = 1, #subject.editor.sections do
  31. local section = subject.editor.sections[i]
  32. local header = self.gooey:add(lib.gooey.label, 'prop.' .. section.title)
  33. header.geometry = self:createOffsetFunction(8, y)
  34. header.label = section.title
  35. table.insert(components, header)
  36. y = y + 20
  37. for j = 1, #section do
  38. local prop = section[j]
  39. local editor = self.gooey:add(lib.gooey.editor, 'config.' .. editing .. '.' .. prop, {value = subject.config[prop]})
  40. editor.label = prop:gsub('[A-Z]', function(x) return ' ' .. x:lower() end)
  41. editor.valueSubject:onNext(editor.value)
  42. editor.geometry = self:createOffsetFunction(8, y, self.config.width - 16)
  43. editor.valueSubject:subscribe(function(newValue)
  44. subject.config[prop] = tonumber(newValue) or newValue
  45. end)
  46. y = y + 20
  47. table.insert(components, editor)
  48. end
  49. y = y + 20
  50. end
  51. return components
  52. else
  53. local config = subject.config or subject
  54. return util.map(util.keys(config), function(prop, i)
  55. local editor = self.gooey:add(lib.gooey.editor, 'config.' .. editing .. '.' .. prop, {value = config[prop]})
  56. editor.label = prop:gsub('[A-Z]', function(x) return ' ' .. x:lower() end)
  57. editor.valueSubject:onNext(editor.value)
  58. editor.geometry = self:createOffsetFunction(8, 24 + 20 * i, self.config.width - 16)
  59. editor.valueSubject:subscribe(function(newValue)
  60. subject.config[prop] = tonumber(newValue) or newValue
  61. end)
  62. return editor
  63. end)
  64. end
  65. end
  66. function inspector:draw(_, editors)
  67. local u, v = g.getDimensions()
  68. local x = self.x
  69. local width = self.config.width
  70. if x > 0 then
  71. width = width + x
  72. x = 0
  73. end
  74. g.setColor(35, 35, 35, 220)
  75. g.rectangle('fill', x, 0, width, v)
  76. if editors then
  77. g.white()
  78. for i = 1, #editors do
  79. self.gooey:render(editors[i])
  80. end
  81. end
  82. self.gooey:render(self.dropdown)
  83. return -10000
  84. end
  85. return inspector