Wrapping SpinGadget with optional text.

Share your advanced PureBasic knowledge/code with the community.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Wrapping SpinGadget with optional text.

Post by einander »

Code: Select all

;by einander
;january 19-2009
;PB 4.30
Procedure SpinWrap(SG,EvTyp, ARRAY Arr.s(1))
   Static Value
   Select EvTyp
      Case 1 
         If Value=GetGadgetAttribute(SG,#PB_Spin_Maximum)
            SetGadgetState(SG,GetGadgetAttribute(SG,#PB_Spin_Minimum))
         EndIf
      Case -1 
         If Value = GetGadgetAttribute(SG,#PB_Spin_Minimum)
            SetGadgetState(SG,GetGadgetAttribute(SG,#PB_Spin_Maximum))
         EndIf
   EndSelect 
   If ArraySize(Arr())
      SetGadgetText(SG, Arr(GetGadgetState(SG)))
   Else
      SetGadgetText(SG, Str(GetGadgetState(SG)))
   EndIf
   Value=GetGadgetState(SG)
EndProcedure

;<<<<<<<<<<<<<<<<<<<
; test it
If OpenWindow(0, 200,200,300,100, "SpinWrap", #PB_Window_SystemMenu | 1 )
   StickyWindow(0,1)
   SG=SpinGadget(#PB_Any, 40, 10, 100, 25, 0, 4)
   SetGadgetState (SG, 2)   

;==============   
; To wrap numbers (without text), Dim SpinArr.s(0)  
   Dim SpinArr.s(4)
   SpinArr(0)="Zero"
   SpinArr(1)="One"
   SpinArr(2)="Two"
   SpinArr(3)="Three"
   SpinArr(4)="Four"
   SetGadgetText(SG,SpinArr(GetGadgetState(SG)))
;=============
   Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Gadget
         If EventGadget() = SG
            SpinWrap(SG,EventType(),SpinArr() )
         EndIf
      EndIf
   Until Event = #PB_Event_CloseWindow
EndIf
Cheers
Last edited by einander on Wed Jan 21, 2009 12:09 am, edited 1 time in total.
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Post by Kwai chang caine »

Cool and funny this idea :D
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Useful when there are lots of states, and it is also portable!
Salut! Roberto. Still waiting to see your newest versions of midi keyboard... :wink:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Thanks and salut!
I Can't found the old MidiKeyboard on my disks; probably is buried under a pile of CDs, so I've download it from the Forum.
I'm making a revamp, and I'll upload it soon.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

@einander:

I think there is a bugette in your Procedure SpinWrap() in that

Code: Select all

If ArraySize(Arr())>1
should be

Code: Select all

If ArraySize(Arr())>0
Anthony Jordan
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Post by einander »

Thanks AKJ.
I've forgot to check the return value for the new PB command ArraySize() when i've replaced my custom ArraySize(), that returns the number of elements. :oops:
Now the code is updated.

Here is a better variant with a structure, to use more than one SpinGadget simultaneously.

Code: Select all

#nElements1=5

Structure SpinWrap
    SpinNum.i
    nElements.l
    Arr.s[#nElements1]     
    Value.i
EndStructure

Procedure SpinWrap(*SP.SpinWrap)
    With *SP
        Select EventType()
            Case 1 
                If \Value=GetGadgetAttribute(\SpinNum,#PB_Spin_Maximum)
                    SetGadgetState(\SpinNum,GetGadgetAttribute(\SpinNum,#PB_Spin_Minimum))
                EndIf
            Case -1 
                If \Value = GetGadgetAttribute(\SpinNum,#PB_Spin_Minimum)
                    SetGadgetState(\SpinNum,GetGadgetAttribute(\SpinNum,#PB_Spin_Maximum))
                EndIf
        EndSelect 
        If \nElements : SetGadgetText(\SpinNum, \Arr[GetGadgetState(\SpinNum)])
        Else               : SetGadgetText(\SpinNum, Str(GetGadgetState(\SpinNum)))
        EndIf
        \Value=GetGadgetState(\SpinNum)
    EndWith
EndProcedure

;<<<<<<<<<<<<<<<<<<<
; test it
If OpenWindow(0, 200,200,300,300, "SpinWrap", #PB_Window_SystemMenu | 1 )
    StickyWindow(0,1)
    
    SW1.SpinWrap
    SW1\SpinNum=SpinGadget(#PB_Any, 40, 10, 100, 25, 0, 4)
    SW1\nElements=#nElements1
    SetGadgetState (SW1\SpinNum, 2)   ; initial value
    
    SW1\Arr[0]="Zero"
    SW1\Arr[1]="One"
    SW1\Arr[2]="Two"
    SW1\Arr[3]="Three"
    SW1\Arr[4]="Four"
    SetGadgetText(SW1\SpinNum,SW1\Arr[GetGadgetState(SW1\SpinNum)])
    ;------------

    SW2.SpinWrap  ; ignore the array to wrap only numbers
    SW2\SpinNum=SpinGadget(#PB_Any, 40, 50, 50, 25, 1,10)
    SetGadgetState (SW2\SpinNum, 8)  ; initial Value   
    SetGadgetText(SW2\SpinNum,Str(GetGadgetState(SW2\SpinNum)))
    ; 
    Repeat
        Event = WaitWindowEvent()
        If Event = #PB_Event_Gadget
            Select EventGadget()
                Case  SW1\SpinNum : SpinWrap(SW1 )
                Case  SW2\SpinNum : SpinWrap(SW2 )
            EndSelect
        EndIf
    Until Event = #PB_Event_CloseWindow
EndIf
Post Reply