dlg_rebind_keys.lua 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. -- Luanti
  2. -- SPDX-License-Identifier: LGPL-2.1-or-later
  3. -- Modified based on dlg_reinstall_mtg.lua
  4. -- Note that this is only needed for migrating from <5.11 to 5.12.
  5. local doc_url = "https://docs.luanti.org/for-players/controls/"
  6. local SETTING_NAME = "no_keycode_migration_warning"
  7. local function get_formspec(dialogdata)
  8. local markup = table.concat({
  9. "<big>" .. hgettext("Keybindings changed") .. "</big>",
  10. hgettext("The input handling system was reworked in Luanti 5.12.0."),
  11. hgettext("As a result, your keybindings may have been changed."),
  12. hgettext("Check out the key settings or refer to the documentation:"),
  13. ("<action name='doc_url'><style color='cyan' hovercolor='orangered'>%s</style></action>"):format(doc_url),
  14. }, "\n")
  15. return table.concat({
  16. "formspec_version[6]",
  17. "size[12,7]",
  18. "hypertext[0.5,0.5;11,4.7;text;", core.formspec_escape(markup), "]",
  19. "container[0.5,5.7]",
  20. "button[0,0;4,0.8;dismiss;", fgettext("Close"), "]",
  21. "button[4.5,0;6.5,0.8;reconfigure;", fgettext("Open settings"), "]",
  22. "container_end[]",
  23. })
  24. end
  25. local function close_dialog(this)
  26. cache_settings:set_bool(SETTING_NAME, true)
  27. this:delete()
  28. end
  29. local function buttonhandler(this, fields)
  30. if fields.reconfigure then
  31. local parent = this.parent
  32. close_dialog(this)
  33. local dlg = create_settings_dlg("controls_keyboard_and_mouse")
  34. dlg:set_parent(parent)
  35. parent:hide()
  36. dlg:show()
  37. return true
  38. end
  39. if fields.dismiss then
  40. close_dialog(this)
  41. return true
  42. end
  43. if fields.text == "action:doc_url" then
  44. core.open_url(doc_url)
  45. end
  46. end
  47. local function eventhandler(event)
  48. if event == "DialogShow" then
  49. mm_game_theme.set_engine()
  50. return true
  51. elseif event == "MenuQuit" then
  52. -- Don't allow closing the dialog with ESC, but still allow exiting
  53. -- Luanti
  54. core.close()
  55. return true
  56. end
  57. return false
  58. end
  59. local function create_rebind_keys_dlg()
  60. local dlg = dialog_create("dlg_rebind_keys", get_formspec,
  61. buttonhandler, eventhandler)
  62. return dlg
  63. end
  64. function migrate_keybindings(parent)
  65. -- Show migration dialog if the user upgraded from an earlier version
  66. -- and this has not yet been shown before, *or* if keys settings had to be changed
  67. if core.is_first_run then
  68. cache_settings:set_bool(SETTING_NAME, true)
  69. end
  70. local has_migration = not cache_settings:get_bool(SETTING_NAME)
  71. -- normalize all existing key settings, this converts them from KEY_KEY_C to SYSTEM_SCANCODE_6
  72. local settings = core.settings:to_table()
  73. for name, value in pairs(settings) do
  74. if name:match("^keymap_") then
  75. local normalized = core.normalize_keycode(value)
  76. if value ~= normalized then
  77. has_migration = true
  78. core.settings:set(name, normalized)
  79. end
  80. end
  81. end
  82. if not has_migration then
  83. return parent
  84. end
  85. local dlg = create_rebind_keys_dlg()
  86. dlg:set_parent(parent)
  87. parent:hide()
  88. dlg:show()
  89. ui.update()
  90. return dlg
  91. end