init.lua 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. local S = minetest.get_translator("in_game_guide")
  2. in_game_guide = {}
  3. local pages = {}
  4. local root_page
  5. local root_page_back_button
  6. function in_game_guide.set_root_page_back_button_name(name)
  7. assert(type(name) == "nil" or type(name) == "string",
  8. "Cannot set non-string or nil value as back_button name.")
  9. root_page_back_button = name
  10. end
  11. --generates a formspec from a page definition
  12. local function to_formspec(page_definition)
  13. --for background of title TODO: fix this
  14. local title_border_offset = string.len(page_definition.title)
  15. title_border_offset = title_border_offset / 8.5 + 0.5
  16. local spec =
  17. {
  18. "size[12,8]" ..
  19. "real_coordinates[true]" ..
  20. "background[3,0.6;8,0.7;in_game_guide_background.png]" ..
  21. "label[3.05,1;" .. page_definition.title .."]" ..
  22. "background[3,2;8,5;in_game_guide_background.png]" ..
  23. "textarea[3,2;8,5;;;" .. page_definition.text .. "]"
  24. }
  25. --back button
  26. if (page_definition.parent and page_definition ~= root_page) or
  27. (root_page_back_button and page_definition == root_page)
  28. then
  29. local back_button = "back"
  30. if page_definition == root_page
  31. then
  32. back_button = root_page_back_button
  33. end
  34. table.insert(spec, "button[0.5,1;1,1;" .. back_button .. ";<]")
  35. end
  36. --list of child pages
  37. if page_definition.children
  38. then
  39. table.insert(spec, "table[0.5,2;2.5,5;page_children;")
  40. local children = {}
  41. for _, child in ipairs(page_definition.children)
  42. do
  43. child = pages[child]
  44. table.insert(children, child.title);
  45. table.insert(children, ",")
  46. end
  47. children[#children] = nil --remove last comma
  48. table.insert(spec, table.concat(children))
  49. table.insert(spec, ";1]")
  50. end
  51. return page_definition.name, table.concat(spec)
  52. end
  53. function in_game_guide.set_root_page(name)
  54. assert(pages[name], "There is no page registered with name " .. name)
  55. root_page = pages[name]
  56. end
  57. function in_game_guide.show_root_page(playername, return_button_name)
  58. assert(root_page, "There is no root page set")
  59. minetest.show_formspec(playername, to_formspec(root_page, return_button_name))
  60. end
  61. minetest.register_on_player_receive_fields(function(player, formname, fields)
  62. local page = pages[formname]
  63. if not page
  64. then
  65. return false
  66. end
  67. if fields.back
  68. then
  69. local parent = page.parent
  70. if parent
  71. then
  72. minetest.show_formspec(
  73. player:get_player_name(),
  74. to_formspec(pages[parent]))
  75. return true
  76. end
  77. end
  78. local page_children = fields.page_children
  79. if page_children
  80. then
  81. local evt = minetest.explode_table_event(page_children)
  82. if evt.type ~= "DCL" --if not double click
  83. then
  84. return true
  85. end
  86. minetest.show_formspec(
  87. player:get_player_name(),
  88. to_formspec(pages[page.children[evt.row]]))
  89. end
  90. return true
  91. end)
  92. local err = "Error registering guide page: "
  93. function in_game_guide.register_page(name, definition)
  94. local split_name = string.split(name, ":")
  95. assert(not pages[name],
  96. err .. "There is already a page registered with name " .. name)
  97. assert(minetest.get_current_modname() == split_name[1],
  98. err .. "Name must be of format 'modname:pagename'")
  99. assert(type(definition.text) == "string",
  100. err .. "field 'text' of definition must be a string")
  101. definition.text = minetest.formspec_escape(definition.text)
  102. assert(type(definition.title) == "string",
  103. err .. "field 'title' of definition must be a string")
  104. definition.title = minetest.formspec_escape(definition.title)
  105. if type(definition.children) == "table"
  106. then
  107. assert(#definition.children > 0,
  108. err .. "field 'children' must not be an empty table, set it to nil instead.")
  109. for _, childname in ipairs(definition.children)
  110. do
  111. assert(type(childname) == "string",
  112. err .. "Cannot try to register non-string value as child name")
  113. assert(pages[childname],
  114. err .. "There is no registered page with name " ..
  115. childname .. " to be set as child.")
  116. assert(not pages[childname].parent,
  117. err .. "Child " .. childname .. " already has a parent.")
  118. pages[childname].parent = name
  119. end
  120. else
  121. assert(type(definition.children) == "nil",
  122. "Error registering guide page: field 'children of definition must be a table or nil")
  123. end
  124. definition.name = name
  125. pages[name] = definition
  126. end
  127. minetest.register_chatcommand("guide",
  128. {
  129. description = S("Shows the guide screen"), --TODO: translate
  130. privs = {},
  131. func = function(name, param)
  132. local player = minetest.get_player_by_name(name)
  133. if player
  134. then
  135. if root_page
  136. then
  137. in_game_guide.show_root_page(name)
  138. return true
  139. end
  140. end
  141. return false, S("There is no guide page set as root")
  142. end,
  143. })
  144. --testing stuff
  145. --[[
  146. in_game_guide.register_page("in_game_guide:testchild",
  147. {
  148. text = "henlo im a child",
  149. title = "Test child",
  150. })
  151. in_game_guide.register_page("in_game_guide:test",
  152. {
  153. text =
  154. "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Dolor sed viverra ipsum nunc aliquet bibendum enim. In massa tempor nec feugiat. Nunc aliquet bibendum enim facilisis gravida. Nisl nunc mi ipsum faucibus vitae aliquet nec ullamcorper. Amet luctus venenatis lectus magna fringilla. Volutpat maecenas volutpat blandit aliquam etiam erat velit scelerisque in. Egestas egestas fringilla phasellus faucibus scelerisque eleifend. Sagittis orci a scelerisque purus semper eget duis. Nulla pharetra diam sit amet nisl suscipit. Sed adipiscing diam donec adipiscing tristique risus nec feugiat in. Fusce ut placerat orci nulla. Pharetra vel turpis nunc eget lorem dolor. Tristique senectus et netus et malesuada." ..
  155. "Etiam tempor orci eu lobortis elementum nibh tellus molestie. Neque egestas congue quisque egestas. Egestas integer eget aliquet nibh praesent tristique. Vulputate mi sit amet mauris. Sodales neque sodales ut etiam sit. Dignissim suspendisse in est ante in. Volutpat commodo sed egestas egestas. Felis donec et odio pellentesque diam. Pharetra vel turpis nunc eget lorem dolor sed viverra. Porta nibh venenatis cras sed felis eget. Aliquam ultrices sagittis orci a. Dignissim diam quis enim lobortis. Aliquet porttitor lacus luctus accumsan. Dignissim convallis aenean et tortor at risus viverra adipiscing at.",
  156. title = "Lorem Ipsum",
  157. children = {"in_game_guide:testchild"},
  158. })
  159. in_game_guide.set_root_page("in_game_guide:test")
  160. --]]