LEDGadget() (the round ones)

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

LEDGadget() (the round ones)

Post by infratec »

Hi,

since I never posted it in Tips and Tricks, only somewhere in an other thread,
I'll post it now as an improved version.

Code: Select all

;
; LEDGadget.pbi
;

CompilerIf #PB_Compiler_IsMainFile
EnableExplicit
CompilerEndIf

Enumeration
  #LEDGadget_OnColor
  #LEDGadget_OffColor
  #LEDGadget_BorderColor
  #LEDGadget_BackColor
EndEnumeration


Structure LEDGadgetStructure
  Radius.i
  OnColor.i
  OffColor.i
  BackColor.i
  ActualColor.i
  BorderColor.i
  OnImg.i
  OffImg.i
EndStructure



Procedure LEDGadgetDraw(Gadget)
  
  Protected *LEDGadget.LEDGadgetStructure
  
  
  If IsGadget(Gadget)
    *LEDGadget = GetGadgetData(Gadget)
    With *LEDGadget
      If StartDrawing(CanvasOutput(Gadget))
        
        If \OnImg <> -1 And \OffImg <> -1
          
          If \ActualColor = \OnColor
            DrawImage(ImageID(\OnImg), 0, 0)
          Else
            DrawImage(ImageID(\OffImg), 0, 0)
          EndIf
          
        Else
          
          Box(0, 0, OutputWidth(), OutputHeight(), \BackColor)
          If \BorderColor <> -1
            Circle(\Radius, \Radius, \Radius, \BorderColor)
            Circle(\Radius, \Radius, \Radius - 2, \ActualColor)
          Else
            Circle(\Radius, \Radius, \Radius, \ActualColor)
          EndIf
          
        EndIf
        
        StopDrawing()
      EndIf
    EndWith
  EndIf
  
EndProcedure


Procedure LEDGadgetSetState(Gadget, State.i)
  
  Protected *LEDGadget.LEDGadgetStructure
  
  
  If IsGadget(Gadget)
    *LEDGadget = GetGadgetData(Gadget)
    With *LEDGadget
      If State
        \ActualColor = \OnColor
      Else
        \ActualColor = \OffColor
      EndIf
    EndWith
    LEDGadgetDraw(Gadget)
  EndIf
 
EndProcedure


Procedure LEDGadgetSetAttribute(Gadget, attribute, value)
  
  Protected *LEDGadget.LEDGadgetStructure
  
  
  If IsGadget(Gadget)
    *LEDGadget = GetGadgetData(Gadget)
    With *LEDGadget
      Select attribute
        Case #LEDGadget_OnColor : \OnColor = value
        Case #LEDGadget_OffColor : \OffColor = value
        Case #LEDGadget_BorderColor : \BorderColor = value
        Case #LEDGadget_BackColor : \BackColor = value
      EndSelect
    EndWith
    LEDGadgetDraw(Gadget)
  EndIf
  
EndProcedure



Procedure LEDGadget(Gadget, x, y, radius = 5, OnColor = $0000FF, OffColor = $7F7F7F, BorderColor = -1, BackColor = $F0F0F0, OnImg=-1, OffImg=-1)
  
  Protected Result.i, *LEDGadget.LEDGadgetStructure, Width.i, Height.i
  
 
  If Gadget = #PB_Any Or IsGadget(Gadget) = 0
    
    If OnImg > -1
      Width  = ImageWidth(OnImg)
      Height = ImageHeight(OnImg)
    Else
      Width = radius * 2 + 1
      Height = radius * 2 + 1
    EndIf
    
    If Gadget = #PB_Any
      Gadget = CanvasGadget(#PB_Any, x, y, Width, Height)
      Result = Gadget
    Else
      Result = CanvasGadget(Gadget, x, y, Width, Height)
    EndIf
    
    
    If Result
      *LEDGadget = AllocateMemory(SizeOf(LEDGadgetStructure))
      SetGadgetData(Gadget, *LEDGadget)
      
      With *LEDGadget
        If radius  = - 1
          \Radius = 5
        Else
          \Radius = radius
        EndIf
        If OnColor = - 1
          \OnColor = $0000FF
        Else
          \OnColor = OnColor
        EndIf
        If OffColor = - 1
          \OffColor = $7F7F7F
        Else
          \OffColor = OffColor
        EndIf
        If BorderColor <> 1
          \BorderColor = BorderColor
        EndIf
        If BackColor = - 1
          \BackColor = $F0F0F0
        Else
          \BackColor = BackColor
        EndIf
        
        \ActualColor = \OffColor
        \OnImg = OnImg
        \OffImg = OffImg
        
      EndWith
     
      LEDGadgetDraw(Gadget)
     
    EndIf
  EndIf
 
  ProcedureReturn Result
 
EndProcedure



CompilerIf #PB_Compiler_IsMainFile
  
Define.i Exit, Event
  
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)

CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, $ECE9D8)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 0, 0))
FrontColor(RGB(255, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()

CreateImage(1, 20, 20)
StartDrawing(ImageOutput(1))
Box(0, 0, 20, 20, $ECE9D8)
DrawingMode(#PB_2DDrawing_Gradient)
BackColor(RGB(128, 128, 128))
FrontColor(RGB(0, 0, 0))
CircularGradient(9, 9, 8)
Circle(9, 9, 9)
StopDrawing()

LEDGadget(2, 10, 40, 10, -1, -1, -1, -1, 0, 1)
ButtonGadget(3, 40, 40, 40, 20, "On", #PB_Button_Toggle)


Exit = #False
Repeat
  
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          If GetGadgetState(1)
            SetGadgetText(1, "Off")
            LEDGadgetSetState(0, #True)
          Else
            SetGadgetText(1, "On")
            LEDGadgetSetState(0, #False)
          EndIf
        Case 3
          If GetGadgetState(3)
            SetGadgetText(3, "Off")
            LEDGadgetSetState(2, #True)
          Else
            SetGadgetText(3, "On")
            LEDGadgetSetState(2, #False)
          EndIf
      EndSelect
    Case #PB_Event_CloseWindow
      Exit = #True
  EndSelect
  
Until Exit

CompilerEndIf