dialog.lua 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. --Minetest
  2. --Copyright (C) 2014 sapier
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --this program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. local function dialog_event_handler(self,event)
  18. if self.user_eventhandler == nil or
  19. self.user_eventhandler(event) == false then
  20. --close dialog on esc
  21. if event == "MenuQuit" then
  22. self:delete()
  23. return true
  24. end
  25. end
  26. end
  27. local dialog_metatable = {
  28. eventhandler = dialog_event_handler,
  29. get_formspec = function(self)
  30. if not self.hidden then return self.formspec(self.data) end
  31. end,
  32. handle_buttons = function(self,fields)
  33. if not self.hidden then return self.buttonhandler(self,fields) end
  34. end,
  35. handle_events = function(self,event)
  36. if not self.hidden then return self.eventhandler(self,event) end
  37. end,
  38. hide = function(self) self.hidden = true end,
  39. show = function(self) self.hidden = false end,
  40. delete = function(self)
  41. if self.parent ~= nil then
  42. self.parent:show()
  43. end
  44. ui.delete(self)
  45. end,
  46. set_parent = function(self,parent) self.parent = parent end
  47. }
  48. dialog_metatable.__index = dialog_metatable
  49. function dialog_create(name,get_formspec,buttonhandler,eventhandler)
  50. local self = {}
  51. self.name = name
  52. self.type = "toplevel"
  53. self.hidden = true
  54. self.data = {}
  55. self.formspec = get_formspec
  56. self.buttonhandler = buttonhandler
  57. self.user_eventhandler = eventhandler
  58. setmetatable(self,dialog_metatable)
  59. ui.add(self)
  60. return self
  61. end