Code: Select all
;-TOP
; Comment : Object ButtonColorGadget Number 42 ;)
; Author : mk-soft
; Version : v1.05
; Create : 01.05.2019
; Update : 29.07.2019 (DPI)
; OS : All
; Link BaseClass : https://www.purebasic.fr/english/viewtopic.php?f=12&t=64305
IncludeFile "Modul_BaseClassSmall.pb"
EnableExplicit
; *****************************************************************************
;- Public
DeclareModule ButtonColorGadget
UseModule BaseClass
Interface iButtonColorGadget Extends iBaseClass
Resize(x, y, Width, Height)
SetText(Text.s)
SetFont(FontID)
SetColor(ColorType, Color)
GetID()
GetText.s()
GetColor(ColorType)
EndInterface
UnuseModule BaseClass
Declare Create(Gadget, x, y, Width, Height, Text.s, FrontColor = $000000, BackColor = $F5F5F5, Flags = 0)
EndDeclareModule
;- Private
Module ButtonColorGadget
EnableExplicit
UseModule BaseClass
NewClass(iButtonColorGadget)
Structure sButtonColorGadget Extends sBaseClass
Gadget.i
; Params
x.i
y.i
Width.i
Height.i
Text.s
FontID.i
LineColor.i
FrontColor.i
BackColor.i
Flags.i
; Data
Redraw.i
State.i
EndStructure
; ----
Procedure.i BlendColor(Color1.i, Color2.i, Scale.i = 50) ; Thanks to Thorsten
Protected.i R1, G1, B1, R2, G2, B2
Protected.f Blend = Scale / 100
R1 = Red(Color1): G1 = Green(Color1): B1 = Blue(Color1)
R2 = Red(Color2): G2 = Green(Color2): B2 = Blue(Color2)
ProcedureReturn RGB((R1*Blend) + (R2 * (1 - Blend)), (G1*Blend) + (G2 * (1 - Blend)), (B1*Blend) + (B2 * (1 - Blend)))
EndProcedure
; ----
Procedure DrawButton(*this.sButtonColorGadget)
Protected LineColor, FrontColor, BackColor
Protected Width, Height
With *this
Select \State
Case #Null
LineColor = \LineColor
FrontColor = \FrontColor
BackColor = \BackColor
Case #PB_EventType_MouseEnter
LineColor = \LineColor
FrontColor = BlendColor(\FrontColor, $FFFFFF, 80)
BackColor = BlendColor(\BackColor, $FFFFFF, 80)
Case #PB_EventType_MouseLeave
LineColor = \LineColor
FrontColor = \FrontColor
BackColor = \BackColor
Case #PB_EventType_LeftButtonDown
LineColor = \LineColor
FrontColor = BlendColor(\FrontColor, $000000, 80)
BackColor = BlendColor(\BackColor, $000000, 80)
Case #PB_EventType_LeftButtonUp
LineColor = \LineColor
FrontColor = BlendColor(\FrontColor, $FFFFFF, 80)
BackColor = BlendColor(\BackColor, $FFFFFF, 80)
EndSelect
Width = DesktopScaledX(\Width)
Height = DesktopScaledY(\Height)
If StartDrawing(CanvasOutput(\Gadget))
Box(0, 0, Width, Height, LineColor)
Box(1, 1, Width - 2, Height - 2, BackColor)
DrawingFont(\FontID)
DrawText(Width / 2 - TextWidth(\Text) / 2, Height / 2 - TextHeight(\Text) / 2, \Text, FrontColor, BackColor)
StopDrawing()
EndIf
EndWith
EndProcedure
; ----
Procedure DoEvents()
Protected *this.sButtonColorGadget = GetGadgetData(EventGadget())
Protected state, redraw
With *this
If *this
state = EventType()
Select state
Case #PB_EventType_MouseEnter : redraw = #True
Case #PB_EventType_MouseLeave : redraw = #True
Case #PB_EventType_MouseMove
Case #PB_EventType_MouseWheel
Case #PB_EventType_LeftButtonDown : redraw = #True
Case #PB_EventType_LeftButtonUp : redraw = #True
Case #PB_EventType_LeftClick
Case #PB_EventType_LeftDoubleClick
Case #PB_EventType_RightButtonDown
Case #PB_EventType_RightButtonUp
Case #PB_EventType_RightClick
Case #PB_EventType_RightDoubleClick
Case #PB_EventType_MiddleButtonDown
Case #PB_EventType_MiddleButtonUp
Case #PB_EventType_Focus
Case #PB_EventType_LostFocus
Case #PB_EventType_KeyDown
Case #PB_EventType_KeyUp
Case #PB_EventType_Input
Case #PB_EventType_Resize
EndSelect
If redraw
\State = state
DrawButton(*this)
EndIf
EndIf
EndWith
EndProcedure
; ----
Procedure SetText(*this.sButtonColorGadget, Text.s)
With *this
\Text = Text
DrawButton(*this)
EndWith
EndProcedure : AsMethode(SetText)
; ----
Procedure SetFont(*this.sButtonColorGadget, FontID)
With *this
\FontID = FontID
DrawButton(*this)
EndWith
EndProcedure : AsMethode(SetFont)
; ----
Procedure SetColor(*this.sButtonColorGadget, ColorType, Color)
With *this
Select ColorType
Case #PB_Gadget_FrontColor
\FrontColor = Color
Case #PB_Gadget_BackColor
\BackColor = Color
Case #PB_Gadget_LineColor
\LineColor = Color
EndSelect
DrawButton(*this)
EndWith
EndProcedure : AsMethode(SetColor)
; ----
Procedure Resize(*this.sButtonColorGadget, x, y, Width, Height)
With *this
ResizeGadget(\Gadget, x, y, Width, Height)
If x <> #PB_Ignore
\x = x
EndIf
If y <> #PB_Ignore
\y = y
EndIf
If Width <> #PB_Ignore
\Width = Width
EndIf
If Height <> #PB_Ignore
\Height = Height
EndIf
DrawButton(*this)
EndWith
EndProcedure : AsMethode(Resize)
; ----
Procedure GetID(*this.sButtonColorGadget)
ProcedureReturn *this\Gadget
EndProcedure : AsMethode(GetID)
; ----
Procedure.s GetText(*this.sButtonColorGadget)
ProcedureReturn *this\Text
EndProcedure : AsMethode(GetText)
; ----
Procedure GetColor(*this.sButtonColorGadget, ColorType)
Protected color
With *this
Select ColorType
Case #PB_Gadget_FrontColor
color = \FrontColor
Case #PB_Gadget_BackColor
color = \BackColor
Case #PB_Gadget_LineColor
color = \LineColor
EndSelect
ProcedureReturn color
EndWith
EndProcedure : AsMethode(GetColor)
; ----
Procedure Initialize(*this.sButtonColorGadget)
Protected result
With *this
result = CanvasGadget(\Gadget, \x, \y, \Width, \Height, \Flags)
If result
If \Gadget = #PB_Any
\Gadget = result
EndIf
\FontID = #PB_Default
\LineColor = #Gray
DrawButton(*this)
SetGadgetData(\Gadget, *this)
BindGadgetEvent(\Gadget, @DoEvents())
EndIf
EndWith
EndProcedure : AsInitializeObject(Initialize)
; ----
Procedure Dispose(*this.sButtonColorGadget)
With *this
If IsGadget(\Gadget)
FreeGadget(\Gadget)
EndIf
EndWith
EndProcedure : AsDisposeObject(Dispose)
; ----
Procedure Create(Gadget, x, y, Width, Height, Text.s, FrontColor = $000000, BackColor = $F5F5F5, Flags = 0)
Protected *object.sButtonColorGadget
With *object
AllocateObject(*object, sButtonColorGadget)
If *object
\Gadget = Gadget
\x = x
\y = y
\Width = Width
\Height = Height
\Text = Text
\FrontColor = FrontColor
\BackColor = BackColor
\Flags = Flags
EndIf
InitializeObject(*object)
ProcedureReturn *object
EndWith
EndProcedure
; ----
CheckInterface()
EndModule
; *****************************************************************************
;- Example
CompilerIf #PB_Compiler_IsMainFile
Enumeration Windows
#Main
EndEnumeration
Enumeration Gadgets
#Button1
#Button2
#Button3
EndEnumeration
Enumeration Status
#MainStatusBar
EndEnumeration
LoadFont(0, "Courier New", 20, #PB_Font_Bold)
Procedure Main()
; Define button object
Protected.ButtonColorGadget::iButtonColorGadget btn, btn2, btn3
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 480, 320, "Object ButtonColorGadget Number 42 ;)", #PB_Window_SystemMenu)
btn = ButtonColorGadget::Create(#Button1, 10, 10, 120, 30, "My Button", #Yellow, #Red)
btn2 = ButtonColorGadget::Create(#Button2, 10, 80, 120, 30, "My Button 2")
btn3 = ButtonColorGadget::Create(#Button3, 10, 120, 120, 30, "My Button 3")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case #Button1
If EventType() = #PB_EventType_LeftClick
If GadgetWidth(#Button1) <= 120
btn\Resize(#PB_Ignore, #PB_Ignore, 240, 60)
btn\SetText("My Big Button")
btn\SetFont(FontID(0))
btn\SetColor(#PB_Gadget_BackColor, #Green)
btn\SetColor(#PB_Gadget_FrontColor, #Black)
btn\SetColor(#PB_Gadget_LineColor, #Red)
Else
btn\Resize(#PB_Ignore, #PB_Ignore, 120, 30)
btn\SetText("My Button")
btn\SetFont(#PB_Default)
btn\SetColor(#PB_Gadget_BackColor, #Red)
btn\SetColor(#PB_Gadget_FrontColor, #Yellow)
btn\SetColor(#PB_Gadget_LineColor, #Gray)
EndIf
EndIf
EndSelect
EndSelect
ForEver
btn\Release()
EndIf
EndProcedure : Main()
CompilerEndIf