Sub Class_Globals
Private Root As B4XView 'ignore
Private xui As XUI 'ignore
Private TappedCard As B4XImageView
Private CardSize, Counter As Int
Private LstCards As List
Private MapRotation As Map
Private B4XImgViewCard(12) As B4XImageView
Private Angle, AddAngle As Int = 10 'AddAngle can be 5, 10 or 15
Private xui As XUI
Private CardBack, CardFace As B4XView
Private pnlMain As Panel
End Sub
'You can add more parameters here.
Public Sub Initialize As Object
LstCards.Initialize
MapRotation.Initialize
Return Me
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
'load the layout to Root
Root.LoadLayout("Page3")
End Sub
Private Sub B4XPage_Appear
PlaceCards
Log("Create")
End Sub
'PLACE 12 CARDS ONTO THE SCREEN
Sub PlaceCards
Dim kardc As Int = 1
'Load card file names into the list
For Each FileName As String In File.ListFiles(File.DirAssets)
If FileName.EndsWith(".png") And Not(FileName.StartsWith("back.")) Then LstCards.Add(FileName)
Next
LstCards = ShuffleCards(LstCards) 'Shuffle the cards
For Each cardname As String In LstCards
Log("Card number: " & kardc & " Card Name: " & cardname)
kardc = kardc + 1
Next
Dim OffSetX, OffSetY, Rows = 3, Columns = 2 As Int
CardSize = pnlMain.Width / 8
OffSetX = (pnlMain.Width - CardSize * Columns - 10dip * (Columns - 1)) / 2
OffSetY = (pnlMain.Height - CardSize * Rows - 10dip * (Rows - 1)) / 2
CardBack = xui.LoadBitmapResize(File.DirAssets, "back.png", CardSize, CardSize, True) 'Load the flag back image
'Place 2 x 6 shuffled flags onto a panel of 12 3x4 flags
For X = 0 To Columns - 1
For Y = 0 To Rows - 1
B4XImgViewCard(Counter).Initialize(Me, "B4XImgViewCard")
B4XImgViewCard(Counter).AddToParent(pnlMain, OffSetX + x * (CardSize + 10dip), OffSetY + y * (CardSize + 10dip), CardSize, CardSize)
B4XImgViewCard(Counter).B4XBitmap = CardBack 'Set card back image
B4XImgViewCard(Counter).Tag = $"${LstCards.Get(Counter)}~${Counter}"$ 'Store card name and card position
MapRotation.Put(Counter, 0) 'Store card name and set rotation to 0
Counter = Counter + 1
Next
Next
End Sub
'CLICK ON CARD ROTATE THE CARD
Sub B4XImgViewCard_OneClick
Dim slowd As Int = 50
Dim card_name As String
If Not(Angle = 0) Then Return 'Wait for card angle to be 0 before spinning the nect card
TappedCard = Sender 'Create a sender to capture B4XImageView click
'LogColor("Card: " & TappedCard.Tag,Colors.Red) 'ime karte in številka karte od 0 - nn
card_name = Regex.Split("_", TappedCard.Tag)(0).Replace("_", " ").Replace(".png", "")
'card_name = Regex.Split("_", TappedCard.Tag)(0).Replace("_", " ").Replace(".png", "")
LogColor(card_name,Colors.Blue)
Dim CardRotation As Int = Regex.Split("~", TappedCard.Tag)(1) 'Read card position
CardFace = xui.LoadBitmapResize(File.DirAssets, Regex.Split("~", TappedCard.Tag)(0), CardSize, CardSize, True) 'Load the card face image
DisableCards(True) 'Disable cards
If 180 = MapRotation.Get(CardRotation) Then Angle = 180 'Check last tapped card rotation position against the current angle and set the rotation angle
For i = 0 To 180
#If debug
slowd= 1
#end if
Sleep(slowd)
Angle = Angle + AddAngle 'Set the angle
B4XImgViewCard(CardRotation).Rotate(0, Angle + 180, 0) 'Rotate the card to the newly set angle
Select Angle
Case 90 'At 90°
B4XImgViewCard(CardRotation).B4XBitmap = CardFace 'Set card face image
Case 180 'At 180° 'če to izpustim, se karta zavrti za 360
Angle = 0
MapRotation.Put(CardRotation, 180) 'Set card rotation to angle
DisableCards(False) 'Enable cards
Log("End of Card_ON_Click - 180")
Return
Case 270 'At 270°
B4XImgViewCard(CardRotation).B4XBitmap = CardBack 'Set card back image
Case 360 'At 360°
Angle = 0
MapRotation.Put(CardRotation, 0) 'Set card rotation to 0
DisableCards(False) 'Enable cards
Log("End of Card_ON_Click - 360")
Return
End Select
Sleep(0)
Next
End Sub
'ENABLE OR DISABLE DORMANT CARDS
Sub DisableCards(SetCardsState As Boolean)
For i = 0 To Counter - 1
If Not(SetCardsState) Then
B4XImgViewCard(i).B4XObject.Enabled = True
Else
B4XImgViewCard(i).B4XObject.Enabled = False 'Enable/Disable cards
End If
Next
End Sub
'SHUFFLE CARDS
Sub ShuffleCards(Cards As List) As List
Dim ArrayVal As String
Dim Random As Int
For i = 0 To Cards.Size - 1
Random = Rnd(i, Cards.Size)
ArrayVal = Cards.Get(i)
Cards.Set(i, Cards.Get(Random))
Cards.Set(Random, ArrayVal)
Next
Return Cards
End Sub
Sub shuffle_Click
pnlMain.RemoveAllViews
Counter = 0
LstCards.Initialize
PlaceCards
End Sub