#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2010 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using System.Collections.Generic;
using Nuclex.Input;
using Nuclex.UserInterface.Input;
namespace Nuclex.UserInterface.Controls.Desktop {
  /// Base class for a slider that can be moved using the mouse
  /// 
  ///   Implements the common functionality for a slider moving either the direction
  ///   of the X or the Y axis (but not both). Derive any scroll bar-like controls
  ///   from this class to simplify their implementation.
  /// 
  public abstract class SliderControl : Control {
    /// Triggered when the slider has been moved
    public event EventHandler Moved;
    /// Initializes a new slider control
    public SliderControl() {
      this.ThumbPosition = 0.0f;
      this.ThumbSize = 1.0f;
    }
    /// whether the mouse is currently hovering over the thumb
    public bool MouseOverThumb {
      get { return this.mouseOverThumb; }
    }
    /// Whether the pressable control is in the depressed state
    public virtual bool ThumbDepressed {
      get {
        return
          this.pressedDown &&
          this.mouseOverThumb;
      }
    }
    /// Called when a mouse button has been pressed down
    /// Index of the button that has been pressed
    protected override void OnMousePressed(MouseButtons button) {
      if(button == MouseButtons.Left) {
        RectangleF thumbRegion = GetThumbRegion();
        if(thumbRegion.Contains(this.pickupX, this.pickupY)) {
          this.pressedDown = true;
          
          this.pickupX -= thumbRegion.X;
          this.pickupY -= thumbRegion.Y;
        }
      }
    }
    /// Called when a mouse button has been released again
    /// Index of the button that has been released
    protected override void OnMouseReleased(MouseButtons button) {
      if(button == MouseButtons.Left) {
        this.pressedDown = false;
      }
    }
    /// Called when the mouse position is updated
    /// X coordinate of the mouse cursor on the control
    /// Y coordinate of the mouse cursor on the control
    protected override void OnMouseMoved(float x, float y) {
      if(this.pressedDown) {
        
        //RectangleF bounds = GetAbsoluteBounds();
        MoveThumb(x - this.pickupX, y - this.pickupY);
        
      } else {
        this.pickupX = x;
        this.pickupY = y;
      }
      this.mouseOverThumb = GetThumbRegion().Contains(x, y);
    }
    /// 
    ///   Called when the mouse has left the control and is no longer hovering over it
    /// 
    protected override void OnMouseLeft() {
      this.mouseOverThumb = false;
    }
    /// Fires the slider's Moved event
    protected virtual void OnMoved() {
      if(Moved != null) {
        Moved(this, EventArgs.Empty);
      }
    }
    /// Moves the thumb to the specified location
    /// Location the thumb will be moved to
    protected abstract void MoveThumb(float x, float y);
    /// Obtains the region covered by the slider's thumb
    /// The region covered by the slider's thumb
    protected abstract RectangleF GetThumbRegion();
    /// Can be set by renderers to allow the control to locate its thumb
    public IThumbLocator ThumbLocator;
    /// Fraction of the slider filled by the thumb (0.0 .. 1.0)
    public float ThumbSize;
    /// Position of the thumb within the slider (0.0 .. 1.0)
    public float ThumbPosition;
    /// Whether the mouse cursor is hovering over the thumb
    private bool mouseOverThumb;
    /// Whether the slider's thumb is currently in the depressed state
    private bool pressedDown;
    /// X coordinate at which the thumb was picked up
    private float pickupX;
    /// Y coordinate at which the thumb was picked up
    private float pickupY;
  }
} // namespace Nuclex.UserInterface.Controls.Desktop