Page 1 of 1

Custom slider gadget

Posted: Sun Jul 17, 2005 1:45 pm
by Blade
Code updated For 5.20+

Hello, I've made the following slider gadget.
The idea is something similat to those used with paint programs:

Code: Select all

#COLOR_BTNFACE=15

#grad_paddingleft=5
#grad_paddingright=30

Declare UpdateGradient(i,g)

Procedure GetMouseX(gadget)
  GetCursorPos_(mouse.POINT)
  MapWindowPoints_(0, GadgetID(gadget), mouse, 1)
  ProcedureReturn mouse\x
EndProcedure

Procedure GetMouseY(gadget)
  GetCursorPos_(mouse.POINT)
  MapWindowPoints_(0, GadgetID(gadget), mouse, 1)
  ProcedureReturn mouse\y
EndProcedure

Procedure min(n1,n2)
  If n1<n2
    ProcedureReturn n1
  Else
    ProcedureReturn n2
  EndIf
EndProcedure

Procedure max(n1,n2)
  If n1>n2
    ProcedureReturn n1
  Else
    ProcedureReturn n2
  EndIf
EndProcedure

Procedure GradientPickerInit(i,g,x,y,w,h)
  alphaImg = CreateImage(i,w,h)
  alphaGad = ImageGadget(g,x,y,w,h,colorsImg)
  
  bgcolor=GetSysColor_(#COLOR_BTNFACE)
  StartDrawing(ImageOutput(i))
  Box(0,0,w,h,bgcolor)
  gradientwidth=w-#grad_paddingleft-#grad_paddingright
  For x=0 To gradientwidth
    LineXY(x+#grad_paddingleft, 0, x+#grad_paddingleft, h-9, RGB(x*255/gradientwidth,x*255/gradientwidth,x*255/gradientwidth))
  Next
  StopDrawing()
  SetGadgetState(g, alphaImg)
  UpdateGradient(i,g)
EndProcedure

Procedure UpdateGradient(i,g)
  alphaImg=ImageID(i)
  iwid=ImageWidth(i)
  value=min(max(GetMouseX(g),#grad_paddingleft), iwid-#grad_paddingright)
  bgcolor=GetSysColor_(#COLOR_BTNFACE)
  yy=ImageHeight(i)-9
  xx=value
  value-#grad_paddingleft
  value=min(value,iwid-#grad_paddingleft-#grad_paddingright)
  value=255*value/(iwid-#grad_paddingleft-#grad_paddingright)
  
  StartDrawing(ImageOutput(i))
  Box(0,yy,iwid,ImageHeight(i),bgcolor)
  LineXY(xx,   12,xx-5, 17,000)
  LineXY(xx-5, 17,xx+5, 17,000)
  LineXY(xx+5, 17,xx,   12,000)
  BackColor(RGB(Red(bgcolor),Green(bgcolor),Blue(bgcolor)))
  DrawText(iwid-#grad_paddingright+6,0,Str(value)+"  ")
  
  StopDrawing()
  
  SetGadgetState(g, alphaImg)
  ProcedureReturn value
EndProcedure

#i_alpha1=0
#g_alpha1=0

hTWindow=OpenWindow(1,510,0,128,100,"test",#PB_Window_SystemMenu|#PB_Window_MinimizeGadget)


GradientPickerInit(#i_alpha1,#g_alpha1, 0, 50, 120, 20)

Repeat
  wwe = WaitWindowEvent()
  egid = EventGadget()
  Select wwe:
    Case #PB_Event_CloseWindow
      End
    Case $332c
      Select egid
        Case #g_alpha1
          v=UpdateGradient(#i_alpha1,#g_alpha1)
      EndSelect
  EndSelect
ForEver
Despite I've posted this code in Tricks 'n' Tips, I still ask you if is there a better way to do it.
I'm looking with a better integration with the other gadgets, ability to create many instances etc.
Great would be a cross platform compatibility too.
Perhaps should exist a library to ceate cross compatible custom gadtets?

Posted: Sun Jul 17, 2005 6:49 pm
by einander
Not better, but different:

Code: Select all

;Draw Palette and pick color
;by einander
;PB 3.93 

Procedure Blend (RGB1,RGB2,Alpha)
    r1=Red(RGB1):g1=Green(RGB1):b1=Blue(RGB1)
    r2=Red(RGB2):g2=Green(RGB2):b2=Blue(RGB2)
    Red = r2 + ((((r1 - r2) * 256 / 255) * Alpha) / 256)
    Green= g2 + ((((g1 - g2) * 256 / 255) * Alpha) / 256)
    Blue = b2 + ((((b1 - b2) * 256 / 255) * Alpha) / 256)
    ProcedureReturn RGB(Red,Green,Blue)
EndProcedure 

hWnd = OpenWindow(0,0, 0,0,0 , #WS_OVERLAPPEDWINDOW | #WS_MAXIMIZE , "Palette and Color Picker")  
StartDrawing(WindowOutput())  
Box(0,0,WindowWidth(),WindowHeight(),0)
X=200:Y=100:X1=X+512:y1=Y+256
yy=Y
For j=0 To 255
    c1=Blend(RGB(0,0,255),RGB(255,255,0),j)
    c2=Blend(RGB(0,255,0),RGB(255,0,255),j)
    c3=Blend(RGB(255,0,0),RGB(0,255,255),j)
    xx=X
    For Alpha=0 To 255
        Plot(xx,yy,Blend(c2,c3,Alpha))
        Plot(xx+256,yy,Blend(c1,c2,Alpha))
        xx+1
    Next
    yy+1
Next

Repeat 
    If  PtInRegion_(CreateRectRgn_(X,Y,X1,y1),WindowMouseX(),WindowMouseY()) 
        Color=Point(WindowMouseX(),WindowMouseY())
        Circle(100,200,100,Color)
        Locate(10,80)
        DrawText("Red: "+Str(Red(Color))+"   Green: "+Str(Green(Color))+"   Blue: "+Str(Blue(Color))+"        ")
    EndIf
Until WaitWindowEvent()= #PB_Event_CloseWindow 
End