private void writeBezelConfig(ConfigFile retroarchConfig, string systemName, string rom, ScreenResolution resolution)
        {
            string overlayUser = AppConfig.GetFullPath("decorations");

            string overlaySystem = AppConfig.GetFullPath("system.decorations");

            if (string.IsNullOrEmpty(overlaySystem) || !Directory.Exists(overlaySystem))
            {
                overlaySystem = Path.Combine(AppConfig.GetFullPath("home"), "decorations");
            }

            string bezel = Directory.Exists(overlayUser) && !string.IsNullOrEmpty(SystemConfig["bezel"]) ? SystemConfig["bezel"] : "default";

            if (SystemConfig.isOptSet("forceNoBezel") && SystemConfig.getOptBoolean("forceNoBezel"))
            {
                bezel = null;
            }
            else if (!SystemConfig.isOptSet("bezel"))
            {
                bezel = "thebezelproject";
            }

            retroarchConfig["input_overlay_hide_in_menu"] = "false";
            retroarchConfig["input_overlay_enable"]       = "false";
            retroarchConfig["video_message_pos_x"]        = "0.05";
            retroarchConfig["video_message_pos_y"]        = "0.05";

            if (string.IsNullOrEmpty(bezel) || bezel == "none")
            {
                return;
            }

            if (systemName == "wii")
            {
                return;
            }

            string romBase = Path.GetFileNameWithoutExtension(rom);

            string overlay_info_file = overlayUser + "/" + bezel + "/games/" + systemName + "/" + romBase + ".info";
            string overlay_png_file  = overlayUser + "/" + bezel + "/games/" + systemName + "/" + romBase + ".png";

            if (!File.Exists(overlay_png_file) && !string.IsNullOrEmpty(overlaySystem))
            {
                overlay_info_file = overlaySystem + "/" + bezel + "/games/" + systemName + "/" + romBase + ".info";
                overlay_png_file  = overlaySystem + "/" + bezel + "/games/" + systemName + "/" + romBase + ".png";
            }

            if (!File.Exists(overlay_png_file))
            {
                overlay_info_file = overlayUser + "/" + bezel + "/games/" + romBase + ".info";
                overlay_png_file  = overlayUser + "/" + bezel + "/games/" + romBase + ".png";
            }

            if (!string.IsNullOrEmpty(overlaySystem) && !File.Exists(overlay_png_file))
            {
                overlay_info_file = overlaySystem + "/" + bezel + "/games/" + romBase + ".info";
                overlay_png_file  = overlaySystem + "/" + bezel + "/games/" + romBase + ".png";
            }

            if (!File.Exists(overlay_png_file))
            {
                overlay_info_file = overlayUser + "/" + bezel + "/systems/" + systemName + ".info";
                overlay_png_file  = overlayUser + "/" + bezel + "/systems/" + systemName + ".png";
            }

            if (!string.IsNullOrEmpty(overlaySystem) && !File.Exists(overlay_png_file))
            {
                overlay_info_file = overlaySystem + "/" + bezel + "/systems/" + systemName + ".info";
                overlay_png_file  = overlaySystem + "/" + bezel + "/systems/" + systemName + ".png";
            }

            if (!File.Exists(overlay_png_file))
            {
                overlay_info_file = overlayUser + "/" + bezel + "/default.info";
                overlay_png_file  = overlayUser + "/" + bezel + "/default.png";
            }

            if (!string.IsNullOrEmpty(overlaySystem) && !File.Exists(overlay_png_file))
            {
                overlay_info_file = overlaySystem + "/" + bezel + "/default.info";
                overlay_png_file  = overlaySystem + "/" + bezel + "/default.png";
            }

            if (!File.Exists(overlay_png_file))
            {
                overlay_info_file = overlayUser + "/default_unglazed/systems/" + systemName + ".info";
                overlay_png_file  = overlayUser + "/default_unglazed/systems/" + systemName + ".png";
            }

            if (!string.IsNullOrEmpty(overlaySystem) && !File.Exists(overlay_png_file))
            {
                overlay_info_file = overlaySystem + "/default_unglazed/systems/" + systemName + ".info";
                overlay_png_file  = overlaySystem + "/default_unglazed/systems/" + systemName + ".png";
            }

            if (!File.Exists(overlay_png_file))
            {
                overlay_info_file = overlayUser + "/default/systems/" + systemName + ".info";
                overlay_png_file  = overlayUser + "/default/systems/" + systemName + ".png";
            }

            if (!string.IsNullOrEmpty(overlaySystem) && !File.Exists(overlay_png_file))
            {
                overlay_info_file = overlaySystem + "/default/systems/" + systemName + ".info";
                overlay_png_file  = overlaySystem + "/default/systems/" + systemName + ".png";
            }

            if (!File.Exists(overlay_png_file))
            {
                return;
            }

            Size imageSize;

            try
            {
                imageSize = GetImageSize(overlay_png_file);
            }
            catch
            {
                return;
            }

            BezelInfo infos = new BezelInfo();

            if (File.Exists(overlay_info_file))
            {
                try { infos = JsonSerializer.DeserializeFile <BezelInfo>(overlay_info_file); }
                catch { }
            }

            // if image is not at the correct size, find the correct size
            bool bezelNeedAdaptation = false;
            bool viewPortUsed        = true;

            if (!infos.width.HasValue || !infos.height.HasValue || !infos.top.HasValue || !infos.left.HasValue || !infos.bottom.HasValue || !infos.right.HasValue)
            {
                viewPortUsed = false;
            }

            // for testing ->
            //resolution = ScreenResolution.Parse("2280x1080x32x60");
            //resolution = ScreenResolution.Parse("3840x2160x32x60");

            int resX = (resolution == null ? Screen.PrimaryScreen.Bounds.Width : resolution.Width);
            int resY = (resolution == null ? Screen.PrimaryScreen.Bounds.Height : resolution.Height);

            float screenRatio = (float)resX / (float)resY;
            float bezelRatio  = (float)imageSize.Width / (float)imageSize.Height;

            if (viewPortUsed)
            {
                if (resX != infos.width.GetValueOrDefault() || resY != infos.height.GetValueOrDefault())
                {
                    if (screenRatio < 1.6) // use bezels only for 16:10, 5:3, 16:9 and wider aspect ratios
                    {
                        return;
                    }
                    else
                    {
                        bezelNeedAdaptation = true;
                    }
                }

                if (!SystemConfig.isOptSet("ratio"))
                {
                    if (systemName == "mame")
                    {
                        retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("core").ToString();
                    }
                    else
                    {
                        retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("custom").ToString(); // overwritten from the beginning of this file
                    }
                }
            }
            else
            {
                // when there is no information about width and height in the .info, assume that the tv is HD 16/9 and infos are core provided
                if (screenRatio < 1.6) // use bezels only for 16:10, 5:3, 16:9 and wider aspect ratios
                {
                    return;
                }

                infos.width         = imageSize.Width;
                infos.height        = imageSize.Height;
                bezelNeedAdaptation = true;

                if (!SystemConfig.isOptSet("ratio"))
                {
                    retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("core").ToString(); // overwritten from the beginning of this file
                }
            }

            string overlay_cfg_file = Path.Combine(RetroarchPath, "custom-overlay.cfg");

            retroarchConfig["input_overlay_enable"]       = "true";
            retroarchConfig["input_overlay_scale"]        = "1.0";
            retroarchConfig["input_overlay"]              = overlay_cfg_file;
            retroarchConfig["input_overlay_hide_in_menu"] = "true";

            if (!infos.opacity.HasValue)
            {
                infos.opacity = 1.0f;
            }
            if (!infos.messagex.HasValue)
            {
                infos.messagex = 0.0f;
            }
            if (!infos.messagey.HasValue)
            {
                infos.messagey = 0.0f;
            }

            retroarchConfig["input_overlay_opacity"] = infos.opacity.ToString().Replace(",", "."); // "1.0";
            // for testing : retroarchConfig["input_overlay_opacity"] = "0.5";

            if (bezelNeedAdaptation)
            {
                float wratio = resX / (float)infos.width;
                float hratio = resY / (float)infos.height;

                int xoffset = resX - infos.width.Value;
                int yoffset = resY - infos.height.Value;

                bool stretchImage = false;

                if (resX < infos.width || resY < infos.height) // If width or height < original, can't add black borders. Just stretch
                {
                    stretchImage = true;
                }
                else if (Math.Abs(screenRatio - bezelRatio) < 0.2) // FCA : About the same ratio ? Just stretch
                {
                    stretchImage = true;
                }

                if (viewPortUsed)
                {
                    if (stretchImage)
                    {
                        retroarchConfig["custom_viewport_x"]      = ((int)(infos.left * wratio)).ToString();
                        retroarchConfig["custom_viewport_y"]      = ((int)(infos.top * hratio)).ToString();
                        retroarchConfig["custom_viewport_width"]  = ((int)((infos.width - infos.left - infos.right) * wratio)).ToString();
                        retroarchConfig["custom_viewport_height"] = ((int)((infos.height - infos.top - infos.bottom) * hratio)).ToString();
                        retroarchConfig["video_message_pos_x"]    = (infos.messagex.Value * wratio).ToString(CultureInfo.InvariantCulture);
                        retroarchConfig["video_message_pos_y"]    = (infos.messagey.Value * hratio).ToString(CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        retroarchConfig["custom_viewport_x"]      = ((int)(infos.left + xoffset / 2)).ToString();
                        retroarchConfig["custom_viewport_y"]      = ((int)(infos.top + yoffset / 2)).ToString();
                        retroarchConfig["custom_viewport_width"]  = ((int)((infos.width - infos.left - infos.right))).ToString();
                        retroarchConfig["custom_viewport_height"] = ((int)((infos.height - infos.top - infos.bottom))).ToString();
                        retroarchConfig["video_message_pos_x"]    = (infos.messagex.Value + xoffset / 2).ToString(CultureInfo.InvariantCulture);
                        retroarchConfig["video_message_pos_y"]    = (infos.messagey.Value + yoffset / 2).ToString(CultureInfo.InvariantCulture);
                    }
                }

                if (!stretchImage)
                {
                    var    f  = Path.GetFileNameWithoutExtension(overlay_png_file);
                    var    d  = Path.GetFileName(Path.GetDirectoryName(overlay_png_file));
                    var    fn = "bezel." + d + "." + f + "." + resX + "x" + resY + ".png";
                    string output_png_file = Path.Combine(Path.GetTempPath(), fn);

                    if (File.Exists(output_png_file))
                    {
                        overlay_png_file = output_png_file;
                    }
                    else
                    {
                        try
                        {
                            using (Image img = Image.FromFile(overlay_png_file))
                            {
                                using (Bitmap bmp = new Bitmap(resX, resY))
                                {
                                    using (Graphics g = Graphics.FromImage(bmp))
                                    {
                                        Rectangle rect = Misc.GetPictureRect(img.Size, new Rectangle(0, 0, resX, resY));
                                        if (rect.X != 0 && rect.Y != 0)
                                        {
                                            g.ExcludeClip(rect);
                                            g.FillRectangle(Brushes.Black, new Rectangle(0, 0, bmp.Width, bmp.Height));
                                            g.ResetClip();
                                        }

                                        g.DrawImage(img, rect);
                                    }

                                    bmp.Save(output_png_file, System.Drawing.Imaging.ImageFormat.Png);
                                    overlay_png_file = output_png_file;
                                }
                            }
                        }
                        catch { }
                    }
                }
            }
            else
            {
                if (viewPortUsed)
                {
                    retroarchConfig["custom_viewport_x"]      = infos.left.Value.ToString();
                    retroarchConfig["custom_viewport_y"]      = infos.top.Value.ToString();
                    retroarchConfig["custom_viewport_width"]  = (infos.width.Value - infos.left.Value - infos.right.Value).ToString();
                    retroarchConfig["custom_viewport_height"] = (infos.height.Value - infos.top.Value - infos.bottom.Value).ToString();
                }

                retroarchConfig["video_message_pos_x"] = infos.messagex.Value.ToString(CultureInfo.InvariantCulture);
                retroarchConfig["video_message_pos_y"] = infos.messagey.Value.ToString(CultureInfo.InvariantCulture);
            }

            retroarchConfig["input_overlay_show_mouse_cursor"] = "false";

            StringBuilder fd = new StringBuilder();

            fd.AppendLine("overlays = 1");
            fd.AppendLine("overlay0_overlay = \"" + overlay_png_file + "\"");
            fd.AppendLine("overlay0_full_screen = true");
            fd.AppendLine("overlay0_descs = 0");
            File.WriteAllText(overlay_cfg_file, fd.ToString());
        }
Example #2
0
        private void writeBezelConfig(ConfigFile retroarchConfig, string systemName, string rom, ScreenResolution resolution)
        {
            retroarchConfig["input_overlay_hide_in_menu"] = "false";
            retroarchConfig["input_overlay_enable"]       = "false";
            retroarchConfig["video_message_pos_x"]        = "0.05";
            retroarchConfig["video_message_pos_y"]        = "0.05";

            if (systemName == "wii")
            {
                return;
            }

            var bezelInfo = BezelFiles.GetBezelFiles(systemName, rom);

            if (bezelInfo == null)
            {
                return;
            }

            string overlay_info_file = bezelInfo.InfoFile;
            string overlay_png_file  = bezelInfo.PngFile;

            Size imageSize;

            try
            {
                imageSize = GetImageSize(overlay_png_file);
            }
            catch
            {
                return;
            }

            BezelInfo infos = bezelInfo.BezelInfos;

            // if image is not at the correct size, find the correct size
            bool bezelNeedAdaptation = false;
            bool viewPortUsed        = true;

            if (!infos.IsValid())
            {
                viewPortUsed = false;
            }

            // for testing ->
            //resolution = ScreenResolution.Parse("2280x1080x32x60");
            //resolution = ScreenResolution.Parse("3840x2160x32x60");

            int resX = (resolution == null ? Screen.PrimaryScreen.Bounds.Width : resolution.Width);
            int resY = (resolution == null ? Screen.PrimaryScreen.Bounds.Height : resolution.Height);

            float screenRatio = (float)resX / (float)resY;
            float bezelRatio  = (float)imageSize.Width / (float)imageSize.Height;

            if (viewPortUsed)
            {
                if (resX != infos.width.GetValueOrDefault() || resY != infos.height.GetValueOrDefault())
                {
                    if (screenRatio < 1.6) // use bezels only for 16:10, 5:3, 16:9 and wider aspect ratios
                    {
                        return;
                    }
                    else
                    {
                        bezelNeedAdaptation = true;
                    }
                }

                if (!SystemConfig.isOptSet("ratio"))
                {
                    if (systemName == "mame")
                    {
                        retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("core").ToString();
                    }
                    else
                    {
                        retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("custom").ToString(); // overwritten from the beginning of this file
                    }
                }
            }
            else
            {
                // when there is no information about width and height in the .info, assume that the tv is HD 16/9 and infos are core provided
                if (screenRatio < 1.6) // use bezels only for 16:10, 5:3, 16:9 and wider aspect ratios
                {
                    return;
                }

                infos.width         = imageSize.Width;
                infos.height        = imageSize.Height;
                bezelNeedAdaptation = true;

                if (!SystemConfig.isOptSet("ratio"))
                {
                    retroarchConfig["aspect_ratio_index"] = ratioIndexes.IndexOf("core").ToString(); // overwritten from the beginning of this file
                }
            }

            string overlay_cfg_file = Path.Combine(RetroarchPath, "custom-overlay.cfg");

            retroarchConfig["input_overlay_enable"]       = "true";
            retroarchConfig["input_overlay_scale"]        = "1.0";
            retroarchConfig["input_overlay"]              = overlay_cfg_file;
            retroarchConfig["input_overlay_hide_in_menu"] = "true";

            if (!infos.opacity.HasValue)
            {
                infos.opacity = 1.0f;
            }
            if (!infos.messagex.HasValue)
            {
                infos.messagex = 0.0f;
            }
            if (!infos.messagey.HasValue)
            {
                infos.messagey = 0.0f;
            }

            retroarchConfig["input_overlay_opacity"] = infos.opacity.ToString().Replace(",", "."); // "1.0";
            // for testing : retroarchConfig["input_overlay_opacity"] = "0.5";

            if (bezelNeedAdaptation)
            {
                float wratio = resX / (float)infos.width;
                float hratio = resY / (float)infos.height;

                int xoffset = resX - infos.width.Value;
                int yoffset = resY - infos.height.Value;

                bool stretchImage = false;

                if (resX < infos.width || resY < infos.height) // If width or height < original, can't add black borders. Just stretch
                {
                    stretchImage = true;
                }
                else if (Math.Abs(screenRatio - bezelRatio) < 0.2) // FCA : About the same ratio ? Just stretch
                {
                    stretchImage = true;
                }

                if (viewPortUsed)
                {
                    if (stretchImage)
                    {
                        retroarchConfig["custom_viewport_x"]      = ((int)(infos.left * wratio)).ToString();
                        retroarchConfig["custom_viewport_y"]      = ((int)(infos.top * hratio)).ToString();
                        retroarchConfig["custom_viewport_width"]  = ((int)((infos.width - infos.left - infos.right) * wratio)).ToString();
                        retroarchConfig["custom_viewport_height"] = ((int)((infos.height - infos.top - infos.bottom) * hratio)).ToString();
                        retroarchConfig["video_message_pos_x"]    = (infos.messagex.Value * wratio).ToString(CultureInfo.InvariantCulture);
                        retroarchConfig["video_message_pos_y"]    = (infos.messagey.Value * hratio).ToString(CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        retroarchConfig["custom_viewport_x"]      = ((int)(infos.left + xoffset / 2)).ToString();
                        retroarchConfig["custom_viewport_y"]      = ((int)(infos.top + yoffset / 2)).ToString();
                        retroarchConfig["custom_viewport_width"]  = ((int)((infos.width - infos.left - infos.right))).ToString();
                        retroarchConfig["custom_viewport_height"] = ((int)((infos.height - infos.top - infos.bottom))).ToString();
                        retroarchConfig["video_message_pos_x"]    = (infos.messagex.Value + xoffset / 2).ToString(CultureInfo.InvariantCulture);
                        retroarchConfig["video_message_pos_y"]    = (infos.messagey.Value + yoffset / 2).ToString(CultureInfo.InvariantCulture);
                    }
                }

                if (!stretchImage)
                {
                    overlay_png_file = BezelFiles.GetStretchedBezel(overlay_png_file, resX, resY);
                }
            }
            else
            {
                if (viewPortUsed)
                {
                    retroarchConfig["custom_viewport_x"]      = infos.left.Value.ToString();
                    retroarchConfig["custom_viewport_y"]      = infos.top.Value.ToString();
                    retroarchConfig["custom_viewport_width"]  = (infos.width.Value - infos.left.Value - infos.right.Value).ToString();
                    retroarchConfig["custom_viewport_height"] = (infos.height.Value - infos.top.Value - infos.bottom.Value).ToString();
                }

                retroarchConfig["video_message_pos_x"] = infos.messagex.Value.ToString(CultureInfo.InvariantCulture);
                retroarchConfig["video_message_pos_y"] = infos.messagey.Value.ToString(CultureInfo.InvariantCulture);
            }

            retroarchConfig["input_overlay_show_mouse_cursor"] = "false";

            StringBuilder fd = new StringBuilder();

            fd.AppendLine("overlays = 1");
            fd.AppendLine("overlay0_overlay = \"" + overlay_png_file + "\"");
            fd.AppendLine("overlay0_full_screen = true");
            fd.AppendLine("overlay0_descs = 0");
            File.WriteAllText(overlay_cfg_file, fd.ToString());
        }