chalkboard.lua 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. local esc = minetest.formspec_escape
  2. local function chalkboard_edit(pos)
  3. local meta = minetest.get_meta(pos)
  4. local title = meta:get_string('infotext')
  5. local body = meta:get_string('body')
  6. local formspec =
  7. 'formspec_version[3]'..
  8. 'size[16,9]'..
  9. 'background[0,0;16,9;signs_chalkboard_bg.png]'..
  10. 'field[0.25,.75;15.5,.75;title;Title:;'..esc(title)..']'..
  11. 'textarea[.25,1.75;15.5,5.5;body;;'..esc(body)..']'..
  12. 'button_exit[4.25,7.5;3,1;save;Save]'..
  13. 'button_exit[8.75,7.5;3,1;preview;Preview]'
  14. return formspec
  15. end
  16. local function chalkboard_read(pos)
  17. local meta = minetest.get_meta(pos)
  18. local title = meta:get_string('infotext')
  19. local body = meta:get_string('body')
  20. local formspec =
  21. 'formspec_version[3]'..
  22. 'size[16,9]'..
  23. 'background[0,0;16,9;signs_chalkboard_bg.png]'..
  24. 'hypertext[.25,.25;15.5,1.5;;<center><style color=white size=40>'..esc(title)..'</style></center>]'..
  25. 'textarea[.25,1.75;15.5,7;;;'..esc(body)..']'
  26. return formspec
  27. end
  28. minetest.register_node('signs:chalkboard', {
  29. description = 'Chalkboard',
  30. drawtype = 'mesh',
  31. mesh = 'signs_chalkboard.obj',
  32. tiles = {'signs_chalkboard.png'},
  33. paramtype2 = 'facedir',
  34. paramtype = 'light',
  35. selection_box = {
  36. type = 'fixed',
  37. fixed = {-1, -.75, .4375, 1, .5, .5},
  38. },
  39. collision_box = {
  40. type = 'fixed',
  41. fixed = {-1, -.75, .4375, 1, .5, .5},
  42. },
  43. groups = {breakable=1},
  44. after_place_node = function(pos, placer)
  45. local meta = minetest.get_meta(pos)
  46. meta:set_string('infotext', '')
  47. meta:set_string('body', '')
  48. end,
  49. on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
  50. local name = clicker:get_player_name()
  51. if not minetest.is_protected(pos, name) or minetest.check_player_privs(name, { server = true }) then
  52. signs.player_pos[name] = pos
  53. minetest.show_formspec(name, 'signs:chalkboard_edit', chalkboard_edit(pos))
  54. else
  55. minetest.show_formspec(name, 'signs:chalkboard_read', chalkboard_read(pos))
  56. end
  57. end,
  58. })
  59. minetest.register_on_player_receive_fields(function(player, formname, fields)
  60. if formname == 'signs:chalkboard_edit' then
  61. local name = player:get_player_name()
  62. local pos = signs.player_pos[name]
  63. local meta = minetest.get_meta(pos)
  64. if fields.save then
  65. meta:set_string('infotext', fields.title)
  66. meta:set_string('body', fields.body)
  67. minetest.log('action', (name or '')..' wrote \''..fields.title..'\' to chalkboard at '..minetest.pos_to_string(pos))
  68. elseif fields.preview then
  69. meta:set_string('infotext', fields.title)
  70. meta:set_string('body', fields.body)
  71. minetest.log('action', (name or '')..' wrote \''..fields.title..'\' to chalkboard at '..minetest.pos_to_string(pos))
  72. minetest.show_formspec(name, 'signs:chalkboard_read', chalkboard_read(pos))
  73. end
  74. end
  75. end)