SliderGadget

Share your advanced PureBasic knowledge/code with the community.
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

SliderGadget

Post by HeX0R »

A small by-product.
Unfortunately I have ten thumbs when we talk about graphic qualities, I'm pretty sure someone can improve it.

Code: Select all

;/---------------------------
;|
;| SliderGadget.pbi
;|      V1.03
;|      01.10.2023
;|
;| Builts a type of slider on top of a ButtonImageGadget
;| Don't use #PB_Button_Toggle, use SLIDER::Get/SetState instead
;|
;| (c)HeX0R
;|
;|
;|
;\---------------------------

; ----------------------------------------------------------------------------
; "THE BEER-WARE LICENSE":
; <HeX0R@coderbu.de> wrote this file. as long as you retain this notice you
; can do whatever you want with this stuff. If we meet some day, and you think
; this stuff is worth it, you can buy me a beer in return
; (see address on https://hex0rs.coderbu.de/).
; Or send money for a beer via =>
; https://www.paypal.com/paypalme/hex0r
; Or just go out and drink a few on your own/with your friends ;)
;=============================================================================

DeclareModule SLIDER
	
	Structure _SLIDERTHEME_
		ONFrameColor1.i
		ONFrameColor2.i
		ONMainColor1.i
		ONMainColor2.i
		ONBorderColor1.i
		ONBorderColor2.i
		ONFontColor.i
		OFFFrameColor1.i
		OFFFrameColor2.i
		OFFMainColor1.i
		OFFMainColor2.i
		OFFBorderColor1.i
		OFFBorderColor2.i
		OFFFontColor.i
		SLIDERMainColor1.i
		SLIDERMainColor2.i
		SLIDERKnobColor1.i
		SLIDERKnobColor2.i
		FontName.s
		TextOn$
		TextOff$
	EndStructure
	
	Declare GetState(Gadget)
	;Get state,
	;  #True  = "on"
	;  #False = "off"
	; -1      = this is no slider
	Declare SetState(Gadget, State)
	;Set state,
	;  result of -1 means this Gadget is no slider
	Declare SetToolTips(Gadget, ToolTipOn$, ToolTipOff$)
	;Set tooltips for the two different states
	Declare Create(Window, Gadget, CustomEvent, *CallBack = #False, State = #False, Swapped = #False, *THEME._SLIDERTHEME_ = 0)
	;Create the Slider
	; Window        = parent window of the Gadget (we need that to send events)
	; Gadget        = EXISTING ButtonImageGadget()
	;                 I went that way to be able to use it easily for dialogs also.
	;                 Just create a dialog with a buttonimagegadget and no image (= 0)
	; CustomEvent   = A custom event (created with the help of #PB_Event_FirstCustomValue) for internal things.
	; *CallBack     = An optional Callback, which will be called right after a click (because we are using our own bind event
	; State         = the initial state of the slider, #False = off, #True = on
	; Swapped       = slider built-up in inverse logic
	; *THEME        = change the look and feel of the slider with this
	Declare Remove(Gadget = #PB_All)
	;Remove the resources of a slider gadget, or all
	;Make sure, that it still exists, when calling Remove()!
	
	
EndDeclareModule


Module SLIDER
	EnableExplicit
	
	Prototype CustomEvent()
	
	Structure _SLIDERGADGET_
		Ident.i
		Window.i
		Gadget.i
		ThreadID.i
		CustomEvent.i
		GadgetWidth.i
		GadgetHeight.i
		Image.i
		ImageON.i
		ImageOff.i
		ImageSlider.i
		Font.i
		EndPos.i
		SliderWidth.i
		SliderHeight.i
		State.i
		IsVertical.i
		IsSwapped.i
		LongSide.i
		ShortSide.i
		Moving.i
		Mutex.i
		ToolTipOn$
		ToolTipOff$
		THEME._SLIDERTHEME_
		CallBack.CustomEvent
	EndStructure
	
	Enumeration
		#MOVE_LEFT
		#MOVE_RIGHT
		#MOVE_UP
		#MOVE_DOWN
	EndEnumeration
	
	#Ident = $0Cfa123b
	
	Global NewList *SLIDERS._SLIDERGADGET_()
	
	Procedure _SetDefaultTheme(*SLIDER._SLIDERGADGET_)
		
		With *SLIDER\THEME
			\ONFrameColor1    = $FF9C9C9C
			\ONFrameColor2    = $FFFDFDFD
			\ONBorderColor1   = $FF10901C
			\ONBorderColor2   = $FF1D7E29
			\ONMainColor1     = $FF009016
			\ONMainColor2     = $FF00D621
			\ONFontColor      = $FFFFFFFF
			\OFFFrameColor1   = $FF9C9C9C
			\OFFFrameColor2   = $FFFDFDFD
			\OFFBorderColor1  = $FF423F9A
			\OFFBorderColor2  = $FF6260A8
			\OFFMainColor1    = $FF0E0E7B
			\OFFMainColor2    = $FF0103DB
			\OFFFontColor     = $FFFFFFFF
			\SLIDERMainColor1 = $FFF7F7F7
			\SLIDERMainColor2 = $FFCFD4D0
			\SLIDERKnobColor1 = $FFD2D2D2
			\SLIDERKnobColor2 = $FFF8F8F8
			\FontName         = "Arial"
			\TextOff$         = "OFF"
			\TextOn$          = "ON"
		EndWith
	EndProcedure
	
	Procedure _Thread(*SLIDER._SLIDERGADGET_) ;moving slider thread
		Protected Move, Image1, Image2
		
		With *SLIDER
			Repeat
				LockMutex(\Mutex)
				\Moving + \LongSide / 25
				If \Moving + \SliderWidth + \EndPos * 2 >= \LongSide
					;endpicture reached
					\Moving = 0
					If StartDrawing(ImageOutput(\Image))
						If \State
							DrawAlphaImage(ImageID(\ImageOn), 0, 0)
						Else
							DrawAlphaImage(ImageID(\ImageOff), 0, 0)
						EndIf
						If (\State And \IsVertical And \IsSwapped) Or (\State = 0 And \IsVertical And \IsSwapped = 0)
							DrawAlphaImage(ImageID(\ImageSlider), \GadgetWidth / 2 - \SliderHeight / 2, \EndPos)
						ElseIf (\State And \IsVertical And \IsSwapped = 0) Or (\State = 0 And \IsVertical And \IsSwapped)
							DrawAlphaImage(ImageID(\ImageSlider), (\GadgetWidth / 2) - (\SliderHeight / 2), 1 + \GadgetHeight - \SliderWidth - \EndPos)
						ElseIf (\State And \IsVertical = 0 And \IsSwapped) Or (\State = 0 And \IsVertical = 0 And \IsSwapped = 0)
							DrawAlphaImage(ImageID(\ImageSlider), 1 + \GadgetWidth - \SliderWidth - \EndPos, \EndPos)
						Else
							DrawAlphaImage(ImageID(\ImageSlider), \EndPos, \EndPos)
						EndIf
						StopDrawing()
					EndIf
					UnlockMutex(\Mutex)
					Break
				EndIf
				;moving slider
				
				Image1 = ImageID(\ImageOn)
				Image2 = ImageID(\ImageOff)
				If \State = 0
					Swap Image1, Image2
				EndIf
				
				Move = #MOVE_LEFT
				If (\State And \IsVertical And \IsSwapped) Or (\State = 0 And \IsVertical And \IsSwapped = 0)
					Move = #MOVE_UP
				ElseIf (\State And \IsVertical And \IsSwapped = 0) Or (\State = 0 And \IsVertical And \IsSwapped)
					Move = #MOVE_DOWN
				ElseIf (\State And \IsVertical = 0 And \IsSwapped) Or (\State = 0 And \IsVertical = 0 And \IsSwapped = 0)
					Move = #MOVE_RIGHT
				EndIf
				
				If StartDrawing(ImageOutput(\Image))
					Select Move
						Case #MOVE_DOWN
							ClipOutput(0, 0, \GadgetWidth, \SliderWidth + (\Moving / 2))
							DrawAlphaImage(Image1, 0, 0)
							ClipOutput(0, \SliderWidth + (\Moving / 2), \GadgetWidth, \GadgetHeight)
							DrawAlphaImage(Image2, 0, 0)
							UnclipOutput()
							DrawAlphaImage(ImageID(\ImageSlider), (\GadgetWidth / 2) - (\SliderHeight / 2), \Moving)
						Case #MOVE_LEFT
							ClipOutput(0, 0, \GadgetWidth - \SliderWidth - \EndPos - (\Moving / 2), \GadgetHeight)
							DrawAlphaImage(Image2, 0, 0)
							ClipOutput(\GadgetWidth - \SliderWidth - \EndPos - (\Moving / 2), 0, \GadgetWidth, \GadgetHeight)
							DrawAlphaImage(Image1, 0, 0)
							UnclipOutput()
							DrawAlphaImage(ImageID(\ImageSlider), \GadgetWidth - \SliderWidth - \EndPos - \Moving, \EndPos)
						Case #MOVE_UP
							ClipOutput(0, 0, \GadgetWidth, \GadgetHeight - \SliderWidth - (\Moving / 2))
							DrawAlphaImage(Image2, 0, 0)
							ClipOutput(0, \GadgetHeight - \SliderWidth - (\Moving / 2), \GadgetWidth, \GadgetHeight)
							DrawAlphaImage(Image1, 0, 0)
							UnclipOutput()
							DrawAlphaImage(ImageID(\ImageSlider), (\GadgetWidth / 2) - (\SliderHeight / 2), \GadgetHeight - \SliderWidth - \Moving)
						Case #MOVE_RIGHT
							ClipOutput(0, 0, \EndPos + \Moving + (\SliderWidth / 2), \GadgetHeight)
							DrawAlphaImage(Image1, 0, 0)
							ClipOutput(\EndPos + \Moving + (\SliderWidth / 2), 0, \GadgetWidth, \GadgetHeight)
							DrawAlphaImage(Image2, 0, 0)
							UnclipOutput()
							DrawAlphaImage(ImageID(\ImageSlider), \EndPos + 1 + \Moving, \EndPos)
					EndSelect
					StopDrawing()
					PostEvent(\CustomEvent, \Window, \Gadget, 0, *SLIDER)
				EndIf
				UnlockMutex(\Mutex)
				Delay(10)
			ForEver
			PostEvent(\CustomEvent, \Window, \Gadget, 0, *SLIDER)
		EndWith
	EndProcedure
	
	Procedure _MyCustomEvent()
		Protected *SLIDER._SLIDERGADGET_
		
		*SLIDER = EventData()
		If *SLIDER <> 0 And *SLIDER\Ident = #Ident
			LockMutex(*SLIDER\Mutex)
			SetGadgetAttribute(*SLIDER\Gadget, #PB_Button_Image, ImageID(*SLIDER\Image))
			If *SLIDER\Moving = 0 And (*SLIDER\ToolTipOn$ Or *SLIDER\ToolTipOff$)
				If *SLIDER\State
					GadgetToolTip(*SLIDER\Gadget, *SLIDER\ToolTipOn$)
				Else
					GadgetToolTip(*SLIDER\Gadget, *SLIDER\ToolTipOff$)
				EndIf
			EndIf
			UnlockMutex(*SLIDER\Mutex)
		EndIf
	EndProcedure
	
	Procedure GetState(Gadget)
		Protected *SLIDER._SLIDERGADGET_
		
		*SLIDER = GetGadgetData(Gadget)
		If *SLIDER = 0 Or *SLIDER\Ident <> #Ident
			ProcedureReturn - 1
		EndIf
		ProcedureReturn *SLIDER\State
	EndProcedure
	
	Procedure _Click()
		Protected *SLIDER._SLIDERGADGET_
		
		*SLIDER = GetGadgetData(EventGadget())
		If *SLIDER = 0 Or *SLIDER\Ident <> #Ident
			ProcedureReturn
		EndIf
		If IsThread(*SLIDER\ThreadID)
			WaitThread(*SLIDER\ThreadID)
		EndIf
		*SLIDER\State    = *SLIDER\State ! 1
		*SLIDER\ThreadID = CreateThread(@_Thread(), *SLIDER)
		If *SLIDER\CallBack
			*SLIDER\CallBack()
		EndIf
		
	EndProcedure
	
	Procedure SetState(Gadget, State)
		Protected *SLIDER._SLIDERGADGET_
		
		*SLIDER = GetGadgetData(Gadget)
		If *SLIDER = 0 Or *SLIDER\Ident <> #Ident
			ProcedureReturn - 1
		EndIf
		If *SLIDER\State <> State
			*SLIDER\State    = State & 1
			*SLIDER\ThreadID = CreateThread(@_Thread(), *SLIDER)
		EndIf
	EndProcedure
	
	Procedure _Remove(*SLIDER._SLIDERGADGET_)
		If *SLIDER = 0 Or *SLIDER\Ident <> #Ident
			ProcedureReturn
		EndIf
		
		If IsThread(*SLIDER\ThreadID)
			WaitThread(*SLIDER\ThreadID)
		EndIf
		FreeImage(*SLIDER\Image)
		FreeImage(*SLIDER\ImageOff)
		FreeImage(*SLIDER\ImageON)
		FreeImage(*SLIDER\ImageSlider)
		FreeMutex(*SLIDER\Mutex)
		UnbindGadgetEvent(*SLIDER\Gadget, @_Click())
		If *SLIDER\Font <> -1
			FreeFont(*SLIDER\Font)
		EndIf
		SetGadgetData(*SLIDER\Gadget, 0)
		FreeStructure(*SLIDER)
	EndProcedure
	
	Procedure Remove(Gadget = #PB_All)
		If Gadget = #PB_All
			ForEach *SLIDERS()
				_Remove(*SLIDERS())
			Next
			ClearList(*SLIDERS())
		Else
			ForEach *SLIDERS()
				If *SLIDERS()\Gadget = Gadget
					_Remove(*SLIDERS())
					DeleteElement(*SLIDERS())
					Break
				EndIf
			Next
		EndIf
	EndProcedure
	
	Procedure SetToolTips(Gadget, ToolTipOn$, ToolTipOff$)
		Protected *SLIDER._SLIDERGADGET_
		
		*SLIDER = GetGadgetData(Gadget)
		If *SLIDER = 0 Or *SLIDER\Ident <> #Ident
			ProcedureReturn - 1
		EndIf
		*SLIDER\ToolTipOn$  = ToolTipOn$
		*SLIDER\ToolTipOff$ = ToolTipOff$
		If *SLIDER\State
			GadgetToolTip(Gadget, ToolTipOn$)
		Else
			GadgetToolTip(Gadget, ToolTipOff$)
		EndIf
	EndProcedure
	
	Procedure Create(Window, Gadget, CustomEvent, *CallBack = #False, CurrentState = #False, Swapped = #False, *THEME._SLIDERTHEME_ = 0)
		Protected *SLIDER._SLIDERGADGET_, th, tw, Width, Height, hf.f, wf.f
		Protected FontSize, MaxSize
		;creates Slider on an existing ImageButtonGadget
		
		*SLIDER = AllocateStructure(_SLIDERGADGET_)
		With *SLIDER
			\Ident        = #Ident
			\Window       = Window
			\Gadget       = Gadget
			\CustomEvent  = CustomEvent
			\CallBack     = *CallBack
			\IsSwapped    = Swapped
			Width         = DesktopScaledX(GadgetWidth(Gadget))
			Height        = DesktopScaledY(GadgetHeight(Gadget))
			\GadgetWidth  = Width
			\GadgetHeight = Height
			If Height > Width
				\IsVertical = #True
				\LongSide   = Height
				\ShortSide  = Width
			Else
				\LongSide  = Width
				\ShortSide = Height
			EndIf
			If *THEME = 0
				_SetDefaultTheme(*SLIDER)
			Else
				CopyStructure(*THEME, *SLIDER\THEME, _SLIDERTHEME_)
			EndIf
			\EndPos       = \LongSide / 25
			\SliderWidth  = 1 + \LongSide / 2 - \EndPos
			\SliderHeight = 1 + \ShortSide - 2 * \EndPos
			\State        = CurrentState & 1
			\Moving       = 0
			\Mutex        = CreateMutex()
			\ImageON      = CreateImage(#PB_Any, Width, Height, 32, #PB_Image_Transparent)
			If \ImageON
				If StartDrawing(ImageOutput(\ImageON))
					DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AllChannels)
				;grey frame
					BackColor(\THEME\ONFrameColor1)
					FrontColor(\THEME\ONFrameColor2)
					hf = \LongSide / 13.3
					If \IsVertical
						LinearGradient(0, hf, Width, Height - hf)
					Else
						LinearGradient(hf, 0, Width - hf, Height)
					EndIf
					RoundBox(0, 0, Width - 1, Height - 1, \ShortSide / 2, \ShortSide / 2)
				;green inlet
					BackColor(\THEME\ONBorderColor1)
					FrontColor(\THEME\ONBorderColor2)
					hf = \LongSide / 25
					If hf > \ShortSide / 10
						hf = \ShortSide / 10
					EndIf
					If \IsVertical
						LinearGradient(0, Height / 2, Width - 1, Height / 2)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						BackColor(\THEME\ONMainColor1)
						FrontColor(\THEME\ONMainColor2)
						\EndPos       = hf
						\SliderHeight = 1 + Width - 2 * hf
						hf + 2
						LinearGradient(hf, Height / 2, Width - 2 * hf, Height / 2)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						MaxSize = Width - 2 * hf
					Else
						LinearGradient(Width / 2, 0, Width / 2, Height - 1)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						BackColor(\THEME\ONMainColor1)
						FrontColor(\THEME\ONMainColor2)
						\EndPos       = hf
						\SliderHeight = 1 + Height - 2 * hf
						hf + 2
						LinearGradient(Width / 2, 2, Width / 2, Height - 3)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						MaxSize = Height - 2 * hf
					EndIf
					FontSize = \ShortSide / 3.63
					If \THEME\TextOn$ Or \THEME\TextOff$
						StopDrawing()
						Repeat
							;that looks awful, but somehow PB doesn't allow LoadFont() or FreeFont() inside a StartDrawing() loop?!
							\Font = LoadFont(#PB_Any, \THEME\FontName, FontSize)
							StartDrawing(ImageOutput(\ImageON))
							DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AllChannels)
							DrawingFont(FontID(\Font))
							If TextWidth(\THEME\TextOff$) > MaxSize Or TextWidth(\THEME\TextOn$) > MaxSize
								FontSize - 1
								StopDrawing()
								FreeFont(\Font)
							Else
								Break
							EndIf
						Until FontSize < 5
						If FontSize < 5
							StartDrawing(ImageOutput(\ImageON))
							DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AllChannels)
							DrawingFont(FontID(\Font))
						EndIf
					EndIf
					If \THEME\TextOn$
						;text
						th = TextHeight(\THEME\TextOn$)
						tw = TextWidth(\THEME\TextOn$)
						DrawingMode(#PB_2DDrawing_Transparent)
						If \IsSwapped
							If \IsVertical
								DrawText(Width / 2 - (tw / 2), Height - Height / 10 - th, \THEME\TextOn$, \THEME\ONFontColor)
							Else
								DrawText(Width / 10, Height / 2 - th / 2, \THEME\TextOn$, \THEME\ONFontColor)
							EndIf
						Else
							If \IsVertical
								DrawText(Width / 2 - (tw / 2), Height / 10, \THEME\TextOn$, \THEME\ONFontColor)
							Else
								DrawText(Width / 2 + Width / 10, Height / 2 - (th / 2), \THEME\TextOn$, \THEME\ONFontColor)
							EndIf
						EndIf
					EndIf
				EndIf
				StopDrawing()
			EndIf
			
			\ImageOff = CreateImage(#PB_Any, Width, Height, 32, #PB_Image_Transparent)
			If \ImageOff
				If StartDrawing(ImageOutput(\ImageOff))
					DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_AllChannels)
				;grey frame
					BackColor(\THEME\OffFrameColor1)
					FrontColor(\THEME\OffFrameColor2)
					hf = \LongSide / 25
					If hf > \ShortSide / 10
						hf = \ShortSide / 10
					EndIf
					If \IsVertical
						LinearGradient(0, hf, Width, Height - hf)
					Else
						LinearGradient(hf, 0, Width - hf, Height)
					EndIf
					RoundBox(0, 0, Width - 1, Height - 1, \ShortSide / 2, \ShortSide / 2)
				;red inlet
					BackColor(\THEME\OFFBorderColor1)
					FrontColor(\THEME\OFFBorderColor2)
					hf = \LongSide / 25
					If hf > \ShortSide / 10
						hf = \ShortSide / 10
					EndIf
					If \IsVertical
						LinearGradient(0, Height / 2, Width - 1, Height / 2)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						BackColor(\THEME\OFFMainColor1)
						FrontColor(\THEME\OFFMainColor2)
						\EndPos       = hf
						\SliderHeight = 1 + Width - 2 * hf
						hf + 2
						LinearGradient(hf, Height / 2, Width - 2 * hf, Height / 2)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
					Else
						LinearGradient(Width / 2, 0, Width / 2, Height - 1)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
						BackColor(\THEME\OFFMainColor1)
						FrontColor(\THEME\OFFMainColor2)
						\EndPos       = hf
						\SliderHeight = 1 + Height - 2 * hf
						hf + 2
						LinearGradient(Width / 2, 2, Width / 2, Height - 3)
						RoundBox(hf, hf, Width - 2 * hf, Height - 2 * hf, \ShortSide / 4, \ShortSide / 4)
					EndIf
					;text
					If \THEME\TextOff$
						DrawingFont(FontID(\Font))
						th = TextHeight(\THEME\TextOff$)
						tw = TextWidth(\THEME\TextOff$)
						DrawingMode(#PB_2DDrawing_Transparent)
						If \IsSwapped
							If \IsVertical
								DrawText(Width / 2 - (tw / 2), Height / 10, \THEME\TextOff$, \THEME\OFFFontColor)
							Else
								DrawText(Width / 2 + Width / 10, Height / 2 - (th / 2), \THEME\TextOff$, \THEME\OFFFontColor)
							EndIf
						Else
							If \IsVertical
								DrawText(Width / 2 - (tw / 2), Height - Height / 10 - th, \THEME\TextOff$, \THEME\OFFFontColor)
							Else
								DrawText(Width / 10, Height / 2 - th / 2, \THEME\TextOff$, \THEME\OFFFontColor)
							EndIf
						EndIf
					EndIf
					StopDrawing()
				EndIf
			EndIf
			
			If \Font = 0
				\Font = -1
			EndIf
			
			
			\ImageSlider = CreateImage(#PB_Any, \SliderWidth, \SliderWidth, 32, #PB_Image_Transparent)
			If \ImageSlider
				If StartDrawing(ImageOutput(\ImageSlider))
					DrawingMode(#PB_2DDrawing_Gradient | #PB_2DDrawing_Transparent | #PB_2DDrawing_AllChannels)
				;slider main
					BackColor(\THEME\SLIDERMainColor1)
					FrontColor(\THEME\SLIDERMainColor2)
					If \IsVertical
						LinearGradient(0, \SliderWidth / 9, \SliderHeight - 1, \SliderWidth - \SliderWidth / 9)
						RoundBox(0, 0, \SliderHeight - 1, \SliderWidth - 1, \ShortSide / 4, \ShortSide / 4)
					Else
						LinearGradient(\SliderWidth / 9, 0, \SliderWidth - \SliderWidth / 9, \SliderHeight - 1)
						RoundBox(0, 0, \SliderWidth - 1, \SliderHeight - 1, \ShortSide / 4, \ShortSide / 4)
					EndIf
				;slider knob
					BackColor(\THEME\SLIDERKnobColor1)
					FrontColor(\THEME\SLIDERKnobColor2)
					If \IsVertical
						LinearGradient(0, \SliderWidth / 2, \SliderHeight - 1, \SliderWidth / 2)
						Circle(\SliderHeight / 2, (\SliderWidth / 3) * 2, \SliderHeight / 3)
					Else
						LinearGradient(\SliderWidth / 2, 0, \SliderWidth / 2, \SliderHeight - 1)
						Circle((\SliderWidth / 3) * 2, \SliderHeight / 2, \SliderHeight / 3)
					EndIf
					StopDrawing()
				EndIf
			EndIf
			
			\Image = CreateImage(#PB_Any, Width, Height, 32, #PB_Image_Transparent)
			If StartDrawing(ImageOutput(\Image))
				If CurrentState
					DrawAlphaImage(ImageID(\ImageON), 0, 0)
				Else
					DrawAlphaImage(ImageID(\ImageOff), 0, 0)
				EndIf
				If (\State And \IsSwapped And \IsVertical) Or (\State = 0 And \IsSwapped = 0 And \IsVertical)
					DrawAlphaImage(ImageID(\ImageSlider), \GadgetWidth / 2 - \SliderHeight / 2, \EndPos)
				ElseIf (\State And \IsSwapped And \IsVertical = 0) Or (\State = 0 And \IsSwapped = 0 And \IsVertical = 0)
					DrawAlphaImage(ImageID(\ImageSlider), 1 + \GadgetWidth - \SliderWidth - \EndPos, \EndPos)
				ElseIf (\State And \IsSwapped = 0 And \IsVertical) Or (\State = 0 And \IsSwapped And \IsVertical)
					DrawAlphaImage(ImageID(\ImageSlider), (\GadgetWidth / 2) - (\SliderHeight / 2), 1 + \GadgetHeight - \SliderWidth - \EndPos)
				Else
					DrawAlphaImage(ImageID(\ImageSlider), \EndPos - 1, \EndPos)
				EndIf
				StopDrawing()
				SetGadgetAttribute(Gadget, #PB_Button_Image, ImageID(\Image))
			EndIf
			SetGadgetData(Gadget, *SLIDER)
			BindGadgetEvent(Gadget, @_Click())
			BindEvent(\CustomEvent, @_MyCustomEvent())
			AddElement(*SLIDERS())
			*SLIDERS() = *SLIDER
		EndWith
		
		ProcedureReturn *SLIDER
	EndProcedure
EndModule

CompilerIf #PB_Compiler_IsMainFile
	Enumeration #PB_Event_FirstCustomValue
		#EventSLIDER
	EndEnumeration
	
	Procedure MyCallBack()
		Debug "Gadget #" + Str(EventGadget()) + " changed state into: " + Str(SLIDER::GetState(EventGadget()))
	EndProcedure
	
	If OpenWindow(0, 0, 0, 200, 425, "Slider Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ButtonImageGadget(0, 0, 0, 200, 80, 0)
		SLIDER::Create(0, 0, #EventSLIDER, @MyCallBack())
		SLIDER::SetToolTips(0, "I am on now", "Now I'm off :(")
		ButtonImageGadget(1, 0, 90, 80, 30, 0)
		SLIDER::Create(0, 1, #EventSLIDER, @MyCallBack(), #False, #True)
		ButtonImageGadget(2, 100, 90, 80, 30, 0)
		SLIDER::Create(0, 2, #EventSLIDER, @MyCallBack(), #True)
		ButtonImageGadget(3, 10, 201, 60, 130, 0)
		SLIDER::Create(0, 3, #EventSLIDER, @MyCallBack())
		ButtonImageGadget(4, 110, 201, 60, 130, 0)
		
		Define THEME.SLIDER::_SLIDERTHEME_
		THEME\FontName         = "Akashi"
		THEME\OFFFrameColor1   = $FF1AEAEF
		THEME\OFFFrameColor2   = $FF329C9E
		THEME\OFFBorderColor1  = $FF36101F
		THEME\OFFBorderColor2  = $FFAB6C86
		THEME\OFFMainColor1    = $FF0797F9
		THEME\OFFMainColor2    = $FF4D6A7F
		THEME\OFFFontColor     = $FFFFFFFF
		THEME\ONFrameColor1    = $FF391E1C
		THEME\ONFrameColor2    = $FF52A2AB
		THEME\ONBorderColor1   = $FF406038
		THEME\ONBorderColor2   = $FF55A842
		THEME\ONMainColor1     = $FFE917D6
		THEME\ONMainColor2     = $FF4A2A47
		THEME\ONFontColor      = $FFFFFFFF
		THEME\TextOn$          = "AN"
		THEME\TextOff$         = "AUS"
		THEME\SLIDERKnobColor1 = $800D21F0
		THEME\SLIDERKnobColor2 = $809DA1C9
		THEME\SLIDERMainColor1 = $8029E3E7
		THEME\SLIDERMainColor2 = $801022DF
		
		SLIDER::Create(0, 4, #EventSLIDER, @MyCallBack(), #True, #True, @THEME)
		THEME\TextOn$          = ""
		THEME\TextOff$         = ""
		ButtonImageGadget(5, 10, 340, 180, 80, 0)
		SLIDER::Create(0, 5, #EventSLIDER, @MyCallBack(), 0, 0, @THEME)
		Repeat
			Event = WaitWindowEvent()
		Until Event = #PB_Event_CloseWindow
		SLIDER::Remove()
	EndIf
	
CompilerEndIf
Last edited by HeX0R on Sun Oct 01, 2023 7:06 pm, edited 7 times in total.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: SliderGadget

Post by Mindphazer »

Nice !
But I'm not much of a graphic designer, so I'm afraid I can't help you ! :mrgreen:
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: SliderGadget

Post by BarryG »

Reminds me of this -> viewtopic.php?f=5&t=57013
Boulcat
User
User
Posts: 28
Joined: Fri Feb 28, 2020 11:51 am

Re: SliderGadget

Post by Boulcat »

Very nice On|Off button :)
Looks good by adding this, to be compatible with DPIaware compil option

Code: Select all

Width                = DesktopScaledX(GadgetWidth(Gadget))
Height               = DesktopScaledY(GadgetHeight(Gadget))
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: SliderGadget

Post by HeX0R »

BarryG wrote: Wed Aug 09, 2023 11:10 pm Reminds me of this -> viewtopic.php?f=5&t=57013
Damn, I wasn't aware of that
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: SliderGadget

Post by Kwai chang caine »

Really nice :shock:
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: SliderGadget

Post by Mesa »

morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: SliderGadget

Post by morosh »

Hello:
Thank you very much for your great job!!
would it be possible to have vertical slider also?

I tried myself with no great success :oops:

Best Regards
PureBasic: Surprisingly simple, diabolically powerful
Quin
Addict
Addict
Posts: 1127
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: SliderGadget

Post by Quin »

Seconding the suggestion for vertical sliders. Fantastic work here!
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: SliderGadget

Post by Andre »

Looks good, thank you :)
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: SliderGadget

Post by HeX0R »

vertical slider support added
morosh
Enthusiast
Enthusiast
Posts: 329
Joined: Wed Aug 03, 2011 4:52 am
Location: Beirut, Lebanon

Re: SliderGadget

Post by morosh »

Awesome, Thank you very much
PureBasic: Surprisingly simple, diabolically powerful
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: SliderGadget

Post by ZX80 »

HeX0R,

thanks for your wonderful gadget. Can I ask you to change the logic a little? Now this seems a bit counterintuitive for me, sorry. I think that when the lever is in the up position (for vertical gadget) it should mean "ON". Accordingly, the text on a green background should be at the bottom. And when the lever is in the down position, the text on a red background opens from the top. And that means "OFF". The same applies to the horizontal gadget. The lever on the right = "ON" and the text on the green background is on the left. And vice versa, when the lever position = left, then the inscription on a red background is on the right. That is, "Off". Don't you think this behavior of your gadget is more logical? Sorry again.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: SliderGadget

Post by idle »

looks good thanks for sharing
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: SliderGadget

Post by Little John »

HeXOR, thanks for sharing!
ZX80 wrote: Don't you think this behavior of your gadget is more logical?
That's not a question of logic (which is something objective), but of your subjective personal intuition.
For me, the gadget is fine the way it is.
Post Reply