Learning Basic For Total Beginners (BlitzMax NG)

Started by Midimaster, December 22, 2021, 18:29:29

Previous topic - Next topic

Midimaster

#30
Radio Group Buttons

I expanded the last lesson to demonstrate RADIO BUTTONS. Please have again a look on the previous lesson:
https://www.syntaxbomb.com/index.php?msg=347060951



The Button Neighbour Problem


If more than one button can be pressed at the same time, you need different images for the case, when two neighbours are pressed:

RadioButtonProblem.gif


This need 2 more images and a complex search algorithm:

    Method FindBest:Int()
        If Selected=0 Return 0
        If Search(ID-1)
            If Search(ID-1).Selected=True
                If Search(ID+1).Selected=True
                    Return 4
                EndIf
                Return 2           
            EndIf
        EndIf
        If Search(ID+1)
            If Search(ID+1).Selected=True
                Return 1
            EndIf
        EndIf
        Return 3
    End Method
   
    Method Search:TButton(SearchId:Int)
        For Local loc:TButton=EachIn All
            If loc.ID=SearchID
                Return loc
            EndIf
        Next
        Return Null
    End Method


Now it looks better:

RadioButtonSolved.gif


This is the whole code:

SuperStrict
Graphics 500,200
For Local i:Int= 0 To 5
    New TButton(i+1,30+i*66,30,66,122, 0)
Next

SetClsColor 0,155,155

Global MouseState:Int
Repeat
    Cls
    MouseState = MouseHit(1)
    TButton.DrawAll
    Local Which:TButton = TButton.CheckAll()
    If Which
        If Which.Selected=1
            Print "Button " + Which.ID + " selected"      
        Else
            Print "Button " + Which.ID + " un-selected"
        EndIf
    EndIf
    Flip
Until AppTerminate()


Type TButton
    Global All:TList = New TList
  
    Field ID:Int, X:Int, Y:Int, W:Int, H:Int
    Field Selected:Int, RadioGroup:Int
    Global Images:TImage
  
    Method New (id:Int, x:Int, y:Int, w:Int, h:Int, group:Int)
        If Images=Null
            Images=LoadAnimImage("VintageButtonBetter.png",66,122,0,5)
        EndIf
        Self.ID = id
        Self.X  = x
        Self.Y  = y
        Self.W  = w
        Self.H  = h
        Self.RadioGroup = group
        All.Addlast Self
    End Method
  
    Function DrawAll()
        For Local loc:TButton = EachIn All
            loc.Draw
        Next
        SetColor 255,255,255
    End Function
  
    Method Draw()
        SetColor 255,255,255
        Local ImageNr:Int = FindBest()
        DrawImage Images,X,Y,ImageNr
     End Method
  
    Method FindBest:Int()
        If Selected=0 Return 0
        If Search(ID-1)
            If Search(ID-1).Selected=True
                If Search(ID+1).Selected=True
                    Return 4
                EndIf
                Return 2           
            EndIf
        EndIf
        If Search(ID+1)
            If Search(ID+1).Selected=True
                Return 1
            EndIf
        EndIf
        Return 3
    End Method
   
    Method Search:TButton(SearchId:Int)
        For Local loc:TButton=EachIn All
            If loc.ID=SearchID
                Return loc
            EndIf
        Next
        Return Null
    End Method
   
    Function CheckAll:TButton ()
        If MouseState=0 Return Null
        For Local loc:TButton = EachIn All
            Local result:Int = loc.Check()
            If result>0 Return loc
        Next
        Return Null  
    End Function
  
    Method Check:Int()
        Local mX:Int = MouseX() - X
        Local mY:Int = MouseY() - Y
        If mX<0 Or mX>W Return 0
        If mY<0 Or mY>H Return 0
        Selected=1-Selected
        If Selected=1
            Deselect RadioGroup
        EndIf
        Return ID
    End Method

    Method DeSelect(Group:Int)
        If RadioGroup=0 Return
        For Local loc:TButton=EachIn All
            If loc=Self Continue
            If loc.RadioGroup=Group
                loc.Selected=0
            EndIf
        Next
    End Method
End Type


And you need this better PNG to run the app:

VintageButtonBetter.png


...back from Egypt