From b90da2bbecca7ca2a4539aa3909294f6cdbfa623 Mon Sep 17 00:00:00 2001 From: lukemoore66 <33060484+lukemoore66@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:42:44 +0800 Subject: [PATCH] Add files via upload --- cap.bat | 3 + cap.cfg | 53 +++++++ cap.ps1 | 268 +++++++++++++++++++++++++++++++++++ caplib.ps1 | 407 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 731 insertions(+) create mode 100644 cap.bat create mode 100644 cap.cfg create mode 100644 cap.ps1 create mode 100644 caplib.ps1 diff --git a/cap.bat b/cap.bat new file mode 100644 index 0000000..6c8c6f3 --- /dev/null +++ b/cap.bat @@ -0,0 +1,3 @@ +@ECHO OFF +pwsh.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'" +REM PAUSE \ No newline at end of file diff --git a/cap.cfg b/cap.cfg new file mode 100644 index 0000000..7eae880 --- /dev/null +++ b/cap.cfg @@ -0,0 +1,53 @@ +[CapConfig] +FolderPath= +[PresetTexts] +a photo of a man +a portrait photo of a man +a close up photo of a man +a low angle photo of a man +a high angle photo of a man +a selfie photo of a man + +a photo of a woman +a portrait photo of a woman +a close up photo of a woman +a low angle photo of a woman +a high angle photo of a woman +a selfie photo of a woman + +brown hair +blonde hair +long hair +short hair + +brown eyes +blue eyes +green eyes + +facing the viewer +facing away from the viewer +facing left +facing right + +standing +sitting +laying +lounging +posing + +wearing a jacket +wearing a t-shirt +wearing a suit +wearing shorts +wearing pants +wearing a costume + +studio photoshoot syle +iPhone night time style +cinematic style +cosplay convention photo + +in front of a wall +in front of a crowd of people +in times square +in the woods diff --git a/cap.ps1 b/cap.ps1 new file mode 100644 index 0000000..a8f0cd7 --- /dev/null +++ b/cap.ps1 @@ -0,0 +1,268 @@ +# import functions and assemblies +. ('{0}\{1}' -f $PSScriptRoot, 'caplib.ps1') + +# hide the console window +Hide-Console + +# declare global variables +$Script:presetControls = [ordered]@{} +$Script:txtBox = $null + +$Script:InputDir = $null +$Script:InputDirName = $null +$Script:cfgPath = $null +$Script:capCfgPath = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, '.cfg') + +$Script:imageFormats = Get-ImageFormats +$Script:imageFiles = $null +$Script:selectedImageFile = $null +$Script:currentImagePath = $null +$Script:currentTextPath = $null + +# open a folder +Open-Folder $null + +# Create a form +$form = New-Object System.Windows.Forms.Form +$form.Text = "Fast Caption" +$form.Width = 1205 +$form.Height = [System.Math]::Floor([System.Windows.Forms.Screen]::PrimaryScreen.Bounds.Height * 0.9) +$form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen +$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle +$form.MaximizeBox = $false +$form.KeyPreview = $true + +# Add a tool strip menu +$fileMenu = New-Object System.Windows.Forms.ToolStripMenuItem +$fileMenu.Text = "&File" +$openMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem +$openMenuItem.Text = "&Open Folder" +$openMenuItem.Add_Click({ + Open-Folder $form +}) +$exitMenuItem = New-Object System.Windows.Forms.ToolStripMenuItem +$exitMenuItem.Text = "E&xit" +$exitMenuItem.Add_Click({ + $form.Close() +}) +$fileMenu.DropDownItems.Add($openMenuItem) | Out-Null +$fileMenu.DropDownItems.Add($exitMenuItem) | Out-Null +$menuStrip = New-Object System.Windows.Forms.MenuStrip +$menuStrip.Items.Add($fileMenu) | Out-Null +$form.Controls.Add($menuStrip) + +# Create a picture box to display the selected image +$picBox = New-Object System.Windows.Forms.PictureBox +$picBox.SizeMode = 'Zoom' +$picBox.Width = 512 +$picBox.Height = 512 +$picBox.Top = 25 +$picBox.Left = 10 +$form.Controls.Add($picBox) + +#add all of the preset controls +Add-PresetControls $form $picBox + +# create a flow layout panel +$flowLayoutPanel = New-Object System.Windows.Forms.FlowLayoutPanel +$flowLayoutPanel.Top = $picBox.Top +$flowLayoutPanel.Left = ($picBox.Left + $picBox.Width + 10) +$flowLayoutPanel.Width = $form.Width - $flowLayoutPanel.Left - 16 +$flowLayoutPanel.Height = $picBox.Height - 5 +$flowLayoutPanel.FlowDirection = "LeftToRight" +$flowLayoutPanel.WrapContents = $true +$flowLayoutPanel.AutoScroll = $true +$form.Controls.Add($flowLayoutPanel) +$flowLayoutPanel.Add_KeyPress({ + if ($eventArgs.KeyChar -eq 'M' -and $sender.Focused) { + Write-Host "Doing the thing" + $button = $sender.GetNextControl($sender.ActiveControl, $true) + # Set the border colour of the button + If ((Get-SectionLines 'EditedFiles' $Script:cfgPath -ErrorAction SilentlyContinue) -contains $imagefile) { + $button.FlatAppearance.BorderSize = 5 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Green + } + Else { + $button.FlatAppearance.BorderSize = 1 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Black + } + } +}) + +# Create a text box to display the text file contents +$txtBox = New-Object System.Windows.Forms.TextBox +$txtBox.Text = $null +$txtBox.Multiline = $true +$txtBox.Width = $form.Width - $picBox.width - 45 +$txtBox.Height = ($form.Height - ($picBox.Top + $picBox.Height + 10) - 225) #225 is the height of the saveButton + fileNameTxtBox + clearButton + showButton +$txtBox.Left = $picBox.Left + $picBox.Width + 10 +$txtBox.Top = $flowLayoutPanel.Top + $flowLayoutPanel.Height + 10 +$form.Controls.Add($txtBox) + +# Add a button to save the edited text file +$saveButton = New-Object System.Windows.Forms.Button +$saveButton.Text = "Save" +$saveButton.Width = $txtBox.Width / 2 - 5 +$saveButton.Height = 40 +$savebutton.Top = $txtBox.Top + $txtBox.Height + 10 +$saveButton.Left = $txtBox.Left +$form.Controls.Add($saveButton) +$saveButton.Add_Click({ + $selectedTextFile = '{0}\{1}' -f $InputDir, [System.IO.Path]::ChangeExtension($selectedImageFile, '.txt') + $cleanedText = Clean-Text $txtBox.Text + $txtBox.Text = $cleanedText + Set-Content -LiteralPath $selectedTextFile -Value $cleanedText +}) + +# Create another text box to display the filename +$fileNameTxtBox = New-Object System.Windows.Forms.TextBox +$fileNameTxtBox.Multiline = $false +$fileNameTxtBox.ReadOnly = $true +$fileNameTxtBox.Width = $txtBox.Width +$fileNameTxtBox.Height = $fileNameTxtBox.PreferredHeight +$fileNameTxtBox.Left = $txtBox.Left +$fileNameTxtBox.Top = $saveButton.Top + $saveButton.Height + 10 +$fileNameTxtBox.MultiLine = $true +$fileNameTxtBox.AcceptsReturn = $true +$fileNameTxtBox.Text = $null +$form.Controls.Add($fileNameTxtBox) + +# Create a clear button +$clearButton = New-Object System.Windows.Forms.Button +$clearButton.Text = 'Clear Text' +$clearButton.Width = ($txtBox.Width / 2) - 5 +$clearButton.Height = 40 +$clearButton.Left = $picBox.Left + $picBox.Width + 10 +$clearButton.Top = $fileNameTxtBox.Top + $fileNameTxtBox.PreferredHeight + 5 +$form.Controls.Add($clearButton) +$clearButton.Add_Click({ + $Script:txtBox.Text = '' +}) + +# Add a "Show" button +$undoButton = New-Object System.Windows.Forms.Button +$undoButton.Text = "Undo" +$undoButton.Width = ($fileNameTxtBox.Width / 2) +$undoButton.Height = $saveButton.Height +$undoButton.Top = $clearButton.Top +$undoButton.Left = $clearButton.Left + $clearButton.Width + 5 +$undoButton.Enabled = $true +$Form.Controls.Add($undoButton) +$undoButton.Add_Click({ + $Script:txtBox.Undo() +}) + +# Add a "Show in explorer" button +$showButton = New-Object System.Windows.Forms.Button +$showButton.Text = "Show In Explorer" +$showButton.Width = $fileNameTxtBox.Width +$showButton.Height = $clearButton.Height +$showButton.Top = $clearButton.Top + $clearButton.Height + 15 +$showButton.Left = $clearButton.Left +$showButton.Enabled = $true +$Form.Controls.Add($showButton) +$showButton.Add_Click({ + $textfile = Resolve-Path -LiteralPath ('{0}\{1}{2}' -f $InputDir, [System.IO.Path]::GetFileNameWithoutExtension($Script:selectedImageFile)), '.txt' + Start-Process -FilePath "explorer.exe" -ArgumentList "/select, $textfile" +}) + +# Add a "Mark as Edited" button +$MarkAsEditedButton = New-Object System.Windows.Forms.Button +$MarkAsEditedButton.Text = "Mark as Edited" +$MarkAsEditedButton.Width = $txtBox.Width - $saveButton.Width - 60 +$MarkAsEditedButton.Height = $saveButton.Height +$MarkAsEditedButton.Top = $saveButton.Top +$MarkAsEditedButton.Left = $saveButton.Left + $saveButton.Width + 5 +$MarkAsEditedButton.Enabled = $true +$Form.Controls.Add($MarkAsEditedButton) +$MarkAsEditedButton.Add_Click({ + Prompt-Save $form + + foreach ($button in $flowLayoutPanel.Controls) { + if ($button.Tag -ne $Script:selectedImageFile) { + continue + } + + #if the button is green, we remove the file from the edited list + If ($button.FlatAppearance.BorderColor -eq [System.Drawing.Color]::Green) { + $button.FlatAppearance.BorderSize = 1 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Black + $MarkAsEditedButton.Text = "Mark as Edited" + } + #otherwise we add it to the edited list + Else { + $button.FlatAppearance.BorderSize = 5 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Green + $MarkAsEditedButton.Text = "Unmark as Edited" + } + break + } +}) + +# Add a "Previous" button +$prevButton = New-Object System.Windows.Forms.Button +$prevButton.Text = "<" +$prevButton.Width = 20 +$prevButton.Height = $saveButton.Height +$prevButton.Top = $saveButton.Top +$prevButton.Left = $MarkAsEditedButton.Left + $MarkAsEditedButton.Width + 5 +$prevButton.Enabled = $true +$Form.Controls.Add($prevButton) +$prevButton.Add_Click({ + $i = 0 + foreach ($button in $flowLayoutPanel.Controls) { + if ($button.Tag -eq $Script:selectedImageFile) { + If (-not $i) {return} + $control = $flowLayoutPanel.Controls[$i - 1] + $control.Select() + $control.PerformClick() + break + } + $i++ + } +}) + +# Add a "Next" button +$nextButton = New-Object System.Windows.Forms.Button +$nextButton.Text = ">" +$nextButton.Width = 20 +$nextButton.Height = $saveButton.Height +$nextButton.Top = $saveButton.Top +$nextButton.Left = $prevButton.Left + $prevButton.Width + 5 +$nextButton.Enabled = $true +$Form.Controls.Add($nextButton) +$nextButton.Add_Click({ + $i = 0 + foreach ($button in $flowLayoutPanel.Controls) { + if ($button.Tag -eq $Script:selectedImageFile) { + If ($i -ge ($flowLayoutPanel.Controls.Count - 1)) {return} + $control = $flowLayoutPanel.Controls[$i + 1] + $control.Select() + $control.PerformClick() + break + } + $i++ + } +}) + +$form.Add_Shown({ + Populate-Form $form +}) + +$form.Add_FormClosing({ + Prompt-Save $form + + Write-Config $form + + Write-CapCfg 'FolderPath' $Script:InputDir + + #release all handles to image / text files + $flowLayoutPanel.Controls | % {$_.Dispose()} + $flowLayoutPanel.Controls.Clear() + $flowLayoutPanel.Dispose() + $picBox.Dispose() + $form.Dispose() + $Script:txtBox.Dispose() +}) + +$form.ShowDialog() | Out-Null diff --git a/caplib.ps1 b/caplib.ps1 new file mode 100644 index 0000000..fc45e00 --- /dev/null +++ b/caplib.ps1 @@ -0,0 +1,407 @@ +# import assemblies +Add-Type -AssemblyName System.Windows.Forms + +Add-Type -Name Window -Namespace Console -MemberDefinition ' +[DllImport("Kernel32.dll")] +public static extern IntPtr GetConsoleWindow(); + +[DllImport("user32.dll")] +public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); +' + +# functions +Function Hide-Console { + $consolePtr = [Console.Window]::GetConsoleWindow() + [Console.Window]::ShowWindow($consolePtr, 0) | Out-Null +} + +Function Get-ImageFormats () { + $formats = [System.Drawing.Imaging.ImageCodecInfo]::GetImageEncoders().FilenameExtension + $formats = $formats.Replace('*', '') + $formats = $formats.Split(';') + $formats = $formats.ToLower() + $formats = $formats.Replace('.', '*.') + $formats = $formats.Trim().Split() + + return $formats +} + +Function Prompt-Save ($form) { + if (-not $form) {return} + + foreach ($button in $flowLayoutPanel.Controls) { + if ($button.Tag -ne $Script:selectedImageFile) { + continue + } + + if ((Get-Content -LiteralPath $Script:currentTextPath) -eq $txtBox.Text) { + break + } + + $result = [System.Windows.Forms.MessageBox]::Show("You have not saved your changes. Do you wish to save them?", "Confirmation", ` + [System.Windows.Forms.MessageBoxButtons]::YesNo, [System.Windows.Forms.MessageBoxIcon]::Question) + + if ($result -eq [System.Windows.Forms.DialogResult]::Yes) { + $saveButton.PerformClick() + } + } +} + +Function Clean-Text ($text) { + $cleanedText = $text -replace '\r\n', ', ' + $cleanedText = $cleanedText -replace ',+', ', ' + $cleanedText = $cleanedText -replace '\s+', ' ' + $cleanedText = $cleanedText.Trim(' ,').Trim().ToLower() + + Return $cleanedText +} + +Function Add-PresetControl ($top, $left, $width, $height) { + $textBox = New-Object System.Windows.Forms.TextBox + $textBox.Width = $width - 45 + $textBox.Height = $height + $textBox.Location = New-Object System.Drawing.Point($left, $top) + $form.Controls.Add($textBox) + + $button = New-Object System.Windows.Forms.Button + $button.Text = 'Add' + $button.Width = 40 + $button.Height = $height - 5 + $buttonLeft = $textBox.Left + $textBox.Width + 5 + $button.Location = New-Object System.Drawing.Point($buttonLeft, $top) + $form.Controls.Add($button) + + $Script:presetControls[$button] = $textbox + + $button.Add_Click({ + param($sender) + $currentTextBox = $Script:presetControls[$sender] + If ($currentTextBox.Text) { + $joinString = ', ' + + $Script:txtBox.Refresh() + $text = $Script:txtBox.Text + + $cursorPos = $Script:txtBox.SelectionStart + + $startText = $text.SubString(0, $cursorPos) + $midText = ($Script:presetControls[$sender]).Text + $endText = $text.SubString($cursorPos) + + $cleanStartText = (Clean-Text $startText) + $joinString + (Clean-Text $midtext) + $joinString + $cleanEndText = (Clean-Text $endText) + $joinString + $cleanText = $cleanStartText + $cleanEndText + + $Script:txtBox.Text = Clean-Text $cleanText + + If ($Script:txtBox.SelectionStart -gt ($Script:txtBox.Text.Length - 1)) { + $Script:txtBox.Text = Clean-Text $Script:txtBox.Text + $Script:txtBox.SelectionStart = $Script:txtBox.Text.Length - 1 + } + Else { + $Script:txtBox.SelectionStart = $cleanStartText.Length + } + } + }) +} + +Function Add-PresetControls ($form, $picBox) { + #get the top, left, width and height + $top = $picBox.Top + $picBox.Height + 10 + $left = $picBox.Left + + #get the default control height + $getControlHeight = New-Object System.Windows.Forms.TextBox + $controlHeight = $getControlHeight.PreferredHeight + 5 + $getControlHeight = $null + + #calculate the amount of controls that can be fit into the form's height + $numOfControlsHeight = (($form.Height - $top - 40) / $controlHeight) + $numOfControlsHeight = [System.Math]::Floor($numOfControlsHeight) + + #set the number of controls for the given width + $numOfControlsWidth = 2 + + #calculate the width that each button and textbox pair has to work with + $controlWidth = [System.Math]::Floor($picBox.Width / $numOfControlsWidth) + + $topOffset = 0 + $leftOffset = 0 + $controlCount = 0 + For ($i = 0; $i -lt $numOfControlsWidth; $i++) { + $controlLeft = $left + $leftOffset + For ($j = 0; $j -lt $numOfControlsHeight; $j++) { + $controlTop = $top + $topOffset + Add-PresetControl $controlTop $controlLeft $controlWidth $controlHeight + $topOffset += $controlHeight + $controlCount++ + } + + $topOffset = 0 + $leftOffset += $controlWidth + 5 + } +} + +Function Populate-PresetControls () { + # get preset lines + $presetLines = @() + $presetLines = Get-SectionLines 'PresetTexts' $Script:cfgPath + + #pad the array as needed + $paddingAmount = $Script:presetControls.Count - $presetLines.Size + $presetLines += [string[]]::new($paddingAmount) + + $i = 0 + ForEach ($entry in $Script:presetControls.GetEnumerator()) { + $entry.Value.Text = $presetLines[$i] + $i++ + } +} + +Function Get-SectionLines ($section, $configPath) { + $cfgContent = Get-Content -LiteralPath $configPath + + $sectionLines = @() + $isInSection = $false + + ForEach ($line in $cfgContent) { + if ($line -match "^\[($section)\]$") { + $isInSection = $true + } elseif ($line -match "^\[.+\]$") { + $isInSection = $false + } elseif ($isInSection) { + $sectionLines += $line + } + } + + Return $sectionLines +} + +Function Create-Cfg () { + If (-not (Test-Path -LiteralPath $Script:cfgPath)) { + $content = @() + $content += @('[EditedFiles]') + $content += @('[PresetTexts]') + $content += Get-SectionLines 'PresetTexts' $Script:capCfgPath + + Set-Content -LiteralPath $script:cfgPath -Value $content + } +} + +Function Populate-FlowLayoutPanel ($inputDir, $flowLayoutPanel) { + $flowLayoutPanel.Controls.Clear() + + ForEach ($imageFile in $Script:imageFiles) { + # Load the thumbnail image and add it to the flow layout panel + $button = New-Object System.Windows.Forms.Button + $button.Width = 100 + $button.Height = 100 + $thumb = [System.Drawing.Image]::FromFile($imageFile.FullName) + $thumb = $thumb.GetThumbnailImage($button.Width - 8, $button.Height - 8, $null, [System.IntPtr]::Zero) + $button.FlatStyle = "Flat" + $button.FlatAppearance.BorderSize = 1 + $button.Image = $thumb + $button.ImageAlign = "MiddleCenter" + $button.Tag = $imageFile.Name + $flowLayoutPanel.Controls.Add($button) + + Set-ButtonBorder $button + + $button.Add_Click({ + Prompt-Save $form + + # Update the picture box and text box with the selected image and text file + $Script:selectedImageFile = $this.Tag + $selectedImageFile = $this.Tag + $Script:currentImagePath = '{0}\{1}' -f $InputDir, $selectedImageFile + $picBox.Image = [System.Drawing.Image]::FromFile($Script:currentImagePath) + $selectedTextFile = '{0}\{1}' -f $InputDir, [System.IO.Path]::ChangeExtension($selectedImageFile, '.txt') + + # create the text file if it DNE + If (-not (Test-Path -LiteralPath $selectedTextFile)) { + Set-Content -LiteralPath $selectedTextFile -Value '' + } + + $Script:currentTextPath = $selectedTextFile + $Script:txtBox.Text = Get-Content -LiteralPath $selectedTextFile + $Script:txtBox.SelectionStart = $txtBox.Text.Length + $fileNameTxtBox.Text = $Script:selectedImageFile + + If ($this.FlatAppearance.BorderColor -eq [System.Drawing.Color]::Green) { + $MarkAsEditedButton.Text = "Unmark as Edited" + } + Else { + $MarkAsEditedButton.Text = "Mark as Edited" + } + }) + + $button.Add_KeyPress({ + param($sender, $eventArgs) + if ($eventArgs.KeyChar -eq 'M') { + $MarkAsEditedButton.PerformClick() + } + }) + } +} + +Function Set-ButtonBorder ($button) { + $imageFile = $button.tag + # Set the border colour of the button + If ((Get-SectionLines 'EditedFiles' $Script:cfgPath -ErrorAction SilentlyContinue) -contains $imageFile) { + $button.FlatAppearance.BorderSize = 5 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Green + } + Else { + $button.FlatAppearance.BorderSize = 1 + $button.FlatAppearance.BorderColor = [System.Drawing.Color]::Black + } +} + +Function Open-Folder ($form) { + If (-not $form) { + #try and get the last folder if it exists + $savedFolderPath = Get-SectionLines 'CapConfig' $Script:capCfgPath | Where-Object {$_ -Match '^FolderPath=.+'} + If ($savedFolderPath) { + $savedFolderPath = $savedFolderPath.Split('=')[-1] + If (Test-Path -LiteralPath $savedFolderPath) { + $imageFiles = Get-ChildItem -LiteralPath $savedFolderPath -Include $Script:imageFormats + If ($imageFiles) { + Init-Form $savedFolderPath $form $ImageFiles + return + } + Else { + Write-CapCfg 'FolderPath' '' + Open-Folder $form + return + } + } + } + } + + $folderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog + $folderBrowserDialog.ShowNewFolderButton = $false + + $response = $folderBrowserDialog.ShowDialog() + + If ($response -eq [System.Windows.Forms.DialogResult]::OK) { + $imageFiles = Get-ChildItem -LiteralPath $folderBrowserDialog.SelectedPath -Include $Script:imageFormats + If ($imageFiles) { + Init-Form $folderBrowserDialog.SelectedPath $form $ImageFiles + $folderBrowserDialog.Dispose() + } + Else { + Open-Folder $form + } + } + Else { + $folderBrowserDialog.Dispose() + If (-not $form) {exit} + } +} + +Function Init-Form ($InputDir, $form, $ImageFiles) { + Prompt-Save $form + Write-Config $form + + If (-not $form) { + $Script:presetControls = [ordered]@{} + $Script:txtBox = $null + } + + $Script:InputDir = Resolve-Path -LiteralPath $InputDir + $Script:InputDirName = Split-Path $InputDir -Leaf + $Script:cfgPath = '{0}\{1}{2}' -f $InputDir, $InputDirName, '.cfg' + + $Script:imageFiles = $ImageFiles + $Script:selectedImageFile = $null + $Script:currentImagePath = $null + $Script:currentTextPath = $null + + # make a cfg file if it dne + Create-Cfg + + Populate-Form $form +} + +Function Populate-Form ($form) { + if (-not $form) {return} + + #populate the flow layout panel + Populate-FlowLayoutPanel $InputDir $flowLayoutPanel + + #add all of the preset controls + Populate-PresetControls + + # select the first control in the flow layout panel + $control = $flowLayoutPanel.Controls[0] + $control.Select() + $control.PerformClick() +} + +Function Write-Config ($form) { + if (-not $form) {return} + + $cfgContent = New-Object System.Collections.Generic.List[string] + + #set the edited files section of the config file + $cfgContent.Add('[EditedFiles]') + ForEach ($button in $flowLayoutPanel.Controls) { + #if the button is green, we add the file to the edited list + If ($button.FlatAppearance.BorderColor -eq [System.Drawing.Color]::Green) { + $cfgContent.Add($button.tag) + } + } + + #set the preset texts section of the config file + $cfgContent.Add('[PresetTexts]') + ForEach($entry in ($Script:presetControls).GetEnumerator()) { + $cfgContent.Add($entry.Value.Text) + } + + Set-Content -LiteralPath $Script:cfgPath -Value $cfgContent +} + +Function Write-CapCfg ($key, $value) { + If (-not $key) {return} + + $entry = '{0}={1}' -f $key, $value + + $cfgContent = New-Object System.Collections.Generic.List[string] + + #set the config section of the config file + $cfgContent.Add('[CapConfig]') + + $sectionLines = @() + $sectionLines += Get-SectionLines 'CapConfig' $Script:capCfgPath + + $i = 0 + $matchFlag = $false + ForEach ($line in $sectionLines) { + If ($line -match "^$key=(.+|$)") { + $matchFlag = $true + $sectionLines[$i] = $entry + break + } + + $i++ + } + + If (-not $matchFlag) { + $sectionLines += $entry + } + + ForEach ($line in $sectionLines) { + $cfgContent.Add($line) + } + + #set the preset section of the config file + $cfgContent.Add('[PresetTexts]') + + $sectionLines = @() + $sectionLines += Get-SectionLines 'PresetTexts' $Script:capCfgPath + ForEach ($line in $sectionLines) { + $cfgContent.Add($line) + } + + Set-Content -LiteralPath $Script:capCfgPath -Value $cfgContent +}