using System; //using System.Drawing; using Gwen.Input; namespace Gwen.Control { /// /// Vertical scrollbar. /// public class VerticalScrollBar : ScrollBar { /// /// Bar size (in pixels). /// public override int BarSize { get { return m_Bar.Height; } set { m_Bar.Height = value; } } /// /// Bar position (in pixels). /// public override int BarPos { get { return m_Bar.Y - Width; } } /// /// Button size (in pixels). /// public override int ButtonSize { get { return Width; } } /// /// Initializes a new instance of the class. /// /// Parent control. public VerticalScrollBar(Base parent) : base(parent) { m_Bar.IsVertical = true; m_ScrollButton[0].SetDirectionUp(); m_ScrollButton[0].Clicked += NudgeUp; m_ScrollButton[1].SetDirectionDown(); m_ScrollButton[1].Clicked += NudgeDown; 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].Height = Width; m_ScrollButton[0].Dock = Pos.Top; m_ScrollButton[1].Height = Width; m_ScrollButton[1].Dock = Pos.Bottom; m_Bar.Width = ButtonSize; m_Bar.Padding = new Padding(0, ButtonSize, 0, ButtonSize); float barHeight = 0.0f; if (m_ContentSize > 0.0f) barHeight = (m_ViewableContentSize/m_ContentSize)*(Height - (ButtonSize*2)); if (barHeight < ButtonSize*0.5f) barHeight = (int) (ButtonSize*0.5f); m_Bar.Height = (int) (barHeight); m_Bar.IsHidden = Height - (ButtonSize*2) <= barHeight; //Based on our last scroll amount, produce a position for the bar if (!m_Bar.IsHeld) { SetScrollAmount(ScrollAmount, true); } } public virtual void NudgeUp(Base control) { if (!IsDisabled) SetScrollAmount(ScrollAmount - NudgeAmount, true); } public virtual void NudgeDown(Base control) { if (!IsDisabled) SetScrollAmount(ScrollAmount + NudgeAmount, true); } public override void ScrollToTop() { SetScrollAmount(0, true); } public override void ScrollToBottom() { 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.Y < m_Bar.Y) NudgeUp(this); else if (clickPos.Y > m_Bar.Y + m_Bar.Height) NudgeDown(this); m_Depressed = false; InputHandler.MouseFocus = null; } } protected override float CalculateScrolledAmount() { return (float)(m_Bar.Y - ButtonSize) / (Height - m_Bar.Height - (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 newY = (int)(ButtonSize + (value * ((Height - m_Bar.Height) - (ButtonSize * 2)))); m_Bar.MoveTo(m_Bar.X, newY); } return true; } /// /// Handler for the BarMoved event. /// /// The control. protected override void OnBarMoved(Base control) { if (m_Bar.IsHeld) { SetScrollAmount(CalculateScrolledAmount(), false); base.OnBarMoved(control); } else InvalidateParent(); } } }