using System; using System.Drawing; using Gwen.ControlInternal; using Gwen.Input; namespace Gwen.Control { /// /// Base slider. /// public class Slider : Base { protected readonly SliderBar m_SliderBar; protected bool m_SnapToNotches; protected int m_NotchCount; protected float m_Value; protected float m_Min; protected float m_Max; /// /// Number of notches on the slider axis. /// public int NotchCount { get { return m_NotchCount; } set { m_NotchCount = value; } } /// /// Determines whether the slider should snap to notches. /// public bool SnapToNotches { get { return m_SnapToNotches; } set { m_SnapToNotches = value; } } /// /// Minimum value. /// public float Min { get { return m_Min; } set { SetRange(value, m_Max); } } /// /// Maximum value. /// public float Max { get { return m_Max; } set { SetRange(m_Min, value); } } /// /// Current value. /// public float Value { get { return m_Min + (m_Value * (m_Max - m_Min)); } set { if (value < m_Min) value = m_Min; if (value > m_Max) value = m_Max; // Normalize Value value = (value - m_Min) / (m_Max - m_Min); SetValueInternal(value); Redraw(); } } /// /// Invoked when the value has been changed. /// public event GwenEventHandler ValueChanged; /// /// Initializes a new instance of the class. /// /// Parent control. protected Slider(Base parent) : base(parent) { SetBounds(new Rectangle(0, 0, 32, 128)); m_SliderBar = new SliderBar(this); m_SliderBar.Dragged += OnMoved; m_Min = 0.0f; m_Max = 1.0f; m_SnapToNotches = false; m_NotchCount = 5; m_Value = 0.0f; KeyboardInputEnabled = true; IsTabable = true; } /// /// Handler for Right Arrow keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyRight(bool down) { if (down) Value = Value + 1; return true; } /// /// Handler for Up Arrow keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyUp(bool down) { if (down) Value = Value + 1; return true; } /// /// Handler for Left Arrow keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyLeft(bool down) { if (down) Value = Value - 1; return true; } /// /// Handler for Down Arrow keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyDown(bool down) { if (down) Value = Value - 1; return true; } /// /// Handler for Home keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyHome(bool down) { if (down) Value = m_Min; return true; } /// /// Handler for End keyboard event. /// /// Indicates whether the key was pressed or released. /// /// True if handled. /// protected override bool OnKeyEnd(bool down) { if (down) Value = m_Max; return true; } /// /// Handler invoked on mouse click (left) event. /// /// X coordinate. /// Y coordinate. /// If set to true mouse button is down. protected override void OnMouseClickedLeft(int x, int y, bool down) { } protected virtual void OnMoved(Base control) { SetValueInternal(CalculateValue()); } protected virtual float CalculateValue() { return 0; } protected virtual void UpdateBarFromValue() { } protected virtual void SetValueInternal(float val) { if (m_SnapToNotches) { val = (float)Math.Floor((val * m_NotchCount) + 0.5f); val /= m_NotchCount; } if (m_Value != val) { m_Value = val; if (ValueChanged != null) ValueChanged.Invoke(this); } UpdateBarFromValue(); } /// /// Sets the value range. /// /// Minimum value. /// Maximum value. public void SetRange(float min, float max) { m_Min = min; m_Max = max; } /// /// Renders the focus overlay. /// /// Skin to use. protected override void RenderFocus(Skin.Base skin) { if (InputHandler.KeyboardFocus != this) return; if (!IsTabable) return; skin.DrawKeyboardHighlight(this, RenderBounds, 0); } } }