Custom buttons using images on a CanvasGadget
Posted: Sat Jul 11, 2015 6:02 pm
Custom buttons using images on a CanvasGadget
Hello.
I'm creating custom buttons using images. So monent no problem, but I would like to optimize the code or find a more efficient way.
This is my code:
There are three states; Normal, highlighted and pressed.
Thanks in advance.
Hello.
I'm creating custom buttons using images. So monent no problem, but I would like to optimize the code or find a more efficient way.
This is my code:
Code: Select all
EnableExplicit
#Window = 0
#ImgBtn1=1
#ImgBtn2=2
#ImgBtn3=3
UsePNGImageDecoder()
LoadImage(#ImgBtn1, "button\button_image_n_22.png") ; Normal
LoadImage(#ImgBtn2, "button\button_image_h_26.png") ; Highlight
LoadImage(#ImgBtn3, "button\button_image_p_24.png") ; Pressed
Define myGadgetHeight=22, myGadgetWidth=70
Define colorNormal = RGB($00, $00, $00) ; Color normal del botón
Define colorHighlight = RGB($FF, $FF, $FF) ; Color del botón resaltado
Define Event, text$, xpos=10, ypos=10
Define offsetX, offsetY
If OpenWindow(#Window, 0, 0, 320, 240, "Canvas Buttons Images",
#PB_Window_ScreenCentered | #PB_Window_SystemMenu)
;------- Boton 1 -------
CopyImage(#ImgBtn1, 11)
If StartDrawing(ImageOutput(11))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 1"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorNormal) ; Normal
StopDrawing()
EndIf
CopyImage(#ImgBtn2, 12)
If StartDrawing(ImageOutput(12))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 1"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Highlight
StopDrawing()
EndIf
CopyImage(#ImgBtn3, 13)
If StartDrawing(ImageOutput(13))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 1"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Pressed
StopDrawing()
EndIf
CanvasGadget(1, xpos, ypos, myGadgetWidth, myGadgetHeight)
SetGadgetAttribute (1, #PB_Canvas_Image, ImageID(11))
;------- Boton 2 -------
CopyImage(#ImgBtn1, 21)
If StartDrawing(ImageOutput(21))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 2"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorNormal) ; Normal
StopDrawing()
EndIf
CopyImage(#ImgBtn2, 22)
If StartDrawing(ImageOutput(22))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 2"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Highlight
StopDrawing()
EndIf
CopyImage(#ImgBtn3, 23)
If StartDrawing(ImageOutput(23))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 2"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Pressed
StopDrawing()
EndIf
CanvasGadget(2, xpos+80, ypos, myGadgetWidth, myGadgetHeight)
SetGadgetAttribute (2, #PB_Canvas_Image, ImageID(21))
;------- Boton 3 -------
CopyImage(#ImgBtn1, 31)
If StartDrawing(ImageOutput(31))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 3"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorNormal) ; Normal
StopDrawing()
EndIf
CopyImage(#ImgBtn2, 32)
If StartDrawing(ImageOutput(32))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 3"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Highlight
StopDrawing()
EndIf
CopyImage(#ImgBtn3, 33)
If StartDrawing(ImageOutput(33))
DrawingMode(#PB_2DDrawing_Transparent)
text$="Boton 3"
offsetX=(myGadgetWidth - TextWidth(text$))/2
offsetY=(myGadgetHeight - TextHeight(text$))/2
DrawText(offsetX, offsetY, text$, colorHighlight) ; Pressed
StopDrawing()
EndIf
CanvasGadget(3, xpos+160, ypos, myGadgetWidth, myGadgetHeight)
SetGadgetAttribute (3, #PB_Canvas_Image, ImageID(31))
EndIf
; --------------------------------------------------------------
Repeat
Event = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventType()
Case #PB_EventType_MouseLeave ; Normal
Select EventGadget()
Case 1 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(11))
Case 2 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(21))
Case 3 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(31))
EndSelect
Case #PB_EventType_MouseEnter ; Highlight
Select EventGadget()
Case 1 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(12))
Case 2 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(22))
Case 3 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(32))
EndSelect
Case #PB_EventType_LeftButtonDown ; Pressed
Select EventGadget()
Case 1 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(13))
Case 2 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(23))
Case 3 : SetGadgetAttribute (EventGadget(), #PB_Canvas_Image, ImageID(33))
EndSelect
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
End
Thanks in advance.