dlg_contentdb.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. --Luanti
  2. --Copyright (C) 2018-20 rubenwardy
  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. if not core.get_http_api then
  18. function create_contentdb_dlg()
  19. return messagebox("contentdb",
  20. fgettext("ContentDB is not available when Luanti was compiled without cURL"))
  21. end
  22. return
  23. end
  24. -- Filter
  25. local search_string = ""
  26. local cur_page = 1
  27. local filter_type
  28. -- Automatic package installation
  29. local auto_install_spec = nil
  30. local filter_type_names = {
  31. { "type_all", nil },
  32. { "type_game", "game" },
  33. { "type_mod", "mod" },
  34. { "type_txp", "txp" },
  35. }
  36. -- Resolves the package specification stored in auto_install_spec into an actual package.
  37. -- May only be called after the package list has been loaded successfully.
  38. local function resolve_auto_install_spec()
  39. assert(contentdb.load_ok)
  40. if not auto_install_spec then
  41. return nil
  42. end
  43. local spec = contentdb.aliases[auto_install_spec] or auto_install_spec
  44. local resolved = nil
  45. for _, pkg in ipairs(contentdb.packages_full_unordered) do
  46. if pkg.id == spec then
  47. resolved = pkg
  48. break
  49. end
  50. end
  51. if not resolved then
  52. gamedata.errormessage = fgettext_ne("The package $1 was not found.", auto_install_spec)
  53. ui.update()
  54. auto_install_spec = nil
  55. end
  56. return resolved
  57. end
  58. -- Installs the package specified by auto_install_spec.
  59. -- Only does something if:
  60. -- a. The package list has been loaded successfully.
  61. -- b. The ContentDB dialog is currently visible.
  62. local function do_auto_install()
  63. if not contentdb.load_ok then
  64. return
  65. end
  66. local pkg = resolve_auto_install_spec()
  67. if not pkg then
  68. return
  69. end
  70. local contentdb_dlg = ui.find_by_name("contentdb")
  71. if not contentdb_dlg or contentdb_dlg.hidden then
  72. return
  73. end
  74. install_or_update_package(contentdb_dlg, pkg)
  75. auto_install_spec = nil
  76. end
  77. local function sort_and_filter_pkgs()
  78. contentdb.update_paths()
  79. contentdb.sort_packages()
  80. contentdb.filter_packages(search_string, filter_type)
  81. local auto_install_pkg = resolve_auto_install_spec()
  82. if auto_install_pkg then
  83. local idx = table.indexof(contentdb.packages, auto_install_pkg)
  84. if idx ~= -1 then
  85. table.remove(contentdb.packages, idx)
  86. table.insert(contentdb.packages, 1, auto_install_pkg)
  87. end
  88. end
  89. end
  90. local function load()
  91. if contentdb.load_ok then
  92. sort_and_filter_pkgs()
  93. return
  94. end
  95. if contentdb.loading then
  96. return
  97. end
  98. contentdb.fetch_pkgs(function(result)
  99. if result then
  100. sort_and_filter_pkgs()
  101. do_auto_install()
  102. end
  103. ui.update()
  104. end)
  105. end
  106. local function get_info_formspec(size, padding, text)
  107. return table.concat({
  108. "formspec_version[6]",
  109. "size[", size.x, ",", size.y, "]",
  110. "padding[0,0]",
  111. "bgcolor[;true]",
  112. "label[", padding.x + 3.625, ",4.35;", text, "]",
  113. "container[", padding.x, ",", size.y - 0.8 - padding.y, "]",
  114. "button[0,0;2,0.8;back;", fgettext("Back"), "]",
  115. "container_end[]",
  116. })
  117. end
  118. -- Determines how to fit `num_per_page` into `size` space
  119. local function fit_cells(num_per_page, size)
  120. local cell_spacing = 0.5
  121. local columns = 1
  122. local cell_w, cell_h
  123. -- Fit cells into the available height
  124. while true do
  125. cell_w = (size.x - (columns-1)*cell_spacing) / columns
  126. cell_h = cell_w / 4
  127. local required_height = math.ceil(num_per_page / columns) * (cell_h + cell_spacing) - cell_spacing
  128. -- Add 0.1 to be more lenient
  129. if required_height <= size.y + 0.1 then
  130. break
  131. end
  132. columns = columns + 1
  133. end
  134. return cell_spacing, columns, cell_w, cell_h
  135. end
  136. local function calculate_num_per_page()
  137. local size = contentdb.get_formspec_size()
  138. local padding = contentdb.get_formspec_padding()
  139. local window = core.get_window_info()
  140. size.x = size.x - padding.x * 2
  141. size.y = size.y - padding.y * 2 - 1.425 - 0.25 - 0.8
  142. local coordToPx = window.size.x / window.max_formspec_size.x / window.real_gui_scaling
  143. local num_per_page = 12
  144. while num_per_page > 2 do
  145. local _, _, cell_w, _ = fit_cells(num_per_page, size)
  146. if cell_w * coordToPx > 350 then
  147. break
  148. end
  149. num_per_page = num_per_page - 1
  150. end
  151. return num_per_page
  152. end
  153. local function get_formspec(dlgdata)
  154. local window_padding = contentdb.get_formspec_padding()
  155. local size = contentdb.get_formspec_size()
  156. if contentdb.loading then
  157. return get_info_formspec(size, window_padding, fgettext("Loading..."))
  158. end
  159. if contentdb.load_error then
  160. return get_info_formspec(size, window_padding, fgettext("No packages could be retrieved"))
  161. end
  162. assert(contentdb.load_ok)
  163. contentdb.update_paths()
  164. local num_per_page = dlgdata.num_per_page
  165. dlgdata.pagemax = math.max(math.ceil(#contentdb.packages / num_per_page), 1)
  166. if cur_page > dlgdata.pagemax then
  167. cur_page = 1
  168. end
  169. local W = size.x - window_padding.x * 2
  170. local H = size.y - window_padding.y * 2
  171. local category_x = 0
  172. local number_category_buttons = 4
  173. local max_button_w = (W - 0.375 - 0.25 - 7) / number_category_buttons
  174. local category_button_w = math.min(max_button_w, 3)
  175. local function make_category_button(name, label, selected)
  176. category_x = category_x + 1
  177. local color = selected and mt_color_green or ""
  178. return ("style[%s;bgcolor=%s]button[%f,0;%f,0.8;%s;%s]"):format(name, color,
  179. (category_x - 1) * category_button_w, category_button_w, name, label)
  180. end
  181. local selected_type = filter_type
  182. local search_box_width = W - 0.375 - 0.25 - 2*0.8
  183. - number_category_buttons * category_button_w
  184. local formspec = {
  185. "formspec_version[7]",
  186. "size[", size.x, ",", size.y, "]",
  187. "padding[0,0]",
  188. "bgcolor[;true]",
  189. "container[", window_padding.x, ",", window_padding.y, "]",
  190. -- Top-left: categories
  191. make_category_button("type_all", fgettext("All"), selected_type == nil),
  192. make_category_button("type_game", fgettext("Games"), selected_type == "game"),
  193. make_category_button("type_mod", fgettext("Mods"), selected_type == "mod"),
  194. make_category_button("type_txp", fgettext("Texture Packs"), selected_type == "txp"),
  195. -- Top-right: Search
  196. "container[", W - search_box_width - 0.8*2, ",0]",
  197. "field[0,0;", search_box_width, ",0.8;search_string;;", core.formspec_escape(search_string), "]",
  198. "field_enter_after_edit[search_string;true]",
  199. "image_button[", search_box_width, ",0;0.8,0.8;",
  200. core.formspec_escape(defaulttexturedir .. "search.png"), ";search;]",
  201. "image_button[", search_box_width + 0.8, ",0;0.8,0.8;",
  202. core.formspec_escape(defaulttexturedir .. "clear.png"), ";clear;]",
  203. "container_end[]",
  204. -- Bottom strip start
  205. "container[0,", H - 0.8, "]",
  206. "button[0,0;2,0.8;back;", fgettext("Back"), "]",
  207. -- Bottom-center: Page nav buttons
  208. "container[", (W - 1*4 - 2) / 2, ",0]",
  209. "image_button[0,0;1,0.8;", core.formspec_escape(defaulttexturedir), "start_icon.png;pstart;]",
  210. "image_button[1,0;1,0.8;", core.formspec_escape(defaulttexturedir), "prev_icon.png;pback;]",
  211. "style[pagenum;border=false]",
  212. "button[2,0;2,0.8;pagenum;", tonumber(cur_page), " / ", tonumber(dlgdata.pagemax), "]",
  213. "image_button[4,0;1,0.8;", core.formspec_escape(defaulttexturedir), "next_icon.png;pnext;]",
  214. "image_button[5,0;1,0.8;", core.formspec_escape(defaulttexturedir), "end_icon.png;pend;]",
  215. "container_end[]", -- page nav end
  216. -- Bottom-right: updating
  217. "container[", W - 3, ",0]",
  218. "style[status,downloading,queued;border=false]",
  219. }
  220. if contentdb.number_downloading > 0 then
  221. formspec[#formspec + 1] = "button[0,0;3,0.8;downloading;"
  222. if #contentdb.download_queue > 0 then
  223. formspec[#formspec + 1] = fgettext("$1 downloading,\n$2 queued",
  224. contentdb.number_downloading, #contentdb.download_queue)
  225. else
  226. formspec[#formspec + 1] = fgettext("$1 downloading...", contentdb.number_downloading)
  227. end
  228. formspec[#formspec + 1] = "]"
  229. else
  230. local num_avail_updates = 0
  231. for i=1, #contentdb.packages_full do
  232. local package = contentdb.packages_full[i]
  233. if package.path and package.installed_release < package.release and
  234. not (package.downloading or package.queued) then
  235. num_avail_updates = num_avail_updates + 1
  236. end
  237. end
  238. if num_avail_updates == 0 then
  239. formspec[#formspec + 1] = "button[0,0;3,0.8;status;"
  240. formspec[#formspec + 1] = fgettext("No updates")
  241. formspec[#formspec + 1] = "]"
  242. else
  243. formspec[#formspec + 1] = "button[0,0;3,0.8;update_all;"
  244. formspec[#formspec + 1] = fgettext("Update All [$1]", num_avail_updates)
  245. formspec[#formspec + 1] = "]"
  246. end
  247. end
  248. formspec[#formspec + 1] = "container_end[]" -- updating end
  249. formspec[#formspec + 1] = "container_end[]" -- bottom strip end
  250. if #contentdb.packages == 0 then
  251. formspec[#formspec + 1] = "label[4,4.75;"
  252. formspec[#formspec + 1] = fgettext("No results")
  253. formspec[#formspec + 1] = "]"
  254. end
  255. -- download/queued tooltips always have the same message
  256. local tooltip_colors = ";#dff6f5;#302c2e]"
  257. formspec[#formspec + 1] = "tooltip[downloading;" .. fgettext("Downloading...") .. tooltip_colors
  258. formspec[#formspec + 1] = "tooltip[queued;" .. fgettext("Queued") .. tooltip_colors
  259. formspec[#formspec + 1] = "container[0,1.425]"
  260. local cell_spacing, columns, cell_w, cell_h = fit_cells(num_per_page, {
  261. x = W,
  262. y = H - 1.425 - 0.25 - 0.8
  263. })
  264. local img_w = cell_h * 3 / 2
  265. local start_idx = (cur_page - 1) * num_per_page + 1
  266. for i=start_idx, math.min(#contentdb.packages, start_idx+num_per_page-1) do
  267. local package = contentdb.packages[i]
  268. table.insert_all(formspec, {
  269. "container[",
  270. (cell_w + cell_spacing) * ((i - start_idx) % columns),
  271. ",",
  272. (cell_h + cell_spacing) * math.floor((i - start_idx) / columns),
  273. "]",
  274. "box[0,0;", cell_w, ",", cell_h, ";#ffffff11]",
  275. -- image,
  276. "image[0,0;", img_w, ",", cell_h, ";",
  277. core.formspec_escape(get_screenshot(package, package.thumbnail, 2)), "]",
  278. "label[", img_w + 0.25 + 0.05, ",0.5;",
  279. core.formspec_escape(
  280. core.colorize(mt_color_green, package.title) ..
  281. core.colorize("#BFBFBF", " by " .. package.author)), "]",
  282. "textarea[", img_w + 0.25, ",0.75;", cell_w - img_w - 0.25, ",", cell_h - 0.75, ";;;",
  283. core.formspec_escape(package.short_description), "]",
  284. "style[view_", i, ";border=false]",
  285. "style[view_", i, ":hovered;bgimg=", core.formspec_escape(defaulttexturedir .. "button_hover_semitrans.png"), "]",
  286. "style[view_", i, ":pressed;bgimg=", core.formspec_escape(defaulttexturedir .. "button_press_semitrans.png"), "]",
  287. "button[0,0;", cell_w, ",", cell_h, ";view_", i, ";]",
  288. })
  289. if package.featured then
  290. table.insert_all(formspec, {
  291. "tooltip[0,0;0.8,0.8;", fgettext("Featured"), "]",
  292. "image[0.2,0.2;0.4,0.4;", core.formspec_escape(defaulttexturedir .. "server_favorite.png"), "]",
  293. })
  294. end
  295. table.insert_all(formspec, {
  296. "container[", cell_w - 0.625,",", 0.25, "]",
  297. })
  298. if package.downloading then
  299. table.insert_all(formspec, {
  300. "animated_image[0,0;0.5,0.5;downloading;", core.formspec_escape(defaulttexturedir .. "cdb_downloading.png"),
  301. ";3;400;;]",
  302. })
  303. elseif package.queued then
  304. table.insert_all(formspec, {
  305. "image[0,0;0.5,0.5;", core.formspec_escape(defaulttexturedir .. "cdb_queued.png"), "]",
  306. })
  307. elseif package.path then
  308. if package.installed_release < package.release then
  309. table.insert_all(formspec, {
  310. "image[0,0;0.5,0.5;", core.formspec_escape(defaulttexturedir .. "cdb_update.png"), "]",
  311. })
  312. else
  313. table.insert_all(formspec, {
  314. "image[0.1,0.1;0.3,0.3;", core.formspec_escape(defaulttexturedir .. "checkbox_64.png"), "]",
  315. })
  316. end
  317. end
  318. table.insert_all(formspec, {
  319. "container_end[]",
  320. "container_end[]",
  321. })
  322. end
  323. formspec[#formspec + 1] = "container_end[]"
  324. formspec[#formspec + 1] = "container_end[]"
  325. return table.concat(formspec)
  326. end
  327. local function handle_submit(this, fields)
  328. if fields.search or fields.key_enter_field == "search_string" then
  329. search_string = fields.search_string:trim()
  330. cur_page = 1
  331. contentdb.filter_packages(search_string, filter_type)
  332. return true
  333. end
  334. if fields.clear then
  335. search_string = ""
  336. cur_page = 1
  337. contentdb.filter_packages("", filter_type)
  338. return true
  339. end
  340. if fields.back then
  341. this:delete()
  342. return true
  343. end
  344. if fields.pstart then
  345. cur_page = 1
  346. return true
  347. end
  348. if fields.pend then
  349. cur_page = this.data.pagemax
  350. return true
  351. end
  352. if fields.pnext then
  353. cur_page = cur_page + 1
  354. if cur_page > this.data.pagemax then
  355. cur_page = 1
  356. end
  357. return true
  358. end
  359. if fields.pback then
  360. if cur_page == 1 then
  361. cur_page = this.data.pagemax
  362. else
  363. cur_page = cur_page - 1
  364. end
  365. return true
  366. end
  367. for _, pair in ipairs(filter_type_names) do
  368. if fields[pair[1]] then
  369. filter_type = pair[2]
  370. cur_page = 1
  371. contentdb.filter_packages(search_string, filter_type)
  372. return true
  373. end
  374. end
  375. if fields.update_all then
  376. for i=1, #contentdb.packages_full do
  377. local package = contentdb.packages_full[i]
  378. if package.path and package.installed_release < package.release and
  379. not (package.downloading or package.queued) then
  380. contentdb.queue_download(package, contentdb.REASON_UPDATE)
  381. end
  382. end
  383. return true
  384. end
  385. local num_per_page = this.data.num_per_page
  386. local start_idx = (cur_page - 1) * num_per_page + 1
  387. assert(start_idx ~= nil)
  388. for i=start_idx, math.min(#contentdb.packages, start_idx+num_per_page-1) do
  389. local package = contentdb.packages[i]
  390. assert(package)
  391. if fields["view_" .. i] or fields["title_" .. i] or fields["author_" .. i] then
  392. local dlg = create_package_dialog(package)
  393. dlg:set_parent(this)
  394. this:hide()
  395. dlg:show()
  396. return true
  397. end
  398. end
  399. return false
  400. end
  401. local function handle_events(event)
  402. if event == "DialogShow" then
  403. -- Don't show the header image behind the dialog.
  404. mm_game_theme.set_engine(true)
  405. -- If ContentDB is already loaded, auto-install packages here.
  406. do_auto_install()
  407. return true
  408. end
  409. if event == "WindowInfoChange" then
  410. ui.update()
  411. return true
  412. end
  413. return false
  414. end
  415. --- Creates a ContentDB dialog.
  416. ---
  417. --- @param type string | nil
  418. --- Sets initial package filter. "game", "mod", "txp" or nil (no filter).
  419. --- @param install_spec table | nil
  420. --- ContentDB ID of package as returned by pkgmgr.get_contentdb_id().
  421. --- Sets package to install or update automatically.
  422. function create_contentdb_dlg(type, install_spec)
  423. search_string = ""
  424. cur_page = 1
  425. filter_type = type
  426. -- Keep the old auto_install_spec if the caller doesn't specify one.
  427. if install_spec then
  428. auto_install_spec = install_spec
  429. end
  430. load()
  431. local dlg = dialog_create("contentdb",
  432. get_formspec,
  433. handle_submit,
  434. handle_events)
  435. dlg.data.num_per_page = calculate_num_per_page()
  436. return dlg
  437. end