unit dgui_sliders;

{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}

interface

uses
  deng,
  dgui3;

type
PHorSlider = ^THorSlider;
THorSlider = object(TWidget)
private
  FState: TGUISliderState;
protected
  procedure DoRender; virtual;
  procedure DoUpdate; virtual;
  function DoOnWindowEvent(Ev: PWindowEvent): Boolean; virtual;
public
  constructor Init(aParent: PWidget);
  function GetValue: Single;
end;

implementation

procedure THorSlider.DoRender;
begin
  FState.Hover := Style^.HSliderIsPointInThumb(@Self, @FState, Eng.GetMouse);
  Style^.HSliderRender(@Self, @FState);
end;

procedure THorSlider.DoUpdate;
var
  OldValue: Single;
begin
  if FState.Dragging then begin
    OldValue := FState.Value;
    FState.Value := Style^.HSliderPointToValue(@Self, @FState, Eng.GetMouse);
    if (OldValue <> FState.Value) then
      Callback^.Call;
  end;
end;

function THorSlider.DoOnWindowEvent(Ev: PWindowEvent): Boolean;
begin
  if Ev^.IsLeftClickDown then begin
    if Style^.HSliderIsPointInThumb(@Self, @FState, Eng.GetMouse) then begin
      FState.Dragging := True;
      Exit(True);
    end;
  end else if Ev^.IsLeftClickUp then begin
    FState.Dragging := False;
  end;
  Exit(False);
end;

constructor THorSlider.Init(aParent: PWidget);
begin
  inherited Init(STYLE_DEFAULT, aParent);
  FState.Value := 0.5;
  FState.Hover := False;
  FState.Dragging := False;
end;

function THorSlider.GetValue: Single;
begin
  Exit(FState.Value);
end;

end.
