dialog.lua 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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
  62. function messagebox(name, message)
  63. return dialog_create(name,
  64. function()
  65. return ([[
  66. formspec_version[3]
  67. size[8,3]
  68. textarea[0.375,0.375;7.25,1.2;;;%s]
  69. button[3,1.825;2,0.8;ok;%s]
  70. ]]):format(message, fgettext("OK"))
  71. end,
  72. function(this, fields)
  73. if fields.ok then
  74. this:delete()
  75. return true
  76. end
  77. end,
  78. nil)
  79. end