From efb5d225fcc9490d0b7893057669db26ebc43af5 Mon Sep 17 00:00:00 2001 From: niksedk Date: Sat, 27 Aug 2022 13:29:35 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20assa=20background=20box=20issue=20-=20thx?= =?UTF-8?q?=20=C3=89rico=20:)=20Showing=20multiple=20different=20boxes=20a?= =?UTF-8?q?t=20the=20same=20time=20should=20now=20work?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ui/Forms/Assa/AssSetBackground.cs | 37 ++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/ui/Forms/Assa/AssSetBackground.cs b/src/ui/Forms/Assa/AssSetBackground.cs index a1e38959b..7c8ebd2c9 100644 --- a/src/ui/Forms/Assa/AssSetBackground.cs +++ b/src/ui/Forms/Assa/AssSetBackground.cs @@ -46,6 +46,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa private bool _loading = true; private string _assaBox; private readonly Random _random = new Random(); + private string _boxStyleName; private int _top; private int _bottom; private int _left; @@ -55,6 +56,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa private Color _boxOutlineColor; private long _totalFrames; private FileSystemWatcher _drawingFileWatcher; + private readonly Subtitle _wholeSubtitle; public AssSetBackground(Subtitle subtitle, int[] selectedIndices, string videoFileName, VideoInfo videoInfo, double videoPositionSeconds) { @@ -65,6 +67,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa _videoFileName = videoFileName; _videoInfo = videoInfo; _videoPositionSeconds = videoPositionSeconds; + _wholeSubtitle = subtitle; _subtitleWithNewHeader = new Subtitle(subtitle, false); if (string.IsNullOrWhiteSpace(_subtitleWithNewHeader.Header)) @@ -213,6 +216,15 @@ namespace Nikse.SubtitleEdit.Forms.Assa buttonAssaDraw.Visible = File.Exists(Path.Combine(Configuration.PluginsDirectory, "AssaDraw.dll")); progressBar1.Visible = false; labelProgress.Text = string.Empty; + + var tryCount = 0; + _boxStyleName = "SE-box-bg"; + var styleNames = AdvancedSubStationAlpha.GetStylesFromHeader(subtitle.Header); + while (styleNames.Any(p => p == _boxStyleName) && tryCount < 100) + { + _boxStyleName = $"SE-box-bg{_random.Next(1234)}"; + tryCount++; + } } private static void SafeNumericUpDownAssign(NumericUpDown numericUpDown, int value) @@ -292,8 +304,8 @@ namespace Nikse.SubtitleEdit.Forms.Assa var p2 = new Paragraph(_assaBox ?? string.Empty, p.StartTime.TotalMilliseconds, p.EndTime.TotalMilliseconds) { - Layer = Configuration.Settings.Tools.AssaBgBoxLayer, - Extra = "SE-box-bg" + Layer = GetLayer(), + Extra = _boxStyleName, }; if (!checkBoxOnlyDrawing.Checked) @@ -308,6 +320,19 @@ namespace Nikse.SubtitleEdit.Forms.Assa DialogResult = DialogResult.OK; } + private int GetLayer() + { + var layer = Configuration.Settings.Tools.AssaBgBoxLayer; + var tryCount = 0; + while (_wholeSubtitle.Paragraphs.Any(p => p.Layer == layer) && tryCount < 100) + { + layer = Configuration.Settings.Tools.AssaBgBoxLayer - _random.Next(8421); + tryCount++; + } + + return layer; + } + private void AddBgBoxStyles(Subtitle subtitle) { if (string.IsNullOrWhiteSpace(subtitle.Header)) @@ -338,7 +363,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa var styleBoxBg = new SsaStyle { Alignment = "7", - Name = "SE-box-bg", + Name = _boxStyleName, MarginLeft = 0, MarginRight = 0, MarginVertical = 0, @@ -482,8 +507,8 @@ namespace Nikse.SubtitleEdit.Forms.Assa { StartTime = { TotalMilliseconds = p.StartTime.TotalMilliseconds }, EndTime = { TotalMilliseconds = p.EndTime.TotalMilliseconds }, - Layer = Configuration.Settings.Tools.AssaBgBoxLayer, - Extra = "SE-box-bg" + Layer = GetLayer(), + Extra = _boxStyleName, }; if (!checkBoxOnlyDrawing.Checked) @@ -1388,7 +1413,7 @@ namespace Nikse.SubtitleEdit.Forms.Assa { ColorChooser.SetLastColor(buttonPickColor.BackColor); pictureBoxPreview_MouseLeave(null, null); - labelProgress.Text = string.Format(LanguageSettings.Current.AssaSetBackgroundBox.ColorPickerSetLastColor , Utilities.ColorToHexWithTransparency(buttonPickColor.BackColor)); + labelProgress.Text = string.Format(LanguageSettings.Current.AssaSetBackgroundBox.ColorPickerSetLastColor, Utilities.ColorToHexWithTransparency(buttonPickColor.BackColor)); System.Threading.SynchronizationContext.Current.Post(TimeSpan.FromMilliseconds(3500), () => { labelProgress.Text = _videoText;