using System; using System.Drawing; using Gwen.Input; namespace Gwen.Control { /// /// Horizontal scrollbar. /// public class HorizontalScrollBar : ScrollBar { /// /// Bar size (in pixels). /// public override int BarSize { get { return m_Bar.Width; } set { m_Bar.Width = value; } } /// /// Bar position (in pixels). /// public override int BarPos { get { return m_Bar.X - Height; } } /// /// Indicates whether the bar is horizontal. /// public override bool IsHorizontal { get { return true; } } /// /// Button size (in pixels). /// public override int ButtonSize { get { return Height; } } /// /// Initializes a new instance of the class. /// /// Parent control. public HorizontalScrollBar(Base parent) : base(parent) { m_Bar.IsHorizontal = true; m_ScrollButton[0].SetDirectionLeft(); m_ScrollButton[0].Clicked += NudgeLeft; m_ScrollButton[1].SetDirectionRight(); m_ScrollButton[1].Clicked += NudgeRight; m_Bar.Dragged += OnBarMoved; } /// /// Lays out the control's interior according to alignment, padding, dock etc. /// /// Skin to use. protected override void Layout(Skin.Base skin) { base.Layout(skin); m_ScrollButton[0].Width = Height; m_ScrollButton[0].Dock = Pos.Left; m_ScrollButton[1].Width = Height; m_ScrollButton[1].Dock = Pos.Right; m_Bar.Height = ButtonSize; m_Bar.Padding = new Padding(ButtonSize, 0, ButtonSize, 0); float barWidth = (m_ViewableContentSize / m_ContentSize) * (Width - (ButtonSize * 2)); if (barWidth < ButtonSize * 0.5f) barWidth = (int)(ButtonSize * 0.5f); m_Bar.Width = (int)(barWidth); m_Bar.IsHidden = Width - (ButtonSize * 2) <= barWidth; //Based on our last scroll amount, produce a position for the bar if (!m_Bar.IsHeld) { SetScrollAmount(ScrollAmount, true); } } public void NudgeLeft(Base control) { if (!IsDisabled) SetScrollAmount(ScrollAmount - NudgeAmount, true); } public void NudgeRight(Base control) { if (!IsDisabled) SetScrollAmount(ScrollAmount + NudgeAmount, true); } public override void ScrollToLeft() { SetScrollAmount(0, true); } public override void ScrollToRight() { SetScrollAmount(1, true); } public override float NudgeAmount { get { if (m_Depressed) return m_ViewableContentSize / m_ContentSize; else return base.NudgeAmount; } set { base.NudgeAmount = value; } } /// /// 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) { if (down) { m_Depressed = true; InputHandler.MouseFocus = this; } else { Point clickPos = CanvasPosToLocal(new Point(x, y)); if (clickPos.X < m_Bar.X) NudgeLeft(this); else if (clickPos.X > m_Bar.X + m_Bar.Width) NudgeRight(this); m_Depressed = false; InputHandler.MouseFocus = null; } } protected override float CalculateScrolledAmount() { return (float)(m_Bar.X - ButtonSize) / (Width - m_Bar.Width - (ButtonSize * 2)); } /// /// Sets the scroll amount (0-1). /// /// Scroll amount. /// Determines whether the control should be updated. /// /// True if control state changed. /// public override bool SetScrollAmount(float value, bool forceUpdate = false) { value = Util.Clamp(value, 0, 1); if (!base.SetScrollAmount(value, forceUpdate)) return false; if (forceUpdate) { int newX = (int)(ButtonSize + (value * ((Width - m_Bar.Width) - (ButtonSize * 2)))); m_Bar.MoveTo(newX, m_Bar.Y); } return true; } /// /// Handler for the BarMoved event. /// /// Event source. protected override void OnBarMoved(Base control) { if (m_Bar.IsHeld) { SetScrollAmount(CalculateScrolledAmount(), false); base.OnBarMoved(control); } else InvalidateParent(); } } }