dialog.lua 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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)
  39. if not self.hidden then
  40. self.hidden = true
  41. self.eventhandler(self, "DialogHide")
  42. end
  43. end,
  44. show = function(self)
  45. if self.hidden then
  46. self.hidden = false
  47. self.eventhandler(self, "DialogShow")
  48. end
  49. end,
  50. delete = function(self)
  51. if self.parent ~= nil then
  52. self.parent:show()
  53. end
  54. ui.delete(self)
  55. end,
  56. set_parent = function(self,parent) self.parent = parent end
  57. }
  58. dialog_metatable.__index = dialog_metatable
  59. function dialog_create(name,get_formspec,buttonhandler,eventhandler)
  60. local self = {}
  61. self.name = name
  62. self.type = "toplevel"
  63. self.hidden = true
  64. self.data = {}
  65. self.formspec = get_formspec
  66. self.buttonhandler = buttonhandler
  67. self.user_eventhandler = eventhandler
  68. setmetatable(self,dialog_metatable)
  69. ui.add(self)
  70. return self
  71. end
  72. function messagebox(name, message)
  73. return dialog_create(name,
  74. function()
  75. return ([[
  76. formspec_version[3]
  77. size[8,3]
  78. textarea[0.375,0.375;7.25,1.2;;;%s]
  79. button[3,1.825;2,0.8;ok;%s]
  80. ]]):format(message, fgettext("OK"))
  81. end,
  82. function(this, fields)
  83. if fields.ok then
  84. this:delete()
  85. return true
  86. end
  87. end,
  88. nil)
  89. end