@@ -387,6 +387,21 @@ Module moduleB2S
387387 Return Drawing.Color.FromArgb( CInt (colorvalues( 0 )), CInt (colorvalues( 1 )), CInt (colorvalues( 2 )))
388388 End Function
389389
390+ Public Function SafeParseInt(_value As String , _defaultValue As Integer ) As Integer
391+ Dim result As Integer
392+ If Integer .TryParse(_value, result) Then
393+ Return result
394+ End If
395+ Return _defaultValue
396+ End Function
397+ Public Function SafeParseBool(_value As String , _defaultValue As Boolean ) As Boolean
398+ Dim result As Integer
399+ If Integer .TryParse(_value, result) Then
400+ Return result = 1
401+ End If
402+ Return _defaultValue
403+ End Function
404+
390405 Public Function TranslateIndex2DodgeColor( ByVal index As Integer ) As Color
391406 Select Case index
392407 Case 1 : Return Color.Red
@@ -568,4 +583,98 @@ Module moduleB2S
568583 Return ExportSetImages(_selectedName, _selectedIndex, _xmlPath, DefaultLEDs, "//LEDSets" )
569584 End Function
570585
586+ Public Function ImportAnimations(_filePath As String ) As List( Of Animation.AnimationHeader)
587+ Dim importedAnimations As New List( Of Animation.AnimationHeader)
588+
589+ ' Load and validate the XML file
590+ Dim xmlDoc As New Xml.XmlDocument()
591+ xmlDoc.Load(_filePath)
592+
593+ ' Validate the root element
594+ If xmlDoc.DocumentElement Is Nothing OrElse xmlDoc.DocumentElement.Name <> "Animations" Then
595+ Throw New Exception( "The file does not have a valid <Animations> root element." )
596+ End If
597+
598+ ' Iterate through each <Animation> node
599+ For Each animationNode As Xml.XmlNode In xmlDoc.DocumentElement.SelectNodes( "Animation" )
600+ Try
601+ Dim animation As New Animation.AnimationHeader()
602+
603+ ' Safely read attributes
604+ animation.Name = If (animationNode.Attributes( "Name" )?.Value, String .Empty)
605+ animation.DualMode = SafeParseInt(animationNode.Attributes( "DualMode" )?.Value, 0 )
606+ animation.Interval = SafeParseInt(animationNode.Attributes( "Interval" )?.Value, 0 )
607+ animation.Loops = SafeParseInt(animationNode.Attributes( "Loops" )?.Value, 0 )
608+ animation.IDJoin = If (animationNode.Attributes( "IDJoin" )?.Value, String .Empty)
609+ animation.StartAnimationAtBackglassStartup = SafeParseBool(animationNode.Attributes( "StartAnimationAtBackglassStartup" )?.Value, False )
610+ animation.LightsStateAtAnimationStart = SafeParseInt(animationNode.Attributes( "LightsStateAtAnimationStart" )?.Value, 0 )
611+ animation.LightsStateAtAnimationEnd = SafeParseInt(animationNode.Attributes( "LightsStateAtAnimationEnd" )?.Value, 0 )
612+ animation.AnimationStopBehaviour = SafeParseInt(animationNode.Attributes( "AnimationStopBehaviour" )?.Value, 0 )
613+ animation.LockInvolvedLamps = SafeParseBool(animationNode.Attributes( "LockInvolvedLamps" )?.Value, False )
614+ animation.HideScoreDisplays = SafeParseBool(animationNode.Attributes( "HideScoreDisplays" )?.Value, False )
615+ animation.BringToFront = SafeParseBool(animationNode.Attributes( "BringToFront" )?.Value, False )
616+ animation.RandomStart = SafeParseBool(animationNode.Attributes( "RandomStart" )?.Value, False )
617+ animation.RandomQuality = SafeParseInt(animationNode.Attributes( "RandomQuality" )?.Value, 1 )
618+
619+ ' Read animation steps
620+ For Each stepNode As Xml.XmlNode In animationNode.SelectNodes( "AnimationStep" )
621+ Dim stepItem As New Animation.AnimationStep()
622+ stepItem.Step = SafeParseInt(stepNode.Attributes( "Step" )?.Value, 0 )
623+ stepItem.On = If (stepNode.Attributes( "On" )?.Value, String .Empty)
624+ stepItem.WaitLoopsAfterOn = SafeParseInt(stepNode.Attributes( "WaitLoopsAfterOn" )?.Value, 0 )
625+ stepItem.Off = If (stepNode.Attributes( "Off" )?.Value, String .Empty)
626+ stepItem.WaitLoopsAfterOff = SafeParseInt(stepNode.Attributes( "WaitLoopsAfterOff" )?.Value, 0 )
627+ animation.AnimationSteps.Add(stepItem)
628+ Next
629+
630+ ' Add the animation to the list
631+ importedAnimations.Add(animation)
632+ Catch ex As Exception
633+ ' Log the issue for debugging
634+ MessageBox.Show( $"Error parsing animation: {ex.Message}" , "Error" , MessageBoxButtons.OK, MessageBoxIcon.Error)
635+ End Try
636+ Next
637+
638+ Return importedAnimations
639+ End Function
640+
641+ Public Sub ExportAnimation(_animationHeader As Animation.AnimationHeader, _filePath As String )
642+ Dim xmlDoc As New Xml.XmlDocument()
643+ Dim animationsNode As Xml.XmlElement = xmlDoc.CreateElement( "Animations" )
644+ xmlDoc.AppendChild(animationsNode)
645+
646+ ' Create the Animation node with attributes
647+ Dim animationNode As Xml.XmlElement = xmlDoc.CreateElement( "Animation" )
648+ animationNode.SetAttribute( "Name" , _animationHeader.Name)
649+ animationNode.SetAttribute( "DualMode" , CType (_animationHeader.DualMode, Integer ).ToString()) ' Ensure numeric value is saved
650+ animationNode.SetAttribute( "Interval" , _animationHeader.Interval.ToString())
651+ animationNode.SetAttribute( "Loops" , _animationHeader.Loops.ToString())
652+ animationNode.SetAttribute( "IDJoin" , If ( String .IsNullOrEmpty(_animationHeader.IDJoin), "" , _animationHeader.IDJoin))
653+ animationNode.SetAttribute( "StartAnimationAtBackglassStartup" , If (_animationHeader.StartAnimationAtBackglassStartup, "1" , "0" ))
654+ animationNode.SetAttribute( "LightsStateAtAnimationStart" , CType (_animationHeader.LightsStateAtAnimationStart, Integer ).ToString())
655+ animationNode.SetAttribute( "LightsStateAtAnimationEnd" , CType (_animationHeader.LightsStateAtAnimationEnd, Integer ).ToString())
656+ animationNode.SetAttribute( "AnimationStopBehaviour" , CType (_animationHeader.AnimationStopBehaviour, Integer ).ToString())
657+ animationNode.SetAttribute( "LockInvolvedLamps" , If (_animationHeader.LockInvolvedLamps, "1" , "0" ))
658+ animationNode.SetAttribute( "HideScoreDisplays" , If (_animationHeader.HideScoreDisplays, "1" , "0" ))
659+ animationNode.SetAttribute( "BringToFront" , If (_animationHeader.BringToFront, "1" , "0" ))
660+ animationNode.SetAttribute( "RandomStart" , If (_animationHeader.RandomStart, "1" , "0" ))
661+ animationNode.SetAttribute( "RandomQuality" , CType (_animationHeader.RandomQuality, Integer ).ToString())
662+
663+ ' Add Animation Steps as child nodes
664+ For Each stepItem As Animation.AnimationStep In _animationHeader.AnimationSteps
665+ Dim stepNode As Xml.XmlElement = xmlDoc.CreateElement( "AnimationStep" )
666+ stepNode.SetAttribute( "Step" , stepItem.Step.ToString())
667+ stepNode.SetAttribute( "On" , stepItem.On)
668+ stepNode.SetAttribute( "WaitLoopsAfterOn" , stepItem.WaitLoopsAfterOn.ToString())
669+ stepNode.SetAttribute( "Off" , stepItem.Off)
670+ stepNode.SetAttribute( "WaitLoopsAfterOff" , stepItem.WaitLoopsAfterOff.ToString())
671+ animationNode.AppendChild(stepNode)
672+ Next
673+
674+ ' Append the animation node to the XML structure
675+ animationsNode.AppendChild(animationNode)
676+
677+ ' Save the XML to the specified file path
678+ xmlDoc.Save(_filePath)
679+ End Sub
571680End Module
0 commit comments