CanvasGadget for Custom Gadgets

Just starting out? Need help? Post your questions and find answers here.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

CanvasGadget for Custom Gadgets

Post 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?
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CanvasGadget for Custom Gadgets

Post 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
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: CanvasGadget for Custom Gadgets

Post 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. :?
freak
PureBasic Team
PureBasic Team
Posts: 5946
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: CanvasGadget for Custom Gadgets

Post 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 ;)
quidquid Latine dictum sit altum videtur
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CanvasGadget for Custom Gadgets

Post 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?
ElementE
Enthusiast
Enthusiast
Posts: 139
Joined: Sun Feb 22, 2015 2:33 am

Re: CanvasGadget for Custom Gadgets

Post 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?
Think Unicode!
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CanvasGadget for Custom Gadgets

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1316
Joined: Tue Oct 09, 2007 2:15 am

Re: CanvasGadget for Custom Gadgets

Post 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 ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CanvasGadget for Custom Gadgets

Post by User_Russian »

Bisonte wrote:Copy the background under the canvas
To do this in PB has a cross-platform function?
User avatar
Bisonte
Addict
Addict
Posts: 1316
Joined: Tue Oct 09, 2007 2:15 am

Re: CanvasGadget for Custom Gadgets

Post 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()
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CanvasGadget for Custom Gadgets

Post 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
User_Russian
Addict
Addict
Posts: 1580
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CanvasGadget for Custom Gadgets

Post 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
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CanvasGadget for Custom Gadgets

Post 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
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: CanvasGadget for Custom Gadgets

Post by falsam »

Hello. Another approach with version 4.40 of Purebasic.

Code: Select all

 code off topic  
Last edited by falsam on Tue Nov 10, 2015 1:21 pm, edited 1 time in total.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
infratec
Always Here
Always Here
Posts: 7658
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: CanvasGadget for Custom Gadgets

Post 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
Post Reply