Exemplo n.º 1
0
        public void DownloadWallpaper()
        {
            var cache   = new ImageCache();
            var creator = new WallpaperCreator();

            var insectNames     = cache.InsectList.Take(2).ToList();
            var dummyDownloader = cache.GetDownloader(insectNames.First());
            var level           = dummyDownloader.WallpaperLevel(1920, 1080);
            var insects         = cache.Download(insectNames, level, true).ToList();

            creator.CreateWallpapers(insects, 1920, 1080, true, 50);
        }
        private void OnExecute(CommandLineApplication app)
        {
            // Check parameters
            if (WallpaperWidth <= 0 || WallpaperHeight <= 0)
            {
                app.Error.WriteLine("Wallpaper width and height need to be positive.");
                app.ShowHint();
                return;
            }

            if (WallpaperBorder <= 0 || WallpaperBorder >= WallpaperWidth || WallpaperBorder >= WallpaperHeight)
            {
                app.Error.WriteLine("Wallpaper border must be positive and cannot be larger than the width or height.");
                app.ShowHint();
                return;
            }

            // List all insects
            if (ListInsects)
            {
                foreach (var insect in Cache.InsectList)
                {
                    app.Out.WriteLine(insect);
                }

                return;
            }

            // All other commands require a list of insects to work on
            if (!TryGetInsects(app, out var insectNames) || !insectNames.Any())
            {
                return;
            }

            // List all resolutions for each selected insect
            if (ListResolutions)
            {
                var maxWidth = insectNames.Select(name => name.Length).Max();
                foreach (var insectName in insectNames)
                {
                    app.Out.WriteLine($"{insectName.PadRight(maxWidth)} {string.Join(", ", Cache.GetDownloader(insectName).GetLevels())}");
                }

                return;
            }

            /*
             * Find an appropriate quality level for wallpaper generation
             * This is just a heuristic, because:
             * - When trimming, the resolution might drop under the given size.
             * - Different images might have different quality definitions.
             */
            if (!Level.HasValue && WallpaperGenerate)
            {
                var dummyDownloader = Cache.GetDownloader(insectNames.First());
                Level = dummyDownloader.WallpaperLevel(WallpaperWidth, WallpaperHeight);
            }

            // Download all selected insects
            var insects = Cache.Download(insectNames, Level, ForceDownload).ToList();

            // Create wallpapers
            if (WallpaperGenerate)
            {
                Creator.CreateWallpapers(insects, WallpaperWidth, WallpaperHeight, WallpaperTrim, WallpaperBorder);
            }
        }