A simple sample of using animation control object in Basic

REM  *****  BASIC  *****

Private oDlg As Object
Private oLbl As Object
Private oAnim As Object
Private oTopListener As Object
Private basePath As String
Private arrayNames() As String
Private IsGained As Boolean
Private oTopListener  As Object
Private oFocusListener As Object
Private IsGained  As Boolean

Sub BuildUI

	Dim oDialogModel As Object
	Dim oDialog As Object
	Dim oAnimation As Object
	Dim oControl As Object
	
	basePath = Left(thisComponent.Url, InStrReverse(thisComponent.Url, "/"))
	Redim arrayNames()
	Dim patterns As Variant
	patterns = Array("*.öäå", "*.gif")
	Dim i As Long, f As String, IsExisting As Boolean
	Dim sfa As Object
	sfa = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
	
	For i = 0 To UBound(patterns)
		f = Dir(basePath & patterns(i))	
		Do While f <> ""
			If sfa.exists(basePath & f) Then
				IsExisting = True : Exit Do
			End If
		f = Dir()
		Loop
		If IsExisting Then Exit For
	Next i
	
	If Not IsExisting Then
		MsgBox "No iamge files found from the project directory", "48", "System"
		sfa = Nothing : Exit Sub
	End If
	
	Dim items As Variant, lstNames As String , j As Integer
	items = sfa.getFolderContents(basePath, True)
	For i = LBound(items) To UBound(items)
		For j = Lbound(patterns) To Ubound(patterns)
			If InStr(items(i), Replace(patterns(j), "*", "")) > 0 Then
				lstFnames = lstFnames & items(i) & ","
			End If
		Next j
	Next i

	sfa = Nothing
	lstFnames = Left(lstFnames, Len(lstFnames) - 1)
	arrayNames = Split(lstFnames, ",") : lstNames = ""

	oDialogModel = CreateUnoService("com.sun.star.awt.UnoControlDialogModel")
	oDialogModel.Width = 150
	oDialogModel.Height = 120
	oDialogModel.TabIndex = 0
	oDialogModel.BackGroundColor = RGB(225,225,225)

	oDialog = CreateUnoService("com.sun.star.awt.UnoControlDialog")
	oDialog.setModel(oDialogModel)
	oDlg = oDialog : oDlg.Title = "Animation in Pure Basic"
	oTopListener = createUnoListener("TopListen_", "com.sun.star.awt.XTopWindowListener")
	oDlg.addTopWindowlistener(oTopListener)

	lblFocusModel = oDialogModel.createInstance("com.sun.star.awt.UnoControlFixedTextModel")
	lblFocusModel.Name = "Label1"
	lblFocusModel.PositionX = 149
	lblFocusModel.PositionY = 119
	lblFocusModel.Label = ""
	lblFocusModel.Width = 1
	lblFocusModel.Height = 1
	lblFocusModel.TabIndex = 1
	lblFocusModel.TabStop = True

	oDialogModel.insertByName("Label1", lblFocusModel)
	oLbl = oDlg.getControl("Label1")
	AddFocusListenerToControl oLbl
	
    oAnimationModel = oDialogModel.createInstance("com.sun.star.awt.AnimatedImagesControlModel")
	oAnimationModel.Name = "oAnimation1"
	oAnimationModel.PositionX = 5
	oAnimationModel.PositionY = 5
	oAnimationModel.Width = 140
	oAnimationModel.Height = 110
	oAnimationModel.ScaleMode = 1
	oAnimationModel.BackGroundColor = RGB(0,0,0)
	oDialogModel.insertByName("oAnimation1", oAnimationModel)

    oDialog.createPeer(CreateUnoService("com.sun.star.awt.Toolkit"), Null)
   
	On Error Resume Next
	Dim oSize As New com.sun.star.awt.Size, factor As Double
	Dim oCC, oComponentWindow, oTopWindowPosSize
	oCC = ThisComponent.getCurrentController()
	oComponentWindow = oCC.ComponentWindow
	oTopWindowPosSize = oComponentWindow.Toolkit.ActiveTopWindow.getPosSize()

	oSize.Width = oDialog.Model.Width
	oSize.Height = oDialog.Model.Height
	factor = oSize.Width / oDialog.convertSizeToPixel(oSize, com.sun.star.util.MeasureUnit.APPFONT).Width

	With oDialog.Model
		.PositionX = (factor * oTopWindowPosSize.Width - .Width) / 2 
		.PositionY = (factor * oTopWindowPosSize.Height - .Height) / 1.75
	End With
	If Err <> 0 Then Err.Clear
	IsGained = False
	ITicks = GetSystemTicks()
	
	Do 
		If GetSystemTicks() > ITicks + 100 Then Exit Do
		DoEvents
	Loop

	oDlg.execute

	RemoveFocusListenerFromControl oLbl
	oLbl.dispose : oAnim.dispose
	oDlg.dispose

End Sub

Sub ShowAnimation

	oDlg.getPeer().setVisible(False)
	oDlg.getPeer().setVisible(True)
	oAnim = oDlg.getControl("oAnimation1")
	oAnim.Model.AutoRepeat = True
	oAnim.Model.StepTime = 60  ' test
	oAnim.Model.insertImageSet(0, arrayNames)
	oAnim.startAnimation()
	
End Sub

Sub TopListen_windowOpened
End Sub
Sub TopListen_windowClosed
End Sub
Sub TopListen_windowMinimized
End Sub
Sub TopListen_windowClosing
End Sub
Sub TopListen_windowNormalized
End Sub
Sub TopListen_windowActivated
End Sub
Sub TopListen_windowDeactivated
	oDlg.removeTopWindowlistener(oTopListener)
	oTopListener = Nothing
End Sub
Sub TopListen_disposing
End Sub

Sub AddFocusListenerToControl(oControl As Object)
	oFocusListener = CreateUnoListener("FocusListener_","com.sun.star.awt.XFocusListener")
	oControl.addFocusListener(oFocusListener)
End Sub

Sub RemoveFocusListenerFromControl(oControl As Object)
		oControl.removeFocusListener(oFocusListener)
		oFocusListener = Nothing
End Sub

Sub FocusListener_focusGained(oEvent)

	If Not  IsGained  Then
		 IsGained = True
		 ShowAnimation
		 oDlg.endexecute
	End If

End Sub

Sub FocusListener_focusLost(oEvent)
End Sub

Sub  FocusListener_disposing : End Sub

Function InStrReverse(sText As String, search As String) As Long
	
	If sText = "" Or search = "" Or Len(search) > Len(sText) Then
    	InStrReverse = 0
    	Exit Function
	End If
	
	Dim i As Long

	For i = Len(sText) To 1 Step -1
		If Mid(sText, i, Len(search)) = search Then
			InStrReverse = i : Exit Function
		End If
	Next i
	
End Function

Sub OnClose
	ThisComponent.setModified(False)
End Sub

AnimationInPureBasic.odt (16.4 KB)

AnimationInPureBasic

The project package with sample frame files can be downloaded here

1 Like

I don’t like animations, but your example is funny, interesting idea :slight_smile:

1 Like