
#compact
'uses rtl32
'uses rtl64
uses ../GDIWindow/window


sys wx=640,wy=480,fx=10,fy=20,fw=400
Window "Knobs in Moveable Dialog Box",wx,wy,2
Font fx,fy,fw,"arial"

sys sxMouse,syMouse

class Knob
  '
  sys px,py,sx,sy     'position and sensing-size
  single sens,ang
  string txt
  single sang
  sys spx,spy,act
  '
  method zone() as sys
  if sxMouse>=px-sx and sxMouse<=px+sx then
    if syMouse>=py-sy and syMouse<=py+sy then
      return 1
    end if
  end if
  end method
  '
  method pos() as sys
  if MouseButton=1 then
    if sxMouse>=px-sx and sxMouse<=px+sx then
    if syMouse>=py-sy and syMouse<=py+sy then
      ang=sang+(xMouse-sxMouse+yMouse-syMouse)*sens
      if ang>1 then ang=1
      if ang<0 then ang=0
      act=1
      return 1
    end if
    end if
  elseif MouseButton=0 then
    sang=ang
    spx=px
    spy=py
  end if
  act=0
  end method
  '
  method show() as sys
  if act then
    Oval px-sx, py-sy, sx*2, sy*2, 250,0,250
  else
    Oval px-sx, py-sy, sx*2, sy*2, 100,0,100
  end if
  Line px,py,px+sx*sin(ang*6),py-sy*cos(ang*6),2,200,200,200
  'Text px+sx+10,py-sy+(fy>>2),txt+" "+str(ang,2),255,255,255
  Text px+sx+10,py-sy+(fy>>2),txt+" "+str(ang*255,0),255,255,255
  end method
  '
  method trans(sys tx,ty)
  px=spx+tx : py=spy+ty
  end method

  method fvalue() as single
  return ang
  end method
  '
  method bvalue() as sys
  return 255*ang
  end method
  '
end class


class DialogBox
  sys px,py,sx,sy     'position and sensing-size
  string txt
  sys spx,spy,act
  '
  method zone() as sys
  if xMouse>=px-sx and xMouse<=px+sx then
    if yMouse>=py-sy and yMouse<=py+sy then
      return 1
    end if
  end if
  end method
  '
  method pos() as sys
  if MouseButton=1 then
    if xMouse>=px-sx and xMouse<=px+sx then
    if yMouse>=py-sy and yMouse<=py+sy then
      px=spx+xMouse-sxMouse
      py=spy+yMouse-syMouse
      if px+sx>wx then px=wx-sx
      if py+sy>wy then py=wy-sy
      if px<0 then px=0
      if py<0 then py=0
      act=1
      return 1
    end if
    end if
  elseif MouseButton=0 then
    spx=px : spy=py
  end if
  act=0
  end method
  '
  method show() as sys
  FillBox px,py,sx,sy,160,160,160
  Text px+5,py+5,txt,0,0,0 '255,255,255
  end method
  '
end class


DialogBox d1<=(50,50,200,200,"Background Colors")

knob c1<=(100,110,25,15,.012,.5,"red")
knob c2<=(100,160,25,15,.012,.3,"green")
knob c3<=(100,210,25,15,.012,.5,"blue")

sys p,q,tx,ty

While AscKey=0 and WinExit=0
ClsColor c1.bvalue,c2.bvalue,c3.bvalue
if mouseButton=0 then
  sxMouse=xMouse
  syMouse=yMouse
  q=0
end if

p=d1.zone() or q

if p then
  if q then
    p=0
  else
    p=c1.zone()+c2.zone()+c3.zone()
  end if
  if p=1 or MouseButton=0 then
    c1.pos
    c2.pos
    c3.pos
    if MouseButton=0 then d1.pos
  else 'p=0 and mousebutton=1
    q=1 'inhibit child control activity
    d1.pos
    tx=d1.px-d1.spx
    ty=d1.py-d1.spy
    c1.trans tx,ty
    c2.trans tx,ty
    c3.trans tx,ty
  end if
end if
d1.show
c1.show
c2.show
c3.show

FlipBuffer
if MouseButton then sleep 10 else sleep 50
Wend
