mm_menubar.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. --Minetest
  2. --Copyright (C) 2013 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. menubar = {}
  18. --------------------------------------------------------------------------------
  19. function menubar.handle_buttons(fields)
  20. for i=1,#menubar.buttons,1 do
  21. if fields[menubar.buttons[i].btn_name] ~= nil then
  22. menu.last_game = menubar.buttons[i].index
  23. engine.setting_set("main_menu_last_game_idx",menu.last_game)
  24. menu.update_gametype()
  25. end
  26. end
  27. end
  28. --------------------------------------------------------------------------------
  29. function menubar.refresh()
  30. menubar.formspec = "box[-0.3,5.625;12.4,1.2;#000000]" ..
  31. "box[-0.3,5.6;12.4,0.05;#FFFFFF]"
  32. menubar.buttons = {}
  33. local button_base = -0.08
  34. local maxbuttons = #gamemgr.games
  35. if maxbuttons > 11 then
  36. maxbuttons = 11
  37. end
  38. for i=1,maxbuttons,1 do
  39. local btn_name = "menubar_btn_" .. gamemgr.games[i].id
  40. local buttonpos = button_base + (i-1) * 1.1
  41. if gamemgr.games[i].menuicon_path ~= nil and
  42. gamemgr.games[i].menuicon_path ~= "" then
  43. menubar.formspec = menubar.formspec ..
  44. "image_button[" .. buttonpos .. ",5.72;1.165,1.175;" ..
  45. engine.formspec_escape(gamemgr.games[i].menuicon_path) .. ";" ..
  46. btn_name .. ";;true;false]"
  47. else
  48. local part1 = gamemgr.games[i].id:sub(1,5)
  49. local part2 = gamemgr.games[i].id:sub(6,10)
  50. local part3 = gamemgr.games[i].id:sub(11)
  51. local text = part1 .. "\n" .. part2
  52. if part3 ~= nil and
  53. part3 ~= "" then
  54. text = text .. "\n" .. part3
  55. end
  56. menubar.formspec = menubar.formspec ..
  57. "image_button[" .. buttonpos .. ",5.72;1.165,1.175;;" ..btn_name ..
  58. ";" .. text .. ";true;true]"
  59. end
  60. local toadd = {
  61. btn_name = btn_name,
  62. index = i,
  63. }
  64. table.insert(menubar.buttons,toadd)
  65. end
  66. end