Draggable thumbs

This commit is contained in:
Paul Chote
2010-12-31 23:59:23 +13:00
parent d98e09e096
commit b948b9d2b7

View File

@@ -18,14 +18,13 @@ namespace OpenRA.Widgets
{ {
public readonly string Background = "dialog3"; public readonly string Background = "dialog3";
public readonly int ScrollbarWidth = 24; public readonly int ScrollbarWidth = 24;
public readonly float ScrollVelocity = 4f; public readonly float ScrollVelocity = 4f;
public readonly int HeaderHeight = 0;
public int ContentHeight = 0; public int ContentHeight = 0;
float ListOffset = 0; float ListOffset = 0;
bool UpPressed = false; bool UpPressed = false;
bool DownPressed = false; bool DownPressed = false;
bool ThumbPressed = false;
Rectangle upButtonRect; Rectangle upButtonRect;
Rectangle downButtonRect; Rectangle downButtonRect;
Rectangle backgroundRect; Rectangle backgroundRect;
@@ -43,8 +42,9 @@ namespace OpenRA.Widgets
backgroundRect = other.backgroundRect; backgroundRect = other.backgroundRect;
thumbRect = other.thumbRect; thumbRect = other.thumbRect;
UpPressed = other.UpPressed; UpPressed = other.UpPressed;
DownPressed = other.DownPressed; DownPressed = other.DownPressed;
ThumbPressed = other.ThumbPressed;
} }
public override void DrawInner( WorldRenderer wr ) {} public override void DrawInner( WorldRenderer wr ) {}
@@ -53,7 +53,6 @@ namespace OpenRA.Widgets
if (!IsVisible()) if (!IsVisible())
return; return;
// Height/ContentHeight*Height
var ScrollbarHeight = RenderBounds.Height - 2 * ScrollbarWidth; var ScrollbarHeight = RenderBounds.Height - 2 * ScrollbarWidth;
var thumbHeight = ContentHeight == 0 ? 0 : (int)(ScrollbarHeight*Math.Min(RenderBounds.Height*1f/ContentHeight, 1f)); var thumbHeight = ContentHeight == 0 ? 0 : (int)(ScrollbarHeight*Math.Min(RenderBounds.Height*1f/ContentHeight, 1f));
@@ -85,7 +84,7 @@ namespace OpenRA.Widgets
WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", "down_arrow"), WidgetUtils.DrawRGBA(ChromeProvider.GetImage("scrollbar", "down_arrow"),
new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset)); new float2(downButtonRect.Left + downOffset, downButtonRect.Top + downOffset));
Game.Renderer.EnableScissor(backgroundRect.X+1, backgroundRect.Y + HeaderHeight+1, backgroundRect.Width-2, backgroundRect.Height - HeaderHeight-2); Game.Renderer.EnableScissor(backgroundRect.X + 1, backgroundRect.Y + 1, backgroundRect.Width - 2, backgroundRect.Height - 2);
foreach (var child in Children) foreach (var child in Children)
child.Draw( wr ); child.Draw( wr );
@@ -104,13 +103,13 @@ namespace OpenRA.Widgets
{ {
if (UpPressed && ListOffset <= 0) ListOffset += ScrollVelocity; if (UpPressed && ListOffset <= 0) ListOffset += ScrollVelocity;
if (DownPressed) ListOffset -= ScrollVelocity; if (DownPressed) ListOffset -= ScrollVelocity;
ListOffset = Math.Min(0,Math.Max(RenderBounds.Height - ContentHeight, ListOffset)); ListOffset = Math.Min(0,Math.Max(RenderBounds.Height - ContentHeight, ListOffset));
} }
public override bool LoseFocus (MouseInput mi) public override bool LoseFocus (MouseInput mi)
{ {
UpPressed = DownPressed = false; UpPressed = DownPressed = ThumbPressed = false;
return base.LoseFocus(mi); return base.LoseFocus(mi);
} }
@@ -122,13 +121,21 @@ namespace OpenRA.Widgets
if (!Focused) if (!Focused)
return false; return false;
UpPressed = upButtonRect.Contains(mi.Location.X,mi.Location.Y); UpPressed = upButtonRect.Contains(mi.Location.X, mi.Location.Y);
DownPressed = downButtonRect.Contains(mi.Location.X,mi.Location.Y); DownPressed = downButtonRect.Contains(mi.Location.X, mi.Location.Y);
ThumbPressed = thumbRect.Contains(mi.Location.X, mi.Location.Y);
if (Focused && mi.Event == MouseInputEvent.Up) if (Focused && mi.Event == MouseInputEvent.Up)
LoseFocus(mi); LoseFocus(mi);
return (UpPressed || DownPressed); if (ThumbPressed && mi.Event == MouseInputEvent.Move)
{
var ScrollbarHeight = RenderBounds.Height - 2 * ScrollbarWidth;
var thumbHeight = ContentHeight == 0 ? 0 : (int)(ScrollbarHeight*Math.Min(RenderBounds.Height*1f/ContentHeight, 1f));
ListOffset += (int)((Viewport.LastMousePos.Y - mi.Location.Y)*(ContentHeight - RenderBounds.Height)*1f/(ScrollbarHeight - thumbHeight));
ListOffset = Math.Min(0,Math.Max(RenderBounds.Height - ContentHeight, ListOffset));
}
return (UpPressed || DownPressed || ThumbPressed);
} }
public override Widget Clone() { return new ScrollPanelWidget(this); } public override Widget Clone() { return new ScrollPanelWidget(this); }