init.lua 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. --[[
  2. signs mod for Minetest - Various signs with text displayed on
  3. (c) Pierre-Yves Rollo
  4. This file is part of signs.
  5. signs is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. signs 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with signs. If not, see <http://www.gnu.org/licenses/>.
  15. --]]
  16. signs_api = {}
  17. signs_api.name = minetest.get_current_modname()
  18. signs_api.path = minetest.get_modpath(signs_api.name)
  19. -- Load support for intllib.
  20. local S, NS = dofile(signs_api.path.."/intllib.lua")
  21. signs_api.intllib = S
  22. local F = function(...) return minetest.formspec_escape(S(...)) end
  23. function signs_api.set_display_text(pos, text, font)
  24. local meta = minetest.get_meta(pos)
  25. meta:set_string("display_text", text)
  26. if text and text ~= "" then
  27. meta:set_string("infotext", "\""..text.."\"")
  28. else
  29. meta:set_string("infotext", "")
  30. end
  31. if font then
  32. meta:set_string("font", font)
  33. end
  34. display_api.update_entities(pos)
  35. end
  36. function signs_api.set_formspec(pos)
  37. local meta = minetest.get_meta(pos)
  38. local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
  39. if ndef and ndef.display_entities
  40. and ndef.display_entities["signs:display_text"] then
  41. local maxlines = ndef.display_entities["signs:display_text"].maxlines
  42. local fs, y
  43. if maxlines == 1 then
  44. fs = "field[0.5,0.7;5.5,1;display_text;"..F("Text")..
  45. ";${display_text}]"
  46. y = 1.2
  47. else
  48. local extralabel = ""
  49. if maxlines then
  50. extralabel = F(" (first %s lines only)"):format(maxlines)
  51. end
  52. fs = "textarea[0.5,0.7;5.5,2;display_text;"..F("Text")..""..
  53. extralabel..";${display_text}]"
  54. y = 2.4
  55. end
  56. fs = fs.."button[1,"..y..";2,1;font;"..F("Font").."]"
  57. fs = fs.."button_exit[3,"..y..";2,1;ok;"..F("Write").."]"
  58. y = y + 0.8
  59. fs = "size[6,"..y.."]"..default.gui_bg..
  60. default.gui_bg_img..default.gui_slots..fs
  61. meta:set_string("formspec", fs)
  62. end
  63. end
  64. function signs_api.on_receive_fields(pos, formname, fields, player)
  65. if not minetest.is_protected(pos, player:get_player_name()) then
  66. if fields and (fields.ok or fields.key_enter) then
  67. signs_api.set_display_text(pos, fields.display_text)
  68. end
  69. if fields and (fields.font) then
  70. signs_api.set_display_text(pos, fields.display_text)
  71. font_api.show_font_list(player, pos)
  72. end
  73. end
  74. end
  75. -- On place callback for direction signs
  76. -- (chooses which sign according to look direction)
  77. function signs_api.on_place_direction(itemstack, placer, pointed_thing)
  78. local name = itemstack:get_name()
  79. local ndef = minetest.registered_nodes[name]
  80. local restriction = display_api.is_rotation_restricted()
  81. local bdir = {
  82. x = pointed_thing.under.x - pointed_thing.above.x,
  83. y = pointed_thing.under.y - pointed_thing.above.y,
  84. z = pointed_thing.under.z - pointed_thing.above.z}
  85. local pdir = placer:get_look_dir()
  86. local ndir, test
  87. if ndef.paramtype2 == "facedir" then
  88. -- If legacy mode, only accept upright nodes
  89. if restriction and bdir.x == 0 and bdir.z == 0 then
  90. -- Ceiling or floor pointed (facedir chosen from player dir)
  91. ndir = minetest.dir_to_facedir({x=pdir.x, y=0, z=pdir.z})
  92. else
  93. -- Wall pointed or no rotation restriction
  94. ndir = minetest.dir_to_facedir(bdir, not restriction)
  95. end
  96. test = { [0]=-pdir.x, pdir.z, pdir.x, -pdir.z, -pdir.x, [8]=pdir.x }
  97. end
  98. if ndef.paramtype2 == "wallmounted" then
  99. ndir = minetest.dir_to_wallmounted(bdir)
  100. -- If legacy mode, only accept upright nodes
  101. if restriction and (ndir == 0 or ndir == 1) then
  102. ndir = minetest.dir_to_wallmounted({x=pdir.x, y=0, z=pdir.z})
  103. end
  104. test = { [0]=-pdir.x, -pdir.x, pdir.z, -pdir.z, -pdir.x, pdir.x}
  105. end
  106. -- Only for direction signs
  107. -- TODO:Maybe improve ground and ceiling placement in every directions
  108. if ndef.signs_other_dir then
  109. if test[ndir] > 0 then
  110. itemstack:set_name(ndef.signs_other_dir)
  111. end
  112. itemstack = minetest.item_place(itemstack, placer, pointed_thing, ndir)
  113. itemstack:set_name(name)
  114. return itemstack
  115. else
  116. return minetest.item_place(itemstack, placer, pointed_thing, ndir)
  117. end
  118. end
  119. -- Handles screwdriver rotation
  120. -- (see "if" block below for rotation restriction mode).
  121. signs_api.on_rotate = function(pos, node, player, mode, new_param2)
  122. -- If rotation mode is 1 and sign is directional, swap direction between
  123. -- each rotation.
  124. if mode == 1 then
  125. local ndef = minetest.registered_nodes[node.name]
  126. if ndef.signs_other_dir then
  127. -- Switch direction
  128. node = {name = ndef.signs_other_dir,
  129. param1 = node.param1, param2 = node.param2}
  130. minetest.swap_node(pos, node)
  131. display_api.update_entities(pos)
  132. -- Rotate only if not "main" sign
  133. -- TODO:Improve detection of "main" direction sign
  134. if ndef.groups and ndef.groups.not_in_creative_inventory then
  135. return display_api.on_rotate(pos, node, player, mode, new_param2)
  136. else
  137. return true
  138. end
  139. end
  140. end
  141. return display_api.on_rotate(pos, node, player, mode, new_param2)
  142. end
  143. -- Legacy mode with rotation restriction
  144. -- TODO:When MT < 5.0 no more in use, to be removed
  145. if display_api.is_rotation_restricted() then
  146. signs_api.on_rotate = function(pos, node, player, mode, new_param2)
  147. -- If rotation mode is 2 and sign is directional, swap direction.
  148. -- Otherwise use display_api's on_rotate function.
  149. if mode == 2 then
  150. local ndef = minetest.registered_nodes[node.name]
  151. if ndef.signs_other_dir then
  152. minetest.swap_node(pos, {name = ndef.signs_other_dir,
  153. param1 = node.param1, param2 = node.param2})
  154. display_api.update_entities(pos)
  155. return true
  156. end
  157. end
  158. return display_api.on_rotate(pos, node, player, mode, new_param2)
  159. end
  160. end
  161. function signs_api.register_sign(mod, name, model)
  162. -- Default fields
  163. local fields = {
  164. sunlight_propagates = true,
  165. paramtype = "light",
  166. paramtype2 = "facedir",
  167. drawtype = "nodebox",
  168. node_box = {
  169. type = "fixed",
  170. fixed = {-model.width/2, -model.height/2, 0.5,
  171. model.width/2, model.height/2, 0.5 - model.depth},
  172. },
  173. groups = {choppy=2, dig_immediate=2, not_blocking_trains=1, display_api=1},
  174. sounds = default.node_sound_defaults(),
  175. display_entities = {
  176. ["signs:display_text"] = {
  177. on_display_update = font_api.on_display_update,
  178. depth = 0.5 - display_api.entity_spacing - model.depth,
  179. size = { x = model.width, y = model.height },
  180. aspect_ratio = 1/2,
  181. maxlines = 1,
  182. },
  183. },
  184. on_place = display_api.on_place,
  185. on_construct = function(pos)
  186. local ndef = minetest.registered_nodes[minetest.get_node(pos).name]
  187. local meta = minetest.get_meta(pos)
  188. meta:set_string("font", ndef.display_entities.font_name or
  189. font_api.get_default_font_name())
  190. signs_api.set_formspec(pos)
  191. display_api.on_construct(pos)
  192. end,
  193. on_destruct = display_api.on_destruct,
  194. on_rotate = signs_api.on_rotate,
  195. on_receive_fields = signs_api.on_receive_fields,
  196. on_punch = function(pos, node, player, pointed_thing)
  197. signs_api.set_formspec(pos)
  198. display_api.update_entities(pos)
  199. end,
  200. }
  201. -- Node fields override
  202. for key, value in pairs(model.node_fields) do
  203. if key == "groups" then
  204. for key2, value2 in pairs(value) do
  205. fields[key][key2] = value2
  206. end
  207. else
  208. fields[key] = value
  209. end
  210. end
  211. if not fields.wield_image then fields.wield_image = fields.inventory_image end
  212. -- Entity fields override
  213. for key, value in pairs(model.entity_fields) do
  214. fields.display_entities["signs:display_text"][key] = value
  215. end
  216. minetest.register_node(mod..":"..name, fields)
  217. end
  218. -- Text entity for all signs
  219. display_api.register_display_entity("signs:display_text")