Add screenshots to online content browser
authorrubenwardy <rw@rubenwardy.com>
Sat, 19 May 2018 23:25:17 +0000 (00:25 +0100)
committerGitHub <noreply@github.com>
Sat, 19 May 2018 23:25:17 +0000 (00:25 +0100)
builtin/mainmenu/dlg_contentstore.lua
src/script/lua_api/l_mainmenu.cpp
textures/base/pack/error_screenshot.png [new file with mode: 0644]
textures/base/pack/loading_screenshot.png [new file with mode: 0644]

index 2c9e80e151b063fd344f18fbc3619ab1a5b0c8fa..59c2480c130af70879e8e1c679e41eead9af1754 100644 (file)
 local store = {}
 local package_dialog = {}
 
+-- Screenshot
+local screenshot_dir = os.tempfolder()
+assert(core.create_dir(screenshot_dir))
+local screenshot_downloading = {}
+local screenshot_downloaded = {}
+
+-- Filter
 local search_string = ""
 local cur_page = 1
 local num_per_page = 5
@@ -137,6 +144,58 @@ local function start_install(calling_dialog, package)
        new_dlg:show()
 end
 
+local function get_screenshot(package)
+       if #package.screenshots == 0 then
+               return defaulttexturedir .. "no_screenshot.png"
+       elseif screenshot_downloading[package.screenshots[1]] then
+               return defaulttexturedir .. "loading_screenshot.png"
+       end
+
+       -- Get tmp screenshot path
+       local filepath = screenshot_dir .. DIR_DELIM ..
+               package.type .. "-" .. package.author .. "-" .. package.name .. ".png"
+
+       -- Return if already downloaded
+       local file = io.open(filepath, "r")
+       if file then
+               file:close()
+               return filepath
+       end
+
+       -- Show error if we've failed to download before
+       if screenshot_downloaded[package.screenshots[1]] then
+               return defaulttexturedir .. "error_screenshot.png"
+       end
+
+       -- Download
+
+       local function download_screenshot(params)
+               return core.download_file(params.url, params.dest)
+       end
+       local function callback(success)
+               screenshot_downloading[package.screenshots[1]] = nil
+               screenshot_downloaded[package.screenshots[1]] = true
+               if not success then
+                       core.log("warning", "Screenshot download failed for some reason")
+               end
+
+               local ele = ui.childlist.store
+               if ele and not ele.hidden then
+                       core.update_formspec(ele:formspec())
+               end
+       end
+       if core.handle_async(download_screenshot,
+                       { dest = filepath, url = package.screenshots[1] }, callback) then
+               screenshot_downloading[package.screenshots[1]] = true
+       else
+               core.log("error", "ERROR: async event failed")
+               return defaulttexturedir .. "error_screenshot.png"
+       end
+
+       return defaulttexturedir .. "loading_screenshot.png"
+end
+
+
 
 function package_dialog.get_formspec()
        local package = package_dialog.package
@@ -315,8 +374,7 @@ function store.get_formspec()
 
                -- image
                formspec[#formspec + 1] = "image[-0.4,0;1.5,1;"
-               formspec[#formspec + 1] = defaulttexturedir
-               formspec[#formspec + 1] = "no_screenshot.png"
+               formspec[#formspec + 1] = get_screenshot(package)
                formspec[#formspec + 1] = "]"
 
                -- title
index 812fdffe7fe218eeeb4776a49f2f3bf3e4895b12..1f49fc211ceef6a0a32eb43edf19869d555353cf 100644 (file)
@@ -1043,9 +1043,21 @@ int ModApiMainMenu::l_get_package_list(lua_State *L)
                lua_pushstring(L, package.url.c_str());
                lua_settable  (L, top_lvl2);
 
-               lua_pushstring(L, "release");
+               lua_pushstring (L, "release");
                lua_pushinteger(L, package.release);
-               lua_settable  (L, top_lvl2);
+               lua_settable   (L, top_lvl2);
+
+               lua_pushstring(L, "screenshots");
+               lua_newtable(L);
+               {
+                       int top_screenshots = lua_gettop(L);
+                       for (size_t i = 0; i < package.screenshots.size(); ++i) {
+                               lua_pushnumber(L, i + 1);
+                               lua_pushstring(L, package.screenshots[i].c_str());
+                               lua_settable(L, top_screenshots);
+                       }
+               }
+               lua_settable(L, top_lvl2);
 
                lua_settable(L, top);
                index++;
diff --git a/textures/base/pack/error_screenshot.png b/textures/base/pack/error_screenshot.png
new file mode 100644 (file)
index 0000000..e35a0a3
Binary files /dev/null and b/textures/base/pack/error_screenshot.png differ
diff --git a/textures/base/pack/loading_screenshot.png b/textures/base/pack/loading_screenshot.png
new file mode 100644 (file)
index 0000000..4d65a5d
Binary files /dev/null and b/textures/base/pack/loading_screenshot.png differ