Page 1 of 2

CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 2:09 pm
by coder14
Is the canvas gadget a too much to be used as custom gadgets? I am trying to make my own gadgets and the canvas gadget is the only one that responds to keyboard and mouse events. In my program I use up to 30 or 40 gadgets in one window. Is that too many canvas gadgets in one window? Will it use too much memory when the program is running compared to other gadgets like buttons and text? Will doing this slow the program down more? Buttons, texts and image gadgets take up less memory right?

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 3:41 pm
by infratec
Hi,

it uses more RAM and codesize.
But it has nothing todo with speed.

PB programming is event driven, no event, no time is used.
If a gadget is not active it sends no events.

Bernd

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 5:19 pm
by coder14
infratec wrote:Hi,

it uses more RAM and codesize.
But it has nothing todo with speed.

PB programming is event driven, no event, no time is used.
If a gadget is not active it sends no events.

Bernd
Thanks Bernd. That is right. I checked the exe file size and the running memory. The file size is double when canvas gadget is used instead of button.

Why does the program use more ram when the canvas gadget is bigger but not when the button is bigger? I made 2 codes: 1 with just window and button and 1 with window and canvas. Making the button bigger does not increase the ram use but making the canvas bigger increases the ram use. :?

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 6:34 pm
by freak
The main use for CanvasGadget is to create custom gadgets. That is what it is there for.

About the memory use: Every CanvasGadget has an image that stores its content for double-buffering. This image must grow when the gadget grows. A button does not have that. Still, this memory use should be no problem on any kind of recent computer ;)

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 6:58 pm
by User_Russian
freak wrote:The main use for CanvasGadget is to create custom gadgets.
Without the support of transparency is hard to do. For example how to make a round button?

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 7:15 pm
by ElementE
User_Russian wrote:
freak wrote:The main use for CanvasGadget is to create custom gadgets.
Without the support of transparency is hard to do. For example how to make a round button?
infratec has posted code to make round LEDGadgets.

LEDGadget() (the round ones)
http://www.purebasic.fr/english/viewtop ... 12&t=59084

Can transparency be added to it?

Re: CanvasGadget for Custom Gadgets

Posted: Mon Nov 09, 2015 11:24 pm
by User_Russian
ElementE wrote:the round ones
Are you kidding? :D :D
The gadget is not transparent and square.

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)
SetWindowColor(0, RGB(0, 255, 0))
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

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 9:25 am
by Bisonte
User_Russian wrote:
freak wrote:The main use for CanvasGadget is to create custom gadgets.
Without the support of transparency is hard to do. For example how to make a round button?
Simulate transparency ! Copy the background under the canvas and paint it on, or use as backgroundcolor the window color.... ! Nothing magic ;)

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 9:53 am
by User_Russian
Bisonte wrote:Copy the background under the canvas
To do this in PB has a cross-platform function?

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 10:43 am
by Bisonte
User_Russian wrote:
Bisonte wrote:Copy the background under the canvas
To do this in PB has a cross-platform function?
Ähem... Yes :

Code: Select all

StartDrawing(WindowOutput(#Window))
Image = GrabDrawingImage(#PB_Any, x, y, w, h)
StopDrawing()

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 11:11 am
by infratec
Modified 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 = -1, 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
          Debug y
          StartDrawing(WindowOutput(GetActiveWindow()))
          \BackColor = Point(x + (Width >> 1), y + (Height >> 1))
          StopDrawing()
        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, BackgroundColor
 
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)

StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()

CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
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, BackgroundColor)
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
If no image is used, now the color of the window is used as backgroundcolor.
Should be crossplatform.

Bernd

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 11:30 am
by User_Russian

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 = -1, 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
          Debug y
          StartDrawing(WindowOutput(GetActiveWindow()))
          \BackColor = Point(x + (Width >> 1), y + (Height >> 1))
          StopDrawing()
        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, BackgroundColor
 
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))

ContainerGadget(100, 0, 0, 100, 90)
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)

StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()

CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
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, BackgroundColor)
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)
CloseGadgetList()

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

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 = -1, 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
          Debug y
          StartDrawing(WindowOutput(GetActiveWindow()))
          \BackColor = Point(x + (Width >> 1), y + (Height >> 1))
          StopDrawing()
        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, BackgroundColor
 
OpenWindow(0, 0, 0, 100, 90, "LED Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
SetWindowColor(0, RGB(0, 255, 0))

PanelGadget(100, 0, 0, 100, 90)
AddGadgetItem(100, -1, "Panel")
LEDGadget(0, 10, 10, 10)
ButtonGadget(1, 40, 10, 40, 20, "On", #PB_Button_Toggle)

StartDrawing(WindowOutput(0))
BackgroundColor = Point(19, 10)
StopDrawing()

CreateImage(0, 20, 20)
StartDrawing(ImageOutput(0))
Box(0, 0, 20, 20, BackgroundColor)
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, BackgroundColor)
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)
CloseGadgetList()

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

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 11:34 am
by infratec
Hm....

just tested with Linux: not working :cry:

Because Point() doesn't return the right color.
And also SetWindowColor() has a border arround.

Bernd

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 11:57 am
by falsam
Hello. Another approach with version 4.40 of Purebasic.

Code: Select all

 code off topic  

Re: CanvasGadget for Custom Gadgets

Posted: Tue Nov 10, 2015 12:57 pm
by infratec
@falsam

your approach has nothing todo with a gadget.
At least if you want to place it in a panel or a container it fails.

Bernd